2007-06-05 23:34:49 +02:00
|
|
|
require File.join(File.dirname(__FILE__), 'preamble')
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/html5parser'
|
|
|
|
require 'html5/treewalkers'
|
|
|
|
require 'html5/treebuilders'
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
$tree_types_to_test = {
|
|
|
|
'simpletree' =>
|
2007-07-05 00:36:59 +02:00
|
|
|
{:builder => HTML5::TreeBuilders['simpletree'],
|
|
|
|
:walker => HTML5::TreeWalkers['simpletree']},
|
2007-06-05 23:34:49 +02:00
|
|
|
'rexml' =>
|
2007-07-05 00:36:59 +02:00
|
|
|
{:builder => HTML5::TreeBuilders['rexml'],
|
|
|
|
:walker => HTML5::TreeWalkers['rexml']},
|
2007-06-22 10:12:08 +02:00
|
|
|
'hpricot' =>
|
2007-07-05 00:36:59 +02:00
|
|
|
{:builder => HTML5::TreeBuilders['hpricot'],
|
|
|
|
:walker => HTML5::TreeWalkers['hpricot']},
|
2007-06-05 23:34:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
puts 'Testing tree walkers: ' + $tree_types_to_test.keys * ', '
|
|
|
|
|
|
|
|
class TestTreeWalkers < Test::Unit::TestCase
|
2007-07-05 00:36:59 +02:00
|
|
|
include HTML5::TestSupport
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
def concatenateCharacterTokens(tokens)
|
|
|
|
charactersToken = nil
|
|
|
|
for token in tokens
|
|
|
|
type = token[:type]
|
|
|
|
if [:Characters, :SpaceCharacters].include?(type)
|
|
|
|
if charactersToken == nil
|
|
|
|
charactersToken = {:type => :Characters, :data => token[:data]}
|
|
|
|
else
|
|
|
|
charactersToken[:data] += token[:data]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if charactersToken != nil
|
|
|
|
yield charactersToken
|
|
|
|
charactersToken = nil
|
|
|
|
end
|
|
|
|
yield token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
yield charactersToken if charactersToken != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def convertTokens(tokens)
|
|
|
|
output = []
|
|
|
|
indent = 0
|
|
|
|
concatenateCharacterTokens(tokens) do |token|
|
2007-06-22 10:12:08 +02:00
|
|
|
case token[:type]
|
2007-10-06 18:55:58 +02:00
|
|
|
when :StartTag, :EmptyTag
|
|
|
|
output << "#{' '*indent}<#{token[:name]}>"
|
|
|
|
indent += 2
|
|
|
|
for name, value in token[:data].to_a.sort
|
|
|
|
next if name=='xmlns'
|
|
|
|
output << "#{' '*indent}#{name}=\"#{value}\""
|
|
|
|
end
|
|
|
|
indent -= 2 if token[:type] == :EmptyTag
|
|
|
|
when :EndTag
|
|
|
|
indent -= 2
|
|
|
|
when :Comment
|
|
|
|
output << "#{' '*indent}<!-- #{token[:data]} -->"
|
|
|
|
when :Doctype
|
|
|
|
if token[:name] and token[:name].any?
|
|
|
|
output << "#{' '*indent}<!DOCTYPE #{token[:name]}>"
|
2007-06-05 23:34:49 +02:00
|
|
|
else
|
2007-10-06 18:55:58 +02:00
|
|
|
output << "#{' '*indent}<!DOCTYPE >"
|
|
|
|
end
|
|
|
|
when :Characters, :SpaceCharacters
|
|
|
|
output << "#{' '*indent}\"#{token[:data]}\""
|
2007-06-22 10:12:08 +02:00
|
|
|
end
|
2007-06-05 23:34:49 +02:00
|
|
|
end
|
2007-10-06 18:55:58 +02:00
|
|
|
output.join("\n")
|
2007-06-05 23:34:49 +02:00
|
|
|
end
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
html5_test_files('tree-construction').each do |test_file|
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
test_name = File.basename(test_file).sub('.dat', '')
|
2007-06-22 10:12:08 +02:00
|
|
|
next if test_name == 'tests5' # TODO
|
2007-06-05 23:34:49 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
TestData.new(test_file, %w(data errors document-fragment document)).
|
2007-08-30 19:19:10 +02:00
|
|
|
each_with_index do |(input, errors, inner_html, expected), index|
|
2007-06-05 23:34:49 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
expected = expected.gsub("\n| ","\n")[2..-1]
|
2007-06-05 23:34:49 +02:00
|
|
|
|
2007-06-22 10:12:08 +02:00
|
|
|
$tree_types_to_test.each do |tree_name, tree_class|
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
define_method "test_#{test_name}_#{index}_#{tree_name}" do
|
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
parser = HTML5::HTMLParser.new(:tree => tree_class[:builder])
|
2007-06-05 23:34:49 +02:00
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
if inner_html
|
|
|
|
parser.parse_fragment(input, inner_html)
|
2007-06-05 23:34:49 +02:00
|
|
|
else
|
|
|
|
parser.parse(input)
|
|
|
|
end
|
|
|
|
|
2007-08-30 19:19:10 +02:00
|
|
|
document = parser.tree.get_document
|
2007-06-05 23:34:49 +02:00
|
|
|
|
|
|
|
begin
|
2007-06-22 10:12:08 +02:00
|
|
|
output = sortattrs(convertTokens(tree_class[:walker].new(document)))
|
2007-07-05 00:36:59 +02:00
|
|
|
expected = sortattrs(expected)
|
2007-06-22 10:12:08 +02:00
|
|
|
assert_equal expected, output, [
|
|
|
|
'', 'Input:', input,
|
|
|
|
'', 'Expected:', expected,
|
|
|
|
'', 'Recieved:', output
|
|
|
|
].join("\n")
|
2007-06-05 23:34:49 +02:00
|
|
|
rescue NotImplementedError
|
|
|
|
# Amnesty for those that confess...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-10-06 18:55:58 +02:00
|
|
|
|
|
|
|
def test_all_tokens
|
|
|
|
expected = [
|
|
|
|
{:data => [], :type => :StartTag, :name => 'html'},
|
|
|
|
{:data => [], :type => :StartTag, :name => 'head'},
|
|
|
|
{:data => [], :type => :EndTag, :name => 'head'},
|
|
|
|
{:data => [], :type => :StartTag, :name => 'body'},
|
|
|
|
{:data => [], :type => :EndTag, :name => 'body'},
|
|
|
|
{:data => [], :type => :EndTag, :name => 'html'}]
|
|
|
|
for treeName, tree_class in $tree_types_to_test
|
|
|
|
p = HTML5::HTMLParser.new(:tree => tree_class[:builder])
|
|
|
|
document = p.parse("<html></html>")
|
|
|
|
# document = tree_class.get(:adapter)(document)
|
|
|
|
output = tree_class[:walker].new(document)
|
|
|
|
expected.zip(output) do |expected_token, output_token|
|
|
|
|
assert_equal(expected_token, output_token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-06-05 23:34:49 +02:00
|
|
|
end
|