2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/treebuilders/base'
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
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
|
2007-08-30 19:19:10 +02:00
|
|
|
@name = name
|
|
|
|
@value = nil
|
2007-05-30 17:45:52 +02:00
|
|
|
@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
|
2007-08-30 19:19:10 +02:00
|
|
|
childNodes.length > 0 and childNodes.last.kind_of? TextNode
|
|
|
|
childNodes.last.value += node.value
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
childNodes << 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)
|
2007-08-30 19:19:10 +02:00
|
|
|
if node.kind_of?(TextNode) && index > 0 && 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
|
2007-08-30 19:19:10 +02:00
|
|
|
childNodes.length > 0
|
2007-05-30 17:45:52 +02:00
|
|
|
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
|
2007-08-30 19:19:10 +02:00
|
|
|
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
|
2007-08-30 19:19:10 +02:00
|
|
|
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
|
2007-08-30 19:19:10 +02:00
|
|
|
attr_accessor :public_id, :system_id
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def to_s
|
2007-08-30 19:19:10 +02:00
|
|
|
"<!DOCTYPE #{name}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize name
|
|
|
|
super name
|
|
|
|
@public_id = nil
|
|
|
|
@system_id = nil
|
2007-05-30 17:45:52 +02:00
|
|
|
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
|
2007-08-30 19:19:10 +02:00
|
|
|
@doctypeClass = DocumentType
|
|
|
|
@elementClass = Element
|
|
|
|
@commentClass = CommentNode
|
2007-05-30 17:45:52 +02:00
|
|
|
@fragmentClass = DocumentFragment
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def testSerializer node
|
2007-08-30 19:19:10 +02:00
|
|
|
node.printTree
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def get_fragment
|
2007-05-30 17:45:52 +02:00
|
|
|
@document = super
|
2007-10-06 18:55:58 +02:00
|
|
|
@document
|
2007-05-30 17:45:52 +02:00
|
|
|
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
|