2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/treebuilders/base'
|
2007-05-26 03:52:27 +02:00
|
|
|
require 'rexml/document'
|
|
|
|
require 'forwardable'
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
2007-05-30 17:45:52 +02:00
|
|
|
module TreeBuilders
|
2007-06-05 23:34:49 +02:00
|
|
|
module REXML
|
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
|
2007-08-30 19:19:10 +02:00
|
|
|
if node.kind_of?(TextNode) && childNodes.length > 0 && childNodes.last.kind_of?(TextNode)
|
|
|
|
childNodes.last.rxobj.value = childNodes.last.rxobj.to_s + node.rxobj.to_s
|
|
|
|
childNodes.last.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)
|
2007-08-30 19:19:10 +02:00
|
|
|
if node.kind_of?(TextNode) and index > 0 && childNodes[index-1].kind_of?(TextNode)
|
|
|
|
childNodes[index-1].rxobj.value = childNodes[index-1].rxobj.to_s + node.rxobj.to_s
|
2007-05-26 03:52:27 +02:00
|
|
|
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-06-05 23:34:49 +02:00
|
|
|
refNode.rxobj.parent.insert_before(refNode.rxobj,node.rxobj)
|
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
|
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 self.rxclass
|
2007-06-05 23:34:49 +02:00
|
|
|
::REXML::Element
|
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 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
|
2007-08-30 19:19:10 +02:00
|
|
|
value.each {|name, value| rxobj.attributes[name] = value}
|
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|#{' ' * 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
|
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 self.rxclass
|
2007-06-05 23:34:49 +02:00
|
|
|
::REXML::Document
|
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 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
|
2007-08-30 19:19:10 +02:00
|
|
|
def_delegator :@rxobj, :public, :public_id
|
|
|
|
|
|
|
|
def_delegator :@rxobj, :system, :system_id
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def self.rxclass
|
2007-06-05 23:34:49 +02:00
|
|
|
::REXML::DocType
|
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 initialize name, public_id, system_id
|
|
|
|
super(name)
|
|
|
|
if public_id
|
|
|
|
@rxobj = ::REXML::DocType.new [name, ::REXML::DocType::PUBLIC, public_id, system_id]
|
|
|
|
elsif system_id
|
|
|
|
@rxobj = ::REXML::DocType.new [name, ::REXML::DocType::SYSTEM, nil, system_id]
|
|
|
|
else
|
|
|
|
@rxobj = ::REXML::DocType.new name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2007-08-30 19:19:10 +02:00
|
|
|
raw = data.gsub('&', '&').gsub('<', '<').gsub('>', '>')
|
2007-06-05 23:34:49 +02:00
|
|
|
@rxobj = ::REXML::Text.new(raw, true, nil, true)
|
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
|
|
|
|
"\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
|
2007-06-05 23:34:49 +02:00
|
|
|
::REXML::Comment
|
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
|
|
|
|
"\n|#{' ' * indent}<!-- #{rxobj.string} -->"
|
|
|
|
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-08-30 19:19:10 +02:00
|
|
|
def insertDoctype(name, public_id, system_id)
|
|
|
|
doctype = @doctypeClass.new(name, public_id, system_id)
|
|
|
|
@document.appendChild(doctype)
|
|
|
|
end
|
|
|
|
|
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_document
|
2007-05-30 17:45:52 +02:00
|
|
|
@document.rxobj
|
|
|
|
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
|
|
|
|
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
|