2020-08-09 05:13:34 +03:00
|
|
|
module html
|
|
|
|
|
2020-12-09 22:08:15 +03:00
|
|
|
import strings
|
2020-08-09 05:13:34 +03:00
|
|
|
|
2023-02-03 01:32:03 +03:00
|
|
|
fn test_parse_empty_string() {
|
|
|
|
mut parser := Parser{}
|
|
|
|
|
|
|
|
parser.parse_html('')
|
|
|
|
|
|
|
|
assert parser.tags.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_text() {
|
|
|
|
mut parser := Parser{}
|
|
|
|
text_content := 'test\nparse\ntext'
|
|
|
|
|
|
|
|
parser.parse_html(text_content)
|
|
|
|
|
|
|
|
assert parser.tags.len == 1
|
|
|
|
assert parser.tags.first().text() == text_content
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_one_tag_with_text() {
|
|
|
|
mut parser := Parser{}
|
|
|
|
text_content := 'tag\nwith\ntext'
|
|
|
|
p_tag := '<p>${text_content}</p>'
|
|
|
|
|
|
|
|
parser.parse_html(p_tag)
|
|
|
|
|
|
|
|
assert parser.tags.first().text() == text_content
|
|
|
|
}
|
|
|
|
|
2020-08-09 05:13:34 +03:00
|
|
|
fn test_split_parse() {
|
|
|
|
mut parser := Parser{}
|
2020-12-09 22:08:15 +03:00
|
|
|
parser.init()
|
2020-08-09 05:13:34 +03:00
|
|
|
parser.split_parse('<!doctype htm')
|
|
|
|
parser.split_parse('l public')
|
|
|
|
parser.split_parse('><html><he')
|
|
|
|
parser.split_parse('ad><t')
|
|
|
|
parser.split_parse('itle> Hum... ')
|
|
|
|
parser.split_parse('A Tit')
|
|
|
|
parser.split_parse('\nle</ti\ntle>')
|
|
|
|
parser.split_parse('</\nhead><body>\t\t\t<h3>')
|
|
|
|
parser.split_parse('Nice Test!</h3>')
|
|
|
|
parser.split_parse('</bo\n\n\ndy></html>')
|
|
|
|
parser.finalize()
|
2020-12-09 22:08:15 +03:00
|
|
|
assert parser.tags.len == 11
|
|
|
|
assert parser.tags[3].content == ' Hum... A Tit\nle'
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_giant_string() {
|
2020-12-09 22:08:15 +03:00
|
|
|
mut temp_html := strings.new_builder(200)
|
|
|
|
mut parser := Parser{}
|
2021-02-22 14:18:11 +03:00
|
|
|
temp_html.write_string('<!doctype html><html><head><title>Giant String</title></head><body>')
|
2020-08-09 05:13:34 +03:00
|
|
|
for counter := 0; counter < 2000; counter++ {
|
2022-11-15 16:53:13 +03:00
|
|
|
temp_html.write_string("<div id='name_${counter}' class='several-${counter}'>Look at ${counter}</div>")
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
2021-02-22 14:18:11 +03:00
|
|
|
temp_html.write_string('</body></html>')
|
2020-12-09 22:08:15 +03:00
|
|
|
parser.parse_html(temp_html.str())
|
|
|
|
assert parser.tags.len == 4009
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_script_tag() {
|
|
|
|
mut parser := Parser{}
|
2023-05-07 03:55:02 +03:00
|
|
|
script_content := '\nvar googletag = googletag || {};\ngoogletag.cmd = googletag.cmd || [];if(3 > 5) {console.log("Quoted \'message\'");}\n'
|
2022-11-15 16:53:13 +03:00
|
|
|
temp_html := '<html><body><script>${script_content}</script></body></html>'
|
2020-12-09 22:08:15 +03:00
|
|
|
parser.parse_html(temp_html)
|
2023-02-03 01:32:03 +03:00
|
|
|
assert parser.tags[2].content.len == script_content.len
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|