libxml-ruby 5.0.5 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/{HISTORY → CHANGELOG.md} +875 -923
  3. data/LICENSE +20 -20
  4. data/README.md +67 -0
  5. data/Rakefile +77 -98
  6. data/ext/libxml/extconf.rb +34 -20
  7. data/ext/libxml/libxml.c +67 -79
  8. data/ext/libxml/ruby_libxml.h +1 -1
  9. data/ext/libxml/ruby_xml_attr.c +338 -333
  10. data/ext/libxml/ruby_xml_attr.h +13 -12
  11. data/ext/libxml/ruby_xml_attr_decl.c +19 -12
  12. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  13. data/ext/libxml/ruby_xml_attributes.c +18 -9
  14. data/ext/libxml/ruby_xml_document.c +111 -98
  15. data/ext/libxml/ruby_xml_document.h +1 -0
  16. data/ext/libxml/ruby_xml_dtd.c +49 -19
  17. data/ext/libxml/ruby_xml_dtd.h +10 -9
  18. data/ext/libxml/ruby_xml_html_parser.c +91 -91
  19. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  20. data/ext/libxml/ruby_xml_html_parser_context.c +19 -6
  21. data/ext/libxml/ruby_xml_html_parser_context.h +11 -10
  22. data/ext/libxml/ruby_xml_html_parser_options.c +48 -48
  23. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  24. data/ext/libxml/ruby_xml_input_cbg.c +45 -17
  25. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  26. data/ext/libxml/ruby_xml_io.c +49 -47
  27. data/ext/libxml/ruby_xml_io.h +10 -10
  28. data/ext/libxml/ruby_xml_namespace.c +20 -12
  29. data/ext/libxml/ruby_xml_namespace.h +11 -10
  30. data/ext/libxml/ruby_xml_namespaces.c +313 -293
  31. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  32. data/ext/libxml/ruby_xml_node.c +71 -32
  33. data/ext/libxml/ruby_xml_node.h +15 -13
  34. data/ext/libxml/ruby_xml_parser.c +91 -91
  35. data/ext/libxml/ruby_xml_parser_context.c +54 -47
  36. data/ext/libxml/ruby_xml_parser_context.h +11 -10
  37. data/ext/libxml/ruby_xml_reader.c +25 -15
  38. data/ext/libxml/ruby_xml_reader.h +14 -14
  39. data/ext/libxml/ruby_xml_registry.c +31 -0
  40. data/ext/libxml/ruby_xml_registry.h +22 -0
  41. data/ext/libxml/ruby_xml_relaxng.c +21 -5
  42. data/ext/libxml/ruby_xml_relaxng.h +9 -8
  43. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  44. data/ext/libxml/ruby_xml_sax_parser.c +1 -1
  45. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  46. data/ext/libxml/ruby_xml_schema.c +18 -11
  47. data/ext/libxml/ruby_xml_schema.h +26 -25
  48. data/ext/libxml/ruby_xml_schema_attribute.c +7 -7
  49. data/ext/libxml/ruby_xml_schema_attribute.h +37 -37
  50. data/ext/libxml/ruby_xml_schema_element.c +8 -8
  51. data/ext/libxml/ruby_xml_schema_element.h +11 -11
  52. data/ext/libxml/ruby_xml_schema_facet.c +50 -50
  53. data/ext/libxml/ruby_xml_schema_facet.h +9 -9
  54. data/ext/libxml/ruby_xml_schema_type.c +12 -19
  55. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  56. data/ext/libxml/ruby_xml_version.h +4 -4
  57. data/ext/libxml/ruby_xml_writer.c +1136 -1124
  58. data/ext/libxml/ruby_xml_writer.h +6 -6
  59. data/ext/libxml/ruby_xml_xinclude.c +20 -20
  60. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  61. data/ext/libxml/ruby_xml_xpath.c +195 -195
  62. data/ext/libxml/ruby_xml_xpath.h +15 -15
  63. data/ext/libxml/ruby_xml_xpath_context.c +408 -362
  64. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  65. data/ext/libxml/ruby_xml_xpath_expression.c +11 -5
  66. data/ext/libxml/ruby_xml_xpath_expression.h +11 -10
  67. data/ext/libxml/ruby_xml_xpath_object.c +69 -54
  68. data/ext/libxml/ruby_xml_xpath_object.h +19 -17
  69. data/ext/vc/libxml_ruby/libxml_ruby.vcxproj +271 -0
  70. data/ext/xcode/libxml-ruby.xcodeproj/project.pbxproj +633 -0
  71. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  72. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  73. data/ext/xcode/libxml-ruby.xcodeproj/xcshareddata/xcschemes/libxml-ruby.xcscheme +80 -0
  74. data/lib/libxml/document.rb +0 -29
  75. data/lib/libxml/error.rb +30 -3
  76. data/lib/libxml/html_parser.rb +0 -16
  77. data/lib/libxml/node.rb +1 -3
  78. data/lib/libxml/parser.rb +0 -8
  79. data/lib/libxml/schema/attribute.rb +27 -19
  80. data/lib/libxml/schema/element.rb +20 -0
  81. data/lib/libxml/schema/type.rb +44 -21
  82. data/lib/libxml/schema.rb +47 -47
  83. data/lib/libxml-ruby.rb +30 -30
  84. data/libxml-ruby.gemspec +12 -16
  85. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  86. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  87. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  88. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  89. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  90. data/test/c14n/result/with-comments/example-1 +5 -5
  91. data/test/c14n/result/with-comments/example-2 +10 -10
  92. data/test/c14n/result/with-comments/example-3 +13 -13
  93. data/test/c14n/result/with-comments/example-4 +8 -8
  94. data/test/c14n/result/with-comments/example-5 +3 -3
  95. data/test/c14n/result/without-comments/example-1 +3 -3
  96. data/test/c14n/result/without-comments/example-2 +10 -10
  97. data/test/c14n/result/without-comments/example-3 +13 -13
  98. data/test/c14n/result/without-comments/example-4 +8 -8
  99. data/test/c14n/result/without-comments/example-5 +2 -2
  100. data/test/test_attr.rb +179 -180
  101. data/test/test_attr_decl.rb +131 -131
  102. data/test/test_attributes.rb +135 -135
  103. data/test/test_canonicalize.rb +122 -120
  104. data/test/test_document.rb +2 -4
  105. data/test/test_dtd.rb +12 -4
  106. data/test/test_encoding_sax.rb +114 -114
  107. data/test/test_error.rb +6 -3
  108. data/test/test_helper.rb +1 -0
  109. data/test/test_html_parser.rb +6 -2
  110. data/test/test_html_parser_context.rb +22 -22
  111. data/test/test_input_callbacks.rb +36 -0
  112. data/test/test_namespace.rb +1 -1
  113. data/test/test_namespaces.rb +200 -200
  114. data/test/test_node.rb +16 -0
  115. data/test/test_node_cdata.rb +50 -50
  116. data/test/test_node_comment.rb +32 -32
  117. data/test/test_node_copy.rb +40 -40
  118. data/test/test_node_edit.rb +176 -158
  119. data/test/test_node_pi.rb +37 -37
  120. data/test/test_node_text.rb +69 -69
  121. data/test/test_node_xlink.rb +28 -28
  122. data/test/test_parser.rb +5 -41
  123. data/test/test_parser_context.rb +198 -198
  124. data/test/test_properties.rb +38 -38
  125. data/test/test_reader.rb +55 -6
  126. data/test/test_relaxng.rb +59 -53
  127. data/test/test_sax_parser.rb +345 -345
  128. data/test/test_schema.rb +28 -0
  129. data/test/test_traversal.rb +152 -152
  130. data/test/test_writer.rb +469 -468
  131. data/test/test_xinclude.rb +20 -20
  132. data/test/test_xml.rb +3 -7
  133. data/test/test_xpath.rb +244 -244
  134. data/test/test_xpath_context.rb +87 -87
  135. data/test/test_xpath_expression.rb +37 -37
  136. metadata +33 -27
  137. data/README.rdoc +0 -208
  138. data/ext/libxml/extconf.h +0 -3
  139. data/ext/libxml/ruby_xml_cbg.c +0 -85
  140. data/lib/libxml/hpricot.rb +0 -78
  141. data/lib/libxml.rb +0 -5
  142. data/lib/xml/libxml.rb +0 -10
  143. data/lib/xml.rb +0 -14
  144. data/script/benchmark/depixelate +0 -634
  145. data/script/benchmark/hamlet.xml +0 -9055
  146. data/script/benchmark/parsecount +0 -170
  147. data/script/benchmark/sock_entries.xml +0 -507
  148. data/script/benchmark/throughput +0 -41
  149. data/script/test +0 -6
  150. data/test/test.rb +0 -5
  151. data/test/test_deprecated_require.rb +0 -12
@@ -1,20 +1,20 @@
1
- # encoding: UTF-8
2
-
3
- require_relative './test_helper'
4
-
5
- class TestXInclude < Minitest::Test
6
- def setup
7
- @doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/xinclude.xml'))
8
- assert_instance_of(LibXML::XML::Document, @doc)
9
- end
10
-
11
- def teardown
12
- @doc = nil
13
- end
14
-
15
- def test_ruby_xml_xinclude
16
- assert_equal(1, @doc.xinclude)
17
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<document xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n <p>This libxml2 binding has the following project information:\n <code>This is some text to include in an xml file via XInclude.</code></p>\n</document>\n",
18
- @doc.to_s)
19
- end
20
- end
1
+ # encoding: UTF-8
2
+
3
+ require_relative './test_helper'
4
+
5
+ class TestXInclude < Minitest::Test
6
+ def setup
7
+ @doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/xinclude.xml'))
8
+ assert_instance_of(LibXML::XML::Document, @doc)
9
+ end
10
+
11
+ def teardown
12
+ @doc = nil
13
+ end
14
+
15
+ def test_ruby_xml_xinclude
16
+ assert_equal(1, @doc.xinclude)
17
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<document xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n <p>This libxml2 binding has the following project information:\n <code>This is some text to include in an xml file via XInclude.</code></p>\n</document>\n",
18
+ @doc.to_s)
19
+ end
20
+ end
data/test/test_xml.rb CHANGED
@@ -72,11 +72,7 @@ class TestXml < Minitest::Test
72
72
  end
73
73
 
74
74
  def test_enabled_docbook
75
- if windows?
76
- refute(LibXML::XML.enabled_docbook?)
77
- else
78
- assert(LibXML::XML.enabled_docbook?)
79
- end
75
+ LibXML::XML.enabled_docbook?
80
76
  end
81
77
 
82
78
  def test_enabled_ftp
@@ -88,7 +84,7 @@ class TestXml < Minitest::Test
88
84
  end
89
85
 
90
86
  def test_enabled_http
91
- assert(LibXML::XML.enabled_http?)
87
+ LibXML::XML.enabled_http?
92
88
  end
93
89
 
94
90
  def test_enabled_html
@@ -117,7 +113,7 @@ class TestXml < Minitest::Test
117
113
  end
118
114
 
119
115
  def test_enabled_unicode
120
- assert(LibXML::XML.enabled_unicode?)
116
+ LibXML::XML.enabled_unicode?
121
117
  end
122
118
 
123
119
  def test_enabled_xinclude
data/test/test_xpath.rb CHANGED
@@ -1,244 +1,244 @@
1
- # encoding: UTF-8
2
-
3
- require_relative './test_helper'
4
- require 'tempfile'
5
-
6
- class TestXPath < Minitest::Test
7
- def setup
8
- @doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
9
- end
10
-
11
- def teardown
12
- @doc = nil
13
- end
14
-
15
- def test_doc_find
16
- nodes = @doc.find('/soap:Envelope')
17
- assert_instance_of(LibXML::XML::XPath::Object, nodes)
18
- assert_equal(1, nodes.length)
19
- assert_equal(nodes.xpath_type, LibXML::XML::XPath::NODESET)
20
- end
21
-
22
- def test_doc_find_first
23
- node = @doc.find_first('/soap:Envelope/soap:Body')
24
- assert_instance_of(LibXML::XML::Node, node)
25
- end
26
-
27
- def test_ns
28
- nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
29
- assert_equal(3, nodes.length)
30
- end
31
-
32
- def test_ns_gc
33
- _stress = GC.stress
34
- GC.stress = true
35
-
36
- doc = LibXML::XML::Document.string('<foo xmlns="http://bar.com" />')
37
- node = doc.root
38
- # This line segfaults on prior versions of libxml-ruby
39
- node.find("namespace::*")
40
-
41
- GC.stress = _stress
42
- end
43
-
44
- def test_ns_array
45
- nodes = @doc.find('//ns1:IdAndName', ['ns1:http://domain.somewhere.com'])
46
- assert_equal(3, nodes.length)
47
- end
48
-
49
- def test_default_ns1
50
- # Find all nodes with http://services.somewhere.com namespace
51
- nodes = @doc.find('//*[namespace-uri()="http://services.somewhere.com"]')
52
- assert_equal(2, nodes.length)
53
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
54
- assert_equal('IDAndNameList', nodes[1].name)
55
- end
56
-
57
- def test_default_ns2
58
- # Find all nodes with http://services.somewhere.com namespace
59
- nodes = @doc.find('//ns:*', 'ns:http://services.somewhere.com')
60
- assert_equal(2, nodes.length)
61
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
62
- assert_equal('IDAndNameList', nodes[1].name)
63
-
64
- # Get getManufacturerNamesResponse node
65
- nodes = @doc.find('//ns:getManufacturerNamesResponse', 'ns:http://services.somewhere.com')
66
- assert_equal(1, nodes.length)
67
-
68
- # Get IdAndName node
69
- nodes = @doc.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse/ns0:IDAndNameList/ns1:IdAndName',
70
- ['ns0:http://services.somewhere.com', 'ns1:http://domain.somewhere.com'])
71
- assert_equal(3, nodes.length)
72
- end
73
-
74
- def test_default_ns3
75
- # Find all nodes with http://services.somewhere.com namespace
76
- nodes = @doc.find('//ns:*', 'ns' => 'http://services.somewhere.com')
77
- assert_equal(2, nodes.length)
78
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
79
- assert_equal('IDAndNameList', nodes[1].name)
80
- end
81
-
82
- def test_default_ns4
83
- # Find all nodes with http://services.somewhere.com namespace
84
- nodes = @doc.find('//ns:*', :ns => 'http://services.somewhere.com')
85
- assert_equal(2, nodes.length)
86
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
87
- assert_equal('IDAndNameList', nodes[1].name)
88
- end
89
-
90
- def test_default_ns5
91
- # Find all nodes with http://services.somewhere.com namespace
92
- LibXML::XML::Namespace.new(@doc.root, 'ns', 'http://services.somewhere.com')
93
- nodes = @doc.find('//ns:*')
94
- assert_equal(2, nodes.length)
95
- assert_equal('getManufacturerNamesResponse', nodes[0].name)
96
- assert_equal('IDAndNameList', nodes[1].name)
97
- end
98
-
99
- def test_attribute_ns
100
- # Pull all nodes with http://services.somewhere.com namespace
101
- nodes = @doc.find('@soap:encodingStyle')
102
- assert_equal(1, nodes.length)
103
- assert_equal('encodingStyle', nodes.first.name)
104
- assert_equal('http://www.w3.org/2001/12/soap-encoding', nodes.first.value)
105
- end
106
-
107
- def test_register_default_ns
108
- doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/atom.xml'))
109
-
110
- # No namespace has been yet defined
111
- assert_raises(LibXML::XML::Error) do
112
- doc.find("atom:title")
113
- end
114
-
115
- node = doc.find('atom:title', 'atom:http://www.w3.org/2005/Atom')
116
- refute_nil(node)
117
-
118
- # Register namespace
119
- doc.root.namespaces.default_prefix = 'atom'
120
- node = doc.find("atom:title")
121
- refute_nil(node)
122
- end
123
-
124
- def test_node_find
125
- nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
126
- node = nodes.first
127
-
128
- # Since we are searching on the node, don't have to register namespace
129
- nodes = node.find('ns1:name')
130
- assert_equal(1, nodes.length)
131
- refute_equal(nodes.first.object_id, nodes.last.object_id)
132
- assert_equal('name', nodes.first.name)
133
- assert_equal('man1', nodes.first.content)
134
- end
135
-
136
- def test_node_find_first
137
- node = @doc.find_first('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
138
-
139
- # Since we are searching on the node, don't have to register namespace
140
- node = node.find_first('ns1:name')
141
- assert_equal('name', node.name)
142
- assert_equal('man1', node.content)
143
- end
144
-
145
- def test_node_no_doc
146
- node = LibXML::XML::Node.new('header', 'some content')
147
- assert_raises(TypeError) do
148
- node = node.find_first('/header')
149
- end
150
- end
151
-
152
- def test_memory
153
- # This sometimes causes a segmentation fault because
154
- # an xml document is sometimes freed before the
155
- # xpath_object used to query it. When the xpath_object
156
- # is free, it iterates over its results which are pointers
157
- # to the document's nodes. A segmentation fault then happens.
158
-
159
- 1000.times do
160
- doc = LibXML::XML::Document.new('1.0')
161
- doc.root = LibXML::XML::Node.new("header")
162
-
163
- 1000.times do
164
- doc.root << LibXML::XML::Node.new("footer")
165
- end
166
-
167
- doc.find('/header/footer')
168
- end
169
- end
170
-
171
- # Test that document doesn't get freed before nodes
172
- def test_xpath_free
173
- doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
174
- nodes = doc.find('//*')
175
- GC.start
176
- assert_equal('Envelope', nodes.first.name)
177
- end
178
-
179
- def test_xpath_namespace_nodes
180
- doc = LibXML::XML::Document.string('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml"><entry/></feed>')
181
- nodes = doc.find('//atom:entry|namespace::*', :atom => "http://www.w3.org/2005/Atom")
182
- assert_equal(4, nodes.length)
183
-
184
- node = nodes[0]
185
- assert_equal(LibXML::XML::Node::ELEMENT_NODE, node.node_type)
186
-
187
- node = nodes[1]
188
- assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
189
-
190
- node = nodes[2]
191
- assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
192
-
193
- node = nodes[3]
194
- assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
195
- end
196
-
197
- # Test to make sure we don't get nil on empty results.
198
- # This is also to test that we don't segfault due to our C code getting a NULL pointer
199
- # and not handling it properly.
200
- def test_xpath_empty_result
201
- doc = LibXML::XML::Document.string('<html><body><p>Welcome to XHTML land!</p></body></html>')
202
- nodes = doc.find("//object/param[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'wmode']")
203
- refute_nil nodes
204
- end
205
-
206
- def test_invalid_expression
207
- xml = LibXML::XML::Document.string('<a></a>')
208
-
209
- # Using the expression twice used to cause a Segmentation Fault
210
- error = assert_raises(LibXML::XML::Error) do
211
- xml.find('//a/')
212
- end
213
- assert_equal("Error: Invalid expression.", error.to_s)
214
-
215
- # Try again - this used to cause a Segmentation Fault
216
- error = assert_raises(LibXML::XML::Error) do
217
- xml.find('//a/')
218
- end
219
- assert_equal("Error: Invalid expression.", error.to_s)
220
- end
221
-
222
- def test_find_cdata
223
- doc = LibXML::XML::Document.string('<root>hi there <![CDATA[ mycdata ]]> bye!</root>')
224
-
225
- nodes = doc.find('/root/text()')
226
- assert_equal(3, nodes.length)
227
- assert_equal(nodes[0].node_type, LibXML::XML::Node::TEXT_NODE)
228
- assert_equal(nodes[0].content, 'hi there ')
229
-
230
- assert_equal(nodes[1].node_type, LibXML::XML::Node::CDATA_SECTION_NODE)
231
- assert_equal(nodes[1].content, ' mycdata ')
232
-
233
- assert_equal(nodes[2].node_type, LibXML::XML::Node::TEXT_NODE)
234
- assert_equal(nodes[2].content, ' bye!')
235
- end
236
-
237
- def test_find_comment
238
- doc = LibXML::XML::Document.string('<root>hi there <!-- my comment --> bye!</root>')
239
-
240
- nodes = doc.find('//comment()')
241
- assert_equal(1, nodes.length)
242
- assert_equal(nodes[0].content, ' my comment ')
243
- end
244
- end
1
+ # encoding: UTF-8
2
+
3
+ require_relative './test_helper'
4
+ require 'tempfile'
5
+
6
+ class TestXPath < Minitest::Test
7
+ def setup
8
+ @doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
9
+ end
10
+
11
+ def teardown
12
+ @doc = nil
13
+ end
14
+
15
+ def test_doc_find
16
+ nodes = @doc.find('/soap:Envelope')
17
+ assert_instance_of(LibXML::XML::XPath::Object, nodes)
18
+ assert_equal(1, nodes.length)
19
+ assert_equal(nodes.xpath_type, LibXML::XML::XPath::NODESET)
20
+ end
21
+
22
+ def test_doc_find_first
23
+ node = @doc.find_first('/soap:Envelope/soap:Body')
24
+ assert_instance_of(LibXML::XML::Node, node)
25
+ end
26
+
27
+ def test_ns
28
+ nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
29
+ assert_equal(3, nodes.length)
30
+ end
31
+
32
+ def test_ns_gc
33
+ _stress = GC.stress
34
+ GC.stress = true
35
+
36
+ doc = LibXML::XML::Document.string('<foo xmlns="http://bar.com" />')
37
+ node = doc.root
38
+ # This line segfaults on prior versions of libxml-ruby
39
+ node.find("namespace::*")
40
+
41
+ GC.stress = _stress
42
+ end
43
+
44
+ def test_ns_array
45
+ nodes = @doc.find('//ns1:IdAndName', ['ns1:http://domain.somewhere.com'])
46
+ assert_equal(3, nodes.length)
47
+ end
48
+
49
+ def test_default_ns1
50
+ # Find all nodes with http://services.somewhere.com namespace
51
+ nodes = @doc.find('//*[namespace-uri()="http://services.somewhere.com"]')
52
+ assert_equal(2, nodes.length)
53
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
54
+ assert_equal('IDAndNameList', nodes[1].name)
55
+ end
56
+
57
+ def test_default_ns2
58
+ # Find all nodes with http://services.somewhere.com namespace
59
+ nodes = @doc.find('//ns:*', 'ns:http://services.somewhere.com')
60
+ assert_equal(2, nodes.length)
61
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
62
+ assert_equal('IDAndNameList', nodes[1].name)
63
+
64
+ # Get getManufacturerNamesResponse node
65
+ nodes = @doc.find('//ns:getManufacturerNamesResponse', 'ns:http://services.somewhere.com')
66
+ assert_equal(1, nodes.length)
67
+
68
+ # Get IdAndName node
69
+ nodes = @doc.find('/soap:Envelope/soap:Body/ns0:getManufacturerNamesResponse/ns0:IDAndNameList/ns1:IdAndName',
70
+ ['ns0:http://services.somewhere.com', 'ns1:http://domain.somewhere.com'])
71
+ assert_equal(3, nodes.length)
72
+ end
73
+
74
+ def test_default_ns3
75
+ # Find all nodes with http://services.somewhere.com namespace
76
+ nodes = @doc.find('//ns:*', 'ns' => 'http://services.somewhere.com')
77
+ assert_equal(2, nodes.length)
78
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
79
+ assert_equal('IDAndNameList', nodes[1].name)
80
+ end
81
+
82
+ def test_default_ns4
83
+ # Find all nodes with http://services.somewhere.com namespace
84
+ nodes = @doc.find('//ns:*', :ns => 'http://services.somewhere.com')
85
+ assert_equal(2, nodes.length)
86
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
87
+ assert_equal('IDAndNameList', nodes[1].name)
88
+ end
89
+
90
+ def test_default_ns5
91
+ # Find all nodes with http://services.somewhere.com namespace
92
+ LibXML::XML::Namespace.new(@doc.root, 'ns', 'http://services.somewhere.com')
93
+ nodes = @doc.find('//ns:*')
94
+ assert_equal(2, nodes.length)
95
+ assert_equal('getManufacturerNamesResponse', nodes[0].name)
96
+ assert_equal('IDAndNameList', nodes[1].name)
97
+ end
98
+
99
+ def test_attribute_ns
100
+ # Pull all nodes with http://services.somewhere.com namespace
101
+ nodes = @doc.find('@soap:encodingStyle')
102
+ assert_equal(1, nodes.length)
103
+ assert_equal('encodingStyle', nodes.first.name)
104
+ assert_equal('http://www.w3.org/2001/12/soap-encoding', nodes.first.value)
105
+ end
106
+
107
+ def test_register_default_ns
108
+ doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/atom.xml'))
109
+
110
+ # No namespace has been yet defined
111
+ assert_raises(LibXML::XML::Error) do
112
+ doc.find("atom:title")
113
+ end
114
+
115
+ node = doc.find('atom:title', 'atom:http://www.w3.org/2005/Atom')
116
+ refute_nil(node)
117
+
118
+ # Register namespace
119
+ doc.root.namespaces.default_prefix = 'atom'
120
+ node = doc.find("atom:title")
121
+ refute_nil(node)
122
+ end
123
+
124
+ def test_node_find
125
+ nodes = @doc.find('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
126
+ node = nodes.first
127
+
128
+ # Since we are searching on the node, don't have to register namespace
129
+ nodes = node.find('ns1:name')
130
+ assert_equal(1, nodes.length)
131
+ refute_equal(nodes.first.object_id, nodes.last.object_id)
132
+ assert_equal('name', nodes.first.name)
133
+ assert_equal('man1', nodes.first.content)
134
+ end
135
+
136
+ def test_node_find_first
137
+ node = @doc.find_first('//ns1:IdAndName', 'ns1:http://domain.somewhere.com')
138
+
139
+ # Since we are searching on the node, don't have to register namespace
140
+ node = node.find_first('ns1:name')
141
+ assert_equal('name', node.name)
142
+ assert_equal('man1', node.content)
143
+ end
144
+
145
+ def test_node_no_doc
146
+ node = LibXML::XML::Node.new('header', 'some content')
147
+ assert_raises(TypeError) do
148
+ node = node.find_first('/header')
149
+ end
150
+ end
151
+
152
+ def test_memory
153
+ # This sometimes causes a segmentation fault because
154
+ # an xml document is sometimes freed before the
155
+ # xpath_object used to query it. When the xpath_object
156
+ # is free, it iterates over its results which are pointers
157
+ # to the document's nodes. A segmentation fault then happens.
158
+
159
+ 1000.times do
160
+ doc = LibXML::XML::Document.new('1.0')
161
+ doc.root = LibXML::XML::Node.new("header")
162
+
163
+ 1000.times do
164
+ doc.root << LibXML::XML::Node.new("footer")
165
+ end
166
+
167
+ doc.find('/header/footer')
168
+ end
169
+ end
170
+
171
+ # Test that document doesn't get freed before nodes
172
+ def test_xpath_free
173
+ doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
174
+ nodes = doc.find('//*')
175
+ GC.start
176
+ assert_equal('Envelope', nodes.first.name)
177
+ end
178
+
179
+ def test_xpath_namespace_nodes
180
+ doc = LibXML::XML::Document.string('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml"><entry/></feed>')
181
+ nodes = doc.find('//atom:entry|namespace::*', :atom => "http://www.w3.org/2005/Atom")
182
+ assert_equal(4, nodes.length)
183
+
184
+ node = nodes[0]
185
+ assert_equal(LibXML::XML::Node::ELEMENT_NODE, node.node_type)
186
+
187
+ node = nodes[1]
188
+ assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
189
+
190
+ node = nodes[2]
191
+ assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
192
+
193
+ node = nodes[3]
194
+ assert_equal(LibXML::XML::Node::NAMESPACE_DECL, node.node_type)
195
+ end
196
+
197
+ # Test to make sure we don't get nil on empty results.
198
+ # This is also to test that we don't segfault due to our C code getting a NULL pointer
199
+ # and not handling it properly.
200
+ def test_xpath_empty_result
201
+ doc = LibXML::XML::Document.string('<html><body><p>Welcome to XHTML land!</p></body></html>')
202
+ nodes = doc.find("//object/param[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'wmode']")
203
+ refute_nil nodes
204
+ end
205
+
206
+ def test_invalid_expression
207
+ xml = LibXML::XML::Document.string('<a></a>')
208
+
209
+ # Using the expression twice used to cause a Segmentation Fault
210
+ error = assert_raises(LibXML::XML::Error) do
211
+ xml.find('//a/')
212
+ end
213
+ assert_equal("Error: Invalid expression.", error.to_s)
214
+
215
+ # Try again - this used to cause a Segmentation Fault
216
+ error = assert_raises(LibXML::XML::Error) do
217
+ xml.find('//a/')
218
+ end
219
+ assert_equal("Error: Invalid expression.", error.to_s)
220
+ end
221
+
222
+ def test_find_cdata
223
+ doc = LibXML::XML::Document.string('<root>hi there <![CDATA[ mycdata ]]> bye!</root>')
224
+
225
+ nodes = doc.find('/root/text()')
226
+ assert_equal(3, nodes.length)
227
+ assert_equal(nodes[0].node_type, LibXML::XML::Node::TEXT_NODE)
228
+ assert_equal(nodes[0].content, 'hi there ')
229
+
230
+ assert_equal(nodes[1].node_type, LibXML::XML::Node::CDATA_SECTION_NODE)
231
+ assert_equal(nodes[1].content, ' mycdata ')
232
+
233
+ assert_equal(nodes[2].node_type, LibXML::XML::Node::TEXT_NODE)
234
+ assert_equal(nodes[2].content, ' bye!')
235
+ end
236
+
237
+ def test_find_comment
238
+ doc = LibXML::XML::Document.string('<root>hi there <!-- my comment --> bye!</root>')
239
+
240
+ nodes = doc.find('//comment()')
241
+ assert_equal(1, nodes.length)
242
+ assert_equal(nodes[0].content, ' my comment ')
243
+ end
244
+ end