class

HTML5::Node

Inherits Reference < Object

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".

Constructors

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

Instance methods

[](attribute_name : String)
Source
[]?(attribute_name : String)
Source
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

Source
attr
Source
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.

Source
attributes
Source
clone

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

Source
comment?

Returns true if this node is a Comment Node

Source
css(expression : String) : Array(Node)

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

Source
data
Source
data_atom
Source
doctype?

Returns true if this node is a Doctype Node

Source
document?

Returns true if this node is a Document Node

Source
element?

Returns true if this node is an Element Node

Source
error?

Returns true if this node is an Error Node

Source
first_child
Source
inner_text

returns the inner text of current node

Source
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

Source
last_child
Source
namespace
Source
next_sibling
Source
parent
Source
prev_sibling
Source
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

Source
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 <html>, <head> and <body> elements. Another example is that the programmatic equivalent of "a<head>b</head>c" becomes "<html><head><head/><body>abc</body></html>".

Source
text?

Returns true if this node is a Text Node

Source
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

Source
type
Source
xpath(path : String) : Node | Nil

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

Source
xpath_bool(path : String)

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

Source
xpath_evaluate(path)

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

Source
xpath_float(path : String)

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

Source
xpath_nodes(path : String) : Array(Node)

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

Source

CrystalDocs.org - Crystal Shard Documentation Hosting

The Bushido Collective builds and maintains this site.
Licensed under the Apache License 2.0