htree 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +4 -0
- data/Makefile +20 -0
- data/Manifest +58 -0
- data/README +61 -0
- data/Rakefile +37 -0
- data/htree.gemspec +32 -0
- data/init.rb +1 -0
- data/install.rb +112 -0
- data/lib/htree.rb +97 -0
- data/lib/htree/container.rb +8 -0
- data/lib/htree/context.rb +69 -0
- data/lib/htree/display.rb +46 -0
- data/lib/htree/doc.rb +149 -0
- data/lib/htree/elem.rb +262 -0
- data/lib/htree/encoder.rb +217 -0
- data/lib/htree/equality.rb +219 -0
- data/lib/htree/extract_text.rb +37 -0
- data/lib/htree/fstr.rb +32 -0
- data/lib/htree/gencode.rb +193 -0
- data/lib/htree/htmlinfo.rb +672 -0
- data/lib/htree/inspect.rb +108 -0
- data/lib/htree/leaf.rb +92 -0
- data/lib/htree/loc.rb +369 -0
- data/lib/htree/modules.rb +49 -0
- data/lib/htree/name.rb +122 -0
- data/lib/htree/output.rb +212 -0
- data/lib/htree/parse.rb +410 -0
- data/lib/htree/raw_string.rb +127 -0
- data/lib/htree/regexp-util.rb +19 -0
- data/lib/htree/rexml.rb +131 -0
- data/lib/htree/scan.rb +176 -0
- data/lib/htree/tag.rb +113 -0
- data/lib/htree/template.rb +961 -0
- data/lib/htree/text.rb +115 -0
- data/lib/htree/traverse.rb +497 -0
- data/test-all.rb +5 -0
- data/test/assign.html +1 -0
- data/test/template.html +4 -0
- data/test/test-attr.rb +67 -0
- data/test/test-charset.rb +79 -0
- data/test/test-context.rb +29 -0
- data/test/test-display_xml.rb +45 -0
- data/test/test-elem-new.rb +101 -0
- data/test/test-encoder.rb +53 -0
- data/test/test-equality.rb +55 -0
- data/test/test-extract_text.rb +18 -0
- data/test/test-gencode.rb +27 -0
- data/test/test-leaf.rb +25 -0
- data/test/test-loc.rb +60 -0
- data/test/test-namespace.rb +147 -0
- data/test/test-output.rb +133 -0
- data/test/test-parse.rb +115 -0
- data/test/test-raw_string.rb +17 -0
- data/test/test-rexml.rb +70 -0
- data/test/test-scan.rb +153 -0
- data/test/test-security.rb +37 -0
- data/test/test-subnode.rb +142 -0
- data/test/test-template.rb +313 -0
- data/test/test-text.rb +43 -0
- data/test/test-traverse.rb +69 -0
- metadata +166 -0
- metadata.gz.sig +1 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'htree/extract_text'
|
3
|
+
require 'htree/equality'
|
4
|
+
|
5
|
+
class TestExtractText < Test::Unit::TestCase
|
6
|
+
def test_single
|
7
|
+
n = HTree::Text.new('abc')
|
8
|
+
assert_equal(n, n.extract_text)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_elem
|
12
|
+
t = HTree::Text.new('abc')
|
13
|
+
n = HTree::Elem.new('e', t)
|
14
|
+
assert_equal(t, n.extract_text)
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'htree/gencode'
|
3
|
+
require 'htree/parse'
|
4
|
+
|
5
|
+
class TestGenCode < Test::Unit::TestCase
|
6
|
+
def run_code(code, top_context)
|
7
|
+
out = HTree::Encoder.new(HTree::Encoder.internal_charset, HTree::Encoder.internal_charset)
|
8
|
+
eval(code)
|
9
|
+
out.finish
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_xmlns
|
13
|
+
t = HTree.parse_xml('<p:n xmlns:p=z><p:m>bb').children[0].children[0] # <p:m>bb</p:m>
|
14
|
+
code = t.generate_xml_output_code
|
15
|
+
|
16
|
+
assert_equal("<p:m xmlns:p=\"z\"\n>bb</p:m\n>", run_code(code, HTree::DefaultContext))
|
17
|
+
assert_equal("<p:m\n>bb</p:m\n>", run_code(code, HTree::DefaultContext.subst_namespaces("p"=>"z")))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_xmlns_chref
|
21
|
+
t = HTree.parse_xml('<p:n xmlns:p="a&<>"b"/>').children[0]
|
22
|
+
code = t.generate_xml_output_code
|
23
|
+
assert_equal("<p:n xmlns:p=\"a&<>"b\"\n/>", run_code(code, HTree::DefaultContext))
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
data/test/test-leaf.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'htree/leaf'
|
3
|
+
|
4
|
+
class TestProcIns < Test::Unit::TestCase
|
5
|
+
def test_initialize
|
6
|
+
assert_raises(HTree::ProcIns::Error) { HTree::ProcIns.new!('target', "?>") }
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_new
|
10
|
+
assert_equal('? >', HTree::ProcIns.new('target', "?>").content)
|
11
|
+
assert_equal(nil, HTree::ProcIns.new('target', nil).content)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestComment < Test::Unit::TestCase
|
16
|
+
def test_initialize
|
17
|
+
assert_raises(HTree::Comment::Error) { HTree::Comment.new!("a--b") }
|
18
|
+
assert_raises(HTree::Comment::Error) { HTree::Comment.new!("a-") }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_new
|
22
|
+
assert_equal('a- -b', HTree::Comment.new("a--b").content)
|
23
|
+
assert_equal('a- ', HTree::Comment.new("a-").content)
|
24
|
+
end
|
25
|
+
end
|
data/test/test-loc.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'htree/loc'
|
3
|
+
require 'htree/parse'
|
4
|
+
require 'htree/traverse'
|
5
|
+
|
6
|
+
class TestLoc < Test::Unit::TestCase
|
7
|
+
def test_make_loc
|
8
|
+
t = HTree.parse('<?xml version="1.0"?><!DOCTYPE root><root>a<?x y?><!-- c --></boo>')
|
9
|
+
assert_instance_of(HTree::Doc::Loc, t.make_loc)
|
10
|
+
assert_instance_of(HTree::XMLDecl::Loc, t.children[0].make_loc)
|
11
|
+
assert_instance_of(HTree::DocType::Loc, t.children[1].make_loc)
|
12
|
+
assert_instance_of(HTree::Elem::Loc, t.children[2].make_loc)
|
13
|
+
assert_instance_of(HTree::Text::Loc, t.children[2].children[0].make_loc)
|
14
|
+
assert_instance_of(HTree::ProcIns::Loc, t.children[2].children[1].make_loc)
|
15
|
+
assert_instance_of(HTree::Comment::Loc, t.children[2].children[2].make_loc)
|
16
|
+
assert_instance_of(HTree::BogusETag::Loc, t.children[2].children[3].make_loc)
|
17
|
+
assert_equal(nil, t.make_loc.parent)
|
18
|
+
assert_equal(nil, t.make_loc.index)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_get_subnode
|
22
|
+
t = HTree.parse('<?xml version="1.0"?><!DOCTYPE root><root>a<?x y?><!-- c --></boo>')
|
23
|
+
l = t.make_loc
|
24
|
+
assert_instance_of(HTree::Doc::Loc, l)
|
25
|
+
assert_instance_of(HTree::Location, l.get_subnode(-1))
|
26
|
+
assert_instance_of(HTree::XMLDecl::Loc, l.get_subnode(0))
|
27
|
+
assert_instance_of(HTree::DocType::Loc, l.get_subnode(1))
|
28
|
+
assert_instance_of(HTree::Elem::Loc, l2 = l.get_subnode(2))
|
29
|
+
assert_instance_of(HTree::Location, l.get_subnode(3))
|
30
|
+
assert_instance_of(HTree::Location, l2.get_subnode(-1))
|
31
|
+
assert_instance_of(HTree::Location, l2.get_subnode('attr'))
|
32
|
+
assert_instance_of(HTree::Text::Loc, l2.get_subnode(0))
|
33
|
+
assert_instance_of(HTree::ProcIns::Loc, l2.get_subnode(1))
|
34
|
+
assert_instance_of(HTree::Comment::Loc, l2.get_subnode(2))
|
35
|
+
assert_instance_of(HTree::BogusETag::Loc, l2.get_subnode(3))
|
36
|
+
assert_instance_of(HTree::Location, l2.get_subnode(4))
|
37
|
+
assert_same(l.get_subnode(0), l.get_subnode(0))
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_find_loc_step
|
41
|
+
t = HTree.parse('<a><b>x<!---->y</a><c/><a/>')
|
42
|
+
assert_equal('a[1]', t.find_loc_step(0))
|
43
|
+
assert_equal('c', t.find_loc_step(1))
|
44
|
+
assert_equal('a[2]', t.find_loc_step(2))
|
45
|
+
t = t.children[0]
|
46
|
+
assert_equal('b', t.find_loc_step(0))
|
47
|
+
t = t.children[0]
|
48
|
+
assert_equal('text()[1]', t.find_loc_step(0))
|
49
|
+
assert_equal('comment()', t.find_loc_step(1))
|
50
|
+
assert_equal('text()[2]', t.find_loc_step(2))
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_path
|
54
|
+
l = HTree.parse('<a><b>x</b><b/><a/>').make_loc
|
55
|
+
l2 = l.get_subnode(0, 0, 0)
|
56
|
+
assert_equal('doc()', l.path)
|
57
|
+
assert_equal('doc()/a/b[1]/text()', l2.path)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'htree/tag'
|
3
|
+
|
4
|
+
class TestNamespace < Test::Unit::TestCase
|
5
|
+
def assert_equal_exact(expected, actual, message=nil)
|
6
|
+
full_message = build_message(message, <<EOT, expected, actual)
|
7
|
+
<?> expected but was
|
8
|
+
<?>.
|
9
|
+
EOT
|
10
|
+
assert_block(full_message) { expected.equal_exact? actual }
|
11
|
+
end
|
12
|
+
|
13
|
+
# <ppp:nnn xmlns:ppp="uuu">
|
14
|
+
def test_prefixed
|
15
|
+
stag = HTree::STag.new("ppp:nnn",
|
16
|
+
[["xmlns:ppp", "uuu"], ["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
|
17
|
+
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
|
18
|
+
assert_equal("ppp:nnn", stag.element_name.qualified_name)
|
19
|
+
assert_equal("{uuu}nnn", stag.element_name.universal_name)
|
20
|
+
assert_equal("nnn", stag.element_name.local_name)
|
21
|
+
assert_equal("uuu", stag.element_name.namespace_uri)
|
22
|
+
assert_equal("ppp", stag.element_name.namespace_prefix)
|
23
|
+
|
24
|
+
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
|
25
|
+
assert_equal(1, nsattrs.length)
|
26
|
+
assert_equal(["ppp", "uuu"], nsattrs.shift)
|
27
|
+
|
28
|
+
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
|
29
|
+
assert_equal(3, attrs.length)
|
30
|
+
assert_equal(['', nil, "a", "x"], attrs.shift)
|
31
|
+
assert_equal(["u", "q", "b", "y"], attrs.shift)
|
32
|
+
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
|
33
|
+
end
|
34
|
+
|
35
|
+
# <nnn xmlns="uuu">
|
36
|
+
def test_default_ns
|
37
|
+
stag = HTree::STag.new("nnn",
|
38
|
+
[["xmlns", "uuu"],
|
39
|
+
["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
|
40
|
+
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
|
41
|
+
|
42
|
+
assert_equal("nnn", stag.element_name.qualified_name)
|
43
|
+
assert_equal("{uuu}nnn", stag.element_name.universal_name)
|
44
|
+
assert_equal("nnn", stag.element_name.local_name)
|
45
|
+
assert_equal("uuu", stag.element_name.namespace_uri)
|
46
|
+
assert_equal(nil, stag.element_name.namespace_prefix)
|
47
|
+
|
48
|
+
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
|
49
|
+
assert_equal(1, nsattrs.length)
|
50
|
+
assert_equal([nil, "uuu"], nsattrs.shift)
|
51
|
+
|
52
|
+
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
|
53
|
+
assert_equal(3, attrs.length)
|
54
|
+
assert_equal(['', nil, "a", "x"], attrs.shift)
|
55
|
+
assert_equal(["u", "q", "b", "y"], attrs.shift)
|
56
|
+
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
|
57
|
+
end
|
58
|
+
|
59
|
+
# <nnn xmlns="">
|
60
|
+
def test_no_default_ns
|
61
|
+
[{"q"=>"u"}, {nil=>"uu", "q"=>"u"}].each {|inh|
|
62
|
+
stag = HTree::STag.new("nnn",
|
63
|
+
[["xmlns", ""], ["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
|
64
|
+
HTree::DefaultContext.subst_namespaces(inh))
|
65
|
+
assert_equal("nnn", stag.element_name.qualified_name)
|
66
|
+
assert_equal("nnn", stag.element_name.universal_name)
|
67
|
+
assert_equal("nnn", stag.element_name.local_name)
|
68
|
+
assert_equal('', stag.element_name.namespace_uri)
|
69
|
+
assert_equal(nil, stag.element_name.namespace_prefix)
|
70
|
+
|
71
|
+
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
|
72
|
+
assert_equal(1, nsattrs.length)
|
73
|
+
assert_equal([nil, ""], nsattrs.shift)
|
74
|
+
|
75
|
+
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
|
76
|
+
assert_equal(3, attrs.length)
|
77
|
+
assert_equal(['', nil, "a", "x"], attrs.shift)
|
78
|
+
assert_equal(["u", "q", "b", "y"], attrs.shift)
|
79
|
+
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
# <nnn>
|
84
|
+
def test_no_ns
|
85
|
+
stag = HTree::STag.new("nnn",
|
86
|
+
[["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
|
87
|
+
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
|
88
|
+
|
89
|
+
assert_equal("nnn", stag.element_name.qualified_name)
|
90
|
+
assert_equal("nnn", stag.element_name.universal_name)
|
91
|
+
assert_equal("nnn", stag.element_name.local_name)
|
92
|
+
assert_equal('', stag.element_name.namespace_uri)
|
93
|
+
assert_equal(nil, stag.element_name.namespace_prefix)
|
94
|
+
|
95
|
+
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
|
96
|
+
assert_equal(0, nsattrs.length)
|
97
|
+
|
98
|
+
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
|
99
|
+
assert_equal(3, attrs.length)
|
100
|
+
assert_equal(['', nil, "a", "x"], attrs.shift)
|
101
|
+
assert_equal(["u", "q", "b", "y"], attrs.shift)
|
102
|
+
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
|
103
|
+
end
|
104
|
+
|
105
|
+
# internally allocated element without prefix
|
106
|
+
def test_universal_name_to_be_default_namespace
|
107
|
+
stag = HTree::STag.new("{uuu}nnn",
|
108
|
+
[["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"]],
|
109
|
+
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
|
110
|
+
assert_equal("nnn", stag.element_name.qualified_name)
|
111
|
+
assert_equal("{uuu}nnn", stag.element_name.universal_name)
|
112
|
+
assert_equal("nnn", stag.element_name.local_name)
|
113
|
+
assert_equal("uuu", stag.element_name.namespace_uri)
|
114
|
+
assert_equal(nil, stag.element_name.namespace_prefix)
|
115
|
+
|
116
|
+
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
|
117
|
+
assert_equal(0, nsattrs.length)
|
118
|
+
|
119
|
+
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
|
120
|
+
assert_equal(3, attrs.length)
|
121
|
+
assert_equal(['', nil, "a", "x"], attrs.shift)
|
122
|
+
assert_equal(["u", "q", "b", "y"], attrs.shift)
|
123
|
+
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_prefixed_universal_name
|
127
|
+
stag = HTree::STag.new("ppp{uuu}nnn",
|
128
|
+
[["a", "x"], ["q:b", "y"], ["pp{uu}c", "z"], ["q{uu}d", "w"]],
|
129
|
+
HTree::DefaultContext.subst_namespaces({"q"=>"u"}))
|
130
|
+
assert_equal("ppp:nnn", stag.element_name.qualified_name)
|
131
|
+
assert_equal("{uuu}nnn", stag.element_name.universal_name)
|
132
|
+
assert_equal("nnn", stag.element_name.local_name)
|
133
|
+
assert_equal("uuu", stag.element_name.namespace_uri)
|
134
|
+
assert_equal("ppp", stag.element_name.namespace_prefix)
|
135
|
+
|
136
|
+
nsattrs = []; stag.each_namespace_attribute {|p, u| nsattrs << [p, u] }
|
137
|
+
assert_equal(0, nsattrs.length)
|
138
|
+
|
139
|
+
attrs = []; stag.each_attribute {|n,t| attrs << [n.namespace_uri,n.namespace_prefix,n.local_name,t.to_s] }
|
140
|
+
assert_equal(4, attrs.length)
|
141
|
+
assert_equal(['', nil, "a", "x"], attrs.shift)
|
142
|
+
assert_equal(["u", "q", "b", "y"], attrs.shift)
|
143
|
+
assert_equal(["uu", "pp", "c", "z"], attrs.shift)
|
144
|
+
assert_equal(["uu", "q", "d", "w"], attrs.shift)
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
data/test/test-output.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'htree'
|
3
|
+
|
4
|
+
class TestOutput < Test::Unit::TestCase
|
5
|
+
def gen(t, meth=:output, *rest)
|
6
|
+
encoder = HTree::Encoder.new('US-ASCII', 'US-ASCII')
|
7
|
+
t.__send__(meth, *(rest + [encoder, HTree::DefaultContext]))
|
8
|
+
encoder.finish
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_text
|
12
|
+
assert_equal('a&<>"b', gen(HTree::Text.new('a&<>"b')))
|
13
|
+
|
14
|
+
assert_equal("abc&def", gen(HTree::Text.new("abc&def")))
|
15
|
+
assert_equal('"\'&', gen(HTree::Text.new('"\'&')))
|
16
|
+
assert_equal('"\'<&>', gen(HTree::Text.new('"\'<&>')))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_text_attvalue
|
20
|
+
assert_equal('"a&<>"b"', gen(HTree::Text.new('a&<>"b'), :output_attvalue))
|
21
|
+
|
22
|
+
assert_equal('"abc"', gen(HTree::Text.new("abc"), :output_attvalue))
|
23
|
+
assert_equal('"""', gen(HTree::Text.new('"'), :output_attvalue))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_name
|
27
|
+
assert_equal('abc', gen(HTree::Name.parse_element_name('abc', HTree::DefaultContext)))
|
28
|
+
assert_equal('n', gen(HTree::Name.new(nil, 'u', 'n')))
|
29
|
+
assert_equal('p:n', gen(HTree::Name.new('p', 'u', 'n')))
|
30
|
+
assert_equal('n', gen(HTree::Name.new(nil, '', 'n')))
|
31
|
+
assert_equal('xmlns', gen(HTree::Name.new('xmlns', nil, nil)))
|
32
|
+
assert_equal('xmlns:n', gen(HTree::Name.new('xmlns', nil, 'n')))
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_name_attribute
|
36
|
+
assert_equal('abc="a&<>"b"',
|
37
|
+
gen(HTree::Name.parse_element_name('abc', HTree::DefaultContext),
|
38
|
+
:output_attribute,
|
39
|
+
HTree::Text.new('a&<>"b')))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_doc
|
43
|
+
t = HTree::Doc.new(HTree::Elem.new('a'), HTree::Elem.new('b'))
|
44
|
+
assert_equal("<a\n/><b\n/>", gen(t))
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_elem
|
48
|
+
t = HTree::Elem.new('a', [])
|
49
|
+
assert_equal("<a\n></a\n>", gen(t))
|
50
|
+
|
51
|
+
assert_equal("<b\n/>",
|
52
|
+
gen(HTree::Elem.new!(HTree::STag.new('b'))))
|
53
|
+
assert_equal("<b\n></b\n>",
|
54
|
+
gen(HTree::Elem.new!(HTree::STag.new('b'), [])))
|
55
|
+
assert_equal("<a\n><b\n/><c\n/><d\n/></a\n>",
|
56
|
+
gen(HTree::Elem.new!(HTree::STag.new('a'), [
|
57
|
+
HTree::Elem.new!(HTree::STag.new('b')),
|
58
|
+
HTree::Elem.new!(HTree::STag.new('c')),
|
59
|
+
HTree::Elem.new!(HTree::STag.new('d'))
|
60
|
+
])))
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_elem_empty
|
64
|
+
t = HTree::Elem.new('a')
|
65
|
+
assert_equal("<a\n/>", gen(t))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_stag
|
69
|
+
assert_equal("<name\n>",
|
70
|
+
gen(HTree::STag.new("name"), :output_stag))
|
71
|
+
assert_equal("<name\n/>",
|
72
|
+
gen(HTree::STag.new("name"), :output_emptytag))
|
73
|
+
assert_equal("</name\n>",
|
74
|
+
gen(HTree::STag.new("name"), :output_etag))
|
75
|
+
|
76
|
+
assert_equal("<name a=\"b\"\n/>",
|
77
|
+
gen(HTree::STag.new("name", [["a", "b"]]), :output_emptytag))
|
78
|
+
assert_equal("<name a=\"<"\'>\"\n/>",
|
79
|
+
gen(HTree::STag.new("name", [['a', '<"\'>']]), :output_emptytag))
|
80
|
+
|
81
|
+
assert_equal("<ppp:nnn xmlns=\"uuu"b\"\n/>",
|
82
|
+
gen(HTree::STag.new("ppp:nnn", [["xmlns", "uuu\"b"]]), :output_emptytag))
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_xmldecl
|
86
|
+
t = HTree::XMLDecl.new('1.0', 'US-ASCII')
|
87
|
+
assert_equal('', gen(t))
|
88
|
+
assert_equal('<?xml version="1.0" encoding="US-ASCII"?>',
|
89
|
+
gen(t, :output_prolog_xmldecl))
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_doctype
|
93
|
+
t = HTree::DocType.new('html',
|
94
|
+
'-//W3C//DTD HTML 4.01//EN',
|
95
|
+
'http://www.w3.org/TR/html4/strict.dtd')
|
96
|
+
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', gen(t))
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_procins
|
100
|
+
t = HTree::ProcIns.new('xml-stylesheet', 'type="text/xml" href="#style1"')
|
101
|
+
assert_equal('<?xml-stylesheet type="text/xml" href="#style1"?>', gen(t))
|
102
|
+
t = HTree::ProcIns.new('x', nil)
|
103
|
+
assert_equal('<?x?>', gen(t))
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_comment
|
107
|
+
t = HTree::Comment.new('xxx')
|
108
|
+
assert_equal('<!--xxx-->', gen(t))
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
class TestHTMLOutput < Test::Unit::TestCase
|
114
|
+
def test_top_xmlns
|
115
|
+
assert_equal("<html\n>aaa</html\n>", HTree("<html>aaa").display_html(""))
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_script
|
119
|
+
assert_equal("<html\n><script\n>a < b</script\n></html\n>",
|
120
|
+
HTree("<html><script>a < b").display_html(""))
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_script_invalid_content
|
124
|
+
assert_raise(ArgumentError) {
|
125
|
+
HTree("<html><script>a </ b").display_html("")
|
126
|
+
}
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_br
|
130
|
+
assert_equal("<html\n>a<br\n>b<br\n>c</html\n>",
|
131
|
+
HTree("<html>a<br>b<br>c").display_html(""))
|
132
|
+
end
|
133
|
+
end
|
data/test/test-parse.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'htree/parse'
|
3
|
+
require 'htree/equality'
|
4
|
+
require 'htree/traverse'
|
5
|
+
|
6
|
+
class TestParse < Test::Unit::TestCase
|
7
|
+
def test_empty
|
8
|
+
assert_equal(HTree::Doc.new([]), HTree.parse_xml("").eliminate_raw_string)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_xmlns_default
|
12
|
+
t1 = HTree::Doc.new([
|
13
|
+
HTree::Elem.new!(
|
14
|
+
HTree::STag.new('x1', [['xmlns', 'bb']],
|
15
|
+
HTree::DefaultContext.subst_namespaces({'xml'=>'http://www.w3.org/XML/1998/namespace'})),
|
16
|
+
[HTree::Elem.new!(HTree::STag.new('x2', [],
|
17
|
+
HTree::DefaultContext.subst_namespaces({nil => 'bb', 'xml'=>'http://www.w3.org/XML/1998/namespace'})), nil)])
|
18
|
+
])
|
19
|
+
t2 = HTree.parse_xml('<x1 xmlns="bb"><x2>')
|
20
|
+
assert_equal(t1, t2)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_doctype_root_element_name
|
24
|
+
assert_equal('html',
|
25
|
+
HTree.parse('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>').children[0].root_element_name)
|
26
|
+
|
27
|
+
# xxx: should be downcased?
|
28
|
+
assert_equal('HTML',
|
29
|
+
HTree.parse('<?xml version="1.0"?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>').children[1].root_element_name)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_doctype_system_identifier
|
33
|
+
assert_equal('http://www.w3.org/TR/html4/loose.dtd',
|
34
|
+
HTree.parse("<!DOCTYPE HTML SYSTEM 'http://www.w3.org/TR/html4/loose.dtd'>").children[0].system_identifier)
|
35
|
+
assert_equal('http://www.w3.org/TR/html4/loose.dtd',
|
36
|
+
HTree.parse("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>").children[0].system_identifier)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_procins
|
40
|
+
t = HTree.parse_xml("<?x?>").children[0]
|
41
|
+
assert_equal('x', t.target)
|
42
|
+
assert_equal(nil, t.content)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_eol_html
|
46
|
+
t1 = HTree::Elem.new('a', "\nb\n")
|
47
|
+
s = "<a>\nb\n</a>"
|
48
|
+
t2 = HTree.parse_xml(s).root
|
49
|
+
assert_equal(t1, t2)
|
50
|
+
assert_equal(s, t2.raw_string)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_parse_html
|
54
|
+
t1 = HTree.parse("<html>a</html>")
|
55
|
+
assert_equal("{http://www.w3.org/1999/xhtml}html", t1.root.element_name.universal_name)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_bare_url
|
59
|
+
t1 = HTree::Elem.new('a', {'href'=>'http://host/'})
|
60
|
+
s = "<a href=http://host/>"
|
61
|
+
t2 = HTree.parse(s).root
|
62
|
+
assert_equal(t1, t2)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_bare_slash
|
66
|
+
t1 = HTree::Elem.new('n', {'a'=>'v/'}, 'x')
|
67
|
+
s = "<n a=v/>x"
|
68
|
+
t2 = HTree.parse(s).root
|
69
|
+
assert_equal(t1, t2)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_bare_slash_empty
|
73
|
+
t1 = HTree::Elem.new('n', {'a'=>'v/'})
|
74
|
+
s = "<n a=v/>"
|
75
|
+
t2 = HTree.parse(s).root
|
76
|
+
assert_equal(t1, t2)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_downcase
|
80
|
+
assert_equal("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF",
|
81
|
+
HTree.parse('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>').root.name)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_downcase_name
|
85
|
+
# HTML && !XML
|
86
|
+
assert_equal('html', HTree.parse('<HTML>').root.element_name.local_name)
|
87
|
+
assert_equal('html', HTree.parse('<html>').root.element_name.local_name)
|
88
|
+
# HTML && XML
|
89
|
+
assert_equal('html', HTree.parse('<?xml version="1.0"?><html>').root.element_name.local_name)
|
90
|
+
assert_equal('v', HTree.parse('<?xml version="1.0"?><html X:Y=v xmlns:X=u>').root.get_attr('{u}Y'))
|
91
|
+
# !HTML && XML
|
92
|
+
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)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_script_etag
|
96
|
+
assert_equal(HTree::Doc.new(HTree::Elem.new('{http://www.w3.org/1999/xhtml}script', [])),
|
97
|
+
HTree.parse('<script></script>'))
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_html_emptyelem
|
101
|
+
t = HTree.parse('<html>')
|
102
|
+
assert_equal(HTree::Doc.new(HTree::Elem.new('{http://www.w3.org/1999/xhtml}html')), t)
|
103
|
+
assert(!t.children[0].empty_element?)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_hr_emptyelem
|
107
|
+
t = HTree.parse('<html><hr>')
|
108
|
+
assert_equal(
|
109
|
+
HTree::Doc.new(
|
110
|
+
HTree::Elem.new('{http://www.w3.org/1999/xhtml}html',
|
111
|
+
HTree::Elem.new('{http://www.w3.org/1999/xhtml}hr'))), t)
|
112
|
+
assert(t.children[0].children[0].empty_element?)
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|