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,180 +0,0 @@
1
- require "helper"
2
-
3
- module Nokogiri
4
- module XML
5
- class TestC14N < Nokogiri::TestCase
6
- # http://www.w3.org/TR/xml-c14n#Example-OutsideDoc
7
- def test_3_1
8
- doc = Nokogiri.XML <<-eoxml
9
- <?xml version="1.0"?>
10
-
11
- <?xml-stylesheet href="doc.xsl"
12
- type="text/xsl" ?>
13
-
14
- <!DOCTYPE doc SYSTEM "doc.dtd">
15
-
16
- <doc>Hello, world!<!-- Comment 1 --></doc>
17
-
18
- <?pi-without-data ?>
19
-
20
- <!-- Comment 2 -->
21
-
22
- <!-- Comment 3 -->
23
- eoxml
24
-
25
- c14n = doc.canonicalize
26
- assert_no_match(/version=/, c14n)
27
- assert_match(/Hello, world/, c14n)
28
- assert_no_match(/Comment/, c14n)
29
- c14n = doc.canonicalize(nil, nil, true)
30
- assert_match(/Comment/, c14n)
31
- c14n = doc.canonicalize(nil, nil, false)
32
- assert_no_match(/Comment/, c14n)
33
- end
34
-
35
- def test_exclude_block_params
36
- xml = '<a><b></b></a>'
37
- doc = Nokogiri.XML xml
38
-
39
- list = []
40
- doc.canonicalize do |node, parent|
41
- list << [node, parent]
42
- true
43
- end
44
- if Nokogiri.jruby?
45
- assert_equal(
46
- ['a', 'document', 'document', nil, 'b', 'a'],
47
- list.flatten.map { |x| x ? x.name : x }
48
- )
49
- else
50
- assert_equal(
51
- ['a', 'document', 'document', nil, 'b', 'a', 'a', 'document'],
52
- list.flatten.map { |x| x ? x.name : x }
53
- )
54
- end
55
- end
56
-
57
- def test_exclude_block_true
58
- xml = '<a><b></b></a>'
59
- doc = Nokogiri.XML xml
60
-
61
- c14n = doc.canonicalize do |node, parent|
62
- true
63
- end
64
- assert_equal xml, c14n
65
- end
66
-
67
- def test_exclude_block_false
68
- xml = '<a><b></b></a>'
69
- doc = Nokogiri.XML xml
70
-
71
- c14n = doc.canonicalize do |node, parent|
72
- false
73
- end
74
- assert_equal '', c14n
75
- end
76
-
77
- def test_exclude_block_nil
78
- xml = '<a><b></b></a>'
79
- doc = Nokogiri.XML xml
80
-
81
- c14n = doc.canonicalize do |node, parent|
82
- nil
83
- end
84
- assert_equal '', c14n
85
- end
86
-
87
- def test_exclude_block_object
88
- xml = '<a><b></b></a>'
89
- doc = Nokogiri.XML xml
90
-
91
- c14n = doc.canonicalize do |node, parent|
92
- Object.new
93
- end
94
- assert_equal xml, c14n
95
- end
96
-
97
- def test_c14n_node
98
- xml = '<a><b><c></c></b></a>'
99
- doc = Nokogiri.XML xml
100
- c14n = doc.at_xpath('//b').canonicalize
101
- assert_equal '<b><c></c></b>', c14n
102
- end
103
-
104
- def test_c14n_modes
105
- # http://www.w3.org/TR/xml-exc-c14n/#sec-Enveloping
106
-
107
- doc1 = Nokogiri.XML <<-EOXML
108
- <n0:local xmlns:n0="http://foobar.org" xmlns:n3="ftp://example.org">
109
- <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
110
- <n3:stuff xmlns:n3="ftp://example.org"/>
111
- </n1:elem2>
112
- </n0:local>
113
- EOXML
114
-
115
- doc2 = Nokogiri.XML <<-EOXML
116
- <n2:pdu xmlns:n1="http://example.com"
117
- xmlns:n2="http://foo.example"
118
- xmlns:n4="http://foo.example"
119
- xml:lang="fr"
120
- xml:space="retain">
121
- <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
122
- <n3:stuff xmlns:n3="ftp://example.org"/>
123
- <n4:stuff />
124
- </n1:elem2>
125
- </n2:pdu>
126
- EOXML
127
-
128
- c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize
129
- assert_equal '<n1:elem2 xmlns:n0="http://foobar.org" xmlns:n1="http://example.net" xmlns:n3="ftp://example.org" xml:lang="en">
130
- <n3:stuff></n3:stuff>
131
- </n1:elem2>', c14n
132
-
133
- expected = '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain">
134
- <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
135
- <n4:stuff></n4:stuff>
136
- </n1:elem2>'
137
- c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize
138
- assert_equal expected, c14n
139
-
140
- expected = '<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
141
- <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
142
- </n1:elem2>'
143
- c14n = doc1.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0)
144
- assert_equal expected, c14n
145
-
146
- expected = '<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
147
- <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
148
- <n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
149
- </n1:elem2>'
150
- c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0)
151
- assert_equal expected, c14n
152
-
153
- expected = '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en">
154
- <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
155
- <n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
156
- </n1:elem2>'
157
- c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0, ['n2'])
158
- assert_equal expected, c14n
159
-
160
- expected = '<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en">
161
- <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
162
- <n4:stuff></n4:stuff>
163
- </n1:elem2>'
164
- c14n = doc2.at_xpath('//n1:elem2', {'n1' => 'http://example.net'}).canonicalize(XML::XML_C14N_EXCLUSIVE_1_0, ['n2', 'n4'])
165
- assert_equal expected, c14n
166
- end
167
-
168
- def test_wrong_params
169
- xml = '<a><b></b></a>'
170
- doc = Nokogiri.XML xml
171
-
172
- assert_raise(TypeError){ doc.canonicalize :wrong_type }
173
- assert_raise(TypeError){ doc.canonicalize nil, :wrong_type }
174
- doc.canonicalize nil, nil, :wrong_type
175
- end
176
-
177
-
178
- end
179
- end
180
- end
@@ -1,54 +0,0 @@
1
- require "helper"
2
-
3
- module Nokogiri
4
- module XML
5
- class TestCDATA < Nokogiri::TestCase
6
- def setup
7
- super
8
- @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
9
- end
10
-
11
- def test_cdata_node
12
- name = @xml.xpath('//employee[2]/name').first
13
- assert cdata = name.children[1]
14
- assert cdata.cdata?
15
- assert_equal '#cdata-section', cdata.name
16
- end
17
-
18
- def test_new
19
- node = CDATA.new(@xml, "foo")
20
- assert_equal "foo", node.content
21
-
22
- node = CDATA.new(@xml.root, "foo")
23
- assert_equal "foo", node.content
24
- end
25
-
26
- def test_new_with_nil
27
- node = CDATA.new(@xml, nil)
28
- assert_nil node.content
29
- end
30
-
31
- def test_new_with_non_string
32
- assert_raises(TypeError) do
33
- CDATA.new(@xml, 1.234)
34
- end
35
- end
36
-
37
- def test_lots_of_new_cdata
38
- assert 100.times { CDATA.new(@xml, "asdfasdf") }
39
- end
40
-
41
- def test_content=
42
- node = CDATA.new(@xml, 'foo')
43
- assert_equal('foo', node.content)
44
-
45
- node.content = '& <foo> &amp;'
46
- assert_equal('& <foo> &amp;', node.content)
47
- assert_equal('<![CDATA[& <foo> &amp;]]>', node.to_xml)
48
-
49
- node.content = 'foo ]]> bar'
50
- assert_equal('foo ]]> bar', node.content)
51
- end
52
- end
53
- end
54
- end
@@ -1,40 +0,0 @@
1
- require "helper"
2
-
3
- module Nokogiri
4
- module XML
5
- class TestComment < Nokogiri::TestCase
6
- def setup
7
- super
8
- @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
9
- end
10
-
11
- def test_new
12
- comment = Nokogiri::XML::Comment.new(@xml, 'hello world')
13
- assert_equal('<!--hello world-->', comment.to_s)
14
- end
15
-
16
- def test_comment?
17
- comment = Nokogiri::XML::Comment.new(@xml, 'hello world')
18
- assert(comment.comment?)
19
- assert(!@xml.root.comment?)
20
- end
21
-
22
- def test_passing_a_node_uses_the_node_document
23
- comment = Nokogiri::XML::Comment.new(@xml.at_css("employee"), 'hello world')
24
- assert_equal @xml, comment.document
25
- end
26
-
27
- def test_passing_anything_else
28
- assert_raises ArgumentError do
29
- Nokogiri::XML::Comment.new("NOT A NOKOGIRI CLASS", 'hello world')
30
- end
31
- end
32
-
33
- def test_many_comments
34
- 100.times {
35
- Nokogiri::XML::Comment.new(@xml, 'hello world')
36
- }
37
- end
38
- end
39
- end
40
- end
@@ -1,982 +0,0 @@
1
- require "helper"
2
-
3
- require 'uri'
4
-
5
- module Nokogiri
6
- module XML
7
- class TestDocument < Nokogiri::TestCase
8
- URI = if URI.const_defined?(:DEFAULT_PARSER)
9
- ::URI::DEFAULT_PARSER
10
- else
11
- ::URI
12
- end
13
-
14
- def setup
15
- super
16
- @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
17
- end
18
-
19
- def test_dtd_with_empty_internal_subset
20
- doc = Nokogiri::XML <<-eoxml
21
- <?xml version="1.0"?>
22
- <!DOCTYPE people >
23
- <people>
24
- </people>
25
- eoxml
26
- assert doc.root
27
- end
28
-
29
- # issue #1005
30
- def test_strict_parsing_empty_doc_should_raise_exception
31
- ["", " "].each do |empty_string|
32
- assert_raises(SyntaxError, "empty string '#{empty_string}' should raise a SyntaxError") do
33
- Nokogiri::XML(empty_string) { |c| c.strict }
34
- end
35
- assert_raises(SyntaxError, "StringIO of '#{empty_string}' should raise a SyntaxError") do
36
- Nokogiri::XML(StringIO.new(empty_string)) { |c| c.strict }
37
- end
38
- end
39
- end
40
-
41
- # issue #838
42
- def test_document_with_invalid_prolog
43
- doc = Nokogiri::XML '<? ?>'
44
- assert_empty doc.content
45
- end
46
-
47
- # issue #837
48
- def test_document_with_refentity
49
- doc = Nokogiri::XML '&amp;'
50
- assert_equal '', doc.content
51
- end
52
-
53
- # issue #835
54
- def test_manually_adding_reference_entities
55
- d = Nokogiri::XML::Document.new
56
- root = Nokogiri::XML::Element.new('bar', d)
57
- txt = Nokogiri::XML::Text.new('foo', d)
58
- ent = Nokogiri::XML::EntityReference.new(d, '#8217')
59
- root << txt
60
- root << ent
61
- d << root
62
- assert_match(/&#8217;/, d.to_html)
63
- end
64
-
65
- def test_document_with_initial_space
66
- doc = Nokogiri::XML(" <?xml version='1.0' encoding='utf-8' ?><first \>")
67
- assert_equal 2, doc.children.size
68
- end
69
-
70
- def test_root_set_to_nil
71
- @xml.root = nil
72
- assert_nil @xml.root
73
- end
74
-
75
- def test_million_laugh_attach
76
- doc = Nokogiri::XML '<?xml version="1.0"?>
77
- <!DOCTYPE lolz [
78
- <!ENTITY lol "lol">
79
- <!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
80
- <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
81
- <!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
82
- <!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
83
- <!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
84
- <!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
85
- <!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
86
- <!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
87
- ]>
88
- <lolz>&lol9;</lolz>'
89
- assert_not_nil doc
90
- end
91
-
92
- def test_million_laugh_attach_2
93
- doc = Nokogiri::XML '<?xml version="1.0" encoding="UTF-8"?>
94
- <!DOCTYPE member [
95
- <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
96
- <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
97
- <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
98
- <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
99
- <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
100
- <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
101
- <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
102
- ]>
103
- <member>
104
- &a;
105
- </member>'
106
- assert_not_nil doc
107
- end
108
-
109
- def test_ignore_unknown_namespace
110
- doc = Nokogiri::XML(<<-eoxml)
111
- <xml>
112
- <unknown:foo xmlns='http://hello.com/' />
113
- <bar />
114
- </xml>
115
- eoxml
116
- if Nokogiri.jruby?
117
- refute doc.xpath('//foo').first.namespace # assert that the namespace is nil
118
- end
119
- refute_empty doc.xpath('//bar'), "bar wasn't found in the document" # bar should be part of the doc
120
- end
121
-
122
- def test_collect_namespaces
123
- doc = Nokogiri::XML(<<-eoxml)
124
- <xml>
125
- <foo xmlns='hello'>
126
- <bar xmlns:foo='world' />
127
- </foo>
128
- </xml>
129
- eoxml
130
- assert_equal({"xmlns"=>"hello", "xmlns:foo"=>"world"},
131
- doc.collect_namespaces)
132
- end
133
-
134
- def test_subclass_initialize_modify # testing a segv
135
- Class.new(Nokogiri::XML::Document) {
136
- def initialize
137
- super
138
- body_node = Nokogiri::XML::Node.new "body", self
139
- body_node.content = "stuff"
140
- self.root = body_node
141
- end
142
- }.new
143
- end
144
-
145
- def test_create_text_node
146
- txt = @xml.create_text_node 'foo'
147
- assert_instance_of Nokogiri::XML::Text, txt
148
- assert_equal 'foo', txt.text
149
- assert_equal @xml, txt.document
150
- end
151
-
152
- def test_create_text_node_with_block
153
- @xml.create_text_node 'foo' do |txt|
154
- assert_instance_of Nokogiri::XML::Text, txt
155
- assert_equal 'foo', txt.text
156
- assert_equal @xml, txt.document
157
- end
158
- end
159
-
160
- def test_create_element
161
- elm = @xml.create_element('foo')
162
- assert_instance_of Nokogiri::XML::Element, elm
163
- assert_equal 'foo', elm.name
164
- assert_equal @xml, elm.document
165
- end
166
-
167
- def test_create_element_with_block
168
- @xml.create_element('foo') do |elm|
169
- assert_instance_of Nokogiri::XML::Element, elm
170
- assert_equal 'foo', elm.name
171
- assert_equal @xml, elm.document
172
- end
173
- end
174
-
175
- def test_create_element_with_attributes
176
- elm = @xml.create_element('foo',:a => "1")
177
- assert_instance_of Nokogiri::XML::Element, elm
178
- assert_instance_of Nokogiri::XML::Attr, elm.attributes["a"]
179
- assert_equal "1", elm["a"]
180
- end
181
-
182
- def test_create_element_with_namespace
183
- elm = @xml.create_element('foo',:'xmlns:foo' => 'http://tenderlovemaking.com')
184
- assert_equal 'http://tenderlovemaking.com', elm.namespaces['xmlns:foo']
185
- end
186
-
187
- def test_create_element_with_hyphenated_namespace
188
- elm = @xml.create_element('foo',:'xmlns:SOAP-ENC' => 'http://tenderlovemaking.com')
189
- assert_equal 'http://tenderlovemaking.com', elm.namespaces['xmlns:SOAP-ENC']
190
- end
191
-
192
- def test_create_element_with_content
193
- elm = @xml.create_element('foo',"needs more xml/violence")
194
- assert_equal "needs more xml/violence", elm.content
195
- end
196
-
197
- def test_create_cdata
198
- cdata = @xml.create_cdata("abc")
199
- assert_instance_of Nokogiri::XML::CDATA, cdata
200
- assert_equal "abc", cdata.content
201
- end
202
-
203
- def test_create_cdata_with_block
204
- @xml.create_cdata("abc") do |cdata|
205
- assert_instance_of Nokogiri::XML::CDATA, cdata
206
- assert_equal "abc", cdata.content
207
- end
208
- end
209
-
210
- def test_create_comment
211
- comment = @xml.create_comment("abc")
212
- assert_instance_of Nokogiri::XML::Comment, comment
213
- assert_equal "abc", comment.content
214
- end
215
-
216
- def test_create_comment_with_block
217
- @xml.create_comment("abc") do |comment|
218
- assert_instance_of Nokogiri::XML::Comment, comment
219
- assert_equal "abc", comment.content
220
- end
221
- end
222
-
223
- def test_pp
224
- out = StringIO.new(String.new)
225
- ::PP.pp @xml, out
226
- assert_operator out.string.length, :>, 0
227
- end
228
-
229
- def test_create_internal_subset_on_existing_subset
230
- assert_not_nil @xml.internal_subset
231
- assert_raises(RuntimeError) do
232
- @xml.create_internal_subset('staff', nil, 'staff.dtd')
233
- end
234
- end
235
-
236
- def test_create_internal_subset
237
- xml = Nokogiri::XML('<root />')
238
- assert_nil xml.internal_subset
239
-
240
- xml.create_internal_subset('name', nil, 'staff.dtd')
241
- ss = xml.internal_subset
242
- assert_equal 'name', ss.name
243
- assert_nil ss.external_id
244
- assert_equal 'staff.dtd', ss.system_id
245
- end
246
-
247
- def test_external_subset
248
- assert_nil @xml.external_subset
249
- Dir.chdir(ASSETS_DIR) do
250
- @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
251
- cfg.dtdload
252
- }
253
- end
254
- assert @xml.external_subset
255
- end
256
-
257
- def test_create_external_subset_fails_with_existing_subset
258
- assert_nil @xml.external_subset
259
- Dir.chdir(ASSETS_DIR) do
260
- @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
261
- cfg.dtdload
262
- }
263
- end
264
- assert @xml.external_subset
265
-
266
- assert_raises(RuntimeError) do
267
- @xml.create_external_subset('staff', nil, 'staff.dtd')
268
- end
269
- end
270
-
271
- def test_create_external_subset
272
- dtd = @xml.create_external_subset('staff', nil, 'staff.dtd')
273
- assert_nil dtd.external_id
274
- assert_equal 'staff.dtd', dtd.system_id
275
- assert_equal 'staff', dtd.name
276
- assert_equal dtd, @xml.external_subset
277
- end
278
-
279
- def test_version
280
- assert_equal '1.0', @xml.version
281
- end
282
-
283
- def test_add_namespace
284
- assert_raise NoMethodError do
285
- @xml.add_namespace('foo', 'bar')
286
- end
287
- end
288
-
289
- def test_attributes
290
- assert_raise NoMethodError do
291
- @xml.attributes
292
- end
293
- end
294
-
295
- def test_namespace
296
- assert_raise NoMethodError do
297
- @xml.namespace
298
- end
299
- end
300
-
301
- def test_namespace_definitions
302
- assert_raise NoMethodError do
303
- @xml.namespace_definitions
304
- end
305
- end
306
-
307
- def test_line
308
- assert_raise NoMethodError do
309
- @xml.line
310
- end
311
- end
312
-
313
- def test_empty_node_converted_to_html_is_not_self_closing
314
- doc = Nokogiri::XML('<a></a>')
315
- assert_equal "<a></a>", doc.inner_html
316
- end
317
-
318
- def test_fragment
319
- fragment = @xml.fragment
320
- assert_equal 0, fragment.children.length
321
- end
322
-
323
- def test_add_child_fragment_with_single_node
324
- doc = Nokogiri::XML::Document.new
325
- fragment = doc.fragment('<hello />')
326
- doc.add_child fragment
327
- assert_equal '/hello', doc.at('//hello').path
328
- assert_equal 'hello', doc.root.name
329
- end
330
-
331
- def test_add_child_fragment_with_multiple_nodes
332
- doc = Nokogiri::XML::Document.new
333
- fragment = doc.fragment('<hello /><goodbye />')
334
- assert_raises(RuntimeError) do
335
- doc.add_child fragment
336
- end
337
- end
338
-
339
- def test_add_child_with_multiple_roots
340
- assert_raises(RuntimeError) do
341
- @xml << Node.new('foo', @xml)
342
- end
343
- end
344
-
345
- def test_add_child_with_string
346
- doc = Nokogiri::XML::Document.new
347
- doc.add_child "<div>quack!</div>"
348
- assert_equal 1, doc.root.children.length
349
- assert_equal "quack!", doc.root.children.first.content
350
- end
351
-
352
- def test_prepend
353
- doc = Nokogiri::XML('<root>')
354
-
355
- node_set = doc.root.prepend_child '<branch/>'
356
- assert_equal %w[branch], node_set.map(&:name)
357
-
358
- branch = doc.at('//branch')
359
-
360
- leaves = %w[leaf1 leaf2 leaf3]
361
- leaves.each { |name|
362
- branch.prepend_child('<%s/>' % name)
363
- }
364
- assert_equal leaves.length, branch.children.length
365
- assert_equal leaves.reverse, branch.children.map(&:name)
366
- end
367
-
368
- def test_prepend_child_fragment_with_single_node
369
- doc = Nokogiri::XML::Document.new
370
- fragment = doc.fragment('<hello />')
371
- doc.prepend_child fragment
372
- assert_equal '/hello', doc.at('//hello').path
373
- assert_equal 'hello', doc.root.name
374
- end
375
-
376
- def test_prepend_child_fragment_with_multiple_nodes
377
- doc = Nokogiri::XML::Document.new
378
- fragment = doc.fragment('<hello /><goodbye />')
379
- assert_raises(RuntimeError) do
380
- doc.prepend_child fragment
381
- end
382
- end
383
-
384
- def test_prepend_child_with_multiple_roots
385
- assert_raises(RuntimeError) do
386
- @xml.prepend_child Node.new('foo', @xml)
387
- end
388
- end
389
-
390
- def test_prepend_child_with_string
391
- doc = Nokogiri::XML::Document.new
392
- doc.prepend_child "<div>quack!</div>"
393
- assert_equal 1, doc.root.children.length
394
- assert_equal "quack!", doc.root.children.first.content
395
- end
396
-
397
- def test_move_root_to_document_with_no_root
398
- sender = Nokogiri::XML('<root>foo</root>')
399
- newdoc = Nokogiri::XML::Document.new
400
- newdoc.root = sender.root
401
- end
402
-
403
- def test_move_root_with_existing_root_gets_gcd
404
- doc = Nokogiri::XML('<root>test</root>')
405
- doc2 = Nokogiri::XML("<root>#{'x' * 5000000}</root>")
406
- doc2.root = doc.root
407
- end
408
-
409
- def test_validate
410
- if Nokogiri.uses_libxml?
411
- assert_equal 44, @xml.validate.length
412
- else
413
- xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) {|cfg| cfg.dtdvalid}
414
- assert_equal 40, xml.validate.length
415
- end
416
- end
417
-
418
- def test_validate_no_internal_subset
419
- doc = Nokogiri::XML('<test/>')
420
- assert_nil doc.validate
421
- end
422
-
423
- def test_clone
424
- assert @xml.clone
425
- end
426
-
427
- def test_document_should_not_have_default_ns
428
- doc = Nokogiri::XML::Document.new
429
-
430
- assert_raises NoMethodError do
431
- doc.default_namespace = 'http://innernet.com/'
432
- end
433
-
434
- assert_raises NoMethodError do
435
- doc.add_namespace_definition('foo', 'bar')
436
- end
437
- end
438
-
439
- def test_parse_handles_nil_gracefully
440
- @doc = Nokogiri::XML::Document.parse(nil)
441
- assert_instance_of Nokogiri::XML::Document, @doc
442
- end
443
-
444
- def test_parse_takes_block
445
- options = nil
446
- Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
447
- options = cfg
448
- end
449
- assert options
450
- end
451
-
452
- def test_parse_yields_parse_options
453
- options = nil
454
- Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) do |cfg|
455
- options = cfg
456
- options.nonet.nowarning.dtdattr
457
- end
458
- assert options.nonet?
459
- assert options.nowarning?
460
- assert options.dtdattr?
461
- end
462
-
463
- def test_XML_takes_block
464
- options = nil
465
- Nokogiri::XML(File.read(XML_FILE), XML_FILE) do |cfg|
466
- options = cfg
467
- options.nonet.nowarning.dtdattr
468
- end
469
- assert options.nonet?
470
- assert options.nowarning?
471
- assert options.dtdattr?
472
- end
473
-
474
- def test_subclass
475
- klass = Class.new(Nokogiri::XML::Document)
476
- doc = klass.new
477
- assert_instance_of klass, doc
478
- end
479
-
480
- def test_subclass_initialize
481
- klass = Class.new(Nokogiri::XML::Document) do
482
- attr_accessor :initialized_with
483
-
484
- def initialize(*args)
485
- @initialized_with = args
486
- end
487
- end
488
- doc = klass.new("1.0", 1)
489
- assert_equal ["1.0", 1], doc.initialized_with
490
- end
491
-
492
- def test_subclass_dup
493
- klass = Class.new(Nokogiri::XML::Document)
494
- doc = klass.new.dup
495
- assert_instance_of klass, doc
496
- end
497
-
498
- def test_subclass_parse
499
- klass = Class.new(Nokogiri::XML::Document)
500
- doc = klass.parse(File.read(XML_FILE))
501
- # lame hack uses root to avoid comparing DOCTYPE tags which can appear out of order.
502
- # I should really finish lorax and use that here.
503
- assert_equal @xml.root.to_s, doc.root.to_s
504
- assert_instance_of klass, doc
505
- end
506
-
507
- def test_document_parse_method
508
- xml = Nokogiri::XML::Document.parse(File.read(XML_FILE))
509
- # lame hack uses root to avoid comparing DOCTYPE tags which can appear out of order.
510
- # I should really finish lorax and use that here.
511
- assert_equal @xml.root.to_s, xml.root.to_s
512
- end
513
-
514
- def test_encoding=
515
- @xml.encoding = 'UTF-8'
516
- assert_match 'UTF-8', @xml.to_xml
517
-
518
- @xml.encoding = 'EUC-JP'
519
- assert_match 'EUC-JP', @xml.to_xml
520
- end
521
-
522
- def test_namespace_should_not_exist
523
- assert_raises(NoMethodError) {
524
- @xml.namespace
525
- }
526
- end
527
-
528
- def test_non_existant_function
529
- # WTF. I don't know why this is different between MRI and Jruby
530
- # They should be the same... Either way, raising an exception
531
- # is the correct thing to do.
532
- exception = RuntimeError
533
-
534
- if !Nokogiri.uses_libxml? || (Nokogiri.uses_libxml? && Nokogiri::VERSION_INFO['libxml']['platform'] == 'jruby')
535
- exception = Nokogiri::XML::XPath::SyntaxError
536
- end
537
-
538
- assert_raises(exception) {
539
- @xml.xpath('//name[foo()]')
540
- }
541
- end
542
-
543
- def test_xpath_syntax_error
544
- assert_raises(Nokogiri::XML::XPath::SyntaxError) do
545
- @xml.xpath('\\')
546
- end
547
- end
548
-
549
- def test_ancestors
550
- assert_equal 0, @xml.ancestors.length
551
- end
552
-
553
- def test_root_node_parent_is_document
554
- parent = @xml.root.parent
555
- assert_equal @xml, parent
556
- assert_instance_of Nokogiri::XML::Document, parent
557
- end
558
-
559
- def test_xmlns_is_automatically_registered
560
- doc = Nokogiri::XML(<<-eoxml)
561
- <root xmlns="http://tenderlovemaking.com/">
562
- <foo>
563
- bar
564
- </foo>
565
- </root>
566
- eoxml
567
- assert_equal 1, doc.css('xmlns|foo').length
568
- assert_equal 1, doc.css('foo').length
569
- assert_equal 0, doc.css('|foo').length
570
- assert_equal 1, doc.xpath('//xmlns:foo').length
571
- assert_equal 1, doc.search('xmlns|foo').length
572
- assert_equal 1, doc.search('//xmlns:foo').length
573
- assert doc.at('xmlns|foo')
574
- assert doc.at('//xmlns:foo')
575
- assert doc.at('foo')
576
- end
577
-
578
- def test_xmlns_is_registered_for_nodesets
579
- doc = Nokogiri::XML(<<-eoxml)
580
- <root xmlns="http://tenderlovemaking.com/">
581
- <foo>
582
- <bar>
583
- baz
584
- </bar>
585
- </foo>
586
- </root>
587
- eoxml
588
- assert_equal 1, doc.css('xmlns|foo').css('xmlns|bar').length
589
- assert_equal 1, doc.css('foo').css('bar').length
590
- assert_equal 1, doc.xpath('//xmlns:foo').xpath('./xmlns:bar').length
591
- assert_equal 1, doc.search('xmlns|foo').search('xmlns|bar').length
592
- assert_equal 1, doc.search('//xmlns:foo').search('./xmlns:bar').length
593
- end
594
-
595
- def test_to_xml_with_indent
596
- doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
597
- doc = Nokogiri::XML(doc.to_xml(:indent => 5))
598
-
599
- assert_indent 5, doc
600
- end
601
-
602
- def test_write_xml_to_with_indent
603
- io = StringIO.new
604
- doc = Nokogiri::XML('<root><foo><bar/></foo></root>')
605
- doc.write_xml_to io, :indent => 5
606
- io.rewind
607
- doc = Nokogiri::XML(io.read)
608
- assert_indent 5, doc
609
- end
610
-
611
- # wtf... osx's libxml sucks.
612
- unless !Nokogiri.uses_libxml? || Nokogiri::LIBXML_VERSION =~ /^2\.6\./
613
- def test_encoding
614
- xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE, 'UTF-8')
615
- assert_equal 'UTF-8', xml.encoding
616
- end
617
- end
618
-
619
- def test_memory_explosion_on_invalid_xml
620
- doc = Nokogiri::XML("<<<")
621
- refute_nil doc
622
- refute_empty doc.errors
623
- end
624
-
625
- def test_memory_explosion_on_wrong_formatted_element_following_the_root_element
626
- doc = Nokogiri::XML("<a/><\n")
627
- refute_nil doc
628
- refute_empty doc.errors
629
- end
630
-
631
- def test_document_has_errors
632
- doc = Nokogiri::XML(<<-eoxml)
633
- <foo><bar></foo>
634
- eoxml
635
- assert doc.errors.length > 0
636
- doc.errors.each do |error|
637
- assert_match error.message, error.inspect
638
- assert_match error.message, error.to_s
639
- end
640
- end
641
-
642
- def test_strict_document_throws_syntax_error
643
- assert_raises(Nokogiri::XML::SyntaxError) {
644
- Nokogiri::XML('<foo><bar></foo>', nil, nil, 0)
645
- }
646
-
647
- assert_raises(Nokogiri::XML::SyntaxError) {
648
- Nokogiri::XML('<foo><bar></foo>') { |cfg|
649
- cfg.strict
650
- }
651
- }
652
-
653
- assert_raises(Nokogiri::XML::SyntaxError) {
654
- Nokogiri::XML(StringIO.new('<foo><bar></foo>')) { |cfg|
655
- cfg.strict
656
- }
657
- }
658
- end
659
-
660
- def test_XML_function
661
- xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
662
- assert xml.xml?
663
- end
664
-
665
- def test_url
666
- assert @xml.url
667
- assert_equal XML_FILE, URI.unescape(@xml.url).sub('file:///', '')
668
- end
669
-
670
- def test_document_parent
671
- xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
672
- assert_raises(NoMethodError) {
673
- xml.parent
674
- }
675
- end
676
-
677
- def test_document_name
678
- xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
679
- assert_equal 'document', xml.name
680
- end
681
-
682
- def test_parse_can_take_io
683
- xml = nil
684
- File.open(XML_FILE, 'rb') { |f|
685
- xml = Nokogiri::XML(f)
686
- }
687
- assert xml.xml?
688
- set = xml.search('//employee')
689
- assert set.length > 0
690
- end
691
-
692
- def test_parsing_empty_io
693
- doc = Nokogiri::XML.parse(StringIO.new(''))
694
- refute_nil doc
695
- end
696
-
697
- def test_parse_works_with_an_object_that_responds_to_read
698
- klass = Class.new do
699
- def read *args
700
- "<div>foo</div>"
701
- end
702
- end
703
-
704
- doc = Nokogiri::XML.parse klass.new
705
- doc.at_css("div").content.must_equal("foo")
706
- end
707
-
708
- def test_search_on_empty_documents
709
- doc = Nokogiri::XML::Document.new
710
- ns = doc.search('//foo')
711
- assert_equal 0, ns.length
712
-
713
- ns = doc.css('foo')
714
- assert_equal 0, ns.length
715
-
716
- ns = doc.xpath('//foo')
717
- assert_equal 0, ns.length
718
- end
719
-
720
- def test_document_search_with_multiple_queries
721
- xml = '<document>
722
- <thing>
723
- <div class="title">important thing</div>
724
- </thing>
725
- <thing>
726
- <div class="content">stuff</div>
727
- </thing>
728
- <thing>
729
- <p class="blah">more stuff</div>
730
- </thing>
731
- </document>'
732
- document = Nokogiri::XML(xml)
733
- assert_kind_of Nokogiri::XML::Document, document
734
-
735
- assert_equal 3, document.xpath('.//div', './/p').length
736
- assert_equal 3, document.css('.title', '.content', 'p').length
737
- assert_equal 3, document.search('.//div', 'p.blah').length
738
- end
739
-
740
- def test_bad_xpath_raises_syntax_error
741
- assert_raises(XML::XPath::SyntaxError) {
742
- @xml.xpath('\\')
743
- }
744
- end
745
-
746
- def test_find_with_namespace
747
- doc = Nokogiri::XML.parse(<<-eoxml)
748
- <x xmlns:tenderlove='http://tenderlovemaking.com/'>
749
- <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
750
- </x>
751
- eoxml
752
-
753
- ctx = Nokogiri::XML::XPathContext.new(doc)
754
- ctx.register_ns 'tenderlove', 'http://tenderlovemaking.com/'
755
- set = ctx.evaluate('//tenderlove:foo')
756
- assert_equal 1, set.length
757
- assert_equal 'foo', set.first.name
758
-
759
- # It looks like only the URI is important:
760
- ctx = Nokogiri::XML::XPathContext.new(doc)
761
- ctx.register_ns 'america', 'http://tenderlovemaking.com/'
762
- set = ctx.evaluate('//america:foo')
763
- assert_equal 1, set.length
764
- assert_equal 'foo', set.first.name
765
-
766
- # Its so important that a missing slash will cause it to return nothing
767
- ctx = Nokogiri::XML::XPathContext.new(doc)
768
- ctx.register_ns 'america', 'http://tenderlovemaking.com'
769
- set = ctx.evaluate('//america:foo')
770
- assert_equal 0, set.length
771
- end
772
-
773
- def test_xml?
774
- assert @xml.xml?
775
- end
776
-
777
- def test_document
778
- assert @xml.document
779
- end
780
-
781
- def test_singleton_methods
782
- assert node_set = @xml.search('//name')
783
- assert node_set.length > 0
784
- node = node_set.first
785
- def node.test
786
- 'test'
787
- end
788
- assert node_set = @xml.search('//name')
789
- assert_equal 'test', node_set.first.test
790
- end
791
-
792
- def test_multiple_search
793
- assert node_set = @xml.search('//employee', '//name')
794
- employees = @xml.search('//employee')
795
- names = @xml.search('//name')
796
- assert_equal(employees.length + names.length, node_set.length)
797
- end
798
-
799
- def test_node_set_index
800
- assert node_set = @xml.search('//employee')
801
-
802
- assert_equal(5, node_set.length)
803
- assert node_set[4]
804
- assert_nil node_set[5]
805
- end
806
-
807
- def test_search
808
- assert node_set = @xml.search('//employee')
809
-
810
- assert_equal(5, node_set.length)
811
-
812
- node_set.each do |node|
813
- assert_equal('employee', node.name)
814
- end
815
- end
816
-
817
- def test_dump
818
- assert @xml.serialize
819
- assert @xml.to_xml
820
- end
821
-
822
- def test_dup
823
- dup = @xml.dup
824
- assert_instance_of Nokogiri::XML::Document, dup
825
- assert dup.xml?, 'duplicate should be xml'
826
- end
827
-
828
- def test_new
829
- doc = nil
830
- doc = Nokogiri::XML::Document.new
831
- assert doc
832
- assert doc.xml?
833
- assert_nil doc.root
834
- end
835
-
836
- def test_set_root
837
- doc = nil
838
- doc = Nokogiri::XML::Document.new
839
- assert doc
840
- assert doc.xml?
841
- assert_nil doc.root
842
- node = Nokogiri::XML::Node.new("b", doc) { |n|
843
- n.content = 'hello world'
844
- }
845
- assert_equal('hello world', node.content)
846
- doc.root = node
847
- assert_equal(node, doc.root)
848
- end
849
-
850
- def test_remove_namespaces
851
- doc = Nokogiri::XML <<-EOX
852
- <root xmlns:a="http://a.flavorjon.es/" xmlns:b="http://b.flavorjon.es/">
853
- <a:foo>hello from a</a:foo>
854
- <b:foo>hello from b</b:foo>
855
- <container xmlns:c="http://c.flavorjon.es/">
856
- <c:foo c:attr='attr-value'>hello from c</c:foo>
857
- </container>
858
- </root>
859
- EOX
860
-
861
- namespaces = doc.root.namespaces
862
-
863
- # assert on setup
864
- assert_equal 2, doc.root.namespaces.length
865
- assert_equal 3, doc.at_xpath("//container").namespaces.length
866
- assert_equal 0, doc.xpath("//foo").length
867
- assert_equal 1, doc.xpath("//a:foo").length
868
- assert_equal 1, doc.xpath("//a:foo").length
869
- assert_equal 1, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
870
- assert_match %r{foo c:attr}, doc.to_xml
871
- doc.at_xpath("//x:foo", "x" => "http://c.flavorjon.es/").tap do |node|
872
- assert_nil node["attr"]
873
- assert_equal "attr-value", node["c:attr"]
874
- assert_nil node.attribute_with_ns("attr", nil)
875
- assert_equal "attr-value", node.attribute_with_ns("attr", "http://c.flavorjon.es/").value
876
- assert_equal "attr-value", node.attributes["attr"].value
877
- end
878
-
879
- doc.remove_namespaces!
880
-
881
- assert_equal 0, doc.root.namespaces.length
882
- assert_equal 0, doc.at_xpath("//container").namespaces.length
883
- assert_equal 3, doc.xpath("//foo").length
884
- assert_equal 0, doc.xpath("//a:foo", namespaces).length
885
- assert_equal 0, doc.xpath("//a:foo", namespaces).length
886
- assert_equal 0, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
887
- assert_match %r{foo attr}, doc.to_xml
888
- doc.at_xpath("//container/foo").tap do |node|
889
- assert_equal "attr-value", node["attr"]
890
- assert_nil node["c:attr"]
891
- assert_equal "attr-value", node.attribute_with_ns("attr", nil).value
892
- assert_nil node.attribute_with_ns("attr", "http://c.flavorjon.es/")
893
- assert_equal "attr-value", node.attributes["attr"].value # doesn't change!
894
- end
895
- end
896
-
897
- # issue #785
898
- def test_attribute_decoration
899
- decorator = Module.new do
900
- def test_method
901
- end
902
- end
903
-
904
- util_decorate(@xml, decorator)
905
-
906
- assert @xml.search('//@street').first.respond_to?(:test_method)
907
- end
908
-
909
- def test_subset_is_decorated
910
- x = Module.new do
911
- def awesome!
912
- end
913
- end
914
- util_decorate(@xml, x)
915
-
916
- assert @xml.respond_to?(:awesome!)
917
- assert node_set = @xml.search('//staff')
918
- assert node_set.respond_to?(:awesome!)
919
- assert subset = node_set.search('.//employee')
920
- assert subset.respond_to?(:awesome!)
921
- assert sub_subset = node_set.search('.//name')
922
- assert sub_subset.respond_to?(:awesome!)
923
- end
924
-
925
- def test_decorator_is_applied
926
- x = Module.new do
927
- def awesome!
928
- end
929
- end
930
- util_decorate(@xml, x)
931
-
932
- assert @xml.respond_to?(:awesome!)
933
- assert node_set = @xml.search('//employee')
934
- assert node_set.respond_to?(:awesome!)
935
- node_set.each do |node|
936
- assert node.respond_to?(:awesome!), node.class
937
- end
938
- assert @xml.root.respond_to?(:awesome!)
939
- assert @xml.children.respond_to?(:awesome!)
940
- end
941
-
942
- if Nokogiri.jruby?
943
- def wrap_java_document
944
- require 'java'
945
- factory = javax.xml.parsers.DocumentBuilderFactory.newInstance
946
- builder = factory.newDocumentBuilder
947
- document = builder.newDocument
948
- root = document.createElement("foo")
949
- document.appendChild(root)
950
- Nokogiri::XML::Document.wrap(document)
951
- end
952
- end
953
-
954
- def test_java_integration
955
- skip("Ruby doesn't have the wrap method") unless Nokogiri.jruby?
956
- noko_doc = wrap_java_document
957
- assert_equal 'foo', noko_doc.root.name
958
-
959
- noko_doc = Nokogiri::XML(<<eoxml)
960
- <foo xmlns='hello'>
961
- <bar xmlns:foo='world' />
962
- </foo>
963
- eoxml
964
- dom = noko_doc.to_java
965
- assert dom.kind_of? org.w3c.dom.Document
966
- assert_equal 'foo', dom.getDocumentElement().getTagName()
967
- end
968
-
969
- def test_add_child
970
- skip("Ruby doesn't have the wrap method") unless Nokogiri.jruby?
971
- doc = wrap_java_document
972
- doc.root.add_child "<bar />"
973
- end
974
-
975
- def test_can_be_closed
976
- f = File.open XML_FILE
977
- Nokogiri::XML f
978
- f.close
979
- end
980
- end
981
- end
982
- end