Skip to content

Commit 814baeb

Browse files
committed
various tree builder improvements. mostly ported from r1107, and a few others
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%401212
1 parent af475f7 commit 814baeb

14 files changed

+142
-55
lines changed

Diff for: Manifest.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ lib/html5/html5parser/in_table_body_phase.rb
3131
lib/html5/html5parser/in_table_phase.rb
3232
lib/html5/html5parser/initial_phase.rb
3333
lib/html5/html5parser/phase.rb
34-
lib/html5/html5parser/root_element_phase.rb
34+
lib/html5/html5parser/before_html_phase.rb
3535
lib/html5/html5parser/trailing_end_phase.rb
3636
lib/html5/inputstream.rb
3737
lib/html5/liberalxmlparser.rb

Diff for: lib/html5/constants.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def self._(str); str end
1212
]
1313

1414
SCOPING_ELEMENTS = %w[
15+
applet
1516
button
1617
caption
1718
html
@@ -977,6 +978,8 @@ def self._(str); str end
977978
"unexpected-char-implies-table-voodoo" =>
978979
_("Unexpected non-space characters in " +
979980
"table context caused voodoo mode."),
981+
"unpexted-hidden-input-in-table" =>
982+
_("Unexpected input with type hidden in table context."),
980983
"unexpected-start-tag-implies-table-voodoo" =>
981984
_("Unexpected start tag (%(name)) in " +
982985
"table context caused voodoo mode."),
@@ -997,12 +1000,18 @@ def self._(str); str end
9971000
_("Unexpected end tag (%(name)) in the table row phase. Ignored."),
9981001
"unexpected-select-in-select" =>
9991002
_("Unexpected select start tag in the select phase " +
1000-
"implies select start tag."),
1003+
"treated as select end tag."),
1004+
"unexpected-input-in-select" =>
1005+
_("Unexpected input start tag in the select phase."),
10011006
"unexpected-start-tag-in-select" =>
10021007
_("Unexpected start tag token (%(name)) in the select phase. " +
10031008
"Ignored."),
10041009
"unexpected-end-tag-in-select" =>
10051010
_("Unexpected end tag (%(name)) in the select phase. Ignored."),
1011+
"unexpected-table-element-start-tag-in-select-in-table" =>
1012+
_("Unexpected table element start tag (%(name)s) in the select in table phase."),
1013+
"unexpected-table-element-end-tag-in-select-in-table" =>
1014+
_("Unexpected table element end tag (%(name)s) in the select in table phase."),
10061015
"unexpected-char-after-body" =>
10071016
_("Unexpected non-space characters in the after body phase."),
10081017
"unexpected-start-tag-after-body" =>

Diff for: lib/html5/html5parser.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def self.parse_fragment(stream, options = {})
3131
new(options).parse_fragment(stream, container, encoding)
3232
end
3333

34-
@@phases = %w( initial rootElement beforeHead inHead afterHead inBody inTable inCaption
35-
inColumnGroup inTableBody inRow inCell inSelect afterBody inFrameset afterFrameset trailingEnd )
34+
@@phases = %w( initial beforeHtml beforeHead inHead afterHead inBody inTable inCaption
35+
inColumnGroup inTableBody inRow inCell inSelect inSelectInTable afterBody inFrameset afterFrameset trailingEnd )
3636

3737
# :strict - raise an exception when a parse error is encountered
3838
# :tree - a treebuilder class controlling the type of tree that will be
@@ -80,7 +80,7 @@ def _parse(stream, inner_html, encoding, container = 'div')
8080
@tokenizer.content_model_flag = :PCDATA
8181
end
8282

83-
@phase = @phases[:rootElement]
83+
@phase = @phases[:beforeHtml]
8484
@phase.insert_html_element
8585
reset_insertion_mode
8686
else

Diff for: lib/html5/html5parser/before_head_phase.rb

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ def process_eof
1212
@parser.phase.process_eof
1313
end
1414

15+
def processSpaceCharacters(data)
16+
end
17+
1518
def processCharacters(data)
1619
startTagHead('head', {})
1720
@parser.phase.processCharacters(data)

Diff for: lib/html5/html5parser/root_element_phase.rb renamed to lib/html5/html5parser/before_html_phase.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'html5/html5parser/phase'
22

33
module HTML5
4-
class RootElementPhase < Phase
4+
class BeforeHtmlPhase < Phase
55

66
def process_eof
77
insert_html_element

Diff for: lib/html5/html5parser/in_body_phase.rb

+15-14
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ class InBodyPhase < Phase
66
# http://www.whatwg.org/specs/web-apps/current-work/#in-body
77

88
handle_start 'html'
9-
handle_start %w(base link meta script style) => 'ProcessInHead'
10-
handle_start 'title'
9+
handle_start %w(base link meta script style title) => 'ProcessInHead'
1110

1211
handle_start 'body', 'form', 'plaintext', 'a', 'button', 'xmp', 'table', 'hr', 'image'
1312

14-
handle_start 'input', 'textarea', 'select', 'isindex', %w(marquee object)
13+
handle_start 'input', 'textarea', 'select', 'isindex', %w(applet marquee object)
1514

1615
handle_start %w(li dd dt) => 'ListItem'
1716

@@ -28,7 +27,7 @@ class InBodyPhase < Phase
2827

2928
handle_start %w(event-source section nav article aside header footer datagrid command) => 'New'
3029

31-
handle_end 'p', 'body', 'html', 'form', %w(button marquee object), %w(dd dt li) => 'ListItem'
30+
handle_end 'p', 'body', 'html', 'form', %w(applet button marquee object), %w(dd dt li) => 'ListItem'
3231

3332
handle_end %w(address blockquote center div dl fieldset listing menu ol pre ul) => 'Block'
3433

@@ -64,7 +63,7 @@ class << self
6463
end
6564

6665
if (data.length > 0 and data[0] == ?\n &&
67-
%w[pre textarea].include?(@tree.open_elements.last.name) && !@tree.open_elements.last.hasContent)
66+
%w[listing pre textarea].include?(@tree.open_elements.last.name) && !@tree.open_elements.last.hasContent)
6867
data = data[1..-1]
6968
end
7069

@@ -91,11 +90,6 @@ def startTagProcessInHead(name, attributes)
9190
@parser.phases[:inHead].processStartTag(name, attributes)
9291
end
9392

94-
def startTagTitle(name, attributes)
95-
parse_error("unexpected-start-tag-out-of-my-head", {"name" => name})
96-
@parser.phases[:inHead].processStartTag(name, attributes)
97-
end
98-
9993
def startTagBody(name, attributes)
10094
parse_error("unexpected-start-tag", {"name" => "body"})
10195

@@ -113,7 +107,7 @@ def startTagBody(name, attributes)
113107
def startTagCloseP(name, attributes)
114108
endTagP('p') if in_scope?('p')
115109
@tree.insert_element(name, attributes)
116-
if name == 'pre'
110+
if ['pre', 'listing'].include?(name)
117111
class << self
118112
remove_method :processSpaceCharacters rescue nil
119113
alias processSpaceCharacters processSpaceCharactersDropNewline
@@ -220,7 +214,7 @@ def startTagButton(name, attributes)
220214
end
221215
end
222216

223-
def startTagMarqueeObject(name, attributes)
217+
def startTagAppletMarqueeObject(name, attributes)
224218
@tree.reconstructActiveFormattingElements
225219
@tree.insert_element(name, attributes)
226220
@tree.activeFormattingElements.push(Marker)
@@ -303,7 +297,14 @@ def startTagCdata(name, attributes)
303297
def startTagSelect(name, attributes)
304298
@tree.reconstructActiveFormattingElements
305299
@tree.insert_element(name, attributes)
306-
@parser.phase = @parser.phases[:inSelect]
300+
301+
if [@parser.phases[:inTable], @parser.phases[:inCaption],
302+
@parser.phases[:inColumnGroup], @parser.phases[:inTableBody], @parser.phases[:inRow],
303+
@parser.phases[:inCell]].include?(@parser.phase)
304+
@parser.phase = @parser.phases[:inSelectInTable]
305+
else
306+
@parser.phase = @parser.phases[:inSelect]
307+
end
307308
end
308309

309310
def startTagMisplaced(name, attributes)
@@ -528,7 +529,7 @@ def endTagFormatting(name)
528529
end
529530
end
530531

531-
def endTagButtonMarqueeObject(name)
532+
def endTagAppletButtonMarqueeObject(name)
532533
@tree.generateImpliedEndTags if in_scope?(name)
533534

534535
unless @tree.open_elements.last.name == name

Diff for: lib/html5/html5parser/in_head_phase.rb

+14-9
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ def startTagHead(name, attributes)
3333
end
3434

3535
def startTagTitle(name, attributes)
36-
element = @tree.createElement(name, attributes)
37-
appendToHead(element)
38-
@tree.open_elements.push(element)
36+
if @tree.head_pointer != nil# && @parser.phase == @parser.phases[:inHead]
37+
element = @tree.createElement(name, attributes)
38+
appendToHead(element)
39+
@tree.open_elements << element
40+
else
41+
@tree.insert_element(name, attributes)
42+
end
3943
@parser.tokenizer.content_model_flag = :RCDATA
4044
end
4145

4246
def startTagStyle(name, attributes)
43-
element = @tree.createElement(name, attributes)
4447
if @tree.head_pointer != nil and @parser.phase == @parser.phases[:inHead]
48+
element = @tree.createElement(name, attributes)
4549
appendToHead(element)
50+
@tree.open_elements.push(element)
4651
else
47-
@tree.open_elements.last.appendChild(element)
52+
@tree.insert_element(name, attributes)
4853
end
49-
@tree.open_elements.push(element)
5054
@parser.tokenizer.content_model_flag = :CDATA
5155
end
5256

@@ -65,7 +69,7 @@ def startTagNoscript(name, attributes)
6569
def startTagScript(name, attributes)
6670
#XXX Inner HTML case may be wrong
6771
element = @tree.createElement(name, attributes)
68-
element._flags.push("parser-inserted")
72+
element.flags.push("parser-inserted")
6973
if @tree.head_pointer != nil and @parser.phase == @parser.phases[:inHead]
7074
appendToHead(element)
7175
else
@@ -76,11 +80,12 @@ def startTagScript(name, attributes)
7680
end
7781

7882
def startTagBaseLinkMeta(name, attributes)
79-
element = @tree.createElement(name, attributes)
8083
if @tree.head_pointer != nil and @parser.phase == @parser.phases[:inHead]
84+
element = @tree.createElement(name, attributes)
8185
appendToHead(element)
8286
else
83-
@tree.open_elements.last.appendChild(element)
87+
@tree.insert_element(name, attributes)
88+
@tree.open_elements.pop
8489
end
8590
end
8691

Diff for: lib/html5/html5parser/in_row_phase.rb

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def processCharacters(data)
1313
@parser.phases[:inTable].processCharacters(data)
1414
end
1515

16+
def processSpaceCharacters(data)
17+
@parser.phases[:inTable].processSpaceCharacters(data)
18+
end
19+
1620
def startTagTableCell(name, attributes)
1721
clearStackToTableRowContext
1822
@tree.insert_element(name, attributes)

Diff for: lib/html5/html5parser/in_select_phase.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class InSelectPhase < Phase
66
# http://www.whatwg.org/specs/web-apps/current-work/#in-select
77

88
handle_start 'html', 'option', 'optgroup', 'select'
9-
9+
handle_start 'input'
1010
handle_end 'option', 'optgroup', 'select', %w( caption table tbody tfoot thead tr td th ) => 'TableElements'
1111

1212
def processCharacters(data)
@@ -30,6 +30,11 @@ def startTagSelect(name, attributes)
3030
endTagSelect('select')
3131
end
3232

33+
def startTagInput(name, attributes)
34+
@parser.parse_error("unexpected-input-in-select")
35+
endTagSelect("select")
36+
@parser.phase.processStartTag(name, attributes)
37+
end
3338
def startTagOther(name, attributes)
3439
parse_error("unexpected-start-tag-in-select", {"name" => name})
3540
end

Diff for: lib/html5/html5parser/in_table_body_phase.rb

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def processCharacters(data)
1313
@parser.phases[:inTable].processCharacters(data)
1414
end
1515

16+
def processSpaceCharacters(data)
17+
@parser.phases[:inTable].processSpaceCharacters(data)
18+
end
19+
1620
def startTagTr(name, attributes)
1721
clearStackToTableBodyContext
1822
@tree.insert_element(name, attributes)

0 commit comments

Comments
 (0)