tenderlove-nokogiri 0.0.0.20081001111445
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +105 -0
- data/README.txt +51 -0
- data/Rakefile +70 -0
- data/ext/nokogiri/extconf.rb +24 -0
- data/ext/nokogiri/html_document.c +85 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_sax_parser.c +32 -0
- data/ext/nokogiri/html_sax_parser.h +11 -0
- data/ext/nokogiri/native.c +35 -0
- data/ext/nokogiri/native.h +32 -0
- data/ext/nokogiri/xml_cdata.c +36 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_document.c +159 -0
- data/ext/nokogiri/xml_document.h +10 -0
- data/ext/nokogiri/xml_node.c +573 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +90 -0
- data/ext/nokogiri/xml_node_set.h +9 -0
- data/ext/nokogiri/xml_reader.c +420 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_sax_parser.c +161 -0
- data/ext/nokogiri/xml_sax_parser.h +10 -0
- data/ext/nokogiri/xml_text.c +25 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath.c +39 -0
- data/ext/nokogiri/xml_xpath.h +11 -0
- data/ext/nokogiri/xml_xpath_context.c +69 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +83 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/nokogiri.rb +45 -0
- data/lib/nokogiri/css.rb +6 -0
- data/lib/nokogiri/css/node.rb +95 -0
- data/lib/nokogiri/css/parser.rb +24 -0
- data/lib/nokogiri/css/parser.y +198 -0
- data/lib/nokogiri/css/tokenizer.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rex +63 -0
- data/lib/nokogiri/css/xpath_visitor.rb +153 -0
- data/lib/nokogiri/decorators.rb +1 -0
- data/lib/nokogiri/decorators/hpricot.rb +3 -0
- data/lib/nokogiri/decorators/hpricot/node.rb +47 -0
- data/lib/nokogiri/decorators/hpricot/node_set.rb +14 -0
- data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +13 -0
- data/lib/nokogiri/hpricot.rb +46 -0
- data/lib/nokogiri/html.rb +64 -0
- data/lib/nokogiri/html/builder.rb +9 -0
- data/lib/nokogiri/html/document.rb +9 -0
- data/lib/nokogiri/html/sax/parser.rb +21 -0
- data/lib/nokogiri/version.rb +3 -0
- data/lib/nokogiri/xml.rb +29 -0
- data/lib/nokogiri/xml/after_handler.rb +18 -0
- data/lib/nokogiri/xml/before_handler.rb +32 -0
- data/lib/nokogiri/xml/builder.rb +79 -0
- data/lib/nokogiri/xml/document.rb +22 -0
- data/lib/nokogiri/xml/node.rb +162 -0
- data/lib/nokogiri/xml/node_set.rb +136 -0
- data/lib/nokogiri/xml/reader.rb +14 -0
- data/lib/nokogiri/xml/sax.rb +9 -0
- data/lib/nokogiri/xml/sax/document.rb +59 -0
- data/lib/nokogiri/xml/sax/parser.rb +33 -0
- data/lib/nokogiri/xml/text.rb +6 -0
- data/lib/nokogiri/xml/xpath.rb +6 -0
- data/lib/nokogiri/xslt.rb +11 -0
- data/lib/nokogiri/xslt/stylesheet.rb +6 -0
- data/nokogiri.gemspec +33 -0
- data/test/css/test_nthiness.rb +141 -0
- data/test/css/test_parser.rb +214 -0
- data/test/css/test_tokenizer.rb +162 -0
- data/test/files/staff.xml +57 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/helper.rb +70 -0
- data/test/hpricot/files/basic.xhtml +17 -0
- data/test/hpricot/files/boingboing.html +2266 -0
- data/test/hpricot/files/cy0.html +3653 -0
- data/test/hpricot/files/immob.html +400 -0
- data/test/hpricot/files/pace_application.html +1320 -0
- data/test/hpricot/files/tenderlove.html +16 -0
- data/test/hpricot/files/uswebgen.html +220 -0
- data/test/hpricot/files/utf8.html +1054 -0
- data/test/hpricot/files/week9.html +1723 -0
- data/test/hpricot/files/why.xml +19 -0
- data/test/hpricot/load_files.rb +7 -0
- data/test/hpricot/test_alter.rb +67 -0
- data/test/hpricot/test_builder.rb +27 -0
- data/test/hpricot/test_parser.rb +412 -0
- data/test/hpricot/test_paths.rb +15 -0
- data/test/hpricot/test_preserved.rb +72 -0
- data/test/hpricot/test_xml.rb +26 -0
- data/test/html/sax/test_parser.rb +27 -0
- data/test/html/test_builder.rb +78 -0
- data/test/html/test_document.rb +22 -0
- data/test/test_convert_xpath.rb +173 -0
- data/test/test_nokogiri.rb +36 -0
- data/test/test_reader.rb +222 -0
- data/test/test_xslt_transforms.rb +29 -0
- data/test/xml/sax/test_parser.rb +93 -0
- data/test/xml/test_builder.rb +16 -0
- data/test/xml/test_document.rb +141 -0
- data/test/xml/test_node.rb +148 -0
- data/test/xml/test_node_set.rb +54 -0
- data/test/xml/test_text.rb +13 -0
- metadata +191 -0
data/test/test_reader.rb
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
3
|
+
|
4
|
+
class TestReader < Nokogiri::TestCase
|
5
|
+
def test_default?
|
6
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
7
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
8
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
9
|
+
</x>
|
10
|
+
eoxml
|
11
|
+
assert_equal false, reader.default?
|
12
|
+
assert_equal [false, false, false, false, false, false, false],
|
13
|
+
reader.map { |x| x.default? }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_value?
|
17
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
18
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
19
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
20
|
+
</x>
|
21
|
+
eoxml
|
22
|
+
assert_equal false, reader.value?
|
23
|
+
assert_equal [false, true, false, true, false, true, false],
|
24
|
+
reader.map { |x| x.value? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_attributes?
|
28
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
29
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
30
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
31
|
+
</x>
|
32
|
+
eoxml
|
33
|
+
assert_equal false, reader.attributes?
|
34
|
+
assert_equal [true, false, true, false, true, false, true],
|
35
|
+
reader.map { |x| x.attributes? }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_attributes
|
39
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
40
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'
|
41
|
+
xmlns='http://mothership.connection.com/'
|
42
|
+
>
|
43
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
44
|
+
</x>
|
45
|
+
eoxml
|
46
|
+
assert_equal({}, reader.attributes)
|
47
|
+
assert_equal [{'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
|
48
|
+
'xmlns'=>'http://mothership.connection.com/'},
|
49
|
+
{}, {"awesome"=>"true"}, {}, {"awesome"=>"true"}, {},
|
50
|
+
{'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
|
51
|
+
'xmlns'=>'http://mothership.connection.com/'}],
|
52
|
+
reader.map { |x| x.attributes }
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_attribute_roundtrip
|
56
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
57
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'
|
58
|
+
xmlns='http://mothership.connection.com/'
|
59
|
+
>
|
60
|
+
<tenderlove:foo awesome='true' size='giant'>snuggles!</tenderlove:foo>
|
61
|
+
</x>
|
62
|
+
eoxml
|
63
|
+
reader.each do |node|
|
64
|
+
node.attributes.each do |key, value|
|
65
|
+
assert_equal value, node.attribute(key)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_attribute_at
|
71
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
72
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
73
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
74
|
+
</x>
|
75
|
+
eoxml
|
76
|
+
assert_nil reader.attribute_at(nil)
|
77
|
+
assert_nil reader.attribute_at(0)
|
78
|
+
assert_equal ['http://tenderlovemaking.com/', nil, 'true', nil, 'true', nil, 'http://tenderlovemaking.com/'],
|
79
|
+
reader.map { |x| x.attribute_at(0) }
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_attribute
|
83
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
84
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
85
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
86
|
+
</x>
|
87
|
+
eoxml
|
88
|
+
assert_nil reader.attribute(nil)
|
89
|
+
assert_nil reader.attribute('awesome')
|
90
|
+
assert_equal [nil, nil, 'true', nil, 'true', nil, nil],
|
91
|
+
reader.map { |x| x.attribute('awesome') }
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_attribute_length
|
95
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
96
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
97
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
98
|
+
</x>
|
99
|
+
eoxml
|
100
|
+
assert_equal 0, reader.attribute_count
|
101
|
+
assert_equal [1, 0, 1, 0, 0, 0, 0], reader.map { |x| x.attribute_count }
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_depth
|
105
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
106
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
107
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
108
|
+
</x>
|
109
|
+
eoxml
|
110
|
+
assert_equal 0, reader.depth
|
111
|
+
assert_equal [0, 1, 1, 2, 1, 1, 0], reader.map { |x| x.depth }
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_encoding
|
115
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
116
|
+
<awesome>
|
117
|
+
<p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
|
118
|
+
<p xml:lang="ja">日本語が上手です</p>
|
119
|
+
</awesome>
|
120
|
+
eoxml
|
121
|
+
assert_nil reader.encoding
|
122
|
+
assert_equal [nil], reader.map { |x| x.encoding }.uniq
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_xml_version
|
126
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
127
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
128
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
129
|
+
</x>
|
130
|
+
eoxml
|
131
|
+
assert_nil reader.xml_version
|
132
|
+
assert_equal ['1.0'], reader.map { |x| x.xml_version }.uniq
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_lang
|
136
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
137
|
+
<awesome>
|
138
|
+
<p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
|
139
|
+
<p xml:lang="ja">日本語が上手です</p>
|
140
|
+
</awesome>
|
141
|
+
eoxml
|
142
|
+
assert_nil reader.lang
|
143
|
+
assert_equal [nil, nil, "en", "en", "en", nil, "ja", "ja", "ja", nil, nil],
|
144
|
+
reader.map { |x| x.lang }
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_value
|
148
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
149
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
150
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
151
|
+
</x>
|
152
|
+
eoxml
|
153
|
+
assert_nil reader.value
|
154
|
+
assert_equal [nil, "\n ", nil, "snuggles!", nil, "\n ", nil],
|
155
|
+
reader.map { |x| x.value }
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_prefix
|
159
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
160
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
161
|
+
<edi:foo>hello</edi:foo>
|
162
|
+
</x>
|
163
|
+
eoxml
|
164
|
+
assert_nil reader.prefix
|
165
|
+
assert_equal [nil, nil, "edi", nil, "edi", nil, nil],
|
166
|
+
reader.map { |n| n.prefix }
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_state
|
170
|
+
reader = Nokogiri::XML::Reader.from_memory('<foo>bar</bar>')
|
171
|
+
assert reader.state
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_ns_uri
|
175
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
176
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
177
|
+
<edi:foo>hello</edi:foo>
|
178
|
+
</x>
|
179
|
+
eoxml
|
180
|
+
assert_nil reader.namespace_uri
|
181
|
+
assert_equal([nil,
|
182
|
+
nil,
|
183
|
+
"http://ecommerce.example.org/schema",
|
184
|
+
nil,
|
185
|
+
"http://ecommerce.example.org/schema",
|
186
|
+
nil,
|
187
|
+
nil],
|
188
|
+
reader.map { |n| n.namespace_uri })
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_local_name
|
192
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
193
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
194
|
+
<edi:foo>hello</edi:foo>
|
195
|
+
</x>
|
196
|
+
eoxml
|
197
|
+
assert_nil reader.local_name
|
198
|
+
assert_equal(["x", "#text", "foo", "#text", "foo", "#text", "x"],
|
199
|
+
reader.map { |n| n.local_name })
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_name
|
203
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
204
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
205
|
+
<edi:foo>hello</edi:foo>
|
206
|
+
</x>
|
207
|
+
eoxml
|
208
|
+
assert_nil reader.name
|
209
|
+
assert_equal(["x", "#text", "edi:foo", "#text", "edi:foo", "#text", "x"],
|
210
|
+
reader.map { |n| n.name })
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_read_from_memory
|
214
|
+
called = false
|
215
|
+
reader = Nokogiri::XML::Reader.from_memory('<foo>bar</foo>')
|
216
|
+
reader.each do |node|
|
217
|
+
called = true
|
218
|
+
assert node
|
219
|
+
end
|
220
|
+
assert called
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestXsltTransforms < Nokogiri::TestCase
|
4
|
+
def test_transform
|
5
|
+
assert doc = Nokogiri::XML.parse(File.read(XML_FILE))
|
6
|
+
assert doc.xml?
|
7
|
+
|
8
|
+
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
|
9
|
+
|
10
|
+
assert result = style.apply_to(doc, ['title', '"Booyah"'])
|
11
|
+
assert_match %r{<h1>Booyah</h1>}, result
|
12
|
+
assert_match %r{<th.*Employee ID</th>}, result
|
13
|
+
assert_match %r{<th.*Name</th>}, result
|
14
|
+
assert_match %r{<th.*Position</th>}, result
|
15
|
+
assert_match %r{<th.*Salary</th>}, result
|
16
|
+
assert_match %r{<td>EMP0003</td>}, result
|
17
|
+
assert_match %r{<td>Margaret Martin</td>}, result
|
18
|
+
assert_match %r{<td>Computer Specialist</td>}, result
|
19
|
+
assert_match %r{<td>100,000</td>}, result
|
20
|
+
assert_no_match %r{Dallas|Texas}, result
|
21
|
+
assert_no_match %r{Female}, result
|
22
|
+
|
23
|
+
assert result = style.apply_to(doc, ['title', '"Grandma"'])
|
24
|
+
assert_match %r{<h1>Grandma</h1>}, result
|
25
|
+
|
26
|
+
assert result = style.apply_to(doc)
|
27
|
+
assert_match %r{<h1></h1>}, result
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', "helper"))
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
module SAX
|
6
|
+
class TestParser < Nokogiri::SAX::TestCase
|
7
|
+
def setup
|
8
|
+
@parser = XML::SAX::Parser.new(Doc.new)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_parse
|
12
|
+
File.open(XML_FILE, 'rb') { |f|
|
13
|
+
@parser.parse(f)
|
14
|
+
}
|
15
|
+
@parser.parse(File.read(XML_FILE))
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_parse_io
|
19
|
+
File.open(XML_FILE, 'rb') { |f|
|
20
|
+
@parser.parse_io(f)
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_parse_file
|
25
|
+
@parser.parse_file(XML_FILE)
|
26
|
+
assert_raises(Errno::ENOENT) {
|
27
|
+
@parser.parse_file('')
|
28
|
+
}
|
29
|
+
assert_raises(Errno::EISDIR) {
|
30
|
+
@parser.parse_file(File.expand_path(File.dirname(__FILE__)))
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_ctag
|
35
|
+
@parser.parse_memory(<<-eoxml)
|
36
|
+
<p id="asdfasdf">
|
37
|
+
<![CDATA[ This is a comment ]]>
|
38
|
+
Paragraph 1
|
39
|
+
</p>
|
40
|
+
eoxml
|
41
|
+
assert_equal [' This is a comment '], @parser.document.cdata_blocks
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_comment
|
45
|
+
@parser.parse_memory(<<-eoxml)
|
46
|
+
<p id="asdfasdf">
|
47
|
+
<!-- This is a comment -->
|
48
|
+
Paragraph 1
|
49
|
+
</p>
|
50
|
+
eoxml
|
51
|
+
assert_equal [' This is a comment '], @parser.document.comments
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_characters
|
55
|
+
@parser.parse_memory(<<-eoxml)
|
56
|
+
<p id="asdfasdf">Paragraph 1</p>
|
57
|
+
eoxml
|
58
|
+
assert_equal ['Paragraph 1'], @parser.document.data
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_end_document
|
62
|
+
@parser.parse_memory(<<-eoxml)
|
63
|
+
<p id="asdfasdf">Paragraph 1</p>
|
64
|
+
eoxml
|
65
|
+
assert @parser.document.end_document_called
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_end_element
|
69
|
+
@parser.parse_memory(<<-eoxml)
|
70
|
+
<p id="asdfasdf">Paragraph 1</p>
|
71
|
+
eoxml
|
72
|
+
assert_equal [["p"]],
|
73
|
+
@parser.document.end_elements
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_start_element_attrs
|
77
|
+
@parser.parse_memory(<<-eoxml)
|
78
|
+
<p id="asdfasdf">Paragraph 1</p>
|
79
|
+
eoxml
|
80
|
+
assert_equal [["p", ["id", "asdfasdf"]]],
|
81
|
+
@parser.document.start_elements
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_parse_document
|
85
|
+
@parser.parse_memory(<<-eoxml)
|
86
|
+
<p>Paragraph 1</p>
|
87
|
+
<p>Paragraph 2</p>
|
88
|
+
eoxml
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestBuilder < Nokogiri::TestCase
|
6
|
+
def test_cdata
|
7
|
+
builder = Nokogiri::XML::Builder.new do
|
8
|
+
root {
|
9
|
+
cdata "hello world"
|
10
|
+
}
|
11
|
+
end
|
12
|
+
assert_equal("<?xml version=\"1.0\"?><root><![CDATA[hello world]]></root>", builder.to_xml.gsub(/\n/, ''))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestDocument < Nokogiri::TestCase
|
6
|
+
def setup
|
7
|
+
@xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_find_with_namespace
|
11
|
+
doc = Nokogiri::XML.parse(<<-eoxml)
|
12
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
13
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
14
|
+
</x>
|
15
|
+
eoxml
|
16
|
+
|
17
|
+
ctx = Nokogiri::XML::XPathContext.new(doc)
|
18
|
+
ctx.register_ns 'tenderlove', 'http://tenderlovemaking.com/'
|
19
|
+
set = ctx.evaluate('//tenderlove:foo').node_set
|
20
|
+
assert_equal 1, set.length
|
21
|
+
assert_equal 'foo', set.first.name
|
22
|
+
|
23
|
+
# It looks like only the URI is important:
|
24
|
+
ctx = Nokogiri::XML::XPathContext.new(doc)
|
25
|
+
ctx.register_ns 'america', 'http://tenderlovemaking.com/'
|
26
|
+
set = ctx.evaluate('//america:foo').node_set
|
27
|
+
assert_equal 1, set.length
|
28
|
+
assert_equal 'foo', set.first.name
|
29
|
+
|
30
|
+
# Its so important that a missing slash will cause it to return nothing
|
31
|
+
ctx = Nokogiri::XML::XPathContext.new(doc)
|
32
|
+
ctx.register_ns 'america', 'http://tenderlovemaking.com'
|
33
|
+
set = ctx.evaluate('//america:foo').node_set
|
34
|
+
assert_equal 0, set.length
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_xml?
|
38
|
+
assert @xml.xml?
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_document
|
42
|
+
assert @xml.document
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_multiple_search
|
46
|
+
assert node_set = @xml.search('//employee', '//name')
|
47
|
+
employees = @xml.search('//employee')
|
48
|
+
names = @xml.search('//name')
|
49
|
+
assert_equal(employees.length + names.length, node_set.length)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_node_set_index
|
53
|
+
assert node_set = @xml.search('//employee')
|
54
|
+
|
55
|
+
assert_equal(5, node_set.length)
|
56
|
+
assert node_set[4]
|
57
|
+
assert_nil node_set[5]
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_search
|
61
|
+
assert node_set = @xml.search('//employee')
|
62
|
+
|
63
|
+
assert_equal(5, node_set.length)
|
64
|
+
|
65
|
+
node_set.each do |node|
|
66
|
+
assert_equal('employee', node.name)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_dump
|
71
|
+
assert @xml.serialize
|
72
|
+
assert @xml.to_xml
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_subset_is_decorated
|
76
|
+
x = Module.new do
|
77
|
+
def awesome!
|
78
|
+
end
|
79
|
+
end
|
80
|
+
util_decorate(@xml, x)
|
81
|
+
|
82
|
+
assert @xml.respond_to?(:awesome!)
|
83
|
+
assert node_set = @xml.search('//staff')
|
84
|
+
assert node_set.respond_to?(:awesome!)
|
85
|
+
assert subset = node_set.search('.//employee')
|
86
|
+
assert subset.respond_to?(:awesome!)
|
87
|
+
assert sub_subset = node_set.search('.//name')
|
88
|
+
assert sub_subset.respond_to?(:awesome!)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_decorator_is_applied
|
92
|
+
x = Module.new do
|
93
|
+
def awesome!
|
94
|
+
end
|
95
|
+
end
|
96
|
+
util_decorate(@xml, x)
|
97
|
+
|
98
|
+
assert @xml.respond_to?(:awesome!)
|
99
|
+
assert node_set = @xml.search('//employee')
|
100
|
+
assert node_set.respond_to?(:awesome!)
|
101
|
+
node_set.each do |node|
|
102
|
+
assert node.respond_to?(:awesome!)
|
103
|
+
end
|
104
|
+
assert @xml.root.respond_to?(:awesome!)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_new
|
108
|
+
doc = nil
|
109
|
+
assert_nothing_raised {
|
110
|
+
doc = Nokogiri::XML::Document.new
|
111
|
+
}
|
112
|
+
assert doc
|
113
|
+
assert doc.xml?
|
114
|
+
assert_nil doc.root
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_set_root
|
118
|
+
doc = nil
|
119
|
+
assert_nothing_raised {
|
120
|
+
doc = Nokogiri::XML::Document.new
|
121
|
+
}
|
122
|
+
assert doc
|
123
|
+
assert doc.xml?
|
124
|
+
assert_nil doc.root
|
125
|
+
node = Nokogiri::XML::Node.new("b") { |n|
|
126
|
+
n.content = 'hello world'
|
127
|
+
}
|
128
|
+
assert_equal('hello world', node.content)
|
129
|
+
doc.root = node
|
130
|
+
assert_equal(node, doc.root)
|
131
|
+
end
|
132
|
+
|
133
|
+
def util_decorate(document, x)
|
134
|
+
document.decorators['document'] << x
|
135
|
+
document.decorators['node'] << x
|
136
|
+
document.decorators['nodeset'] << x
|
137
|
+
document.decorate!
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|