Minor S5 tweaks and Sync with Latest HTML5lib

This commit is contained in:
Jacques Distler 2007-08-30 12:19:10 -05:00
parent dbed460843
commit 81d3cdc8e4
81 changed files with 9887 additions and 1687 deletions

View file

@ -24,9 +24,9 @@ module HTML5
attr_accessor :_flags
def initialize(name)
@parent = nil
@parent = nil
@childNodes = []
@_flags = []
@_flags = []
end
# Insert node as a child of the current node
@ -76,13 +76,13 @@ module HTML5
# Base treebuilder implementation
class TreeBuilder
attr_accessor :openElements
attr_accessor :open_elements
attr_accessor :activeFormattingElements
attr_accessor :document
attr_accessor :headPointer
attr_accessor :head_pointer
attr_accessor :formPointer
@ -106,25 +106,25 @@ module HTML5
end
def reset
@openElements = []
@open_elements = []
@activeFormattingElements = []
#XXX - rename these to headElement, formElement
@headPointer = nil
@head_pointer = nil
@formPointer = nil
self.insertFromTable = false
self.insert_from_table = false
@document = @documentClass.new
end
def elementInScope(target, tableVariant=false)
# Exit early when possible.
return true if @openElements[-1].name == target
return true if @open_elements[-1].name == target
# AT How about while true and simply set node to [-1] and set it to
# [-2] at the end...
@openElements.reverse.each do |element|
@open_elements.reverse.each do |element|
if element.name == target
return true
elsif element.name == 'table'
@ -149,10 +149,10 @@ module HTML5
# Step 2 and step 3: we start with the last element. So i is -1.
i = -1
entry = @activeFormattingElements[i]
return if entry == Marker or @openElements.include?(entry)
return if entry == Marker or @open_elements.include?(entry)
# Step 6
until entry == Marker or @openElements.include?(entry)
until entry == Marker or @open_elements.include?(entry)
# Step 5: let entry be one earlier in the list.
i -= 1
begin
@ -171,7 +171,7 @@ module HTML5
clone = @activeFormattingElements[i].cloneNode
# Step 9
element = insertElement(clone.name, clone.attributes)
element = insert_element(clone.name, clone.attributes)
# Step 10
@activeFormattingElements[i] = element
@ -198,12 +198,15 @@ module HTML5
return false
end
def insertDoctype(name)
@document.appendChild(@doctypeClass.new(name))
def insertDoctype(name, public_id, system_id)
doctype = @doctypeClass.new(name)
doctype.public_id = public_id
doctype.system_id = system_id
@document.appendChild(doctype)
end
def insertComment(data, parent=nil)
parent = @openElements[-1] if parent.nil?
def insert_comment(data, parent=nil)
parent = @open_elements[-1] if parent.nil?
parent.appendChild(@commentClass.new(data))
end
@ -216,28 +219,28 @@ module HTML5
# Switch the function used to insert an element from the
# normal one to the misnested table one and back again
def insertFromTable=(value)
@insertFromTable = value
@insertElement = value ? :insertElementTable : :insertElementNormal
def insert_from_table=(value)
@insert_from_table = value
@insert_element = value ? :insert_elementTable : :insert_elementNormal
end
def insertElement(name, attributes)
send(@insertElement, name, attributes)
def insert_element(name, attributes)
send(@insert_element, name, attributes)
end
def insertElementNormal(name, attributes)
def insert_elementNormal(name, attributes)
element = @elementClass.new(name)
element.attributes = attributes
@openElements[-1].appendChild(element)
@openElements.push(element)
@open_elements.last.appendChild(element)
@open_elements.push(element)
return element
end
# Create an element and insert it into the tree
def insertElementTable(name, attributes)
def insert_elementTable(name, attributes)
element = @elementClass.new(name)
element.attributes = attributes
if TABLE_INSERT_MODE_ELEMENTS.include?(@openElements[-1].name)
if TABLE_INSERT_MODE_ELEMENTS.include?(@open_elements.last.name)
#We should be in the InTable mode. This means we want to do
#special magic element rearranging
parent, insertBefore = getTableMisnestedNodePosition
@ -246,17 +249,17 @@ module HTML5
else
parent.insertBefore(element, insertBefore)
end
@openElements.push(element)
@open_elements.push(element)
else
return insertElementNormal(name, attributes)
return insert_elementNormal(name, attributes)
end
return element
end
def insertText(data, parent=nil)
parent = @openElements[-1] if parent.nil?
parent = @open_elements[-1] if parent.nil?
if (not(@insertFromTable) or (@insertFromTable and not TABLE_INSERT_MODE_ELEMENTS.include?(@openElements[-1].name)))
if (not(@insert_from_table) or (@insert_from_table and not TABLE_INSERT_MODE_ELEMENTS.include?(@open_elements[-1].name)))
parent.insertText(data)
else
#We should be in the InTable mode. This means we want to do
@ -265,7 +268,7 @@ module HTML5
parent.insertText(data, insertBefore)
end
end
# Get the foster parent element, and sibling to insert before
# (or nil) when inserting a misnested table node
def getTableMisnestedNodePosition
@ -275,7 +278,7 @@ module HTML5
lastTable = nil
fosterParent = nil
insertBefore = nil
@openElements.reverse.each do |element|
@open_elements.reverse.each do |element|
if element.name == "table"
lastTable = element
break
@ -288,33 +291,34 @@ module HTML5
fosterParent = lastTable.parent
insertBefore = lastTable
else
fosterParent = @openElements[@openElements.index(lastTable) - 1]
fosterParent = @open_elements[@open_elements.index(lastTable) - 1]
end
else
fosterParent = @openElements[0]
fosterParent = @open_elements[0]
end
return fosterParent, insertBefore
end
def generateImpliedEndTags(exclude=nil)
name = @openElements[-1].name
name = @open_elements[-1].name
if (['dd', 'dt', 'li', 'p', 'td', 'th', 'tr'].include?(name) and name != exclude)
@openElements.pop
# XXX td, th and tr are not actually needed
if (%w[dd dt li p td th tr].include?(name) and name != exclude)
@open_elements.pop
# XXX This is not entirely what the specification says. We should
# investigate it more closely.
generateImpliedEndTags(exclude)
end
end
def getDocument
def get_document
@document
end
def getFragment
#assert @innerHTML
def get_fragment
#assert @inner_html
fragment = @fragmentClass.new
@openElements[0].reparentChildren(fragment)
@open_elements[0].reparentChildren(fragment)
return fragment
end

View file

@ -8,7 +8,6 @@ module HTML5
module Hpricot
class Node < Base::Node
extend Forwardable
def_delegators :@hpricot, :name
@ -22,7 +21,7 @@ module HTML5
def appendChild(node)
if node.kind_of?(TextNode) and childNodes.any? and childNodes.last.kind_of?(TextNode)
childNodes[-1].hpricot.content = childNodes[-1].hpricot.to_s + node.hpricot.to_s
childNodes.last.hpricot.content = childNodes.last.hpricot.content + node.hpricot.content
else
childNodes << node
hpricot.children << node.hpricot
@ -145,21 +144,27 @@ module HTML5
end
class DocumentType < Node
def_delegators :@hpricot, :public_id, :system_id
def self.hpricot_class
::Hpricot::DocType
end
def initialize(name)
def initialize(name, public_id, system_id)
begin
super(name)
rescue ArgumentError # needs 3...
end
@hpricot = ::Hpricot::DocType.new(name, nil, nil)
@hpricot = ::Hpricot::DocType.new(name, public_id, system_id)
end
def printTree(indent=0)
"\n|#{' ' * indent}<!DOCTYPE #{hpricot.target}>"
if hpricot.target and hpricot.target.any?
"\n|#{' ' * indent}<!DOCTYPE #{hpricot.target}>"
else
"\n|#{' ' * indent}<!DOCTYPE >"
end
end
end
@ -169,7 +174,7 @@ module HTML5
end
def printTree(indent=0)
childNodes.inject('') { |tree, child| tree + child.printTree(indent+2) }
childNodes.inject('') {|tree, child| tree + child.printTree(indent + 2) }
end
end
@ -196,21 +201,26 @@ module HTML5
class TreeBuilder < Base::TreeBuilder
def initialize
@documentClass = Document
@doctypeClass = DocumentType
@elementClass = Element
@commentClass = CommentNode
@doctypeClass = DocumentType
@elementClass = Element
@commentClass = CommentNode
@fragmentClass = DocumentFragment
end
def insertDoctype(name, public_id, system_id)
doctype = @doctypeClass.new(name, public_id, system_id)
@document.appendChild(doctype)
end
def testSerializer(node)
node.printTree
end
def getDocument
def get_document
@document.hpricot
end
def getFragment
def get_fragment
@document = super
return @document.hpricot.children
end

View file

@ -17,11 +17,9 @@ module HTML5
end
def appendChild node
if node.kind_of? TextNode and
childNodes.length>0 and childNodes[-1].kind_of? TextNode
childNodes[-1].rxobj.value =
childNodes[-1].rxobj.to_s + node.rxobj.to_s
childNodes[-1].rxobj.raw = true
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
else
childNodes.push node
rxobj.add node.rxobj
@ -45,10 +43,8 @@ module HTML5
def insertBefore node, refNode
index = childNodes.index(refNode)
if node.kind_of? TextNode and index>0 and
childNodes[index-1].kind_of? TextNode
childNodes[index-1].rxobj.value =
childNodes[index-1].rxobj.to_s + node.rxobj.to_s
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
childNodes[index-1].rxobj.raw = true
else
childNodes.insert index, node
@ -57,7 +53,7 @@ module HTML5
end
def hasContent
return (childNodes.length > 0)
(childNodes.length > 0)
end
end
@ -77,7 +73,7 @@ module HTML5
end
def attributes= value
value.each {|name, value| rxobj.attributes[name]=value}
value.each {|name, value| rxobj.attributes[name] = value}
end
def printTree indent=0
@ -90,7 +86,7 @@ module HTML5
for child in childNodes
tree += child.printTree(indent)
end
return tree
tree
end
end
@ -120,10 +116,25 @@ module HTML5
end
class DocumentType < Node
def_delegator :@rxobj, :public, :public_id
def_delegator :@rxobj, :system, :system_id
def self.rxclass
::REXML::DocType
end
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
def printTree indent=0
"\n|#{' ' * indent}<!DOCTYPE #{name}>"
end
@ -145,7 +156,7 @@ module HTML5
class TextNode < Node
def initialize data
raw=data.gsub('&','&amp;').gsub('<','&lt;').gsub('>','&gt;')
raw = data.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')
@rxobj = ::REXML::Text.new(raw, true, nil, true)
end
@ -167,21 +178,26 @@ module HTML5
class TreeBuilder < Base::TreeBuilder
def initialize
@documentClass = Document
@doctypeClass = DocumentType
@elementClass = Element
@commentClass = CommentNode
@doctypeClass = DocumentType
@elementClass = Element
@commentClass = CommentNode
@fragmentClass = DocumentFragment
end
def testSerializer node
node.printTree()
def insertDoctype(name, public_id, system_id)
doctype = @doctypeClass.new(name, public_id, system_id)
@document.appendChild(doctype)
end
def getDocument
def testSerializer node
node.printTree
end
def get_document
@document.rxobj
end
def getFragment
def get_fragment
@document = super
return @document.rxobj.children
end

View file

@ -18,17 +18,17 @@ module HTML5
def initialize name
super
@name = name
@value = nil
@name = name
@value = nil
@attributes = {}
end
def appendChild node
if node.kind_of? TextNode and
childNodes.length>0 and childNodes[-1].kind_of? TextNode
childNodes[-1].value += node.value
childNodes.length > 0 and childNodes.last.kind_of? TextNode
childNodes.last.value += node.value
else
childNodes.push node
childNodes << node
end
node.parent = self
end
@ -55,8 +55,7 @@ module HTML5
def insertBefore node, refNode
index = childNodes.index(refNode)
if node.kind_of? TextNode and index>0 and
childNodes[index-1].kind_of? TextNode
if node.kind_of?(TextNode) && index > 0 && childNodes[index-1].kind_of?(TextNode)
childNodes[index-1].value += node.value
else
childNodes.insert index, node
@ -72,7 +71,7 @@ module HTML5
end
def hasContent
return (childNodes.length > 0)
childNodes.length > 0
end
end
@ -90,7 +89,7 @@ module HTML5
for child in childNodes
tree += child.printTree(indent)
end
return tree
tree
end
end
@ -108,13 +107,21 @@ module HTML5
for child in childNodes
tree += child.printTree(indent + 2)
end
return tree
tree
end
end
class DocumentType < Node
attr_accessor :public_id, :system_id
def to_s
"<!DOCTYPE %s>" % name
"<!DOCTYPE #{name}>"
end
def initialize name
super name
@public_id = nil
@system_id = nil
end
end
@ -157,19 +164,19 @@ module HTML5
class TreeBuilder < Base::TreeBuilder
def initialize
@documentClass = Document
@doctypeClass = DocumentType
@elementClass = Element
@commentClass = CommentNode
@doctypeClass = DocumentType
@elementClass = Element
@commentClass = CommentNode
@fragmentClass = DocumentFragment
end
def testSerializer node
node.printTree()
node.printTree
end
def getFragment
def get_fragment
@document = super
return @document.childNodes
@document.childNodes
end
end