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
|
|
|
fn generate_temp_html() string {
|
2020-12-09 22:08:15 +03:00
|
|
|
mut temp_html := strings.new_builder(200)
|
|
|
|
temp_html.write('<!doctype html><html><head><title>Giant String</title></head><body>')
|
2020-08-09 05:13:34 +03:00
|
|
|
for counter := 0; counter < 4; counter++ {
|
2020-12-09 22:08:15 +03:00
|
|
|
temp_html.write("<div id='name_$counter' ")
|
|
|
|
temp_html.write("class='several-$counter'>Look at $counter</div>")
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
2020-12-09 22:08:15 +03:00
|
|
|
temp_html.write('</body></html>')
|
|
|
|
return temp_html.str()
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_search_by_tag_type() {
|
2020-12-09 22:08:15 +03:00
|
|
|
dom := parse(generate_temp_html())
|
|
|
|
assert dom.get_tag('div').len == 4
|
|
|
|
assert dom.get_tag('head').len == 1
|
|
|
|
assert dom.get_tag('body').len == 1
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_search_by_attribute_value() {
|
2020-12-09 22:08:15 +03:00
|
|
|
mut dom := parse(generate_temp_html())
|
2020-08-09 05:13:34 +03:00
|
|
|
// println(temp_html)
|
|
|
|
print('Amount ')
|
2020-12-09 22:08:15 +03:00
|
|
|
println(dom.get_tag_by_attribute_value('id', 'name_0'))
|
|
|
|
assert dom.get_tag_by_attribute_value('id', 'name_0').len == 1
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_access_parent() {
|
2020-12-09 22:08:15 +03:00
|
|
|
mut dom := parse(generate_temp_html())
|
|
|
|
div_tags := dom.get_tag('div')
|
|
|
|
parent := div_tags[0].parent
|
|
|
|
assert parent != 0
|
2020-08-09 05:13:34 +03:00
|
|
|
for div_tag in div_tags {
|
2020-12-09 22:08:15 +03:00
|
|
|
assert div_tag.parent == parent
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_search_by_attributes() {
|
2020-12-09 22:08:15 +03:00
|
|
|
dom := parse(generate_temp_html())
|
|
|
|
assert dom.get_tag_by_attribute('id').len == 4
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_tags_used() {
|
2020-12-09 22:08:15 +03:00
|
|
|
dom := parse(generate_temp_html())
|
|
|
|
assert dom.get_tags().len == 9
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_access_tag_fields() {
|
2020-12-09 22:08:15 +03:00
|
|
|
dom := parse(generate_temp_html())
|
|
|
|
id_tags := dom.get_tag_by_attribute('id')
|
|
|
|
assert id_tags[0].name == 'div'
|
|
|
|
assert id_tags[1].attributes['class'] == 'several-1'
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|