Module: HTMLTree::TreeElement
- Includes:
- Enumerable
- Defined in:
- lib/html/xpath.rb,
lib/html/element.rb
Instance Method Summary collapse
-
#add_child(*children_to_add) ⇒ Object
(also: #add_children)
Add one or more children to this node.
-
#can_have_children? ⇒ Boolean
Return true if my content is a collection of Elements rather than actual data.
-
#children ⇒ Object
Return a collection of my children.
-
#content ⇒ Object
Return my content; either my children or my data.
-
#dump(indent = 0, io = $stdout) ⇒ Object
Print out to $stdout (or given IO or String) a formatted dump of my structure.
-
#each(&block) ⇒ Object
Breadth-first iterator, required by Enumerable.
-
#has_children? ⇒ Boolean
Return true if I have any children.
-
#parent ⇒ Object
Return my parent element.
-
#parent=(parent_or_nil) ⇒ Object
Change my parent.
- #path ⇒ Object
-
#remove_child(*children_to_remove) ⇒ Object
(also: #remove_children)
Remove one or more children from this node.
-
#rexml_match(path) ⇒ Object
Given the XPath path, return an Array of matching sub-elements of the REXML tree.
-
#root ⇒ Object
Return the ultimate parent.
Instance Method Details
#add_child(*children_to_add) ⇒ Object Also known as: add_children
Add one or more children to this node.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/html/element.rb', line 30 def add_child(*children_to_add) if can_have_children? children_to_add.each do |child| @_content << child child._parent = self end else raise(ArgumentError.exception('node cannot have children')) end end |
#can_have_children? ⇒ Boolean
Return true if my content is a collection of Elements rather than actual data.
64 65 66 |
# File 'lib/html/element.rb', line 64 def can_have_children? @_content.kind_of?(Array) end |
#children ⇒ Object
Return a collection of my children. Returns an empty Array if I am a data element, just to keep other methods simple.
70 71 72 |
# File 'lib/html/element.rb', line 70 def children can_have_children? ? @_content : [] end |
#content ⇒ Object
Return my content; either my children or my data.
75 76 77 |
# File 'lib/html/element.rb', line 75 def content @_content end |
#dump(indent = 0, io = $stdout) ⇒ Object
Print out to $stdout (or given IO or String) a formatted dump of my structure.
106 107 108 109 110 111 |
# File 'lib/html/element.rb', line 106 def dump(indent=0, io=$stdout) io << " " * indent io << self.to_s io << "\n" children.each { |ea| ea.dump(indent+1, io) } end |
#each(&block) ⇒ Object
Breadth-first iterator, required by Enumerable.
99 100 101 102 |
# File 'lib/html/element.rb', line 99 def each(&block) block.call(self) children.each { |ch| ch.each(&block) } end |
#has_children? ⇒ Boolean
Return true if I have any children.
94 95 96 |
# File 'lib/html/element.rb', line 94 def has_children? children.size > 0 end |
#parent ⇒ Object
Return my parent element.
80 81 82 |
# File 'lib/html/element.rb', line 80 def parent @_parent end |
#parent=(parent_or_nil) ⇒ Object
Change my parent. Disconnects from prior parent, if any.
57 58 59 60 |
# File 'lib/html/element.rb', line 57 def parent=(parent_or_nil) @_parent.remove_child(self) if @_parent parent_or_nil.add_child(self) if parent_or_nil end |
#path ⇒ Object
84 85 86 |
# File 'lib/html/element.rb', line 84 def path "/" end |
#remove_child(*children_to_remove) ⇒ Object Also known as: remove_children
Remove one or more children from this node.
44 45 46 47 48 49 50 51 52 |
# File 'lib/html/element.rb', line 44 def remove_child(*children_to_remove) if can_have_children? children_to_remove.each do |child| child._parent = nil if @_content.delete(child) end else raise(ArgumentError.exception('node cannot have children')) end end |
#rexml_match(path) ⇒ Object
Given the XPath path, return an Array of matching sub-elements of the REXML tree.
19 20 21 22 |
# File 'lib/html/xpath.rb', line 19 def rexml_match(path) node = as_rexml_document REXML::XPath.match(node, path) end |