2007-05-26 03:52:27 +02:00
|
|
|
require 'html5lib/treebuilders/base'
|
2007-06-05 23:34:49 +02:00
|
|
|
require 'rubygems'
|
2007-05-26 03:52:27 +02:00
|
|
|
require 'hpricot'
|
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
module HTML5lib
|
2007-05-30 17:45:52 +02:00
|
|
|
module TreeBuilders
|
|
|
|
module Hpricot
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class Node < Base::Node
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
extend Forwardable
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def_delegators :@hpricot, :name
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
attr_accessor :hpricot
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize(name)
|
|
|
|
super(name)
|
|
|
|
@hpricot = self.class.hpricot_class.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.any? and childNodes.last.kind_of?(TextNode)
|
2007-05-26 03:52:27 +02:00
|
|
|
childNodes[-1].hpricot.content = childNodes[-1].hpricot.to_s + node.hpricot.to_s
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-05-26 03:52:27 +02:00
|
|
|
childNodes << node
|
|
|
|
hpricot.children << node.hpricot
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-06-05 23:34:49 +02:00
|
|
|
node.hpricot.parent = hpricot
|
2007-05-30 17:45:52 +02:00
|
|
|
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)
|
|
|
|
hpricot.children.delete_at(hpricot.children.index(node.hpricot))
|
2007-06-05 23:34:49 +02:00
|
|
|
node.hpricot.parent = nil
|
2007-05-30 17:45:52 +02:00
|
|
|
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].hpricot.content = childNodes[index-1].hpricot.to_s + node.hpricot.to_s
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-06-05 23:34:49 +02:00
|
|
|
refNode.hpricot.parent.insert_before(node.hpricot,refNode.hpricot)
|
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
|
|
|
|
childNodes.any?
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class Element < Node
|
|
|
|
def self.hpricot_class
|
|
|
|
::Hpricot::Elem
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize(name)
|
|
|
|
super(name)
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
@hpricot = ::Hpricot::Elem.new(::Hpricot::STag.new(name))
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def name
|
|
|
|
@hpricot.stag.name
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def cloneNode
|
|
|
|
attributes.inject(self.class.new(name)) do |node, (name, value)|
|
2007-05-26 03:52:27 +02:00
|
|
|
node.hpricot[name] = value
|
|
|
|
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
|
|
|
# A call to Hpricot::Elem#raw_attributes is built dynamically,
|
|
|
|
# so alterations to the returned value (a hash) will be lost.
|
|
|
|
#
|
|
|
|
# AttributeProxy works around this by forwarding :[]= calls
|
|
|
|
# to the raw_attributes accessor on the element start tag.
|
|
|
|
#
|
|
|
|
class AttributeProxy
|
|
|
|
def initialize(hpricot)
|
2007-05-26 03:52:27 +02:00
|
|
|
@hpricot = hpricot
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(k, v)
|
2007-05-26 03:52:27 +02:00
|
|
|
@hpricot.stag.send(stag_attributes_method)[k] = v
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def stag_attributes_method
|
2007-05-26 03:52:27 +02:00
|
|
|
# STag#attributes changed to STag#raw_attributes after Hpricot 0.5
|
|
|
|
@hpricot.stag.respond_to?(:raw_attributes) ? :raw_attributes : :attributes
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(*a, &b)
|
2007-05-26 03:52:27 +02:00
|
|
|
@hpricot.attributes.send(*a, &b)
|
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 attributes
|
|
|
|
AttributeProxy.new(@hpricot)
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def attributes=(attrs)
|
|
|
|
attrs.each { |name, value| @hpricot[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
|
|
|
|
attributes.each do |name, value|
|
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
|
|
|
|
childNodes.inject(tree) { |tree, child| tree + child.printTree(indent) }
|
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.hpricot_class
|
|
|
|
::Hpricot::Doc
|
|
|
|
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)
|
|
|
|
childNodes.inject('#document') { |tree, child| tree + child.printTree(indent + 2) }
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class DocumentType < Node
|
|
|
|
def self.hpricot_class
|
|
|
|
::Hpricot::DocType
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize(name)
|
|
|
|
begin
|
2007-05-26 03:52:27 +02:00
|
|
|
super(name)
|
2007-05-30 17:45:52 +02:00
|
|
|
rescue ArgumentError # needs 3...
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
@hpricot = ::Hpricot::DocType.new(name, nil, nil)
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree(indent=0)
|
|
|
|
"\n|#{' ' * indent}<!DOCTYPE #{hpricot.target}>"
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class DocumentFragment < Element
|
|
|
|
def initialize
|
|
|
|
super('')
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree(indent=0)
|
|
|
|
childNodes.inject('') { |tree, child| tree + child.printTree(indent+2) }
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class TextNode < Node
|
|
|
|
def initialize(data)
|
|
|
|
@hpricot = ::Hpricot::Text.new(data)
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree(indent=0)
|
|
|
|
"\n|#{' ' * indent}\"#{hpricot.content}\""
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class CommentNode < Node
|
|
|
|
def self.hpricot_class
|
|
|
|
::Hpricot::Comment
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def printTree(indent=0)
|
|
|
|
"\n|#{' ' * indent}<!-- #{hpricot.content} -->"
|
|
|
|
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.hpricot
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def getFragment
|
|
|
|
@document = super
|
|
|
|
return @document.hpricot.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
|