net.html #
net/html is an HTML Parser written in pure V.
Usage
import net.html
fn main() {
doc := html.parse('<html><body><h1 class="title">Hello world!</h1></body></html>')
tag := doc.get_tags(name: 'h1')[0] // <h1>Hello world!</h1>
println(tag.name) // h1
println(tag.content) // Hello world!
println(tag.attributes) // {'class':'title'}
println(tag.str()) // <h1 class="title">Hello world!</h1>
}
For tags with nested markup, tag.content preserves the inner HTML:
import net.html
fn main() {
doc := html.parse('<p>before <span>in between</span> after</p>')
tag := doc.get_tags(name: 'p')[0]
println(tag.content) // before <span>in between</span> after
println(tag.text()) // before in between after
}
Parser resource limits
Parsing rejects documents deeper than 256 elements or when aggregate Tag.content bytes rebuilt by normalization exceed min(64 MiB, 256 × input bytes). Content that needs no normalization is not counted. parse_checked and parse_file_checked return errors for these limits. Legacy parse and parse_file return an empty DOM, and dom.parse_error() exposes the reason. Parser.parse_html_checked provides the same error path for direct parser use.
More examples found on parser_test.v and html_test.v
Comments are omitted from the parsed tree. Text before and after comments remains in order, including text wrapped by hydration markers such as <!--[-->hello<!--]-->. Comments do not create empty text nodes or change the surrounding elements.
Calling get_tag_by_class_name without class names returns none; get_tags_by_class_name without class names returns an empty array.
Script and style elements preserve their raw bodies in tag.content and when serialized with tag.str(). Text following a closing script or style tag belongs to the surrounding document.
Document searches include the root element in name, class, and attribute matches. Class searches match all supplied class names and return elements in document order. Searches on a Tag inspect only its descendants.
Attribute searches match attributes by presence, including boolean attributes such as disabled and attributes with an empty value. Searching for an empty value matches only tags that have the attribute. Searches on a Tag recursively inspect its children.
Unterminated script and style elements retain their collected body when parsing ends.
Newlines inside tags separate names and attributes like spaces. Consecutive separators preserve a pending quoted attribute value.
Trailing text is retained when parsing ends, including text after a closing tag and text in an unclosed element. Text after a closing tag belongs to the surrounding element.
Attribute names are case insensitive. Quoted and unquoted attribute values preserve their case, and both forms populate class selectors. HTML whitespace separates attributes; whitespace around = is accepted. Empty attribute names are omitted.
fn parse #
fn parse(text string) DocumentObjectModel
parse parses and returns the DOM from the given text.
Note: this function converts tags to lowercase. E.g. <MyTag>content</MyTag> is parsed as <mytag>content</mytag>. If a parser resource limit is exceeded, it returns an empty DOM with an error available from parse_error().
fn parse_checked #
fn parse_checked(text string) !DocumentObjectModel
parse_checked parses the given text and returns an error if nesting exceeds 256 elements or aggregate rebuilt Tag.content bytes would exceed min(64 MiB, 256 times the input size).
fn parse_file #
fn parse_file(filename string) DocumentObjectModel
parse_file parses and returns the DOM from the contents of a file.
Note: this function converts tags to lowercase. E.g. <MyTag>content</MyTag> is parsed as <mytag>content</mytag>. If a parser resource limit is exceeded or the file cannot be read, it returns an empty DOM with an error available from parse_error().
fn parse_file_checked #
fn parse_file_checked(filename string) !DocumentObjectModel
parse_file_checked parses a file and returns an error if it cannot be read or a parser resource limit is exceeded.
fn CloseTagType.from #
fn CloseTagType.from[W](input W) !CloseTagType
from returns the CloseTagType value of an integer or of a field name, or an error if there is none.
enum CloseTagType #
enum CloseTagType {
in_name
new_tag
}
struct DocumentObjectModel #
struct DocumentObjectModel {
mut:
root &Tag = unsafe { nil }
constructed bool
btree BTree
all_tags []&Tag
all_attributes map[string][]&Tag
close_tags map[string]bool // add a counter to see count how many times is closed and parse correctly
attribute_value_indexes map[string]&datatypes.BTree[string, int]
tag_attributes map[string][][]&Tag
tag_type map[string][]&Tag
debug_file os.File
parse_error_msg string
source_bytes i64
}
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.
fn (DocumentObjectModel) get_root #
fn (dom &DocumentObjectModel) get_root() &Tag
get_root returns the root of the document.
fn (DocumentObjectModel) parse_error #
fn (dom &DocumentObjectModel) parse_error() ?string
parse_error returns the reason the DOM could not be parsed, or none on success.
struct GetTagsOptions #
struct GetTagsOptions {
pub:
name string
}
struct Parser #
struct Parser {
mut:
dom DocumentObjectModel
lexical_attributes LexicalAttributes = LexicalAttributes{
current_tag: &Tag{}
}
filename string = 'direct-parse'
initialized bool
tags []&Tag
debug_file os.File
}
Parser is responsible for reading the HTML strings and converting them into a DocumentObjectModel.
fn (Parser) add_code_tag #
fn (mut parser Parser) add_code_tag(name string)
This function is used to add a tag for the parser ignore it's content. For example, if you have an html or XML with a custom tag, like <script>, using this function, like add_code_tag('script') will make all script tags content be jumped, so you still have its content, but will not confuse the parser with it's > or <.
fn (Parser) split_parse #
fn (mut parser Parser) split_parse(data string)
split_parse parses the HTML fragment
fn (Parser) parse_html #
fn (mut parser Parser) parse_html(data string)
parse_html parses the given HTML string as a new document. Use split_parse for streaming chunks. If a parser resource limit is exceeded, get_dom returns an empty DOM with the reason available from parse_error.
fn (Parser) parse_html_checked #
fn (mut parser Parser) parse_html_checked(data string) !
parse_html_checked parses the data and returns an error if nesting exceeds 256 elements or aggregate rebuilt Tag.content bytes would exceed min(64 MiB, 256 times the input size).
fn (Parser) finalize #
fn (mut parser Parser) finalize()
finalize flushes pending text and finishes the parsing stage.
fn (Parser) get_dom #
fn (mut parser Parser) get_dom() DocumentObjectModel
get_dom returns the parser's current DOM representation.
struct Tag #
struct Tag {
pub mut:
name string
content string
children []&Tag
attributes map[string]string // attributes will be like map[name]value
last_attribute string
class_set datatypes.Set[string]
parent &Tag = unsafe { nil }
position_in_parent int
closed bool
close_type CloseTagType = .in_name
mut:
is_element_node bool
is_text_node bool
text_content string
content_is_inner_html bool
}
Tag holds the information of an HTML tag.
fn (Tag) text #
fn (tag &Tag) text() string
text returns the text contents of the tag and its children. A br element (including the parser's br/ form) contributes a newline.
fn (Tag) str #
fn (tag &Tag) str() string
str serializes the tag and its children without recursive calls.
fn (Tag) get_tag #
fn (tag &Tag) get_tag(name string) ?&Tag
get_tag retrieves the first found child tag in the tag that has the given tag name.
fn (Tag) get_tag_by_attribute #
fn (tag &Tag) get_tag_by_attribute(name string) ?&Tag
get_tag_by_attribute retrieves the first found child tag in the tag that has the given attribute name.
fn (Tag) get_tag_by_attribute_value #
fn (tag &Tag) get_tag_by_attribute_value(name string, value string) ?&Tag
get_tag_by_attribute_value retrieves the first found child tag in the tag that has the given attribute name and value.
fn (Tag) get_tag_by_class_name #
fn (tag &Tag) get_tag_by_class_name(names ...string) ?&Tag
get_tag_by_class_name retrieves the first found child tag in the tag that has the given class name(s).