Class: HTML5::TreeBuilders::SimpleTree::Node

Inherits:
Base::Node
  • Object
show all
Defined in:
lib/html5/treebuilders/simpletree.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

#reparentChildren

Constructor Details

#initialize(name) ⇒ Node

Returns a new instance of Node.



19
20
21
22
23
24
# File 'lib/html5/treebuilders/simpletree.rb', line 19

def initialize name
  super
  @name       = name
  @value      = nil
  @attributes = {}
end

Instance Attribute Details

#attributesObject

a dict holding name, value pairs for attributes of the node



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

def attributes
  @attributes
end

#nameObject

Node representing an item in the tree. name - The tag name associated with the node



10
11
12
# File 'lib/html5/treebuilders/simpletree.rb', line 10

def name
  @name
end

#valueObject

The value of the current node (applies to text nodes and comments



14
15
16
# File 'lib/html5/treebuilders/simpletree.rb', line 14

def value
  @value
end

Instance Method Details

#appendChild(node) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/html5/treebuilders/simpletree.rb', line 26

def appendChild node
  if node.kind_of? TextNode and 
    childNodes.length > 0 and childNodes.last.kind_of? TextNode
    childNodes.last.value += node.value
  else
    childNodes << node
  end
  node.parent = self
end

#cloneNodeObject



41
42
43
44
45
46
# File 'lib/html5/treebuilders/simpletree.rb', line 41

def cloneNode
  newNode = self.class.new name
  attributes.each {|name,value| newNode.attributes[name] = value}
  newNode.value = value
  newNode
end

#hasContentObject



73
74
75
# File 'lib/html5/treebuilders/simpletree.rb', line 73

def hasContent
  childNodes.length > 0
end

#insertBefore(node, refNode) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/html5/treebuilders/simpletree.rb', line 56

def insertBefore node, refNode
  index = childNodes.index(refNode)
  if node.kind_of?(TextNode) && index > 0 && childNodes[index-1].kind_of?(TextNode)
    childNodes[index-1].value += node.value
  else
    childNodes.insert index, node
  end
end

#insertText(data, before = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/html5/treebuilders/simpletree.rb', line 48

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

#printTree(indent = 0) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/html5/treebuilders/simpletree.rb', line 65

def printTree indent=0
  tree = "\n|%s%s" % [' '* indent, self.to_s]
  for child in childNodes
    tree += child.printTree(indent + 2)
  end
  return tree
end

#removeChild(node) ⇒ Object



36
37
38
39
# File 'lib/html5/treebuilders/simpletree.rb', line 36

def removeChild node
   childNodes.delete node
   node.parent = nil
end