class HTML5::Node

Overview

A Node consists of a NodeType and some data (tag name for element nodes, content for text) and are part of a tree of Nodes. Element nodes may also have a #namespace and contain an array of Attribute. #data is unescaped, so that it looks like "a<b" rather than "a<b". For element nodes, #data_atom is the atom for data, or zero if data is not a known tag name.

An empty namespace implies a "http://www.w3.org/1999/xhtml" namespace. Similarly, "math" is short for "http://www.w3.org/1998/Math/MathML", and "svg" is short for "http://www.w3.org/2000/svg".

Defined in:

html5/css/selector.cr
html5/node.cr
html5/xpath/xpath.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(type : HTML5::NodeType, data_atom : HTML5::Atom::Atom = Atom::Atom.zero, data : String = "", namespace : String = "", attr : Array(HTML5::Attribute) = Array(Attribute).new) #

[View source]

Instance Method Detail

def [](attribute_name : String) #

[View source]
def []?(attribute_name : String) #

[View source]
def append_child(c : Node) #

append_child adds a node as child of self. It will raise exception if c already has a parent or siblings


[View source]
def attr : Array(Attribute) #

[View source]
def attribute_value(name : String) #

returns the attribute value with specified name. if current node is #element? it returns the inner text or else it will scan through node attributes and returns the value of attribute matching the name returns "" if no attribute is found.


[View source]
def attributes #

[View source]
def clone #

clone returns a new node with the same type, data and attributes. The clone has no parent, no siblings and no children


[View source]
def comment? : Bool #

Returns true if this node is a Comment Node


[View source]
def css(expression : String) : Array(Node) #

Searches this node for CSS Selector expression. Returns all of the matched HTML5::Node


[View source]
def data : String #

[View source]
def data_atom : Atom::Atom #

[View source]
def doctype? : Bool #

Returns true if this node is a Doctype Node


[View source]
def document? : Bool #

Returns true if this node is a Document Node


[View source]
def element? : Bool #

Returns true if this node is an Element Node


[View source]
def error? : Bool #

Returns true if this node is an Error Node


[View source]
def first_child : Node | Nil #

[View source]
def inner_text : String #

returns the inner text of current node


[View source]
def insert_before(new_child : Node, old_child : Node | Nil) #

insert_before inserts a new_child as a child of self, immediately before old_child in the sequence of self's children. old child may be nil, in which case new child is appended to the end of self's children.

It will raise exception if new child already has a parent or sibling


[View source]
def last_child : Node | Nil #

[View source]
def namespace : String #

[View source]
def next_sibling : Node | Nil #

[View source]
def parent : Node | Nil #

[View source]
def prev_sibling : Node | Nil #

[View source]
def remove_child(c : Node) #

remove_child remove a node c that is a child of self. Afterwards, c will have no parent and no siblings.

It will raise exception if c's parent is not this node


[View source]
def render(io : IO) : Nil #

#render renders the parse tree node to the given IO writer.

Rendering is done on a 'best effort' basis: calling parse on the output of #render will always result in something similar to the original tree, but it is not necessarily an exact clone unless the original tree was 'well-formed'. 'Well-formed' is not easily specified; the HTML5 specification is complicated.

Calling parse on arbitrary input typically results in a 'well-formed' parse tree. However, it is possible for Parse to yield a 'badly-formed' parse tree. For example, in a 'well-formed' parse tree, no element is a child of another element: parsing "" results in two sibling elements. Similarly, in a 'well-formed' parse tree, no element is a child of a

element: parsing "

" results in a

with two sibling children; the is reparented to the

's parent. However, calling Parse on "
" does not return an error, but the result has an element with an child, and is therefore not 'well-formed'.

Programmatically constructed trees are typically also 'well-formed', but it is possible to construct a tree that looks innocuous but, when rendered and re-parsed, results in a different tree. A simple example is that a solitary text node would become a tree containing , and elements. Another example is that the programmatic equivalent of "abc" becomes "abc".


[View source]
def text? : Bool #

Returns true if this node is a Text Node


[View source]
def to_html(self_only : Bool = true) : String #

Renders node to HTML. self_only will render current node only, but if its false it will return the HTML of current node as well as all of its children


[View source]
def type : NodeType #

[View source]
def xpath(path : String) : Node | Nil #

Searches this node for XPath path. Returns first matched HTML5::Node or nil


[View source]
def xpath_bool(path : String) #

Searches this node for XPath path and restricts the return type to String.


[View source]
def xpath_evaluate(path) #

Searches this node for XPath path and return result with appropriate type (Bool | Float64 | String | NodeIterator | Nil)


[View source]
def xpath_float(path : String) #

Searches this node for XPath path and restricts the return type to Float64.


[View source]
def xpath_nodes(path : String) : Array(Node) #

Searches this node for XPath path. Returns all of the matched HTML5::Node


[View source]