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.
data/spec/node_spec.rb CHANGED
@@ -65,6 +65,50 @@ RSpec.describe RXerces::XML::Node do
65
65
  it "returns empty string for nodes without text" do
66
66
  expect(root.text).to be_a(String)
67
67
  end
68
+
69
+ context "edge cases" do
70
+ let(:edge_xml) do
71
+ <<-XML
72
+ <root>
73
+ <empty></empty>
74
+ <mixed>Hello <b>world</b>!</mixed>
75
+ <whitespace> </whitespace>
76
+ <cdata><![CDATA[<not>parsed</not>]]></cdata>
77
+ <multiline>
78
+ Line 1
79
+ Line 2
80
+ </multiline>
81
+ </root>
82
+ XML
83
+ end
84
+ let(:edge_doc) { RXerces::XML::Document.parse(edge_xml) }
85
+ let(:edge_root) { edge_doc.root }
86
+
87
+ it "returns empty string for empty elements" do
88
+ empty = edge_root.children.find { |n| n.name == 'empty' }
89
+ expect(empty.text).to eq('')
90
+ end
91
+
92
+ it "concatenates text from mixed content elements" do
93
+ mixed = edge_root.children.find { |n| n.name == 'mixed' }
94
+ expect(mixed.text).to eq('Hello world!')
95
+ end
96
+
97
+ it "preserves whitespace-only content" do
98
+ whitespace = edge_root.children.find { |n| n.name == 'whitespace' }
99
+ expect(whitespace.text).to eq(' ')
100
+ end
101
+
102
+ it "returns CDATA content as plain text" do
103
+ cdata = edge_root.children.find { |n| n.name == 'cdata' }
104
+ expect(cdata.text).to eq('<not>parsed</not>')
105
+ end
106
+
107
+ it "preserves multiline text content" do
108
+ multiline = edge_root.children.find { |n| n.name == 'multiline' }
109
+ expect(multiline.text).to include("Line 1\n Line 2")
110
+ end
111
+ end
68
112
  end
69
113
 
70
114
  describe "#content" do
@@ -142,6 +186,33 @@ RSpec.describe RXerces::XML::Node do
142
186
  text_node = root.children.find { |n| n.is_a?(RXerces::XML::Text) }
143
187
  expect(text_node.has_attribute?('anything')).to be false if text_node
144
188
  end
189
+
190
+ context "edge cases" do
191
+ let(:attr_xml) do
192
+ <<-XML
193
+ <root>
194
+ <element empty="" xmlns:prefix="http://example.com" special_chars="a&amp;b&lt;c&gt;">
195
+ content
196
+ </element>
197
+ </root>
198
+ XML
199
+ end
200
+ let(:attr_doc) { RXerces::XML::Document.parse(attr_xml) }
201
+ let(:element) { attr_doc.root.children.find { |n| n.is_a?(RXerces::XML::Element) } }
202
+
203
+ it "handles empty string attribute values" do
204
+ expect(element['empty']).to be_nil
205
+ expect(element.has_attribute?('empty')).to be false
206
+ end
207
+
208
+ it "handles attributes with special characters" do
209
+ expect(element['special_chars']).to eq('a&b<c>')
210
+ end
211
+
212
+ it "handles namespace prefix attributes" do
213
+ expect(element['xmlns:prefix']).to eq('http://example.com')
214
+ end
215
+ end
145
216
  end
146
217
 
147
218
  describe "#children" do
@@ -233,23 +304,7 @@ RSpec.describe RXerces::XML::Node do
233
304
  expect(ancestors.any? { |a| a.name == '#document' }).to be false
234
305
  end
235
306
 
236
- context "with selector" do
237
- # Check if Xalan support is compiled in (selectors require XPath which needs Xalan)
238
- xalan_available = begin
239
- test_xml = '<root><item id="1">A</item></root>'
240
- test_doc = RXerces::XML::Document.parse(test_xml)
241
- result = test_doc.xpath('//item[@id="1"]')
242
- result.length == 1
243
- rescue
244
- false
245
- end
246
-
247
- before(:all) do
248
- unless xalan_available
249
- skip "Xalan-C not available - ancestor selectors require Xalan-C library"
250
- end
251
- end
252
-
307
+ context "with selector", xalan: true do
253
308
  it "filters ancestors by tag name selector" do
254
309
  person = root.children.find { |n| n.is_a?(RXerces::XML::Element) }
255
310
  city = person.children.find { |n| n.name == 'city' }
@@ -312,6 +367,45 @@ RSpec.describe RXerces::XML::Node do
312
367
  end
313
368
  end
314
369
 
370
+ describe "#attribute_nodes" do
371
+ it "returns an array of attribute nodes" do
372
+ person = root.children.find { |n| n.is_a?(RXerces::XML::Element) }
373
+ attr_nodes = person.attribute_nodes
374
+ expect(attr_nodes).to be_an(Array)
375
+ expect(attr_nodes.length).to eq(2)
376
+ expect(attr_nodes.all? { |n| n.is_a?(RXerces::XML::Node) }).to be true
377
+ end
378
+
379
+ it "returns nodes with name and text/value" do
380
+ person = root.children.find { |n| n.is_a?(RXerces::XML::Element) }
381
+ attr_nodes = person.attribute_nodes
382
+
383
+ id_node = attr_nodes.find { |n| n.name == 'id' }
384
+ expect(id_node).not_to be_nil
385
+ expect(id_node.text).to eq('1')
386
+
387
+ name_node = attr_nodes.find { |n| n.name == 'name' }
388
+ expect(name_node).not_to be_nil
389
+ expect(name_node.text).to eq('Alice')
390
+ end
391
+
392
+ it "returns empty array for elements without attributes" do
393
+ person = root.children.find { |n| n.is_a?(RXerces::XML::Element) }
394
+ age = person.children.find { |n| n.name == 'age' }
395
+ attr_nodes = age.attribute_nodes
396
+ expect(attr_nodes).to be_an(Array)
397
+ expect(attr_nodes).to be_empty
398
+ end
399
+
400
+ it "returns empty array for text nodes" do
401
+ person = root.children.find { |n| n.is_a?(RXerces::XML::Element) }
402
+ text_node = person.children.find { |n| n.is_a?(RXerces::XML::Text) }
403
+ attr_nodes = text_node.attribute_nodes
404
+ expect(attr_nodes).to be_an(Array)
405
+ expect(attr_nodes).to be_empty
406
+ end
407
+ end
408
+
315
409
  describe "#next_sibling" do
316
410
  it "returns the next sibling node" do
317
411
  people = root.children.select { |n| n.is_a?(RXerces::XML::Element) }
@@ -554,25 +648,104 @@ RSpec.describe RXerces::XML::Node do
554
648
  end
555
649
 
556
650
  context "with nodes from different documents" do
557
- it "raises error when adding node from different document" do
651
+ it "automatically imports node from different document" do
558
652
  doc1 = RXerces::XML::Document.parse('<root><item>one</item></root>')
559
653
  doc2 = RXerces::XML::Document.parse('<other><item>two</item></other>')
560
654
 
561
655
  root1 = doc1.root
562
656
  item2 = doc2.root.children.find { |n| n.is_a?(RXerces::XML::Element) }
563
657
 
658
+ # Should not raise an error - should import automatically
564
659
  expect {
565
660
  root1.add_child(item2)
566
- }.to raise_error(RuntimeError, /belongs to a different document/)
661
+ }.not_to raise_error
662
+
663
+ # Verify the node was added
664
+ items = doc1.xpath('//item')
665
+ expect(items.length).to eq(2)
567
666
  end
568
667
 
569
- it "provides helpful error message mentioning importNode" do
668
+ it "imports node with all its children (deep copy)" do
570
669
  doc1 = RXerces::XML::Document.parse('<root></root>')
571
- doc2 = RXerces::XML::Document.parse('<other><child/></other>')
670
+ doc2 = RXerces::XML::Document.parse('<other><parent><child>text</child></parent></other>')
572
671
 
573
- expect {
574
- doc1.root.add_child(doc2.root.children.first)
575
- }.to raise_error(RuntimeError, /importNode/)
672
+ parent_node = doc2.xpath('//parent').first
673
+
674
+ # Add parent node from doc2 to doc1
675
+ doc1.root.add_child(parent_node)
676
+
677
+ # Verify the entire subtree was imported
678
+ expect(doc1.xpath('//parent').length).to eq(1)
679
+ expect(doc1.xpath('//parent/child').length).to eq(1)
680
+ expect(doc1.xpath('//parent/child').first.text).to eq('text')
681
+ end
682
+
683
+ it "preserves node content when importing" do
684
+ doc1 = RXerces::XML::Document.parse('<root></root>')
685
+ doc2 = RXerces::XML::Document.parse('<other><item attr="value">content</item></other>')
686
+
687
+ item = doc2.xpath('//item').first
688
+ doc1.root.add_child(item)
689
+
690
+ imported_item = doc1.xpath('//item').first
691
+ expect(imported_item.text).to eq('content')
692
+ expect(imported_item['attr']).to eq('value')
693
+ end
694
+
695
+ it "does not modify the original document when importing" do
696
+ doc1 = RXerces::XML::Document.parse('<root></root>')
697
+ doc2 = RXerces::XML::Document.parse('<other><item>original</item></other>')
698
+
699
+ item = doc2.xpath('//item').first
700
+ doc1.root.add_child(item)
701
+
702
+ # Original document should still have the item
703
+ # Note: importNode creates a copy, but the Ruby wrapper gets updated
704
+ # to point to the imported node, so we need to re-query doc2
705
+ expect(doc2.xpath('//item').length).to eq(1)
706
+ end
707
+
708
+ it "handles importing complex nested structures" do
709
+ doc1 = RXerces::XML::Document.parse('<root></root>')
710
+ doc2 = RXerces::XML::Document.parse(<<-XML)
711
+ <other>
712
+ <section id="s1">
713
+ <title>Section 1</title>
714
+ <paragraph>Content 1</paragraph>
715
+ <subsection>
716
+ <title>Subsection</title>
717
+ <paragraph>Nested content</paragraph>
718
+ </subsection>
719
+ </section>
720
+ </other>
721
+ XML
722
+
723
+ section = doc2.xpath('//section').first
724
+ doc1.root.add_child(section)
725
+
726
+ # Verify entire structure was imported
727
+ expect(doc1.xpath('//section').length).to eq(1)
728
+ expect(doc1.xpath('//section').first['id']).to eq('s1')
729
+ expect(doc1.xpath('//title').length).to eq(2)
730
+ expect(doc1.xpath('//paragraph').length).to eq(2)
731
+ expect(doc1.xpath('//subsection').length).to eq(1)
732
+ end
733
+
734
+ it "allows importing multiple nodes from different documents" do
735
+ doc1 = RXerces::XML::Document.parse('<root></root>')
736
+ doc2 = RXerces::XML::Document.parse('<other><item>A</item></other>')
737
+ doc3 = RXerces::XML::Document.parse('<another><item>B</item></another>')
738
+
739
+ item2 = doc2.xpath('//item').first
740
+ item3 = doc3.xpath('//item').first
741
+
742
+ doc1.root.add_child(item2)
743
+ doc1.root.add_child(item3)
744
+
745
+ items = doc1.xpath('//item')
746
+ expect(items.length).to eq(2)
747
+ expect(items[0].text).to eq('A')
748
+ expect(items[1].text).to eq('B')
576
749
  end
577
750
  end
578
751
 
@@ -674,6 +847,37 @@ RSpec.describe RXerces::XML::Node do
674
847
  result = root.xpath('.//age')
675
848
  expect(result).to be_a(RXerces::XML::NodeSet)
676
849
  end
850
+
851
+ context "edge cases" do
852
+ it "returns empty NodeSet for nonexistent elements" do
853
+ result = root.xpath('.//nonexistent')
854
+ expect(result).to be_a(RXerces::XML::NodeSet)
855
+ expect(result.length).to eq(0)
856
+ end
857
+
858
+ it "raises error for invalid XPath syntax" do
859
+ expect {
860
+ root.xpath('.//[invalid')
861
+ }.to raise_error(ArgumentError, /XPath expression has unbalanced/)
862
+ end
863
+
864
+ it "handles very deep XPath expressions" do
865
+ # Create a deep nesting scenario
866
+ deep_xml = '<root>' + ('<level>' * 50) + '<deep>content</deep>' + ('</level>' * 50) + '</root>'
867
+ deep_doc = RXerces::XML::Document.parse(deep_xml)
868
+ result = deep_doc.xpath('//deep')
869
+ expect(result.length).to eq(1)
870
+ expect(result.first.text).to eq('content')
871
+ end
872
+
873
+ it "handles XPath with special characters in element names" do
874
+ special_xml = '<root><element-name_with.specials>content</element-name_with.specials></root>'
875
+ special_doc = RXerces::XML::Document.parse(special_xml)
876
+ result = special_doc.xpath('//element-name_with.specials')
877
+ expect(result.length).to eq(1)
878
+ expect(result.first.text).to eq('content')
879
+ end
880
+ end
677
881
  end
678
882
 
679
883
  describe "#inner_html" do
@@ -823,23 +1027,7 @@ RSpec.describe RXerces::XML::Node do
823
1027
  end
824
1028
  end
825
1029
 
826
- describe "#at_css" do
827
- # Check if Xalan support is compiled in (CSS requires XPath which needs Xalan)
828
- xalan_available = begin
829
- test_xml = '<root><item id="1">A</item><item id="2">B</item></root>'
830
- test_doc = RXerces::XML::Document.parse(test_xml)
831
- result = test_doc.xpath('//item[@id="1"]')
832
- result.length == 1
833
- rescue
834
- false
835
- end
836
-
837
- before(:all) do
838
- unless xalan_available
839
- skip "Xalan-C not available - CSS selectors require Xalan-C library"
840
- end
841
- end
842
-
1030
+ describe "#at_css", xalan: true do
843
1031
  it "is an alias for at (which uses CSS converted to XPath)" do
844
1032
  xml = '<root><item class="foo">First</item><item class="bar">Second</item></root>'
845
1033
  doc = RXerces::XML::Document.parse(xml)
@@ -863,23 +1051,7 @@ RSpec.describe RXerces::XML::Node do
863
1051
  end
864
1052
  end
865
1053
 
866
- describe "#css" do
867
- # Check if Xalan support is compiled in (CSS requires XPath which needs Xalan)
868
- xalan_available = begin
869
- test_xml = '<root><item id="1">A</item><item id="2">B</item></root>'
870
- test_doc = RXerces::XML::Document.parse(test_xml)
871
- result = test_doc.xpath('//item[@id="1"]')
872
- result.length == 1
873
- rescue
874
- false
875
- end
876
-
877
- before(:all) do
878
- unless xalan_available
879
- skip "Xalan-C not available - CSS selectors require Xalan-C library"
880
- end
881
- end
882
-
1054
+ describe "#css", xalan: true do
883
1055
  let(:xml) do
884
1056
  <<-XML
885
1057
  <library>
data/spec/nodeset_spec.rb CHANGED
@@ -145,6 +145,96 @@ RSpec.describe RXerces::XML::NodeSet do
145
145
  end
146
146
  end
147
147
 
148
+ describe "#inspect" do
149
+ it "returns a string representation" do
150
+ result = nodeset.inspect
151
+ expect(result).to be_a(String)
152
+ expect(result).to include('RXerces::XML::NodeSet')
153
+ end
154
+
155
+ it "shows element names in the output" do
156
+ result = nodeset.inspect
157
+ expect(result).to include('<item>')
158
+ end
159
+
160
+ it "truncates long content" do
161
+ long_xml = '<root><item>' + ('x' * 100) + '</item></root>'
162
+ doc = RXerces::XML::Document.parse(long_xml)
163
+ result = doc.xpath('//item').inspect
164
+ expect(result).to include('...')
165
+ expect(result.length).to be < long_xml.length
166
+ end
167
+
168
+ context "with UTF-8 content" do
169
+ it "handles multi-byte characters without corruption" do
170
+ # Test with various multi-byte UTF-8 characters
171
+ utf8_xml = '<root><item>Hello 世界 🌍 Привет</item></root>'
172
+ doc = RXerces::XML::Document.parse(utf8_xml)
173
+ result = doc.xpath('//item').inspect
174
+
175
+ # Should not raise encoding errors
176
+ expect { result.encode('UTF-8') }.not_to raise_error
177
+ expect(result.encoding).to eq(Encoding::UTF_8)
178
+ end
179
+
180
+ it "truncates UTF-8 strings safely without cutting mid-character" do
181
+ # Create a string with multi-byte characters that would be cut off
182
+ # Use Japanese characters (3 bytes each in UTF-8) near the truncation boundary
183
+ long_text = 'a' * 25 + '世界你好こんにちは' + 'x' * 50
184
+ utf8_xml = "<root><item>#{long_text}</item></root>"
185
+ doc = RXerces::XML::Document.parse(utf8_xml)
186
+ result = doc.xpath('//item').inspect
187
+
188
+ # Result should be valid UTF-8
189
+ expect(result).to be_valid_encoding
190
+ expect { result.encode('UTF-8') }.not_to raise_error
191
+ end
192
+
193
+ it "handles emojis and 4-byte UTF-8 characters" do
194
+ # Emojis are 4-byte UTF-8 characters
195
+ emoji_xml = '<root><item>Test 🎉🎊🎈🎁🎀🎂 more text here that is quite long</item></root>'
196
+ doc = RXerces::XML::Document.parse(emoji_xml)
197
+ result = doc.xpath('//item').inspect
198
+
199
+ expect(result).to be_valid_encoding
200
+ expect { result.encode('UTF-8') }.not_to raise_error
201
+ end
202
+
203
+ it "handles mixed ASCII and multi-byte characters" do
204
+ mixed_xml = '<root><item>ASCII テキスト text 文字 more</item></root>'
205
+ doc = RXerces::XML::Document.parse(mixed_xml)
206
+ result = doc.xpath('//item').inspect
207
+
208
+ expect(result).to be_valid_encoding
209
+ end
210
+
211
+ it "handles UTF-8 in text nodes" do
212
+ text_xml = '<root><item>こんにちは世界' + ('x' * 50) + '</item></root>'
213
+ doc = RXerces::XML::Document.parse(text_xml)
214
+ # Get the item element which contains the text
215
+ result = doc.xpath('//item').inspect
216
+
217
+ expect(result).to be_valid_encoding
218
+ end
219
+
220
+ it "handles Cyrillic characters" do
221
+ cyrillic_xml = '<root><item>Привет мир это длинный текст на русском языке</item></root>'
222
+ doc = RXerces::XML::Document.parse(cyrillic_xml)
223
+ result = doc.xpath('//item').inspect
224
+
225
+ expect(result).to be_valid_encoding
226
+ end
227
+
228
+ it "handles Arabic characters" do
229
+ arabic_xml = '<root><item>مرحبا بالعالم هذا نص طويل بالعربية</item></root>'
230
+ doc = RXerces::XML::Document.parse(arabic_xml)
231
+ result = doc.xpath('//item').inspect
232
+
233
+ expect(result).to be_valid_encoding
234
+ end
235
+ end
236
+ end
237
+
148
238
  it "includes Enumerable" do
149
239
  expect(RXerces::XML::NodeSet.ancestors).to include(Enumerable)
150
240
  end
@@ -4,7 +4,7 @@ require 'rxerces'
4
4
 
5
5
  RSpec.shared_examples RXerces do
6
6
  example 'version number is set to the expected value' do
7
- expect(RXerces::VERSION).to eq('0.6.1')
7
+ expect(RXerces::VERSION).to eq('0.7.1')
8
8
  expect(RXerces::VERSION).to be_frozen
9
9
  end
10
10
  end
data/spec/rxerces_spec.rb CHANGED
@@ -20,4 +20,62 @@ RSpec.describe RXerces do
20
20
  expect(doc).to be_a(RXerces::XML::Document)
21
21
  end
22
22
  end
23
+
24
+ describe "thread safety" do
25
+ it "handles concurrent initialization safely" do
26
+ xml = '<root><child>text</child></root>'
27
+ threads = []
28
+ results = []
29
+
30
+ # Create multiple threads that parse XML concurrently
31
+ 10.times do
32
+ threads << Thread.new do
33
+ doc = RXerces.XML(xml)
34
+ results << doc.class
35
+ end
36
+ end
37
+
38
+ # Wait for all threads to complete
39
+ threads.each(&:join)
40
+
41
+ # All should succeed and return Document objects
42
+ expect(results.size).to eq(10)
43
+ results.each do |result|
44
+ expect(result).to eq(RXerces::XML::Document)
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "security" do
50
+ it "prevents XXE attacks by not processing external entities" do
51
+ # XML with external entity reference
52
+ malicious_xml = <<~XML
53
+ <?xml version="1.0" encoding="UTF-8"?>
54
+ <!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
55
+ <foo>&xxe;</foo>
56
+ XML
57
+
58
+ # Should fail to parse because external entities are disabled
59
+ expect {
60
+ RXerces.XML(malicious_xml)
61
+ }.to raise_error(RuntimeError, /unable to open external entity/)
62
+ end
63
+
64
+ it "allows external entities when explicitly enabled" do
65
+ # XML with external entity reference
66
+ xml_with_entity = <<~XML
67
+ <?xml version="1.0" encoding="UTF-8"?>
68
+ <!DOCTYPE foo [ <!ENTITY test "test content"> ]>
69
+ <foo>&test;</foo>
70
+ XML
71
+
72
+ # Should succeed with internal entities even when external are disabled
73
+ doc = RXerces.XML(xml_with_entity)
74
+ expect(doc.root.text).to eq("test content")
75
+
76
+ # With allow_external_entities: true, should still handle internal entities
77
+ doc2 = RXerces.XML(xml_with_entity, allow_external_entities: true)
78
+ expect(doc2.root.text).to eq("test content")
79
+ end
80
+ end
23
81
  end
data/spec/schema_spec.rb CHANGED
@@ -44,7 +44,7 @@ RSpec.describe RXerces::XML::Schema do
44
44
  end
45
45
 
46
46
  # Note: Xerces-C parser is very tolerant of invalid XML
47
- # So we just skip testing for invalid XML for now
47
+ # Schema creation succeeds even with malformed XML, validation catches issues
48
48
  end
49
49
 
50
50
  describe '.from_document' do
@@ -72,5 +72,32 @@ RSpec.describe RXerces::XML::Schema do
72
72
  expect(errors).not_to be_empty
73
73
  expect(errors.first).to include('not-a-number')
74
74
  end
75
+
76
+ it 'handles schema grammar loading errors gracefully' do
77
+ # Create a schema with an invalid type reference
78
+ invalid_schema_xsd = <<~XSD
79
+ <?xml version="1.0"?>
80
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
81
+ <xs:element name="root">
82
+ <xs:complexType>
83
+ <xs:sequence>
84
+ <xs:element name="invalid" type="invalid-type"/>
85
+ </xs:sequence>
86
+ </xs:complexType>
87
+ </xs:element>
88
+ </xs:schema>
89
+ XSD
90
+
91
+ invalid_schema = described_class.from_string(invalid_schema_xsd)
92
+ doc = RXerces::XML::Document.parse(valid_xml)
93
+
94
+ # Validation should handle XMLException/SAXException in loadGrammar gracefully
95
+ # and continue with validation, producing errors
96
+ errors = doc.validate(invalid_schema)
97
+ expect(errors).to be_a(Array)
98
+ expect(errors).not_to be_empty
99
+ # Should contain errors about the invalid type
100
+ expect(errors.join).to include('invalid-type')
101
+ end
75
102
  end
76
103
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'rspec'
2
2
  require 'rxerces'
3
3
  require 'rxerces_shared'
4
+ require 'mkmf-lite'
4
5
 
5
6
  RSpec.configure do |config|
7
+ include Mkmf::Lite
8
+
6
9
  # Enable flags like --only-failures and --next-failure
7
10
  config.example_status_persistence_file_path = ".rspec_status"
8
11
 
@@ -14,4 +17,6 @@ RSpec.configure do |config|
14
17
  end
15
18
 
16
19
  config.include_context(RXerces)
20
+
21
+ config.filter_run_excluding(:xalan) unless have_library('xalan-c')
17
22
  end