moxml 0.1.26 → 0.2.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.
- checksums.yaml +4 -4
- data/.github/workflows/windows.yml +62 -0
- data/Gemfile +3 -1
- data/README.adoc +69 -6
- data/lib/compat/opal/moxml_boot.rb +5 -0
- data/lib/moxml/adapter/base.rb +53 -49
- data/lib/moxml/adapter/customized_leptris/declaration.rb +32 -0
- data/lib/moxml/adapter/customized_leptris/doctype.rb +31 -0
- data/lib/moxml/adapter/customized_leptris/entity_reference.rb +12 -0
- data/lib/moxml/adapter/customized_leptris/text_segment.rb +26 -0
- data/lib/moxml/adapter/customized_leptris.rb +16 -0
- data/lib/moxml/adapter/customized_libxml/cdata.rb +3 -12
- data/lib/moxml/adapter/customized_libxml/comment.rb +2 -9
- data/lib/moxml/adapter/customized_libxml/declaration.rb +1 -10
- data/lib/moxml/adapter/customized_libxml/node.rb +8 -0
- data/lib/moxml/adapter/customized_libxml/processing_instruction.rb +2 -10
- data/lib/moxml/adapter/customized_libxml/text.rb +2 -9
- data/lib/moxml/adapter/customized_oga/entity_decoder.rb +37 -0
- data/lib/moxml/adapter/customized_oga/raw_value_override.rb +52 -0
- data/lib/moxml/adapter/customized_oga/xml_generator.rb +4 -34
- data/lib/moxml/adapter/customized_oga.rb +2 -0
- data/lib/moxml/adapter/customized_ox/entity_reference.rb +1 -17
- data/lib/moxml/adapter/customized_rexml/entity_reference.rb +1 -11
- data/lib/moxml/adapter/headed_ox.rb +9 -128
- data/lib/moxml/adapter/leptris.rb +965 -0
- data/lib/moxml/adapter/libxml.rb +131 -99
- data/lib/moxml/adapter/nokogiri.rb +29 -8
- data/lib/moxml/adapter/oga.rb +91 -54
- data/lib/moxml/adapter/ox.rb +93 -137
- data/lib/moxml/adapter/rexml.rb +61 -50
- data/lib/moxml/adapter.rb +2 -1
- data/lib/moxml/attribute.rb +8 -4
- data/lib/moxml/attribute_resolver.rb +189 -0
- data/lib/moxml/config.rb +29 -3
- data/lib/moxml/context.rb +12 -0
- data/lib/moxml/element.rb +29 -12
- data/lib/moxml/entity/reference.rb +28 -0
- data/lib/moxml/entity.rb +125 -0
- data/lib/moxml/node.rb +90 -1
- data/lib/moxml/sax/namespace_splitter.rb +5 -2
- data/lib/moxml/signature/model/key/dsa_key_value.rb +1 -2
- data/lib/moxml/version.rb +1 -1
- data/lib/moxml/xml_emitter.rb +71 -0
- data/lib/moxml/xpath/compiler.rb +60 -7
- data/lib/moxml/xpath/parser.rb +22 -15
- data/lib/moxml.rb +3 -0
- data/spec/examples/readme_examples_spec.rb +0 -4
- data/spec/examples/xpath_examples_spec.rb +0 -11
- data/spec/integration/all_adapters_spec.rb +17 -0
- data/spec/integration/sax_parity_spec.rb +58 -0
- data/spec/integration/shared_examples/edge_cases.rb +0 -10
- data/spec/integration/shared_examples/integration_workflows.rb +0 -10
- data/spec/integration/shared_examples/node_wrappers/attribute_behavior.rb +98 -0
- data/spec/integration/shared_examples/node_wrappers/namespace_behavior.rb +26 -0
- data/spec/integration/shared_examples/node_wrappers/node_behavior.rb +0 -8
- data/spec/moxml/adapter/leptris_spec.rb +75 -0
- data/spec/moxml/adapter/ox_spec.rb +20 -6
- data/spec/moxml/adapter/platform_spec.rb +15 -2
- data/spec/moxml/adapter/shared_examples/adapter_contract.rb +180 -0
- data/spec/moxml/attribute_resolver_spec.rb +108 -0
- data/spec/moxml/doctype_spec.rb +1 -1
- data/spec/moxml/entity_spec.rb +76 -0
- data/spec/moxml/node_spec.rb +104 -0
- data/spec/moxml/sax_entity_parity_spec.rb +358 -0
- data/spec/moxml/xml_emitter_spec.rb +64 -0
- data/spec/moxml/xpath/axes_spec.rb +72 -0
- data/spec/moxml/xpath/parser_spec.rb +6 -0
- data/spec/moxml/xpath_capabilities_spec.rb +5 -3
- data/spec/performance/benchmark_spec.rb +13 -6
- data/spec/performance/thread_safety_spec.rb +0 -4
- data/spec/spec_helper.rb +5 -1
- metadata +21 -2
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Moxml::AttributeResolver do
|
|
6
|
+
let(:context) { Moxml.new }
|
|
7
|
+
let(:doc) do
|
|
8
|
+
context.parse(<<~XML)
|
|
9
|
+
<root xmlns:p="http://x.org" xmlns:q="http://x.org">
|
|
10
|
+
<e q:type="namespaced" type="bare"/>
|
|
11
|
+
</root>
|
|
12
|
+
XML
|
|
13
|
+
end
|
|
14
|
+
let(:element) { doc.root.children.find(&:element?) }
|
|
15
|
+
|
|
16
|
+
describe ".resolve" do
|
|
17
|
+
it "matches bare names against no-namespace attributes only" do
|
|
18
|
+
expect(described_class.resolve(element, "type")&.value).to eq("bare")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "matches prefixed names by expanded name" do
|
|
22
|
+
expect(described_class.resolve(element, "p:type")&.value)
|
|
23
|
+
.to eq("namespaced")
|
|
24
|
+
expect(described_class.resolve(element, "q:type")&.value)
|
|
25
|
+
.to eq("namespaced")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "returns nil for undeclared prefixes and declarations" do
|
|
29
|
+
expect(described_class.resolve(element, "z:type")).to be_nil
|
|
30
|
+
expect(described_class.resolve(element, "xmlns:q")).to be_nil
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe ".assign" do
|
|
35
|
+
it "delegates xmlns declarations to the adapter" do
|
|
36
|
+
described_class.assign(element, "xmlns:r", "http://r.org")
|
|
37
|
+
expect(doc.to_xml).to include('xmlns:r="http://r.org"')
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "replaces the matching attribute in place" do
|
|
41
|
+
described_class.assign(element, "p:type", "rewritten")
|
|
42
|
+
expect(element["q:type"]).to eq("rewritten")
|
|
43
|
+
expect(element.attributes.size).to eq(2)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "stores undeclared prefixes raw until they come into scope" do
|
|
47
|
+
described_class.assign(element, "z:type", "value")
|
|
48
|
+
expect(element["z:type"]).to be_nil
|
|
49
|
+
|
|
50
|
+
element.add_namespace("z", "http://z.org")
|
|
51
|
+
expect(element["z:type"]).to eq("value")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
describe ".remove" do
|
|
56
|
+
it "removes by expanded name and returns the attribute" do
|
|
57
|
+
removed = described_class.remove(element, "p:type")
|
|
58
|
+
expect(removed.value).to eq("namespaced")
|
|
59
|
+
expect(element["q:type"]).to be_nil
|
|
60
|
+
expect(element["type"]).to eq("bare")
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "returns nil when nothing matches" do
|
|
64
|
+
expect(described_class.remove(element, "z:type")).to be_nil
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
describe ".attribute_test?" do
|
|
69
|
+
let(:attributes) { element.attributes }
|
|
70
|
+
|
|
71
|
+
it "applies Namespaces 1.0 §5.2 to bare names" do
|
|
72
|
+
bare = attributes.find { |a| a.value == "bare" }
|
|
73
|
+
namespaced = attributes.find { |a| a.value == "namespaced" }
|
|
74
|
+
expect(described_class.attribute_test?(element, bare, nil, "type"))
|
|
75
|
+
.to be(true)
|
|
76
|
+
expect(described_class.attribute_test?(element, namespaced, nil, "type"))
|
|
77
|
+
.to be(false)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "matches prefixed tests by URI and accepts any local for nil" do
|
|
81
|
+
namespaced = attributes.find { |a| a.value == "namespaced" }
|
|
82
|
+
expect(described_class.attribute_test?(element, namespaced, "p", "type"))
|
|
83
|
+
.to be(true)
|
|
84
|
+
expect(described_class.attribute_test?(element, namespaced, "p", nil))
|
|
85
|
+
.to be(true)
|
|
86
|
+
expect(described_class.attribute_test?(element, namespaced, "z", "type"))
|
|
87
|
+
.to be(false)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it "treats :any as any namespace including none" do
|
|
91
|
+
bare = attributes.find { |a| a.value == "bare" }
|
|
92
|
+
expect(described_class.attribute_test?(element, bare, :any, "type"))
|
|
93
|
+
.to be(true)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
describe ".attribute_test_uri?" do
|
|
98
|
+
it "matches the compile-time URI regardless of document prefixes" do
|
|
99
|
+
namespaced = element.attributes.find { |a| a.value == "namespaced" }
|
|
100
|
+
expect(described_class.attribute_test_uri?(
|
|
101
|
+
element, namespaced, "http://x.org", "type"
|
|
102
|
+
)).to be(true)
|
|
103
|
+
expect(described_class.attribute_test_uri?(
|
|
104
|
+
element, namespaced, "http://other.org", "type"
|
|
105
|
+
)).to be(false)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
data/spec/moxml/doctype_spec.rb
CHANGED
|
@@ -48,7 +48,7 @@ RSpec.describe Moxml::Doctype do
|
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
describe "parsing" do
|
|
51
|
-
%i[nokogiri oga rexml ox].each do |adapter_name|
|
|
51
|
+
%i[nokogiri oga rexml ox libxml].each do |adapter_name|
|
|
52
52
|
context "with #{adapter_name} adapter" do
|
|
53
53
|
let(:ctx) { Moxml.new(adapter_name) }
|
|
54
54
|
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Moxml::Entity do
|
|
6
|
+
describe ".preprocess_entities" do
|
|
7
|
+
it "marks non-standard entities and leaves the five standard ones" do
|
|
8
|
+
marked = described_class.preprocess_entities("a © b & c <")
|
|
9
|
+
expect(marked).to include("#{described_class::MARKER}copy;")
|
|
10
|
+
expect(marked).to include("&")
|
|
11
|
+
expect(marked).to include("<")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "returns the input untouched when it contains no ampersand" do
|
|
15
|
+
input = "plain text"
|
|
16
|
+
expect(described_class.preprocess_entities(input)).to equal(input)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "always returns UTF-8" do
|
|
20
|
+
binary = "caf\xC3\xA9 ©".b
|
|
21
|
+
result = described_class.preprocess_entities(binary)
|
|
22
|
+
expect(result.encoding).to eq(Encoding::UTF_8)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "returns an empty string for nil" do
|
|
26
|
+
expect(described_class.preprocess_entities(nil)).to eq("")
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe ".decode_entities" do
|
|
31
|
+
it "decodes numeric and standard named references in one pass" do
|
|
32
|
+
expect(described_class.decode_entities("AB&<"))
|
|
33
|
+
.to eq("AB&<")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "preserves invalid numeric codepoints verbatim" do
|
|
37
|
+
expect(described_class.decode_entities("��")).to eq("��")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "skips strings without ampersands" do
|
|
41
|
+
input = "plain"
|
|
42
|
+
expect(described_class.decode_entities(input)).to equal(input)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe ".restore_entities" do
|
|
47
|
+
it "maps markers back to named references" do
|
|
48
|
+
restored = described_class.restore_entities(
|
|
49
|
+
"a#{described_class::MARKER}copy;b",
|
|
50
|
+
)
|
|
51
|
+
expect(restored).to eq("a©b")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "restores markers a native serializer rendered as character references" do
|
|
55
|
+
expect(described_class.restore_entities("acopy;b"))
|
|
56
|
+
.to eq("a©b")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "returns marker-free strings without a regex pass" do
|
|
60
|
+
input = "no markers here"
|
|
61
|
+
expect(described_class.restore_entities(input)).to equal(input)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
describe Moxml::Entity::Reference do
|
|
66
|
+
it "round-trips as a serialized entity reference" do
|
|
67
|
+
expect(described_class.new("copy").to_xml).to eq("©")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "compares by name" do
|
|
71
|
+
reference = described_class.new("copy")
|
|
72
|
+
expect(reference).to eq(described_class.new("copy"))
|
|
73
|
+
expect(reference).not_to eq(described_class.new("nbsp"))
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
data/spec/moxml/node_spec.rb
CHANGED
|
@@ -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 "&#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="&#38;"/>', "&", "&#38;"],
|
|
42
|
+
"wrapped amp + hex ref" => ['<doc x="&#x26;"/>', "&", "&#x26;"],
|
|
43
|
+
"wrapped amp + non-std ref" => ['<doc x="&copy;"/>', "©", "&copy;"],
|
|
44
|
+
"wrapped amp + lt" => ['<doc x="&lt;"/>', "<", "&lt;"],
|
|
45
|
+
"wrapped amp + amp" => ['<doc x="&amp;"/>', "&", "&amp;"],
|
|
46
|
+
"two amps" => ['<doc x="&&"/>', "&&", "&&"],
|
|
47
|
+
"plain amp" => ['<doc x="&"/>', "&", "&"],
|
|
48
|
+
"plain decimal ref" => ['<doc x="&"/>', "&", "&"],
|
|
49
|
+
"plain hex ref" => ['<doc x="&"/>', "&", "&"],
|
|
50
|
+
"high codepoint" => ['<doc x="©"/>', "©", "©"],
|
|
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>&#38;</doc>", "&", "&#38;"],
|
|
58
|
+
"text wrapped amp + non-std" => ["<doc>&copy;</doc>", "©", "&copy;"],
|
|
59
|
+
"text plain amp" => ["<doc>&</doc>", "&", "&"],
|
|
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
|
+
"&" => "data & more",
|
|
68
|
+
"<" => "data < more",
|
|
69
|
+
">" => "data > more",
|
|
70
|
+
""" => "data " more",
|
|
71
|
+
"'" => "data ' 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 "&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" => "©",
|
|
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 "&" exactly once, never "&amp;".
|
|
161
|
+
# Regression target for adapter/customized_libxml/text.rb where
|
|
162
|
+
# the previous implementation used #content (decoded text) and
|
|
163
|
+
# lost the "&" → "&" 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 & b<")
|
|
172
|
+
expect(serialized).not_to include("&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 & b<")
|
|
196
|
+
expect(serialized).not_to include("&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(""")
|
|
218
|
+
expect(serialized).not_to include("&")
|
|
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 & b<")
|
|
245
|
+
expect(serialized).to include('<?target a " & b?>')
|
|
246
|
+
expect(serialized).not_to include("&amp;")
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Regressions around & 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 & inside CDATA" do
|
|
253
|
+
doc = ctx.parse("<doc><![CDATA[a & b]]></doc>")
|
|
254
|
+
expect(doc.root.text).to eq("a & b")
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
it "preserves literal & inside comment" do
|
|
258
|
+
doc = ctx.parse("<doc><!-- a & b --></doc>")
|
|
259
|
+
comment = doc.root.children.find { |c| c.is_a?(Moxml::Comment) }
|
|
260
|
+
expect(comment.content.strip).to eq("a & 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
|
+
# & 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 & 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} & 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} & 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="&#38;"/>)
|
|
302
|
+
expect(ctx.parse(xml).root["x"]).to eq("&")
|
|
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 "&" 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&c"><root x="&#38;"/>)
|
|
314
|
+
expect(ctx.parse(xml).root["x"]).to eq("&")
|
|
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="&#38;"/>'
|
|
324
|
+
expect(ctx.parse(xml).root["x"]).to eq("&")
|
|
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
|