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
|
|
|
enum CloseTagType {
|
|
|
|
in_name
|
|
|
|
new_tag
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:08:15 +03:00
|
|
|
// Tag holds the information of an HTML tag.
|
2021-02-13 17:52:01 +03:00
|
|
|
[heap]
|
2020-08-09 05:13:34 +03:00
|
|
|
pub struct Tag {
|
2020-08-18 02:08:58 +03:00
|
|
|
pub mut:
|
|
|
|
name string
|
|
|
|
content string
|
|
|
|
children []&Tag
|
2020-08-09 05:13:34 +03:00
|
|
|
attributes map[string]string // attributes will be like map[name]value
|
2020-08-18 02:08:58 +03:00
|
|
|
last_attribute string
|
2020-12-09 22:08:15 +03:00
|
|
|
parent &Tag = 0
|
2020-09-09 15:14:44 +03:00
|
|
|
position_in_parent int
|
2020-09-09 16:34:41 +03:00
|
|
|
closed bool
|
2020-08-09 05:13:34 +03:00
|
|
|
close_type CloseTagType = .in_name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut tag Tag) add_parent(t &Tag, position int) {
|
|
|
|
tag.position_in_parent = position
|
|
|
|
tag.parent = t
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut tag Tag) add_child(t &Tag) int {
|
2020-12-09 22:08:15 +03:00
|
|
|
tag.children << t
|
2020-08-09 05:13:34 +03:00
|
|
|
return tag.children.len
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:08:15 +03:00
|
|
|
// text returns the text contents of the tag.
|
2020-08-09 05:13:34 +03:00
|
|
|
pub fn (tag Tag) text() string {
|
2020-12-09 22:08:15 +03:00
|
|
|
if tag.name.len >= 2 && tag.name[..2] == 'br' {
|
2020-08-09 05:13:34 +03:00
|
|
|
return '\n'
|
|
|
|
}
|
2020-12-09 22:08:15 +03:00
|
|
|
mut text_str := strings.new_builder(200)
|
2021-02-22 14:18:11 +03:00
|
|
|
text_str.write_string(tag.content.replace('\n', ''))
|
2020-12-09 22:08:15 +03:00
|
|
|
for child in tag.children {
|
2021-02-22 14:18:11 +03:00
|
|
|
text_str.write_string(child.text())
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
2020-12-09 22:08:15 +03:00
|
|
|
return text_str.str()
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (tag &Tag) str() string {
|
2020-12-09 22:08:15 +03:00
|
|
|
mut html_str := strings.new_builder(200)
|
2021-02-22 14:18:11 +03:00
|
|
|
html_str.write_string('<$tag.name')
|
2020-12-09 22:08:15 +03:00
|
|
|
for key, value in tag.attributes {
|
2021-02-22 14:18:11 +03:00
|
|
|
html_str.write_string(' $key')
|
2020-08-09 05:13:34 +03:00
|
|
|
if value.len > 0 {
|
2021-02-22 14:18:11 +03:00
|
|
|
html_str.write_string('="$value"')
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 15:24:30 +03:00
|
|
|
html_str.write_string(if tag.closed && tag.close_type == .in_name { '/>' } else { '>' })
|
2021-02-22 14:18:11 +03:00
|
|
|
html_str.write_string(tag.content)
|
2020-08-09 05:13:34 +03:00
|
|
|
if tag.children.len > 0 {
|
2020-12-09 22:08:15 +03:00
|
|
|
for child in tag.children {
|
2021-02-22 14:18:11 +03:00
|
|
|
html_str.write_string(child.str())
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !tag.closed || tag.close_type == .new_tag {
|
2021-02-22 14:18:11 +03:00
|
|
|
html_str.write_string('</$tag.name>')
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
2020-12-09 22:08:15 +03:00
|
|
|
return html_str.str()
|
2020-08-09 05:13:34 +03:00
|
|
|
}
|
2022-01-09 17:07:12 +03:00
|
|
|
|
|
|
|
// get_tags retrieves all the child tags recursively in the tag that has the given tag name.
|
|
|
|
pub fn (tag &Tag) get_tags(name string) []&Tag {
|
|
|
|
mut res := []&Tag{}
|
|
|
|
for child in tag.children {
|
|
|
|
if child.name == name {
|
|
|
|
res << child
|
|
|
|
}
|
|
|
|
res << child.get_tags(name)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_tags_by_attribute retrieves all the child tags recursively in the tag that has the given attribute name.
|
|
|
|
pub fn (tag &Tag) get_tags_by_attribute(name string) []&Tag {
|
|
|
|
mut res := []&Tag{}
|
|
|
|
for child in tag.children {
|
|
|
|
if child.attributes[name] != '' {
|
|
|
|
res << child
|
|
|
|
}
|
|
|
|
res << child.get_tags_by_attribute(name)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_tags_by_attribute_value retrieves all the child tags recursively in the tag that has the given attribute name and value.
|
|
|
|
pub fn (tag &Tag) get_tags_by_attribute_value(name string, value string) []&Tag {
|
|
|
|
mut res := []&Tag{}
|
|
|
|
for child in tag.children {
|
|
|
|
if child.attributes[name] == value {
|
|
|
|
res << child
|
|
|
|
}
|
|
|
|
res << child.get_tags_by_attribute_value(name, value)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|