2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/constants'
|
2007-05-26 03:52:27 +02:00
|
|
|
|
|
|
|
#XXX - TODO; make the default interface more ElementTree-like rather than DOM-like
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# The scope markers are inserted when entering buttons, object elements,
|
|
|
|
# marquees, table cells, and table captions, and are used to prevent formatting
|
|
|
|
# from "leaking" into tables, buttons, object elements, and marquees.
|
|
|
|
Marker = nil
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
module TreeBuilders
|
|
|
|
module Base
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
class Node
|
|
|
|
# The parent of the current node (or nil for the document node)
|
|
|
|
attr_accessor :parent
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# a list of child nodes of the current node. This must
|
|
|
|
# include all elements but not necessarily other node types
|
|
|
|
attr_accessor :childNodes
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# A list of miscellaneous flags that can be set on the node
|
|
|
|
attr_accessor :_flags
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize(name)
|
2007-08-30 19:19:10 +02:00
|
|
|
@parent = nil
|
2007-05-30 17:45:52 +02:00
|
|
|
@childNodes = []
|
2007-08-30 19:19:10 +02:00
|
|
|
@_flags = []
|
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
|
|
|
# Insert node as a child of the current node
|
|
|
|
def appendChild(node)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Insert data as text in the current node, positioned before the
|
|
|
|
# start of node insertBefore or to the end of the node's text.
|
|
|
|
def insertText(data, insertBefore=nil)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Insert node as a child of the current node, before refNode in the
|
|
|
|
# list of child nodes. Raises ValueError if refNode is not a child of
|
|
|
|
# the current node
|
|
|
|
def insertBefore(node, refNode)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Remove node from the children of the current node
|
|
|
|
def removeChild(node)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Move all the children of the current node to newParent.
|
|
|
|
# This is needed so that trees that don't store text as nodes move the
|
|
|
|
# text in the correct way
|
|
|
|
def reparentChildren(newParent)
|
|
|
|
#XXX - should this method be made more general?
|
|
|
|
@childNodes.each { |child| newParent.appendChild(child) }
|
|
|
|
@childNodes = []
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Return a shallow copy of the current node i.e. a node with the same
|
|
|
|
# name and attributes but with no parent or child nodes
|
|
|
|
def cloneNode
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Return true if the node has children or text, false otherwise
|
|
|
|
def hasContent
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Base treebuilder implementation
|
|
|
|
class TreeBuilder
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
attr_accessor :open_elements
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
attr_accessor :activeFormattingElements
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
attr_accessor :document
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
attr_accessor :head_pointer
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
attr_accessor :formPointer
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Class to use for document root
|
|
|
|
documentClass = nil
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Class to use for HTML elements
|
|
|
|
elementClass = nil
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Class to use for comments
|
|
|
|
commentClass = nil
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Class to use for doctypes
|
|
|
|
doctypeClass = nil
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Fragment class
|
|
|
|
fragmentClass = nil
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def initialize
|
|
|
|
reset
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def reset
|
2007-08-30 19:19:10 +02:00
|
|
|
@open_elements = []
|
2007-05-30 17:45:52 +02:00
|
|
|
@activeFormattingElements = []
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
#XXX - rename these to headElement, formElement
|
2007-08-30 19:19:10 +02:00
|
|
|
@head_pointer = nil
|
2007-05-30 17:45:52 +02:00
|
|
|
@formPointer = nil
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
self.insert_from_table = false
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
@document = @documentClass.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def elementInScope(target, tableVariant=false)
|
|
|
|
# Exit early when possible.
|
2007-08-30 19:19:10 +02:00
|
|
|
return true if @open_elements[-1].name == target
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# AT How about while true and simply set node to [-1] and set it to
|
|
|
|
# [-2] at the end...
|
2007-08-30 19:19:10 +02:00
|
|
|
@open_elements.reverse.each do |element|
|
2007-05-26 03:52:27 +02:00
|
|
|
if element.name == target
|
2007-05-30 17:45:52 +02:00
|
|
|
return true
|
2007-05-26 03:52:27 +02:00
|
|
|
elsif element.name == 'table'
|
2007-05-30 17:45:52 +02:00
|
|
|
return false
|
2007-05-26 03:52:27 +02:00
|
|
|
elsif not tableVariant and SCOPING_ELEMENTS.include?(element.name)
|
2007-05-30 17:45:52 +02:00
|
|
|
return false
|
2007-05-26 03:52:27 +02:00
|
|
|
elsif element.name == 'html'
|
2007-05-30 17:45:52 +02:00
|
|
|
return false
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
assert false # We should never reach this point
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def reconstructActiveFormattingElements
|
|
|
|
# Within this algorithm the order of steps described in the
|
|
|
|
# specification is not quite the same as the order of steps in the
|
|
|
|
# code. It should still do the same though.
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Step 1: stop the algorithm when there's nothing to do.
|
2007-06-05 23:34:49 +02:00
|
|
|
return if @activeFormattingElements.empty?
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Step 2 and step 3: we start with the last element. So i is -1.
|
|
|
|
i = -1
|
|
|
|
entry = @activeFormattingElements[i]
|
2007-08-30 19:19:10 +02:00
|
|
|
return if entry == Marker or @open_elements.include?(entry)
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Step 6
|
2007-08-30 19:19:10 +02:00
|
|
|
until entry == Marker or @open_elements.include?(entry)
|
2007-05-26 03:52:27 +02:00
|
|
|
# Step 5: let entry be one earlier in the list.
|
|
|
|
i -= 1
|
|
|
|
begin
|
2007-05-30 17:45:52 +02:00
|
|
|
entry = @activeFormattingElements[i]
|
2007-05-26 03:52:27 +02:00
|
|
|
rescue
|
2007-05-30 17:45:52 +02:00
|
|
|
# Step 4: at this point we need to jump to step 8. By not doing
|
|
|
|
# i += 1 which is also done in step 7 we achieve that.
|
|
|
|
break
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
while true
|
2007-05-26 03:52:27 +02:00
|
|
|
# Step 7
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# Step 8
|
|
|
|
clone = @activeFormattingElements[i].cloneNode
|
|
|
|
|
|
|
|
# Step 9
|
2007-08-30 19:19:10 +02:00
|
|
|
element = insert_element(clone.name, clone.attributes)
|
2007-05-26 03:52:27 +02:00
|
|
|
|
|
|
|
# Step 10
|
|
|
|
@activeFormattingElements[i] = element
|
|
|
|
|
|
|
|
# Step 11
|
|
|
|
break if element == @activeFormattingElements[-1]
|
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 clearActiveFormattingElements
|
|
|
|
{} until @activeFormattingElements.empty? || @activeFormattingElements.pop == Marker
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Check if an element exists between the end of the active
|
|
|
|
# formatting elements and the last marker. If it does, return it, else
|
|
|
|
# return false
|
|
|
|
def elementInActiveFormattingElements(name)
|
|
|
|
@activeFormattingElements.reverse.each do |element|
|
2007-05-26 03:52:27 +02:00
|
|
|
# Check for Marker first because if it's a Marker it doesn't have a
|
|
|
|
# name attribute.
|
|
|
|
break if element == Marker
|
|
|
|
return element if element.name == name
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
return false
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def insertDoctype(name, public_id, system_id)
|
|
|
|
doctype = @doctypeClass.new(name)
|
|
|
|
doctype.public_id = public_id
|
|
|
|
doctype.system_id = system_id
|
|
|
|
@document.appendChild(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 insert_comment(data, parent=nil)
|
|
|
|
parent = @open_elements[-1] if parent.nil?
|
2007-05-30 17:45:52 +02:00
|
|
|
parent.appendChild(@commentClass.new(data))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create an element but don't insert it anywhere
|
|
|
|
def createElement(name, attributes)
|
|
|
|
element = @elementClass.new(name)
|
|
|
|
element.attributes = attributes
|
|
|
|
return element
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Switch the function used to insert an element from the
|
|
|
|
# normal one to the misnested table one and back again
|
2007-08-30 19:19:10 +02:00
|
|
|
def insert_from_table=(value)
|
|
|
|
@insert_from_table = value
|
|
|
|
@insert_element = value ? :insert_elementTable : :insert_elementNormal
|
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 insert_element(name, attributes)
|
|
|
|
send(@insert_element, name, attributes)
|
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 insert_elementNormal(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
element = @elementClass.new(name)
|
|
|
|
element.attributes = attributes
|
2007-08-30 19:19:10 +02:00
|
|
|
@open_elements.last.appendChild(element)
|
|
|
|
@open_elements.push(element)
|
2007-05-30 17:45:52 +02:00
|
|
|
return element
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Create an element and insert it into the tree
|
2007-08-30 19:19:10 +02:00
|
|
|
def insert_elementTable(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
element = @elementClass.new(name)
|
|
|
|
element.attributes = attributes
|
2007-08-30 19:19:10 +02:00
|
|
|
if TABLE_INSERT_MODE_ELEMENTS.include?(@open_elements.last.name)
|
2007-05-26 03:52:27 +02:00
|
|
|
#We should be in the InTable mode. This means we want to do
|
|
|
|
#special magic element rearranging
|
|
|
|
parent, insertBefore = getTableMisnestedNodePosition
|
|
|
|
if insertBefore.nil?
|
2007-05-30 17:45:52 +02:00
|
|
|
parent.appendChild(element)
|
2007-05-26 03:52:27 +02:00
|
|
|
else
|
2007-05-30 17:45:52 +02:00
|
|
|
parent.insertBefore(element, insertBefore)
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
@open_elements.push(element)
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
return insert_elementNormal(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
return element
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def insertText(data, parent=nil)
|
2007-08-30 19:19:10 +02:00
|
|
|
parent = @open_elements[-1] if parent.nil?
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
if (not(@insert_from_table) or (@insert_from_table and not TABLE_INSERT_MODE_ELEMENTS.include?(@open_elements[-1].name)))
|
2007-05-26 03:52:27 +02:00
|
|
|
parent.insertText(data)
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-05-26 03:52:27 +02:00
|
|
|
#We should be in the InTable mode. This means we want to do
|
|
|
|
#special magic element rearranging
|
|
|
|
parent, insertBefore = getTableMisnestedNodePosition
|
|
|
|
parent.insertText(data, insertBefore)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Get the foster parent element, and sibling to insert before
|
|
|
|
# (or nil) when inserting a misnested table node
|
|
|
|
def getTableMisnestedNodePosition
|
|
|
|
#The foster parent element is the one which comes before the most
|
|
|
|
#recently opened table element
|
|
|
|
#XXX - this is really inelegant
|
|
|
|
lastTable = nil
|
|
|
|
fosterParent = nil
|
|
|
|
insertBefore = nil
|
2007-08-30 19:19:10 +02:00
|
|
|
@open_elements.reverse.each do |element|
|
2007-05-26 03:52:27 +02:00
|
|
|
if element.name == "table"
|
2007-05-30 17:45:52 +02:00
|
|
|
lastTable = element
|
|
|
|
break
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
if lastTable
|
2007-05-26 03:52:27 +02:00
|
|
|
#XXX - we should really check that this parent is actually a
|
|
|
|
#node here
|
|
|
|
if lastTable.parent
|
2007-05-30 17:45:52 +02:00
|
|
|
fosterParent = lastTable.parent
|
|
|
|
insertBefore = lastTable
|
2007-05-26 03:52:27 +02:00
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
fosterParent = @open_elements[@open_elements.index(lastTable) - 1]
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
fosterParent = @open_elements[0]
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
return fosterParent, insertBefore
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def generateImpliedEndTags(exclude=nil)
|
2007-08-30 19:19:10 +02:00
|
|
|
name = @open_elements[-1].name
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
# 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
|
2007-05-26 03:52:27 +02:00
|
|
|
# XXX This is not entirely what the specification says. We should
|
|
|
|
# investigate it more closely.
|
|
|
|
generateImpliedEndTags(exclude)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def get_document
|
2007-05-30 17:45:52 +02:00
|
|
|
@document
|
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
def get_fragment
|
|
|
|
#assert @inner_html
|
2007-05-30 17:45:52 +02:00
|
|
|
fragment = @fragmentClass.new
|
2007-08-30 19:19:10 +02:00
|
|
|
@open_elements[0].reparentChildren(fragment)
|
2007-05-30 17:45:52 +02:00
|
|
|
return fragment
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
# Serialize the subtree of node in the format required by unit tests
|
|
|
|
# node - the node from which to start serializing
|
|
|
|
def testSerializer(node)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-05-26 03:52:27 +02:00
|
|
|
end
|