Module: HTMLTree::TreeElement
- Includes:
- Enumerable
- Defined in:
- lib/web.rb,
lib/web/htmltools/xpath.rb,
lib/web/htmltools/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.
- #get_elements(aname, elements = []) ⇒ Object
-
#has_children? ⇒ Boolean
Return true if I have any children.
-
#parent ⇒ Object
Return my parent element.
-
#parent=(parent_or_nil) ⇒ Object
Change my parent.
-
#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.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/web/htmltools/element.rb', line 29 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.
63 64 65 |
# File 'lib/web/htmltools/element.rb', line 63 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.
69 70 71 |
# File 'lib/web/htmltools/element.rb', line 69 def children can_have_children? ? @_content : [] end |
#content ⇒ Object
Return my content; either my children or my data.
74 75 76 |
# File 'lib/web/htmltools/element.rb', line 74 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.
101 102 103 104 105 106 |
# File 'lib/web/htmltools/element.rb', line 101 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.
94 95 96 97 |
# File 'lib/web/htmltools/element.rb', line 94 def each(&block) block.call(self) children.each { |ch| ch.each(&block) } end |
#get_elements(aname, elements = []) ⇒ Object
410 411 412 413 414 415 416 417 418 419 |
# File 'lib/web.rb', line 410 def get_elements aname, elements=[] children.each { |element| if element.tag == aname elements.push element else element.get_elements aname, elements end } elements end |
#has_children? ⇒ Boolean
Return true if I have any children.
89 90 91 |
# File 'lib/web/htmltools/element.rb', line 89 def has_children? children.size > 0 end |
#parent ⇒ Object
Return my parent element.
79 80 81 |
# File 'lib/web/htmltools/element.rb', line 79 def parent @_parent end |
#parent=(parent_or_nil) ⇒ Object
Change my parent. Disconnects from prior parent, if any.
56 57 58 59 |
# File 'lib/web/htmltools/element.rb', line 56 def parent=(parent_or_nil) @_parent.remove_child(self) if @_parent parent_or_nil.add_child(self) if parent_or_nil end |
#remove_child(*children_to_remove) ⇒ Object Also known as: remove_children
Remove one or more children from this node.
43 44 45 46 47 48 49 50 51 |
# File 'lib/web/htmltools/element.rb', line 43 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.
18 19 20 21 |
# File 'lib/web/htmltools/xpath.rb', line 18 def rexml_match(path) node = as_rexml_document REXML::XPath.match(node, path) end |