moxml 0.6.22 → 0.6.24

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d96cf20ddab1915f70f97da23f4d4f4c29fdb70d932f5bfe8b967a516c9c6fc
4
- data.tar.gz: '091e0ae806bdad337cd00382d6591eea7ddffed177f450e57f2a5e5bf727faee'
3
+ metadata.gz: 90baa6c32eb44a50601f1d9784e50bfa00c4b2d172d1ebdbef30ffbba80c1fbd
4
+ data.tar.gz: 2eaf0b64683cb001d40585752601ec6f41af5f88339744c108370e51404ea508
5
5
  SHA512:
6
- metadata.gz: e4a8447e6b217c0c2dafdf3637bb237ef1f9427a67ba713e878981d343583611fcc7662ee15812671d634092dda42505a7e4df6417d3743e9f16df5d5ffc0bb6
7
- data.tar.gz: 5206932c75097fb82d3c39a8ca7a0c71e5940fbf0a92df55a5faf2b5dcfab10f892c039c9007ac27dbf048e5912031c2e1c109d888901d299d2202abc176ae0c
6
+ metadata.gz: 811ab8695281e1d5bebe4abdc3a2ab70e518aeb799fd4300c6b7a26d64d965188f3734fcd970251f06a1fab0e317028ee1af817362415ce8aecf0c12f22ab939
7
+ data.tar.gz: 7243257da8297195ebbc3039e21557033fc299b18b8e77c9eb796878e8e57f20f483378f218347e070f6eabd8b7fb316f274dd275de9fb150c75a65d3bcf2f80
@@ -29,6 +29,12 @@ module Moxml
29
29
  Entity.preprocess_entities(xml)
30
30
  end
31
31
 
32
+ # The document-parse-entry variant (#604): adds the
33
+ # declared-encoding rewrite on top of the general path.
34
+ def preprocess_for_parse(xml)
35
+ Entity.preprocess_for_parse(xml)
36
+ end
37
+
32
38
  def decode_entities(text)
33
39
  Entity.decode_entities(text)
34
40
  end
@@ -3670,6 +3670,12 @@ module Moxml
3670
3670
  end
3671
3671
 
3672
3672
  def add_child_impl(parent, child)
3673
+ # TextSegment value objects (the entity-retention split
3674
+ # runs) are the adapter's own parse output - attaching one
3675
+ # attaches its text content (#606): the raw value object
3676
+ # would fall through to the binding, which rejects anything
3677
+ # that is not a native Node or String.
3678
+ child = child.content.to_s if child.is_a?(CustomizedLeptris::TextSegment)
3673
3679
  # Same-document Identity append — the builder hot path. One
3674
3680
  # document pair decides the verdict: the pin, the absorb
3675
3681
  # checks and the binding bridges only exist for
@@ -200,7 +200,7 @@ module Moxml
200
200
  # Preprocess entities before parsing.
201
201
  # This converts the string to UTF-8; LibXML will use the encoding
202
202
  # parameter or XML declaration for byte interpretation.
203
- xml_string = preprocess_entities(xml_string)
203
+ xml_string = preprocess_for_parse(xml_string)
204
204
 
205
205
  # Extract DOCTYPE before parsing. Locate the literal first:
206
206
  # a plain memscan keeps doctype-less documents (the common
@@ -32,7 +32,7 @@ module Moxml
32
32
  # Nokogiri has a real fragment node type — its children ARE
33
33
  # the fragment's top-level nodes.
34
34
  def parse_fragment(xml, _context = nil)
35
- processed = Entity.preprocess_entities(xml)
35
+ processed = Entity.preprocess_for_parse(xml)
36
36
  ::Nokogiri::XML::DocumentFragment.parse(processed) do |config|
37
37
  config.strict.nonet
38
38
  config.recover
@@ -44,7 +44,7 @@ module Moxml
44
44
  end
45
45
 
46
46
  def parse(xml, options = {}, _context = nil)
47
- processed_xml = strip_bom(preprocess_entities(xml))
47
+ processed_xml = strip_bom(preprocess_for_parse(xml))
48
48
 
49
49
  native_doc = begin
50
50
  ::Oga.parse_xml(processed_xml, strict: options[:strict])
@@ -35,7 +35,7 @@ module Moxml
35
35
  end
36
36
 
37
37
  def parse(xml, options = {}, _context = nil)
38
- processed_xml = preprocess_entities(xml)
38
+ processed_xml = preprocess_for_parse(xml)
39
39
  native_doc = begin
40
40
  # ::Ox.parse has arity 1 and hardcodes skip: :skip_white;
41
41
  # ::Ox.load takes the policy (issue #189). Both knobs flow
@@ -36,7 +36,7 @@ module Moxml
36
36
  processed_xml = xml.encode("UTF-8")
37
37
 
38
38
  # Preprocess entities to avoid double-escaping on output
39
- processed_xml = preprocess_entities(processed_xml)
39
+ processed_xml = preprocess_for_parse(processed_xml)
40
40
 
41
41
  native_doc = begin
42
42
  ::REXML::Document.new(processed_xml)
data/lib/moxml/entity.rb CHANGED
@@ -93,7 +93,13 @@ module Moxml
93
93
  # whether the result can contain markers: the flag rides the
94
94
  # same `&` scan, so callers needing it (leptris parse) avoid a
95
95
  # second full-buffer multibyte probe.
96
- def preprocess_with_marker_flag(xml)
96
+ #
97
+ # rewrite_encoding: the declared-encoding rewrite is a
98
+ # document-parse-entry protection only (#604) - the general
99
+ # path serves text emitters whose payloads legitimately BEGIN
100
+ # with an XML declaration (verbatim data), and rewriting them
101
+ # is a content mutation.
102
+ def preprocess_with_marker_flag(xml, rewrite_encoding: false)
97
103
  return ["", false] if xml.nil?
98
104
 
99
105
  str = if xml.encoding == Encoding::BINARY
@@ -112,7 +118,7 @@ module Moxml
112
118
  else
113
119
  xml.encode("UTF-8")
114
120
  end
115
- str = rewrite_declared_encoding(str)
121
+ str = rewrite_declared_encoding(str) if rewrite_encoding
116
122
  # Fast path: no `&` means no entity references to mark — skip
117
123
  # the regex scan and string allocation entirely. The vast
118
124
  # majority of XML payloads contain no entity references.
@@ -136,10 +142,23 @@ module Moxml
136
142
  [processed, marked]
137
143
  end
138
144
 
145
+ # The general path (#604): entity marking + UTF-8 normalization
146
+ # WITHOUT the declared-encoding rewrite - payloads that begin
147
+ # with an XML declaration are verbatim data here (text emitters),
148
+ # not parse payloads.
139
149
  def preprocess_entities(xml)
140
150
  preprocess_with_marker_flag(xml)[0]
141
151
  end
142
152
 
153
+ # The document-parse-entry path: everything preprocess_entities
154
+ # does, plus the declared-encoding rewrite - moxml feeds engines
155
+ # UTF-8 bytes, so a declaration naming another encoding must be
156
+ # rewritten or the bytes decode twice into mojibake. Only the
157
+ # adapter parse entries call this.
158
+ def preprocess_for_parse(xml)
159
+ preprocess_with_marker_flag(xml, rewrite_encoding: true)[0]
160
+ end
161
+
143
162
  # Engines honor the DECLARED encoding and re-decode the bytes they
144
163
  # are handed — moxml always feeds UTF-8 (see the normalization
145
164
  # above), so a declaration naming another encoding must be
data/lib/moxml/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Moxml
4
- VERSION = "0.6.22"
4
+ VERSION = "0.6.24"
5
5
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml/moxml#606: under entity-reference retention, parsing
6
+ # marker-bearing text mints CustomizedLeptris::TextSegment value
7
+ # objects as the native children behind Wrappers::Text. Re-attaching
8
+ # such a parsed child via add_child crashed - the attach path handed
9
+ # the raw value object to the binding, which accepts only native
10
+ # Nodes and Strings. The adapter's own parse output must be accepted
11
+ # by its own attach path: a TextSegment attaches as its text content.
12
+ RSpec.describe "TextSegment attach under entity retention (#606)" do
13
+ let(:ctx) { Moxml.new(:leptris) }
14
+
15
+ before do
16
+ ctx.config.restore_entities = true
17
+ end
18
+
19
+ it "re-attaches parsed marker-split children to another element" do
20
+ parsed = ctx.parse(
21
+ "<fragment>alpha&nbsp;beta&mdash;gamma<child a=\"1\"/></fragment>",
22
+ )
23
+ target = parsed.document.create_element("t")
24
+
25
+ parsed.root.children.each { |c| target.add_child(c) }
26
+
27
+ names = target.children.map { |c| c.respond_to?(:name) ? c.name : nil }
28
+ expect(names).to include("child")
29
+ expect(target.children.count).to be_positive
30
+ end
31
+
32
+ it "preserves the re-attached text runs verbatim" do
33
+ parsed = ctx.parse("<fragment>alpha&nbsp;beta&mdash;gamma</fragment>")
34
+ target = parsed.document.create_element("t")
35
+
36
+ parsed.root.children.each { |c| target.add_child(c) }
37
+
38
+ combined = target.children.to_a.join
39
+ expect(combined).to include("alpha")
40
+ expect(combined).to include("beta")
41
+ expect(combined).to include("gamma")
42
+ end
43
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml/moxml#604 + lutaml/lutaml-model#1249: the declared-encoding
6
+ # rewrite is a document-parse-entry protection (moxml feeds engines
7
+ # UTF-8 bytes, so a declaration naming another encoding must be
8
+ # rewritten or the bytes decode twice into mojibake). The general
9
+ # `preprocess_entities` API also serves text emitters - payloads that
10
+ # legitimately BEGIN with an XML declaration (an RFC sourcecode block
11
+ # carrying an example document) are verbatim data, and the rewrite
12
+ # mutated them. The general path keeps the entity marking and drops
13
+ # the rewrite; the rewrite rides the parse-entry variant only.
14
+ RSpec.describe "the preprocess encoding scope (#604)" do
15
+ let(:declared_payload) do
16
+ %(<?xml version="1.0" encoding="utf-8"?>\n<EBMLSchema xmlns="urn:ietf:rfc:8794"/>)
17
+ end
18
+
19
+ describe "the general path (text emitters)" do
20
+ it "preserves a payload's declared encoding verbatim" do
21
+ processed = Moxml.preprocess_entities(declared_payload)
22
+
23
+ expect(processed).to include('encoding="utf-8"')
24
+ expect(processed).not_to include('encoding="UTF-8"?>')
25
+ end
26
+
27
+ it "keeps the entity marking behavior" do
28
+ processed = Moxml.preprocess_entities("<a>&nbsp;</a>")
29
+
30
+ # non-standard entities are MARKED (the marker lifecycle), not
31
+ # passed through
32
+ expect(processed).to include(Moxml::Entity::MARKER)
33
+ end
34
+ end
35
+
36
+ describe "the parse-entry path (engine feeds)" do
37
+ it "still rewrites a foreign declared encoding for the engine" do
38
+ # UTF-8 bytes handed to the engine with a declaration naming
39
+ # iso-8859-1: the rewrite keeps the bytes decoding once
40
+ doc = Moxml.new(:leptris).parse(
41
+ %(<?xml version="1.0" encoding="iso-8859-1"?><a>caf\xc3\xa9</a>),
42
+ )
43
+
44
+ expect(doc.root.text).to eq("café")
45
+ end
46
+
47
+ it "rewrites the declaration on the parsed document's reparsed form" do
48
+ payload = declared_payload
49
+ doc = Moxml.new(:leptris).parse(payload)
50
+
51
+ expect(doc.root.name).to eq("EBMLSchema")
52
+ end
53
+ end
54
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moxml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.22
4
+ version: 0.6.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-27 00:00:00.000000000 Z
11
+ date: 2026-11-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Moxml is a unified XML manipulation library that provides a common API
@@ -507,6 +507,7 @@ files:
507
507
  - spec/moxml/adapter/leptris_sax_records_spec.rb
508
508
  - spec/moxml/adapter/leptris_spec.rb
509
509
  - spec/moxml/adapter/leptris_strict_mismatch_spec.rb
510
+ - spec/moxml/adapter/leptris_textsegment_attach_spec.rb
510
511
  - spec/moxml/adapter/leptris_xpath_bulk_rows_spec.rb
511
512
  - spec/moxml/adapter/leptris_xpath_identity_spec.rb
512
513
  - spec/moxml/adapter/libxml_internals_spec.rb
@@ -584,6 +585,7 @@ files:
584
585
  - spec/moxml/node_type_map_spec.rb
585
586
  - spec/moxml/plan_conformance_spec.rb
586
587
  - spec/moxml/plan_spec.rb
588
+ - spec/moxml/preprocess_encoding_spec.rb
587
589
  - spec/moxml/processing_instruction_spec.rb
588
590
  - spec/moxml/reader_spec.rb
589
591
  - spec/moxml/readonly_parse_spec.rb