Myhtml::Node
Constructors
Class methods
Instance methods
Alias for add, remove attribute node["class"] = "red" node["style"] = nil
Add a child node to the end
This inserts the child node at the end of parent node's children
document = Myhtml::Parser.new("<html><body><p>Hi!</p></body></html>")
body = document.body!
span = document.tree.create_node(:span)
body.append_child(span)
body.to_html # <body><p>Hi!</p><span></span></body>
Find attribute by key
iterate over all attributes to find it
if you need to use it multiple times, better to use cached method attributes[key]?
Css selector with string rule in scope on the current node return Myhtml::Iterator::Collection
Example:
some_node.css("div.red").each { |node| p node } # iterate over all divs with class red in the scope of node
Css selector with filter in scope on the current node return Myhtml::Iterator::Collection
Example:
filter = Myhtml::CssFilter.new("div.red")
some_node.css(filter).each { |node| p node } # iterator over all divs with class red in the scope of node
Css select which yielding collection this allows to free collection after block call and not waiting for GC
Example: some_node.css("div.red") { |collection| collection.each { |node| p node } }
Right neighbour node from current
different from right method by not down to children nodes
Helper method to add inner text to node
document = Myhtml::Parser.new("<html><body><div></div></body></html>")
div = document.css("div").first
div.inner_text = "bla"
document.to_html # <html><head></head><body><div>bla</div></body></html>
Add a sibling node after this node
document = Myhtml::Parser.new("<html><body><div></div></body></html>")
div = document.css("div").first
img = document.tree.create_node(:img)
div.insert_after(img)
document.body!.to_html # <body><div></div><img></body>
Add a sibling node before this node
document = Myhtml::Parser.new("<html><body><main></main></body></html>")
main = document.css("main").first
header = document.tree.create_node(:header)
main.insert_before(header)
document.body!.to_html # <body><header></header><main></main></body>
Left iterator iterate over all nodes from current to the root of document by moving from current to left node every time
Right iterator iterate over all nodes from current to the end of document by moving from current to right node every time
Tag Text
Direct text content of node
present only on MyHTML_TAG__TEXT, MyHTML_TAG_STYLE, MyHTML_TAG__COMMENT nodes (node.textable?)
for other nodes, you should call inner_text method
Convert node to html to IO deep - option, means visit children nodes or not (by default true).
Convert node to html string deep - option, means visit children nodes or not (by default true).
Example:
parser = Myhtml::Parser.new("<html><body><div class=AAA style='color:red'>Haha <span>11</span></div></body></html>")
node = parser.nodes(:div).first
node.to_html # => `<div class="AAA" style="color:red">Haha <span>11</span></div>`
node.to_html(deep: false) # => `<div class="AAA" style="color:red">`
Convert end of tag to string ex: node.to_html_end # => "