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

net.html: fix semantic inconsistencies of tag retrieving functions (#18558)

This commit is contained in:
Turiiya
2023-06-27 19:10:31 +02:00
committed by GitHub
parent 1bae9e4538
commit b2ca3ac089
4 changed files with 65 additions and 39 deletions

View File

@ -21,6 +21,11 @@ mut:
debug_file os.File
}
[params]
pub struct GetTagsOptions {
name string
}
[if debug_html ?]
fn (mut dom DocumentObjectModel) print_debug(data string) {
if data.len > 0 {
@ -163,8 +168,43 @@ fn (mut dom DocumentObjectModel) construct(tag_list []&Tag) {
dom.root = tag_list[0]
}
// 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 {
// get_root returns the root of the document.
pub fn (dom DocumentObjectModel) get_root() &Tag {
return dom.root
}
// get_tag retrieves all tags in the document that have the given tag name.
[deprecated: 'use get_tags instead']
pub fn (dom DocumentObjectModel) get_tag(name string) []&Tag {
return if name in dom.tag_type { dom.tag_type[name] } else { []&Tag{} }
}
// get_tags returns all tags stored in the document.
pub fn (dom DocumentObjectModel) get_tags(options GetTagsOptions) []&Tag {
if options.name != '' {
return if options.name in dom.tag_type { dom.tag_type[options.name] } else { []&Tag{} }
}
return dom.all_tags
}
// get_tags_by_class_name retrieves all tags recursively in the document root that have the given class name(s).
pub fn (dom DocumentObjectModel) get_tags_by_class_name(names ...string) []&Tag {
return dom.root.get_tags_by_class_name(...names)
}
// get_tag_by_attribute retrieves all tags in the document that have the given attribute name.
[deprecated: 'use get_tags_by_attribute instead']
pub fn (dom DocumentObjectModel) get_tag_by_attribute(name string) []&Tag {
return if name in dom.all_attributes { dom.all_attributes[name] } else { []&Tag{} }
}
// get_tags_by_attribute retrieves all tags in the document that have the given attribute name.
pub fn (dom DocumentObjectModel) get_tags_by_attribute(name string) []&Tag {
return if name in dom.all_attributes { dom.all_attributes[name] } else { []&Tag{} }
}
// get_tags_by_attribute_value retrieves all tags in the document that have the given attribute name and value.
pub fn (mut dom DocumentObjectModel) get_tags_by_attribute_value(name string, value string) []&Tag {
location := dom.where_is(value, name)
return if dom.tag_attributes[name].len > location {
dom.tag_attributes[name][location]
@ -173,27 +213,13 @@ pub fn (mut dom DocumentObjectModel) get_tag_by_attribute_value(name string, val
}
}
// 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{} }
}
// 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{} }
}
// get_root returns the root of the document.
pub fn (dom DocumentObjectModel) get_root() &Tag {
return dom.root
}
// get_tags returns all of the tags stored in the document.
pub fn (dom DocumentObjectModel) get_tags() []&Tag {
return dom.all_tags
}
// get_tags_by_class_name retrieves all the tags recursively in the document that has the given class name(s).
pub fn (dom DocumentObjectModel) get_tags_by_class_name(names ...string) []&Tag {
return dom.root.get_tags_by_class_name(...names)
// get_tag_by_attribute_value retrieves all tags in the document that have the given attribute name and value.
[deprecated: 'use get_tags_by_attribute_value instead']
pub fn (mut dom DocumentObjectModel) get_tag_by_attribute_value(name string, value string) []&Tag {
location := dom.where_is(value, name)
return if dom.tag_attributes[name].len > location {
dom.tag_attributes[name][location]
} else {
[]&Tag{}
}
}