Module: Hexp::Node::Children

Included in:
Hexp::Node
Defined in:
lib/hexp/node/children.rb

Overview

Node API methods that deal with child_nodes

Instance Method Summary collapse

Instance Method Details

#add_child(child) ⇒ Hexp::Node Also known as: add, <<

Add a child node to the end of the list of children

Examples:

H[:ul].add_child(H[:li, "chunky"]) #=> H[:ul, [H[:li, "chunky]]]

Parameters:

Returns:

  • (Hexp::Node)

    A new node containing that has the child added to its children



32
33
34
35
36
37
38
# File 'lib/hexp/node/children.rb', line 32

def add_child(child)
  H[
    self.tag,
    self.attributes,
    self.children + [child]
  ]
end

#append(*nodes) ⇒ Object



99
100
101
# File 'lib/hexp/node/children.rb', line 99

def append(*nodes)
  H[tag, attributes, children + nodes]
end

#content(*args) ⇒ Hexp::Node Also known as: set_children

Replace the children of this node with a new list of children

Examples:

H[:div, "Hello"].set_children([H[:span, "wicked!"], H[:br]])
# => H[:div, [H[:span, "wicked!"], H[:br]]]

Parameters:

  • new_children (Array, Hexp::NodeList)

Returns:



75
76
77
# File 'lib/hexp/node/children.rb', line 75

def content(*args)
  H[tag, attributes, *args]
end

#empty?true, false

Is this node an empty node

Examples:

H[:p, class: 'foo'].empty? #=> true
H[:p, [H[:span]].empty?    #=> false

Returns:

  • (true, false)

    True if this node has no children



16
17
18
# File 'lib/hexp/node/children.rb', line 16

def empty?
  children.empty?
end

#map_children {|The| ... } ⇒ Hexp::Node

Perform an action on each child node, and replace the node with the result

Examples:

H[:div, [H[:span, "foo"]]].map_children do |node|
  node.add_class(node.text)
end
# => H[:div, [H[:span, {class: "foo"}, "foo"]]]

Yield Parameters:

Returns:



94
95
96
97
# File 'lib/hexp/node/children.rb', line 94

def map_children(&blk)
  return to_enum(:map_children) unless block_given?
  H[tag, attributes, children.map(&blk)]
end

#textString

All the text in this node and its descendants

Concatenates the contents of all text nodes.

Examples:

H[:div, [
    H[:span, "My name is"],
    " ",
    H[:strong, "@plexus"],
    "."
  ]
].text #=> "My name is @plexus."

Returns:

  • (String)


58
59
60
61
62
# File 'lib/hexp/node/children.rb', line 58

def text
  children.map do |node|
    node.text? ? node : node.text
  end.join
end