1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net.html: polish module, update docs (#7193)

This commit is contained in:
Ned Palacios
2020-12-10 03:08:15 +08:00
committed by GitHub
parent 5fa1e403ec
commit b952bf2e6b
9 changed files with 302 additions and 446 deletions

View File

@ -2,6 +2,11 @@ module html
import os
// The W3C Document Object Model (DOM) is a platform and language-neutral
// interface that allows programs and scripts to dynamically access and
// update the content, structure, and style of a document.
//
// https://www.w3.org/TR/WD-DOM/introduction.html
pub struct DocumentObjectModel {
mut:
root &Tag
@ -25,24 +30,14 @@ fn (mut dom DocumentObjectModel) print_debug(data string) {
}
}
/*
fn (dom mut DocumentObjectModel) new_root(tag &Tag) {
mut new_tag := &Tag{} new_tag.name = "div"
new_tag.add_child(dom.root) new_tag.add_child(tag)
dom.root = new_tag
}
*/
[inline]
fn is_close_tag(tag &Tag) bool {
if tag.name.len > 0 {
return tag.name[0] == 47 // return if equals to /
}
return false
return tag.name.len > 0 && tag.name[0] == `/`
}
fn (mut dom DocumentObjectModel) where_is(item_name string, attribute_name string) int {
if !(attribute_name in dom.attributes) {
temp_array := []string{}
dom.attributes[attribute_name] = temp_array
if attribute_name !in dom.attributes {
dom.attributes[attribute_name] = []string{}
}
mut string_array := dom.attributes[attribute_name]
mut counter := 0
@ -58,10 +53,10 @@ fn (mut dom DocumentObjectModel) where_is(item_name string, attribute_name strin
}
fn (mut dom DocumentObjectModel) add_tag_attribute(tag &Tag) {
for attribute_name in tag.attributes.keys() {
for attribute_name, _ in tag.attributes {
attribute_value := tag.attributes[attribute_name]
location := dom.where_is(attribute_value, attribute_name)
if !(attribute_name in dom.tag_attributes) {
if attribute_name !in dom.tag_attributes {
dom.tag_attributes[attribute_name] = []
}
for {
@ -91,7 +86,7 @@ fn (mut dom DocumentObjectModel) add_tag_by_type(tag &Tag) {
fn (mut dom DocumentObjectModel) add_tag_by_attribute(tag &Tag) {
for attribute_name in tag.attributes.keys() {
if !(attribute_name in dom.all_attributes) {
if attribute_name !in dom.all_attributes {
dom.all_attributes[attribute_name] = [tag]
} else {
mut temp_array := dom.all_attributes[attribute_name]
@ -101,22 +96,10 @@ fn (mut dom DocumentObjectModel) add_tag_by_attribute(tag &Tag) {
}
}
fn compare_string(a string, b string) bool { // for some reason == doesn't work
if a.len != b.len {
return false
}
for i := 0; i < a.len; i++ {
if a[i] != b[i] {
return false
}
}
return true
}
fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.constructed = true
mut temp_map := map[string]int{}
mut temp_int := C.INT_MIN
mut temp_int := null_element
mut temp_string := ''
mut stack := Stack{}
dom.btree = BTree{}
@ -130,21 +113,16 @@ fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.print_debug(tag.str())
if is_close_tag(tag) {
temp_int = stack.peek()
temp_string = tag.name[1..tag.name.len] // print(temp_string + " != " + tag_list[temp_int].name + " >> ") // println(temp_string != tag_list[temp_int].name)
for !stack.is_null(temp_int) &&
!compare_string(temp_string, tag_list[temp_int].name) && !tag_list[temp_int].closed {
dom.print_debug(temp_string + ' >> ' + tag_list[temp_int].name + ' ' +
compare_string(temp_string, tag_list[temp_int].name).str())
temp_string = tag.name[1..]
for !is_null(temp_int) && temp_string != tag_list[temp_int].name && !tag_list[temp_int].closed {
dom.print_debug(temp_string + ' >> ' + tag_list[temp_int].name + ' ' + (temp_string ==
tag_list[temp_int].name).str())
stack.pop()
temp_int = stack.peek()
}
temp_int = stack.peek()
if !stack.is_null(temp_int) {
temp_int = stack.pop()
} else {
temp_int = root_index
}
if stack.is_null(temp_int) {
temp_int = if !is_null(temp_int) { stack.pop() } else { root_index }
if is_null(temp_int) {
stack.push(root_index)
}
dom.print_debug('Removed ' + temp_string + ' -- ' + tag_list[temp_int].name)
@ -154,7 +132,7 @@ fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.add_tag_by_type(tag)
dom.all_tags << tag
temp_int = stack.peek()
if !stack.is_null(temp_int) {
if !is_null(temp_int) {
dom.btree.move_pointer(temp_map[temp_int.str()])
temp_map[index.str()] = dom.btree.add_children(tag)
mut temp_tag := tag_list[temp_int]
@ -164,8 +142,7 @@ fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.print_debug("Added ${tag.name} as child of '" + tag_list[temp_int].name +
"' which now has ${dom.btree.get_children().len} childrens")
*/
dom.print_debug("Added $tag.name as child of '" + temp_tag.name +
"' which now has $temp_tag.get_children().len childrens")
dom.print_debug("Added $tag.name as child of '" + temp_tag.name + "' which now has $temp_tag.children.len childrens")
} else { // dom.new_root(tag)
stack.push(root_index)
}
@ -179,40 +156,40 @@ fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.root = tag_list[0]
}
pub fn (mut dom DocumentObjectModel) get_by_attribute_value(name string, value string) []&Tag {
// get_tag_by_attribute_value retrieves all the tags in the document that has the given attribute name and value.
pub fn (mut dom DocumentObjectModel) get_tag_by_attribute_value(name string, value string) []&Tag {
location := dom.where_is(value, name)
if dom.tag_attributes[name].len > location {
return dom.tag_attributes[name][location]
return if dom.tag_attributes[name].len > location {
dom.tag_attributes[name][location]
} else {
[]&Tag{}
}
return []&Tag{}
}
pub fn (dom DocumentObjectModel) get_by_tag(name string) []&Tag {
if name in dom.tag_type {
return dom.tag_type[name]
// get_tag retrieves all the tags in the document that has the given tag name.
pub fn (dom DocumentObjectModel) get_tag(name string) []&Tag {
return if name in dom.tag_type {
dom.tag_type[name]
} else {
[]&Tag{}
}
return []&Tag{}
}
pub fn (dom DocumentObjectModel) get_by_attribute(name string) []&Tag {
if name in dom.all_attributes {
return dom.all_attributes[name]
// get_tag_by_attribute retrieves all the tags in the document that has the given attribute name.
pub fn (dom DocumentObjectModel) get_tag_by_attribute(name string) []&Tag {
return if name in dom.all_attributes {
dom.all_attributes[name]
} else {
[]&Tag{}
}
return []&Tag{}
}
// get_root returns the root of the document.
pub fn (dom DocumentObjectModel) get_root() &Tag {
return dom.root
}
pub fn (dom DocumentObjectModel) get_all_tags() []&Tag {
// get_tags returns all of the tags stored in the document.
pub fn (dom DocumentObjectModel) get_tags() []&Tag {
return dom.all_tags
}
/*
pub fn (dom DocumentObjectModel) get_xpath() XPath {
return XPath{
dom: dom
}
}
*/