Allow script/template in various table elements

Fixes #105
This commit is contained in:
Yorick Peterse 2015-05-23 10:46:49 +02:00
parent 5182d0c488
commit d0d597e2d9
6 changed files with 128 additions and 28 deletions

View File

@ -50,6 +50,10 @@ module Oga
%w{thead tbody tfoot tr caption colgroup col} %w{thead tbody tfoot tr caption colgroup col}
) )
HTML_SCRIPT_ELEMENTS = Whitelist.new(%w{script template})
HTML_TABLE_ROW_ELEMENTS = Whitelist.new(%w{tr}) + HTML_SCRIPT_ELEMENTS
# Elements that should be closed automatically before a new opening tag is # Elements that should be closed automatically before a new opening tag is
# processed. # processed.
HTML_CLOSE_SELF = { HTML_CLOSE_SELF = {
@ -71,11 +75,11 @@ module Oga
'option' => Blacklist.new(%w{optgroup option}), 'option' => Blacklist.new(%w{optgroup option}),
'colgroup' => Whitelist.new(%w{col template}), 'colgroup' => Whitelist.new(%w{col template}),
'caption' => HTML_TABLE_ALLOWED.to_blacklist, 'caption' => HTML_TABLE_ALLOWED.to_blacklist,
'table' => HTML_TABLE_ALLOWED, 'table' => HTML_TABLE_ALLOWED + HTML_SCRIPT_ELEMENTS,
'thead' => Whitelist.new(%w{tr}), 'thead' => HTML_TABLE_ROW_ELEMENTS,
'tbody' => Whitelist.new(%w{tr}), 'tbody' => HTML_TABLE_ROW_ELEMENTS,
'tfoot' => Whitelist.new(%w{tr}), 'tfoot' => HTML_TABLE_ROW_ELEMENTS,
'tr' => Whitelist.new(%w{td th}), 'tr' => Whitelist.new(%w{td th}) + HTML_SCRIPT_ELEMENTS,
'td' => Blacklist.new(%w{td th}) + HTML_TABLE_ALLOWED, 'td' => Blacklist.new(%w{td th}) + HTML_TABLE_ALLOWED,
'th' => Blacklist.new(%w{td th}) + HTML_TABLE_ALLOWED 'th' => Blacklist.new(%w{td th}) + HTML_TABLE_ALLOWED
} }

View File

@ -1,30 +1,46 @@
require 'spec_helper' require 'spec_helper'
describe Oga::XML::Lexer do describe Oga::XML::Lexer do
describe 'HTML tables' do describe 'using HTML <table> elements' do
describe 'with unclosed <tr> tags' do it 'lexes two unclosed <table> elements following each other as separate elements' do
it 'lexes a <tr> tag followed by a <tbody> tag' do lex_html('<table>foo<table>bar').should == [
lex_html('<tr>foo<tbody></tbody>').should == [ [:T_ELEM_NAME, 'table', 1],
[:T_ELEM_NAME, 'tr', 1], [:T_TEXT, 'foo', 1],
[:T_TEXT, 'foo', 1], [:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1], [:T_ELEM_NAME, 'table', 1],
[:T_ELEM_NAME, 'tbody', 1], [:T_TEXT, 'bar', 1],
[:T_ELEM_END, nil, 1] [:T_ELEM_END, nil, 1]
] ]
end end
it 'lexes an unclosed <th> tag followed by a <tbody> tag' do it 'lexes a <table> element containing a <thead> element' do
lex_html('<tr><th>foo<tbody>bar</tbody>').should == [ lex_html('<table><thead>foo</thead></table>').should == [
[:T_ELEM_NAME, 'tr', 1], [:T_ELEM_NAME, 'table', 1],
[:T_ELEM_NAME, 'th', 1], [:T_ELEM_NAME, 'thead', 1],
[:T_TEXT, 'foo', 1], [:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1], [:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1], [:T_ELEM_END, nil, 1]
[:T_ELEM_NAME, 'tbody', 1], ]
[:T_TEXT, 'bar', 1], end
[:T_ELEM_END, nil, 1]
] it 'lexes a <table> element containing a <script> element' do
end lex_html('<table><script>foo</script></table>').should == [
[:T_ELEM_NAME, 'table', 1],
[:T_ELEM_NAME, 'script', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
it 'lexes a <table> element containing a <template> element' do
lex_html('<table><template>foo</template></table>').should == [
[:T_ELEM_NAME, 'table', 1],
[:T_ELEM_NAME, 'template', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end end
end end
end end

View File

@ -33,5 +33,25 @@ describe Oga::XML::Lexer do
[:T_ELEM_END, nil, 1] [:T_ELEM_END, nil, 1]
] ]
end end
it 'lexes a <tbody> element containing a <script> element' do
lex_html('<tbody><script>foo</script></tbody>').should == [
[:T_ELEM_NAME, 'tbody', 1],
[:T_ELEM_NAME, 'script', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
it 'lexes a <tbody> element containing a <template> element' do
lex_html('<tbody><template>foo</template></tbody>').should == [
[:T_ELEM_NAME, 'tbody', 1],
[:T_ELEM_NAME, 'template', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
end end
end end

View File

@ -33,5 +33,25 @@ describe Oga::XML::Lexer do
[:T_ELEM_END, nil, 1] [:T_ELEM_END, nil, 1]
] ]
end end
it 'lexes a <tfoot> element containing a <script> element' do
lex_html('<tfoot><script>foo</script></tfoot>').should == [
[:T_ELEM_NAME, 'tfoot', 1],
[:T_ELEM_NAME, 'script', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
it 'lexes a <tfoot> element containing a <template> element' do
lex_html('<tfoot><template>foo</template></tfoot>').should == [
[:T_ELEM_NAME, 'tfoot', 1],
[:T_ELEM_NAME, 'template', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
end end
end end

View File

@ -33,5 +33,25 @@ describe Oga::XML::Lexer do
[:T_ELEM_END, nil, 1] [:T_ELEM_END, nil, 1]
] ]
end end
it 'lexes a <thead> element containing a <script> element' do
lex_html('<thead><script>foo</script></thead>').should == [
[:T_ELEM_NAME, 'thead', 1],
[:T_ELEM_NAME, 'script', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
it 'lexes a <thead> element containing a <template> element' do
lex_html('<thead><template>foo</template></thead>').should == [
[:T_ELEM_NAME, 'thead', 1],
[:T_ELEM_NAME, 'template', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
end end
end end

View File

@ -32,5 +32,25 @@ describe Oga::XML::Lexer do
[:T_ELEM_END, nil, 1] [:T_ELEM_END, nil, 1]
] ]
end end
it 'lexes a <tr> element containing a <script> element' do
lex_html('<tr><script>foo</script></tr>').should == [
[:T_ELEM_NAME, 'tr', 1],
[:T_ELEM_NAME, 'script', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
it 'lexes a <tr> element containing a <template> element' do
lex_html('<tr><template>foo</template></tr>').should == [
[:T_ELEM_NAME, 'tr', 1],
[:T_ELEM_NAME, 'template', 1],
[:T_TEXT, 'foo', 1],
[:T_ELEM_END, nil, 1],
[:T_ELEM_END, nil, 1]
]
end
end end
end end