Class: Hexp::Node
- Inherits:
-
Object
- Object
- Hexp::Node
- Extended by:
- Forwardable
- Includes:
- Attributes, Children
- Defined in:
- lib/hexp/node.rb,
lib/hexp/node/pp.rb,
lib/hexp/node/domize.rb,
lib/hexp/node/selector.rb,
lib/hexp/node/children.rb,
lib/hexp/node/rewriter.rb,
lib/hexp/node/normalize.rb,
lib/hexp/node/attributes.rb,
lib/hexp/node/css_selection.rb
Overview
A Hexp::Node represents a single element in a HTML syntax tree. It consists of three parts : the #tag, the #attributes and the #children.
Instances of Hexp::Node are immutable. Because of this all methods that "alter" the node actually return a new instance, leaving the old one untouched.
The Hexp::Node constructor takes a Symbol, a Hash and an Array for the #tag, #attributes and #children respectively. However the attributes and children can each be ommitted if they are empty.
If the node contains a single child node, then it is not necessary to wrap that child node in an array. Just put the single node in place of the list of children.
One can use H[tag, attrs, children] as a shorthand syntax. Finally one can use Hexp.build to construct nodes using Ruby blocks, not unlike the Builder or Nokogiri gems.
Methods that read or alter the attributes Hash are defined in Attributes. Methods that read or alter the list of child nodes are defined in Children
CSS selectors
When working with large trees of Node objects, it is convenient to be able to target specific nodes using CSS selector syntax. Hexp supports a subset of the CSS 3 selector syntax, see CssSelection for info on the supported syntax.
For changing, replacing or removing specific nodes based on a CSS selector string, see #replace. To iterate over nodes, see #select.
Defined Under Namespace
Modules: Attributes, Children Classes: CssSelection, Domize, Normalize, PP, Rewriter, Selector
Instance Attribute Summary (collapse)
-
- (Hash<String, String>) attributes
readonly
The attributes of this node.
-
- (Hexp::List) children
readonly
The child nodes of this node.
-
- (Symbol) tag
readonly
The HTML tag of this node.
Class Method Summary (collapse)
-
+ (Hexp::Node) [](*args)
Main entry point for creating literal hexps.
-
+ (String) inspect_name
private
Returns the class name for use in creating inspection strings.
Instance Method Summary (collapse)
-
- (Hexp::Node) initialize(*args)
constructor
Normalize the arguments.
-
- (String) inspect
Return a string representation that is close to the literal form.
-
- (String) pp
Pretty print, a multiline representation with indentation.
-
- (Hexp::Node) process(*processors)
Run a number of processors on this node.
-
- (Hexp::Node) rewrite(css_selector = nil, &block)
(also: #replace)
Replace nodes in a tree.
-
- (Object) select(css_selector = nil, &block)
Select nodes based on a css selector.
-
- (Hexp::Node) set_tag(tag)
Return a new node, with a different tag.
-
- (FalseClass) text?
Is this a text node? Returns false.
-
- (Nokogiri::HTML::Document) to_dom(options = {})
private
Convert this node into a Nokogiri Document.
-
- (Hexp::Node) to_hexp
Standard hexp coercion protocol, return self.
-
- (String) to_html(options = {})
Serialize this node to HTML.
Methods included from Children
#add_child, #empty?, #map_children, #set_children, #text
Methods included from Attributes
#[], #add_class, #attr, #class?, #class_list, #has_attr?, #merge_attrs, #remove_attr, #remove_class, #set_attrs
Constructor Details
- (Hexp::Node) initialize(*args)
Normalize the arguments
123 124 125 |
# File 'lib/hexp/node.rb', line 123 def initialize(*args) @tag, @attributes, @children = Normalize.new(args).call end |
Instance Attribute Details
- (Hash<String, String>) attributes (readonly)
The attributes of this node
79 80 81 |
# File 'lib/hexp/node.rb', line 79 def attributes @attributes end |
- (Hexp::List) children (readonly)
The child nodes of this node
90 91 92 |
# File 'lib/hexp/node.rb', line 90 def children @children end |
- (Symbol) tag (readonly)
The HTML tag of this node
69 70 71 |
# File 'lib/hexp/node.rb', line 69 def tag @tag end |
Class Method Details
+ (Hexp::Node) [](*args)
Main entry point for creating literal hexps
At the moment this just redirects to #new, and since Hexp::Node is aliased to H this provides a shorthand for the contructor,
Note that while the H[] form is part of the public API and expected to remain, it's implementation might change. In particular H might become a class or module in its own right, so it is recommended to only use this method in its H[] form.
109 110 111 |
# File 'lib/hexp/node.rb', line 109 def self.[](*args) new(*args) end |
+ (String) inspect_name
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the class name for use in creating inspection strings
This will return "H" if H == Hexp::Node, or "Hexp::Node" otherwise.
308 309 310 311 312 313 314 |
# File 'lib/hexp/node.rb', line 308 def inspect_name if defined?(H) && H == self 'H' else self.name end end |
Instance Method Details
- (String) inspect
Return a string representation that is close to the literal form
175 176 177 |
# File 'lib/hexp/node.rb', line 175 def inspect self.class.inspect_name + [tag, attributes, children].compact.reject(&:empty?).inspect end |
- (String) pp
Pretty print, a multiline representation with indentation
187 188 189 |
# File 'lib/hexp/node.rb', line 187 def pp self.class::PP.new(self).call end |
- (Hexp::Node) process(*processors)
Run a number of processors on this node
This is pure convenience, but it helps to conceptualize the "processor" idea of a component (be it a lambda or other object), that responds to call, and transform a Hexp::Node tree.
271 272 273 |
# File 'lib/hexp/node.rb', line 271 def process(*processors) processors.empty? ? self : processors.first.(self).process(*processors.drop(1)) end |
- (Hexp::Node) rewrite(css_selector = nil, &block) Also known as: replace
Replace nodes in a tree
With a CSS selector string like "form.checkout" you specify the nodes you want to operate on. These will be passed one by one into the block. The block returns the Hexp::Node that will replace the old node, or it can replace an Array of nodes to fill the place of the old node.
Because of this you can add one or more nodes, or remove nodes by returning an empty array.
238 239 240 241 |
# File 'lib/hexp/node.rb', line 238 def rewrite(css_selector = nil, &block) return Rewriter.new(self, block) if css_selector.nil? CssSelection.new(self, css_selector).rewrite(&block) end |
- (Object) select(css_selector = nil, &block)
Select nodes based on a css selector
246 247 248 249 250 251 252 |
# File 'lib/hexp/node.rb', line 246 def select(css_selector = nil, &block) if css_selector CssSelection.new(self, css_selector).each(&block) else Selector.new(self, block) end end |
- (Hexp::Node) set_tag(tag)
Return a new node, with a different tag
212 213 214 |
# File 'lib/hexp/node.rb', line 212 def set_tag(tag) H[tag.to_sym, attributes, children] end |
- (FalseClass) text?
Is this a text node? Returns false
199 200 201 |
# File 'lib/hexp/node.rb', line 199 def text? false end |
- (Nokogiri::HTML::Document) to_dom(options = {})
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert this node into a Nokogiri Document
163 164 165 |
# File 'lib/hexp/node.rb', line 163 def to_dom( = {}) Domize.new(self, ).call end |
- (Hexp::Node) to_hexp
Standard hexp coercion protocol, return self
135 136 137 |
# File 'lib/hexp/node.rb', line 135 def to_hexp self end |
- (String) to_html(options = {})
Serialize this node to HTML
148 149 150 |
# File 'lib/hexp/node.rb', line 148 def to_html( = {}) to_dom().to_html end |