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.

Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nokogiri/css/parser.rb +61 -61
  3. data/lib/nokogiri/version.rb +1 -1
  4. metadata +4 -146
  5. data/.autotest +0 -22
  6. data/.cross_rubies +0 -8
  7. data/.editorconfig +0 -17
  8. data/.gemtest +0 -0
  9. data/.travis.yml +0 -63
  10. data/CHANGELOG.md +0 -1402
  11. data/CONTRIBUTING.md +0 -42
  12. data/C_CODING_STYLE.rdoc +0 -33
  13. data/Gemfile +0 -23
  14. data/Gemfile-libxml-ruby +0 -3
  15. data/Manifest.txt +0 -371
  16. data/ROADMAP.md +0 -111
  17. data/Rakefile +0 -358
  18. data/SECURITY.md +0 -19
  19. data/STANDARD_RESPONSES.md +0 -47
  20. data/Y_U_NO_GEMSPEC.md +0 -155
  21. data/appveyor.yml +0 -29
  22. data/build_all +0 -48
  23. data/patches/sort-patches-by-date +0 -25
  24. data/suppressions/README.txt +0 -1
  25. data/suppressions/nokogiri_ruby-2.supp +0 -10
  26. data/tasks/test.rb +0 -100
  27. data/test/css/test_nthiness.rb +0 -226
  28. data/test/css/test_parser.rb +0 -386
  29. data/test/css/test_tokenizer.rb +0 -215
  30. data/test/css/test_xpath_visitor.rb +0 -96
  31. data/test/decorators/test_slop.rb +0 -23
  32. data/test/files/2ch.html +0 -108
  33. data/test/files/GH_1042.html +0 -18
  34. data/test/files/address_book.rlx +0 -12
  35. data/test/files/address_book.xml +0 -10
  36. data/test/files/atom.xml +0 -344
  37. data/test/files/bar/bar.xsd +0 -4
  38. data/test/files/bogus.xml +0 -0
  39. data/test/files/dont_hurt_em_why.xml +0 -422
  40. data/test/files/encoding.html +0 -82
  41. data/test/files/encoding.xhtml +0 -84
  42. data/test/files/exslt.xml +0 -8
  43. data/test/files/exslt.xslt +0 -35
  44. data/test/files/foo/foo.xsd +0 -4
  45. data/test/files/metacharset.html +0 -10
  46. data/test/files/namespace_pressure_test.xml +0 -1684
  47. data/test/files/noencoding.html +0 -47
  48. data/test/files/po.xml +0 -32
  49. data/test/files/po.xsd +0 -66
  50. data/test/files/saml/saml20assertion_schema.xsd +0 -283
  51. data/test/files/saml/saml20protocol_schema.xsd +0 -302
  52. data/test/files/saml/xenc_schema.xsd +0 -146
  53. data/test/files/saml/xmldsig_schema.xsd +0 -318
  54. data/test/files/shift_jis.html +0 -10
  55. data/test/files/shift_jis.xml +0 -5
  56. data/test/files/shift_jis_no_charset.html +0 -9
  57. data/test/files/slow-xpath.xml +0 -25509
  58. data/test/files/snuggles.xml +0 -3
  59. data/test/files/staff.dtd +0 -10
  60. data/test/files/staff.xml +0 -59
  61. data/test/files/staff.xslt +0 -32
  62. data/test/files/test_document_url/bar.xml +0 -2
  63. data/test/files/test_document_url/document.dtd +0 -4
  64. data/test/files/test_document_url/document.xml +0 -6
  65. data/test/files/tlm.html +0 -851
  66. data/test/files/to_be_xincluded.xml +0 -2
  67. data/test/files/valid_bar.xml +0 -2
  68. data/test/files/xinclude.xml +0 -4
  69. data/test/helper.rb +0 -271
  70. data/test/html/sax/test_parser.rb +0 -168
  71. data/test/html/sax/test_parser_context.rb +0 -46
  72. data/test/html/sax/test_parser_text.rb +0 -163
  73. data/test/html/sax/test_push_parser.rb +0 -87
  74. data/test/html/test_attributes.rb +0 -85
  75. data/test/html/test_builder.rb +0 -164
  76. data/test/html/test_document.rb +0 -722
  77. data/test/html/test_document_encoding.rb +0 -143
  78. data/test/html/test_document_fragment.rb +0 -310
  79. data/test/html/test_element_description.rb +0 -105
  80. data/test/html/test_named_characters.rb +0 -14
  81. data/test/html/test_node.rb +0 -212
  82. data/test/html/test_node_encoding.rb +0 -91
  83. data/test/namespaces/test_additional_namespaces_in_builder_doc.rb +0 -14
  84. data/test/namespaces/test_namespaces_aliased_default.rb +0 -24
  85. data/test/namespaces/test_namespaces_in_builder_doc.rb +0 -75
  86. data/test/namespaces/test_namespaces_in_cloned_doc.rb +0 -31
  87. data/test/namespaces/test_namespaces_in_created_doc.rb +0 -75
  88. data/test/namespaces/test_namespaces_in_parsed_doc.rb +0 -80
  89. data/test/namespaces/test_namespaces_preservation.rb +0 -31
  90. data/test/test_convert_xpath.rb +0 -135
  91. data/test/test_css_cache.rb +0 -47
  92. data/test/test_encoding_handler.rb +0 -48
  93. data/test/test_memory_leak.rb +0 -226
  94. data/test/test_nokogiri.rb +0 -138
  95. data/test/test_soap4r_sax.rb +0 -52
  96. data/test/test_xslt_transforms.rb +0 -314
  97. data/test/xml/node/test_save_options.rb +0 -28
  98. data/test/xml/node/test_subclass.rb +0 -44
  99. data/test/xml/sax/test_parser.rb +0 -402
  100. data/test/xml/sax/test_parser_context.rb +0 -115
  101. data/test/xml/sax/test_parser_text.rb +0 -202
  102. data/test/xml/sax/test_push_parser.rb +0 -265
  103. data/test/xml/test_attr.rb +0 -96
  104. data/test/xml/test_attribute_decl.rb +0 -86
  105. data/test/xml/test_builder.rb +0 -353
  106. data/test/xml/test_c14n.rb +0 -180
  107. data/test/xml/test_cdata.rb +0 -54
  108. data/test/xml/test_comment.rb +0 -40
  109. data/test/xml/test_document.rb +0 -982
  110. data/test/xml/test_document_encoding.rb +0 -31
  111. data/test/xml/test_document_fragment.rb +0 -312
  112. data/test/xml/test_dtd.rb +0 -187
  113. data/test/xml/test_dtd_encoding.rb +0 -31
  114. data/test/xml/test_element_content.rb +0 -56
  115. data/test/xml/test_element_decl.rb +0 -73
  116. data/test/xml/test_entity_decl.rb +0 -122
  117. data/test/xml/test_entity_reference.rb +0 -262
  118. data/test/xml/test_namespace.rb +0 -96
  119. data/test/xml/test_node.rb +0 -1379
  120. data/test/xml/test_node_attributes.rb +0 -115
  121. data/test/xml/test_node_encoding.rb +0 -75
  122. data/test/xml/test_node_inheritance.rb +0 -32
  123. data/test/xml/test_node_reparenting.rb +0 -661
  124. data/test/xml/test_node_set.rb +0 -828
  125. data/test/xml/test_parse_options.rb +0 -64
  126. data/test/xml/test_processing_instruction.rb +0 -30
  127. data/test/xml/test_reader.rb +0 -620
  128. data/test/xml/test_reader_encoding.rb +0 -134
  129. data/test/xml/test_relax_ng.rb +0 -60
  130. data/test/xml/test_schema.rb +0 -142
  131. data/test/xml/test_syntax_error.rb +0 -36
  132. data/test/xml/test_text.rb +0 -69
  133. data/test/xml/test_unparented_node.rb +0 -483
  134. data/test/xml/test_xinclude.rb +0 -83
  135. data/test/xml/test_xpath.rb +0 -470
  136. data/test/xslt/test_custom_functions.rb +0 -133
  137. data/test/xslt/test_exception_handling.rb +0 -37
@@ -1,96 +0,0 @@
1
- require "helper"
2
-
3
- module Nokogiri
4
- module XML
5
- class TestNamespace < Nokogiri::TestCase
6
- def setup
7
- super
8
- @xml = Nokogiri::XML <<-eoxml
9
- <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
10
- <awesome/>
11
- </root>
12
- eoxml
13
- end
14
-
15
- if Nokogiri.uses_libxml?
16
- def test_namespace_is_in_node_cache
17
- node = @xml.root.namespace
18
- assert @xml.instance_variable_get(:@node_cache).include?(node)
19
- end
20
- end
21
-
22
- def test_built_nodes_keep_namespace_decls
23
- doc = Document.new
24
- e = Node.new 'element', doc
25
- c = Node.new 'child', doc
26
- c.default_namespace = 'woop:de:doo'
27
-
28
- assert c.namespace, 'has a namespace'
29
- e.add_child c
30
- assert c.namespace, 'has a namespace'
31
-
32
- doc.add_child e
33
- assert c.namespace, 'has a namespace'
34
- end
35
-
36
- def test_inspect
37
- ns = @xml.root.namespace
38
- assert_equal "#<#{ns.class.name}:#{sprintf("0x%x", ns.object_id)} href=#{ns.href.inspect}>", ns.inspect
39
- end
40
-
41
- def test_namespace_node_prefix
42
- namespaces = @xml.root.namespace_definitions
43
- assert_equal [nil, 'foo'], namespaces.map(&:prefix)
44
- end
45
-
46
- def test_namespace_node_href
47
- namespaces = @xml.root.namespace_definitions
48
- assert_equal [
49
- 'http://tenderlovemaking.com/',
50
- 'bar'
51
- ], namespaces.map(&:href)
52
- end
53
-
54
- def test_equality
55
- namespaces = @xml.root.namespace_definitions
56
- assert_equal namespaces, @xml.root.namespace_definitions
57
- end
58
-
59
- def test_add_definition
60
- @xml.root.add_namespace_definition('baz', 'bar')
61
- assert_equal 3, @xml.root.namespace_definitions.length
62
- end
63
-
64
- def test_add_definition_return
65
- ns = @xml.root.add_namespace_definition('baz', 'bar')
66
- assert_equal 'baz', ns.prefix
67
- end
68
-
69
- def test_remove_entity_namespace
70
- s = %q{<?xml version='1.0'?><!DOCTYPE schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd" [<!ENTITY % p ''>]>}
71
- Nokogiri::XML(s).remove_namespaces!
72
- # TODO: we should probably assert something here. See commit 14d2f59.
73
- end
74
-
75
- def test_maintain_element_namespaces
76
- doc = Document.new
77
- subject = Nokogiri::XML::Node.new 'foo', doc
78
- subject << '<foobar xmlns="barfoo"/>'
79
- child = subject.children.first
80
- assert_equal 'foobar', child.name
81
- assert_equal 'barfoo', child.namespace.href
82
- assert_empty child.attributes
83
- end
84
-
85
- def test_maintain_element_namespaces_in_urn
86
- doc = Document.new
87
- subject = Nokogiri::XML::Node.new 'foo', doc
88
- subject << '<foobar xmlns="urn:xmpp:foospec:barfoo"/>'
89
- child = subject.children.first
90
- assert_equal 'foobar', child.name
91
- assert_equal 'urn:xmpp:foospec:barfoo', child.namespace.href
92
- assert_empty child.attributes
93
- end
94
- end
95
- end
96
- end
@@ -1,1379 +0,0 @@
1
- require "helper"
2
-
3
- require 'stringio'
4
-
5
- module Nokogiri
6
- module XML
7
- class TestNode < Nokogiri::TestCase
8
- def setup
9
- super
10
- @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
11
- end
12
-
13
- def test_first_element_child
14
- node = @xml.root.first_element_child
15
- assert_equal 'employee', node.name
16
- assert node.element?, 'node is an element'
17
- end
18
-
19
- def test_element_children
20
- nodes = @xml.root.element_children
21
- assert_equal @xml.root.first_element_child, nodes.first
22
- assert nodes.all?(&:element?), 'all nodes are elements'
23
- end
24
-
25
- def test_last_element_child
26
- nodes = @xml.root.element_children
27
- assert_equal nodes.last, @xml.root.element_children.last
28
- end
29
-
30
- def test_bad_xpath
31
- bad_xpath = '//foo['
32
-
33
- begin
34
- @xml.xpath(bad_xpath)
35
- rescue Nokogiri::XML::XPath::SyntaxError => e
36
- assert_match(bad_xpath, e.to_s)
37
- end
38
- end
39
-
40
- def test_namespace_type_error
41
- assert_raises(TypeError) do
42
- @xml.root.namespace = Object.new
43
- end
44
- end
45
-
46
- def test_remove_namespace
47
- @xml = Nokogiri::XML('<r xmlns="v"><s /></r>')
48
- tag = @xml.at('s')
49
- assert tag.namespace
50
- tag.namespace = nil
51
- assert_nil tag.namespace
52
- end
53
-
54
- def test_parse_needs_doc
55
- list = @xml.root.parse('fooooooo <hello />')
56
- assert_equal 1, list.css('hello').length
57
- end
58
-
59
- def test_parse
60
- list = @xml.root.parse('fooooooo <hello />')
61
- assert_equal 2, list.length
62
- end
63
-
64
- def test_parse_with_block
65
- called = false
66
- list = @xml.root.parse('<hello />') { |cfg|
67
- called = true
68
- assert_instance_of Nokogiri::XML::ParseOptions, cfg
69
- }
70
- assert called, 'config block called'
71
- assert_equal 1, list.length
72
- end
73
-
74
- def test_parse_with_io
75
- list = @xml.root.parse(StringIO.new('<hello />'))
76
- assert_equal 1, list.length
77
- assert_equal 'hello', list.first.name
78
- end
79
-
80
- def test_parse_with_empty_string
81
- list = @xml.root.parse('')
82
- assert_equal 0, list.length
83
- end
84
-
85
- def test_parse_config_option
86
- node = @xml.root
87
- options = nil
88
- node.parse("<item></item>") do |config|
89
- options = config
90
- end
91
- assert_equal Nokogiri::XML::ParseOptions::DEFAULT_XML, options.to_i
92
- end
93
-
94
- def test_node_context_parsing_of_malformed_html_fragment
95
- doc = HTML.parse "<html><body><div></div></body></html>"
96
- context_node = doc.at_css "div"
97
- nodeset = context_node.parse("<div </div>")
98
-
99
- assert_equal 1, doc.errors.length
100
- assert_equal 1, nodeset.length
101
- assert_equal "<div></div>", nodeset.to_s
102
- end
103
-
104
- def test_node_context_parsing_of_malformed_html_fragment_with_recover_is_corrected
105
- doc = HTML.parse "<html><body><div></div></body></html>"
106
- context_node = doc.at_css "div"
107
- nodeset = context_node.parse("<div </div>") do |options|
108
- options.recover
109
- end
110
-
111
- assert_equal 1, doc.errors.length
112
- assert_equal 1, nodeset.length
113
- assert_equal "<div></div>", nodeset.to_s
114
- end
115
-
116
- def test_node_context_parsing_of_malformed_html_fragment_without_recover_is_not_corrected
117
- doc = HTML.parse "<html><body><div></div></body></html>"
118
- context_node = doc.at_css "div"
119
- nodeset = context_node.parse("<div </div>") do |options|
120
- options.strict
121
- end
122
-
123
- assert_equal 1, doc.errors.length
124
- assert_equal 0, nodeset.length
125
- end
126
-
127
- def test_parse_error_list
128
- error_count = @xml.errors.length
129
- @xml.root.parse('<hello>')
130
- assert(error_count < @xml.errors.length, "errors should have increased")
131
- end
132
-
133
- def test_parse_error_on_fragment_with_empty_document
134
- doc = Document.new
135
- fragment = DocumentFragment.new(doc, '<foo><bar/></foo>')
136
- node = fragment%'bar'
137
- node.parse('<baz><</baz>')
138
- end
139
-
140
- def test_parse_with_unparented_text_context_node
141
- doc = XML::Document.new
142
- elem = XML::Text.new("foo", doc)
143
- x = elem.parse("<bar/>") # should not raise an exception
144
- assert_equal x.first.name, "bar"
145
- end
146
-
147
- def test_parse_with_unparented_html_text_context_node
148
- doc = HTML::Document.new
149
- elem = XML::Text.new("div", doc)
150
- x = elem.parse("<div/>") # should not raise an exception
151
- assert_equal x.first.name, "div"
152
- end
153
-
154
- def test_parse_with_unparented_fragment_text_context_node
155
- doc = XML::DocumentFragment.parse "<div><span>foo</span></div>"
156
- elem = doc.at_css "span"
157
- x = elem.parse("<span/>") # should not raise an exception
158
- assert_equal x.first.name, "span"
159
- end
160
-
161
- def test_parse_with_unparented_html_fragment_text_context_node
162
- doc = HTML::DocumentFragment.parse "<div><span>foo</span></div>"
163
- elem = doc.at_css "span"
164
- x = elem.parse("<span/>") # should not raise an exception
165
- assert_equal x.first.name, "span"
166
- end
167
-
168
- def test_dup_is_deep_copy_by_default
169
- doc = XML::Document.parse "<root><div><p>hello</p></div></root>"
170
- div = doc.at_css "div"
171
- node = div.dup
172
- assert_equal 1, node.children.length
173
- assert_equal "<p>hello</p>", node.children.first.to_html
174
- end
175
-
176
- def test_dup_deep_copy
177
- doc = XML::Document.parse "<root><div><p>hello</p></div></root>"
178
- div = doc.at_css "div"
179
- node = div.dup(1)
180
- assert_equal 1, node.children.length
181
- assert_equal "<p>hello</p>", node.children.first.to_html
182
- end
183
-
184
- def test_dup_shallow_copy
185
- doc = XML::Document.parse "<root><div><p>hello</p></div></root>"
186
- div = doc.at_css "div"
187
- node = div.dup(0)
188
- assert_equal 0, node.children.length
189
- end
190
-
191
- if Nokogiri.uses_libxml?
192
- def test_dup_to_another_document
193
- doc1 = HTML::Document.parse "<root><div><p>hello</p></div></root>"
194
- doc2 = HTML::Document.parse "<div></div>"
195
-
196
- div = doc1.at_css "div"
197
- duplicate_div = div.dup(1, doc2)
198
-
199
- assert_not_nil doc1.at_css("div")
200
- assert_equal doc2, duplicate_div.document
201
- assert_equal 1, duplicate_div.children.length
202
- assert_equal "<p>hello</p>", duplicate_div.children.first.to_html
203
- end
204
- end
205
-
206
- def test_subclass_dup
207
- subclass = Class.new(Nokogiri::XML::Node)
208
- node = subclass.new('foo', @xml).dup
209
- assert_instance_of subclass, node
210
- end
211
-
212
- def test_gt_string_arg
213
- node = @xml.at('employee')
214
- nodes = (node > 'name')
215
- assert_equal 1, nodes.length
216
- assert_equal node, nodes.first.parent
217
- end
218
-
219
- def test_next_element_when_next_sibling_is_element_should_return_next_sibling
220
- doc = Nokogiri::XML "<root><foo /><quux /></root>"
221
- node = doc.at_css("foo")
222
- next_element = node.next_element
223
- assert next_element.element?
224
- assert_equal doc.at_css("quux"), next_element
225
- end
226
-
227
- def test_next_element_when_there_is_no_next_sibling_should_return_nil
228
- doc = Nokogiri::XML "<root><foo /><quux /></root>"
229
- assert_nil doc.at_css("quux").next_element
230
- end
231
-
232
- def test_next_element_when_next_sibling_is_not_an_element_should_return_closest_next_element_sibling
233
- doc = Nokogiri::XML "<root><foo />bar<quux /></root>"
234
- node = doc.at_css("foo")
235
- next_element = node.next_element
236
- assert next_element.element?
237
- assert_equal doc.at_css("quux"), next_element
238
- end
239
-
240
- def test_next_element_when_next_sibling_is_not_an_element_and_no_following_element_should_return_nil
241
- doc = Nokogiri::XML "<root><foo />bar</root>"
242
- node = doc.at_css("foo")
243
- next_element = node.next_element
244
- assert_nil next_element
245
- end
246
-
247
- def test_previous_element_when_previous_sibling_is_element_should_return_previous_sibling
248
- doc = Nokogiri::XML "<root><foo /><quux /></root>"
249
- node = doc.at_css("quux")
250
- previous_element = node.previous_element
251
- assert previous_element.element?
252
- assert_equal doc.at_css("foo"), previous_element
253
- end
254
-
255
- def test_previous_element_when_there_is_no_previous_sibling_should_return_nil
256
- doc = Nokogiri::XML "<root><foo /><quux /></root>"
257
- assert_nil doc.at_css("foo").previous_element
258
- end
259
-
260
- def test_previous_element_when_previous_sibling_is_not_an_element_should_return_closest_previous_element_sibling
261
- doc = Nokogiri::XML "<root><foo />bar<quux /></root>"
262
- node = doc.at_css("quux")
263
- previous_element = node.previous_element
264
- assert previous_element.element?
265
- assert_equal doc.at_css("foo"), previous_element
266
- end
267
-
268
- def test_previous_element_when_previous_sibling_is_not_an_element_and_no_following_element_should_return_nil
269
- doc = Nokogiri::XML "<root>foo<bar /></root>"
270
- node = doc.at_css("bar")
271
- previous_element = node.previous_element
272
- assert_nil previous_element
273
- end
274
-
275
- def test_element?
276
- assert @xml.root.element?, 'is an element'
277
- end
278
-
279
- def test_slash_search
280
- assert_equal 'EMP0001', (@xml/:staff/:employee/:employeeId).first.text
281
- end
282
-
283
- def test_append_with_document
284
- assert_raises(ArgumentError) do
285
- @xml.root << Nokogiri::XML::Document.new
286
- end
287
- end
288
-
289
- def test_append_with_attr
290
- r = Nokogiri.XML('<r a="1" />').root
291
- assert_raises(ArgumentError) do
292
- r << r.at_xpath('@a')
293
- end
294
- end
295
-
296
- def test_inspect_ns
297
- xml = Nokogiri::XML(<<-eoxml) { |c| c.noblanks }
298
- <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
299
- <awesome/>
300
- </root>
301
- eoxml
302
- ins = xml.inspect
303
-
304
- xml.traverse do |node|
305
- assert_match node.class.name, ins
306
- if node.respond_to? :attributes
307
- node.attributes.each do |k,v|
308
- assert_match k, ins
309
- assert_match v, ins
310
- end
311
- end
312
-
313
- if node.respond_to?(:namespace) && node.namespace
314
- assert_match node.namespace.class.name, ins
315
- assert_match node.namespace.href, ins
316
- end
317
- end
318
- end
319
-
320
- def test_namespace_definitions_when_some_exist
321
- xml = Nokogiri::XML <<-eoxml
322
- <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
323
- <awesome/>
324
- </root>
325
- eoxml
326
- namespace_definitions = xml.root.namespace_definitions
327
- assert_equal 2, namespace_definitions.length
328
- end
329
-
330
- def test_namespace_definitions_when_no_exist
331
- xml = Nokogiri::XML <<-eoxml
332
- <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
333
- <awesome/>
334
- </root>
335
- eoxml
336
- namespace_definitions = xml.at_xpath('//xmlns:awesome').namespace_definitions
337
- assert_equal 0, namespace_definitions.length
338
- end
339
-
340
- def test_namespace_goes_to_children
341
- fruits = Nokogiri::XML(<<-eoxml)
342
- <Fruit xmlns='www.fruits.org'>
343
- </Fruit>
344
- eoxml
345
- apple = Nokogiri::XML::Node.new('Apple', fruits)
346
- orange = Nokogiri::XML::Node.new('Orange', fruits)
347
- apple << orange
348
- fruits.root << apple
349
- assert fruits.at('//fruit:Orange',{'fruit'=>'www.fruits.org'})
350
- assert fruits.at('//fruit:Apple',{'fruit'=>'www.fruits.org'})
351
- end
352
-
353
- def test_description
354
- assert_nil @xml.at('employee').description
355
- end
356
-
357
- def test_spaceship
358
- nodes = @xml.xpath('//employee')
359
- assert_equal(-1, (nodes.first <=> nodes.last))
360
- list = [nodes.first, nodes.last].sort
361
- assert_equal nodes.first, list.first
362
- assert_equal nodes.last, list.last
363
- end
364
-
365
- def test_incorrect_spaceship
366
- nodes = @xml.xpath('//employee')
367
- assert_nil(nodes.first <=> 'asdf')
368
- end
369
-
370
- def test_document_compare
371
- skip "underlying libxml2 behavior changed in libxml2@a005199"
372
- nodes = @xml.xpath('//employee')
373
- assert_equal(-1, (nodes.first <=> @xml)) # post-libxml2@a005199, returns 1
374
- end
375
-
376
- def test_different_document_compare
377
- nodes = @xml.xpath('//employee')
378
- doc = Nokogiri::XML('<a><b/></a>')
379
- b = doc.at('b')
380
- assert_nil(nodes.first <=> b)
381
- end
382
-
383
- def test_duplicate_node_removes_namespace
384
- fruits = Nokogiri::XML(<<-eoxml)
385
- <Fruit xmlns='www.fruits.org'>
386
- <Apple></Apple>
387
- </Fruit>
388
- eoxml
389
- apple = fruits.root.xpath('fruit:Apple', {'fruit'=>'www.fruits.org'})[0]
390
- new_apple = apple.dup
391
- fruits.root << new_apple
392
- assert_equal 2, fruits.xpath('//xmlns:Apple').length
393
- assert_equal 1, fruits.to_xml.scan('www.fruits.org').length
394
- end
395
-
396
- [:clone, :dup].each do |symbol|
397
- define_method "test_#{symbol}" do
398
- node = @xml.at('//employee')
399
- other = node.send(symbol)
400
- assert_equal "employee", other.name
401
- assert_nil other.parent
402
- end
403
- end
404
-
405
- def test_fragment_creates_elements
406
- apple = @xml.fragment('<Apple/>')
407
- apple.children.each do |child|
408
- assert_equal Nokogiri::XML::Node::ELEMENT_NODE, child.type
409
- assert_instance_of Nokogiri::XML::Element, child
410
- end
411
- end
412
-
413
- def test_node_added_to_root_should_get_namespace
414
- fruits = Nokogiri::XML(<<-eoxml)
415
- <Fruit xmlns='http://www.fruits.org'>
416
- </Fruit>
417
- eoxml
418
- apple = fruits.fragment('<Apple/>')
419
- fruits.root << apple
420
- assert_equal 1, fruits.xpath('//xmlns:Apple').length
421
- end
422
-
423
- def test_new_node_can_have_ancestors
424
- xml = Nokogiri::XML('<root>text</root>')
425
- item = Nokogiri::XML::Element.new('item', xml)
426
- assert_equal 0, item.ancestors.length
427
- end
428
-
429
- def test_children
430
- doc = Nokogiri::XML(<<-eoxml)
431
- <root>#{'<a/>' * 9 }</root>
432
- eoxml
433
- assert_equal 9, doc.root.children.length
434
- assert_equal 9, doc.root.children.to_a.length
435
-
436
- doc = Nokogiri::XML(<<-eoxml)
437
- <root>#{'<a/>' * 15 }</root>
438
- eoxml
439
- assert_equal 15, doc.root.children.length
440
- assert_equal 15, doc.root.children.to_a.length
441
- end
442
-
443
- def test_add_namespace
444
- node = @xml.at('address')
445
- node.add_namespace('foo', 'http://tenderlovemaking.com')
446
- assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
447
- end
448
-
449
- def test_add_namespace_twice
450
- node = @xml.at('address')
451
- ns = node.add_namespace('foo', 'http://tenderlovemaking.com')
452
- ns2 = node.add_namespace('foo', 'http://tenderlovemaking.com')
453
- assert_equal ns, ns2
454
- end
455
-
456
- def test_add_default_namespace
457
- node = @xml.at('address')
458
- node.add_namespace(nil, 'http://tenderlovemaking.com')
459
- assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
460
- end
461
-
462
- def test_add_default_namespace_twice
463
- node = @xml.at('address')
464
- ns = node.add_namespace(nil, 'http://tenderlovemaking.com')
465
- ns2 = node.add_namespace(nil, 'http://tenderlovemaking.com')
466
- assert_equal ns.object_id, ns2.object_id
467
- end
468
-
469
- def test_add_multiple_namespaces
470
- node = @xml.at('address')
471
-
472
- node.add_namespace(nil, 'http://tenderlovemaking.com')
473
- assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
474
-
475
- node.add_namespace('foo', 'http://tenderlovemaking.com')
476
- assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
477
- end
478
-
479
- def test_default_namespace=
480
- node = @xml.at('address')
481
- node.default_namespace = 'http://tenderlovemaking.com'
482
- assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
483
- end
484
-
485
- def test_namespace=
486
- node = @xml.at('address')
487
- assert_nil node.namespace
488
- definition = node.add_namespace_definition 'bar', 'http://tlm.com/'
489
-
490
- node.namespace = definition
491
-
492
- assert_equal definition, node.namespace
493
-
494
- assert_equal node, @xml.at('//foo:address', {
495
- 'foo' => 'http://tlm.com/'
496
- })
497
- end
498
-
499
- def test_add_namespace_with_nil_associates_node
500
- node = @xml.at('address')
501
- assert_nil node.namespace
502
- definition = node.add_namespace_definition nil, 'http://tlm.com/'
503
- assert_equal definition, node.namespace
504
- end
505
-
506
- def test_add_namespace_does_not_associate_node
507
- node = @xml.at('address')
508
- assert_nil node.namespace
509
- assert node.add_namespace_definition 'foo', 'http://tlm.com/'
510
- assert_nil node.namespace
511
- end
512
-
513
- def test_set_namespace_from_different_doc
514
- node = @xml.at('address')
515
- doc = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
516
- decl = doc.root.add_namespace_definition 'foo', 'bar'
517
-
518
- assert_raises(ArgumentError) do
519
- node.namespace = decl
520
- end
521
- end
522
-
523
- def test_set_namespace_must_only_take_a_namespace
524
- node = @xml.at('address')
525
- assert_raises(TypeError) do
526
- node.namespace = node
527
- end
528
- end
529
-
530
- def test_at
531
- node = @xml.at('address')
532
- assert_equal node, @xml.xpath('//address').first
533
- end
534
-
535
- def test_at_self
536
- node = @xml.at('address')
537
- assert_equal node, node.at('.')
538
- end
539
-
540
- def test_at_xpath
541
- node = @xml.at_xpath('//address')
542
- nodes = @xml.xpath('//address')
543
- assert_equal 5, nodes.size
544
- assert_equal node, nodes.first
545
- end
546
-
547
- def test_at_css
548
- node = @xml.at_css('address')
549
- nodes = @xml.css('address')
550
- assert_equal 5, nodes.size
551
- assert_equal node, nodes.first
552
- end
553
-
554
- def test_percent
555
- node = @xml % ('address')
556
- assert_equal node, @xml.xpath('//address').first
557
- end
558
-
559
- def test_accept
560
- visitor = Class.new {
561
- attr_accessor :visited
562
- def accept target
563
- target.accept(self)
564
- end
565
-
566
- def visit node
567
- node.children.each { |c| c.accept(self) }
568
- @visited = true
569
- end
570
- }.new
571
- visitor.accept(@xml.root)
572
- assert visitor.visited
573
- end
574
-
575
- def test_write_to
576
- io = StringIO.new
577
- @xml.write_to io
578
- io.rewind
579
- assert_equal @xml.to_xml, io.read
580
- end
581
-
582
- def test_attribute_with_symbol
583
- assert_equal 'Yes', @xml.css('address').first[:domestic]
584
- end
585
-
586
- def test_non_existent_attribute_should_return_nil
587
- node = @xml.root.first_element_child
588
- assert_nil node.attribute('type')
589
- end
590
-
591
- def test_write_to_with_block
592
- called = false
593
- io = StringIO.new
594
- conf = nil
595
- @xml.write_to io do |config|
596
- called = true
597
- conf = config
598
- config.format.as_html.no_empty_tags
599
- end
600
- io.rewind
601
- assert called
602
- string = io.read
603
- assert_equal @xml.serialize(nil, conf.options), string
604
- assert_equal @xml.serialize(nil, conf), string
605
- end
606
-
607
- %w{ xml html xhtml }.each do |type|
608
- define_method(:"test_write_#{type}_to") do
609
- io = StringIO.new
610
- assert @xml.send(:"write_#{type}_to", io)
611
- io.rewind
612
- assert_match @xml.send(:"to_#{type}"), io.read
613
- end
614
- end
615
-
616
- def test_serialize_with_block
617
- called = false
618
- conf = nil
619
- string = @xml.serialize do |config|
620
- called = true
621
- conf = config
622
- config.format.as_html.no_empty_tags
623
- end
624
- assert called
625
- assert_equal @xml.serialize(nil, conf.options), string
626
- assert_equal @xml.serialize(nil, conf), string
627
- end
628
-
629
- def test_hold_refence_to_subnode
630
- doc = Nokogiri::XML(<<-eoxml)
631
- <root>
632
- <a>
633
- <b />
634
- </a>
635
- </root>
636
- eoxml
637
- assert node_a = doc.css('a').first
638
- assert node_b = node_a.css('b').first
639
- node_a.unlink
640
- assert_equal 'b', node_b.name
641
- end
642
-
643
- def test_values
644
- assert_equal %w{ Yes Yes }, @xml.xpath('//address')[1].values
645
- end
646
-
647
- def test_keys
648
- assert_equal %w{ domestic street }, @xml.xpath('//address')[1].keys
649
- end
650
-
651
- def test_each
652
- attributes = []
653
- @xml.xpath('//address')[1].each do |key, value|
654
- attributes << [key, value]
655
- end
656
- assert_equal [['domestic', 'Yes'], ['street', 'Yes']], attributes
657
- end
658
-
659
- def test_new
660
- assert node = Nokogiri::XML::Node.new('input', @xml)
661
- assert_equal 1, node.node_type
662
- assert_instance_of Nokogiri::XML::Element, node
663
- end
664
-
665
- def test_to_str
666
- name = @xml.xpath('//name').first
667
- assert_match(/Margaret/, '' + name)
668
- assert_equal('Margaret Martin', '' + name.children.first)
669
- end
670
-
671
- def test_ancestors
672
- address = @xml.xpath('//address').first
673
- assert_equal 3, address.ancestors.length
674
- assert_equal ['employee', 'staff', 'document'],
675
- address.ancestors.map(&:name)
676
- end
677
-
678
- def test_read_only?
679
- assert entity_decl = @xml.internal_subset.children.find { |x|
680
- x.type == Node::ENTITY_DECL
681
- }
682
- assert entity_decl.read_only?
683
- end
684
-
685
- def test_remove_attribute
686
- address = @xml.xpath('/staff/employee/address').first
687
- assert_equal 'Yes', address['domestic']
688
- attr = address.attributes['domestic']
689
-
690
- returned_attr = address.remove_attribute 'domestic'
691
- assert_nil address['domestic']
692
- assert_equal attr, returned_attr
693
- end
694
-
695
- def test_remove_attribute_when_not_found
696
- address = @xml.xpath('/staff/employee/address').first
697
- attr = address.remove_attribute 'not-an-attribute'
698
- assert_nil attr
699
- end
700
-
701
- def test_attribute_setter_accepts_non_string
702
- address = @xml.xpath("/staff/employee/address").first
703
- assert_equal "Yes", address[:domestic]
704
- address[:domestic] = "Altered Yes"
705
- assert_equal "Altered Yes", address[:domestic]
706
- end
707
-
708
- def test_attribute_accessor_accepts_non_string
709
- address = @xml.xpath("/staff/employee/address").first
710
- assert_equal "Yes", address["domestic"]
711
- assert_equal "Yes", address[:domestic]
712
- end
713
-
714
- def test_empty_attribute_reading
715
- node = Nokogiri::XML '<foo empty="" whitespace=" "/>'
716
-
717
- assert_equal '', node.root['empty']
718
- assert_equal ' ', node.root['whitespace']
719
- end
720
-
721
- def test_delete
722
- address = @xml.xpath('/staff/employee/address').first
723
- assert_equal 'Yes', address['domestic']
724
- address.delete 'domestic'
725
- assert_nil address['domestic']
726
- end
727
-
728
- def test_classes
729
- xml = Nokogiri::XML(<<-eoxml)
730
- <div>
731
- <p class=" foo bar foo ">test</p>
732
- <p class="">test</p>
733
- </div>
734
- eoxml
735
- div = xml.at_xpath('//div')
736
- p1, p2 = xml.xpath('//p')
737
-
738
- assert_equal [], div.classes
739
- assert_equal %w[foo bar foo], p1.classes
740
- assert_equal [], p2.classes
741
- end
742
-
743
- def test_add_class
744
- xml = Nokogiri::XML(<<-eoxml)
745
- <div>
746
- <p class=" foo bar foo ">test</p>
747
- <p class="">test</p>
748
- </div>
749
- eoxml
750
- div = xml.at_xpath('//div')
751
- p1, p2 = xml.xpath('//p')
752
-
753
- assert_same div, div.add_class('main')
754
- assert_equal 'main', div['class']
755
-
756
- assert_same p1, p1.add_class('baz foo')
757
- assert_equal 'foo bar foo baz', p1['class']
758
-
759
- assert_same p2, p2.add_class('foo baz foo')
760
- assert_equal 'foo baz foo', p2['class']
761
- end
762
-
763
- def test_append_class
764
- xml = Nokogiri::XML(<<-eoxml)
765
- <div>
766
- <p class=" foo bar foo ">test</p>
767
- <p class="">test</p>
768
- </div>
769
- eoxml
770
- div = xml.at_xpath('//div')
771
- p1, p2 = xml.xpath('//p')
772
-
773
- assert_same div, div.append_class('main')
774
- assert_equal 'main', div['class']
775
-
776
- assert_same p1, p1.append_class('baz foo')
777
- assert_equal 'foo bar foo baz foo', p1['class']
778
-
779
- assert_same p2, p2.append_class('foo baz foo')
780
- assert_equal 'foo baz foo', p2['class']
781
- end
782
-
783
- def test_remove_class
784
- xml = Nokogiri::XML(<<-eoxml)
785
- <div>
786
- <p class=" foo bar baz foo ">test</p>
787
- <p class=" foo bar baz foo ">test</p>
788
- <p class="foo foo">test</p>
789
- <p class="">test</p>
790
- </div>
791
- eoxml
792
- div = xml.at_xpath('//div')
793
- p1, p2, p3, p4 = xml.xpath('//p')
794
-
795
- assert_same div, div.remove_class('main')
796
- assert_nil div['class']
797
-
798
- assert_same p1, p1.remove_class('bar baz')
799
- assert_equal 'foo foo', p1['class']
800
-
801
- assert_same p2, p2.remove_class()
802
- assert_nil p2['class']
803
-
804
- assert_same p3, p3.remove_class('foo')
805
- assert_nil p3['class']
806
-
807
- assert_same p4, p4.remove_class('foo')
808
- assert_nil p4['class']
809
- end
810
-
811
- def test_set_content_with_symbol
812
- node = @xml.at('//name')
813
- node.content = :foo
814
- assert_equal 'foo', node.content
815
- end
816
-
817
- def test_set_native_content_is_unescaped
818
- comment = Nokogiri.XML('<r><!-- foo --></r>').at('//comment()')
819
-
820
- comment.native_content = " < " # content= will escape this string
821
- assert_equal "<!-- < -->", comment.to_xml
822
- end
823
-
824
- def test_find_by_css_class_with_nonstandard_whitespace
825
- doc = Nokogiri::HTML "<div class='a\nb'></div>"
826
- assert_not_nil doc.at_css(".b")
827
- end
828
-
829
- def test_find_by_css_with_tilde_eql
830
- xml = Nokogiri::XML.parse(<<-eoxml)
831
- <root>
832
- <a>Hello world</a>
833
- <a class='foo bar'>Bar</a>
834
- <a class='bar foo'>Bar</a>
835
- <a class='bar'>Bar</a>
836
- <a class='baz bar foo'>Bar</a>
837
- <a class='bazbarfoo'>Awesome</a>
838
- <a class='bazbar'>Awesome</a>
839
- </root>
840
- eoxml
841
- set = xml.css('a[@class~="bar"]')
842
- assert_equal 4, set.length
843
- assert_equal ['Bar'], set.map(&:content).uniq
844
- end
845
-
846
- def test_unlink
847
- xml = Nokogiri::XML.parse(<<-eoxml)
848
- <root>
849
- <a class='foo bar'>Bar</a>
850
- <a class='bar foo'>Bar</a>
851
- <a class='bar'>Bar</a>
852
- <a>Hello world</a>
853
- <a class='baz bar foo'>Bar</a>
854
- <a class='bazbarfoo'>Awesome</a>
855
- <a class='bazbar'>Awesome</a>
856
- </root>
857
- eoxml
858
- node = xml.xpath('//a')[3]
859
- assert_equal('Hello world', node.text)
860
- assert_match(/Hello world/, xml.to_s)
861
- assert node.parent
862
- assert node.document
863
- assert node.previous_sibling
864
- assert node.next_sibling
865
- node.unlink
866
- assert !node.parent
867
- #assert !node.document
868
- assert !node.previous_sibling
869
- assert !node.next_sibling
870
- assert_no_match(/Hello world/, xml.to_s)
871
- end
872
-
873
- def test_next_sibling
874
- assert node = @xml.root
875
- assert sibling = node.child.next_sibling
876
- assert_equal('employee', sibling.name)
877
- end
878
-
879
- def test_previous_sibling
880
- assert node = @xml.root
881
- assert sibling = node.child.next_sibling
882
- assert_equal('employee', sibling.name)
883
- assert_equal(sibling.previous_sibling, node.child)
884
- end
885
-
886
- def test_name=
887
- assert node = @xml.root
888
- node.name = 'awesome'
889
- assert_equal('awesome', node.name)
890
- end
891
-
892
- def test_child
893
- assert node = @xml.root
894
- assert child = node.child
895
- assert_equal('text', child.name)
896
- end
897
-
898
- def test_key?
899
- assert node = @xml.search('//address').first
900
- assert(!node.key?('asdfasdf'))
901
- end
902
-
903
- def test_set_property
904
- assert node = @xml.search('//address').first
905
- node['foo'] = 'bar'
906
- assert_equal('bar', node['foo'])
907
- end
908
-
909
- def test_set_property_non_string
910
- assert node = @xml.search('//address').first
911
- node['foo'] = 1
912
- assert_equal('1', node['foo'])
913
- node['foo'] = false
914
- assert_equal('false', node['foo'])
915
- end
916
-
917
- def test_attributes
918
- assert node = @xml.search('//address').first
919
- assert_nil(node['asdfasdfasdf'])
920
- assert_equal('Yes', node['domestic'])
921
-
922
- assert node = @xml.search('//address')[2]
923
- attr = node.attributes
924
- assert_equal 2, attr.size
925
- assert_equal 'Yes', attr['domestic'].value
926
- assert_equal 'Yes', attr['domestic'].to_s
927
- assert_equal 'No', attr['street'].value
928
- end
929
-
930
- def test_path
931
- assert set = @xml.search('//employee')
932
- assert node = set.first
933
- assert_equal('/staff/employee[1]', node.path)
934
- end
935
-
936
- def test_parent_xpath
937
- assert set = @xml.search('//employee')
938
- assert node = set.first
939
- assert parent_set = node.search('..')
940
- assert parent_node = parent_set.first
941
- assert_equal '/staff', parent_node.path
942
- assert_equal node.parent, parent_node
943
- end
944
-
945
- def test_search_self
946
- node = @xml.at('//employee')
947
- assert_equal node.search('.').to_a, [node]
948
- end
949
-
950
- def test_search_by_symbol
951
- assert set = @xml.search(:employee)
952
- assert 5, set.length
953
-
954
- assert node = @xml.at(:employee)
955
- assert node.text =~ /EMP0001/
956
- end
957
-
958
- def test_new_node
959
- node = Nokogiri::XML::Node.new('form', @xml)
960
- assert_equal('form', node.name)
961
- assert(node.document)
962
- end
963
-
964
- def test_encode_special_chars
965
- foo = @xml.css('employee').first.encode_special_chars('&')
966
- assert_equal '&amp;', foo
967
- end
968
-
969
- def test_content_equals
970
- node = Nokogiri::XML::Node.new('form', @xml)
971
- assert_equal('', node.content)
972
-
973
- node.content = 'hello world!'
974
- assert_equal('hello world!', node.content)
975
-
976
- node.content = '& <foo> &amp;'
977
- assert_equal('& <foo> &amp;', node.content)
978
- assert_equal('<form>&amp; &lt;foo&gt; &amp;amp;</form>', node.to_xml)
979
-
980
- node.content = "1234 <-> 1234"
981
- assert_equal "1234 <-> 1234", node.content
982
- assert_equal "<form>1234 &lt;-&gt; 1234</form>", node.to_xml
983
-
984
- node.content = '1234'
985
- node.add_child '<foo>5678</foo>'
986
- assert_equal '12345678', node.content
987
- end
988
-
989
- # issue #839
990
- def test_encoding_of_copied_nodes
991
- d1 = Nokogiri::XML('<r><a>&amp;</a></r>')
992
- d2 = Nokogiri::XML('<r></r>')
993
- ne = d1.root.xpath('//a').first.dup(1)
994
- ne.content += "& < & > \" &"
995
- d2.root << ne
996
- assert_match(/<a>&amp;&amp; &lt; &amp; &gt; \" &amp;<\/a>/, d2.to_s)
997
- end
998
-
999
- def test_content_after_appending_text
1000
- doc = Nokogiri::XML '<foo />'
1001
- node = doc.children.first
1002
- node.content = 'bar'
1003
- node << 'baz'
1004
- assert_equal 'barbaz', node.content
1005
- end
1006
-
1007
- def test_content_depth_first
1008
- node = Nokogiri::XML '<foo>first<baz>second</baz>third</foo>'
1009
- assert_equal 'firstsecondthird', node.content
1010
- end
1011
-
1012
- def test_set_content_should_unlink_existing_content
1013
- node = @xml.at_css("employee")
1014
- children = node.children
1015
- node.content = "hello"
1016
- children.each { |child| assert_nil child.parent }
1017
- end
1018
-
1019
- def test_whitespace_nodes
1020
- doc = Nokogiri::XML.parse("<root><b>Foo</b>\n<i>Bar</i> <p>Bazz</p></root>")
1021
- children = doc.at('//root').children.collect(&:to_s)
1022
- assert_equal "\n", children[1]
1023
- assert_equal " ", children[3]
1024
- end
1025
-
1026
- def test_node_equality
1027
- doc1 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
1028
- doc2 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
1029
-
1030
- address1_1 = doc1.xpath('//address').first
1031
- address1_2 = doc1.xpath('//address').first
1032
-
1033
- address2 = doc2.xpath('//address').first
1034
-
1035
- assert_not_equal address1_1, address2 # two references to very, very similar nodes
1036
- assert_equal address1_1, address1_2 # two references to the exact same node
1037
- end
1038
-
1039
- def test_namespace_search_with_xpath_and_hash
1040
- xml = Nokogiri::XML.parse(<<-eoxml)
1041
- <root>
1042
- <car xmlns:part="http://general-motors.com/">
1043
- <part:tire>Michelin Model XGV</part:tire>
1044
- </car>
1045
- <bicycle xmlns:part="http://schwinn.com/">
1046
- <part:tire>I'm a bicycle tire!</part:tire>
1047
- </bicycle>
1048
- </root>
1049
- eoxml
1050
-
1051
- tires = xml.xpath('//bike:tire', {'bike' => 'http://schwinn.com/'})
1052
- assert_equal 1, tires.length
1053
- end
1054
-
1055
- def test_namespace_search_with_xpath_and_hash_with_symbol_keys
1056
- xml = Nokogiri::XML.parse(<<-eoxml)
1057
- <root>
1058
- <car xmlns:part="http://general-motors.com/">
1059
- <part:tire>Michelin Model XGV</part:tire>
1060
- </car>
1061
- <bicycle xmlns:part="http://schwinn.com/">
1062
- <part:tire>I'm a bicycle tire!</part:tire>
1063
- </bicycle>
1064
- </root>
1065
- eoxml
1066
-
1067
- tires = xml.xpath('//bike:tire', :bike => 'http://schwinn.com/')
1068
- assert_equal 1, tires.length
1069
- end
1070
-
1071
- def test_namespace_search_with_css
1072
- xml = Nokogiri::XML.parse(<<-eoxml)
1073
- <root>
1074
- <car xmlns:part="http://general-motors.com/">
1075
- <part:tire>Michelin Model XGV</part:tire>
1076
- </car>
1077
- <bicycle xmlns:part="http://schwinn.com/">
1078
- <part:tire>I'm a bicycle tire!</part:tire>
1079
- </bicycle>
1080
- </root>
1081
- eoxml
1082
-
1083
- tires = xml.css('bike|tire', 'bike' => 'http://schwinn.com/' )
1084
- assert_equal 1, tires.length
1085
- end
1086
-
1087
- def test_namespaced_attribute_search_with_xpath
1088
- # from #593
1089
- xmlContent = <<-EOXML
1090
- <?xml version="1.0"?>
1091
- <ns1:el1 xmlns:ns1="http://blabla.com" >
1092
- <ns1:el2 ns1:att="123">with namespace</ns1:el2 >
1093
- <ns1:el2 att="noNameSpace">no namespace</ns1:el2 >
1094
- </ns1:el1>
1095
- EOXML
1096
- xml_doc = Nokogiri::XML(xmlContent)
1097
-
1098
- no_ns = xml_doc.xpath("//*[@att]")
1099
- assert_equal no_ns.length, 1
1100
- assert_equal no_ns.first.content, "no namespace"
1101
-
1102
- with_ns = xml_doc.xpath("//*[@ns1:att]")
1103
- assert_equal with_ns.length, 1
1104
- assert_equal with_ns.first.content, "with namespace"
1105
- end
1106
-
1107
- def test_namespaced_attribute_search_with_css
1108
- # from #593
1109
- xmlContent = <<-EOXML
1110
- <?xml version="1.0"?>
1111
- <ns1:el1 xmlns:ns1="http://blabla.com" >
1112
- <ns1:el2 ns1:att="123">with namespace</ns1:el2 >
1113
- <ns1:el2 att="noNameSpace">no namespace</ns1:el2 >
1114
- </ns1:el1>
1115
- EOXML
1116
- xml_doc = Nokogiri::XML(xmlContent)
1117
-
1118
- no_ns = xml_doc.css('*[att]')
1119
- assert_equal no_ns.length, 1
1120
- assert_equal no_ns.first.content, "no namespace"
1121
-
1122
- with_ns = xml_doc.css('*[ns1|att]')
1123
- assert_equal with_ns.length, 1
1124
- assert_equal with_ns.first.content, "with namespace"
1125
- end
1126
-
1127
- def test_namespaces_should_include_all_namespace_definitions
1128
- xml = Nokogiri::XML.parse(<<-EOF)
1129
- <x xmlns="http://quux.com/" xmlns:a="http://foo.com/" xmlns:b="http://bar.com/">
1130
- <y xmlns:c="http://bazz.com/">
1131
- <z>hello</z>
1132
- <a xmlns:c="http://newc.com/" />
1133
- </y>
1134
- </x>
1135
- EOF
1136
-
1137
- namespaces = xml.namespaces # Document#namespace
1138
- assert_equal({"xmlns" => "http://quux.com/",
1139
- "xmlns:a" => "http://foo.com/",
1140
- "xmlns:b" => "http://bar.com/"}, namespaces)
1141
-
1142
- namespaces = xml.root.namespaces
1143
- assert_equal({"xmlns" => "http://quux.com/",
1144
- "xmlns:a" => "http://foo.com/",
1145
- "xmlns:b" => "http://bar.com/"}, namespaces)
1146
-
1147
- namespaces = xml.at_xpath("//xmlns:y").namespaces
1148
- assert_equal({"xmlns" => "http://quux.com/",
1149
- "xmlns:a" => "http://foo.com/",
1150
- "xmlns:b" => "http://bar.com/",
1151
- "xmlns:c" => "http://bazz.com/"}, namespaces)
1152
-
1153
- namespaces = xml.at_xpath("//xmlns:z").namespaces
1154
- assert_equal({"xmlns" => "http://quux.com/",
1155
- "xmlns:a" => "http://foo.com/",
1156
- "xmlns:b" => "http://bar.com/",
1157
- "xmlns:c" => "http://bazz.com/"}, namespaces)
1158
-
1159
- namespaces = xml.at_xpath("//xmlns:a").namespaces
1160
- assert_equal({"xmlns" => "http://quux.com/",
1161
- "xmlns:a" => "http://foo.com/",
1162
- "xmlns:b" => "http://bar.com/",
1163
- "xmlns:c" => "http://newc.com/"}, namespaces)
1164
- end
1165
-
1166
- def test_namespace
1167
- xml = Nokogiri::XML.parse(<<-EOF)
1168
- <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
1169
- <y xmlns:c='http://bazz.com/'>
1170
- <a:div>hello a</a:div>
1171
- <b:div>hello b</b:div>
1172
- <c:div x="1" b:y="2">hello c</c:div>
1173
- <div x="1" xmlns="http://ns.example.com/d"/>
1174
- <div x="1">hello moon</div>
1175
- </y>
1176
- </x>
1177
- EOF
1178
- set = xml.search("//y/*")
1179
- assert_equal "a", set[0].namespace.prefix
1180
- assert_equal 'http://foo.com/', set[0].namespace.href
1181
- assert_equal "b", set[1].namespace.prefix
1182
- assert_equal 'http://bar.com/', set[1].namespace.href
1183
- assert_equal "c", set[2].namespace.prefix
1184
- assert_equal 'http://bazz.com/', set[2].namespace.href
1185
- assert_nil set[3].namespace.prefix # default namespace
1186
- assert_equal 'http://ns.example.com/d', set[3].namespace.href
1187
- assert_nil set[4].namespace # no namespace
1188
-
1189
- assert_equal 'b', set[2].attributes['y'].namespace.prefix
1190
- assert_equal 'http://bar.com/', set[2].attributes['y'].namespace.href
1191
- assert_nil set[2].attributes['x'].namespace
1192
- assert_nil set[3].attributes['x'].namespace
1193
- assert_nil set[4].attributes['x'].namespace
1194
- end
1195
-
1196
- if Nokogiri.uses_libxml?
1197
- def test_namespace_without_an_href_on_html_node
1198
- # because microsoft word's HTML formatting does this. ick.
1199
- xml = Nokogiri::HTML.parse <<-EOF
1200
- <div><o:p>foo</o:p></div>
1201
- EOF
1202
-
1203
- assert_not_nil(node = xml.at('p'))
1204
-
1205
- assert_equal 1, node.namespaces.keys.size
1206
- assert node.namespaces.has_key?('xmlns:o')
1207
- assert_nil node.namespaces['xmlns:o']
1208
- end
1209
- end
1210
-
1211
- def test_line
1212
- xml = Nokogiri::XML(<<-eoxml)
1213
- <root>
1214
- <a>
1215
- Hello world
1216
- </a>
1217
- </root>
1218
- eoxml
1219
-
1220
- set = xml.search("//a")
1221
- node = set.first
1222
- assert_equal 2, node.line
1223
- end
1224
-
1225
- def test_xpath_results_have_document_and_are_decorated
1226
- x = Module.new do
1227
- def awesome! ; end
1228
- end
1229
- util_decorate(@xml, x)
1230
- node_set = @xml.xpath("//employee")
1231
- assert_equal @xml, node_set.document
1232
- assert node_set.respond_to?(:awesome!)
1233
- end
1234
-
1235
- def test_css_results_have_document_and_are_decorated
1236
- x = Module.new do
1237
- def awesome! ; end
1238
- end
1239
- util_decorate(@xml, x)
1240
- node_set = @xml.css("employee")
1241
- assert_equal @xml, node_set.document
1242
- assert node_set.respond_to?(:awesome!)
1243
- end
1244
-
1245
- def test_blank
1246
- doc = Nokogiri::XML('')
1247
- assert_equal false, doc.blank?
1248
- end
1249
-
1250
- def test_to_xml_allows_to_serialize_with_as_xml_save_option
1251
- xml = Nokogiri::XML("<root><ul><li>Hello world</li></ul></root>")
1252
- set = xml.search("//ul")
1253
- node = set.first
1254
-
1255
- assert_no_match("<ul>\n <li>", xml.to_xml(:save_with => XML::Node::SaveOptions::AS_XML))
1256
- assert_no_match("<ul>\n <li>", node.to_xml(:save_with => XML::Node::SaveOptions::AS_XML))
1257
- end
1258
-
1259
- # issue 647
1260
- def test_default_namespace_should_be_created
1261
- subject = Nokogiri::XML.parse('<foo xml:bar="http://bar.com"/>').root
1262
- ns = subject.attributes['bar'].namespace
1263
- assert_not_nil ns
1264
- assert_equal ns.class, Nokogiri::XML::Namespace
1265
- assert_equal 'xml', ns.prefix
1266
- assert_equal "http://www.w3.org/XML/1998/namespace", ns.href
1267
- end
1268
-
1269
- # issue 648
1270
- def test_namespace_without_prefix_should_be_set
1271
- node = Nokogiri::XML.parse('<foo xmlns="http://bar.com"/>').root
1272
- subject = Nokogiri::XML::Node.new 'foo', node.document
1273
- subject.namespace = node.namespace
1274
- ns = subject.namespace
1275
- assert_equal ns.class, Nokogiri::XML::Namespace
1276
- assert_nil ns.prefix
1277
- assert_equal ns.href, "http://bar.com"
1278
- end
1279
-
1280
- # issue 695
1281
- def test_namespace_in_rendered_xml
1282
- document = Nokogiri::XML::Document.new
1283
- subject = Nokogiri::XML::Node.new 'foo', document
1284
- ns = subject.add_namespace nil, 'bar'
1285
- subject.namespace = ns
1286
- assert_match(/xmlns="bar"/, subject.to_xml)
1287
- end
1288
-
1289
- # issue 771
1290
- def test_format_noblank
1291
- content = <<eoxml
1292
- <foo>
1293
- <bar>hello</bar>
1294
- </foo>
1295
- eoxml
1296
- subject = Nokogiri::XML(content) do |conf|
1297
- conf.default_xml.noblanks
1298
- end
1299
-
1300
- assert_match %r{<bar>hello</bar>}, subject.to_xml(:indent => 2)
1301
- end
1302
-
1303
- def test_text_node_colon
1304
- document = Nokogiri::XML::Document.new
1305
- root = Nokogiri::XML::Node.new 'foo', document
1306
- document.root = root
1307
- root << "<a>hello:with_colon</a>"
1308
- assert_match(/hello:with_colon/, document.to_xml)
1309
- end
1310
-
1311
- def test_document_eh
1312
- html_doc = Nokogiri::HTML "<div>foo</div>"
1313
- xml_doc = Nokogiri::XML "<div>foo</div>"
1314
- html_node = html_doc.at_css "div"
1315
- xml_node = xml_doc.at_css "div"
1316
-
1317
- assert html_doc.document?
1318
- assert xml_doc.document?
1319
- assert ! html_node.document?
1320
- assert ! xml_node.document?
1321
- end
1322
-
1323
- def test_processing_instruction_eh
1324
- xml_doc = Nokogiri::XML %Q{<?xml version="1.0"?>\n<?xml-stylesheet type="text/xsl" href="foo.xsl"?>\n<?xml-stylesheet type="text/xsl" href="foo2.xsl"?>\n<root><div>foo</div></root>}
1325
- pi_node = xml_doc.children.first
1326
- div_node = xml_doc.at_css "div"
1327
- assert pi_node.processing_instruction?
1328
- assert ! div_node.processing_instruction?
1329
- end
1330
-
1331
- def test_node_lang
1332
- document = Nokogiri::XML <<-EOXML
1333
- <root>
1334
- <div class='english' xml:lang='en'>
1335
- <div class='english_child'>foo</div>
1336
- </div>
1337
- <div class='japanese' xml:lang='jp'>bar</div>
1338
- <div class='unspecified'>bar</div>
1339
- </root>
1340
- EOXML
1341
- assert_equal "en", document.at_css(".english").lang
1342
- assert_equal "en", document.at_css(".english_child").lang
1343
- assert_equal "jp", document.at_css(".japanese").lang
1344
- assert_nil document.at_css(".unspecified").lang
1345
- end
1346
-
1347
- def test_set_node_lang
1348
- document = Nokogiri::XML "<root><div class='subject'>foo</div></root>"
1349
- subject = document.at_css(".subject")
1350
-
1351
- subject.lang = "de"
1352
- assert_equal "de", subject.lang
1353
-
1354
- subject.lang = "fr"
1355
- assert_equal "fr", subject.lang
1356
- end
1357
-
1358
- def test_text_node_robustness_gh1426
1359
- # notably, the original bug report was about libxml-ruby interactions
1360
- # this test should blow up under valgrind if we regress on libxml-ruby workarounds
1361
- message = "<h2>BOOM!</h2>"
1362
- 10_000.times do
1363
- node = Nokogiri::HTML::DocumentFragment.parse(message)
1364
- node.add_previous_sibling(Nokogiri::XML::Text.new('before', node.document))
1365
- node.add_next_sibling(Nokogiri::XML::Text.new('after', node.document))
1366
- end
1367
- end
1368
-
1369
- def test_wrap
1370
- xml = '<root><thing><div class="title">important thing</div></thing></root>'
1371
- doc = Nokogiri::XML(xml)
1372
- thing = doc.at_css("thing")
1373
- thing.wrap("<wrapper/>")
1374
- assert_equal 'wrapper', thing.parent.name
1375
- assert_equal 'thing', doc.at_css("wrapper").children.first.name
1376
- end
1377
- end
1378
- end
1379
- end