2020-08-09 04:13:34 +02:00
|
|
|
module html
|
|
|
|
|
2020-12-09 20:08:15 +01:00
|
|
|
import strings
|
|
|
|
|
2020-08-09 04:13:34 +02:00
|
|
|
fn generate_temp_html() string {
|
2020-12-09 20:08:15 +01:00
|
|
|
mut temp_html := strings.new_builder(200)
|
2021-02-22 12:18:11 +01:00
|
|
|
temp_html.write_string('<!doctype html><html><head><title>Giant String</title></head><body>')
|
2020-08-09 04:13:34 +02:00
|
|
|
for counter := 0; counter < 4; counter++ {
|
2021-02-22 12:18:11 +01:00
|
|
|
temp_html.write_string("<div id='name_$counter' ")
|
|
|
|
temp_html.write_string("class='several-$counter'>Look at $counter</div>")
|
2020-08-09 04:13:34 +02:00
|
|
|
}
|
2021-02-22 12:18:11 +01:00
|
|
|
temp_html.write_string('</body></html>')
|
2020-12-09 20:08:15 +01:00
|
|
|
return temp_html.str()
|
2020-08-09 04:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_search_by_tag_type() {
|
2020-12-09 20:08:15 +01: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 04:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_search_by_attribute_value() {
|
2020-12-09 20:08:15 +01:00
|
|
|
mut dom := parse(generate_temp_html())
|
2020-08-09 04:13:34 +02:00
|
|
|
// println(temp_html)
|
|
|
|
print('Amount ')
|
2020-12-09 20:08:15 +01: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 04:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_access_parent() {
|
2020-12-09 20:08:15 +01:00
|
|
|
mut dom := parse(generate_temp_html())
|
|
|
|
div_tags := dom.get_tag('div')
|
|
|
|
parent := div_tags[0].parent
|
2022-05-20 17:30:16 +02:00
|
|
|
assert unsafe { parent != 0 }
|
2020-08-09 04:13:34 +02:00
|
|
|
for div_tag in div_tags {
|
2020-12-09 20:08:15 +01:00
|
|
|
assert div_tag.parent == parent
|
2020-08-09 04:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_search_by_attributes() {
|
2020-12-09 20:08:15 +01:00
|
|
|
dom := parse(generate_temp_html())
|
|
|
|
assert dom.get_tag_by_attribute('id').len == 4
|
2020-08-09 04:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_tags_used() {
|
2020-12-09 20:08:15 +01:00
|
|
|
dom := parse(generate_temp_html())
|
|
|
|
assert dom.get_tags().len == 9
|
2020-08-09 04:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_access_tag_fields() {
|
2020-12-09 20:08:15 +01: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 04:13:34 +02:00
|
|
|
}
|