nokogiri 1.9.0.rc1 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/nokogiri/css/parser.rb +61 -61
- data/lib/nokogiri/version.rb +1 -1
- metadata +4 -146
- data/.autotest +0 -22
- data/.cross_rubies +0 -8
- data/.editorconfig +0 -17
- data/.gemtest +0 -0
- data/.travis.yml +0 -63
- data/CHANGELOG.md +0 -1402
- data/CONTRIBUTING.md +0 -42
- data/C_CODING_STYLE.rdoc +0 -33
- data/Gemfile +0 -23
- data/Gemfile-libxml-ruby +0 -3
- data/Manifest.txt +0 -371
- data/ROADMAP.md +0 -111
- data/Rakefile +0 -358
- data/SECURITY.md +0 -19
- data/STANDARD_RESPONSES.md +0 -47
- data/Y_U_NO_GEMSPEC.md +0 -155
- data/appveyor.yml +0 -29
- data/build_all +0 -48
- data/patches/sort-patches-by-date +0 -25
- data/suppressions/README.txt +0 -1
- data/suppressions/nokogiri_ruby-2.supp +0 -10
- data/tasks/test.rb +0 -100
- data/test/css/test_nthiness.rb +0 -226
- data/test/css/test_parser.rb +0 -386
- data/test/css/test_tokenizer.rb +0 -215
- data/test/css/test_xpath_visitor.rb +0 -96
- data/test/decorators/test_slop.rb +0 -23
- data/test/files/2ch.html +0 -108
- data/test/files/GH_1042.html +0 -18
- data/test/files/address_book.rlx +0 -12
- data/test/files/address_book.xml +0 -10
- data/test/files/atom.xml +0 -344
- data/test/files/bar/bar.xsd +0 -4
- data/test/files/bogus.xml +0 -0
- data/test/files/dont_hurt_em_why.xml +0 -422
- data/test/files/encoding.html +0 -82
- data/test/files/encoding.xhtml +0 -84
- data/test/files/exslt.xml +0 -8
- data/test/files/exslt.xslt +0 -35
- data/test/files/foo/foo.xsd +0 -4
- data/test/files/metacharset.html +0 -10
- data/test/files/namespace_pressure_test.xml +0 -1684
- data/test/files/noencoding.html +0 -47
- data/test/files/po.xml +0 -32
- data/test/files/po.xsd +0 -66
- data/test/files/saml/saml20assertion_schema.xsd +0 -283
- data/test/files/saml/saml20protocol_schema.xsd +0 -302
- data/test/files/saml/xenc_schema.xsd +0 -146
- data/test/files/saml/xmldsig_schema.xsd +0 -318
- data/test/files/shift_jis.html +0 -10
- data/test/files/shift_jis.xml +0 -5
- data/test/files/shift_jis_no_charset.html +0 -9
- data/test/files/slow-xpath.xml +0 -25509
- data/test/files/snuggles.xml +0 -3
- data/test/files/staff.dtd +0 -10
- data/test/files/staff.xml +0 -59
- data/test/files/staff.xslt +0 -32
- data/test/files/test_document_url/bar.xml +0 -2
- data/test/files/test_document_url/document.dtd +0 -4
- data/test/files/test_document_url/document.xml +0 -6
- data/test/files/tlm.html +0 -851
- data/test/files/to_be_xincluded.xml +0 -2
- data/test/files/valid_bar.xml +0 -2
- data/test/files/xinclude.xml +0 -4
- data/test/helper.rb +0 -271
- data/test/html/sax/test_parser.rb +0 -168
- data/test/html/sax/test_parser_context.rb +0 -46
- data/test/html/sax/test_parser_text.rb +0 -163
- data/test/html/sax/test_push_parser.rb +0 -87
- data/test/html/test_attributes.rb +0 -85
- data/test/html/test_builder.rb +0 -164
- data/test/html/test_document.rb +0 -722
- data/test/html/test_document_encoding.rb +0 -143
- data/test/html/test_document_fragment.rb +0 -310
- data/test/html/test_element_description.rb +0 -105
- data/test/html/test_named_characters.rb +0 -14
- data/test/html/test_node.rb +0 -212
- data/test/html/test_node_encoding.rb +0 -91
- data/test/namespaces/test_additional_namespaces_in_builder_doc.rb +0 -14
- data/test/namespaces/test_namespaces_aliased_default.rb +0 -24
- data/test/namespaces/test_namespaces_in_builder_doc.rb +0 -75
- data/test/namespaces/test_namespaces_in_cloned_doc.rb +0 -31
- data/test/namespaces/test_namespaces_in_created_doc.rb +0 -75
- data/test/namespaces/test_namespaces_in_parsed_doc.rb +0 -80
- data/test/namespaces/test_namespaces_preservation.rb +0 -31
- data/test/test_convert_xpath.rb +0 -135
- data/test/test_css_cache.rb +0 -47
- data/test/test_encoding_handler.rb +0 -48
- data/test/test_memory_leak.rb +0 -226
- data/test/test_nokogiri.rb +0 -138
- data/test/test_soap4r_sax.rb +0 -52
- data/test/test_xslt_transforms.rb +0 -314
- data/test/xml/node/test_save_options.rb +0 -28
- data/test/xml/node/test_subclass.rb +0 -44
- data/test/xml/sax/test_parser.rb +0 -402
- data/test/xml/sax/test_parser_context.rb +0 -115
- data/test/xml/sax/test_parser_text.rb +0 -202
- data/test/xml/sax/test_push_parser.rb +0 -265
- data/test/xml/test_attr.rb +0 -96
- data/test/xml/test_attribute_decl.rb +0 -86
- data/test/xml/test_builder.rb +0 -353
- data/test/xml/test_c14n.rb +0 -180
- data/test/xml/test_cdata.rb +0 -54
- data/test/xml/test_comment.rb +0 -40
- data/test/xml/test_document.rb +0 -982
- data/test/xml/test_document_encoding.rb +0 -31
- data/test/xml/test_document_fragment.rb +0 -312
- data/test/xml/test_dtd.rb +0 -187
- data/test/xml/test_dtd_encoding.rb +0 -31
- data/test/xml/test_element_content.rb +0 -56
- data/test/xml/test_element_decl.rb +0 -73
- data/test/xml/test_entity_decl.rb +0 -122
- data/test/xml/test_entity_reference.rb +0 -262
- data/test/xml/test_namespace.rb +0 -96
- data/test/xml/test_node.rb +0 -1379
- data/test/xml/test_node_attributes.rb +0 -115
- data/test/xml/test_node_encoding.rb +0 -75
- data/test/xml/test_node_inheritance.rb +0 -32
- data/test/xml/test_node_reparenting.rb +0 -661
- data/test/xml/test_node_set.rb +0 -828
- data/test/xml/test_parse_options.rb +0 -64
- data/test/xml/test_processing_instruction.rb +0 -30
- data/test/xml/test_reader.rb +0 -620
- data/test/xml/test_reader_encoding.rb +0 -134
- data/test/xml/test_relax_ng.rb +0 -60
- data/test/xml/test_schema.rb +0 -142
- data/test/xml/test_syntax_error.rb +0 -36
- data/test/xml/test_text.rb +0 -69
- data/test/xml/test_unparented_node.rb +0 -483
- data/test/xml/test_xinclude.rb +0 -83
- data/test/xml/test_xpath.rb +0 -470
- data/test/xslt/test_custom_functions.rb +0 -133
- data/test/xslt/test_exception_handling.rb +0 -37
data/test/xml/test_node_set.rb
DELETED
@@ -1,828 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
module Nokogiri
|
4
|
-
module XML
|
5
|
-
class TestNodeSet < Nokogiri::TestCase
|
6
|
-
class TestNodeSetNamespaces < Nokogiri::TestCase
|
7
|
-
def setup
|
8
|
-
super
|
9
|
-
@xml = Nokogiri.XML('<foo xmlns:n0="http://example.com" />')
|
10
|
-
@list = @xml.xpath('//namespace::*')
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_include?
|
14
|
-
assert @list.include?(@list.first), 'list should have item'
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_push
|
18
|
-
@list.push @list.first
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_delete
|
22
|
-
@list.push @list.first
|
23
|
-
@list.delete @list.first
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_reference_after_delete
|
27
|
-
first = @list.first
|
28
|
-
@list.delete(first)
|
29
|
-
assert_equal 'http://www.w3.org/XML/1998/namespace', first.href
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def setup
|
34
|
-
super
|
35
|
-
@xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
|
36
|
-
@list = @xml.css('employee')
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_break_works
|
40
|
-
assert_equal 7, @xml.root.elements.each { |x| break 7 }
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_filter
|
44
|
-
list = @xml.css('address').filter('*[domestic="Yes"]')
|
45
|
-
assert_equal(%w{ Yes } * 4, list.map { |n| n['domestic'] })
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_remove_attr
|
49
|
-
@list.each { |x| x['class'] = 'blah' }
|
50
|
-
assert_equal @list, @list.remove_attr('class')
|
51
|
-
@list.each { |x| assert_nil x['class'] }
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_remove_attribute
|
55
|
-
@list.each { |x| x['class'] = 'blah' }
|
56
|
-
assert_equal @list, @list.remove_attribute('class')
|
57
|
-
@list.each { |x| assert_nil x['class'] }
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_add_class
|
61
|
-
assert_equal @list, @list.add_class('bar')
|
62
|
-
@list.each { |x| assert_equal 'bar', x['class'] }
|
63
|
-
|
64
|
-
@list.add_class('bar')
|
65
|
-
@list.each { |x| assert_equal 'bar', x['class'] }
|
66
|
-
|
67
|
-
@list.add_class('baz')
|
68
|
-
@list.each { |x| assert_equal 'bar baz', x['class'] }
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_append_class
|
72
|
-
assert_equal @list, @list.append_class('bar')
|
73
|
-
@list.each { |x| assert_equal 'bar', x['class'] }
|
74
|
-
|
75
|
-
@list.append_class('bar')
|
76
|
-
@list.each { |x| assert_equal 'bar bar', x['class'] }
|
77
|
-
|
78
|
-
@list.append_class('baz')
|
79
|
-
@list.each { |x| assert_equal 'bar bar baz', x['class'] }
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_remove_class_with_no_class
|
83
|
-
assert_equal @list, @list.remove_class('bar')
|
84
|
-
@list.each { |e| assert_nil e['class'] }
|
85
|
-
|
86
|
-
@list.each { |e| e['class'] = '' }
|
87
|
-
assert_equal @list, @list.remove_class('bar')
|
88
|
-
@list.each { |e| assert_nil e['class'] }
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_remove_class_single
|
92
|
-
@list.each { |e| e['class'] = 'foo bar' }
|
93
|
-
|
94
|
-
assert_equal @list, @list.remove_class('bar')
|
95
|
-
@list.each { |e| assert_equal 'foo', e['class'] }
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_remove_class_completely
|
99
|
-
@list.each { |e| e['class'] = 'foo' }
|
100
|
-
|
101
|
-
assert_equal @list, @list.remove_class
|
102
|
-
@list.each { |e| assert_nil e['class'] }
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_attribute_set
|
106
|
-
@list.each { |e| assert_nil e['foo'] }
|
107
|
-
|
108
|
-
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
109
|
-
@list.send(t.first.to_sym, 'foo', t.last)
|
110
|
-
@list.each { |e| assert_equal t.last, e['foo'] }
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def test_attribute_set_with_block
|
115
|
-
@list.each { |e| assert_nil e['foo'] }
|
116
|
-
|
117
|
-
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
118
|
-
@list.send(t.first.to_sym, 'foo') { |x| t.last }
|
119
|
-
@list.each { |e| assert_equal t.last, e['foo'] }
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_attribute_set_with_hash
|
124
|
-
@list.each { |e| assert_nil e['foo'] }
|
125
|
-
|
126
|
-
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
127
|
-
@list.send(t.first.to_sym, 'foo' => t.last)
|
128
|
-
@list.each { |e| assert_equal t.last, e['foo'] }
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_attribute_no_args
|
133
|
-
@list.first['foo'] = 'bar'
|
134
|
-
assert_equal @list.first.attribute('foo'), @list.attribute('foo')
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_search_empty_node_set
|
138
|
-
set = Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
|
139
|
-
assert_equal 0, set.css('foo').length
|
140
|
-
assert_equal 0, set.xpath('.//foo').length
|
141
|
-
assert_equal 0, set.search('foo').length
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_node_set_search_with_multiple_queries
|
145
|
-
xml = '<document>
|
146
|
-
<thing>
|
147
|
-
<div class="title">important thing</div>
|
148
|
-
</thing>
|
149
|
-
<thing>
|
150
|
-
<div class="content">stuff</div>
|
151
|
-
</thing>
|
152
|
-
<thing>
|
153
|
-
<p class="blah">more stuff</div>
|
154
|
-
</thing>
|
155
|
-
</document>'
|
156
|
-
set = Nokogiri::XML(xml).xpath(".//thing")
|
157
|
-
assert_kind_of Nokogiri::XML::NodeSet, set
|
158
|
-
|
159
|
-
assert_equal 3, set.xpath('./div', './p').length
|
160
|
-
assert_equal 3, set.css('.title', '.content', 'p').length
|
161
|
-
assert_equal 3, set.search('./div', 'p.blah').length
|
162
|
-
end
|
163
|
-
|
164
|
-
def test_search_with_custom_selector
|
165
|
-
set = @xml.xpath('//staff')
|
166
|
-
|
167
|
-
[
|
168
|
-
[:xpath, '//*[awesome(.)]'],
|
169
|
-
[:search, '//*[awesome(.)]'],
|
170
|
-
[:css, '*:awesome'],
|
171
|
-
[:search, '*:awesome']
|
172
|
-
].each do |method, query|
|
173
|
-
custom_employees = set.send(method, query, Class.new {
|
174
|
-
def awesome ns
|
175
|
-
ns.select { |n| n.name == 'employee' }
|
176
|
-
end
|
177
|
-
}.new)
|
178
|
-
|
179
|
-
assert_equal(@xml.xpath('//employee'), custom_employees,
|
180
|
-
"using #{method} with custom selector '#{query}'")
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_search_with_variable_bindings
|
185
|
-
set = @xml.xpath('//staff')
|
186
|
-
|
187
|
-
assert_equal(4, set.xpath('//address[@domestic=$value]', nil, :value => 'Yes').length,
|
188
|
-
"using #xpath with variable binding")
|
189
|
-
|
190
|
-
assert_equal(4, set.search('//address[@domestic=$value]', nil, :value => 'Yes').length,
|
191
|
-
"using #search with variable binding")
|
192
|
-
end
|
193
|
-
|
194
|
-
def test_search_self
|
195
|
-
set = @xml.xpath('//staff')
|
196
|
-
assert_equal set.to_a, set.search('.').to_a
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_css_searches_match_self
|
200
|
-
html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
|
201
|
-
set = html.xpath("/html/body/div")
|
202
|
-
assert_equal set.first, set.css(".a").first
|
203
|
-
assert_equal set.first, set.search(".a").first
|
204
|
-
end
|
205
|
-
|
206
|
-
def test_css_search_with_namespace
|
207
|
-
fragment = Nokogiri::XML.fragment(<<-eoxml)
|
208
|
-
<html xmlns="http://www.w3.org/1999/xhtml">
|
209
|
-
<head></head>
|
210
|
-
<body></body>
|
211
|
-
</html>
|
212
|
-
eoxml
|
213
|
-
assert fragment.children.search( 'body', { 'xmlns' => 'http://www.w3.org/1999/xhtml' })
|
214
|
-
end
|
215
|
-
|
216
|
-
def test_double_equal
|
217
|
-
assert node_set_one = @xml.xpath('//employee')
|
218
|
-
assert node_set_two = @xml.xpath('//employee')
|
219
|
-
|
220
|
-
assert_not_equal node_set_one.object_id, node_set_two.object_id
|
221
|
-
|
222
|
-
assert_equal node_set_one, node_set_two
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_node_set_not_equal_to_string
|
226
|
-
node_set_one = @xml.xpath('//employee')
|
227
|
-
assert_not_equal node_set_one, "asdfadsf"
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_out_of_order_not_equal
|
231
|
-
one = @xml.xpath('//employee')
|
232
|
-
two = @xml.xpath('//employee')
|
233
|
-
two.push two.shift
|
234
|
-
assert_not_equal one, two
|
235
|
-
end
|
236
|
-
|
237
|
-
def test_shorter_is_not_equal
|
238
|
-
node_set_one = @xml.xpath('//employee')
|
239
|
-
node_set_two = @xml.xpath('//employee')
|
240
|
-
node_set_two.delete(node_set_two.first)
|
241
|
-
|
242
|
-
assert_not_equal node_set_one, node_set_two
|
243
|
-
end
|
244
|
-
|
245
|
-
def test_pop
|
246
|
-
set = @xml.xpath('//employee')
|
247
|
-
last = set.last
|
248
|
-
assert_equal last, set.pop
|
249
|
-
end
|
250
|
-
|
251
|
-
def test_shift
|
252
|
-
set = @xml.xpath('//employee')
|
253
|
-
first = set.first
|
254
|
-
assert_equal first, set.shift
|
255
|
-
end
|
256
|
-
|
257
|
-
def test_shift_empty
|
258
|
-
set = Nokogiri::XML::NodeSet.new(@xml)
|
259
|
-
assert_nil set.shift
|
260
|
-
end
|
261
|
-
|
262
|
-
def test_pop_empty
|
263
|
-
set = Nokogiri::XML::NodeSet.new(@xml)
|
264
|
-
assert_nil set.pop
|
265
|
-
end
|
266
|
-
|
267
|
-
def test_first_takes_arguments
|
268
|
-
assert node_set = @xml.xpath('//employee')
|
269
|
-
assert_equal 2, node_set.first(2).length
|
270
|
-
end
|
271
|
-
|
272
|
-
def test_first_clamps_arguments
|
273
|
-
assert node_set = @xml.xpath('//employee[position() < 3]')
|
274
|
-
assert_equal 2, node_set.first(5).length
|
275
|
-
end
|
276
|
-
|
277
|
-
[:dup, :clone].each do |method_name|
|
278
|
-
define_method "test_#{method_name}" do
|
279
|
-
assert node_set = @xml.xpath('//employee')
|
280
|
-
duplicate = node_set.send(method_name)
|
281
|
-
assert_equal node_set.length, duplicate.length
|
282
|
-
node_set.zip(duplicate).each do |a,b|
|
283
|
-
assert_equal a, b
|
284
|
-
end
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
def test_dup_on_empty_set
|
289
|
-
empty_set = Nokogiri::XML::NodeSet.new @xml, []
|
290
|
-
assert_equal 0, empty_set.dup.length # this shouldn't raise null pointer exception
|
291
|
-
end
|
292
|
-
|
293
|
-
def test_xmlns_is_automatically_registered
|
294
|
-
doc = Nokogiri::XML(<<-eoxml)
|
295
|
-
<root xmlns="http://tenderlovemaking.com/">
|
296
|
-
<foo>
|
297
|
-
<bar/>
|
298
|
-
</foo>
|
299
|
-
</root>
|
300
|
-
eoxml
|
301
|
-
set = doc.css('foo')
|
302
|
-
assert_equal 1, set.css('xmlns|bar').length
|
303
|
-
assert_equal 0, set.css('|bar').length
|
304
|
-
assert_equal 1, set.xpath('//xmlns:bar').length
|
305
|
-
assert_equal 1, set.search('xmlns|bar').length
|
306
|
-
assert_equal 1, set.search('//xmlns:bar').length
|
307
|
-
assert set.at('//xmlns:bar')
|
308
|
-
assert set.at('xmlns|bar')
|
309
|
-
assert set.at('bar')
|
310
|
-
end
|
311
|
-
|
312
|
-
def test_children_has_document
|
313
|
-
set = @xml.root.children
|
314
|
-
assert_instance_of(NodeSet, set)
|
315
|
-
assert_equal @xml, set.document
|
316
|
-
end
|
317
|
-
|
318
|
-
def test_length_size
|
319
|
-
assert node_set = @xml.search('//employee')
|
320
|
-
assert_equal node_set.length, node_set.size
|
321
|
-
end
|
322
|
-
|
323
|
-
def test_to_xml
|
324
|
-
assert node_set = @xml.search('//employee')
|
325
|
-
assert node_set.to_xml
|
326
|
-
end
|
327
|
-
|
328
|
-
def test_inner_html
|
329
|
-
doc = Nokogiri::HTML(<<-eohtml)
|
330
|
-
<html>
|
331
|
-
<body>
|
332
|
-
<div>
|
333
|
-
<a>one</a>
|
334
|
-
</div>
|
335
|
-
<div>
|
336
|
-
<a>two</a>
|
337
|
-
</div>
|
338
|
-
</body>
|
339
|
-
</html>
|
340
|
-
eohtml
|
341
|
-
assert html = doc.css('div').inner_html
|
342
|
-
assert_match '<a>', html
|
343
|
-
end
|
344
|
-
|
345
|
-
def test_gt_string_arg
|
346
|
-
assert node_set = @xml.search('//employee')
|
347
|
-
assert_equal node_set.xpath('./employeeId'), (node_set > 'employeeId')
|
348
|
-
end
|
349
|
-
|
350
|
-
def test_at_performs_a_search_with_css
|
351
|
-
assert node_set = @xml.search('//employee')
|
352
|
-
assert_equal node_set.first.first_element_child, node_set.at('employeeId')
|
353
|
-
assert_equal node_set.first.first_element_child, node_set.%('employeeId')
|
354
|
-
end
|
355
|
-
|
356
|
-
def test_at_performs_a_search_with_xpath
|
357
|
-
assert node_set = @xml.search('//employee')
|
358
|
-
assert_equal node_set.first.first_element_child, node_set.at('./employeeId')
|
359
|
-
assert_equal node_set.first.first_element_child, node_set.%('./employeeId')
|
360
|
-
end
|
361
|
-
|
362
|
-
def test_at_with_integer_index
|
363
|
-
assert node_set = @xml.search('//employee')
|
364
|
-
assert_equal node_set.first, node_set.at(0)
|
365
|
-
assert_equal node_set.first, node_set % 0
|
366
|
-
end
|
367
|
-
|
368
|
-
def test_at_xpath
|
369
|
-
assert node_set = @xml.search('//employee')
|
370
|
-
assert_equal node_set.first.first_element_child, node_set.at_xpath('./employeeId')
|
371
|
-
end
|
372
|
-
|
373
|
-
def test_at_css
|
374
|
-
assert node_set = @xml.search('//employee')
|
375
|
-
assert_equal node_set.first.first_element_child, node_set.at_css('employeeId')
|
376
|
-
end
|
377
|
-
|
378
|
-
def test_to_ary
|
379
|
-
assert node_set = @xml.search('//employee')
|
380
|
-
foo = []
|
381
|
-
foo += node_set
|
382
|
-
assert_equal node_set.length, foo.length
|
383
|
-
end
|
384
|
-
|
385
|
-
def test_push
|
386
|
-
node = Nokogiri::XML::Node.new('foo', @xml)
|
387
|
-
node.content = 'bar'
|
388
|
-
|
389
|
-
assert node_set = @xml.search('//employee')
|
390
|
-
node_set.push(node)
|
391
|
-
|
392
|
-
assert node_set.include?(node)
|
393
|
-
end
|
394
|
-
|
395
|
-
def test_delete_with_invalid_argument
|
396
|
-
employees = @xml.search("//employee")
|
397
|
-
positions = @xml.search("//position")
|
398
|
-
|
399
|
-
assert_raises(ArgumentError) { employees.delete(positions) }
|
400
|
-
end
|
401
|
-
|
402
|
-
def test_delete_when_present
|
403
|
-
employees = @xml.search("//employee")
|
404
|
-
wally = employees.first
|
405
|
-
assert employees.include?(wally) # testing setup
|
406
|
-
length = employees.length
|
407
|
-
|
408
|
-
result = employees.delete(wally)
|
409
|
-
assert_equal result, wally
|
410
|
-
assert ! employees.include?(wally)
|
411
|
-
assert length-1, employees.length
|
412
|
-
end
|
413
|
-
|
414
|
-
def test_delete_when_not_present
|
415
|
-
employees = @xml.search("//employee")
|
416
|
-
phb = @xml.search("//position").first
|
417
|
-
assert ! employees.include?(phb) # testing setup
|
418
|
-
length = employees.length
|
419
|
-
|
420
|
-
result = employees.delete(phb)
|
421
|
-
assert_nil result
|
422
|
-
assert length, employees.length
|
423
|
-
end
|
424
|
-
|
425
|
-
def test_delete_on_empty_set
|
426
|
-
empty_set = Nokogiri::XML::NodeSet.new @xml, []
|
427
|
-
employee = @xml.at_xpath("//employee")
|
428
|
-
assert_nil empty_set.delete(employee)
|
429
|
-
end
|
430
|
-
|
431
|
-
def test_unlink
|
432
|
-
xml = Nokogiri::XML.parse(<<-eoxml)
|
433
|
-
<root>
|
434
|
-
<a class='foo bar'>Bar</a>
|
435
|
-
<a class='bar foo'>Bar</a>
|
436
|
-
<a class='bar'>Bar</a>
|
437
|
-
<a>Hello world</a>
|
438
|
-
<a class='baz bar foo'>Bar</a>
|
439
|
-
<a class='bazbarfoo'>Awesome</a>
|
440
|
-
<a class='bazbar'>Awesome</a>
|
441
|
-
</root>
|
442
|
-
eoxml
|
443
|
-
set = xml.xpath('//a')
|
444
|
-
set.unlink
|
445
|
-
set.each do |node|
|
446
|
-
assert !node.parent
|
447
|
-
#assert !node.document
|
448
|
-
assert !node.previous_sibling
|
449
|
-
assert !node.next_sibling
|
450
|
-
end
|
451
|
-
assert_no_match(/Hello world/, xml.to_s)
|
452
|
-
end
|
453
|
-
|
454
|
-
def test_nodeset_search_takes_namespace
|
455
|
-
@xml = Nokogiri::XML.parse(<<-eoxml)
|
456
|
-
<root>
|
457
|
-
<car xmlns:part="http://general-motors.com/">
|
458
|
-
<part:tire>Michelin Model XGV</part:tire>
|
459
|
-
</car>
|
460
|
-
<bicycle xmlns:part="http://schwinn.com/">
|
461
|
-
<part:tire>I'm a bicycle tire!</part:tire>
|
462
|
-
</bicycle>
|
463
|
-
</root>
|
464
|
-
eoxml
|
465
|
-
set = @xml/'root'
|
466
|
-
assert_equal 1, set.length
|
467
|
-
bike_tire = set.search('//bike:tire', 'bike' => "http://schwinn.com/")
|
468
|
-
assert_equal 1, bike_tire.length
|
469
|
-
end
|
470
|
-
|
471
|
-
def test_new_nodeset
|
472
|
-
node_set = Nokogiri::XML::NodeSet.new(@xml)
|
473
|
-
assert_equal(0, node_set.length)
|
474
|
-
node = Nokogiri::XML::Node.new('form', @xml)
|
475
|
-
node_set << node
|
476
|
-
assert_equal(1, node_set.length)
|
477
|
-
assert_equal(node, node_set.last)
|
478
|
-
end
|
479
|
-
|
480
|
-
def test_search_on_nodeset
|
481
|
-
assert node_set = @xml.search('//employee')
|
482
|
-
assert sub_set = node_set.search('.//name')
|
483
|
-
assert_equal(node_set.length, sub_set.length)
|
484
|
-
end
|
485
|
-
|
486
|
-
def test_negative_index_works
|
487
|
-
assert node_set = @xml.search('//employee')
|
488
|
-
assert_equal node_set.last, node_set[-1]
|
489
|
-
end
|
490
|
-
|
491
|
-
def test_large_negative_index_returns_nil
|
492
|
-
assert node_set = @xml.search('//employee')
|
493
|
-
assert_nil(node_set[-1 * (node_set.length + 1)])
|
494
|
-
end
|
495
|
-
|
496
|
-
def test_node_set_fetches_private_data
|
497
|
-
assert node_set = @xml.search('//employee')
|
498
|
-
|
499
|
-
set = node_set
|
500
|
-
assert_equal(set[0], set[0])
|
501
|
-
end
|
502
|
-
|
503
|
-
def test_node_set_returns_0
|
504
|
-
assert node_set = @xml.search('//asdkfjhasdlkfjhaldskfh')
|
505
|
-
assert_equal(0, node_set.length)
|
506
|
-
end
|
507
|
-
|
508
|
-
def test_wrap
|
509
|
-
employees = (@xml/"//employee").wrap("<wrapper/>")
|
510
|
-
assert_equal 'wrapper', employees[0].parent.name
|
511
|
-
assert_equal 'employee', @xml.search("//wrapper").first.children[0].name
|
512
|
-
end
|
513
|
-
|
514
|
-
def test_wrap_various_node_types
|
515
|
-
xml = '<root><foo>contents</foo></root>'
|
516
|
-
doc = Nokogiri::XML xml
|
517
|
-
nodes = doc.at_css("root").xpath(".//* | .//*/text()") # foo and "contents"
|
518
|
-
nodes.wrap("<wrapper/>")
|
519
|
-
wrappers = doc.css("wrapper")
|
520
|
-
assert_equal "root", wrappers.first.parent.name
|
521
|
-
assert_equal "foo", wrappers.first.children.first.name
|
522
|
-
assert_equal "foo", wrappers.last.parent.name
|
523
|
-
assert wrappers.last.children.first.text?
|
524
|
-
assert_equal "contents", wrappers.last.children.first.text
|
525
|
-
end
|
526
|
-
|
527
|
-
def test_wrap_a_fragment
|
528
|
-
frag = Nokogiri::XML::DocumentFragment.parse <<-EOXML
|
529
|
-
<employees>
|
530
|
-
<employee>hello</employee>
|
531
|
-
<employee>goodbye</employee>
|
532
|
-
</employees>
|
533
|
-
EOXML
|
534
|
-
employees = frag.xpath ".//employee"
|
535
|
-
employees.wrap("<wrapper/>")
|
536
|
-
assert_equal 'wrapper', employees[0].parent.name
|
537
|
-
assert_equal 'employee', frag.at(".//wrapper").children.first.name
|
538
|
-
end
|
539
|
-
|
540
|
-
def test_wrap_preserves_document_structure
|
541
|
-
assert_equal "employeeId",
|
542
|
-
@xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
|
543
|
-
@xml.xpath("//employeeId[text()='EMP0001']").wrap("<wrapper/>")
|
544
|
-
assert_equal "wrapper",
|
545
|
-
@xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
|
546
|
-
end
|
547
|
-
|
548
|
-
def test_plus_operator
|
549
|
-
names = @xml.search("name")
|
550
|
-
positions = @xml.search("position")
|
551
|
-
|
552
|
-
names_len = names.length
|
553
|
-
positions_len = positions.length
|
554
|
-
|
555
|
-
assert_raises(ArgumentError) { names + positions.first }
|
556
|
-
|
557
|
-
result = names + positions
|
558
|
-
assert_equal names_len, names.length
|
559
|
-
assert_equal positions_len, positions.length
|
560
|
-
assert_equal names.length + positions.length, result.length
|
561
|
-
|
562
|
-
names += positions
|
563
|
-
assert_equal result.length, names.length
|
564
|
-
end
|
565
|
-
|
566
|
-
def test_union
|
567
|
-
names = @xml.search("name")
|
568
|
-
|
569
|
-
assert_equal(names.length, (names | @xml.search("name")).length)
|
570
|
-
end
|
571
|
-
|
572
|
-
def test_minus_operator
|
573
|
-
employees = @xml.search("//employee")
|
574
|
-
females = @xml.search("//employee[gender[text()='Female']]")
|
575
|
-
|
576
|
-
employees_len = employees.length
|
577
|
-
females_len = females.length
|
578
|
-
|
579
|
-
assert_raises(ArgumentError) { employees - females.first }
|
580
|
-
|
581
|
-
result = employees - females
|
582
|
-
assert_equal employees_len, employees.length
|
583
|
-
assert_equal females_len, females.length
|
584
|
-
assert_equal employees.length - females.length, result.length
|
585
|
-
|
586
|
-
employees -= females
|
587
|
-
assert_equal result.length, employees.length
|
588
|
-
end
|
589
|
-
|
590
|
-
def test_array_index
|
591
|
-
employees = @xml.search("//employee")
|
592
|
-
other = @xml.search("//position").first
|
593
|
-
|
594
|
-
assert_equal 3, employees.index(employees[3])
|
595
|
-
assert_nil employees.index(other)
|
596
|
-
assert_equal 3, employees.index {|employee| employee.search("employeeId/text()").to_s == "EMP0004" }
|
597
|
-
assert_nil employees.index {|employee| employee.search("employeeId/text()").to_s == "EMP0000" }
|
598
|
-
end
|
599
|
-
|
600
|
-
def test_slice_too_far
|
601
|
-
employees = @xml.search("//employee")
|
602
|
-
assert_equal employees.length, employees[0, employees.length + 1].length
|
603
|
-
assert_equal employees.length, employees[0, employees.length].length
|
604
|
-
end
|
605
|
-
|
606
|
-
def test_slice_on_empty_node_set
|
607
|
-
empty_set = Nokogiri::XML::NodeSet.new @xml, []
|
608
|
-
assert_nil empty_set[99]
|
609
|
-
assert_nil empty_set[99..101]
|
610
|
-
assert_nil empty_set[99,2]
|
611
|
-
end
|
612
|
-
|
613
|
-
def test_slice_waaaaaay_off_the_end
|
614
|
-
xml = Nokogiri::XML::Builder.new {
|
615
|
-
root { 100.times { div } }
|
616
|
-
}.doc
|
617
|
-
nodes = xml.css "div"
|
618
|
-
assert_equal 1, nodes.slice(99, 100_000).length
|
619
|
-
assert_equal 0, nodes.slice(100, 100_000).length
|
620
|
-
end
|
621
|
-
|
622
|
-
def test_array_slice_with_start_and_end
|
623
|
-
employees = @xml.search("//employee")
|
624
|
-
assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
|
625
|
-
end
|
626
|
-
|
627
|
-
def test_array_index_bracket_equivalence
|
628
|
-
employees = @xml.search("//employee")
|
629
|
-
assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
|
630
|
-
assert_equal [employees[1], employees[2], employees[3]], employees.slice(1,3).to_a
|
631
|
-
end
|
632
|
-
|
633
|
-
def test_array_slice_with_negative_start
|
634
|
-
employees = @xml.search("//employee")
|
635
|
-
assert_equal [employees[2]], employees[-3,1].to_a
|
636
|
-
assert_equal [employees[2], employees[3]], employees[-3,2].to_a
|
637
|
-
end
|
638
|
-
|
639
|
-
def test_array_slice_with_invalid_args
|
640
|
-
employees = @xml.search("//employee")
|
641
|
-
assert_nil employees[99, 1] # large start
|
642
|
-
assert_nil employees[1, -1] # negative len
|
643
|
-
assert_equal [], employees[1, 0].to_a # zero len
|
644
|
-
end
|
645
|
-
|
646
|
-
def test_array_slice_with_range
|
647
|
-
employees = @xml.search("//employee")
|
648
|
-
assert_equal [employees[1], employees[2], employees[3]], employees[1..3].to_a
|
649
|
-
assert_equal [employees[0], employees[1], employees[2], employees[3]], employees[0..3].to_a
|
650
|
-
end
|
651
|
-
|
652
|
-
def test_intersection_with_no_overlap
|
653
|
-
employees = @xml.search("//employee")
|
654
|
-
positions = @xml.search("//position")
|
655
|
-
|
656
|
-
assert_equal [], (employees & positions).to_a
|
657
|
-
end
|
658
|
-
|
659
|
-
def test_intersection
|
660
|
-
employees = @xml.search("//employee")
|
661
|
-
first_set = employees[0..2]
|
662
|
-
second_set = employees[2..4]
|
663
|
-
|
664
|
-
assert_equal [employees[2]], (first_set & second_set).to_a
|
665
|
-
end
|
666
|
-
|
667
|
-
def test_intersection_on_empty_set
|
668
|
-
empty_set = Nokogiri::XML::NodeSet.new @xml
|
669
|
-
employees = @xml.search("//employee")
|
670
|
-
assert_equal 0, (empty_set & employees).length
|
671
|
-
end
|
672
|
-
|
673
|
-
def test_include?
|
674
|
-
employees = @xml.search("//employee")
|
675
|
-
yes = employees.first
|
676
|
-
no = @xml.search("//position").first
|
677
|
-
|
678
|
-
assert employees.include?(yes)
|
679
|
-
assert ! employees.include?(no)
|
680
|
-
end
|
681
|
-
|
682
|
-
def test_include_on_empty_node_set
|
683
|
-
empty_set = Nokogiri::XML::NodeSet.new @xml, []
|
684
|
-
employee = @xml.at_xpath("//employee")
|
685
|
-
assert ! empty_set.include?(employee)
|
686
|
-
end
|
687
|
-
|
688
|
-
def test_each
|
689
|
-
employees = @xml.search("//employee")
|
690
|
-
enum = employees.each
|
691
|
-
assert_instance_of Enumerator, enum
|
692
|
-
assert_equal enum.next, employees[0]
|
693
|
-
assert_equal enum.next, employees[1]
|
694
|
-
end
|
695
|
-
|
696
|
-
def test_children
|
697
|
-
employees = @xml.search("//employee")
|
698
|
-
count = 0
|
699
|
-
employees.each do |employee|
|
700
|
-
count += employee.children.length
|
701
|
-
end
|
702
|
-
set = employees.children
|
703
|
-
assert_equal count, set.length
|
704
|
-
end
|
705
|
-
|
706
|
-
def test_inspect
|
707
|
-
employees = @xml.search("//employee")
|
708
|
-
inspected = employees.inspect
|
709
|
-
|
710
|
-
assert_equal "[#{employees.map(&:inspect).join(', ')}]",
|
711
|
-
inspected
|
712
|
-
end
|
713
|
-
|
714
|
-
def test_should_not_splode_when_accessing_namespace_declarations_in_a_node_set
|
715
|
-
2.times do
|
716
|
-
xml = Nokogiri::XML "<foo></foo>"
|
717
|
-
node_set = xml.xpath("//namespace::*")
|
718
|
-
assert_equal 1, node_set.size
|
719
|
-
node = node_set.first
|
720
|
-
node.to_s # segfaults in 1.4.0 and earlier
|
721
|
-
|
722
|
-
# if we haven't segfaulted, let's make sure we handled it correctly
|
723
|
-
assert_instance_of Nokogiri::XML::Namespace, node
|
724
|
-
end
|
725
|
-
end
|
726
|
-
|
727
|
-
def test_should_not_splode_when_arrayifying_node_set_containing_namespace_declarations
|
728
|
-
xml = Nokogiri::XML "<foo></foo>"
|
729
|
-
node_set = xml.xpath("//namespace::*")
|
730
|
-
assert_equal 1, node_set.size
|
731
|
-
|
732
|
-
node_array = node_set.to_a
|
733
|
-
node = node_array.first
|
734
|
-
node.to_s # segfaults in 1.4.0 and earlier
|
735
|
-
|
736
|
-
# if we haven't segfaulted, let's make sure we handled it correctly
|
737
|
-
assert_instance_of Nokogiri::XML::Namespace, node
|
738
|
-
end
|
739
|
-
|
740
|
-
def test_should_not_splode_when_unlinking_node_set_containing_namespace_declarations
|
741
|
-
xml = Nokogiri::XML "<foo></foo>"
|
742
|
-
node_set = xml.xpath("//namespace::*")
|
743
|
-
assert_equal 1, node_set.size
|
744
|
-
|
745
|
-
node_set.unlink
|
746
|
-
end
|
747
|
-
|
748
|
-
def test_reverse
|
749
|
-
xml = Nokogiri::XML "<root><a />b<c />d<e /></root>"
|
750
|
-
children = xml.root.children
|
751
|
-
assert_instance_of Nokogiri::XML::NodeSet, children
|
752
|
-
|
753
|
-
reversed = children.reverse
|
754
|
-
assert_equal reversed[0], children[4]
|
755
|
-
assert_equal reversed[1], children[3]
|
756
|
-
assert_equal reversed[2], children[2]
|
757
|
-
assert_equal reversed[3], children[1]
|
758
|
-
assert_equal reversed[4], children[0]
|
759
|
-
|
760
|
-
assert_equal children, children.reverse.reverse
|
761
|
-
end
|
762
|
-
|
763
|
-
def test_node_set_dup_result_has_document_and_is_decorated
|
764
|
-
x = Module.new do
|
765
|
-
def awesome! ; end
|
766
|
-
end
|
767
|
-
util_decorate(@xml, x)
|
768
|
-
node_set = @xml.css("address")
|
769
|
-
new_set = node_set.dup
|
770
|
-
assert_equal node_set.document, new_set.document
|
771
|
-
assert new_set.respond_to?(:awesome!)
|
772
|
-
end
|
773
|
-
|
774
|
-
def test_node_set_union_result_has_document_and_is_decorated
|
775
|
-
x = Module.new do
|
776
|
-
def awesome! ; end
|
777
|
-
end
|
778
|
-
util_decorate(@xml, x)
|
779
|
-
node_set1 = @xml.css("address")
|
780
|
-
node_set2 = @xml.css("address")
|
781
|
-
new_set = node_set1 | node_set2
|
782
|
-
assert_equal node_set1.document, new_set.document
|
783
|
-
assert new_set.respond_to?(:awesome!)
|
784
|
-
end
|
785
|
-
|
786
|
-
def test_node_set_intersection_result_has_document_and_is_decorated
|
787
|
-
x = Module.new do
|
788
|
-
def awesome! ; end
|
789
|
-
end
|
790
|
-
util_decorate(@xml, x)
|
791
|
-
node_set1 = @xml.css("address")
|
792
|
-
node_set2 = @xml.css("address")
|
793
|
-
new_set = node_set1 & node_set2
|
794
|
-
assert_equal node_set1.document, new_set.document
|
795
|
-
assert new_set.respond_to?(:awesome!)
|
796
|
-
end
|
797
|
-
|
798
|
-
def test_node_set_difference_result_has_document_and_is_decorated
|
799
|
-
x = Module.new do
|
800
|
-
def awesome! ; end
|
801
|
-
end
|
802
|
-
util_decorate(@xml, x)
|
803
|
-
node_set1 = @xml.css("address")
|
804
|
-
node_set2 = @xml.css("address")
|
805
|
-
new_set = node_set1 - node_set2
|
806
|
-
assert_equal node_set1.document, new_set.document
|
807
|
-
assert new_set.respond_to?(:awesome!)
|
808
|
-
end
|
809
|
-
|
810
|
-
def test_node_set_slice_result_has_document_and_is_decorated
|
811
|
-
x = Module.new do
|
812
|
-
def awesome! ; end
|
813
|
-
end
|
814
|
-
util_decorate(@xml, x)
|
815
|
-
node_set = @xml.css("address")
|
816
|
-
new_set = node_set[0..-1]
|
817
|
-
assert_equal node_set.document, new_set.document
|
818
|
-
assert new_set.respond_to?(:awesome!)
|
819
|
-
end
|
820
|
-
|
821
|
-
def test_each_should_return_self
|
822
|
-
node_set1 = @xml.css("address")
|
823
|
-
node_set2 = node_set1.each {}
|
824
|
-
assert_equal node_set1, node_set2
|
825
|
-
end
|
826
|
-
end
|
827
|
-
end
|
828
|
-
end
|