-- The visible text of an HTML part must be what a browser renders: the tree -- is built as the HTML Standard says (implied end tags, text in the head and -- after the last tag, no character references in tag names) context("HTML tree building", function() local rspamd_parsers = require "rspamd_parsers" local rspamd_task = require "rspamd_task" local rspamd_config = rspamd_config local function visible(html) return tostring(rspamd_parsers.parse_html(html)) end -- The cases that give another text, one per line; empty if all are right local function mismatches(cases) local bad = {} for _, c in ipairs(cases) do local got = visible(c[1]) if got ~= c[2] then bad[#bad + 1] = string.format('%q: expected %q, got %q', c[1], c[2], got) end end return table.concat(bad, '\n') end test('text after the last tag is shown', function() assert_equal('', mismatches({ { '

one

TAIL', 'one\nTAIL' }, { '

one

TAIL', 'one\nTAIL' }, { '

one

\nTAIL\n', 'one\nTAIL' }, { '

one

TAIL', 'one\nTAIL' }, { '
one
TAIL', 'one\nTAIL' }, { '
one
TAIL', 'one\nTAIL' }, { '
one
TAIL', 'one\nTAIL' }, { '
one
TAIL

two

', 'one\nTAIL\ntwo\n' }, })) end) test('text after the end of the body goes into the body, as in a browser', function() assert_equal('', mismatches({ { 'HIDTAIL', '' }, { '
HIDTAIL', '' }, { 'HIDTAIL', '' }, })) end) test('text in the head is shown', function() assert_equal('', mismatches({ { 'HEADTXTbody', 'HEADTXTbody' }, { ' T HEADTXT', 'HEADTXT' }, { 'HEADTXT', 'HEADTXT' }, { 'HEADTXT', 'HEADTXT' }, })) end) test('head elements and whitespace stay out of the text', function() assert_equal('', mismatches({ { 'Tbody', 'body' }, { ' \n T\n\nbody', 'body' }, { 'body', 'body' }, { 'xTy', 'xy' }, { 'T' .. 'body', 'body' }, })) end) test('a start tag ends the open paragraph', function() assert_equal('', mismatches({ { '

HID

SHOWN', 'SHOWN\n' }, { '

HID

SHOWN
', 'SHOWN\n' }, { '

HID

SHOWN

', 'SHOWN\n' }, { '

HID

', 'SHOWN\n' }, { '

HID

SHOWN
', 'SHOWN\n' }, { '

HID


SHOWN', '\nSHOWN' }, { '

HID
SHOWN', 'SHOWN\n' }, { '

HID

SHOWN
', 'SHOWN\n' }, })) end) test('implied end tags of lists, headings, options and tables', function() assert_equal('', mismatches({ { '
  • HID
  • SHOWN', 'SHOWN\n' }, { '
    HID
    SHOWN', 'SHOWN\n' }, { '
    HID
    SHOWN', 'SHOWN\n' }, { '

    HID

    SHOWN', 'SHOWN\n' }, { '', 'SHOWN' }, { '', 'SHOWN' }, { '
    HIDSHOWN', 'SHOWN\n' }, { '
    HIDSHOWN', 'SHOWN\n' }, { '
    HID
    SHOWN', 'SHOWN\n' }, { '
    HID
    SHOWN', 'SHOWN\n' }, { '
  • HIDHID2
  • SHOWN', 'SHOWN' }, { '
  • HID
    HID2
  • SHOWN', 'SHOWN' }, })) end) test('nested elements keep the style of the one around them', function() assert_equal('', mismatches({ { '
    HID

    STILL HIDDEN

    ', '' }, { '

    HIDHID2

    ', '' }, { '

    HIDHID2', '' }, { '

    HID', '' }, { '

    HID

    HID2

    ', '' }, { '

    HID
    HID2
    ', '' }, { '
    HIDHID2
    HID3
    ', '' }, { '

    HID

    HID2

    ', '' }, { '

    • HID
      • HID2
      • HID3
    • HID4
    ', '' }, { '
  • HID
    • HID2
    ', '' }, { '
  • HID
  • HID2
  • ', '' }, { '', '' }, { '

    HID

    HID2

    ', '' }, -- A cell of an inner table does not end the one of the outer table { '
    HID
    HID2HID3
    HID4', '' }, })) end) test('an end tag of an element that was ended implicitly', function() assert_equal('', mismatches({ { '

    a

    b

    c', 'a\nb\nc' }, { '

    a

    c', 'a\nc' }, { 'a

    b

    cd', 'a\nb\nc\nd' }, { '

    • a
    • b
    c', 'ab\nc' }, { '

    HID

    SHOWN

    after', 'SHOWN\nafter' }, })) end) test('links are not nested', function() local res, task = rspamd_task.load_from_string( 'From: test@example.com\r\nContent-Type: text/html; charset=utf-8\r\n\r\n' .. 'https://example.org/x', rspamd_config) assert_true(res) task:process_message() local seen = {} for _, u in ipairs(task:get_urls() or {}) do seen[u:get_host()] = u end -- The urls belong to the task: read them before it is destroyed local first, second = seen['example.com'], seen['example.net'] local first_phished = first and first:is_phished() local first_visible = first and first:get_visible() local second_phished = second and second:is_phished() task:destroy() assert_not_nil(first) assert_true(first_phished) assert_equal('https://example.org/', first_visible) assert_not_nil(second) assert_false(second_phished) assert_equal( 'onetwothree', visible('onetwothree')) end) test('tag names are not decoded', function() assert_equal('', mismatches({ { 'xINSIDEy', 'xINSIDEy' }, { 'xINSIDEy', 'xINSIDEy' }, { 'xINSIDEy', 'xINSIDEy' }, { '
    xHID
    y', 'y' }, { 'xy', 'xy' }, { 'xy', 'xy' }, { 'xy', 'xy' }, })) end) end)