Class: HTML5::TreeBuilders::Hpricot::Node

Inherits:
Base::Node
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/html5/treebuilders/hpricot.rb

Direct Known Subclasses

CommentNode, Document, DocumentType, Element, TextNode

Instance Attribute Summary collapse

Attributes inherited from Base::Node

#_flags, #childNodes, #parent

Instance Method Summary collapse

Methods inherited from Base::Node

#cloneNode, #reparentChildren

Constructor Details

#initialize(name) ⇒ Node

Returns a new instance of Node.



17
18
19
20
# File 'lib/html5/treebuilders/hpricot.rb', line 17

def initialize(name)
  super(name)
  @hpricot = self.class.hpricot_class.new name
end

Instance Attribute Details

#hpricotObject

Returns the value of attribute hpricot.



15
16
17
# File 'lib/html5/treebuilders/hpricot.rb', line 15

def hpricot
  @hpricot
end

Instance Method Details

#appendChild(node) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/html5/treebuilders/hpricot.rb', line 22

def appendChild(node)
  if node.kind_of?(TextNode) and childNodes.any? and childNodes.last.kind_of?(TextNode)
    childNodes.last.hpricot.content = childNodes.last.hpricot.content + node.hpricot.content
  else
    childNodes << node
    hpricot.children << node.hpricot
  end
  if (oldparent = node.hpricot.parent) != nil
    oldparent.children.delete_at(oldparent.children.index(node.hpricot))
  end
  node.hpricot.parent = hpricot
  node.parent = self
end

#hasContentObject



61
62
63
# File 'lib/html5/treebuilders/hpricot.rb', line 61

def hasContent
  childNodes.any?
end

#insertBefore(node, refNode) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/html5/treebuilders/hpricot.rb', line 51

def insertBefore(node, refNode)
  index = childNodes.index(refNode)
  if node.kind_of?(TextNode) and index > 0 and childNodes[index-1].kind_of?(TextNode)
    childNodes[index-1].hpricot.content = childNodes[index-1].hpricot.to_s + node.hpricot.to_s
  else
    refNode.hpricot.parent.insert_before(node.hpricot,refNode.hpricot)
    childNodes.insert(index, node)
  end
end

#insertText(data, before = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/html5/treebuilders/hpricot.rb', line 43

def insertText(data, before=nil)
  if before
    insertBefore(TextNode.new(data), before)
  else
    appendChild(TextNode.new(data))
  end
end

#removeChild(node) ⇒ Object



36
37
38
39
40
41
# File 'lib/html5/treebuilders/hpricot.rb', line 36

def removeChild(node)
   childNodes.delete(node)
   hpricot.children.delete_at(hpricot.children.index(node.hpricot))
   node.hpricot.parent = nil
   node.parent = nil
end