class HTML5::Node
- HTML5::Node
- Reference
- Object
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.crhtml5/node.cr
html5/xpath/xpath.cr
Constructors
Instance Method Summary
- #[](attribute_name : String)
- #[]?(attribute_name : String)
-
#append_child(c : Node)
append_child adds a node as child of self.
- #attr : Array(Attribute)
-
#attribute_value(name : String)
returns the attribute value with specified name.
- #attributes
-
#clone
clone returns a new node with the same type, data and attributes.
-
#comment? : Bool
Returns
true
if this node is a Comment Node -
#css(expression : String) : Array(Node)
Searches this node for CSS Selector expression.
- #data : String
- #data_atom : Atom::Atom
-
#doctype? : Bool
Returns
true
if this node is a Doctype Node -
#document? : Bool
Returns
true
if this node is a Document Node -
#element? : Bool
Returns
true
if this node is an Element Node -
#error? : Bool
Returns
true
if this node is an Error Node - #first_child : Node | Nil
-
#inner_text : String
returns the inner text of current node
-
#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.
- #last_child : Node | Nil
- #namespace : String
- #next_sibling : Node | Nil
- #parent : Node | Nil
- #prev_sibling : Node | Nil
-
#remove_child(c : Node)
remove_child remove a node c that is a child of self.
-
#render(io : IO) : Nil
#render
renders the parse tree node to the givenIO
writer. -
#text? : Bool
Returns
true
if this node is a Text Node -
#to_html(self_only : Bool = true) : String
Renders node to HTML.
- #type : NodeType
-
#xpath(path : String) : Node | Nil
Searches this node for XPath path.
-
#xpath_bool(path : String)
Searches this node for XPath path and restricts the return type to
String
. -
#xpath_evaluate(path)
Searches this node for XPath path and return result with appropriate type
(Bool | Float64 | String | NodeIterator | Nil)
-
#xpath_float(path : String)
Searches this node for XPath path and restricts the return type to
Float64
. -
#xpath_nodes(path : String) : Array(Node)
Searches this node for XPath path.
Constructor Detail
Instance Method Detail
append_child adds a node as child of self. It will raise exception if c already has a parent or siblings
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.
clone returns a new node with the same type, data and attributes. The clone has no parent, no siblings and no children
Searches this node for CSS Selector expression. Returns all of the matched HTML5::Node
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
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
#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
with two sibling children; the is reparented to the
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".
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
Searches this node for XPath path. Returns first matched HTML5::Node
or nil
Searches this node for XPath path and restricts the return type to String
.
Searches this node for XPath path and return result with appropriate type
(Bool | Float64 | String | NodeIterator | Nil)
Searches this node for XPath path and restricts the return type to Float64
.
Searches this node for XPath path. Returns all of the matched HTML5::Node