2007-05-26 03:52:27 +02:00
|
|
|
require 'html5lib/treebuilders/base'
|
|
|
|
require 'rexml/document'
|
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
module HTML5lib
|
2007-05-30 17:45:52 +02:00
|
|
|
module TreeBuilders
|
|
|
|
module REXMLTree
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class Node < Base::Node
|
|
|
|
extend Forwardable
|
|
|
|
def_delegators :@rxobj, :name, :attributes
|
|
|
|
attr_accessor :rxobj
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize name
|
|
|
|
super name
|
|
|
|
@rxobj = self.class.rxclass.new name
|
|
|
|
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].rxobj.value =
|
|
|
|
childNodes[-1].rxobj.to_s + node.rxobj.to_s
|
|
|
|
childNodes[-1].rxobj.raw = true
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-05-26 03:52:27 +02:00
|
|
|
childNodes.push node
|
|
|
|
rxobj.add node.rxobj
|
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
|
|
|
|
rxobj.delete node.rxobj
|
|
|
|
node.parent = nil
|
|
|
|
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].rxobj.value =
|
|
|
|
childNodes[index-1].rxobj.to_s + node.rxobj.to_s
|
|
|
|
childNodes[index-1].rxobj.raw = true
|
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 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 self.rxclass
|
|
|
|
REXML::Element
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize name
|
|
|
|
super name
|
|
|
|
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
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def attributes= value
|
|
|
|
value.each {|name, value| rxobj.attributes[name]=value}
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
tree = "\n|#{' ' * indent}<#{name}>"
|
|
|
|
indent += 2
|
|
|
|
for name, value in attributes
|
2007-05-26 03:52:27 +02:00
|
|
|
next if name == 'xmlns'
|
|
|
|
tree += "\n|#{' ' * 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 self.rxclass
|
|
|
|
REXML::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 appendChild node
|
|
|
|
if node.kind_of? Element and node.name == 'html'
|
|
|
|
node.rxobj.add_namespace('http://www.w3.org/1999/xhtml')
|
|
|
|
end
|
|
|
|
super node
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
tree = "#document"
|
|
|
|
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 self.rxclass
|
|
|
|
REXML::DocType
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
"\n|#{' ' * indent}<!DOCTYPE #{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 data
|
|
|
|
raw=data.gsub('&','&').gsub('<','<').gsub('>','>')
|
|
|
|
@rxobj = REXML::Text.new(raw, true, nil, true)
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
"\n|#{' ' * indent}\"#{rxobj.value}\""
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class CommentNode < Node
|
|
|
|
def self.rxclass
|
|
|
|
REXML::Comment
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree indent=0
|
|
|
|
"\n|#{' ' * indent}<!-- #{rxobj.string} -->"
|
|
|
|
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 getDocument
|
|
|
|
@document.rxobj
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def getFragment
|
|
|
|
@document = super
|
|
|
|
return @document.rxobj.children
|
|
|
|
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
|