moxml 0.1.26 → 0.3.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 (86) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/windows.yml +62 -0
  3. data/Gemfile +3 -1
  4. data/README.adoc +69 -6
  5. data/lib/compat/opal/moxml_boot.rb +6 -1
  6. data/lib/moxml/adapter/base.rb +53 -56
  7. data/lib/moxml/adapter/customized_leptris/declaration.rb +32 -0
  8. data/lib/moxml/adapter/customized_leptris/doctype.rb +31 -0
  9. data/lib/moxml/adapter/customized_leptris/entity_reference.rb +12 -0
  10. data/lib/moxml/adapter/customized_leptris/text_segment.rb +26 -0
  11. data/lib/moxml/adapter/customized_leptris.rb +16 -0
  12. data/lib/moxml/adapter/customized_libxml/cdata.rb +3 -12
  13. data/lib/moxml/adapter/customized_libxml/comment.rb +2 -9
  14. data/lib/moxml/adapter/customized_libxml/declaration.rb +1 -10
  15. data/lib/moxml/adapter/customized_libxml/node.rb +8 -0
  16. data/lib/moxml/adapter/customized_libxml/processing_instruction.rb +2 -10
  17. data/lib/moxml/adapter/customized_libxml/text.rb +2 -9
  18. data/lib/moxml/adapter/customized_oga/entity_decoder.rb +37 -0
  19. data/lib/moxml/adapter/customized_oga/raw_value_override.rb +52 -0
  20. data/lib/moxml/adapter/customized_oga/xml_generator.rb +4 -34
  21. data/lib/moxml/adapter/customized_oga.rb +2 -0
  22. data/lib/moxml/adapter/customized_ox/entity_reference.rb +1 -17
  23. data/lib/moxml/adapter/customized_rexml/entity_reference.rb +1 -11
  24. data/lib/moxml/adapter/headed_ox.rb +9 -128
  25. data/lib/moxml/adapter/leptris.rb +965 -0
  26. data/lib/moxml/adapter/libxml.rb +137 -117
  27. data/lib/moxml/adapter/nokogiri.rb +29 -8
  28. data/lib/moxml/adapter/oga.rb +139 -55
  29. data/lib/moxml/adapter/ox.rb +93 -137
  30. data/lib/moxml/adapter/rexml.rb +64 -51
  31. data/lib/moxml/adapter.rb +2 -1
  32. data/lib/moxml/attribute.rb +8 -4
  33. data/lib/moxml/attribute_resolver.rb +189 -0
  34. data/lib/moxml/builder.rb +0 -18
  35. data/lib/moxml/config.rb +29 -5
  36. data/lib/moxml/context.rb +33 -0
  37. data/lib/moxml/document.rb +1 -1
  38. data/lib/moxml/element.rb +29 -12
  39. data/lib/moxml/entity/reference.rb +28 -0
  40. data/lib/moxml/entity/restorer.rb +95 -0
  41. data/lib/moxml/entity.rb +126 -0
  42. data/lib/moxml/entity_registry.rb +1 -1
  43. data/lib/moxml/error.rb +0 -42
  44. data/lib/moxml/node.rb +135 -10
  45. data/lib/moxml/node_set.rb +1 -1
  46. data/lib/moxml/sax/namespace_splitter.rb +5 -2
  47. data/lib/moxml/signature/model/key/dsa_key_value.rb +1 -2
  48. data/lib/moxml/version.rb +1 -1
  49. data/lib/moxml/xml_emitter.rb +71 -0
  50. data/lib/moxml/xpath/compiler.rb +143 -8
  51. data/lib/moxml/xpath/parser.rb +22 -15
  52. data/lib/moxml.rb +3 -3
  53. data/spec/examples/readme_examples_spec.rb +0 -4
  54. data/spec/examples/xpath_examples_spec.rb +0 -11
  55. data/spec/integration/all_adapters_spec.rb +18 -1
  56. data/spec/integration/sax_parity_spec.rb +58 -0
  57. data/spec/integration/shared_examples/edge_cases.rb +0 -10
  58. data/spec/integration/shared_examples/high_level/{document_builder_behavior.rb → parse_behavior.rb} +1 -2
  59. data/spec/integration/shared_examples/integration_workflows.rb +0 -10
  60. data/spec/integration/shared_examples/node_wrappers/attribute_behavior.rb +98 -0
  61. data/spec/integration/shared_examples/node_wrappers/namespace_behavior.rb +26 -0
  62. data/spec/integration/shared_examples/node_wrappers/node_behavior.rb +15 -8
  63. data/spec/moxml/adapter/leptris_spec.rb +75 -0
  64. data/spec/moxml/adapter/libxml_internals_spec.rb +1 -1
  65. data/spec/moxml/adapter/ox_spec.rb +20 -6
  66. data/spec/moxml/adapter/platform_spec.rb +15 -2
  67. data/spec/moxml/adapter/shared_examples/adapter_contract.rb +180 -0
  68. data/spec/moxml/attribute_resolver_spec.rb +108 -0
  69. data/spec/moxml/config_spec.rb +0 -1
  70. data/spec/moxml/context_spec.rb +22 -0
  71. data/spec/moxml/doctype_spec.rb +1 -1
  72. data/spec/moxml/entity_spec.rb +76 -0
  73. data/spec/moxml/error_spec.rb +0 -50
  74. data/spec/moxml/node_spec.rb +104 -0
  75. data/spec/moxml/sax_entity_parity_spec.rb +358 -0
  76. data/spec/moxml/xml_emitter_spec.rb +64 -0
  77. data/spec/moxml/xpath/axes_spec.rb +112 -0
  78. data/spec/moxml/xpath/parser_spec.rb +6 -0
  79. data/spec/moxml/xpath_capabilities_spec.rb +5 -3
  80. data/spec/performance/benchmark_spec.rb +13 -6
  81. data/spec/performance/thread_safety_spec.rb +0 -4
  82. data/spec/spec_helper.rb +5 -1
  83. metadata +23 -6
  84. data/lib/moxml/adapter/libxml/entity_restorer.rb +0 -94
  85. data/lib/moxml/document_builder.rb +0 -167
  86. data/spec/moxml/document_builder_spec.rb +0 -30
@@ -92,4 +92,108 @@ RSpec.describe Moxml::Node do
92
92
  expect(child.content).to eq("text")
93
93
  end
94
94
  end
95
+
96
+ describe "#ancestors" do
97
+ let(:doc) { context.parse("<library><section><book><title>Book 1</title></book></section></library>") }
98
+ let(:title) { doc.at_xpath("//title") }
99
+
100
+ it "returns ancestors from parent up to and including the document" do
101
+ ancestors = title.ancestors
102
+ expect(ancestors).to be_a(Moxml::NodeSet)
103
+ expect(ancestors.select(&:element?).map(&:name)).to eq(["book", "section", "library"])
104
+ expect(ancestors[3]).to be_a(Moxml::Document)
105
+ end
106
+
107
+ it "returns empty for the document" do
108
+ expect(doc.ancestors).to be_empty
109
+ end
110
+
111
+ it "returns the document for the root element" do
112
+ ancestors = doc.root.ancestors
113
+ expect(ancestors.size).to eq(1)
114
+ expect(ancestors.first).to be_a(Moxml::Document)
115
+ end
116
+ end
117
+
118
+ describe "#descendants" do
119
+ let(:doc) { context.parse("<section><book><title>Book 1</title></book></section>") }
120
+ let(:section) { doc.at_xpath("//section") }
121
+
122
+ it "returns all descendants excluding self" do
123
+ descendants = section.descendants
124
+ expect(descendants).to be_a(Moxml::NodeSet)
125
+ element_names = descendants.select(&:element?).map(&:name)
126
+ expect(element_names).to eq(["book", "title"])
127
+ end
128
+
129
+ it "includes text nodes" do
130
+ expect(section.descendants.any?(&:text?)).to be true
131
+ end
132
+
133
+ it "returns no elements for a leaf element" do
134
+ expect(doc.at_xpath("//title").descendants.select(&:element?)).to be_empty
135
+ end
136
+ end
137
+
138
+ describe "#path" do
139
+ %i[nokogiri oga rexml ox libxml].each do |adapter_name|
140
+ context "with #{adapter_name} adapter" do
141
+ let(:ctx) { Moxml.new(adapter_name) }
142
+ let(:doc) do
143
+ ctx.parse("<library><section><book id='1'/><book id='2'><title>Book 2</title></book></section></library>")
144
+ end
145
+ let(:first_book) { doc.root.children.find(&:element?).children.find { |c| c.element? && c.name == "book" && c["id"] == "1" } }
146
+ let(:second_book) { doc.root.children.find(&:element?).children.find { |c| c.element? && c.name == "book" && c["id"] == "2" } }
147
+ let(:title) { second_book.children.find(&:element?) }
148
+
149
+ it "builds the path without a predicate for uniquely named elements" do
150
+ expect(title.path).to eq("/library/section/book[2]/title")
151
+ end
152
+
153
+ it "adds a positional predicate for same-named siblings" do
154
+ expect(first_book.path).to eq("/library/section/book[1]")
155
+ expect(second_book.path).to eq("/library/section/book[2]")
156
+ end
157
+
158
+ it "locates the node via its path" do
159
+ # Ox translates only a limited XPath subset (no positional predicates)
160
+ skip "ox xpath translation does not support positional predicates" if adapter_name == :ox
161
+
162
+ expect(doc.at_xpath(title.path)).to eq(title)
163
+ end
164
+
165
+ it "returns / for the document" do
166
+ expect(doc.path).to eq("/")
167
+ end
168
+ end
169
+ end
170
+
171
+ it "raises for node types without a path representation" do
172
+ text = doc.root.children.first.children.first
173
+ expect { text.path }.to raise_error(Moxml::NotImplementedError, /element and document/)
174
+ end
175
+ end
176
+
177
+ describe "#line_number" do
178
+ %i[nokogiri oga rexml ox libxml].each do |adapter_name|
179
+ tracks_source_lines = adapter_name.eql?(:nokogiri) || adapter_name.eql?(:libxml)
180
+ context "with #{adapter_name} adapter" do
181
+ let(:ctx) { Moxml.new(adapter_name) }
182
+ let(:doc) do
183
+ ctx.parse("<?xml version='1.0'?>\n<library>\n <book>\n <title>Book 1</title>\n </book>\n</library>\n")
184
+ end
185
+ let(:title) { doc.root.children.find(&:element?).children.find(&:element?) }
186
+
187
+ if tracks_source_lines
188
+ it "returns the 1-based source line" do
189
+ expect(title.line_number).to eq(4)
190
+ end
191
+ else
192
+ it "returns nil when the adapter does not track lines" do
193
+ expect(title.line_number).to be_nil
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
95
199
  end
@@ -0,0 +1,358 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # Pins cross-adapter parity for entity-decoding in attribute values and
6
+ # text content, across SAX, DOM, and build-then-serialize-then-reparse.
7
+ # Previously the four adapters diverged: nokogiri/libxml SAX failed to
8
+ # resolve "&amp;#NN;"; rexml SAX delivered raw escaped strings; oga DOM
9
+ # and SAX double-decoded.
10
+
11
+ # Namespace-isolated container for the test data and the SAX capture
12
+ # handler. Wrapping these in a module rather than declaring them as
13
+ # top-level constants inside `RSpec.describe` keeps them out of the
14
+ # global namespace and lets the spec reference them without tripping
15
+ # the RSpec/* cops that forbid local-variable use inside examples.
16
+ module SaxEntityParityFixtures
17
+ class CaptureHandler < Moxml::SAX::ElementHandler
18
+ attr_reader :first_attrs, :text
19
+
20
+ def initialize
21
+ super
22
+ @first_attrs = nil
23
+ @text = +""
24
+ end
25
+
26
+ def on_start_element(_name, attrs = {}, _namespaces = {})
27
+ @first_attrs = attrs.dup if @first_attrs.nil?
28
+ end
29
+
30
+ def on_characters(chunk)
31
+ @text << chunk
32
+ end
33
+ end
34
+
35
+ ADAPTERS = %i[nokogiri ox oga rexml libxml headed_ox].freeze
36
+
37
+ # Each row: [input XML, decoded attribute value, expected attribute
38
+ # value as it appears in the serialized XML]. The serialized form
39
+ # asserts what every adapter must emit byte-for-byte for the attribute.
40
+ ATTRIBUTE_CASES = {
41
+ "wrapped amp + decimal ref" => ['<doc x="&amp;#38;"/>', "&#38;", "&amp;#38;"],
42
+ "wrapped amp + hex ref" => ['<doc x="&amp;#x26;"/>', "&#x26;", "&amp;#x26;"],
43
+ "wrapped amp + non-std ref" => ['<doc x="&amp;copy;"/>', "&copy;", "&amp;copy;"],
44
+ "wrapped amp + lt" => ['<doc x="&amp;lt;"/>', "&lt;", "&amp;lt;"],
45
+ "wrapped amp + amp" => ['<doc x="&amp;amp;"/>', "&amp;", "&amp;amp;"],
46
+ "two amps" => ['<doc x="&amp;&amp;"/>', "&&", "&amp;&amp;"],
47
+ "plain amp" => ['<doc x="&amp;"/>', "&", "&amp;"],
48
+ "plain decimal ref" => ['<doc x="&#38;"/>', "&", "&amp;"],
49
+ "plain hex ref" => ['<doc x="&#x26;"/>', "&", "&amp;"],
50
+ "high codepoint" => ['<doc x="&#169;"/>', "©", "©"],
51
+ "no entities" => ['<doc x="plain"/>', "plain", "plain"],
52
+ }.freeze
53
+
54
+ # Each row: [input XML, decoded text, expected text as it appears in
55
+ # serialized XML].
56
+ TEXT_CASES = {
57
+ "text wrapped amp + decimal" => ["<doc>&amp;#38;</doc>", "&#38;", "&amp;#38;"],
58
+ "text wrapped amp + non-std" => ["<doc>&amp;copy;</doc>", "&copy;", "&amp;copy;"],
59
+ "text plain amp" => ["<doc>&amp;</doc>", "&", "&amp;"],
60
+ }.freeze
61
+
62
+ # XML 1.0 §2.6 — entity references inside PI content are NOT resolved.
63
+ # Every adapter must surface the literal source text. The libxml
64
+ # adapter previously decoded these five entities via a gsub chain in
65
+ # processing_instruction_content; pin each one explicitly.
66
+ PI_ENTITY_CASES = {
67
+ "&amp;" => "data &amp; more",
68
+ "&lt;" => "data &lt; more",
69
+ "&gt;" => "data &gt; more",
70
+ "&quot;" => "data &quot; more",
71
+ "&apos;" => "data &apos; more",
72
+ }.freeze
73
+
74
+ # Per-adapter overrides for the rebuild-path serialized form. Oga's
75
+ # set_attribute calls preprocess_entities which marks non-standard
76
+ # named entities so they survive serialization as "&name;" rather than
77
+ # being escaped to "&amp;name;". This is the entity-preservation
78
+ # feature documented in spec/moxml/adapter/entity_restoration_spec.rb
79
+ # and spec/moxml/adapter/oga_spec.rb — not a bug.
80
+ REBUILD_SERIALIZED_OVERRIDES = {
81
+ oga: {
82
+ "wrapped amp + non-std ref" => "&copy;",
83
+ },
84
+ }.freeze
85
+ end
86
+
87
+ RSpec.describe "SAX/DOM entity parity" do
88
+ SaxEntityParityFixtures::ADAPTERS.each do |adapter|
89
+ context "with #{adapter} adapter" do
90
+ let(:ctx) { Moxml.new(adapter) }
91
+
92
+ SaxEntityParityFixtures::ATTRIBUTE_CASES.each do |label, (xml, expected, expected_serialized)|
93
+ it "decodes attribute via SAX: #{label}" do
94
+ handler = SaxEntityParityFixtures::CaptureHandler.new
95
+ ctx.sax_parse(xml, handler)
96
+ expect(handler.first_attrs["x"]).to eq(expected)
97
+ end
98
+
99
+ it "decodes attribute via DOM: #{label}" do
100
+ expect(ctx.parse(xml).root["x"]).to eq(expected)
101
+ end
102
+
103
+ it "serializes attribute to expected XML form: #{label}" do
104
+ # End-to-end: parse → DOM access → set on fresh doc → serialize
105
+ # → assert exact serialized form. This pins what the consumer
106
+ # sees on the wire, not just what the parser delivers in memory.
107
+ parsed_value = ctx.parse(xml).root["x"]
108
+
109
+ doc2 = ctx.create_document
110
+ el = doc2.create_element("doc")
111
+ el["x"] = parsed_value
112
+ doc2.add_child(el)
113
+
114
+ serialized = doc2.to_xml.sub(/\A<\?[^>]*\?>\s*/, "")
115
+ expected_form = SaxEntityParityFixtures::REBUILD_SERIALIZED_OVERRIDES.dig(adapter, label) || expected_serialized
116
+ expect(serialized).to include(%(x="#{expected_form}"))
117
+ end
118
+
119
+ it "round-trips attribute through build+serialize+reparse: #{label}" do
120
+ parsed_value = ctx.parse(xml).root["x"]
121
+
122
+ doc2 = ctx.create_document
123
+ el = doc2.create_element("doc")
124
+ el["x"] = parsed_value
125
+ doc2.add_child(el)
126
+
127
+ serialized = doc2.to_xml.sub(/\A<\?[^>]*\?>/, "")
128
+ expect(ctx.parse(serialized).root["x"]).to eq(parsed_value)
129
+ end
130
+
131
+ it "re-serializes parsed document idempotently: #{label}" do
132
+ # Parse → serialize → re-parse → re-serialize → assert the two
133
+ # serializations match. This catches any non-idempotent
134
+ # mutations the adapter applies during the round-trip.
135
+ first = ctx.parse(xml).to_xml.sub(/\A<\?[^>]*\?>\s*/, "")
136
+ second = ctx.parse(first).to_xml.sub(/\A<\?[^>]*\?>\s*/, "")
137
+ expect(second).to eq(first)
138
+ end
139
+ end
140
+
141
+ SaxEntityParityFixtures::TEXT_CASES.each do |label, (xml, expected, expected_serialized)|
142
+ it "decodes text via SAX: #{label}" do
143
+ handler = SaxEntityParityFixtures::CaptureHandler.new
144
+ ctx.sax_parse(xml, handler)
145
+ expect(handler.text).to eq(expected)
146
+ end
147
+
148
+ it "decodes text via DOM: #{label}" do
149
+ expect(ctx.parse(xml).root.text).to eq(expected)
150
+ end
151
+
152
+ it "serializes text to expected XML form: #{label}" do
153
+ serialized = ctx.parse(xml).to_xml.sub(/\A<\?[^>]*\?>\s*/, "").strip
154
+ expect(serialized).to include(">#{expected_serialized}<")
155
+ end
156
+ end
157
+
158
+ it "does not double-escape '&' in a programmatically-built text node" do
159
+ # Pins the libxml customized Text#to_xml fix: an authored "&"
160
+ # must serialize to "&amp;" exactly once, never "&amp;amp;".
161
+ # Regression target for adapter/customized_libxml/text.rb where
162
+ # the previous implementation used #content (decoded text) and
163
+ # lost the "&" → "&amp;" escape, and earlier variants risked
164
+ # double-escaping on round-trip.
165
+ doc = ctx.create_document
166
+ el = doc.create_element("doc")
167
+ el.add_child("a & b")
168
+ doc.add_child(el)
169
+
170
+ serialized = doc.to_xml.sub(/\A<\?[^>]*\?>\s*/, "").strip
171
+ expect(serialized).to include(">a &amp; b<")
172
+ expect(serialized).not_to include("&amp;amp;")
173
+
174
+ # And the round-trip must preserve the original text exactly.
175
+ reparsed_text = ctx.parse(serialized).root.text
176
+ expect(reparsed_text).to eq("a & b")
177
+ end
178
+
179
+ it "mutates in-tree text node content without double-escaping '&'" do
180
+ # Pins the libxml fix for set_text_content on attached text
181
+ # nodes: libxml-ruby's #content= setter pre-escapes the input,
182
+ # and #to_s escapes again on serialization. The adapter must
183
+ # replace the in-tree node with a fresh raw-storage node to
184
+ # avoid silent double-escape data corruption.
185
+ doc = ctx.create_document
186
+ el = doc.create_element("doc")
187
+ text = doc.create_text("placeholder")
188
+ el.add_child(text)
189
+ doc.add_child(el)
190
+
191
+ text.content = "a & b"
192
+ expect(text.content).to eq("a & b")
193
+
194
+ serialized = doc.to_xml.sub(/\A<\?[^>]*\?>\s*/, "").strip
195
+ expect(serialized).to include(">a &amp; b<")
196
+ expect(serialized).not_to include("&amp;amp;")
197
+ expect(ctx.parse(serialized).root.text).to eq("a & b")
198
+ end
199
+
200
+ it "mutates in-tree PI content verbatim without escaping" do
201
+ # Pins the libxml fix for set_processing_instruction_content
202
+ # on attached PI nodes: libxml-ruby's #content= setter pre-
203
+ # escapes quotes and ampersands, but XML 1.0 §2.6 specifies
204
+ # PI content is verbatim — entity references are not resolved
205
+ # and no escaping is required on serialization.
206
+ doc = ctx.create_document
207
+ el = doc.create_element("doc")
208
+ pi = doc.create_processing_instruction("target", "placeholder")
209
+ el.add_child(pi)
210
+ doc.add_child(el)
211
+
212
+ pi.content = 'a " & b'
213
+ expect(pi.content.strip).to eq('a " & b')
214
+
215
+ serialized = doc.to_xml.sub(/\A<\?[^>]*\?>\s*/, "").strip
216
+ expect(serialized).to include('a " & b?>')
217
+ expect(serialized).not_to include("&quot;")
218
+ expect(serialized).not_to include("&amp;")
219
+ reparsed = ctx.parse(serialized).root.children.find do |c|
220
+ c.is_a?(Moxml::ProcessingInstruction)
221
+ end
222
+ expect(reparsed.content.strip).to eq('a " & b')
223
+ end
224
+
225
+ it "mutates unparented text/PI content before adoption into the tree" do
226
+ # Pins the swap_native_in_place early-return when parent is nil:
227
+ # set_text_content / set_processing_instruction_content called
228
+ # before the node is added to any element must still replace the
229
+ # wrapper's @native with a fresh raw-storage node, so the value
230
+ # survives correctly once the node is later attached and serialized.
231
+ doc = ctx.create_document
232
+ el = doc.create_element("doc")
233
+
234
+ text = doc.create_text("placeholder")
235
+ text.content = "a & b"
236
+ pi = doc.create_processing_instruction("target", "placeholder")
237
+ pi.content = 'a " & b'
238
+
239
+ el.add_child(text)
240
+ el.add_child(pi)
241
+ doc.add_child(el)
242
+
243
+ serialized = doc.to_xml.sub(/\A<\?[^>]*\?>\s*/, "").strip
244
+ expect(serialized).to include(">a &amp; b<")
245
+ expect(serialized).to include('<?target a " & b?>')
246
+ expect(serialized).not_to include("&amp;amp;")
247
+ end
248
+
249
+ # Regressions around &amp; appearing inside contexts that Oga's
250
+ # source-level preprocessor must not touch (CDATA, comments,
251
+ # DOCTYPE) — verified across adapters to ensure parity holds.
252
+ it "preserves literal &amp; inside CDATA" do
253
+ doc = ctx.parse("<doc><![CDATA[a &amp; b]]></doc>")
254
+ expect(doc.root.text).to eq("a &amp; b")
255
+ end
256
+
257
+ it "preserves literal &amp; inside comment" do
258
+ doc = ctx.parse("<doc><!-- a &amp; b --></doc>")
259
+ comment = doc.root.children.find { |c| c.is_a?(Moxml::Comment) }
260
+ expect(comment.content.strip).to eq("a &amp; b")
261
+ end
262
+
263
+ it "preserves literal U+FFFC U+FFFC user data in attribute" do
264
+ xml = "<doc x=\"\u{FFFC}\u{FFFC} test\"/>"
265
+ expect(ctx.parse(xml).root["x"]).to eq("\u{FFFC}\u{FFFC} test")
266
+ end
267
+
268
+ it "preserves literal U+FDD0 U+FDD0 user data in attribute" do
269
+ # Single noncharacters near the Oga marker codepoints — must not
270
+ # collide with the internal DEFAULT_AMP_MARKER sentinel.
271
+ xml = "<doc x=\"\u{FDD0}\u{FDD0} test\"/>"
272
+ expect(ctx.parse(xml).root["x"]).to eq("\u{FDD0}\u{FDD0} test")
273
+ end
274
+
275
+ it "preserves the exact Oga DEFAULT_AMP_MARKER sequence in attribute (no amp)" do
276
+ # User data matching the default Oga marker codepoints with NO
277
+ # &amp; in the input — restore must not run, so the sequence
278
+ # survives intact.
279
+ xml = "<doc x=\"\u{FDD0}\u{FDD1}\u{FDD2} plain\"/>"
280
+ expect(ctx.parse(xml).root["x"]).to eq("\u{FDD0}\u{FDD1}\u{FDD2} plain")
281
+ end
282
+
283
+ it "preserves the exact Oga DEFAULT_AMP_MARKER sequence in attribute (with amp)" do
284
+ # User data matching the default Oga marker codepoints alongside
285
+ # an &amp; that the preprocessor would otherwise substitute.
286
+ # The adapter must pick a marker not present in user data so
287
+ # restore doesn't corrupt the original sequence.
288
+ xml = "<doc x=\"\u{FDD0}\u{FDD1}\u{FDD2} &amp; foo\"/>"
289
+ expect(ctx.parse(xml).root["x"]).to eq("\u{FDD0}\u{FDD1}\u{FDD2} & foo")
290
+ end
291
+
292
+ it "preserves the exact Oga DEFAULT_AMP_MARKER sequence in text" do
293
+ xml = "<doc>\u{FDD0}\u{FDD1}\u{FDD2} &amp; foo</doc>"
294
+ expect(ctx.parse(xml).root.text).to eq("\u{FDD0}\u{FDD1}\u{FDD2} & foo")
295
+ end
296
+
297
+ it "handles DOCTYPE with '[' in quoted system ID" do
298
+ # System ID containing a literal "[" before the internal subset
299
+ # opener — quote-aware DOCTYPE terminator must not pick "]>"
300
+ # based on the bracket inside the quoted string.
301
+ xml = %(<!DOCTYPE root SYSTEM "has[bracket"><root x="&amp;#38;"/>)
302
+ expect(ctx.parse(xml).root["x"]).to eq("&#38;")
303
+ end
304
+
305
+ it "handles bare DOCTYPE with '>' in quoted system ID" do
306
+ # Bare DOCTYPE (no internal subset) — the terminator is ">".
307
+ # A literal ">" inside the quoted SYSTEM id must not be treated
308
+ # as the end of the declaration. For the Oga adapter this also
309
+ # exercises find_block_terminator's quote-aware ">" scan; if
310
+ # the bare-DOCTYPE end was misidentified, the preprocessor
311
+ # would rewrite "&amp;" inside the remaining quoted ID region
312
+ # to DEFAULT_AMP_MARKER and leak it into the serialized doctype.
313
+ xml = %(<!DOCTYPE root SYSTEM "a>b&amp;c"><root x="&amp;#38;"/>)
314
+ expect(ctx.parse(xml).root["x"]).to eq("&#38;")
315
+ end
316
+
317
+ it "handles DOCTYPE with ']>' inside a quoted entity value" do
318
+ # Quote-aware DOCTYPE terminator must skip past a literal "]>"
319
+ # appearing inside a quoted ExternalID / ENTITY value so the
320
+ # internal subset terminator is matched correctly. For the Oga
321
+ # adapter this also exercises the source-level DOCTYPE skipper;
322
+ # native-DOCTYPE parsers must reach the same result.
323
+ xml = '<!DOCTYPE root [ <!ENTITY foo "]>"> ]><root x="&amp;#38;"/>'
324
+ expect(ctx.parse(xml).root["x"]).to eq("&#38;")
325
+ end
326
+
327
+ SaxEntityParityFixtures::PI_ENTITY_CASES.each do |entity_label, payload|
328
+ it "preserves literal #{entity_label} inside processing instruction" do
329
+ doc = ctx.parse("<doc><?target #{payload}?></doc>")
330
+ pi = doc.root.children.find { |c| c.is_a?(Moxml::ProcessingInstruction) }
331
+ expect(pi.content.strip).to eq(payload)
332
+ end
333
+ end
334
+ end
335
+ end
336
+
337
+ # Cross-adapter equivalence: feed the same XML into every adapter and
338
+ # assert all of them emit the same serialized attribute form. Catches
339
+ # any regression where one adapter starts to deviate from the rest.
340
+ describe "cross-adapter serialization equivalence" do
341
+ def serialize_via(adapter, xml)
342
+ ctx = Moxml.new(adapter)
343
+ ctx.parse(xml).to_xml.sub(/\A<\?[^>]*\?>\s*/, "").strip
344
+ end
345
+
346
+ SaxEntityParityFixtures::ATTRIBUTE_CASES.each do |label, (xml, _expected, expected_serialized)|
347
+ it "every adapter emits the same attribute form: #{label}" do
348
+ results = SaxEntityParityFixtures::ADAPTERS.to_h { |a| [a, serialize_via(a, xml)] }
349
+ results.each do |a, out|
350
+ expect(out).to(
351
+ include(%(x="#{expected_serialized}")),
352
+ "#{a} produced #{out.inspect}",
353
+ )
354
+ end
355
+ end
356
+ end
357
+ end
358
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe Moxml::XmlEmitter do
6
+ describe ".escape_text" do
7
+ it "escapes only & < > in a single pass" do
8
+ expect(described_class.escape_text("a & < > ' \""))
9
+ .to eq("a &amp; &lt; &gt; ' \"")
10
+ end
11
+
12
+ it "returns clean strings unallocated" do
13
+ input = "clean"
14
+ expect(described_class.escape_text(input)).to equal(input)
15
+ end
16
+
17
+ it "coerces non-strings" do
18
+ expect(described_class.escape_text(1)).to eq("1")
19
+ end
20
+ end
21
+
22
+ describe ".escape_attribute" do
23
+ it "escapes & < > and double quotes, keeping apostrophes literal" do
24
+ expect(described_class.escape_attribute(%q{& < > ' "}))
25
+ .to eq("&amp; &lt; &gt; ' &quot;")
26
+ end
27
+ end
28
+
29
+ describe ".cdata" do
30
+ it "wraps content in a CDATA section" do
31
+ expect(described_class.cdata("data")).to eq("<![CDATA[data]]>")
32
+ end
33
+
34
+ it "splits an embedded end sequence into adjacent sections" do
35
+ expect(described_class.cdata("a]]>b"))
36
+ .to eq("<![CDATA[a]]]]><![CDATA[>b]]>")
37
+ end
38
+ end
39
+
40
+ describe ".declaration_xml" do
41
+ it "renders only the set attributes" do
42
+ expect(described_class.declaration_xml("1.0", nil, nil))
43
+ .to eq('<?xml version="1.0"?>')
44
+ expect(described_class.declaration_xml("1.0", "UTF-8", "yes"))
45
+ .to eq('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')
46
+ end
47
+
48
+ it "omits empty encodings" do
49
+ expect(described_class.declaration_xml("1.0", "", nil))
50
+ .to eq('<?xml version="1.0"?>')
51
+ end
52
+ end
53
+
54
+ describe ".doctype_xml" do
55
+ it "renders PUBLIC and SYSTEM forms and bare names" do
56
+ expect(described_class.doctype_xml("r", "pub", "sys"))
57
+ .to eq('<!DOCTYPE r PUBLIC "pub" "sys">')
58
+ expect(described_class.doctype_xml("r", nil, "sys"))
59
+ .to eq('<!DOCTYPE r SYSTEM "sys">')
60
+ expect(described_class.doctype_xml("r", nil, nil))
61
+ .to eq("<!DOCTYPE r>")
62
+ end
63
+ end
64
+ end
@@ -163,6 +163,118 @@ RSpec.describe "XPath Axes" do
163
163
  end
164
164
  end
165
165
 
166
+ describe "attribute axis with namespaces" do
167
+ let(:ns_doc) do
168
+ xml = <<~XML
169
+ <root xmlns:p="http://x.org" xmlns:q="http://x.org">
170
+ <item q:type="namespaced" type="bare" other="o"/>
171
+ </root>
172
+ XML
173
+ context.parse(xml)
174
+ end
175
+
176
+ def select(expression, namespaces = nil)
177
+ ast = Moxml::XPath::Parser.parse(expression)
178
+ proc = Moxml::XPath::Compiler.compile_with_cache(ast,
179
+ namespaces: namespaces)
180
+ proc.call(ns_doc).map(&:value)
181
+ end
182
+
183
+ it "matches bare names against no-namespace attributes only" do
184
+ expect(select("/root/item/@type")).to eq(["bare"])
185
+ end
186
+
187
+ it "matches prefixed names by namespace URI, not by prefix spelling" do
188
+ expect(select("/root/item/@p:type")).to eq(["namespaced"])
189
+ expect(select("/root/item/@q:type")).to eq(["namespaced"])
190
+ end
191
+
192
+ it "matches any local name within a prefix's namespace" do
193
+ expect(select("/root/item/@q:*")).to eq(["namespaced"])
194
+ end
195
+
196
+ it "matches nothing for an undeclared prefix" do
197
+ expect(select("/root/item/@z:type")).to be_empty
198
+ end
199
+
200
+ it "prefers context prefix mappings over document declarations" do
201
+ expect(select("/root/item/@p:type", "p" => "http://other.org"))
202
+ .to be_empty
203
+ expect(select("/root/item/@zzz:type", "zzz" => "http://x.org"))
204
+ .to eq(["namespaced"])
205
+ end
206
+ end
207
+
208
+ describe "ancestor and sibling axes" do
209
+ let(:family) do
210
+ context.parse("<root><parent id='p1'><b id='1'/><b id='2'/><c/><d/></parent></root>")
211
+ end
212
+
213
+ def select(expression, node = family)
214
+ ast = Moxml::XPath::Parser.parse(expression)
215
+ proc = Moxml::XPath::Compiler.compile_with_cache(ast)
216
+ proc.call(node).map(&:name)
217
+ end
218
+
219
+ it "selects ancestors with ancestor::" do
220
+ expect(select("//c/ancestor::*")).to eq(%w[parent root])
221
+ end
222
+
223
+ it "includes the context node with ancestor-or-self::" do
224
+ expect(select("//c/ancestor-or-self::*")).to eq(%w[c parent root])
225
+ end
226
+
227
+ it "selects following siblings" do
228
+ expect(select("//c/following-sibling::*")).to eq(%w[d])
229
+ expect(select("//b[@id='1']/following-sibling::b")).to eq(%w[b])
230
+ expect(select("//b[@id='1']/following-sibling::*").size).to eq(3)
231
+ end
232
+
233
+ it "selects preceding siblings" do
234
+ expect(select("//c/preceding-sibling::*")).to eq(%w[b b])
235
+ end
236
+
237
+ it "resolves the bare parent step and steps after it" do
238
+ expect(select("//c/..")).to eq(%w[parent])
239
+ expect(select("//c/../*").size).to eq(4)
240
+ end
241
+
242
+ it "raises a clear error for unimplemented axes" do
243
+ expect { select("//c/following::*") }
244
+ .to raise_error(Moxml::XPath::Error, /not supported/)
245
+ end
246
+ end
247
+
248
+ describe "descendant-or-self shorthand from an element" do
249
+ it "selects descendants with .//step" do
250
+ ast = Moxml::XPath::Parser.parse(".//grandchild")
251
+ proc = Moxml::XPath::Compiler.compile_with_cache(ast)
252
+ result = proc.call(doc.root.children.find(&:element?))
253
+
254
+ expect(result.map { |n| n.attribute("id").value })
255
+ .to contain_exactly("g1", "g2")
256
+ end
257
+ end
258
+
259
+ describe "node type tests as steps" do
260
+ it "selects text nodes with text()" do
261
+ ast = Moxml::XPath::Parser.parse("//grandchild/text()")
262
+ proc = Moxml::XPath::Compiler.compile_with_cache(ast)
263
+ result = proc.call(doc)
264
+
265
+ expect(result.map(&:content)).to contain_exactly("text1", "text2")
266
+ end
267
+
268
+ it "selects comments with comment()" do
269
+ commented = context.parse("<root><!-- c1 --><a/><!-- c2 --></root>")
270
+ ast = Moxml::XPath::Parser.parse("//comment()")
271
+ proc = Moxml::XPath::Compiler.compile_with_cache(ast)
272
+ result = proc.call(commented)
273
+
274
+ expect(result.map(&:content)).to contain_exactly(" c1 ", " c2 ")
275
+ end
276
+ end
277
+
166
278
  describe "descendant axis" do
167
279
  it "finds descendants without self" do
168
280
  ast = Moxml::XPath::Parser.parse("/root/descendant::child")
@@ -60,6 +60,12 @@ RSpec.describe Moxml::XPath::Parser do
60
60
  expect(ast.type).to eq(:relative_path)
61
61
  end
62
62
 
63
+ it "parses prefixed attribute names" do
64
+ ast = described_class.parse("@xmi:type")
65
+ expect(ast.type).to eq(:relative_path)
66
+ expect { described_class.parse("@xmi:*") }.not_to raise_error
67
+ end
68
+
63
69
  it "parses path with parent reference" do
64
70
  ast = described_class.parse("../book")
65
71
  expect(ast.type).to eq(:relative_path)
@@ -131,11 +131,13 @@ RSpec.describe "XPath Capabilities" do
131
131
  end
132
132
 
133
133
  describe "XPath functions" do
134
- it "supports count() function" do
134
+ it "supports count() function, returning the scalar unwrapped" do
135
135
  doc = Moxml.new.parse(simple_xml)
136
- # NOTE: count() returns a number, not nodes
137
136
  result = doc.xpath("count(//book)")
138
- expect(result).to be_a(Numeric) if result.is_a?(Numeric)
137
+ # REXML's XPath has no function support and yields an empty set
138
+ skip "count() function not supported on #{adapter_name}" if result.is_a?(Moxml::NodeSet)
139
+
140
+ expect(result).to eq(2)
139
141
  rescue Moxml::XPathError, NoMethodError
140
142
  skip "count() function not supported on #{adapter_name}"
141
143
  end