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