Debian Codegraph @

test/lua/unit/html.lua

raw
1 context("HTML processing", function()
2 local rspamd_util = require("rspamd_util")
3 local cases = {
4 -- Entities
5 { [[<html><body>.&#102;&#105;&#114;&#101;&#98;&#97;&#115;&#101;&#97;&#112;&#112;.&#99;&#111;&#109;</body></html>]],
6 [[.firebaseapp.com]] },
7 { [[
8 <?xml version="1.0" encoding="iso-8859-1"?>
9 <!DOCTYPE html
10 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
11 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
12 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
13 <head>
14 <title>
15 Wikibooks
16 </title>
17 </head>
18 <body>
19 <p>
20 Hello, world!
21
22 </p>
23 </body>
24 </html>]], 'Hello, world!\n' },
25 { [[
26 <!DOCTYPE html>
27 <html lang="en">
28 <head>
29 <meta charset="utf-8">
30 <title>title</title>
31 <link rel="stylesheet" href="style.css">
32 <script src="script.js"></script>
33 <style><!--
34 - -a -a -a -- --- -
35 --></head>
36 <body>
37 <!-- page content -->
38 Hello, world!
39 </body>
40 </html>
41 ]], 'Hello, world!' },
42 { [[
43 <html lang="en">
44 <head>
45 <meta charset="utf-8">
46 <title>title</title>
47 <link rel="stylesheet" href="style.css">
48 <script src="script.js"></script>
49 </head>
50 <body>
51 <!-- page content -->
52 Hello, world!<br>test</br><br>content</hr>more content<br>
53 <div>
54 content inside div
55 </div>
56 </body>
57 </html>
58 ]], 'Hello, world!\ntest\ncontentmore content\ncontent inside div\n' },
59 { [[
60 <html lang="en">
61 <head>
62 <meta charset="utf-8">
63 <title>title</title>
64 <link rel="stylesheet" href="style.css">
65 <script src="script.js"></script>
66 </head>
67 <body>
68 <!-- tabular content -->
69 <table>
70 content
71 </table>
72 <table>
73 <tr>
74 <th>heada</th>
75 <th>headb</th>
76 </tr>
77 <tr>
78 <td>data1</td>
79 <td>data2</td>
80 </tr>
81 </table>
82
83 </body>
84 </html>
85 ]], 'content\nheada headb\ndata1 data2\n' },
86 { [[
87 <html lang="en">
88 <head>
89 <meta charset="utf-8">
90 <title>title</title>
91 <link rel="stylesheet" href="style.css">
92 <script src="script.js"></script>
93 </head>
94 <body>
95 <!-- escape content -->
96 a&nbsp;b a &gt; b a &lt; b a &amp; b &apos;a &quot;a&quot;
97 </body>
98 </html>
99 ]], 'a b a > b a < b a & b \'a "a"' },
100 }
101
102 for i, c in ipairs(cases) do
103 test("Extract text from HTML " .. tostring(i), function()
104 local t = rspamd_util.parse_html(c[1])
105
106 assert_not_nil(t)
107 assert_equal(c[2], tostring(t), string.format("'%s' doesn't match with '%s'",
108 c[2], t))
109 end)
110 end
111
112 -- Test cases for new HTML tag API methods
113 local function parse_html_and_extract_tags(html_content, pool)
114 local rspamd_parsers = require("rspamd_parsers")
115
116 local parsed = rspamd_parsers.parse_html_content(html_content, pool)
117 local tags = {}
118
119 if parsed then
120 parsed:foreach_tag("any", function(tag, content_length, is_leaf)
121 table.insert(tags, tag)
122 return false
123 end)
124 end
125
126 return parsed, tags
127 end
128
129 test("HTML tag get_all_attributes basic test", function()
130 local rspamd_mempool = require("rspamd_mempool")
131 local pool = rspamd_mempool.create()
132
133 local html = [[<div class="test-class" id="test-id" style="color: red;" width="100">content</div>]]
134 local parsed, tags = parse_html_and_extract_tags(html, pool)
135
136 assert_not_nil(parsed)
137 assert_true(#tags > 0)
138
139 -- Find the div tag
140 local div_tag = nil
141 for _, tag in ipairs(tags) do
142 if tag:get_type() == "div" then
143 div_tag = tag
144 break
145 end
146 end
147
148 assert_not_nil(div_tag)
149
150 local attrs = div_tag:get_all_attributes()
151 assert_not_nil(attrs)
152
153 -- Check that we have the expected attributes
154 assert_equal("test-class", attrs["class"])
155 assert_equal("test-id", attrs["id"])
156 assert_equal("color: red;", attrs["style"])
157 assert_equal("100", attrs["width"])
158
159 pool:destroy()
160 end)
161
162 test("HTML tag has_attribute test", function()
163 local rspamd_mempool = require("rspamd_mempool")
164 local pool = rspamd_mempool.create()
165
166 local html = [[<img src="test.jpg" width="100" height="50" alt="Test image" hidden />]]
167 local parsed, tags = parse_html_and_extract_tags(html, pool)
168
169 assert_not_nil(parsed)
170
171 local img_tag = nil
172 for _, tag in ipairs(tags) do
173 if tag:get_type() == "img" then
174 img_tag = tag
175 break
176 end
177 end
178
179 assert_not_nil(img_tag)
180
181 -- Test existing attributes
182 assert_true(img_tag:has_attribute("src"))
183 assert_true(img_tag:has_attribute("width"))
184 assert_true(img_tag:has_attribute("height"))
185 assert_true(img_tag:has_attribute("alt"))
186 assert_true(img_tag:has_attribute("hidden"))
187
188 -- Test non-existing attributes
189 assert_false(img_tag:has_attribute("nonexistent"))
190 assert_false(img_tag:has_attribute("class"))
191 assert_false(img_tag:has_attribute(""))
192
193 pool:destroy()
194 end)
195
196 test("HTML tag get_numeric_attribute test", function()
197 local rspamd_mempool = require("rspamd_mempool")
198 local pool = rspamd_mempool.create()
199
200 local html = [[<div width="200" height="150" font-size="14" opacity="0.8" tabindex="5">content</div>]]
201 local parsed, tags = parse_html_and_extract_tags(html, pool)
202
203 assert_not_nil(parsed)
204
205 local div_tag = nil
206 for _, tag in ipairs(tags) do
207 if tag:get_type() == "div" then
208 div_tag = tag
209 break
210 end
211 end
212
213 assert_not_nil(div_tag)
214
215 -- Test numeric attributes
216 assert_equal(200, div_tag:get_numeric_attribute("width"))
217 assert_equal(150, div_tag:get_numeric_attribute("height"))
218 assert_equal(14, div_tag:get_numeric_attribute("font-size"))
219
220 -- Test opacity with floating-point tolerance
221 local opacity = div_tag:get_numeric_attribute("opacity")
222 assert_not_nil(opacity)
223 assert_true(math.abs(opacity - 0.8) < 0.01, string.format("Expected opacity ~0.8, got %f", opacity))
224
225 assert_equal(5, div_tag:get_numeric_attribute("tabindex"))
226
227 -- Test non-numeric attributes
228 assert_nil(div_tag:get_numeric_attribute("nonexistent"))
229
230 pool:destroy()
231 end)
232
233 test("HTML tag get_unknown_attributes test", function()
234 local rspamd_mempool = require("rspamd_mempool")
235 local pool = rspamd_mempool.create()
236
237 local html = [[<div class="known" data-track="analytics" unknown-attr="test-value" custom-id="12345">content</div>]]
238 local parsed, tags = parse_html_and_extract_tags(html, pool)
239
240 assert_not_nil(parsed)
241
242 local div_tag = nil
243 for _, tag in ipairs(tags) do
244 if tag:get_type() == "div" then
245 div_tag = tag
246 break
247 end
248 end
249
250 assert_not_nil(div_tag)
251
252 local unknown_attrs = div_tag:get_unknown_attributes()
253 assert_not_nil(unknown_attrs)
254
255 -- Should include unknown attributes but not known ones like "class"
256 assert_not_nil(unknown_attrs["unknown-attr"])
257 assert_equal("test-value", unknown_attrs["unknown-attr"])
258 assert_not_nil(unknown_attrs["custom-id"])
259 assert_equal("12345", unknown_attrs["custom-id"])
260
261 -- data-track should be recognized as a known attribute now
262 -- but if not, it would appear in unknown attributes
263
264 pool:destroy()
265 end)
266
267 test("HTML tag get_children test", function()
268 local rspamd_mempool = require("rspamd_mempool")
269 local pool = rspamd_mempool.create()
270
271 local html = [[
272 <div id="parent">
273 <p>First child</p>
274 <span>Second child</span>
275 <img src="test.jpg" />
276 </div>
277 ]]
278 local parsed, tags = parse_html_and_extract_tags(html, pool)
279
280 assert_not_nil(parsed)
281
282 local parent_div = nil
283 for _, tag in ipairs(tags) do
284 if tag:get_type() == "div" and tag:has_attribute("id") and tag:get_attribute("id") == "parent" then
285 parent_div = tag
286 break
287 end
288 end
289
290 assert_not_nil(parent_div)
291
292 local children = parent_div:get_children()
293 assert_not_nil(children)
294 assert_equal(3, #children)
295
296 -- Check child types
297 local child_types = {}
298 for _, child in ipairs(children) do
299 table.insert(child_types, child:get_type())
300 end
301
302 -- Should contain p, span, and img
303 local child_types_str = table.concat(child_types, ",")
304 assert_true(child_types_str:find("p") ~= nil)
305 assert_true(child_types_str:find("span") ~= nil)
306 assert_true(child_types_str:find("img") ~= nil)
307
308 pool:destroy()
309 end)
310
311 test("HTML tag get_attribute vs get_all_attributes consistency", function()
312 local rspamd_mempool = require("rspamd_mempool")
313 local pool = rspamd_mempool.create()
314
315 local html = [[<a href="https://example.com" class="link" target="_blank" title="Example Link">Link</a>]]
316 local parsed, tags = parse_html_and_extract_tags(html, pool)
317
318 assert_not_nil(parsed)
319
320 local a_tag = nil
321 for _, tag in ipairs(tags) do
322 if tag:get_type() == "a" then
323 a_tag = tag
324 break
325 end
326 end
327
328 assert_not_nil(a_tag)
329
330 local all_attrs = a_tag:get_all_attributes()
331
332 -- Test that individual get_attribute calls match get_all_attributes
333 for attr_name, attr_value in pairs(all_attrs) do
334 assert_equal(attr_value, a_tag:get_attribute(attr_name),
335 string.format("Attribute '%s' mismatch: get_attribute='%s', get_all_attributes='%s'",
336 attr_name, a_tag:get_attribute(attr_name) or "nil", attr_value))
337 end
338
339 -- Test specific expected attributes
340 assert_equal("https://example.com", a_tag:get_attribute("href"))
341 assert_equal("link", a_tag:get_attribute("class"))
342 assert_equal("_blank", a_tag:get_attribute("target"))
343 assert_equal("Example Link", a_tag:get_attribute("title"))
344
345 pool:destroy()
346 end)
347
348
349
350 test("HTML tag attribute edge cases", function()
351 local rspamd_mempool = require("rspamd_mempool")
352 local pool = rspamd_mempool.create()
353
354 local html = [[<div class="" hidden style=" " width="0" height="abc">content</div>]]
355 local parsed, tags = parse_html_and_extract_tags(html, pool)
356
357 assert_not_nil(parsed)
358
359 local div_tag = nil
360 for _, tag in ipairs(tags) do
361 if tag:get_type() == "div" then
362 div_tag = tag
363 break
364 end
365 end
366
367 assert_not_nil(div_tag)
368
369 -- Test empty attribute value
370 assert_true(div_tag:has_attribute("class"))
371 assert_equal("", div_tag:get_attribute("class"))
372
373 -- Test boolean attribute (hidden)
374 assert_true(div_tag:has_attribute("hidden"))
375
376 -- Test whitespace-only attribute
377 assert_true(div_tag:has_attribute("style"))
378 assert_equal(" ", div_tag:get_attribute("style"))
379
380 -- Test numeric attributes with edge cases
381 assert_equal(0, div_tag:get_numeric_attribute("width"))
382 assert_nil(div_tag:get_numeric_attribute("height")) -- "abc" is not numeric
383
384 -- Test non-existent attribute
385 assert_false(div_tag:has_attribute("nonexistent"))
386 assert_nil(div_tag:get_attribute("nonexistent"))
387 assert_nil(div_tag:get_numeric_attribute("nonexistent"))
388
389 pool:destroy()
390 end)
391
392 test("HTML tag complex nested structure", function()
393 local rspamd_mempool = require("rspamd_mempool")
394 local pool = rspamd_mempool.create()
395
396 local html = [[
397 <table cellpadding="5" cellspacing="2" border="1">
398 <tr>
399 <td align="center" valign="top" width="100">
400 <img src="image1.jpg" width="80" height="60" alt="Image 1" />
401 </td>
402 <td align="left" valign="middle">
403 <p font-size="12">Text content</p>
404 </td>
405 </tr>
406 </table>
407 ]]
408 local parsed, tags = parse_html_and_extract_tags(html, pool)
409
410 assert_not_nil(parsed)
411
412 -- Find table tag
413 local table_tag = nil
414 for _, tag in ipairs(tags) do
415 if tag:get_type() == "table" then
416 table_tag = tag
417 break
418 end
419 end
420
421 assert_not_nil(table_tag)
422
423 -- Test table attributes
424 assert_equal(5, table_tag:get_numeric_attribute("cellpadding"))
425 assert_equal(2, table_tag:get_numeric_attribute("cellspacing"))
426 assert_equal("1", table_tag:get_attribute("border"))
427
428 -- Test that table has children
429 local children = table_tag:get_children()
430 assert_not_nil(children)
431 assert_true(#children > 0)
432
433 -- Find img tag
434 local img_tag = nil
435 for _, tag in ipairs(tags) do
436 if tag:get_type() == "img" then
437 img_tag = tag
438 break
439 end
440 end
441
442 assert_not_nil(img_tag)
443
444 -- Test img attributes
445 assert_equal("image1.jpg", img_tag:get_attribute("src"))
446 assert_equal(80, img_tag:get_numeric_attribute("width"))
447 assert_equal(60, img_tag:get_numeric_attribute("height"))
448 assert_equal("Image 1", img_tag:get_attribute("alt"))
449
450 pool:destroy()
451 end)
452
453 test("HTML tag with mixed known and unknown attributes", function()
454 local rspamd_mempool = require("rspamd_mempool")
455 local pool = rspamd_mempool.create()
456
457 local html =
458 [[<div class="container" data-analytics="track" custom-attr="value" style="color: blue;" unknown123="test">content</div>]]
459 local parsed, tags = parse_html_and_extract_tags(html, pool)
460
461 assert_not_nil(parsed)
462
463 local div_tag = nil
464 for _, tag in ipairs(tags) do
465 if tag:get_type() == "div" then
466 div_tag = tag
467 break
468 end
469 end
470
471 assert_not_nil(div_tag)
472
473 local all_attrs = div_tag:get_all_attributes()
474 local unknown_attrs = div_tag:get_unknown_attributes()
475
476 -- All attributes should include both known and unknown
477 assert_not_nil(all_attrs["class"]) -- known
478 assert_not_nil(all_attrs["style"]) -- known
479 assert_not_nil(all_attrs["custom-attr"]) -- unknown
480 assert_not_nil(all_attrs["unknown123"]) -- unknown
481
482 -- Unknown attributes should only include unrecognized ones
483 assert_nil(unknown_attrs["class"]) -- known, shouldn't be here
484 assert_nil(unknown_attrs["style"]) -- known, shouldn't be here
485 assert_not_nil(unknown_attrs["custom-attr"]) -- unknown, should be here
486 assert_not_nil(unknown_attrs["unknown123"]) -- unknown, should be here
487
488 assert_equal("value", unknown_attrs["custom-attr"])
489 assert_equal("test", unknown_attrs["unknown123"])
490
491 pool:destroy()
492 end)
493 end)