Class: Hexp::TextNode
- Inherits:
-
String
- Object
- String
- Hexp::TextNode
- Defined in:
- lib/hexp/text_node.rb,
lib/hexp-rails.rb
Overview
Represents text inside HTML. Instances behave like Strings, but also support the most of ‘Hexp::Node` interface, so you don’t have to treat them differently when traversing across trees.
Strings used inside Hexp literals like ‘H[:span, “Hi!”]` automatically get converted to `TextNode` instances, so there is usually no reason to instantiate these yourself.
Instance Method Summary collapse
-
#attributes ⇒ Hash
The attributes of this Node.
-
#children ⇒ Array
Children of the node.
-
#class?(klz) ⇒ FalseClass
Is a certain CSS class present on this node?.
-
#inspect ⇒ String
Inspect the TextNode.
-
#pp ⇒ String
Same as inspect, used by ‘Hexp::Node#pp`.
-
#rewrite(&blk) ⇒ Hexp::TextNode
Rewrite a node.
- #select(&block) ⇒ Object
-
#tag ⇒ NilClass
The tag of this node.
-
#text? ⇒ TrueClass
Is this a text node?.
-
#to_hexp ⇒ Hexp::TextNode
Standard conversion protocol, returns self.
- #to_html(opts = {}) ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#attributes ⇒ Hash
The attributes of this Node
Text nodes can not have attributes, so this always returns an empty Hash.
37 38 39 |
# File 'lib/hexp/text_node.rb', line 37 def attributes {}.freeze end |
#children ⇒ Array
Children of the node
A text node has no children, this always returns an empty array.
92 93 94 |
# File 'lib/hexp/text_node.rb', line 92 def children [].freeze end |
#class?(klz) ⇒ FalseClass
Is a certain CSS class present on this node?
Text nodes have no attributes, so this always returns false.
121 122 123 |
# File 'lib/hexp/text_node.rb', line 121 def class?(klz) false end |
#inspect ⇒ String
Inspect the TextNode
This delegates to the underlying String, making it non-obvious that you’re dealing with something else. However, a TextNode supports the full API of String, so this might not be a big problem. The benefit is that inspection of complete nodes containing text looks nice
25 26 27 |
# File 'lib/hexp/text_node.rb', line 25 def inspect __getobj__.inspect end |
#pp ⇒ String
Same as inspect, used by ‘Hexp::Node#pp`
50 51 52 |
# File 'lib/hexp/text_node.rb', line 50 def pp inspect end |
#rewrite(&blk) ⇒ Hexp::TextNode
Rewrite a node
See Hexp::Node#rewrite for more info. On a TextNode this simply return self.
138 139 140 |
# File 'lib/hexp/text_node.rb', line 138 def rewrite(&blk) self end |
#select(&block) ⇒ Object
142 143 144 |
# File 'lib/hexp/text_node.rb', line 142 def select(&block) Node::Selection.new(self, block) end |
#tag ⇒ NilClass
The tag of this node
A text node does not have a tag, so this returns nil
65 66 |
# File 'lib/hexp/text_node.rb', line 65 def tag end |
#text? ⇒ TrueClass
Is this a text node?
106 107 108 |
# File 'lib/hexp/text_node.rb', line 106 def text? true end |
#to_hexp ⇒ Hexp::TextNode
Standard conversion protocol, returns self
77 78 79 |
# File 'lib/hexp/text_node.rb', line 77 def to_hexp self end |
#to_html(opts = {}) ⇒ Object
146 147 148 |
# File 'lib/hexp/text_node.rb', line 146 def to_html(opts = {}) Unparser.new(opts).call(self) end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/hexp-rails.rb', line 19 def to_s to_html.html_safe end |