--[[
Presentational attributes (color, bgcolor, hidden) are hints that any author
declaration overrides, and `visibility` is an inherited property of its own that
a descendant can reset, unlike `display:none`.
]]
context("CSS presentational hints and visibility", function()
local rspamd_task = require "rspamd_task"
-- Text of an HTML part with the given stylesheet, body attributes and body
local function text_of(style, body_attrs, body)
local html = '
' .. body .. 'vis
'
local msg = "From: a@example.com\r\nSubject: t\r\nMIME-Version: 1.0\r\n" ..
"Content-Type: text/html; charset=utf-8\r\n\r\n" .. html .. "\r\n"
local res, task = rspamd_task.load_from_string(msg, rspamd_config)
assert(res, "failed to load message")
task:process_message()
local text = tostring(task:get_text_parts()[1]:get_content())
task:destroy()
return text
end
-- Each case: name, stylesheet, body attributes, body, whether TXT is hidden
local cases = {
-- Task 1: presentational attributes are weaker than author CSS
{ "hidden attribute alone hides", '', '', 'TXT
', true },
{ "hidden attribute loses to an inline display", '', '',
'TXT
', false },
{ "hidden attribute loses to a stylesheet display", '.s{display:block}', '',
'TXT
', false },
{ "hidden attribute loses to a stylesheet tag rule", 'div{display:block}', '',
'TXT
', false },
{ "hidden attribute loses to an important stylesheet display", '.s{display:block !important}', '',
'TXT
', false },
{ "hidden attribute does not undo an inline display:none", '', '',
'TXT
', true },
{ "hidden ancestor hides a block child", '', '',
'', true },
{ "hidden ancestor hides a child by tag", '', '',
'', true },
{ "ancestor display overrides its hidden attribute", '', '',
'TXT
', false },
{ "ancestor stylesheet display overrides its hidden attribute", '.s{display:block}', '',
'TXT
', false },
{ "display:none ancestor with hidden attribute hides a block child", '', '',
'', true },
{ "hidden attribute on a span hides it", '', '', 'TXT', true },
{ "font color attribute alone blanks text on a white body", '', 'bgcolor="#ffffff"',
'TXT', true },
{ "font color attribute loses to an inline color", '', 'bgcolor="#ffffff"',
'TXT', false },
{ "font color attribute loses to a stylesheet color", 'font{color:#333333}', 'bgcolor="#ffffff"',
'TXT', false },
{ "font color attribute loses to a class color", '.f{color:#333333}', 'bgcolor="#ffffff"',
'TXT', false },
{ "font color attribute loses to an important stylesheet color", 'font{color:#333333 !important}',
'bgcolor="#ffffff"', 'TXT', false },
{ "visible font color attribute is shown", '', 'bgcolor="#ffffff"',
'TXT', false },
{ "inline color blanks text over a font color attribute", '', 'bgcolor="#ffffff"',
'TXT', true },
{ "bgcolor attribute alone blanks matching text", '', '',
'TXT
', true },
{ "bgcolor attribute with a different font color is shown", '', '',
'TXT
', false },
{ "bgcolor attribute loses to an inline background", '', '',
'TXT
', true },
{ "bgcolor attribute loses to a stylesheet background", 'div{background-color:#000000}', '',
'TXT
', true },
{ "a child inherits a hinted color", '', 'bgcolor="#ffffff"',
'TXT', true },
{ "a child author color overrides an inherited hinted color", '', 'bgcolor="#ffffff"',
'TXT', false },
{ "a hinted color beats an inherited one", '', 'bgcolor="#ffffff"',
'TXT
', true },
{ "an author color of a parent does not beat a hint of a child", '.p{color:#333333}', 'bgcolor="#ffffff"',
'TXT
', true },
-- Task 2: visibility is inherited and can be reset by descendants
{ "visibility:hidden hides", '', '', 'TXT
', true },
{ "visibility:collapse hides", '', '', 'TXT
', true },
{ "visibility:visible alone shows", '', '', 'TXT
', false },
{ "visible child of a hidden parent is shown", '', '',
'TXT
', false },
{ "visibility:inherit does not re-show a child of a hidden parent", '', '',
'TXT
', true },
{ "child without visibility of a hidden parent is hidden", '', '',
'TXT
', true },
{ "text of the hidden parent itself stays hidden", '', '',
'HIDETXT
', false },
{ "visible child by stylesheet in a hidden parent is shown", '.v{visibility:visible}', '',
'TXT
', false },
{ "grandchild inherits the visibility of a re-shown child", '', '',
'', false },
{ "grandchild can hide again", '', '',
'',
true },
{ "important inline visibility:hidden beats a stylesheet visible", '.c{visibility:visible}', '',
'TXT
', true },
{ "important stylesheet visibility:hidden beats an inline visible", '.c{visibility:hidden !important}', '',
'TXT
', true },
{ "normal stylesheet visibility:hidden loses to an inline visible", '.c{visibility:hidden}', '',
'TXT
', false },
{ "important visible child of a hidden parent is shown", '.c{visibility:visible !important}', '',
'TXT
', false },
{ "display:none parent with a visible child stays hidden", '', '',
'TXT
', true },
{ "display:none with visibility:visible on one element stays hidden", '', '',
'TXT
', true },
{ "visibility:visible child of a hidden parent with display:block is shown", '', '',
'', false },
{ "wrapping the message in visibility:hidden and re-showing the content", '', '',
'', false },
}
for _, c in ipairs(cases) do
test(c[1], function()
local text = text_of(c[2], c[3], c[4])
assert_not_nil(text:find("vis", 1, true), "visible text is missing")
assert_equal(c[5], text:find("TXT", 1, true) == nil)
if c[4]:find("HIDE", 1, true) then
assert_nil(text:find("HIDE", 1, true), "parent text must stay hidden")
end
end)
end
end)