rxerces 0.6.1 → 0.7.1

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.
@@ -1,3 +1,3 @@
1
1
  module RXerces
2
- VERSION = "0.6.1".freeze
2
+ VERSION = "0.7.1".freeze
3
3
  end
data/lib/rxerces.rb CHANGED
@@ -5,9 +5,10 @@ require 'rxerces/version'
5
5
  module RXerces
6
6
  # Parse XML from a string
7
7
  # @param string [String] XML string to parse
8
+ # @param options [Hash] parsing options
8
9
  # @return [RXerces::XML::Document] parsed document
9
- def self.XML(string)
10
- RXerces::XML::Document.parse(string)
10
+ def self.XML(string, **options)
11
+ RXerces::XML::Document.parse(string, **options)
11
12
  end
12
13
 
13
14
  # Alias for compatibility
@@ -0,0 +1 @@
1
+ 4d38e048acd6037386109d5b671c47322a28295b8f03d4f61143a3988ae9ce26 rxerces-0.7.1-unsigned.gem
data/rxerces.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "rxerces"
3
- spec.version = "0.6.1"
3
+ spec.version = "0.7.1"
4
4
  spec.author = "Daniel J. Berger"
5
5
  spec.email = "djberg96@gmail.com"
6
6
  spec.cert_chain = ["certs/djberg96_pub.pem"]
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.add_development_dependency "rake", "~> 13.0"
17
17
  spec.add_development_dependency "rake-compiler", "~> 1.2"
18
18
  spec.add_development_dependency "rspec", "~> 3.12"
19
+ spec.add_development_dependency "mkmf-lite", "~> 0.7.5"
19
20
 
20
21
  spec.description = <<-EOF
21
22
  A Ruby XML library with Nokogiri-compatible API, powered by Xerces-C
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Native binding operations' do
4
+ it 'preserves child order and node types while walking siblings' do
5
+ doc = RXerces.XML('<root>text<!--before--><first/><![CDATA[data]]><last/><!--after--> </root>')
6
+ root = doc.root
7
+
8
+ expect(root.children.map(&:name)).to eq(
9
+ ['#text', '#comment', 'first', '#cdata-section', 'last', '#comment', '#text']
10
+ )
11
+ expect(root.element_children.map(&:name)).to eq(['first', 'last'])
12
+ expect(root.first_element_child.name).to eq('first')
13
+ expect(root.last_element_child.name).to eq('last')
14
+ end
15
+
16
+ it 'skips document comments and processing instructions for element access' do
17
+ doc = RXerces.XML('<!--before--><?test value?><root/><!--after-->')
18
+
19
+ expect(doc.children.map(&:name)).to eq(['#comment', 'test', 'root', '#comment'])
20
+ expect(doc.element_children.map(&:name)).to eq(['root'])
21
+ expect(doc.first_element_child.name).to eq('root')
22
+ expect(doc.last_element_child.name).to eq('root')
23
+ end
24
+
25
+ it 'returns nil for element endpoints when there are no element children' do
26
+ ['<root/>', '<root>text<!--comment--><![CDATA[data]]></root>'].each do |xml|
27
+ root = RXerces.XML(xml).root
28
+ expect(root.first_element_child).to be_nil
29
+ expect(root.last_element_child).to be_nil
30
+ expect(root.element_children).to be_empty
31
+ end
32
+ end
33
+
34
+ it 'reflects mutations in child traversal' do
35
+ root = RXerces.XML('<root><first/><last/></root>').root
36
+ root.first_element_child.remove
37
+ expect(root.first_element_child.name).to eq('last')
38
+ root.last_element_child.remove
39
+ expect(root.children).to be_empty
40
+ expect(root.first_element_child).to be_nil
41
+ expect(root.last_element_child).to be_nil
42
+ end
43
+
44
+ it 'returns the first XPath match in document order and respects node context' do
45
+ doc = RXerces.XML('<root><group><item id="1"/><item id="2"/></group><group><item id="3"/></group></root>')
46
+
47
+ expect(doc.at_xpath('//item')['id']).to eq('1')
48
+ expect(doc.root.last_element_child.at_xpath('.//item')['id']).to eq('3')
49
+ expect(doc.at_css('item')['id']).to eq('1')
50
+ expect(doc.root.at_css('item')['id']).to eq('1')
51
+ expect(doc.at_xpath('//missing')).to be_nil
52
+ expect(doc.root.at_xpath('.//missing')).to be_nil
53
+ expect(doc.at_css('missing')).to be_nil
54
+ expect(doc.root.at_css('missing')).to be_nil
55
+ end
56
+
57
+ it 'validates first-match XPath expressions' do
58
+ doc = RXerces.XML('<root/>')
59
+ [doc, doc.root].each do |context|
60
+ expect { context.at_xpath('') }.to raise_error(ArgumentError)
61
+ expect { context.at_xpath('document("file.xml")') }.to raise_error(ArgumentError)
62
+ expect { context.at_xpath('[') }.to raise_error(ArgumentError)
63
+ end
64
+ end
65
+
66
+ it 'keeps the document alive through first-match results' do
67
+ node = RXerces.XML('<root><item id="1"/></root>').at_xpath('//item')
68
+ GC.start
69
+ expect(node['id']).to eq('1')
70
+ expect(node.document.root.name).to eq('root')
71
+ end
72
+
73
+ it 'serializes UTF-8 with a matching declaration and round-trips Unicode' do
74
+ text = 'café 日本語 😀 & < >'
75
+ doc = RXerces.XML('<root>café 日本語 😀 &amp; &lt; &gt;</root>')
76
+ serialized = doc.to_xml
77
+
78
+ expect(serialized.encoding).to eq(Encoding::UTF_8)
79
+ expect(serialized).to be_valid_encoding
80
+ expect(serialized).to include('encoding="UTF-8"')
81
+ expect(RXerces.XML(serialized).root.text.b).to eq(text.b)
82
+ end
83
+
84
+ it 'serializes non-UTF-8 input as correctly declared UTF-8' do
85
+ doc = RXerces.XML('<?xml version="1.0" encoding="ISO-8859-1"?><root>caf&#233;</root>')
86
+ serialized = doc.to_xml
87
+
88
+ expect(serialized).to include('encoding="UTF-8"', 'café')
89
+ expect(RXerces.XML(serialized).root.text.b).to eq('café'.b)
90
+ end
91
+ end
@@ -27,6 +27,76 @@ RSpec.describe RXerces::XML::Document do
27
27
  doc = RXerces::XML::Document.parse(complex_xml)
28
28
  expect(doc).to be_a(RXerces::XML::Document)
29
29
  end
30
+
31
+ it "handles empty documents" do
32
+ expect {
33
+ RXerces::XML::Document.parse('')
34
+ }.to raise_error(RuntimeError, /invalid document structure/)
35
+ end
36
+
37
+ it "handles documents with only XML declaration" do
38
+ expect {
39
+ RXerces::XML::Document.parse('<?xml version="1.0"?>')
40
+ }.to raise_error(RuntimeError, /invalid document structure/)
41
+ end
42
+
43
+ it "handles deeply nested elements" do
44
+ # Create XML with 50 levels of nesting
45
+ xml = '<root>'
46
+ 50.times { |i| xml += "<level#{i}>" }
47
+ xml += 'deep content'
48
+ 50.times { |i| xml += "</level#{49-i}>" }
49
+ xml += '</root>'
50
+
51
+ doc = RXerces::XML::Document.parse(xml)
52
+ expect(doc).to be_a(RXerces::XML::Document)
53
+ expect(doc.root.name).to eq('root')
54
+
55
+ # Verify we can access deeply nested content
56
+ deepest = doc.root
57
+ 50.times { |i| deepest = deepest.element_children.first }
58
+ expect(deepest.text).to eq('deep content')
59
+ end
60
+
61
+ it "handles documents with CDATA sections" do
62
+ xml_with_cdata = '<root><![CDATA[<not>&parsed;]]></root>'
63
+ doc = RXerces::XML::Document.parse(xml_with_cdata)
64
+ expect(doc.root.text).to eq('<not>&parsed;')
65
+ end
66
+
67
+ it "handles documents with processing instructions" do
68
+ xml_with_pi = '<?xml-stylesheet type="text/xsl" href="style.xsl"?><root>content</root>'
69
+ doc = RXerces::XML::Document.parse(xml_with_pi)
70
+ expect(doc.root.text).to eq('content')
71
+ end
72
+
73
+ it "handles documents with comments" do
74
+ xml_with_comment = '<root><!-- This is a comment -->content</root>'
75
+ doc = RXerces::XML::Document.parse(xml_with_comment)
76
+ expect(doc.root.text).to eq('content')
77
+ end
78
+
79
+ it "handles mixed content" do
80
+ mixed_xml = '<root>text <b>bold</b> more text <i>italic</i> end</root>'
81
+ doc = RXerces::XML::Document.parse(mixed_xml)
82
+ expect(doc.root.children.length).to eq(5) # 3 text nodes + 2 elements
83
+ expect(doc.root.text).to eq('text bold more text italic end')
84
+ end
85
+
86
+ it "handles elements with many attributes" do
87
+ xml_many_attrs = '<root ' + (1..100).map { |i| "attr#{i}=\"value#{i}\"" }.join(' ') + '>content</root>'
88
+ doc = RXerces::XML::Document.parse(xml_many_attrs)
89
+ expect(doc.root.attributes.length).to eq(100)
90
+ expect(doc.root['attr50']).to eq('value50')
91
+ end
92
+
93
+ it "handles large text content" do
94
+ large_text = 'x' * 10000
95
+ xml_large = "<root>#{large_text}</root>"
96
+ doc = RXerces::XML::Document.parse(xml_large)
97
+ expect(doc.root.text.length).to eq(10000)
98
+ expect(doc.root.text).to start_with('xxxxx')
99
+ end
30
100
  end
31
101
 
32
102
  describe "#root" do
@@ -64,23 +134,7 @@ RSpec.describe RXerces::XML::Document do
64
134
  end
65
135
  end
66
136
 
67
- describe "#css" do
68
- # Check if Xalan support is compiled in (CSS requires XPath which needs Xalan)
69
- xalan_available = begin
70
- test_xml = '<root><item id="1">A</item><item id="2">B</item></root>'
71
- test_doc = RXerces::XML::Document.parse(test_xml)
72
- result = test_doc.xpath('//item[@id="1"]')
73
- result.length == 1
74
- rescue
75
- false
76
- end
77
-
78
- before(:all) do
79
- unless xalan_available
80
- skip "Xalan-C not available - CSS selectors require Xalan-C library"
81
- end
82
- end
83
-
137
+ describe "#css", xalan: true do
84
138
  let(:xml) do
85
139
  <<-XML
86
140
  <library>
@@ -234,4 +288,117 @@ RSpec.describe RXerces::XML::Document do
234
288
  end
235
289
  end
236
290
  end
291
+
292
+ describe "parse options validation" do
293
+ let(:simple_xml) { '<root><child>test</child></root>' }
294
+
295
+ context "with valid options" do
296
+ it "accepts no options" do
297
+ expect {
298
+ RXerces::XML::Document.parse(simple_xml)
299
+ }.not_to raise_error
300
+ end
301
+
302
+ it "accepts nil options" do
303
+ expect {
304
+ RXerces::XML::Document.parse(simple_xml, nil)
305
+ }.not_to raise_error
306
+ end
307
+
308
+ it "accepts empty hash" do
309
+ expect {
310
+ RXerces::XML::Document.parse(simple_xml, {})
311
+ }.not_to raise_error
312
+ end
313
+
314
+ it "accepts allow_external_entities with symbol key" do
315
+ expect {
316
+ doc = RXerces::XML::Document.parse(simple_xml, allow_external_entities: false)
317
+ expect(doc).to be_a(RXerces::XML::Document)
318
+ }.not_to raise_error
319
+ end
320
+
321
+ it "accepts allow_external_entities with string key" do
322
+ expect {
323
+ doc = RXerces::XML::Document.parse(simple_xml, 'allow_external_entities' => false)
324
+ expect(doc).to be_a(RXerces::XML::Document)
325
+ }.not_to raise_error
326
+ end
327
+
328
+ it "accepts allow_external_entities set to true" do
329
+ expect {
330
+ doc = RXerces::XML::Document.parse(simple_xml, allow_external_entities: true)
331
+ expect(doc).to be_a(RXerces::XML::Document)
332
+ }.not_to raise_error
333
+ end
334
+ end
335
+
336
+ context "with invalid options" do
337
+ it "rejects unknown option keys" do
338
+ expect {
339
+ RXerces::XML::Document.parse(simple_xml, unknown_option: true)
340
+ }.to raise_error(ArgumentError, /Unknown option: unknown_option/)
341
+ end
342
+
343
+ it "rejects multiple unknown options and reports the first one" do
344
+ expect {
345
+ RXerces::XML::Document.parse(simple_xml, foo: 1, bar: 2)
346
+ }.to raise_error(ArgumentError, /Unknown option:/)
347
+ end
348
+
349
+ it "lists allowed options in error message" do
350
+ expect {
351
+ RXerces::XML::Document.parse(simple_xml, invalid: true)
352
+ }.to raise_error(ArgumentError, /Allowed options are: allow_external_entities/)
353
+ end
354
+
355
+ it "rejects options with both valid and invalid keys" do
356
+ expect {
357
+ RXerces::XML::Document.parse(simple_xml,
358
+ allow_external_entities: true,
359
+ invalid_key: false)
360
+ }.to raise_error(ArgumentError, /Unknown option: invalid_key/)
361
+ end
362
+
363
+ it "rejects non-string, non-symbol keys" do
364
+ expect {
365
+ RXerces::XML::Document.parse(simple_xml, 123 => true)
366
+ }.to raise_error(ArgumentError, /Option keys must be symbols or strings/)
367
+ end
368
+
369
+ it "rejects options that are not a hash" do
370
+ expect {
371
+ RXerces::XML::Document.parse(simple_xml, "not a hash")
372
+ }.to raise_error(TypeError)
373
+ end
374
+
375
+ it "rejects options array" do
376
+ expect {
377
+ RXerces::XML::Document.parse(simple_xml, [:allow_external_entities])
378
+ }.to raise_error(TypeError)
379
+ end
380
+ end
381
+
382
+ context "security" do
383
+ it "prevents typos from silently disabling security features" do
384
+ # If someone mistypes allow_external_entities, it should fail
385
+ # rather than silently ignoring the option
386
+ expect {
387
+ RXerces::XML::Document.parse(simple_xml, allow_external_entity: true)
388
+ }.to raise_error(ArgumentError, /Unknown option/)
389
+ end
390
+
391
+ it "prevents typos with underscores" do
392
+ expect {
393
+ RXerces::XML::Document.parse(simple_xml, allowexternalentities: true)
394
+ }.to raise_error(ArgumentError, /Unknown option/)
395
+ end
396
+
397
+ it "prevents similar-looking options" do
398
+ expect {
399
+ RXerces::XML::Document.parse(simple_xml, external_entities: true)
400
+ }.to raise_error(ArgumentError, /Unknown option/)
401
+ end
402
+ end
403
+ end
237
404
  end