1
0
Fork 0

ALL YOUR LIBRARIES ARE BELONG TO US

This commit is contained in:
neingeist 2006-04-17 14:17:33 +00:00
parent 85295f82e8
commit 6d05f8ef2d
56 changed files with 7407 additions and 0 deletions

1
vendor/htree/test/assign.html vendored Normal file
View file

@ -0,0 +1 @@
<span _text="htree_test_toplevel_local_variable = :modified" />

4
vendor/htree/test/template.html vendored Normal file
View file

@ -0,0 +1,4 @@
<?xml version="1.0"?>
<html>
<title _text="self">dummy_title</title>
</html>

67
vendor/htree/test/test-attr.rb vendored Normal file
View file

@ -0,0 +1,67 @@
require 'test/unit'
require 'htree/tag'
require 'htree/elem'
require 'htree/traverse'
class TestAttr < Test::Unit::TestCase
def test_each_attribute
t = HTree::STag.new('ename', [['p:n', 'a&b']], HTree::DefaultContext.subst_namespaces({'p'=>'u'}))
t = HTree::Elem.new!(t)
t.each_attribute {|n, v|
assert_instance_of(HTree::Name, n)
assert_instance_of(HTree::Text, v)
assert_equal('{u}n', n.universal_name)
assert_equal('a&amp;b', v.rcdata)
}
end
def test_each_attr
t = HTree::STag.new('ename', [['p:n', 'a&b']], HTree::DefaultContext.subst_namespaces({'p'=>'u'}))
t = HTree::Elem.new!(t)
t.each_attr {|n, v|
assert_instance_of(String, n)
assert_instance_of(String, v)
assert_equal('{u}n', n)
assert_equal('a&b', v)
}
end
def test_fetch_attribute
t = HTree::STag.new('ename', [['p:n', 'a&b']], HTree::DefaultContext.subst_namespaces({'p'=>'u'}))
t = HTree::Elem.new!(t)
v = t.fetch_attribute('{u}n')
assert_instance_of(HTree::Text, v)
assert_equal('a&amp;b', v.rcdata)
assert_equal('y', t.fetch_attribute('x', 'y'))
assert_raises(IndexError) { t.fetch_attribute('x') }
end
def test_get_attribute
t = HTree::STag.new('ename', [['p:n', 'a&b']], HTree::DefaultContext.subst_namespaces({'p'=>'u'}))
t = HTree::Elem.new!(t)
v = t.get_attribute('{u}n')
assert_instance_of(HTree::Text, v)
assert_equal('a&amp;b', v.rcdata)
assert_equal(nil, t.get_attribute('x'))
end
def test_get_attr
t = HTree::STag.new('ename', [['p:n', 'a&b']], HTree::DefaultContext.subst_namespaces({'p'=>'u'}))
t = HTree::Elem.new!(t)
v = t.get_attr('{u}n')
assert_instance_of(String, v)
assert_equal('a&b', v)
assert_equal(nil, t.get_attr('x'))
end
def test_loc_get_attr
t = HTree::Elem.new('e', {'k'=>'v'})
v = t.make_loc.get_attr('k')
assert_instance_of(String, v)
assert_equal('v', v)
v = t.make_loc.fetch_attr('k')
assert_instance_of(String, v)
assert_equal('v', v)
end
end

53
vendor/htree/test/test-charset.rb vendored Normal file
View file

@ -0,0 +1,53 @@
require 'test/unit'
require 'htree/parse'
class TestCharset < Test::Unit::TestCase
def setup
@old_kcode = $KCODE
end
def teardown
$KCODE = @old_kcode
end
def self.mark_string(str, charset)
def str.read() self end
class << str; self end.__send__(:define_method, :charset) { charset }
str
end
# HIRAGANA LETTER A in various charset
UTF8 = mark_string("\343\201\202", 'UTF-8')
EUCKR = mark_string("\252\242", 'EUC-KR')
EUCJP = mark_string("\244\242", 'EUC-JP')
SJIS = mark_string("\202\240", 'Shift_JIS')
ISO2022JP = mark_string("\e$B$\"\e(B", 'ISO-2022-JP')
def test_u
$KCODE = 'u'
assert_equal(UTF8, HTree.parse(UTF8).children[0].to_s)
assert_equal(UTF8, HTree.parse(EUCKR).children[0].to_s)
assert_equal(UTF8, HTree.parse(EUCJP).children[0].to_s)
assert_equal(UTF8, HTree.parse(SJIS).children[0].to_s)
assert_equal(UTF8, HTree.parse(ISO2022JP).children[0].to_s)
end
def test_e
$KCODE = 'e'
assert_equal(EUCJP, HTree.parse(UTF8).children[0].to_s)
assert_equal(EUCJP, HTree.parse(EUCKR).children[0].to_s)
assert_equal(EUCJP, HTree.parse(EUCJP).children[0].to_s)
assert_equal(EUCJP, HTree.parse(SJIS).children[0].to_s)
assert_equal(EUCJP, HTree.parse(ISO2022JP).children[0].to_s)
end
def test_s
$KCODE = 's'
assert_equal(SJIS, HTree.parse(UTF8).children[0].to_s)
assert_equal(SJIS, HTree.parse(EUCKR).children[0].to_s)
assert_equal(SJIS, HTree.parse(EUCJP).children[0].to_s)
assert_equal(SJIS, HTree.parse(SJIS).children[0].to_s)
assert_equal(SJIS, HTree.parse(ISO2022JP).children[0].to_s)
end
end

29
vendor/htree/test/test-context.rb vendored Normal file
View file

@ -0,0 +1,29 @@
require 'test/unit'
require 'htree/context'
class TestContext < Test::Unit::TestCase
def test_namespaces_validation
assert_raise(ArgumentError) { HTree::Context.new({1=>'u'}) }
assert_raise(ArgumentError) { HTree::Context.new({''=>'u'}) }
assert_raise(ArgumentError) { HTree::Context.new({'p'=>nil}) }
assert_nothing_raised { HTree::Context.new({nil=>'u'}) }
end
def test_namespace_uri
assert_equal('http://www.w3.org/XML/1998/namespace',
HTree::Context.new.namespace_uri('xml'))
assert_equal('u', HTree::Context.new({nil=>'u'}).namespace_uri(nil))
assert_equal('u', HTree::Context.new({'p'=>'u'}).namespace_uri('p'))
assert_equal(nil, HTree::Context.new({'p'=>'u'}).namespace_uri('q'))
end
def test_subst_namespaces
c1 = HTree::Context.new({'p'=>'u'})
c2 = c1.subst_namespaces({'q'=>'v'})
assert_equal('u', c1.namespace_uri('p'))
assert_equal(nil, c1.namespace_uri('q'))
assert_equal('u', c2.namespace_uri('p'))
assert_equal('v', c2.namespace_uri('q'))
end
end

45
vendor/htree/test/test-display_xml.rb vendored Normal file
View file

@ -0,0 +1,45 @@
require 'test/unit'
require 'htree/elem'
require 'htree/display'
class TestXMLNS < Test::Unit::TestCase
def assert_xml(expected, node)
assert_equal(expected, node.display_xml('', 'US-ASCII'))
end
def test_update_xmlns_empty
assert_xml("<n\n/>", HTree::Elem.new('n'))
end
def test_reduce_xmlns
assert_xml(
"<p:n xmlns:p=\"u\"\n/>",
HTree::Elem.new('p:n', {'xmlns:p'=>'u'}))
assert_xml(
"<n xmlns:p=\"u\"\n><p:n\n/></n\n>",
HTree::Elem.new('n', {'xmlns:p'=>'u'}, HTree::Elem.new('p:n', {'xmlns:p'=>'u'})))
assert_xml(
"<n xmlns:p=\"u\"\n><p:n xmlns:p=\"v\"\n/></n\n>",
HTree::Elem.new('n', {'xmlns:p'=>'u'}, HTree::Elem.new('p:n', {'xmlns:p'=>'v'})))
end
def test_extra_xmlns
assert_xml(
"<p:n xmlns:p=\"u\"\n/>",
HTree::Elem.new(HTree::Name.new('p', 'u', 'n')))
assert_xml(
"<nn\n><p:n xmlns:p=\"u\"\n/></nn\n>",
HTree::Elem.new('nn', HTree::Elem.new(HTree::Name.new('p', 'u', 'n'))))
assert_xml(
"<nn xmlns:p=\"u\"\n><p:n\n/></nn\n>",
HTree::Elem.new('nn', {'xmlns:p'=>'u'}, HTree::Elem.new(HTree::Name.new('p', 'u', 'n'))))
assert_xml(
"<nn xmlns:p=\"v\"\n><p:n xmlns:p=\"u\"\n/></nn\n>",
HTree::Elem.new('nn', {'xmlns:p'=>'v'}, HTree::Elem.new(HTree::Name.new('p', 'u', 'n'))))
end
end

101
vendor/htree/test/test-elem-new.rb vendored Normal file
View file

@ -0,0 +1,101 @@
require 'test/unit'
require 'htree/doc'
require 'htree/elem'
require 'htree/equality'
require 'htree/traverse'
class TestElemNew < Test::Unit::TestCase
def test_empty
e = HTree::Elem.new('a')
assert_equal('a', e.qualified_name)
assert_equal({}, e.attributes)
assert_equal(HTree::DefaultContext, e.instance_variable_get(:@stag).inherited_context)
assert_equal([], e.children)
assert_equal(true, e.empty_element?)
assert_nil(e.instance_variable_get(:@etag))
end
def test_empty_array
e = HTree::Elem.new('a', [])
assert_equal('a', e.qualified_name)
assert_equal({}, e.attributes)
assert_equal(HTree::DefaultContext, e.instance_variable_get(:@stag).inherited_context)
assert_equal([], e.children)
assert_equal(false, e.empty_element?)
assert_equal(nil, e.instance_variable_get(:@etag))
end
def test_empty_attr
e = HTree::Elem.new('a', {'href'=>'xxx'})
assert_equal('a', e.qualified_name)
assert_equal({HTree::Name.parse_attribute_name('href', HTree::DefaultContext)=>HTree::Text.new('xxx')}, e.attributes)
assert_equal(HTree::DefaultContext, e.instance_variable_get(:@stag).inherited_context)
assert_equal([], e.children)
assert_equal(true, e.empty_element?)
assert_equal(nil, e.instance_variable_get(:@etag))
end
def test_node
t = HTree::Text.new('t')
e = HTree::Elem.new('a', t)
assert_equal({}, e.attributes)
assert_equal([t], e.children)
end
def test_hash
t = HTree::Text.new('t')
e = HTree::Elem.new('a', {'b' => t})
assert_equal([['b', t]], e.attributes.map {|n,v| [n.universal_name, v] })
assert_equal([], e.children)
end
def test_string
t = HTree::Text.new('s')
e = HTree::Elem.new('a', "s")
assert_equal({}, e.attributes)
assert_equal([t], e.children)
end
def test_interleave
t = HTree::Text.new('t')
e = HTree::Elem.new('a', t, {'b' => t}, t, {'c' => 'd'}, t)
assert_equal([['b', t], ['c', HTree::Text.new('d')]],
e.attributes.map {|n,v| [n.universal_name, v] }.sort)
assert_equal([t, t, t], e.children)
end
def test_nest
t = HTree::Text.new('t')
b = HTree::BogusETag.new('a')
x = HTree::Elem.new('e', HTree::XMLDecl.new('1.0'))
d = HTree::Elem.new('e', HTree::DocType.new('html'))
e = HTree::Elem.new('a', [t, t, t, b, x, d])
assert_equal({}, e.attributes)
assert_equal([t, t, t, b, x, d], e.children)
end
def test_err
assert_raises(TypeError) { HTree::Elem.new('e', HTree::STag.new('a')) }
assert_raises(TypeError) { HTree::Elem.new('e', HTree::ETag.new('a')) }
end
def test_context
context = HTree::DefaultContext.subst_namespaces({'p'=>'u'})
elem = HTree::Elem.new('p:n', {'p:a'=>'t'}, context)
assert_equal('{u}n', elem.name)
assert_equal('t', elem.get_attr('{u}a'))
assert_same(context, elem.instance_variable_get(:@stag).inherited_context)
assert_raises(ArgumentError) { HTree::Elem.new('e', context, context) }
end
def test_hash_in_array
attrs = [{'a'=>'1'}, {'a'=>'2'}]
assert_raises(TypeError) { HTree::Elem.new('e', attrs) }
attrs.pop
assert_raises(TypeError) { HTree::Elem.new('e', attrs) }
attrs.pop
assert_equal([], attrs)
assert_equal(false, HTree::Elem.new('e', attrs).empty_element?)
end
end

49
vendor/htree/test/test-encoder.rb vendored Normal file
View file

@ -0,0 +1,49 @@
require 'test/unit'
require 'htree/encoder'
class TestEncoder < Test::Unit::TestCase
EUC_JISX0212_CH = "\217\260\241" # cannot encode with Shift_JIS.
EUC_JISX0208_CH = "\260\241"
def test_minimal_charset
out = HTree::Encoder.new('Shift_JIS', 'EUC-JP')
assert_equal("US-ASCII", out.minimal_charset)
out.output_text("a")
assert_equal("US-ASCII", out.minimal_charset)
out.output_text(EUC_JISX0212_CH)
assert_equal("US-ASCII", out.minimal_charset)
out.output_text("b")
assert_equal("US-ASCII", out.minimal_charset)
assert_equal("a&#19970;b", out.finish)
end
def test_minimal_charset_2
out = HTree::Encoder.new('ISO-2022-JP-2', 'EUC-JP')
assert_equal("US-ASCII", out.minimal_charset)
out.output_text("a")
assert_equal("US-ASCII", out.minimal_charset)
out.output_text(EUC_JISX0208_CH)
assert_equal("ISO-2022-JP", out.minimal_charset)
out.output_text("b")
assert_equal("ISO-2022-JP", out.minimal_charset)
out.output_text(EUC_JISX0212_CH)
assert_equal("ISO-2022-JP-2", out.minimal_charset)
assert_equal("a\e$B0!\e(Bb\e$(D0!\e(B", out.finish)
end
def test_minimal_charset_u
out = HTree::Encoder.new('UTF-16BE', 'EUC-JP')
assert_equal("UTF-16BE", out.minimal_charset)
out.output_text("a")
assert_equal("UTF-16BE", out.minimal_charset)
assert_equal("\000a", out.finish)
end
def test_close
out = HTree::Encoder.new('ISO-2022-JP', 'EUC-JP')
out.output_string(EUC_JISX0208_CH)
assert_equal("ISO-2022-JP", out.minimal_charset)
assert_equal("\e$B0!\e(B", out.finish)
end
end

55
vendor/htree/test/test-equality.rb vendored Normal file
View file

@ -0,0 +1,55 @@
require 'test/unit'
require 'htree/equality'
class TestEQQ < Test::Unit::TestCase
def assert_exact_equal(expected, actual, message=nil)
full_message = build_message(message, <<EOT, expected, actual)
<?> expected but was
<?>.
EOT
assert_block(full_message) { expected.exact_equal? actual }
end
def test_tag_name_prefix
tags = [
HTree::STag.new('{u}n'),
HTree::STag.new('p1{u}n'),
HTree::STag.new('p2{u}n'),
HTree::STag.new('p1:n', [], HTree::DefaultContext.subst_namespaces({'p1'=>'u'})),
HTree::STag.new('p2:n', [], HTree::DefaultContext.subst_namespaces({'p2'=>'u'})),
]
tags.each {|t1|
tags.each {|t2|
assert_equal(t1, t2)
}
}
end
def test_tag_attribute_name_prefix
tags = [
HTree::STag.new('n', [['p1{u}a', 'v']]),
HTree::STag.new('n', [['p2{u}a', 'v']]),
HTree::STag.new('n', [['p1:a', 'v']], HTree::DefaultContext.subst_namespaces({'p1'=>'u'})),
HTree::STag.new('n', [['p2:a', 'v']], HTree::DefaultContext.subst_namespaces({'p2'=>'u'})),
]
tags.each {|t1|
tags.each {|t2|
assert_equal(t1, t2)
}
}
end
def test_element
assert_equal(HTree::Elem.new('p1{u}n'), HTree::Elem.new('p2{u}n'))
assert_equal(HTree::Elem.new('n', {'p1{u}a'=>'v'}),
HTree::Elem.new('n', {'p2{u}a'=>'v'}))
assert(!HTree::Elem.new('n', {'p1{u}a'=>'v'}).exact_equal?(HTree::Elem.new('n', {'p2{u}a'=>'v'})))
end
def test_tag_namespaces
assert_nothing_raised {
HTree::STag.new("n", [], HTree::DefaultContext.subst_namespaces({nil=>"u1", "p"=>"u2"})).make_exact_equal_object
}
end
end

18
vendor/htree/test/test-extract_text.rb vendored Normal file
View file

@ -0,0 +1,18 @@
require 'test/unit'
require 'htree/extract_text'
require 'htree/equality'
class TestExtractText < Test::Unit::TestCase
def test_single
n = HTree::Text.new('abc')
assert_equal(n, n.extract_text)
end
def test_elem
t = HTree::Text.new('abc')
n = HTree::Elem.new('e', t)
assert_equal(t, n.extract_text)
end
end

27
vendor/htree/test/test-gencode.rb vendored Normal file
View file

@ -0,0 +1,27 @@
require 'test/unit'
require 'htree/gencode'
require 'htree/parse'
class TestGenCode < Test::Unit::TestCase
def run_code(code, top_context)
out = HTree::Encoder.new(HTree::Encoder.internal_charset, HTree::Encoder.internal_charset)
eval(code)
out.finish
end
def test_xmlns
t = HTree.parse_xml('<p:n xmlns:p=z><p:m>bb').children[0].children[0] # <p:m>bb</p:m>
code = t.generate_xml_output_code
assert_equal("<p:m xmlns:p=\"z\"\n>bb</p:m\n>", run_code(code, HTree::DefaultContext))
assert_equal("<p:m\n>bb</p:m\n>", run_code(code, HTree::DefaultContext.subst_namespaces("p"=>"z")))
end
def test_xmlns_chref
t = HTree.parse_xml('<p:n xmlns:p="a&amp;<>&quot;b"/>').children[0]
code = t.generate_xml_output_code
assert_equal("<p:n xmlns:p=\"a&amp;&lt;&gt;&quot;b\"\n/>", run_code(code, HTree::DefaultContext))
end
end

25
vendor/htree/test/test-leaf.rb vendored Normal file
View file

@ -0,0 +1,25 @@
require 'test/unit'
require 'htree/leaf'
class TestProcIns < Test::Unit::TestCase
def test_initialize
assert_raises(HTree::ProcIns::Error) { HTree::ProcIns.new!('target', "?>") }
end
def test_new
assert_equal('? >', HTree::ProcIns.new('target', "?>").content)
assert_equal(nil, HTree::ProcIns.new('target', nil).content)
end
end
class TestComment < Test::Unit::TestCase
def test_initialize
assert_raises(HTree::Comment::Error) { HTree::Comment.new!("a--b") }
assert_raises(HTree::Comment::Error) { HTree::Comment.new!("a-") }
end
def test_new
assert_equal('a- -b', HTree::Comment.new("a--b").content)
assert_equal('a- ', HTree::Comment.new("a-").content)
end
end

60
vendor/htree/test/test-loc.rb vendored Normal file
View file

@ -0,0 +1,60 @@
require 'test/unit'
require 'htree/loc'
require 'htree/parse'
require 'htree/traverse'
class TestLoc < Test::Unit::TestCase
def test_make_loc
t = HTree.parse('<?xml version="1.0"?><!DOCTYPE root><root>a<?x y?><!-- c --></boo>')
assert_instance_of(HTree::Doc::Loc, t.make_loc)
assert_instance_of(HTree::XMLDecl::Loc, t.children[0].make_loc)
assert_instance_of(HTree::DocType::Loc, t.children[1].make_loc)
assert_instance_of(HTree::Elem::Loc, t.children[2].make_loc)
assert_instance_of(HTree::Text::Loc, t.children[2].children[0].make_loc)
assert_instance_of(HTree::ProcIns::Loc, t.children[2].children[1].make_loc)
assert_instance_of(HTree::Comment::Loc, t.children[2].children[2].make_loc)
assert_instance_of(HTree::BogusETag::Loc, t.children[2].children[3].make_loc)
assert_equal(nil, t.make_loc.parent)
assert_equal(nil, t.make_loc.index)
end
def test_get_subnode
t = HTree.parse('<?xml version="1.0"?><!DOCTYPE root><root>a<?x y?><!-- c --></boo>')
l = t.make_loc
assert_instance_of(HTree::Doc::Loc, l)
assert_instance_of(HTree::Location, l.get_subnode(-1))
assert_instance_of(HTree::XMLDecl::Loc, l.get_subnode(0))
assert_instance_of(HTree::DocType::Loc, l.get_subnode(1))
assert_instance_of(HTree::Elem::Loc, l2 = l.get_subnode(2))
assert_instance_of(HTree::Location, l.get_subnode(3))
assert_instance_of(HTree::Location, l2.get_subnode(-1))
assert_instance_of(HTree::Location, l2.get_subnode('attr'))
assert_instance_of(HTree::Text::Loc, l2.get_subnode(0))
assert_instance_of(HTree::ProcIns::Loc, l2.get_subnode(1))
assert_instance_of(HTree::Comment::Loc, l2.get_subnode(2))
assert_instance_of(HTree::BogusETag::Loc, l2.get_subnode(3))
assert_instance_of(HTree::Location, l2.get_subnode(4))
assert_same(l.get_subnode(0), l.get_subnode(0))
end
def test_find_loc_step
t = HTree.parse('<a><b>x<!---->y</a><c/><a/>')
assert_equal('a[1]', t.find_loc_step(0))
assert_equal('c', t.find_loc_step(1))
assert_equal('a[2]', t.find_loc_step(2))
t = t.children[0]
assert_equal('b', t.find_loc_step(0))
t = t.children[0]
assert_equal('text()[1]', t.find_loc_step(0))
assert_equal('comment()', t.find_loc_step(1))
assert_equal('text()[2]', t.find_loc_step(2))
end
def test_path
l = HTree.parse('<a><b>x</b><b/><a/>').make_loc
l2 = l.get_subnode(0, 0, 0)
assert_equal('doc()', l.path)
assert_equal('doc()/a/b[1]/text()', l2.path)
end
end

147
vendor/htree/test/test-namespace.rb vendored Normal file
View file

@ -0,0 +1,147 @@
require 'test/unit'
require 'htree/tag'
class TestNamespace < Test::Unit::TestCase
def assert_equal_exact(expected, actual, message=nil)
full_message = build_message(message, <<EOT, expected, actual)
<?> expected but was
<?>.
EOT
assert_block(full_message) { expected.equal_exact? actual }
end
# <ppp:nnn xmlns:ppp="uuu">
def test_prefixed
stag = HTree::STag.new("ppp:nnn",
[["xmlns:ppp", "uuu"], ["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
assert_equal("ppp:nnn", stag.element_name.qualified_name)
assert_equal("{uuu}nnn", stag.element_name.universal_name)
assert_equal("nnn", stag.element_name.local_name)
assert_equal("uuu", stag.element_name.namespace_uri)
assert_equal("ppp", stag.element_name.namespace_prefix)
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
assert_equal(1, nsattrs.length)
assert_equal(["ppp", "uuu"], nsattrs.shift)
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
assert_equal(3, attrs.length)
assert_equal(['', nil, "a", "x"], attrs.shift)
assert_equal(["u", "q", "b", "y"], attrs.shift)
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
end
# <nnn xmlns="uuu">
def test_default_ns
stag = HTree::STag.new("nnn",
[["xmlns", "uuu"],
["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
assert_equal("nnn", stag.element_name.qualified_name)
assert_equal("{uuu}nnn", stag.element_name.universal_name)
assert_equal("nnn", stag.element_name.local_name)
assert_equal("uuu", stag.element_name.namespace_uri)
assert_equal(nil, stag.element_name.namespace_prefix)
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
assert_equal(1, nsattrs.length)
assert_equal([nil, "uuu"], nsattrs.shift)
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
assert_equal(3, attrs.length)
assert_equal(['', nil, "a", "x"], attrs.shift)
assert_equal(["u", "q", "b", "y"], attrs.shift)
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
end
# <nnn xmlns="">
def test_no_default_ns
[{"q"=>"u"}, {nil=>"uu", "q"=>"u"}].each {|inh|
stag = HTree::STag.new("nnn",
[["xmlns", ""], ["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
HTree::DefaultContext.subst_namespaces(inh))
assert_equal("nnn", stag.element_name.qualified_name)
assert_equal("nnn", stag.element_name.universal_name)
assert_equal("nnn", stag.element_name.local_name)
assert_equal('', stag.element_name.namespace_uri)
assert_equal(nil, stag.element_name.namespace_prefix)
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
assert_equal(1, nsattrs.length)
assert_equal([nil, ""], nsattrs.shift)
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
assert_equal(3, attrs.length)
assert_equal(['', nil, "a", "x"], attrs.shift)
assert_equal(["u", "q", "b", "y"], attrs.shift)
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
}
end
# <nnn>
def test_no_ns
stag = HTree::STag.new("nnn",
[["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
assert_equal("nnn", stag.element_name.qualified_name)
assert_equal("nnn", stag.element_name.universal_name)
assert_equal("nnn", stag.element_name.local_name)
assert_equal('', stag.element_name.namespace_uri)
assert_equal(nil, stag.element_name.namespace_prefix)
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
assert_equal(0, nsattrs.length)
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
assert_equal(3, attrs.length)
assert_equal(['', nil, "a", "x"], attrs.shift)
assert_equal(["u", "q", "b", "y"], attrs.shift)
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
end
# internally allocated element without prefix
def test_universal_name_to_be_default_namespace
stag = HTree::STag.new("{uuu}nnn",
[["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
assert_equal("nnn", stag.element_name.qualified_name)
assert_equal("{uuu}nnn", stag.element_name.universal_name)
assert_equal("nnn", stag.element_name.local_name)
assert_equal("uuu", stag.element_name.namespace_uri)
assert_equal(nil, stag.element_name.namespace_prefix)
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
assert_equal(0, nsattrs.length)
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
assert_equal(3, attrs.length)
assert_equal(['', nil, "a", "x"], attrs.shift)
assert_equal(["u", "q", "b", "y"], attrs.shift)
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
end
def test_prefixed_universal_name
stag = HTree::STag.new("ppp{uuu}nnn",
[["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"], ["q{uu}d", "w"]],
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
assert_equal("ppp:nnn", stag.element_name.qualified_name)
assert_equal("{uuu}nnn", stag.element_name.universal_name)
assert_equal("nnn", stag.element_name.local_name)
assert_equal("uuu", stag.element_name.namespace_uri)
assert_equal("ppp", stag.element_name.namespace_prefix)
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
assert_equal(0, nsattrs.length)
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
assert_equal(4, attrs.length)
assert_equal(['', nil, "a", "x"], attrs.shift)
assert_equal(["u", "q", "b", "y"], attrs.shift)
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
assert_equal(["uu", "q", "d", "w"], attrs.shift)
end
end

133
vendor/htree/test/test-output.rb vendored Normal file
View file

@ -0,0 +1,133 @@
require 'test/unit'
require 'htree'
class TestOutput < Test::Unit::TestCase
def gen(t, meth=:output, *rest)
encoder = HTree::Encoder.new('US-ASCII', 'US-ASCII')
t.__send__(meth, *(rest + [encoder, HTree::DefaultContext]))
encoder.finish
end
def test_text
assert_equal('a&amp;&lt;&gt;"b', gen(HTree::Text.new('a&<>"b')))
assert_equal("abc&amp;def", gen(HTree::Text.new("abc&def")))
assert_equal('"\'&amp;', gen(HTree::Text.new('"\'&')))
assert_equal('"\'&lt;&amp;&gt;', gen(HTree::Text.new('"\'<&>')))
end
def test_text_attvalue
assert_equal('"a&amp;&lt;&gt;&quot;b"', gen(HTree::Text.new('a&<>"b'), :output_attvalue))
assert_equal('"abc"', gen(HTree::Text.new("abc"), :output_attvalue))
assert_equal('"&quot;"', gen(HTree::Text.new('"'), :output_attvalue))
end
def test_name
assert_equal('abc', gen(HTree::Name.parse_element_name('abc', HTree::DefaultContext)))
assert_equal('n', gen(HTree::Name.new(nil, 'u', 'n')))
assert_equal('p:n', gen(HTree::Name.new('p', 'u', 'n')))
assert_equal('n', gen(HTree::Name.new(nil, '', 'n')))
assert_equal('xmlns', gen(HTree::Name.new('xmlns', nil, nil)))
assert_equal('xmlns:n', gen(HTree::Name.new('xmlns', nil, 'n')))
end
def test_name_attribute
assert_equal('abc="a&amp;&lt;&gt;&quot;b"',
gen(HTree::Name.parse_element_name('abc', HTree::DefaultContext),
:output_attribute,
HTree::Text.new('a&<>"b')))
end
def test_doc
t = HTree::Doc.new(HTree::Elem.new('a'), HTree::Elem.new('b'))
assert_equal("<a\n/><b\n/>", gen(t))
end
def test_elem
t = HTree::Elem.new('a', [])
assert_equal("<a\n></a\n>", gen(t))
assert_equal("<b\n/>",
gen(HTree::Elem.new!(HTree::STag.new('b'))))
assert_equal("<b\n></b\n>",
gen(HTree::Elem.new!(HTree::STag.new('b'), [])))
assert_equal("<a\n><b\n/><c\n/><d\n/></a\n>",
gen(HTree::Elem.new!(HTree::STag.new('a'), [
HTree::Elem.new!(HTree::STag.new('b')),
HTree::Elem.new!(HTree::STag.new('c')),
HTree::Elem.new!(HTree::STag.new('d'))
])))
end
def test_elem_empty
t = HTree::Elem.new('a')
assert_equal("<a\n/>", gen(t))
end
def test_stag
assert_equal("<name\n>",
gen(HTree::STag.new("name"), :output_stag))
assert_equal("<name\n/>",
gen(HTree::STag.new("name"), :output_emptytag))
assert_equal("</name\n>",
gen(HTree::STag.new("name"), :output_etag))
assert_equal("<name a=\"b\"\n/>",
gen(HTree::STag.new("name", [["a", "b"]]), :output_emptytag))
assert_equal("<name a=\"&lt;&quot;\'&gt;\"\n/>",
gen(HTree::STag.new("name", [['a', '<"\'>']]), :output_emptytag))
assert_equal("<ppp:nnn xmlns=\"uuu&quot;b\"\n/>",
gen(HTree::STag.new("ppp:nnn", [["xmlns", "uuu\"b"]]), :output_emptytag))
end
def test_xmldecl
t = HTree::XMLDecl.new('1.0', 'US-ASCII')
assert_equal('', gen(t))
assert_equal('<?xml version="1.0" encoding="US-ASCII"?>',
gen(t, :output_prolog_xmldecl))
end
def test_doctype
t = HTree::DocType.new('html',
'-//W3C//DTD HTML 4.01//EN',
'http://www.w3.org/TR/html4/strict.dtd')
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', gen(t))
end
def test_procins
t = HTree::ProcIns.new('xml-stylesheet', 'type="text/xml" href="#style1"')
assert_equal('<?xml-stylesheet type="text/xml" href="#style1"?>', gen(t))
t = HTree::ProcIns.new('x', nil)
assert_equal('<?x?>', gen(t))
end
def test_comment
t = HTree::Comment.new('xxx')
assert_equal('<!--xxx-->', gen(t))
end
end
class TestHTMLOutput < Test::Unit::TestCase
def test_top_xmlns
assert_equal("<html\n>aaa</html\n>", HTree("<html>aaa").display_html(""))
end
def test_script
assert_equal("<html\n><script\n>a < b</script\n></html\n>",
HTree("<html><script>a < b").display_html(""))
end
def test_script_invalid_content
assert_raise(ArgumentError) {
HTree("<html><script>a </ b").display_html("")
}
end
def test_br
assert_equal("<html\n>a<br\n>b<br\n>c</html\n>",
HTree("<html>a<br>b<br>c").display_html(""))
end
end

115
vendor/htree/test/test-parse.rb vendored Normal file
View file

@ -0,0 +1,115 @@
require 'test/unit'
require 'htree/parse'
require 'htree/equality'
require 'htree/traverse'
class TestParse < Test::Unit::TestCase
def test_empty
assert_equal(HTree::Doc.new([]), HTree.parse_xml("").eliminate_raw_string)
end
def test_xmlns_default
t1 = HTree::Doc.new([
HTree::Elem.new!(
HTree::STag.new('x1', [['xmlns', 'bb']],
HTree::DefaultContext.subst_namespaces({'xml'=>'http://www.w3.org/XML/1998/namespace'})),
[HTree::Elem.new!(HTree::STag.new('x2', [],
HTree::DefaultContext.subst_namespaces({nil => 'bb', 'xml'=>'http://www.w3.org/XML/1998/namespace'})), nil)])
])
t2 = HTree.parse_xml('<x1 xmlns="bb"><x2>')
assert_equal(t1, t2)
end
def test_doctype_root_element_name
assert_equal('html',
HTree.parse('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>').children[0].root_element_name)
# xxx: should be downcased?
assert_equal('HTML',
HTree.parse('<?xml version="1.0"?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>').children[1].root_element_name)
end
def test_doctype_system_identifier
assert_equal('http://www.w3.org/TR/html4/loose.dtd',
HTree.parse("<!DOCTYPE HTML SYSTEM 'http://www.w3.org/TR/html4/loose.dtd'>").children[0].system_identifier)
assert_equal('http://www.w3.org/TR/html4/loose.dtd',
HTree.parse("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>").children[0].system_identifier)
end
def test_procins
t = HTree.parse_xml("<?x?>").children[0]
assert_equal('x', t.target)
assert_equal(nil, t.content)
end
def test_eol_html
t1 = HTree::Elem.new('a', "\nb\n")
s = "<a>\nb\n</a>"
t2 = HTree.parse_xml(s).root
assert_equal(t1, t2)
assert_equal(s, t2.raw_string)
end
def test_parse_html
t1 = HTree.parse("<html>a</html>")
assert_equal("{http://www.w3.org/1999/xhtml}html", t1.root.element_name.universal_name)
end
def test_bare_url
t1 = HTree::Elem.new('a', {'href'=>'http://host/'})
s = "<a href=http://host/>"
t2 = HTree.parse(s).root
assert_equal(t1, t2)
end
def test_bare_slash
t1 = HTree::Elem.new('n', {'a'=>'v/'}, 'x')
s = "<n a=v/>x"
t2 = HTree.parse(s).root
assert_equal(t1, t2)
end
def test_bare_slash_empty
t1 = HTree::Elem.new('n', {'a'=>'v/'})
s = "<n a=v/>"
t2 = HTree.parse(s).root
assert_equal(t1, t2)
end
def test_downcase
assert_equal("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF",
HTree.parse('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>').root.name)
end
def test_downcase_name
# HTML && !XML
assert_equal('html', HTree.parse('<HTML>').root.element_name.local_name)
assert_equal('html', HTree.parse('<html>').root.element_name.local_name)
# HTML && XML
assert_equal('html', HTree.parse('<?xml version="1.0"?><html>').root.element_name.local_name)
assert_equal('v', HTree.parse('<?xml version="1.0"?><html X:Y=v xmlns:X=u>').root.get_attr('{u}Y'))
# !HTML && XML
assert_equal('RDF', HTree.parse('<?xml version="1.0"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>').children[1].element_name.local_name)
end
def test_script_etag
assert_equal(HTree::Doc.new(HTree::Elem.new('{http://www.w3.org/1999/xhtml}script', [])),
HTree.parse('<script></script>'))
end
def test_html_emptyelem
t = HTree.parse('<html>')
assert_equal(HTree::Doc.new(HTree::Elem.new('{http://www.w3.org/1999/xhtml}html')), t)
assert(!t.children[0].empty_element?)
end
def test_hr_emptyelem
t = HTree.parse('<html><hr>')
assert_equal(
HTree::Doc.new(
HTree::Elem.new('{http://www.w3.org/1999/xhtml}html',
HTree::Elem.new('{http://www.w3.org/1999/xhtml}hr'))), t)
assert(t.children[0].children[0].empty_element?)
end
end

17
vendor/htree/test/test-raw_string.rb vendored Normal file
View file

@ -0,0 +1,17 @@
require 'test/unit'
require 'htree'
class TestRawString < Test::Unit::TestCase
def test_elem
t = HTree.parse("<a>x</a>")
assert_equal("<a>x</a>", t.root.raw_string)
assert_equal("<a>x</a>", t.root.raw_string) # raw_string shouldn't have side effect.
end
def test_no_raw_string
t = HTree::Elem.new('a')
assert_equal(nil, t.raw_string)
t = HTree::Elem.new('a', HTree.parse("<a>x</a>").root)
assert_equal(nil, t.raw_string)
end
end

70
vendor/htree/test/test-rexml.rb vendored Normal file
View file

@ -0,0 +1,70 @@
require 'test/unit'
require 'htree/parse'
require 'htree/rexml'
begin
require 'rexml/document'
rescue LoadError
end
class TestREXML < Test::Unit::TestCase
def test_doc
r = HTree.parse('<root/>').to_rexml
assert_instance_of(REXML::Document, r)
end
def test_elem
r = HTree.parse('<root a="b"/>').to_rexml
assert_instance_of(REXML::Element, e = r.root)
assert_equal('root', e.name)
assert_equal('b', e.attribute('a').to_s)
end
def test_text
r = HTree.parse('<root>aaa</root>').to_rexml
assert_instance_of(REXML::Text, t = r.root.children[0])
assert_equal('aaa', t.to_s)
end
def test_xmldecl
s = '<?xml version="1.0"?>'
r = HTree.parse(s + '<root>aaa</root>').to_rexml
assert_instance_of(REXML::XMLDecl, x = r.children[0])
assert_equal('1.0', x.version)
assert_equal(nil, x.standalone)
assert_instance_of(REXML::XMLDecl, HTree.parse(s).children[0].to_rexml)
end
def test_doctype
s = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
r = HTree.parse(s + '<html><title>xxx</title></html>').to_rexml
assert_instance_of(REXML::DocType, d = r.children[0])
assert_equal('html', d.name)
assert_equal('PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"', d.external_id)
assert_instance_of(REXML::DocType, HTree.parse(s).children[0].to_rexml)
end
def test_procins
r = HTree.parse('<root><?xxx yyy?></root>').to_rexml
assert_instance_of(REXML::Instruction, i = r.root.children[0])
assert_equal('xxx', i.target)
assert_equal('yyy', i.content)
assert_instance_of(REXML::Instruction, HTree.parse('<?xxx yyy?>').children[0].to_rexml)
end
def test_comment
r = HTree.parse('<root><!-- zzz --></root>').to_rexml
assert_instance_of(REXML::Comment, c = r.root.children[0])
assert_equal(' zzz ', c.to_s)
end
def test_bogusetag
assert_equal(nil, HTree.parse('</e>').children[0].to_rexml)
end
def test_style
assert_equal('<style>a&lt;b</style>', HTree.parse('<html><style>a<b</style></html>').to_rexml.to_s[/<style.*style>/])
end
end if defined? REXML

153
vendor/htree/test/test-scan.rb vendored Normal file
View file

@ -0,0 +1,153 @@
require 'test/unit'
require 'htree/scan'
class TestScan < Test::Unit::TestCase
def scan(str)
result = []
HTree.scan(str) {|e| result << e }
result
end
def test_empty
assert_equal([], scan(''))
end
def t_single(s)
n = yield
assert_equal([n], scan(s))
end
def test_single
s = '<?xml version="1.0"?>'
assert_equal([[:xmldecl, s]], scan(s))
s = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
assert_equal([[:doctype, s]], scan(s))
s = '<?xxx yyy?>'
assert_equal([[:procins, s]], scan(s))
s = '<a>'
assert_equal([[:stag, s]], scan(s))
s = '</a>'
assert_equal([[:etag, s]], scan(s))
s = '<a/>'
assert_equal([[:emptytag, s]], scan(s))
s = '<!-- abc -->'
assert_equal([[:comment, s]], scan(s))
s = '<![CDATA[abc]]>'
assert_equal([[:text_cdata_section, s]], scan(s))
s = 'abc'
assert_equal([[:text_pcdata, s]], scan(s))
end
def test_xmldecl_seen
s0 = '<?xml version="1.0"?>'
s1 = '<A>'
assert_equal([[:stag, s1]], scan(s1))
assert_equal([[:xmldecl, s0], [:stag, s1]], scan(s0 + s1))
end
def test_cdata_content
s = '<html><script><a></script><a>'
assert_equal([
[:stag, '<html>'],
[:stag, '<script>'],
[:text_cdata_content, '<a>'],
[:etag, '</script>'],
[:stag, '<a>'],
], scan(s))
s = '<html><script><a>'
assert_equal([
[:stag, '<html>'],
[:stag, '<script>'],
[:text_cdata_content, '<a>'],
], scan(s))
end
def test_text
s = 'a<e>b<e>c<e>d'
assert_equal([
[:text_pcdata, 'a'],
[:stag, '<e>'],
[:text_pcdata, 'b'],
[:stag, '<e>'],
[:text_pcdata, 'c'],
[:stag, '<e>'],
[:text_pcdata, 'd'],
], scan(s))
end
def test_eol_html
# In SGML, a line break just after start tag and
# a line break just before end tag is ignored.
# http://www.w3.org/TR/REC-html40/appendix/notes.html#notes-line-breaks
#
# But usual browser including mozilla doesn't.
# So HTree doesn't ignore them and treat as usual text.
s = "<html>a\n<e>\nb\n<f>\nc\n</f>\nd\n</e>\ne"
assert_equal([
[:stag, "<html>"],
[:text_pcdata, "a\n"],
[:stag, "<e>"],
[:text_pcdata, "\nb\n"],
[:stag, "<f>"],
[:text_pcdata, "\nc\n"],
[:etag, "</f>"],
[:text_pcdata, "\nd\n"],
[:etag, "</e>"],
[:text_pcdata, "\ne"],
], scan(s))
s = "<html>a\n<e>\nb\n<script>\nc\n</script>\nd\n</e>\ne"
assert_equal([
[:stag, "<html>"],
[:text_pcdata, "a\n"],
[:stag, "<e>"],
[:text_pcdata, "\nb\n"],
[:stag, "<script>"],
[:text_cdata_content, "\nc\n"],
[:etag, "</script>"],
[:text_pcdata, "\nd\n"],
[:etag, "</e>"],
[:text_pcdata, "\ne"],
], scan(s))
end
def test_eol_xml
# In XML, line breaks are treated as part of content.
# It's because KEEPRSRE is yes in XML.
# http://www.satoshii.org/markup/websgml/valid-xml#keeprsre
s = "<?xml version='1.0'?>a\n<e>\nb\n<f>\nc\n</f>\nd\n</e>\ne"
assert_equal([
[:xmldecl, "<?xml version='1.0'?>"],
[:text_pcdata, "a\n"],
[:stag, "<e>"],
[:text_pcdata, "\nb\n"],
[:stag, "<f>"],
[:text_pcdata, "\nc\n"],
[:etag, "</f>"],
[:text_pcdata, "\nd\n"],
[:etag, "</e>"],
[:text_pcdata, "\ne"],
], scan(s))
end
def test_xml_html_detection
assert_equal([false, true], HTree.scan("<html></html>") {})
assert_equal([true, false], HTree.scan("<rss></rss>") {})
assert_equal([true, true], HTree.scan('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">') {})
end
def test_quoted_attr
assert_equal([[:emptytag, '<e a=">"/>']], scan('<e a=">"/>'))
end
def test_bare_slash
assert_equal([[:stag, '<n dir=/foo/bar/>']], scan('<n dir=/foo/bar/>'))
assert_equal([[:stag, '<n a=v/>']], scan('<n a=v/>'))
end
end

37
vendor/htree/test/test-security.rb vendored Normal file
View file

@ -0,0 +1,37 @@
require 'test/unit'
require 'htree/parse'
require 'htree/template'
require 'pathname'
class TestSecurity < Test::Unit::TestCase
def safe(n)
assert_equal(0, $SAFE)
Thread.new {
$SAFE = n
assert_equal(n, $SAFE)
yield
}.join
assert_equal(0, $SAFE)
end
def test_parse
safe(1) {
assert_equal(1, $SAFE)
assert_nothing_raised { HTree.parse("") }
assert_raise(SecurityError) { HTree.parse("".taint) }
}
assert_nothing_raised { HTree.parse("") }
assert_nothing_raised { HTree.parse("".taint) }
end
def test_template
safe(1) {
assert_nothing_raised { HTree.expand_template("/dev/null", nil, '') }
assert_raise(SecurityError) { HTree.expand_template("/dev/null".taint, nil, '') }
}
assert_nothing_raised { HTree.expand_template("/dev/null", nil, '') }
assert_nothing_raised { HTree.expand_template("/dev/null".taint, nil, '') }
end
end

142
vendor/htree/test/test-subnode.rb vendored Normal file
View file

@ -0,0 +1,142 @@
require 'test/unit'
require 'htree'
class TestSubnode < Test::Unit::TestCase
def test_elem_get
e1 = HTree.parse("<a href=x>abc</a>").root
assert_equal(HTree::Text.new("x"), e1.get_subnode("href"))
assert_equal(HTree::Text.new("abc"), e1.get_subnode(0))
end
def test_elem_subst
e1 = HTree.parse_xml("<a href=x>abc</a>").root
e2 = e1.subst_subnode("href"=>"xxx", 0=>"def")
assert_equal("a", e2.name)
assert_equal("xxx", e2.fetch_attr("href"))
assert_equal([HTree::Text.new("def")], e2.children)
assert_equal([], e1.subst_subnode(0=>nil).children)
end
def test_elem_subst_empty
e1 = HTree.parse("<img />").root
assert_equal(true, e1.empty_element?)
assert_equal(true, e1.subst_subnode("src"=>"xxx").empty_element?)
assert_equal(false, e1.subst_subnode(0=>"xxx").empty_element?)
end
def test_elem_multiple_attr_value
h = {"b"=>"c", HTree::Name.new(nil, "", "b")=>"d"}
assert_match(/\A(cd|dc)\z/,
HTree::Elem.new("a").subst_subnode(h).get_subnode('b').to_s)
a = [["b","c"], [HTree::Name.new(nil, "", "b"),"d"]]
assert_equal('cd',
HTree::Elem.new("a").subst_subnode(a).get_subnode('b').to_s)
assert_equal('dc',
HTree::Elem.new("a").subst_subnode(a.reverse).get_subnode('b').to_s)
end
def test_elem_subst_outrange
e1 = HTree("<r>abc</r>").root
e2 = e1.subst_subnode(-1=>HTree('<x/>'), 1=>HTree('<y/>'))
assert_equal(HTree('<r><x/>abc<y/></r>').root, e2)
end
def test_doc_subst_outrange
d1 = HTree("<r>abc</r>")
d2 = d1.subst_subnode(-1=>HTree('<x/>'), 1=>HTree('<y/>'))
assert_equal(HTree('<x/><r>abc</r><y/>'), d2)
end
def test_doc_get
doc = HTree.parse("<?xml?><a href=x>abc</a> ")
assert_equal(doc.root, doc.get_subnode(1))
end
def test_doc_subst
doc1 = HTree.parse("<?xml?><a href=x>abc</a> ")
doc2 = doc1.subst_subnode(1=>"yy")
assert_equal(HTree::Text.new("yy"), doc2.children[1])
assert_equal([], doc1.subst_subnode(0=>nil, 1=>nil, 2=>nil).children)
end
def test_doc_loc
d1 = HTree.parse("<r>a</r>")
d2 = HTree.parse("<q/>")
assert_equal(d2, d1.subst_subnode(0=>d2.make_loc))
end
def test_doc
e = HTree.parse("<r>a</r>").root
d = HTree.parse("<?xml version='1.0'?><!DOCTYPE q><q/>")
r = HTree('<r><q/></r>').root
assert_equal(r, e.subst_subnode(0=>d))
assert_equal(r, e.subst_subnode(0=>d.make_loc))
assert_equal(r, e.subst_subnode(0=>[d]))
assert_equal(r, e.subst_subnode(0=>[d.make_loc]))
end
def test_doc2
e = HTree.parse("<r>a</r>")
d = HTree.parse("<?xml version='1.0'?><!DOCTYPE q><q/>")
r = HTree('<q/>')
assert_equal(r, e.subst_subnode(0=>d))
assert_equal(r, e.subst_subnode(0=>d.make_loc))
assert_equal(r, e.subst_subnode(0=>[d]))
assert_equal(r, e.subst_subnode(0=>[d.make_loc]))
end
def test_change_by_subst_itself
l = HTree("<r>a</r>").make_loc
l2 = l.get_subnode(0, 0).subst_itself('x')
assert_equal(HTree::Text.new('x'), l2.to_node)
assert_equal(HTree('<r>x</r>'), l2.top.to_node)
l2 = l.get_subnode(0).subst_itself('xxx')
assert_equal(HTree::Text.new('xxx'), l2.to_node)
assert_equal(HTree('xxx'), l2.top.to_node)
end
def test_add_by_subst_itself
l = HTree("<r>a</r>").make_loc
l2 = l.get_subnode(0, 'x').subst_itself('y')
assert_equal(HTree::Text.new('y'), l2.to_node)
assert_equal(HTree('<r x="y">a</r>'), l2.top.to_node)
l2 = l.get_subnode(0, 0).subst_itself('b')
assert_equal(HTree::Text.new('b'), l2.to_node)
assert_equal(HTree('<r>b</r>'), l2.top.to_node)
xmldecl = HTree('<?xml version="1.0"?>').get_subnode(0)
l2 = l.get_subnode(-1).subst_itself(xmldecl)
assert_equal(0, l2.index)
assert_equal(xmldecl, l2.to_node)
assert_equal(HTree('<?xml version="1.0"?><r>a</r>'), l2.top.to_node)
procins = HTree('<?xxx yyy?>').get_subnode(0)
l2 = l.get_subnode(10).subst_itself(procins)
assert_equal(1, l2.index)
assert_equal(procins, l2.to_node)
assert_equal(HTree('<r>a</r><?xxx yyy?>'), l2.top.to_node)
end
def test_del_by_subst_itself
l = HTree("<r x='y'><x/>y<z/></r>").make_loc
l2 = l.get_subnode(0, 'x').subst_itself(nil)
assert_equal(nil, l2.to_node)
assert_equal(HTree('<r><x/>y<z/></r>'), l2.top.to_node)
l2 = l.get_subnode(0, 1).subst_itself(nil)
assert_equal(HTree('<r x="y"><x/><z/></r>'), l2.top.to_node)
l = HTree('<?xml version="1.0"?><r/>').make_loc
l2 = l.get_subnode(0).subst_itself(nil)
assert_equal(HTree('<r/>'), l2.top.to_node)
end
def test_subst
l = HTree('<?xml version="1.0"?><r><x/><y/><z/></r>').make_loc
assert_equal(HTree("<r>x<y>a</y><z k=v /></r>"),
l.to_node.subst({
l.get_subnode(0) => nil,
l.get_subnode(1, 0) => 'x',
l.get_subnode(1, 1, 0) => 'a',
l.get_subnode(1, 2, 'k') => 'v'
}))
end
end

287
vendor/htree/test/test-template.rb vendored Normal file
View file

@ -0,0 +1,287 @@
require 'test/unit'
require 'htree/template'
require 'stringio'
class TestTemplate < Test::Unit::TestCase
Decl = '<?xml version="1.0" encoding="US-ASCII"?>'
def assert_xhtml(expected, template, message=nil)
prefix = '<?xml version="1.0" encoding="US-ASCII"?>' +
"<html xmlns=\"http://www.w3.org/1999/xhtml\"\n>"
suffix = "</html\n>"
result = HTree.expand_template(''){"<?xml version=\"1.0\"?><html>#{template}</html>"}
assert_match(/\A#{Regexp.quote prefix}/, result)
assert_match(/#{Regexp.quote suffix}\z/, result)
result = result[prefix.length..(-suffix.length-1)]
assert_equal(expected, result, message)
end
def test_text
assert_xhtml("<e\n>1</e\n>", '<e _text=1>d</e>')
assert_xhtml('1', '<span _text=1>d</span>')
assert_xhtml("<span x=\"2\"\n>1</span\n>", '<span x=2 _text=1>d</span>')
assert_xhtml("abc", %q{a<span _text="'b'"/>c})
end
def test_tree
assert_xhtml("<e\n><z\n>x</z\n></e\n>", '<e _tree="HTree(&quot;<z>x</z>&quot;)">d</e>')
assert_xhtml("<n:e xmlns:n=\"a\"\n><n:z\n>x</n:z\n></n:e\n>", '<n:e xmlns:n=a _tree="HTree(&quot;<n:z xmlns:n=a>x</n:z>&quot;)">d</n:e>')
end
def test_attr
assert_xhtml("<e x=\"1\"\n>d</e\n>", '<e _attr_x=1>d</e>')
assert_xhtml("<span x=\"1\"\n>d</span\n>", '<span _attr_x=1>d</span>')
assert_xhtml("<span x=\"&quot;\"\n>d</span\n>", '<span _attr_x=\'"\x22"\'>d</span>')
end
def test_if
assert_xhtml("<e\n>d</e\n>", '<e _if=true>d</e>')
assert_xhtml('', '<e _if=false>d</e>')
assert_xhtml("<f\n>dd</f\n>", '<e _if=false _else=m>d</e><f _template=m>dd</f>')
assert_xhtml('d', '<span _if=true>d</span>')
end
def test_iter
assert_xhtml("<o\n><i\n>1</i\n></o\n><o\n><i\n>2</i\n></o\n><o\n><i\n>3</i\n></o\n>",
'<o _iter=[1,2,3].each//v><i _text=v /></o>')
assert_xhtml("<i\n>1</i\n><i\n>2</i\n><i\n>3</i\n>",
'<span _iter=[1,2,3].each//v><i _text=v /></span>')
end
def test_iter_content
assert_xhtml("<o\n><i\n>1</i\n><i\n>2</i\n><i\n>3</i\n></o\n>",
'<o _iter_content=[1,2,3].each//v><i _text=v /></o>')
assert_xhtml("<i\n>1</i\n><i\n>2</i\n><i\n>3</i\n>",
'<span _iter_content=[1,2,3].each//v><i _text=v /></span>')
end
def test_iter_local_template
assert_xhtml("<o\n><i\n>1</i\n></o\n><o\n><i\n>2</i\n></o\n><o\n><i\n>3</i\n></o\n>",
'<o _iter=[1,2,3].each//v><i _call=m /><i _template=m _text=v></i></o>')
end
def test_call
assert_xhtml("<f\n>1</f\n>",
'<e _call=m(1) /><f _template=m(v) _text=v></f>')
end
def test_template
assert_xhtml('d',
'<span _template="span()">d</span><e _call="span()"></e>')
end
def test_file
assert_equal(<<'End'.chop,
<?xml version="1.0" encoding="US-ASCII"?><html xmlns="http://www.w3.org/1999/xhtml"
><title
>aaa</title
></html
>
End
HTree.expand_template("#{File.dirname __FILE__}/template.html", "aaa", ''))
end
def test_whitespace
assert_xhtml("<x\n></x\n>", '<x> </x>')
assert_xhtml("<x\n>&#32;</x\n>", '<x>&#32;</x>')
assert_xhtml("<pre\n> </pre\n>", '<pre> </pre>')
assert_xhtml(" ", %q{<span _text="' '"> </span>})
assert_xhtml(" ", %q{<span _text="' '"/>})
end
def test_ignorable
assert_xhtml("<div\n>a</div\n>", '<div>a</div>')
assert_xhtml("<span\n>a</span\n>", '<span>a</span>')
end
def test_template_in_attr
assert_xhtml("<a x=\"1\"\n></a\n>", '<a _attr_x=1><b _template=m></b></a>')
end
def test_empty_block_argument
assert_xhtml("vv", '<span _iter="2.times//">v</span>')
end
def test_empty_element
assert_xhtml("<elem\n/>", '<elem />') # 2004-06-10: reported by Takuo KITAME
assert_xhtml("<elem x=\"1\"\n/>", '<elem _attr_x=1 />')
assert_xhtml("<elem\n></elem\n>", '<elem _text=\'""\' />')
assert_xhtml("<elem\n/>", '<elem _if="true" />')
assert_xhtml("", '<elem _if="false" />')
assert_xhtml("<foo\n/>", '<elem _if="false" _else="foo"/><foo _template="foo"/>')
assert_xhtml("<elem\n/><elem\n/>", '<elem _iter="2.times//" />')
assert_xhtml("<elem\n></elem\n>", '<elem _iter_content="2.times//" />')
end
def test_empty_element_start_end_tag
assert_xhtml("<elem\n></elem\n>", '<elem></elem>')
assert_xhtml("<elem x=\"1\"\n></elem\n>", '<elem _attr_x=1 ></elem>')
assert_xhtml("<elem\n></elem\n>", '<elem _text=\'""\' ></elem>')
assert_xhtml("<elem\n></elem\n>", '<elem _if="true" ></elem>')
assert_xhtml("", '<elem _if="false" ></elem>')
assert_xhtml("<foo\n></foo\n>", '<elem _if="false" _else="foo"></elem><foo _template="foo"></foo>')
assert_xhtml("<elem\n></elem\n><elem\n></elem\n>", '<elem _iter="2.times//" ></elem>')
assert_xhtml("<elem\n></elem\n>", '<elem _iter_content="2.times//" ></elem>')
end
def test_toplevel_local_variable
eval("htree_test_toplevel_local_variable = :non_modified_value", TOPLEVEL_BINDING)
HTree.expand_template("#{File.dirname __FILE__}/assign.html", "aaa", '')
assert_equal(:non_modified_value, eval("htree_test_toplevel_local_variable", TOPLEVEL_BINDING))
eval("htree_test_toplevel_local_variable = 1", TOPLEVEL_BINDING)
end
def test_extend_compiled_template
m = HTree.compile_template('<div _template="m">self is <span _text="inspect"></span></div>')
o = "zzz"
o.extend m
assert_equal('<?xml version="1.0" encoding="US-ASCII"?>self is "zzz"',
HTree.expand_template(''){'<div _call="o.m"></div>'})
end
def test_attr_nbsp
@t = HTree::Text.parse_pcdata('&nbsp;')
assert_xhtml("<span x=\"&nbsp;\"\n>d</span\n>", '<span _attr_x="@t">d</span>')
end
def test_text_nbsp
@t = HTree::Text.parse_pcdata('&nbsp;')
assert_xhtml("&nbsp;", '<span _text="@t">d</span>')
end
def test_content_text
assert_xhtml("<e\n>ab</e\n>", '<e _text>"a"+"b"</e>')
assert_xhtml("<e\n>2</e\n>", '<e _text>1+1</e>')
end
end
class MemFile
def initialize(str)
@str = str
end
def read
@str
end
end
class TestTemplateScopeObj
Const = 'good_const'
@@cvar = 'good_cvar'
def initialize
@ivar = 'good_ivar'
end
end
class TestTemplateScope < Test::Unit::TestCase
Const = 'bad_const'
@@cvar = 'bad_cvar'
def setup
@ivar = 'bad_ivar'
eval("test_local_variable = 'bad_lvar'", TOPLEVEL_BINDING)
end
XMLDeclStr = '<?xml version="1.0" encoding="US-ASCII"?>'
def test_expand_template
obj = TestTemplateScopeObj.new
assert_equal("#{XMLDeclStr}[TestTemplateScopeObj]",
HTree.expand_template(MemFile.new('<span _text="Module.nesting.inspect"/>'), obj, ''))
assert_equal("#{XMLDeclStr}good_ivar",
HTree.expand_template(MemFile.new('<span _text="@ivar"/>'), obj, ''))
assert_equal("#{XMLDeclStr}good_cvar",
HTree.expand_template(MemFile.new('<span _text="@@cvar"/>'), obj, ''))
assert_equal("#{XMLDeclStr}good_const",
HTree.expand_template(MemFile.new('<span _text="Const"/>'), obj, ''))
test_local_variable = 'bad_lvar'
assert_equal("#{XMLDeclStr}good_lvar",
HTree.expand_template(MemFile.new('<span _text="begin test_local_variable rescue NameError; \'good_lvar\' end"/>'), obj, ''))
end
def test_compile_template
obj = TestTemplateScopeObj.new
mod = HTree.compile_template(MemFile.new(<<-'End'))
<span _template=test_nesting _text="Module.nesting.inspect"/>
<span _template=test_const _text="Const"/>
<span _template=test_cvar _text="@@cvar"/>
<span _template=test_ivar _text="@ivar"/>
End
mod.module_eval <<-'End'
Const = 'mod_const'
@@cvar = 'mod_cvar'
@ivar = 'mod_ivar'
End
assert_equal("[#{mod.inspect}]", mod.test_nesting.extract_text.to_s)
assert_equal("mod_const", mod.test_const.extract_text.to_s)
assert_equal("mod_cvar", mod.test_cvar.extract_text.to_s)
assert_equal("mod_ivar", mod.test_ivar.extract_text.to_s)
obj = Object.new
obj.instance_variable_set :@ivar, 'obj_ivar'
obj.extend mod
assert_equal("[#{mod.inspect}]", obj.__send__(:test_nesting).extract_text.to_s)
assert_equal("mod_const", obj.__send__(:test_const).extract_text.to_s)
assert_equal("mod_cvar", obj.__send__(:test_cvar).extract_text.to_s)
assert_equal("obj_ivar", obj.__send__(:test_ivar).extract_text.to_s)
end
end
class TestCDATA < Test::Unit::TestCase
def test_html_script
v = "x<y"
assert_equal("<html><script>x<y</script></html>",
HTree.expand_template('') {"<html><script _text=\"v\">ab</script>"}.gsub(/\n/, ''))
end
def test_xml_script
v = "x<y"
assert_equal("<?xml version=\"1.0\" encoding=\"US-ASCII\"?><html xmlns=\"http://www.w3.org/1999/xhtml\"><script>x&lt;y</script></html>",
HTree.expand_template('') {"<?xml version=\"1.0\"?><html><script _text=\"v\">ab</script>"}.gsub(/\n/, ''))
end
def test_html_script_invalid_content
v = "x</y"
assert_raise(ArgumentError) {
HTree.expand_template('') {"<html><script _text=\"v\">ab</script>"}
}
end
end
class TestCharset < Test::Unit::TestCase
class CharsetString < String
attr_accessor :charset
end
def with_kcode(kcode)
old_kcode = $KCODE
begin
$KCODE = kcode
yield
ensure
$KCODE = old_kcode
end
end
def test_us_ascii
with_kcode('E') {
out = HTree.expand_template(CharsetString.new) { "<html>abc" }
assert_equal(out.charset, 'US-ASCII')
}
end
def test_euc_jp
with_kcode('E') {
out = HTree.expand_template(CharsetString.new) { "<html>\xa1\xa1" }
assert_equal(out.charset, 'EUC-JP')
}
end
end
class TestTemplateDOCTYPE < Test::Unit::TestCase
def test_html
assert_equal(
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html></html>',
HTree.expand_template('') {'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>'}.gsub(/\n/, ''))
end
end

35
vendor/htree/test/test-text.rb vendored Normal file
View file

@ -0,0 +1,35 @@
require 'test/unit'
require 'htree/text'
class TestText < Test::Unit::TestCase
def test_new
assert_equal("abc&amp;amp;def", HTree::Text.new("abc&amp;def").rcdata)
end
=begin
def test_parse
assert_equal("abc&amp;def", HTree::Text.parse("abc&amp;def").rcdata)
end
def test_to_s
assert_equal("abc&def", HTree::Text.parse("abc&amp;def").to_s)
end
=end
def kcode(kc)
old = $KCODE
begin
$KCODE = kc
yield
ensure
$KCODE = old
end
end
def test_normalize
kcode('EUC') {
assert_equal("<ABC&#38;&#38;&#160;\xa6\xc1",
HTree::Text.new_internal("&lt;&#65;&#x42;C&amp;&#38;&nbsp;&alpha;").normalized_rcdata)
}
end
end

69
vendor/htree/test/test-traverse.rb vendored Normal file
View file

@ -0,0 +1,69 @@
require 'test/unit'
require 'htree/traverse'
require 'htree/parse'
require 'htree/equality'
class TestTraverse < Test::Unit::TestCase
def test_filter
l = HTree.parse('<a><b>x</b><b/><a/>').make_loc
l2 = l.filter {|n| n.path != 'doc()/a/b[1]' }
assert_equal(HTree.parse('<a><b/><a/>'), l2)
end
def test_title
inputs = [
HTree.parse('<html><title>aaa</title></html>'),
HTree.parse(<<'End')
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns="http://purl.org/rss/1.0/">
<channel>
<title>aaa</title>
</channel>
</rdf:RDF>
End
]
result = HTree::Text.new('aaa')
inputs.each {|input|
assert_equal(result, input.title)
}
inputs.each {|input|
assert_equal(result, input.make_loc.title)
}
end
def test_author
inputs = [
HTree.parse('<html><meta name=author content=xxx></html>'),
HTree.parse('<html><link rev=made title=xxx></html>'),
HTree.parse(<<'End'),
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns="http://purl.org/rss/1.0/">
<channel>
<dc:creator>xxx</dc:creator>
</channel>
</rdf:RDF>
End
HTree.parse(<<'End')
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns="http://purl.org/rss/1.0/">
<channel>
<dc:publisher>xxx</dc:publisher>
</channel>
</rdf:RDF>
End
]
result = HTree::Text.new('xxx')
inputs.each {|input|
#assert_equal(result, input.author)
}
inputs.each {|input|
assert_equal(result, input.make_loc.author)
}
end
end