Class: Hexp::TextNode

Inherits:
String
  • Object
show all
Defined in:
lib/hexp/text_node.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

- (Hash) attributes

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

- (Array) children

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

- (FalseClass) class?(klz)

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

- (String) inspect

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

- (String) pp

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

- (Hexp::TextNode) rewrite(&blk)

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

- (Object) select(&block)



142
143
144
# File 'lib/hexp/text_node.rb', line 142

def select(&block)
  Node::Selector.new(self, block)
end

- (NilClass) tag

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:

  • (NilClass)


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

def tag
end

- (TrueClass) text?

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

- (Hexp::TextNode) to_hexp

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