2007-05-26 03:52:27 +02:00
|
|
|
require 'html5lib/treebuilders/base'
|
|
|
|
|
|
|
|
module HTML5lib
|
2007-05-30 17:45:52 +02:00
|
|
|
module TreeBuilders
|
|
|
|
module SimpleTree
|
|
|
|
|
|
|
|
class Node < Base::Node
|
|
|
|
# Node representing an item in the tree.
|
|
|
|
# name - The tag name associated with the node
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
# The value of the current node (applies to text nodes and
|
|
|
|
# comments
|
|
|
|
attr_accessor :value
|
|
|
|
|
|
|
|
# a dict holding name, value pairs for attributes of the node
|
|
|
|
attr_accessor :attributes
|
|
|
|
|
|
|
|
def initialize name
|
|
|
|
super
|
|
|
|
@name = name
|
|
|
|
@value = nil
|
|
|
|
@attributes = {}
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def appendChild node
|
|
|
|
if node.kind_of? TextNode and
|
|
|
|
childNodes.length>0 and childNodes[-1].kind_of? TextNode
|
2007-05-26 03:52:27 +02:00
|
|
|
childNodes[-1].value += node.value
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-05-26 03:52:27 +02:00
|
|
|
childNodes.push node
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
node.parent = self
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def removeChild node
|
|
|
|
childNodes.delete node
|
|
|
|
node.parent = nil
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def cloneNode
|
|
|
|
newNode = self.class.new name
|
|
|
|
attributes.each {|name,value| newNode.attributes[name] = value}
|
|
|
|
newNode.value = value
|
|
|
|
newNode
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def insertText data, before=nil
|
|
|
|
if before
|
2007-05-26 03:52:27 +02:00
|
|
|
insertBefore TextNode.new(data), before
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-05-26 03:52:27 +02:00
|
|
|
appendChild TextNode.new(data)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def insertBefore node, refNode
|
|
|
|
index = childNodes.index(refNode)
|
|
|
|
if node.kind_of? TextNode and index>0 and
|
|
|
|
childNodes[index-1].kind_of? TextNode
|
2007-05-26 03:52:27 +02:00
|
|
|
childNodes[index-1].value += node.value
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-05-26 03:52:27 +02:00
|
|
|
childNodes.insert index, node
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
tree = "\n|%s%s" % [' '* indent, self.to_s]
|
|
|
|
for child in childNodes
|
2007-05-26 03:52:27 +02:00
|
|
|
tree += child.printTree(indent + 2)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
return tree
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def hasContent
|
|
|
|
return (childNodes.length > 0)
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class Element < Node
|
|
|
|
def to_s
|
2007-06-13 00:37:55 +02:00
|
|
|
"<#{name}>"
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
tree = "\n|%s%s" % [' '* indent, self.to_s]
|
|
|
|
indent += 2
|
|
|
|
for name, value in attributes
|
2007-05-26 03:52:27 +02:00
|
|
|
tree += "\n|%s%s=\"%s\"" % [' ' * indent, name, value]
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
for child in childNodes
|
2007-05-26 03:52:27 +02:00
|
|
|
tree += child.printTree(indent)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
return tree
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class Document < Node
|
|
|
|
def to_s
|
|
|
|
"#document"
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize
|
|
|
|
super nil
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
tree = to_s
|
|
|
|
for child in childNodes
|
2007-05-26 03:52:27 +02:00
|
|
|
tree += child.printTree(indent + 2)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
return tree
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class DocumentType < Node
|
|
|
|
def to_s
|
|
|
|
"<!DOCTYPE %s>" % name
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class DocumentFragment < Element
|
|
|
|
def initialize
|
|
|
|
super nil
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
tree = ""
|
|
|
|
for child in childNodes
|
2007-05-26 03:52:27 +02:00
|
|
|
tree += child.printTree(indent+2)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
return tree
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class TextNode < Node
|
|
|
|
def initialize value
|
|
|
|
super nil
|
|
|
|
@value = value
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def to_s
|
|
|
|
'"%s"' % value
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class CommentNode < Node
|
|
|
|
def initialize value
|
|
|
|
super nil
|
|
|
|
@value = value
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def to_s
|
|
|
|
"<!-- %s -->" % value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TreeBuilder < Base::TreeBuilder
|
|
|
|
def initialize
|
|
|
|
@documentClass = Document
|
|
|
|
@doctypeClass = DocumentType
|
|
|
|
@elementClass = Element
|
|
|
|
@commentClass = CommentNode
|
|
|
|
@fragmentClass = DocumentFragment
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def testSerializer node
|
|
|
|
node.printTree()
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def getFragment
|
|
|
|
@document = super
|
|
|
|
return @document.childNodes
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|