Module: HTMLTree::TreeElement

Includes:
Enumerable
Included in:
Data, Document, Element
Defined in:
lib/web.rb,
lib/web/htmltools/xpath.rb,
lib/web/htmltools/element.rb

Instance Method Summary collapse

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.

Returns:

  • (Boolean)


63
64
65
# File 'lib/web/htmltools/element.rb', line 63

def can_have_children?
  @_content.kind_of?(Array)
end

#childrenObject

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

#contentObject

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.

Returns:

  • (Boolean)


89
90
91
# File 'lib/web/htmltools/element.rb', line 89

def has_children?
  children.size > 0
end

#parentObject

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

#rootObject

Return the ultimate parent.



84
85
86
# File 'lib/web/htmltools/element.rb', line 84

def root
  @_parent ? self : @_parent.root
end