Class: Hexp::TextNode

Inherits:
String
  • Object
show all
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

Instance Method Details

#attributesHash

The attributes of this Node

Text nodes can not have attributes, so this always returns an empty Hash.

Returns:

  • (Hash)


37
38
39
# File 'lib/hexp/text_node.rb', line 37

def attributes
  {}.freeze
end

#childrenArray

Children of the node

A text node has no children, this always returns an empty array.

Examples:

Hexp::TextNode.new("hello, world").children #=> []

Returns:



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.

Examples:

Hexp::TextNode.new('foo').class?('bar') #=> false

Returns:

  • (FalseClass)


121
122
123
# File 'lib/hexp/text_node.rb', line 121

def class?(klz)
  false
end

#inspectString

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

Examples:

Hexp::TextNode.new("hello, world").inspect #=> "\"hello, world\""

Returns:

  • (String)


25
26
27
# File 'lib/hexp/text_node.rb', line 25

def inspect
  __getobj__.inspect
end

#ppString

Same as inspect, used by ‘Hexp::Node#pp`

Examples:

Hexp::TextNode.new("hello, world").pp #=> "\"hello, world\""

Returns:

  • (String)


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.

Examples:

tree.rewrite do |node|
  H[:div, {class: 'wrap'}, node]
end

Returns:



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

#tagNilClass

The tag of this node

A text node does not have a tag, so this returns nil

Examples:

Hexp::TextNode.new("hello, world").tag #=> nil

Returns:



65
66
# File 'lib/hexp/text_node.rb', line 65

def tag
end

#text?TrueClass

Is this a text node?

Examples:

Hexp::TextNode.new('foo').text? #=> true
H[:p].text? #=> false

Returns:

  • (TrueClass)


106
107
108
# File 'lib/hexp/text_node.rb', line 106

def text?
  true
end

#to_hexpHexp::TextNode

Standard conversion protocol, returns self

Examples:

Hexp::TextNode.new("hello, world").to_hexp #=> #<Hexp::TextNode "hello, world">

Returns:



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_sObject



19
20
21
# File 'lib/hexp-rails.rb', line 19

def to_s
  to_html.html_safe
end