moxml 0.1.21 → 0.1.22
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/opal.yml +37 -0
- data/.rspec-opal +5 -0
- data/Gemfile +6 -0
- data/Rakefile +67 -0
- data/lib/compat/opal/rexml/namespace.rb +56 -0
- data/lib/compat/opal/rexml/parsers/baseparser.rb +952 -0
- data/lib/compat/opal/rexml/source.rb +213 -0
- data/lib/compat/opal/rexml/text.rb +418 -0
- data/lib/compat/opal/rexml/xmltokens.rb +45 -0
- data/lib/compat/opal/rexml_compat.rb +76 -0
- data/lib/moxml/adapter/customized_rexml/formatter.rb +11 -10
- data/lib/moxml/adapter/headed_ox.rb +2 -6
- data/lib/moxml/adapter/libxml.rb +5 -20
- data/lib/moxml/adapter/nokogiri.rb +7 -18
- data/lib/moxml/adapter/oga.rb +4 -22
- data/lib/moxml/adapter/ox.rb +8 -23
- data/lib/moxml/adapter/rexml.rb +29 -33
- data/lib/moxml/adapter.rb +38 -8
- data/lib/moxml/config.rb +1 -1
- data/lib/moxml/entity_registry.rb +36 -31
- data/lib/moxml/entity_registry_opal_data.rb +2137 -0
- data/lib/moxml/node.rb +19 -26
- data/lib/moxml/sax/namespace_splitter.rb +54 -0
- data/lib/moxml/version.rb +1 -1
- data/lib/moxml/xml_utils.rb +9 -1
- data/spec/consistency/adapter_parity_spec.rb +1 -1
- data/spec/integration/all_adapters_spec.rb +1 -1
- data/spec/integration/w3c_namespace_spec.rb +1 -1
- data/spec/moxml/adapter/ox_spec.rb +8 -0
- data/spec/moxml/adapter/platform_spec.rb +69 -0
- data/spec/moxml/adapter/shared_examples/adapter_contract.rb +0 -6
- data/spec/moxml/entity_registry_spec.rb +10 -0
- data/spec/moxml/native_attachment/opal_spec.rb +39 -2
- data/spec/moxml/node_type_map_spec.rb +43 -0
- data/spec/moxml/opal_rexml_adapter_spec.rb +14 -0
- data/spec/moxml/opal_smoke_spec.rb +61 -0
- data/spec/moxml/sax/namespace_splitter_spec.rb +67 -0
- data/spec/moxml/text_spec.rb +1 -1
- data/spec/spec_helper.rb +32 -13
- data/spec/support/opal.rb +16 -0
- metadata +17 -1
data/lib/moxml/node.rb
CHANGED
|
@@ -135,19 +135,6 @@ module Moxml
|
|
|
135
135
|
children.last
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
-
# Returns the text content of this node
|
|
139
|
-
# For elements, returns concatenated text of all text children
|
|
140
|
-
# For text nodes, returns the content if available
|
|
141
|
-
def text
|
|
142
|
-
if respond_to?(:content)
|
|
143
|
-
content
|
|
144
|
-
elsif respond_to?(:children)
|
|
145
|
-
children.grep(Text).map(&:content).join
|
|
146
|
-
else
|
|
147
|
-
""
|
|
148
|
-
end
|
|
149
|
-
end
|
|
150
|
-
|
|
151
138
|
# Returns the text content of this node
|
|
152
139
|
# Subclasses should override this method
|
|
153
140
|
# Element and Text have their own implementations
|
|
@@ -220,22 +207,28 @@ module Moxml
|
|
|
220
207
|
nil
|
|
221
208
|
end
|
|
222
209
|
|
|
210
|
+
# Registry mapping node type symbols to wrapper classes.
|
|
211
|
+
# Built lazily to avoid load-order issues with subclasses.
|
|
212
|
+
def self.node_type_map
|
|
213
|
+
@node_type_map ||= {
|
|
214
|
+
element: Element,
|
|
215
|
+
text: Text,
|
|
216
|
+
cdata: Cdata,
|
|
217
|
+
comment: Comment,
|
|
218
|
+
processing_instruction: ProcessingInstruction,
|
|
219
|
+
document: Document,
|
|
220
|
+
declaration: Declaration,
|
|
221
|
+
doctype: Doctype,
|
|
222
|
+
attribute: Attribute,
|
|
223
|
+
entity_reference: EntityReference,
|
|
224
|
+
}.freeze
|
|
225
|
+
end
|
|
226
|
+
|
|
223
227
|
def self.wrap(node, context)
|
|
224
228
|
return nil if node.nil?
|
|
225
229
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
when :text then Text
|
|
229
|
-
when :cdata then Cdata
|
|
230
|
-
when :comment then Comment
|
|
231
|
-
when :processing_instruction then ProcessingInstruction
|
|
232
|
-
when :document then Document
|
|
233
|
-
when :declaration then Declaration
|
|
234
|
-
when :doctype then Doctype
|
|
235
|
-
when :attribute then Attribute
|
|
236
|
-
when :entity_reference then EntityReference
|
|
237
|
-
else self
|
|
238
|
-
end
|
|
230
|
+
type = adapter(context).node_type(node)
|
|
231
|
+
klass = node_type_map[type] || self
|
|
239
232
|
|
|
240
233
|
klass.new(node, context)
|
|
241
234
|
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Moxml
|
|
4
|
+
module SAX
|
|
5
|
+
# Splits a flat attribute hash into regular attributes and namespace declarations.
|
|
6
|
+
#
|
|
7
|
+
# Every adapter SAX bridge performs the same split: attributes whose names
|
|
8
|
+
# start with "xmlns" are namespace declarations, everything else is a regular
|
|
9
|
+
# attribute. This module provides a single implementation.
|
|
10
|
+
module NamespaceSplitter
|
|
11
|
+
# @param attributes [Hash, Array<Array>] attributes as a hash or array of pairs
|
|
12
|
+
# @return [Array(Hash, Hash)] [regular_attrs, namespaces]
|
|
13
|
+
def split_attributes_and_namespaces(attributes)
|
|
14
|
+
attrs = {}
|
|
15
|
+
ns = {}
|
|
16
|
+
|
|
17
|
+
each_attribute(attributes) do |name, value|
|
|
18
|
+
name_s = name.to_s
|
|
19
|
+
if name_s == "xmlns" || name_s.start_with?("xmlns:")
|
|
20
|
+
prefix = name_s == "xmlns" ? nil : name_s.sub("xmlns:", "")
|
|
21
|
+
ns[prefix] = value
|
|
22
|
+
else
|
|
23
|
+
attrs[name_s] = value
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
[attrs, ns]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def each_attribute(attributes, &block)
|
|
33
|
+
case attributes
|
|
34
|
+
when Hash
|
|
35
|
+
attributes.each(&block)
|
|
36
|
+
when Array
|
|
37
|
+
attributes.each { |pair| yield pair[0], pair[1] }
|
|
38
|
+
when nil
|
|
39
|
+
# nothing
|
|
40
|
+
else
|
|
41
|
+
if attributes.respond_to?(:each)
|
|
42
|
+
attributes.each do |item|
|
|
43
|
+
if item.is_a?(Array) && item.size >= 2
|
|
44
|
+
yield item[0], item[1]
|
|
45
|
+
elsif item.respond_to?(:name) && item.respond_to?(:value)
|
|
46
|
+
yield item.name, item.value
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/moxml/version.rb
CHANGED
data/lib/moxml/xml_utils.rb
CHANGED
|
@@ -75,7 +75,15 @@ module Moxml
|
|
|
75
75
|
# (W3C Namespaces in XML, https://www.w3.org/TR/xml-names/).
|
|
76
76
|
# Use split instead of parse to avoid scheme-specific validation
|
|
77
77
|
# that rejects valid opaque URIs like "mailto:bar".
|
|
78
|
-
URI::RFC3986_PARSER
|
|
78
|
+
if defined?(URI::RFC3986_PARSER)
|
|
79
|
+
URI::RFC3986_PARSER.split(uri)
|
|
80
|
+
elsif uri.match?(/\A[a-zA-Z][a-zA-Z0-9+\-.]*:[^\x00-\x20]*\z/)
|
|
81
|
+
# Minimal URI validation for Opal (no RFC3986_PARSER available)
|
|
82
|
+
else
|
|
83
|
+
# Accept relative references and bare paths
|
|
84
|
+
return unless uri.match?(/[\x00-\x08\x0B\x0C\x0E-\x1F]/)
|
|
85
|
+
raise ValidationError, "Invalid URI: #{uri}"
|
|
86
|
+
end
|
|
79
87
|
rescue URI::InvalidURIError
|
|
80
88
|
raise ValidationError, "Invalid URI: #{uri}"
|
|
81
89
|
end
|
|
@@ -6,7 +6,7 @@ RSpec.describe "Adapter Examples" do
|
|
|
6
6
|
describe "Serialization consistency" do
|
|
7
7
|
it "produces equivalent XML across adapters",
|
|
8
8
|
skip: "No easy way to exclude the declaration from Nokogiri documents" do
|
|
9
|
-
docs = Moxml::Adapter::
|
|
9
|
+
docs = Moxml::Adapter::AVAILABLE_ADAPTERS.map do |adapter|
|
|
10
10
|
Moxml.new(adapter).parse(xml, fragment: true)
|
|
11
11
|
end
|
|
12
12
|
|
|
@@ -32,7 +32,7 @@ RSpec.describe "Cross-adapter integration" do
|
|
|
32
32
|
"Performance Examples",
|
|
33
33
|
]
|
|
34
34
|
|
|
35
|
-
Moxml::Adapter::
|
|
35
|
+
Moxml::Adapter::AVAILABLE_ADAPTERS.each do |adapter_name|
|
|
36
36
|
context "with #{adapter_name}" do
|
|
37
37
|
around do |example|
|
|
38
38
|
Moxml.with_config(adapter_name) do
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
# invalid - validity error; non-validating parsers should accept
|
|
12
12
|
|
|
13
13
|
RSpec.describe "W3C XML Namespaces 1.0 test suite" do
|
|
14
|
-
Moxml::Adapter::
|
|
14
|
+
Moxml::Adapter::AVAILABLE_ADAPTERS.each do |adapter_name|
|
|
15
15
|
context "with #{adapter_name}" do
|
|
16
16
|
around do |example|
|
|
17
17
|
Moxml.with_config(adapter_name) do
|
|
@@ -11,6 +11,14 @@ RSpec.describe Moxml::Adapter::Ox do
|
|
|
11
11
|
|
|
12
12
|
it_behaves_like "xml adapter"
|
|
13
13
|
|
|
14
|
+
describe "node_type" do
|
|
15
|
+
it "returns :namespace for CustomizedOx::Namespace nodes" do
|
|
16
|
+
element = described_class.create_native_element("test")
|
|
17
|
+
ns = described_class.create_native_namespace(element, "ns", "http://example.com")
|
|
18
|
+
expect(described_class.node_type(ns)).to eq(:namespace)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
14
22
|
describe "text handling" do
|
|
15
23
|
let(:doc) { described_class.create_document }
|
|
16
24
|
let(:element) { described_class.create_native_element("test") }
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Moxml::Adapter, ".platform_adapters" do
|
|
6
|
+
it "includes all known adapters under MRI" do
|
|
7
|
+
expect(described_class.platform_adapters).to include(:nokogiri, :oga, :rexml, :ox)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "uses the AVAILABLE_ADAPTERS constant under MRI" do
|
|
11
|
+
expect(described_class.platform_adapters).to eq(Moxml::Adapter::AVAILABLE_ADAPTERS)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
RSpec.describe Moxml::Adapter, ".available?" do
|
|
16
|
+
it "returns true for :oga" do
|
|
17
|
+
expect(described_class.available?(:oga)).to be true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "returns true for :nokogiri under MRI" do
|
|
21
|
+
expect(described_class.available?(:nokogiri)).to be true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
context "when Opal platform adapters are in effect" do
|
|
25
|
+
before do
|
|
26
|
+
allow(described_class).to receive(:platform_adapters)
|
|
27
|
+
.and_return(Moxml::Adapter::OPAL_AVAILABLE_ADAPTERS)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "returns false for :nokogiri" do
|
|
31
|
+
expect(described_class.available?(:nokogiri)).to be false
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "returns true for :rexml" do
|
|
35
|
+
expect(described_class.available?(:rexml)).to be true
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
RSpec.describe Moxml::Adapter, ".load" do
|
|
41
|
+
context "when Opal platform adapters are in effect" do
|
|
42
|
+
before do
|
|
43
|
+
allow(described_class).to receive(:platform_adapters)
|
|
44
|
+
.and_return(Moxml::Adapter::OPAL_AVAILABLE_ADAPTERS)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "raises AdapterError for :nokogiri" do
|
|
48
|
+
expect { described_class.load(:nokogiri) }.to raise_error(
|
|
49
|
+
Moxml::AdapterError, /not available on this platform/
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
RSpec.describe "Moxml::Adapter::OPAL_AVAILABLE_ADAPTERS" do
|
|
56
|
+
it "contains only :rexml" do
|
|
57
|
+
expect(Moxml::Adapter::OPAL_AVAILABLE_ADAPTERS).to eq(%i[rexml])
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
RSpec.describe "Moxml::Adapter::CONST_NAME_MAP" do
|
|
62
|
+
it "maps :headed_ox to HeadedOx" do
|
|
63
|
+
expect(Moxml::Adapter::CONST_NAME_MAP[:headed_ox]).to eq("HeadedOx")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "falls back to capitalize for unmapped adapters" do
|
|
67
|
+
expect(Moxml::Adapter::CONST_NAME_MAP[:nokogiri]).to be_nil
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -159,9 +159,6 @@ RSpec.shared_examples "xml adapter" do
|
|
|
159
159
|
if described_class.name.include?("Oga")
|
|
160
160
|
pending("Oga does not support indentation settings")
|
|
161
161
|
end
|
|
162
|
-
if described_class.name.include?("Rexml")
|
|
163
|
-
pending("Postponed for Rexml till better times")
|
|
164
|
-
end
|
|
165
162
|
if described_class.name.include?("Libxml")
|
|
166
163
|
skip("LibXML serialization does not support indentation (documented limitation)")
|
|
167
164
|
end
|
|
@@ -219,9 +216,6 @@ RSpec.shared_examples "xml adapter" do
|
|
|
219
216
|
end
|
|
220
217
|
|
|
221
218
|
it "preserves and correctly handles multiple namespaces" do
|
|
222
|
-
if described_class.name.include?("Rexml")
|
|
223
|
-
pending("Rexml does not respect ZPath namespaces")
|
|
224
|
-
end
|
|
225
219
|
# Parse original XML
|
|
226
220
|
doc = described_class.parse(xml).native
|
|
227
221
|
|
|
@@ -180,6 +180,16 @@ RSpec.describe Moxml::EntityRegistry do
|
|
|
180
180
|
registry = described_class.new
|
|
181
181
|
expect(registry.load_all).to be(registry)
|
|
182
182
|
end
|
|
183
|
+
|
|
184
|
+
it "emits deprecation warnings" do
|
|
185
|
+
registry = described_class.new
|
|
186
|
+
[-> { registry.load_html5 },
|
|
187
|
+
-> { registry.load_mathml },
|
|
188
|
+
-> { registry.load_iso },
|
|
189
|
+
-> { registry.load_all }].each do |callable|
|
|
190
|
+
expect { callable.call }.to output.to_stderr
|
|
191
|
+
end
|
|
192
|
+
end
|
|
183
193
|
end
|
|
184
194
|
|
|
185
195
|
describe "#standard_entity?" do
|
|
@@ -1,10 +1,47 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "spec_helper"
|
|
4
|
-
require_relative "shared_examples"
|
|
5
4
|
|
|
6
5
|
RSpec.describe Moxml::NativeAttachment::Opal do
|
|
7
|
-
|
|
6
|
+
subject(:attachments) { described_class.new }
|
|
7
|
+
|
|
8
|
+
let(:native) { Object.new }
|
|
9
|
+
let(:other_native) { Object.new }
|
|
10
|
+
|
|
11
|
+
it "stores and reads attachments by native object and key" do
|
|
12
|
+
attachments.set(native, :entity_refs, ["amp"])
|
|
13
|
+
attachments.set(native, :doctype, "html")
|
|
14
|
+
attachments.set(other_native, :entity_refs, ["lt"])
|
|
15
|
+
|
|
16
|
+
aggregate_failures do
|
|
17
|
+
expect(attachments.get(native, :entity_refs)).to eq(["amp"])
|
|
18
|
+
expect(attachments.get(native, :doctype)).to eq("html")
|
|
19
|
+
expect(attachments.get(other_native, :entity_refs)).to eq(["lt"])
|
|
20
|
+
expect(attachments.key?(native, :entity_refs)).to be(true)
|
|
21
|
+
expect(attachments.key?(native, :missing)).to be(false)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "preserves explicit nil attachments" do
|
|
26
|
+
attachments.set(native, :xml_declaration, nil)
|
|
27
|
+
|
|
28
|
+
aggregate_failures do
|
|
29
|
+
expect(attachments.get(native, :xml_declaration)).to be_nil
|
|
30
|
+
expect(attachments.key?(native, :xml_declaration)).to be(true)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "deletes attachments" do
|
|
35
|
+
attachments.set(native, :entity_refs, ["amp"])
|
|
36
|
+
|
|
37
|
+
expect(attachments.delete(native, :entity_refs)).to eq(["amp"])
|
|
38
|
+
|
|
39
|
+
aggregate_failures do
|
|
40
|
+
expect(attachments.get(native, :entity_refs)).to be_nil
|
|
41
|
+
expect(attachments.key?(native, :entity_refs)).to be(false)
|
|
42
|
+
expect(attachments.delete(native, :entity_refs)).to be_nil
|
|
43
|
+
end
|
|
44
|
+
end
|
|
8
45
|
|
|
9
46
|
it "stores attachments in Moxml-owned instance variables" do
|
|
10
47
|
attachments = described_class.new
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Moxml::Node, "node_type_map" do
|
|
6
|
+
it "maps every type symbol to a Node subclass" do
|
|
7
|
+
described_class.node_type_map.each_value do |klass|
|
|
8
|
+
expect(klass < described_class).to be(true), "Expected #{klass} < Moxml::Node"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "covers all standard node types" do
|
|
13
|
+
expected = %i[element text cdata comment processing_instruction
|
|
14
|
+
document declaration doctype attribute entity_reference]
|
|
15
|
+
expect(described_class.node_type_map.keys).to match_array(expected)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "is frozen to prevent modification" do
|
|
19
|
+
expect(described_class.node_type_map).to be_frozen
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe ".wrap" do
|
|
23
|
+
let(:ctx) { Moxml.new(:nokogiri) }
|
|
24
|
+
|
|
25
|
+
it "returns nil for nil input" do
|
|
26
|
+
expect(described_class.wrap(nil, ctx)).to be_nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "wraps native nodes using node_type_map" do
|
|
30
|
+
doc = ctx.parse("<root><child>text</child></root>")
|
|
31
|
+
root = doc.root
|
|
32
|
+
expect(root).to be_a(Moxml::Element)
|
|
33
|
+
|
|
34
|
+
text_node = root.children.first.children.first
|
|
35
|
+
expect(text_node).to be_a(Moxml::Text)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "returns a Node for unknown types" do
|
|
39
|
+
node = described_class.wrap(Object.new, ctx)
|
|
40
|
+
expect(node).to be_a(described_class)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "moxml/adapter/shared_examples/adapter_contract"
|
|
5
|
+
|
|
6
|
+
RSpec.describe Moxml::Adapter::Rexml, if: RUBY_ENGINE == "opal" do
|
|
7
|
+
around do |example|
|
|
8
|
+
Moxml.with_config(:rexml, true, "UTF-8") do
|
|
9
|
+
example.run
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it_behaves_like "xml adapter"
|
|
14
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe "Moxml Opal smoke test", if: RUBY_ENGINE == "opal" do
|
|
6
|
+
let(:context) { Moxml.new(:rexml) }
|
|
7
|
+
|
|
8
|
+
it "parses XML string to document" do
|
|
9
|
+
doc = context.parse("<root><child>text</child></root>")
|
|
10
|
+
expect(doc).not_to be_nil
|
|
11
|
+
root = doc.root
|
|
12
|
+
expect(root.name).to eq("root")
|
|
13
|
+
expect(root.children.first.name).to eq("child")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "round-trips parse and serialize" do
|
|
17
|
+
xml = '<person name="Alice"><age>30</age></person>'
|
|
18
|
+
doc = context.parse(xml)
|
|
19
|
+
serialized = doc.to_xml
|
|
20
|
+
expect(serialized).to include("person")
|
|
21
|
+
expect(serialized).to include("Alice")
|
|
22
|
+
expect(serialized).to include("30")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "builds XML document from scratch" do
|
|
26
|
+
doc = context.create_document
|
|
27
|
+
root = doc.create_element("root")
|
|
28
|
+
root["attr"] = "value"
|
|
29
|
+
text = doc.create_text("hello")
|
|
30
|
+
root.add_child(text)
|
|
31
|
+
doc.add_child(root)
|
|
32
|
+
|
|
33
|
+
expect(doc.to_xml).to include('attr="value"')
|
|
34
|
+
expect(doc.to_xml).to include("hello")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "handles namespaces" do
|
|
38
|
+
xml = '<root xmlns:ns="http://example.com"><ns:child>data</ns:child></root>'
|
|
39
|
+
doc = context.parse(xml)
|
|
40
|
+
expect(doc.to_xml).to include("ns:child")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "handles CDATA sections" do
|
|
44
|
+
xml = "<root><![CDATA[<script>alert('xss')</script>]]></root>"
|
|
45
|
+
doc = context.parse(xml)
|
|
46
|
+
expect(doc.to_xml).to include("<script>")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "handles comments" do
|
|
50
|
+
xml = "<root><!-- a comment --><child/></root>"
|
|
51
|
+
doc = context.parse(xml)
|
|
52
|
+
expect(doc.to_xml).to include("a comment")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "handles processing instructions" do
|
|
56
|
+
xml = '<?xml version="1.0"?><?pi-target pi-data?><root/>'
|
|
57
|
+
doc = context.parse(xml)
|
|
58
|
+
serialized = doc.to_xml
|
|
59
|
+
expect(serialized).to include("pi-target")
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Moxml::SAX::NamespaceSplitter do
|
|
6
|
+
let(:bridge_class) do
|
|
7
|
+
Class.new do
|
|
8
|
+
include Moxml::SAX::NamespaceSplitter
|
|
9
|
+
|
|
10
|
+
attr_reader :last_attrs, :last_ns
|
|
11
|
+
|
|
12
|
+
def split(attributes)
|
|
13
|
+
@last_attrs, @last_ns = split_attributes_and_namespaces(attributes)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
let(:splitter) { bridge_class.new }
|
|
19
|
+
|
|
20
|
+
describe "#split_attributes_and_namespaces" do
|
|
21
|
+
it "splits hash attributes into attrs and namespaces" do
|
|
22
|
+
attrs, ns = splitter.split({
|
|
23
|
+
"xmlns" => "http://default.ns",
|
|
24
|
+
"xmlns:foo" => "http://foo.ns",
|
|
25
|
+
"id" => "123",
|
|
26
|
+
"class" => "bar",
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
expect(attrs).to eq("id" => "123", "class" => "bar")
|
|
30
|
+
expect(ns).to eq(nil => "http://default.ns", "foo" => "http://foo.ns")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "splits array-of-pairs attributes" do
|
|
34
|
+
attrs, ns = splitter.split([
|
|
35
|
+
["xmlns:prefix", "http://example.com"],
|
|
36
|
+
["name", "value"],
|
|
37
|
+
])
|
|
38
|
+
|
|
39
|
+
expect(attrs).to eq("name" => "value")
|
|
40
|
+
expect(ns).to eq("prefix" => "http://example.com")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "returns empty hashes when all attributes are namespaces" do
|
|
44
|
+
attrs, ns = splitter.split({ "xmlns" => "http://default.ns" })
|
|
45
|
+
expect(attrs).to be_empty
|
|
46
|
+
expect(ns).to eq(nil => "http://default.ns")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "returns empty hashes when there are no namespaces" do
|
|
50
|
+
attrs, ns = splitter.split({ "id" => "1", "name" => "test" })
|
|
51
|
+
expect(attrs).to eq("id" => "1", "name" => "test")
|
|
52
|
+
expect(ns).to be_empty
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "handles nil attributes" do
|
|
56
|
+
attrs, ns = splitter.split(nil)
|
|
57
|
+
expect(attrs).to be_empty
|
|
58
|
+
expect(ns).to be_empty
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "handles empty hash" do
|
|
62
|
+
attrs, ns = splitter.split({})
|
|
63
|
+
expect(attrs).to be_empty
|
|
64
|
+
expect(ns).to be_empty
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
data/spec/moxml/text_spec.rb
CHANGED
|
@@ -34,7 +34,7 @@ RSpec.describe Moxml::Text do
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
it "is consistent across adapters" do
|
|
37
|
-
Moxml::Adapter::
|
|
37
|
+
Moxml::Adapter::AVAILABLE_ADAPTERS.each do |adapter_name|
|
|
38
38
|
ctx = Moxml.new(adapter_name)
|
|
39
39
|
d = ctx.parse("<root>hello world</root>")
|
|
40
40
|
text = d.root.children.first
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
# SimpleCov must be loaded before application code
|
|
4
|
-
if ENV.fetch("COVERAGE", nil) == "true"
|
|
4
|
+
if ENV.fetch("COVERAGE", nil) == "true" && RUBY_ENGINE != "opal"
|
|
5
5
|
require "simplecov"
|
|
6
6
|
|
|
7
7
|
SimpleCov.start do
|
|
@@ -25,20 +25,25 @@ if ENV.fetch("COVERAGE", nil) == "true"
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
require "moxml"
|
|
28
|
-
require "nokogiri"
|
|
28
|
+
require "nokogiri" unless RUBY_ENGINE == "opal"
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
unless RUBY_ENGINE == "opal"
|
|
31
|
+
# Load shared examples from new locations
|
|
32
|
+
Dir[File.expand_path("integration/shared_examples/**/*.rb",
|
|
33
|
+
__dir__)].each do |f|
|
|
34
|
+
require f
|
|
35
|
+
end
|
|
36
|
+
Dir[File.expand_path("moxml/adapter/shared_examples/**/*.rb",
|
|
37
|
+
__dir__)].each do |f|
|
|
38
|
+
require f
|
|
39
|
+
end
|
|
38
40
|
end
|
|
39
41
|
Dir[File.expand_path("support/**/*.rb", __dir__)].each { |f| require f }
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
|
|
43
|
+
unless RUBY_ENGINE == "opal"
|
|
44
|
+
Dir[File.expand_path("performance/*.rb", __dir__)].each { |f| require f }
|
|
45
|
+
Dir[File.expand_path("examples/*.rb", __dir__)].each { |f| require f }
|
|
46
|
+
end
|
|
42
47
|
|
|
43
48
|
# Clear XPath caches immediately to ensure fresh compilation
|
|
44
49
|
# This is critical when code changes affect compiled XPath expressions
|
|
@@ -78,12 +83,26 @@ RSpec.configure do |config|
|
|
|
78
83
|
# Configure to skip examples unless explicitly run
|
|
79
84
|
config.filter_run_excluding examples: true unless ENV["RUN_EXAMPLES"]
|
|
80
85
|
|
|
86
|
+
# Under Opal, exclude specs that require native-only adapters or filesystem
|
|
87
|
+
if RUBY_ENGINE == "opal"
|
|
88
|
+
config.filter_run_excluding(
|
|
89
|
+
:native_adapter,
|
|
90
|
+
:native_fs,
|
|
91
|
+
:subprocess,
|
|
92
|
+
:xpath_native,
|
|
93
|
+
:performance,
|
|
94
|
+
:examples,
|
|
95
|
+
:consistency,
|
|
96
|
+
)
|
|
97
|
+
end
|
|
98
|
+
|
|
81
99
|
config.order = :random
|
|
82
100
|
Kernel.srand config.seed
|
|
83
101
|
end
|
|
84
102
|
|
|
85
103
|
Moxml.configure do |config|
|
|
86
|
-
config.adapter = :nokogiri
|
|
104
|
+
config.adapter = RUBY_ENGINE == "opal" ? :rexml : :nokogiri
|
|
87
105
|
config.strict_parsing = true
|
|
88
106
|
config.default_encoding = "UTF-8"
|
|
107
|
+
config.entity_load_mode = :optional if RUBY_ENGINE == "opal"
|
|
89
108
|
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Opal runtime patches for moxml specs.
|
|
4
|
+
#
|
|
5
|
+
# Moxml already handles Opal-specific behavior internally
|
|
6
|
+
# (NativeAttachment::Opal, Config::OPAL_DEFAULT_ADAPTER, EntityRegistry::OPAL_ENTITY_DATA).
|
|
7
|
+
# This file is loaded only under Opal for any additional test-environment patches.
|
|
8
|
+
|
|
9
|
+
# Under Opal, moxml uses REXML adapter (Opal reimplements strscan/stringio
|
|
10
|
+
# in its stdlib, enabling REXML to compile to JavaScript).
|
|
11
|
+
Moxml.configure do |config|
|
|
12
|
+
config.adapter = :rexml
|
|
13
|
+
config.strict_parsing = false
|
|
14
|
+
config.default_encoding = "UTF-8"
|
|
15
|
+
config.entity_load_mode = :optional
|
|
16
|
+
end
|