libxml-ruby 5.0.6 → 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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/{HISTORY → CHANGELOG.md} +875 -930
  3. data/LICENSE +20 -20
  4. data/README.md +67 -0
  5. data/Rakefile +13 -34
  6. data/ext/libxml/extconf.rb +34 -20
  7. data/ext/libxml/libxml.c +2 -14
  8. data/ext/libxml/ruby_libxml.h +1 -1
  9. data/ext/libxml/ruby_xml_attr.c +27 -22
  10. data/ext/libxml/ruby_xml_attr.h +1 -0
  11. data/ext/libxml/ruby_xml_attr_decl.c +19 -12
  12. data/ext/libxml/ruby_xml_attributes.c +18 -9
  13. data/ext/libxml/ruby_xml_document.c +111 -98
  14. data/ext/libxml/ruby_xml_document.h +1 -0
  15. data/ext/libxml/ruby_xml_dtd.c +49 -19
  16. data/ext/libxml/ruby_xml_dtd.h +1 -0
  17. data/ext/libxml/ruby_xml_html_parser.c +1 -1
  18. data/ext/libxml/ruby_xml_html_parser_context.c +19 -6
  19. data/ext/libxml/ruby_xml_html_parser_context.h +1 -0
  20. data/ext/libxml/ruby_xml_input_cbg.c +45 -17
  21. data/ext/libxml/ruby_xml_io.c +3 -1
  22. data/ext/libxml/ruby_xml_namespace.c +20 -12
  23. data/ext/libxml/ruby_xml_namespace.h +1 -0
  24. data/ext/libxml/ruby_xml_namespaces.c +40 -20
  25. data/ext/libxml/ruby_xml_node.c +71 -32
  26. data/ext/libxml/ruby_xml_node.h +2 -0
  27. data/ext/libxml/ruby_xml_parser.c +1 -1
  28. data/ext/libxml/ruby_xml_parser_context.c +54 -47
  29. data/ext/libxml/ruby_xml_parser_context.h +1 -0
  30. data/ext/libxml/ruby_xml_reader.c +25 -15
  31. data/ext/libxml/ruby_xml_registry.c +31 -0
  32. data/ext/libxml/ruby_xml_registry.h +22 -0
  33. data/ext/libxml/ruby_xml_relaxng.c +21 -5
  34. data/ext/libxml/ruby_xml_relaxng.h +1 -0
  35. data/ext/libxml/ruby_xml_sax_parser.c +1 -1
  36. data/ext/libxml/ruby_xml_schema.c +18 -11
  37. data/ext/libxml/ruby_xml_schema.h +1 -0
  38. data/ext/libxml/ruby_xml_schema_attribute.c +7 -7
  39. data/ext/libxml/ruby_xml_schema_element.c +8 -8
  40. data/ext/libxml/ruby_xml_schema_facet.c +7 -7
  41. data/ext/libxml/ruby_xml_schema_type.c +12 -19
  42. data/ext/libxml/ruby_xml_version.h +4 -4
  43. data/ext/libxml/ruby_xml_writer.c +18 -6
  44. data/ext/libxml/ruby_xml_xpath.c +2 -2
  45. data/ext/libxml/ruby_xml_xpath.h +1 -1
  46. data/ext/libxml/ruby_xml_xpath_context.c +72 -26
  47. data/ext/libxml/ruby_xml_xpath_expression.c +11 -5
  48. data/ext/libxml/ruby_xml_xpath_expression.h +1 -0
  49. data/ext/libxml/ruby_xml_xpath_object.c +69 -54
  50. data/ext/libxml/ruby_xml_xpath_object.h +3 -1
  51. data/ext/vc/libxml_ruby/libxml_ruby.vcxproj +271 -0
  52. data/ext/xcode/libxml-ruby.xcodeproj/project.pbxproj +633 -0
  53. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  54. data/ext/xcode/libxml-ruby.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  55. data/ext/xcode/libxml-ruby.xcodeproj/xcshareddata/xcschemes/libxml-ruby.xcscheme +80 -0
  56. data/lib/libxml/document.rb +0 -29
  57. data/lib/libxml/error.rb +30 -3
  58. data/lib/libxml/html_parser.rb +0 -16
  59. data/lib/libxml/node.rb +1 -3
  60. data/lib/libxml/parser.rb +0 -8
  61. data/lib/libxml/schema/attribute.rb +27 -19
  62. data/lib/libxml/schema/element.rb +20 -0
  63. data/lib/libxml/schema/type.rb +44 -21
  64. data/lib/libxml/schema.rb +47 -47
  65. data/lib/libxml-ruby.rb +30 -30
  66. data/libxml-ruby.gemspec +12 -16
  67. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  68. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  69. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  70. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  71. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  72. data/test/c14n/result/with-comments/example-1 +5 -5
  73. data/test/c14n/result/with-comments/example-2 +10 -10
  74. data/test/c14n/result/with-comments/example-3 +13 -13
  75. data/test/c14n/result/with-comments/example-4 +8 -8
  76. data/test/c14n/result/with-comments/example-5 +3 -3
  77. data/test/c14n/result/without-comments/example-1 +3 -3
  78. data/test/c14n/result/without-comments/example-2 +10 -10
  79. data/test/c14n/result/without-comments/example-3 +13 -13
  80. data/test/c14n/result/without-comments/example-4 +8 -8
  81. data/test/c14n/result/without-comments/example-5 +2 -2
  82. data/test/test_attr.rb +179 -180
  83. data/test/test_attr_decl.rb +131 -131
  84. data/test/test_attributes.rb +135 -135
  85. data/test/test_canonicalize.rb +122 -120
  86. data/test/test_document.rb +2 -4
  87. data/test/test_dtd.rb +12 -4
  88. data/test/test_encoding_sax.rb +114 -114
  89. data/test/test_error.rb +1 -1
  90. data/test/test_html_parser.rb +6 -2
  91. data/test/test_html_parser_context.rb +22 -22
  92. data/test/test_input_callbacks.rb +36 -0
  93. data/test/test_namespace.rb +1 -1
  94. data/test/test_namespaces.rb +200 -200
  95. data/test/test_node.rb +16 -0
  96. data/test/test_node_cdata.rb +50 -50
  97. data/test/test_node_comment.rb +32 -32
  98. data/test/test_node_copy.rb +40 -40
  99. data/test/test_node_edit.rb +176 -158
  100. data/test/test_node_pi.rb +37 -37
  101. data/test/test_node_text.rb +69 -69
  102. data/test/test_node_xlink.rb +28 -28
  103. data/test/test_parser.rb +5 -41
  104. data/test/test_parser_context.rb +198 -198
  105. data/test/test_properties.rb +38 -38
  106. data/test/test_reader.rb +55 -6
  107. data/test/test_relaxng.rb +59 -53
  108. data/test/test_sax_parser.rb +345 -345
  109. data/test/test_schema.rb +28 -0
  110. data/test/test_traversal.rb +152 -152
  111. data/test/test_writer.rb +0 -31
  112. data/test/test_xinclude.rb +20 -20
  113. data/test/test_xml.rb +3 -7
  114. data/test/test_xpath.rb +244 -244
  115. data/test/test_xpath_context.rb +87 -87
  116. data/test/test_xpath_expression.rb +37 -37
  117. metadata +32 -25
  118. data/README.rdoc +0 -208
  119. data/ext/libxml/extconf.h +0 -4
  120. data/ext/libxml/ruby_xml_cbg.c +0 -85
  121. data/lib/libxml/hpricot.rb +0 -78
  122. data/lib/libxml.rb +0 -5
  123. data/lib/xml/libxml.rb +0 -10
  124. data/lib/xml.rb +0 -14
  125. data/script/benchmark/depixelate +0 -634
  126. data/script/benchmark/hamlet.xml +0 -9055
  127. data/script/benchmark/parsecount +0 -170
  128. data/script/benchmark/sock_entries.xml +0 -507
  129. data/script/benchmark/throughput +0 -41
  130. data/script/test +0 -6
  131. data/test/test_deprecated_require.rb +0 -12
@@ -1,345 +1,345 @@
1
- # encoding: UTF-8
2
-
3
- require_relative './test_helper'
4
- require 'stringio'
5
-
6
- class DocTypeCallback
7
- include LibXML::XML::SaxParser::Callbacks
8
- def on_start_element(element, attributes)
9
- end
10
- end
11
-
12
- class TestCaseCallbacks
13
- include LibXML::XML::SaxParser::Callbacks
14
-
15
- attr_accessor :result
16
-
17
- def initialize
18
- @result = Array.new
19
- end
20
-
21
- def on_cdata_block(cdata)
22
- @result << "cdata: #{cdata}"
23
- end
24
-
25
- def on_characters(chars)
26
- @result << "characters: #{chars}"
27
- end
28
-
29
- def on_comment(text)
30
- @result << "comment: #{text}"
31
- end
32
-
33
- def on_end_document
34
- @result << "end_document"
35
- end
36
-
37
- def on_end_element(name)
38
- @result << "end_element: #{name}"
39
- end
40
-
41
- def on_end_element_ns(name, prefix, uri)
42
- @result << "end_element_ns #{name}, prefix: #{prefix}, uri: #{uri}"
43
- end
44
-
45
- # Called for parser errors.
46
- def on_error(error)
47
- @result << "error: #{error}"
48
- end
49
-
50
- def on_processing_instruction(target, data)
51
- @result << "pi: #{target} #{data}"
52
- end
53
-
54
- def on_start_document
55
- @result << "startdoc"
56
- end
57
-
58
- def on_start_element(name, attributes)
59
- attributes ||= Hash.new
60
- @result << "start_element: #{name}, attr: #{attributes.inspect}"
61
- end
62
-
63
- def on_start_element_ns(name, attributes, prefix, uri, namespaces)
64
- attributes ||= Hash.new
65
- namespaces ||= Hash.new
66
- @result << "start_element_ns: #{name}, attr: #{attributes.inspect}, prefix: #{prefix}, uri: #{uri}, ns: #{namespaces.inspect}"
67
- end
68
- end
69
-
70
- class TestSaxParser < Minitest::Test
71
- def saxtest_file
72
- File.join(File.dirname(__FILE__), 'model/atom.xml')
73
- end
74
-
75
- def verify(parser)
76
- result = parser.callbacks.result
77
-
78
- i = -1
79
- assert_equal("startdoc", result[i+=1])
80
- assert_equal("pi: xml-stylesheet type=\"text/xsl\" href=\"my_stylesheet.xsl\"", result[i+=1])
81
- assert_equal("start_element: feed, attr: {}", result[i+=1])
82
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
83
- assert_equal("start_element_ns: feed, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {nil=>\"http://www.w3.org/2005/Atom\"}", result[i+=1])
84
- else
85
- assert_equal("start_element_ns: feed, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {nil => \"http://www.w3.org/2005/Atom\"}", result[i+=1])
86
- end
87
- assert_equal("characters: \n ", result[i+=1])
88
- assert_equal("comment: Not a valid atom entry ", result[i+=1])
89
- assert_equal("characters: \n ", result[i+=1])
90
- assert_equal("start_element: entry, attr: {}", result[i+=1])
91
- assert_equal("start_element_ns: entry, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
92
- assert_equal("characters: \n ", result[i+=1])
93
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
94
- assert_equal("start_element: title, attr: {\"type\"=>\"html\"}", result[i+=1])
95
- assert_equal("start_element_ns: title, attr: {\"type\"=>\"html\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
96
- else
97
- assert_equal("start_element: title, attr: {\"type\" => \"html\"}", result[i+=1])
98
- assert_equal("start_element_ns: title, attr: {\"type\" => \"html\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
99
- end
100
-
101
- assert_equal("cdata: <<strong>>", result[i+=1])
102
- assert_equal("end_element: title", result[i+=1])
103
- assert_equal("end_element_ns title, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
104
- assert_equal("characters: \n ", result[i+=1])
105
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
106
- assert_equal("start_element: content, attr: {\"type\"=>\"xhtml\"}", result[i+=1])
107
- assert_equal("start_element_ns: content, attr: {\"type\"=>\"xhtml\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
108
- else
109
- assert_equal("start_element: content, attr: {\"type\" => \"xhtml\"}", result[i+=1])
110
- assert_equal("start_element_ns: content, attr: {\"type\" => \"xhtml\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
111
- end
112
- assert_equal("characters: \n ", result[i+=1])
113
- assert_equal("start_element: xhtml:div, attr: {}", result[i+=1])
114
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
115
- assert_equal("start_element_ns: div, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {\"xhtml\"=>\"http://www.w3.org/1999/xhtml\"}", result[i+=1])
116
- else
117
- assert_equal("start_element_ns: div, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {\"xhtml\" => \"http://www.w3.org/1999/xhtml\"}", result[i+=1])
118
- end
119
- assert_equal("characters: \n ", result[i+=1])
120
- assert_equal("start_element: xhtml:p, attr: {}", result[i+=1])
121
- assert_equal("start_element_ns: p, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {}", result[i+=1])
122
- assert_equal("characters: hi there", result[i+=1])
123
- assert_equal("end_element: xhtml:p", result[i+=1])
124
- assert_equal("end_element_ns p, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
125
- assert_equal("characters: \n ", result[i+=1])
126
- assert_equal("end_element: xhtml:div", result[i+=1])
127
- assert_equal("end_element_ns div, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
128
- assert_equal("characters: \n ", result[i+=1])
129
- assert_equal("end_element: content", result[i+=1])
130
- assert_equal("end_element_ns content, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
131
- assert_equal("characters: \n ", result[i+=1])
132
- assert_equal("end_element: entry", result[i+=1])
133
- assert_equal("end_element_ns entry, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
134
- assert_equal("characters: \n", result[i+=1])
135
- assert_equal("end_element: feed", result[i+=1])
136
- assert_equal("end_element_ns feed, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
137
- assert_equal("end_document", result[i+=1])
138
- end
139
-
140
- def test_file
141
- parser = LibXML::XML::SaxParser.file(saxtest_file)
142
- parser.callbacks = TestCaseCallbacks.new
143
- parser.parse
144
- verify(parser)
145
- end
146
-
147
- def test_file_no_callbacks
148
- parser = LibXML::XML::SaxParser.file(saxtest_file)
149
- assert_equal true, parser.parse
150
- end
151
-
152
- def test_noexistent_file
153
- error = assert_raises(LibXML::XML::Error) do
154
- LibXML::XML::SaxParser.file('i_dont_exist.xml')
155
- end
156
-
157
- assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
158
- end
159
-
160
- def test_nil_file
161
- error = assert_raises(TypeError) do
162
- LibXML::XML::SaxParser.file(nil)
163
- end
164
-
165
- assert_match(/nil into String/, error.to_s)
166
- end
167
-
168
- def test_io
169
- File.open(saxtest_file) do |file|
170
- parser = LibXML::XML::SaxParser.io(file)
171
- parser.callbacks = TestCaseCallbacks.new
172
- parser.parse
173
- verify(parser)
174
- end
175
- end
176
-
177
- def test_nil_io
178
- error = assert_raises(TypeError) do
179
- LibXML::XML::HTMLParser.io(nil)
180
- end
181
-
182
- assert_equal("Must pass in an IO object", error.to_s)
183
- end
184
-
185
- def test_string_no_callbacks
186
- xml = File.read(saxtest_file)
187
- parser = LibXML::XML::SaxParser.string(xml)
188
- assert_equal true, parser.parse
189
- end
190
-
191
- def test_string
192
- xml = File.read(saxtest_file)
193
- parser = LibXML::XML::SaxParser.string(xml)
194
- parser.callbacks = TestCaseCallbacks.new
195
- parser.parse
196
- verify(parser)
197
- end
198
-
199
- def test_string_io
200
- xml = File.read(saxtest_file)
201
- io = StringIO.new(xml)
202
- parser = LibXML::XML::SaxParser.io(io)
203
-
204
- parser.callbacks = TestCaseCallbacks.new
205
- parser.parse
206
- verify(parser)
207
- end
208
-
209
- def test_nil_string
210
- error = assert_raises(TypeError) do
211
- LibXML::XML::SaxParser.string(nil)
212
- end
213
-
214
- assert_equal("wrong argument type nil (expected String)", error.to_s)
215
- end
216
-
217
- def test_doctype
218
- xml = <<-EOS
219
- <?xml version="1.0" encoding="UTF-8"?>
220
- <!DOCTYPE Results SYSTEM "results.dtd">
221
- <Results>
222
- <a>a1</a>
223
- </Results>
224
- EOS
225
- parser = LibXML::XML::SaxParser.string(xml)
226
- parser.callbacks = DocTypeCallback.new
227
- doc = parser.parse
228
- refute_nil(doc)
229
- end
230
-
231
- def test_parse_warning
232
- # Two xml PIs is a warning
233
- xml = <<-EOS
234
- <?xml version="1.0" encoding="utf-8"?>
235
- <?xml-invalid?>
236
- <Test/>
237
- EOS
238
-
239
- parser = LibXML::XML::SaxParser.string(xml)
240
- parser.callbacks = TestCaseCallbacks.new
241
-
242
- parser.parse
243
-
244
- # Check callbacks
245
- result = parser.callbacks.result
246
- i = -1
247
- assert_equal("startdoc", result[i+=1])
248
- assert_equal("error: Warning: xmlParsePITarget: invalid name prefix 'xml' at :2.", result[i+=1])
249
- assert_equal("pi: xml-invalid ", result[i+=1])
250
- assert_equal("start_element: Test, attr: {}", result[i+=1])
251
- assert_equal("start_element_ns: Test, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
252
- assert_equal("end_element: Test", result[i+=1])
253
- assert_equal("end_element_ns Test, prefix: , uri: ", result[i+=1])
254
- assert_equal("end_document", result[i+=1])
255
- end
256
-
257
- def test_parse_error
258
- xml = <<-EOS
259
- <Results>
260
- EOS
261
- parser = LibXML::XML::SaxParser.string(xml)
262
- parser.callbacks = TestCaseCallbacks.new
263
-
264
- error = assert_raises(LibXML::XML::Error) do
265
- parser.parse
266
- end
267
-
268
- # Check callbacks
269
- result = parser.callbacks.result
270
-
271
- i = -1
272
-
273
- base_err_msg = "Fatal error: (Premature end of data in tag Results line 1|EndTag: '<\\/' not found) at :2\\."
274
- re_err_msg1 = /\A(error: )#{base_err_msg}\z/
275
- re_err_msg2 = /\A#{base_err_msg}\z/
276
-
277
- assert_equal("startdoc", result[i+=1])
278
- assert_equal("start_element: Results, attr: {}", result[i+=1])
279
- assert_equal("start_element_ns: Results, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
280
- assert_equal("characters: \n", result[i+=1])
281
- assert_match(re_err_msg1, result[i+=1])
282
- assert_equal("end_document", result[i+=1])
283
-
284
- refute_nil(error)
285
- assert_kind_of(LibXML::XML::Error, error)
286
- assert_match(re_err_msg2, error.message)
287
- assert_equal(LibXML::XML::Error::PARSER, error.domain)
288
-
289
- assert([LibXML::XML::Error::TAG_NOT_FINISHED, LibXML::XML::Error::LTSLASH_REQUIRED].include?(error.code))
290
- assert_equal(LibXML::XML::Error::FATAL, error.level)
291
- assert_nil(error.file)
292
- assert_equal(2, error.line)
293
- # Sometimes this is nil and sometimes its not depending on OS and libxlm version
294
- # assert_nil(error.str1)
295
- assert_nil(error.str2)
296
- assert_nil(error.str3)
297
- assert([0, 1].include?(error.int1))
298
- assert_equal(1, error.int2)
299
- assert_nil(error.node)
300
- end
301
-
302
- def test_parse_seg_fail
303
- xml = <<-EOS
304
- <?xml version="1.0" encoding="ISO-8859-1" ?>
305
- <Products>
306
- <Product>
307
- <ProductDescription>
308
- AQUALIA THERMAL Lichte cr├иme - Versterkende & kalmerende 24 u hydraterende verzorging<br />
309
- Huid wordt continu gehydrateerd, intens versterkt en gekalmeerd.<br />
310
- Hypoallergeen. Geschikt voor de gevoelige huid.<br />
311
- <br />
312
- 01.EFFECTIVITEIT<br />
313
- Intensief gehydrateerd, de huid voelt gekalmeerd. Ze voelt de hele dag soepel en fluweelzacht aan, zonder een trekkerig gevoel. De huid is elastischer, soepeler en stralender. Doeltreffendheid getest onder dermatologisch toezicht. <br />
314
- <br />
315
- 02.GEBRUIK<br />
316
- 's Morgens en/ of 's avonds aanbrengen. <br />
317
- <br />
318
- 03.ACTIEVE INGREDIENTEN<br />
319
- Technologische innovatie: 24 u continue cellulaire vochtnevel. Voor de 1ste keer worden Thermaal Bronwater van Vichy, rijk aan zeldzame mineralen en Actief HyaluronineтДв verwerkt in microcapsules, die deze vervolgens verspreiden in de cellen. <br />
320
- <br />
321
- 04.TEXTUUR<br />
322
- De lichte cr├иme is verfrissend en trekt makkelijk in. Niet vet en niet kleverig. Zonder 'maskereffect'. <br />
323
- <br />
324
- 05.GEUR<br />
325
- Geparfumeerd <br />
326
- <br />
327
- 06.INHOUD<br />
328
- 40 ml tube <br />
329
- </ProductDescription>
330
- </Product>
331
- </Products>
332
- EOS
333
-
334
- parser = LibXML::XML::SaxParser.string(xml)
335
- parser.callbacks = TestCaseCallbacks.new
336
-
337
- error = assert_raises(LibXML::XML::Error) do
338
- parser.parse
339
- end
340
- assert_match("Fatal error: xmlParseEntityRef: no name at", error.to_s)
341
-
342
- # Check callbacks
343
- parser.callbacks.result
344
- end
345
- end
1
+ # encoding: UTF-8
2
+
3
+ require_relative './test_helper'
4
+ require 'stringio'
5
+
6
+ class DocTypeCallback
7
+ include LibXML::XML::SaxParser::Callbacks
8
+ def on_start_element(element, attributes)
9
+ end
10
+ end
11
+
12
+ class TestCaseCallbacks
13
+ include LibXML::XML::SaxParser::Callbacks
14
+
15
+ attr_accessor :result
16
+
17
+ def initialize
18
+ @result = Array.new
19
+ end
20
+
21
+ def on_cdata_block(cdata)
22
+ @result << "cdata: #{cdata}"
23
+ end
24
+
25
+ def on_characters(chars)
26
+ @result << "characters: #{chars}"
27
+ end
28
+
29
+ def on_comment(text)
30
+ @result << "comment: #{text}"
31
+ end
32
+
33
+ def on_end_document
34
+ @result << "end_document"
35
+ end
36
+
37
+ def on_end_element(name)
38
+ @result << "end_element: #{name}"
39
+ end
40
+
41
+ def on_end_element_ns(name, prefix, uri)
42
+ @result << "end_element_ns #{name}, prefix: #{prefix}, uri: #{uri}"
43
+ end
44
+
45
+ # Called for parser errors.
46
+ def on_error(error)
47
+ @result << "error: #{error}"
48
+ end
49
+
50
+ def on_processing_instruction(target, data)
51
+ @result << "pi: #{target} #{data}"
52
+ end
53
+
54
+ def on_start_document
55
+ @result << "startdoc"
56
+ end
57
+
58
+ def on_start_element(name, attributes)
59
+ attributes ||= Hash.new
60
+ @result << "start_element: #{name}, attr: #{attributes.inspect}"
61
+ end
62
+
63
+ def on_start_element_ns(name, attributes, prefix, uri, namespaces)
64
+ attributes ||= Hash.new
65
+ namespaces ||= Hash.new
66
+ @result << "start_element_ns: #{name}, attr: #{attributes.inspect}, prefix: #{prefix}, uri: #{uri}, ns: #{namespaces.inspect}"
67
+ end
68
+ end
69
+
70
+ class TestSaxParser < Minitest::Test
71
+ def saxtest_file
72
+ File.join(File.dirname(__FILE__), 'model/atom.xml')
73
+ end
74
+
75
+ def verify(parser)
76
+ result = parser.callbacks.result
77
+
78
+ i = -1
79
+ assert_equal("startdoc", result[i+=1])
80
+ assert_equal("pi: xml-stylesheet type=\"text/xsl\" href=\"my_stylesheet.xsl\"", result[i+=1])
81
+ assert_equal("start_element: feed, attr: {}", result[i+=1])
82
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
83
+ assert_equal("start_element_ns: feed, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {nil=>\"http://www.w3.org/2005/Atom\"}", result[i+=1])
84
+ else
85
+ assert_equal("start_element_ns: feed, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {nil => \"http://www.w3.org/2005/Atom\"}", result[i+=1])
86
+ end
87
+ assert_equal("characters: \n ", result[i+=1])
88
+ assert_equal("comment: Not a valid atom entry ", result[i+=1])
89
+ assert_equal("characters: \n ", result[i+=1])
90
+ assert_equal("start_element: entry, attr: {}", result[i+=1])
91
+ assert_equal("start_element_ns: entry, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
92
+ assert_equal("characters: \n ", result[i+=1])
93
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
94
+ assert_equal("start_element: title, attr: {\"type\"=>\"html\"}", result[i+=1])
95
+ assert_equal("start_element_ns: title, attr: {\"type\"=>\"html\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
96
+ else
97
+ assert_equal("start_element: title, attr: {\"type\" => \"html\"}", result[i+=1])
98
+ assert_equal("start_element_ns: title, attr: {\"type\" => \"html\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
99
+ end
100
+
101
+ assert_equal("cdata: <<strong>>", result[i+=1])
102
+ assert_equal("end_element: title", result[i+=1])
103
+ assert_equal("end_element_ns title, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
104
+ assert_equal("characters: \n ", result[i+=1])
105
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
106
+ assert_equal("start_element: content, attr: {\"type\"=>\"xhtml\"}", result[i+=1])
107
+ assert_equal("start_element_ns: content, attr: {\"type\"=>\"xhtml\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
108
+ else
109
+ assert_equal("start_element: content, attr: {\"type\" => \"xhtml\"}", result[i+=1])
110
+ assert_equal("start_element_ns: content, attr: {\"type\" => \"xhtml\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
111
+ end
112
+ assert_equal("characters: \n ", result[i+=1])
113
+ assert_equal("start_element: xhtml:div, attr: {}", result[i+=1])
114
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.4')
115
+ assert_equal("start_element_ns: div, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {\"xhtml\"=>\"http://www.w3.org/1999/xhtml\"}", result[i+=1])
116
+ else
117
+ assert_equal("start_element_ns: div, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {\"xhtml\" => \"http://www.w3.org/1999/xhtml\"}", result[i+=1])
118
+ end
119
+ assert_equal("characters: \n ", result[i+=1])
120
+ assert_equal("start_element: xhtml:p, attr: {}", result[i+=1])
121
+ assert_equal("start_element_ns: p, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {}", result[i+=1])
122
+ assert_equal("characters: hi there", result[i+=1])
123
+ assert_equal("end_element: xhtml:p", result[i+=1])
124
+ assert_equal("end_element_ns p, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
125
+ assert_equal("characters: \n ", result[i+=1])
126
+ assert_equal("end_element: xhtml:div", result[i+=1])
127
+ assert_equal("end_element_ns div, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
128
+ assert_equal("characters: \n ", result[i+=1])
129
+ assert_equal("end_element: content", result[i+=1])
130
+ assert_equal("end_element_ns content, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
131
+ assert_equal("characters: \n ", result[i+=1])
132
+ assert_equal("end_element: entry", result[i+=1])
133
+ assert_equal("end_element_ns entry, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
134
+ assert_equal("characters: \n", result[i+=1])
135
+ assert_equal("end_element: feed", result[i+=1])
136
+ assert_equal("end_element_ns feed, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
137
+ assert_equal("end_document", result[i+=1])
138
+ end
139
+
140
+ def test_file
141
+ parser = LibXML::XML::SaxParser.file(saxtest_file)
142
+ parser.callbacks = TestCaseCallbacks.new
143
+ parser.parse
144
+ verify(parser)
145
+ end
146
+
147
+ def test_file_no_callbacks
148
+ parser = LibXML::XML::SaxParser.file(saxtest_file)
149
+ assert_equal true, parser.parse
150
+ end
151
+
152
+ def test_noexistent_file
153
+ error = assert_raises(LibXML::XML::Error) do
154
+ LibXML::XML::SaxParser.file('i_dont_exist.xml')
155
+ end
156
+
157
+ assert_match(/Warning: failed to load.*i_dont_exist\.xml/, error.to_s)
158
+ end
159
+
160
+ def test_nil_file
161
+ error = assert_raises(TypeError) do
162
+ LibXML::XML::SaxParser.file(nil)
163
+ end
164
+
165
+ assert_match(/nil into String/, error.to_s)
166
+ end
167
+
168
+ def test_io
169
+ File.open(saxtest_file) do |file|
170
+ parser = LibXML::XML::SaxParser.io(file)
171
+ parser.callbacks = TestCaseCallbacks.new
172
+ parser.parse
173
+ verify(parser)
174
+ end
175
+ end
176
+
177
+ def test_nil_io
178
+ error = assert_raises(TypeError) do
179
+ LibXML::XML::HTMLParser.io(nil)
180
+ end
181
+
182
+ assert_equal("Must pass in an IO object", error.to_s)
183
+ end
184
+
185
+ def test_string_no_callbacks
186
+ xml = File.read(saxtest_file)
187
+ parser = LibXML::XML::SaxParser.string(xml)
188
+ assert_equal true, parser.parse
189
+ end
190
+
191
+ def test_string
192
+ xml = File.read(saxtest_file)
193
+ parser = LibXML::XML::SaxParser.string(xml)
194
+ parser.callbacks = TestCaseCallbacks.new
195
+ parser.parse
196
+ verify(parser)
197
+ end
198
+
199
+ def test_string_io
200
+ xml = File.read(saxtest_file)
201
+ io = StringIO.new(xml)
202
+ parser = LibXML::XML::SaxParser.io(io)
203
+
204
+ parser.callbacks = TestCaseCallbacks.new
205
+ parser.parse
206
+ verify(parser)
207
+ end
208
+
209
+ def test_nil_string
210
+ error = assert_raises(TypeError) do
211
+ LibXML::XML::SaxParser.string(nil)
212
+ end
213
+
214
+ assert_equal("wrong argument type nil (expected String)", error.to_s)
215
+ end
216
+
217
+ def test_doctype
218
+ xml = <<-EOS
219
+ <?xml version="1.0" encoding="UTF-8"?>
220
+ <!DOCTYPE Results SYSTEM "results.dtd">
221
+ <Results>
222
+ <a>a1</a>
223
+ </Results>
224
+ EOS
225
+ parser = LibXML::XML::SaxParser.string(xml)
226
+ parser.callbacks = DocTypeCallback.new
227
+ doc = parser.parse
228
+ refute_nil(doc)
229
+ end
230
+
231
+ def test_parse_warning
232
+ # Two xml PIs is a warning
233
+ xml = <<-EOS
234
+ <?xml version="1.0" encoding="utf-8"?>
235
+ <?xml-invalid?>
236
+ <Test/>
237
+ EOS
238
+
239
+ parser = LibXML::XML::SaxParser.string(xml)
240
+ parser.callbacks = TestCaseCallbacks.new
241
+
242
+ parser.parse
243
+
244
+ # Check callbacks
245
+ result = parser.callbacks.result
246
+ i = -1
247
+ assert_equal("startdoc", result[i+=1])
248
+ assert_equal("error: Warning: xmlParsePITarget: invalid name prefix 'xml' at :2.", result[i+=1])
249
+ assert_equal("pi: xml-invalid ", result[i+=1])
250
+ assert_equal("start_element: Test, attr: {}", result[i+=1])
251
+ assert_equal("start_element_ns: Test, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
252
+ assert_equal("end_element: Test", result[i+=1])
253
+ assert_equal("end_element_ns Test, prefix: , uri: ", result[i+=1])
254
+ assert_equal("end_document", result[i+=1])
255
+ end
256
+
257
+ def test_parse_error
258
+ xml = <<-EOS
259
+ <Results>
260
+ EOS
261
+ parser = LibXML::XML::SaxParser.string(xml)
262
+ parser.callbacks = TestCaseCallbacks.new
263
+
264
+ error = assert_raises(LibXML::XML::Error) do
265
+ parser.parse
266
+ end
267
+
268
+ # Check callbacks
269
+ result = parser.callbacks.result
270
+
271
+ i = -1
272
+
273
+ base_err_msg = "Fatal error: (Premature end of data in tag Results line 1|EndTag: '<\\/' not found) at :2\\."
274
+ re_err_msg1 = /\A(error: )#{base_err_msg}\z/
275
+ re_err_msg2 = /\A#{base_err_msg}\z/
276
+
277
+ assert_equal("startdoc", result[i+=1])
278
+ assert_equal("start_element: Results, attr: {}", result[i+=1])
279
+ assert_equal("start_element_ns: Results, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
280
+ assert_equal("characters: \n", result[i+=1])
281
+ assert_match(re_err_msg1, result[i+=1])
282
+ assert_equal("end_document", result[i+=1])
283
+
284
+ refute_nil(error)
285
+ assert_kind_of(LibXML::XML::Error, error)
286
+ assert_match(re_err_msg2, error.message)
287
+ assert_equal(LibXML::XML::Error::PARSER, error.domain)
288
+
289
+ assert([LibXML::XML::Error::TAG_NOT_FINISHED, LibXML::XML::Error::LTSLASH_REQUIRED].include?(error.code))
290
+ assert_equal(LibXML::XML::Error::FATAL, error.level)
291
+ assert_nil(error.file)
292
+ assert_equal(2, error.line)
293
+ # Sometimes this is nil and sometimes its not depending on OS and libxlm version
294
+ # assert_nil(error.str1)
295
+ assert_nil(error.str2)
296
+ assert_nil(error.str3)
297
+ assert([0, 1].include?(error.int1))
298
+ assert_equal(1, error.int2)
299
+ assert_nil(error.node)
300
+ end
301
+
302
+ def test_parse_seg_fail
303
+ xml = <<-EOS
304
+ <?xml version="1.0" encoding="ISO-8859-1" ?>
305
+ <Products>
306
+ <Product>
307
+ <ProductDescription>
308
+ AQUALIA THERMAL Lichte cr├иme - Versterkende & kalmerende 24 u hydraterende verzorging<br />
309
+ Huid wordt continu gehydrateerd, intens versterkt en gekalmeerd.<br />
310
+ Hypoallergeen. Geschikt voor de gevoelige huid.<br />
311
+ <br />
312
+ 01.EFFECTIVITEIT<br />
313
+ Intensief gehydrateerd, de huid voelt gekalmeerd. Ze voelt de hele dag soepel en fluweelzacht aan, zonder een trekkerig gevoel. De huid is elastischer, soepeler en stralender. Doeltreffendheid getest onder dermatologisch toezicht. <br />
314
+ <br />
315
+ 02.GEBRUIK<br />
316
+ 's Morgens en/ of 's avonds aanbrengen. <br />
317
+ <br />
318
+ 03.ACTIEVE INGREDIENTEN<br />
319
+ Technologische innovatie: 24 u continue cellulaire vochtnevel. Voor de 1ste keer worden Thermaal Bronwater van Vichy, rijk aan zeldzame mineralen en Actief HyaluronineтДв verwerkt in microcapsules, die deze vervolgens verspreiden in de cellen. <br />
320
+ <br />
321
+ 04.TEXTUUR<br />
322
+ De lichte cr├иme is verfrissend en trekt makkelijk in. Niet vet en niet kleverig. Zonder 'maskereffect'. <br />
323
+ <br />
324
+ 05.GEUR<br />
325
+ Geparfumeerd <br />
326
+ <br />
327
+ 06.INHOUD<br />
328
+ 40 ml tube <br />
329
+ </ProductDescription>
330
+ </Product>
331
+ </Products>
332
+ EOS
333
+
334
+ parser = LibXML::XML::SaxParser.string(xml)
335
+ parser.callbacks = TestCaseCallbacks.new
336
+
337
+ error = assert_raises(LibXML::XML::Error) do
338
+ parser.parse
339
+ end
340
+ assert_match("Fatal error: xmlParseEntityRef: no name at", error.to_s)
341
+
342
+ # Check callbacks
343
+ parser.callbacks.result
344
+ end
345
+ end