| 1 | module html |
| 2 | |
| 3 | import strings |
| 4 | |
| 5 | fn test_parse_empty_string() { |
| 6 | mut parser := Parser{} |
| 7 | |
| 8 | parser.parse_html('') |
| 9 | |
| 10 | assert parser.tags.len == 0 |
| 11 | } |
| 12 | |
| 13 | fn test_parse_text() { |
| 14 | mut parser := Parser{} |
| 15 | text_content := 'test\nparse\ntext' |
| 16 | |
| 17 | parser.parse_html(text_content) |
| 18 | |
| 19 | assert parser.tags.len == 1 |
| 20 | assert parser.tags.first().text() == text_content |
| 21 | } |
| 22 | |
| 23 | fn test_parse_one_tag_with_text() { |
| 24 | mut parser := Parser{} |
| 25 | text_content := 'tag\nwith\ntext' |
| 26 | p_tag := '<p>${text_content}</p>' |
| 27 | |
| 28 | parser.parse_html(p_tag) |
| 29 | |
| 30 | assert parser.tags.first().text() == text_content |
| 31 | } |
| 32 | |
| 33 | fn test_split_parse() { |
| 34 | mut parser := Parser{} |
| 35 | parser.init() |
| 36 | parser.split_parse('<!doctype htm') |
| 37 | parser.split_parse('l public') |
| 38 | parser.split_parse('><html><he') |
| 39 | parser.split_parse('ad><t') |
| 40 | parser.split_parse('itle> Hum... ') |
| 41 | parser.split_parse('A Tit') |
| 42 | parser.split_parse('\nle</ti\ntle>') |
| 43 | parser.split_parse('</\nhead><body>\t\t\t<h3>') |
| 44 | parser.split_parse('Nice Test!</h3>') |
| 45 | parser.split_parse('</bo\n\n\ndy></html>') |
| 46 | parser.finalize() |
| 47 | assert parser.tags.len == 11 |
| 48 | assert parser.tags[3].content == ' Hum... A Tit\nle' |
| 49 | } |
| 50 | |
| 51 | fn test_giant_string() { |
| 52 | mut temp_html := strings.new_builder(200) |
| 53 | mut parser := Parser{} |
| 54 | temp_html.write_string('<!doctype html><html><head><title>Giant String</title></head><body>') |
| 55 | for counter := 0; counter < 2000; counter++ { |
| 56 | temp_html.write_string("<div id='name_${counter}' class='several-${counter}'>Look at ${counter}</div>") |
| 57 | } |
| 58 | temp_html.write_string('</body></html>') |
| 59 | parser.parse_html(temp_html.str()) |
| 60 | assert parser.tags.len == 4009 |
| 61 | } |
| 62 | |
| 63 | fn test_script_tag() { |
| 64 | mut parser := Parser{} |
| 65 | script_content := '\nvar googletag = googletag || {};\ngoogletag.cmd = googletag.cmd || [];if(3 > 5) {console.log("Quoted \'message\'");}\n' |
| 66 | temp_html := '<html><body><script>${script_content}</script></body></html>' |
| 67 | parser.parse_html(temp_html) |
| 68 | assert parser.tags[2].content.len == script_content.len |
| 69 | } |
| 70 | |