2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/html5parser/phase'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
2007-05-30 17:45:52 +02:00
|
|
|
class InBodyPhase < Phase
|
|
|
|
|
|
|
|
# http://www.whatwg.org/specs/web-apps/current-work/#in-body
|
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
handle_start 'html'
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start %w(base link meta script style) => 'ProcessInHead'
|
2007-06-22 10:12:08 +02:00
|
|
|
handle_start 'title'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
handle_start 'body', 'form', 'plaintext', 'a', 'button', 'xmp', 'table', 'hr', 'image'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start 'input', 'textarea', 'select', 'isindex', %w(marquee object)
|
2007-06-22 10:12:08 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start %w(li dd dt) => 'ListItem'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start %w(address blockquote center dir div dl fieldset listing menu ol p pre ul) => 'CloseP'
|
|
|
|
|
|
|
|
handle_start %w(b big em font i s small strike strong tt u) => 'Formatting'
|
2007-06-22 10:12:08 +02:00
|
|
|
handle_start 'nobr'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start %w(area basefont bgsound br embed img param spacer wbr) => 'VoidFormatting'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start %w(iframe noembed noframes noscript) => 'Cdata', HEADING_ELEMENTS => 'Heading'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start %w(caption col colgroup frame frameset head option optgroup tbody td tfoot th thead tr) => 'Misplaced'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_start %w(event-source section nav article aside header footer datagrid command) => 'New'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_end 'p', 'body', 'html', 'form', %w(button marquee object), %w(dd dt li) => 'ListItem'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_end %w(address blockquote center div dl fieldset listing menu ol pre ul) => 'Block'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-06-13 00:37:55 +02:00
|
|
|
handle_end HEADING_ELEMENTS => 'Heading'
|
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_end %w(a b big em font i nobr s small strike strong tt u) => 'Formatting'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_end %w(head frameset select optgroup option table caption colgroup col thead tfoot tbody tr td th) => 'Misplaced'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
handle_end 'br'
|
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_end %w(area basefont bgsound embed hr image img input isindex param spacer wbr frame) => 'None'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_end %w(noframes noscript noembed textarea xmp iframe ) => 'CdataTextAreaXmp'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
handle_end %w(event-source section nav article aside header footer datagrid command) => 'New'
|
2007-05-30 17:45:52 +02:00
|
|
|
|
|
|
|
def initialize(parser, tree)
|
|
|
|
super(parser, tree)
|
|
|
|
|
|
|
|
# for special handling of whitespace in <pre>
|
2008-01-08 07:01:35 +01:00
|
|
|
class << self
|
|
|
|
alias processSpaceCharactersNonPre processSpaceCharacters
|
2007-08-30 19:19:10 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
2007-06-09 00:26:00 +02:00
|
|
|
def processSpaceCharactersDropNewline(data)
|
2007-08-30 19:19:10 +02:00
|
|
|
# #Sometimes (start of <pre> blocks) we want to drop leading newlines
|
|
|
|
|
2007-12-17 10:17:43 +01:00
|
|
|
class << self
|
2008-01-08 07:01:35 +01:00
|
|
|
remove_method :processSpaceCharacters rescue nil
|
|
|
|
alias processSpaceCharacters processSpaceCharactersNonPre
|
2007-08-30 19:19:10 +02:00
|
|
|
end
|
2007-12-17 10:17:43 +01:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
if (data.length > 0 and data[0] == ?\n &&
|
|
|
|
%w[pre textarea].include?(@tree.open_elements.last.name) && !@tree.open_elements.last.hasContent)
|
2007-05-30 17:45:52 +02:00
|
|
|
data = data[1..-1]
|
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
|
|
|
|
if data.length > 0
|
|
|
|
@tree.reconstructActiveFormattingElements
|
|
|
|
@tree.insertText(data)
|
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def processSpaceCharacters(data)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.reconstructActiveFormattingElements()
|
|
|
|
@tree.insertText(data)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def processCharacters(data)
|
|
|
|
# XXX The specification says to do this for every character at the
|
|
|
|
# moment, but apparently that doesn't match the real world so we don't
|
|
|
|
# do it for space characters.
|
|
|
|
@tree.reconstructActiveFormattingElements
|
|
|
|
@tree.insertText(data)
|
|
|
|
end
|
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
def startTagProcessInHead(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
@parser.phases[:inHead].processStartTag(name, attributes)
|
|
|
|
end
|
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
def startTagTitle(name, attributes)
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-start-tag-out-of-my-head", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
@parser.phases[:inHead].processStartTag(name, attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagBody(name, attributes)
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-start-tag", {"name" => "body"})
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-10-06 18:55:58 +02:00
|
|
|
if @tree.open_elements.length == 1 || @tree.open_elements[1].name != 'body'
|
2007-08-30 19:19:10 +02:00
|
|
|
assert @parser.inner_html
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
|
|
|
attributes.each do |attr, value|
|
2007-08-30 19:19:10 +02:00
|
|
|
unless @tree.open_elements[1].attributes.has_key?(attr)
|
|
|
|
@tree.open_elements[1].attributes[attr] = value
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagCloseP(name, attributes)
|
|
|
|
endTagP('p') if in_scope?('p')
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-10-28 00:34:29 +02:00
|
|
|
if name == 'pre'
|
|
|
|
class << self
|
2008-01-08 07:01:35 +01:00
|
|
|
remove_method :processSpaceCharacters rescue nil
|
|
|
|
alias processSpaceCharacters processSpaceCharactersDropNewline
|
2007-10-28 00:34:29 +02:00
|
|
|
end
|
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagForm(name, attributes)
|
|
|
|
if @tree.formPointer
|
2007-10-06 18:55:58 +02:00
|
|
|
parse_error("unexpected-start-tag", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
|
|
|
endTagP('p') if in_scope?('p')
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-10-06 18:55:58 +02:00
|
|
|
@tree.formPointer = @tree.open_elements.last
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagListItem(name, attributes)
|
|
|
|
endTagP('p') if in_scope?('p')
|
|
|
|
stopNames = {'li' => ['li'], 'dd' => ['dd', 'dt'], 'dt' => ['dd', 'dt']}
|
|
|
|
stopName = stopNames[name]
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.reverse.each_with_index do |node, i|
|
2007-05-30 17:45:52 +02:00
|
|
|
if stopName.include?(node.name)
|
2007-08-30 19:19:10 +02:00
|
|
|
poppedNodes = (0..i).collect { @tree.open_elements.pop }
|
2007-06-22 10:12:08 +02:00
|
|
|
if i >= 1
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error(
|
|
|
|
i == 1 ? "missing-end-tag" : "missing-end-tags",
|
|
|
|
{"name" => poppedNodes[0..-1].collect{|n| n.name}.join(", ")})
|
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
# Phrasing elements are all non special, non scoping, non
|
|
|
|
# formatting elements
|
2007-08-30 19:19:10 +02:00
|
|
|
break if ((SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(node.name) && !%w[address div].include?(node.name))
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Always insert an <li> element.
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagPlaintext(name, attributes)
|
|
|
|
endTagP('p') if in_scope?('p')
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
|
|
|
@parser.tokenizer.content_model_flag = :PLAINTEXT
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagHeading(name, attributes)
|
|
|
|
endTagP('p') if in_scope?('p')
|
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
# Uncomment the following for IE7 behavior:
|
|
|
|
# HEADING_ELEMENTS.each do |element|
|
|
|
|
# if in_scope?(element)
|
2007-09-10 05:26:19 +02:00
|
|
|
# parse_error("unexpected-start-tag", {"name" => name})
|
2007-06-22 10:12:08 +02:00
|
|
|
#
|
|
|
|
# remove_open_elements_until do |element|
|
|
|
|
# HEADING_ELEMENTS.include?(element.name)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# break
|
|
|
|
# end
|
|
|
|
# end
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagA(name, attributes)
|
|
|
|
if afeAElement = @tree.elementInActiveFormattingElements('a')
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-start-tag-implies-end-tag", {"startName" => "a", "endName" => "a"})
|
2007-05-30 17:45:52 +02:00
|
|
|
endTagFormatting('a')
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.delete(afeAElement) if @tree.open_elements.include?(afeAElement)
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.activeFormattingElements.delete(afeAElement) if @tree.activeFormattingElements.include?(afeAElement)
|
|
|
|
end
|
|
|
|
@tree.reconstructActiveFormattingElements
|
|
|
|
addFormattingElement(name, attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagFormatting(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
|
|
|
addFormattingElement(name, attributes)
|
|
|
|
end
|
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
def startTagNobr(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
if in_scope?('nobr')
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-start-tag-implies-end-tag", {"startName" => "nobr", "endName" => "nobr"})
|
2007-08-30 19:19:10 +02:00
|
|
|
processEndTag('nobr')
|
|
|
|
# XXX Need tests that trigger the following
|
|
|
|
@tree.reconstructActiveFormattingElements
|
|
|
|
end
|
2007-06-22 10:12:08 +02:00
|
|
|
addFormattingElement(name, attributes)
|
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def startTagButton(name, attributes)
|
|
|
|
if in_scope?('button')
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-start-tag-implies-end-tag", {"startName" => "button", "endName" => "button"})
|
2007-05-30 17:45:52 +02:00
|
|
|
processEndTag('button')
|
|
|
|
@parser.phase.processStartTag(name, attributes)
|
|
|
|
else
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.activeFormattingElements.push(Marker)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagMarqueeObject(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.activeFormattingElements.push(Marker)
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagXmp(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
|
|
|
@parser.tokenizer.content_model_flag = :CDATA
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagTable(name, attributes)
|
|
|
|
processEndTag('p') if in_scope?('p')
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
@parser.phase = @parser.phases[:inTable]
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagVoidFormatting(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
|
|
|
@tree.open_elements.pop
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagHr(name, attributes)
|
|
|
|
endTagP('p') if in_scope?('p')
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
|
|
|
@tree.open_elements.pop
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagImage(name, attributes)
|
|
|
|
# No really...
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-start-tag-treated-as", {"originalName" => "image", "newName" => "img"})
|
2007-05-30 17:45:52 +02:00
|
|
|
processStartTag('img', attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagInput(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
if @tree.formPointer
|
|
|
|
# XXX Not exactly sure what to do here
|
2007-08-30 19:19:10 +02:00
|
|
|
# @tree.open_elements[-1].form = @tree.formPointer
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.pop
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagIsindex(name, attributes)
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("deprecated-tag", {"name" => "isindex"})
|
2007-05-30 17:45:52 +02:00
|
|
|
return if @tree.formPointer
|
|
|
|
processStartTag('form', {})
|
|
|
|
processStartTag('hr', {})
|
|
|
|
processStartTag('p', {})
|
|
|
|
processStartTag('label', {})
|
|
|
|
# XXX Localization ...
|
2007-08-30 19:19:10 +02:00
|
|
|
processCharacters('This is a searchable index. Insert your search keywords here: ')
|
2007-05-30 17:45:52 +02:00
|
|
|
attributes['name'] = 'isindex'
|
|
|
|
attrs = attributes.to_a
|
|
|
|
processStartTag('input', attributes)
|
|
|
|
processEndTag('label')
|
|
|
|
processEndTag('p')
|
|
|
|
processStartTag('hr', {})
|
|
|
|
processEndTag('form')
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagTextarea(name, attributes)
|
|
|
|
# XXX Form element pointer checking here as well...
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
|
|
|
@parser.tokenizer.content_model_flag = :RCDATA
|
2008-01-08 07:01:35 +01:00
|
|
|
class << self
|
|
|
|
remove_method :processSpaceCharacters rescue nil
|
|
|
|
alias processSpaceCharacters processSpaceCharactersDropNewline
|
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# iframe, noembed noframes, noscript(if scripting enabled)
|
|
|
|
def startTagCdata(name, attributes)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
|
|
|
@parser.tokenizer.content_model_flag = :CDATA
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagSelect(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
@parser.phase = @parser.phases[:inSelect]
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagMisplaced(name, attributes)
|
|
|
|
# Elements that should be children of other elements that have a
|
|
|
|
# different insertion mode; here they are ignored
|
|
|
|
# "caption", "col", "colgroup", "frame", "frameset", "head",
|
|
|
|
# "option", "optgroup", "tbody", "td", "tfoot", "th", "thead",
|
|
|
|
# "tr", "noscript"
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-start-tag-ignored", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def startTagNew(name, attributes)
|
|
|
|
# New HTML5 elements, "event-source", "section", "nav",
|
|
|
|
# "article", "aside", "header", "footer", "datagrid", "command"
|
2007-09-10 05:26:19 +02:00
|
|
|
# $stderr.puts("Warning: Undefined behaviour for start tag #{name}")
|
2007-05-30 17:45:52 +02:00
|
|
|
startTagOther(name, attributes)
|
|
|
|
#raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def startTagOther(name, attributes)
|
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def endTagP(name)
|
|
|
|
@tree.generateImpliedEndTags('p') if in_scope?('p')
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-end-tag", {"name" => "p"}) unless @tree.open_elements.last.name == 'p'
|
2007-07-05 00:36:59 +02:00
|
|
|
if in_scope?('p')
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.pop while in_scope?('p')
|
2007-07-05 00:36:59 +02:00
|
|
|
else
|
|
|
|
startTagCloseP('p', {})
|
|
|
|
endTagP('p')
|
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def endTagBody(name)
|
|
|
|
# XXX Need to take open <p> tags into account here. We shouldn't imply
|
|
|
|
# </p> but we should not throw a parse error either. Specification is
|
|
|
|
# likely to be updated.
|
2007-10-28 00:34:29 +02:00
|
|
|
unless @tree.open_elements[1] && @tree.open_elements[1].name == 'body'
|
2007-08-30 19:19:10 +02:00
|
|
|
# inner_html case
|
2007-10-28 00:34:29 +02:00
|
|
|
parse_error "unexpected-end-tag", {:name => 'body'}
|
2007-05-30 17:45:52 +02:00
|
|
|
return
|
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
unless @tree.open_elements.last.name == 'body'
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("expected-one-end-tag-but-got-another",
|
|
|
|
{"expectedName" => "body",
|
|
|
|
"gotName" => @tree.open_elements.last.name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
@parser.phase = @parser.phases[:afterBody]
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagHtml(name)
|
|
|
|
endTagBody(name)
|
2007-08-30 19:19:10 +02:00
|
|
|
@parser.phase.processEndTag(name) unless @parser.inner_html
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def endTagBlock(name)
|
|
|
|
@tree.generateImpliedEndTags if in_scope?(name)
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
unless @tree.open_elements.last.name == name
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("end-tag-too-early", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if in_scope?(name)
|
|
|
|
remove_open_elements_until(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagForm(name)
|
2007-07-05 00:36:59 +02:00
|
|
|
if in_scope?(name)
|
|
|
|
@tree.generateImpliedEndTags
|
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
if @tree.open_elements.last.name != name
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("end-tag-too-early-ignored", {"name" => "form"})
|
2007-07-05 00:36:59 +02:00
|
|
|
else
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.pop
|
2007-07-05 00:36:59 +02:00
|
|
|
end
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.formPointer = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagListItem(name)
|
|
|
|
# AT Could merge this with the Block case
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.generateImpliedEndTags(name) if in_scope?(name)
|
2007-05-30 17:45:52 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
unless @tree.open_elements.last.name == name
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("end-tag-too-early", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
remove_open_elements_until(name) if in_scope?(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagHeading(name)
|
|
|
|
HEADING_ELEMENTS.each do |element|
|
|
|
|
if in_scope?(element)
|
|
|
|
@tree.generateImpliedEndTags
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
unless @tree.open_elements.last.name == name
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("end-tag-too-early", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
HEADING_ELEMENTS.each do |element|
|
|
|
|
if in_scope?(element)
|
2007-08-30 19:19:10 +02:00
|
|
|
remove_open_elements_until {|element| HEADING_ELEMENTS.include?(element.name)}
|
2007-05-30 17:45:52 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# The much-feared adoption agency algorithm
|
|
|
|
def endTagFormatting(name)
|
|
|
|
# http://www.whatwg.org/specs/web-apps/current-work/#adoptionAgency
|
2007-08-30 19:19:10 +02:00
|
|
|
# XXX Better parse_error messages appreciated.
|
2007-05-30 17:45:52 +02:00
|
|
|
while true
|
|
|
|
# Step 1 paragraph 1
|
|
|
|
afeElement = @tree.elementInActiveFormattingElements(name)
|
2007-08-30 19:19:10 +02:00
|
|
|
if !afeElement or (@tree.open_elements.include?(afeElement) && !in_scope?(afeElement.name))
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("adoption-agency-1.1", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
return
|
|
|
|
# Step 1 paragraph 2
|
2007-08-30 19:19:10 +02:00
|
|
|
elsif not @tree.open_elements.include?(afeElement)
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("adoption-agency-1.2", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.activeFormattingElements.delete(afeElement)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# Step 1 paragraph 3
|
2007-08-30 19:19:10 +02:00
|
|
|
if afeElement != @tree.open_elements.last
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("adoption-agency-1.3", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Step 2
|
|
|
|
# Start of the adoption agency algorithm proper
|
2007-08-30 19:19:10 +02:00
|
|
|
afeIndex = @tree.open_elements.index(afeElement)
|
2007-05-30 17:45:52 +02:00
|
|
|
furthestBlock = nil
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements[afeIndex..-1].each do |element|
|
2007-05-30 17:45:52 +02:00
|
|
|
if (SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(element.name)
|
|
|
|
furthestBlock = element
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Step 3
|
|
|
|
if furthestBlock.nil?
|
2007-08-30 19:19:10 +02:00
|
|
|
element = remove_open_elements_until {|element| element == afeElement }
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.activeFormattingElements.delete(element)
|
|
|
|
return
|
|
|
|
end
|
2007-08-30 19:19:10 +02:00
|
|
|
commonAncestor = @tree.open_elements[afeIndex - 1]
|
2007-05-30 17:45:52 +02:00
|
|
|
|
|
|
|
# Step 5
|
|
|
|
furthestBlock.parent.removeChild(furthestBlock) if furthestBlock.parent
|
|
|
|
|
|
|
|
# Step 6
|
|
|
|
# The bookmark is supposed to help us identify where to reinsert
|
|
|
|
# nodes in step 12. We have to ensure that we reinsert nodes after
|
|
|
|
# the node before the active formatting element. Note the bookmark
|
|
|
|
# can move in step 7.4
|
|
|
|
bookmark = @tree.activeFormattingElements.index(afeElement)
|
|
|
|
|
|
|
|
# Step 7
|
|
|
|
lastNode = node = furthestBlock
|
|
|
|
while true
|
|
|
|
# AT replace this with a function and recursion?
|
|
|
|
# Node is element before node in open elements
|
2007-08-30 19:19:10 +02:00
|
|
|
node = @tree.open_elements[@tree.open_elements.index(node) - 1]
|
2007-05-30 17:45:52 +02:00
|
|
|
until @tree.activeFormattingElements.include?(node)
|
|
|
|
tmpNode = node
|
2007-08-30 19:19:10 +02:00
|
|
|
node = @tree.open_elements[@tree.open_elements.index(node) - 1]
|
|
|
|
@tree.open_elements.delete(tmpNode)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
# Step 7.3
|
|
|
|
break if node == afeElement
|
|
|
|
# Step 7.4
|
|
|
|
if lastNode == furthestBlock
|
|
|
|
# XXX should this be index(node) or index(node)+1
|
|
|
|
# Anne: I think +1 is ok. Given x = [2,3,4,5]
|
|
|
|
# x.index(3) gives 1 and then x[1 +1] gives 4...
|
|
|
|
bookmark = @tree.activeFormattingElements.index(node) + 1
|
|
|
|
end
|
|
|
|
# Step 7.5
|
|
|
|
cite = node.parent
|
|
|
|
if node.hasContent
|
|
|
|
clone = node.cloneNode
|
|
|
|
# Replace node with clone
|
|
|
|
@tree.activeFormattingElements[@tree.activeFormattingElements.index(node)] = clone
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements[@tree.open_elements.index(node)] = clone
|
2007-05-30 17:45:52 +02:00
|
|
|
node = clone
|
|
|
|
end
|
|
|
|
# Step 7.6
|
|
|
|
# Remove lastNode from its parents, if any
|
|
|
|
lastNode.parent.removeChild(lastNode) if lastNode.parent
|
|
|
|
node.appendChild(lastNode)
|
|
|
|
# Step 7.7
|
|
|
|
lastNode = node
|
|
|
|
# End of inner loop
|
|
|
|
end
|
|
|
|
|
|
|
|
# Step 8
|
|
|
|
lastNode.parent.removeChild(lastNode) if lastNode.parent
|
|
|
|
commonAncestor.appendChild(lastNode)
|
|
|
|
|
|
|
|
# Step 9
|
|
|
|
clone = afeElement.cloneNode
|
|
|
|
|
|
|
|
# Step 10
|
|
|
|
furthestBlock.reparentChildren(clone)
|
|
|
|
|
|
|
|
# Step 11
|
|
|
|
furthestBlock.appendChild(clone)
|
|
|
|
|
|
|
|
# Step 12
|
|
|
|
@tree.activeFormattingElements.delete(afeElement)
|
|
|
|
@tree.activeFormattingElements.insert([bookmark,@tree.activeFormattingElements.length].min, clone)
|
|
|
|
|
|
|
|
# Step 13
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.delete(afeElement)
|
|
|
|
@tree.open_elements.insert(@tree.open_elements.index(furthestBlock) + 1, clone)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagButtonMarqueeObject(name)
|
|
|
|
@tree.generateImpliedEndTags if in_scope?(name)
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
unless @tree.open_elements.last.name == name
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("end-tag-too-early", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if in_scope?(name)
|
|
|
|
remove_open_elements_until(name)
|
2007-08-30 19:19:10 +02:00
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
@tree.clearActiveFormattingElements
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagMisplaced(name)
|
|
|
|
# This handles elements with end tags in other insertion modes.
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-end-tag", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
def endTagBr(name)
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-end-tag-treated-as",
|
|
|
|
{"originalName" => "br", "newName" => "br element"})
|
2007-06-22 10:12:08 +02:00
|
|
|
@tree.reconstructActiveFormattingElements
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, {})
|
|
|
|
@tree.open_elements.pop()
|
2007-06-22 10:12:08 +02:00
|
|
|
end
|
|
|
|
|
2007-05-30 17:45:52 +02:00
|
|
|
def endTagNone(name)
|
|
|
|
# This handles elements with no end tag.
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("no-end-tag", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def endTagCdataTextAreaXmp(name)
|
2007-08-30 19:19:10 +02:00
|
|
|
if @tree.open_elements.last.name == name
|
|
|
|
@tree.open_elements.pop
|
2007-05-30 17:45:52 +02:00
|
|
|
else
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-end-tag", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagNew(name)
|
|
|
|
# New HTML5 elements, "event-source", "section", "nav",
|
|
|
|
# "article", "aside", "header", "footer", "datagrid", "command"
|
2007-09-10 05:26:19 +02:00
|
|
|
# STDERR.puts "Warning: Undefined behaviour for end tag #{name}"
|
2007-05-30 17:45:52 +02:00
|
|
|
endTagOther(name)
|
|
|
|
#raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def endTagOther(name)
|
|
|
|
# XXX This logic should be moved into the treebuilder
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.open_elements.reverse.each do |node|
|
2007-05-30 17:45:52 +02:00
|
|
|
if node.name == name
|
|
|
|
@tree.generateImpliedEndTags
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
unless @tree.open_elements.last.name == name
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-end-tag", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
remove_open_elements_until {|element| element == node }
|
2007-05-30 17:45:52 +02:00
|
|
|
|
|
|
|
break
|
|
|
|
else
|
|
|
|
if (SPECIAL_ELEMENTS + SCOPING_ELEMENTS).include?(node.name)
|
2007-09-10 05:26:19 +02:00
|
|
|
parse_error("unexpected-end-tag", {"name" => name})
|
2007-05-30 17:45:52 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def addFormattingElement(name, attributes)
|
2007-08-30 19:19:10 +02:00
|
|
|
@tree.insert_element(name, attributes)
|
|
|
|
@tree.activeFormattingElements.push(@tree.open_elements.last)
|
2007-05-30 17:45:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2007-06-09 00:26:00 +02:00
|
|
|
end
|