moxml 0.6.19 → 0.6.21

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: 53942994e010ed70c374e07b25481ef3eb5abfec364e671984d23bc5616f9f47
4
- data.tar.gz: bc0ef12fae020047d4c981c2378fc95d2245d955e7d97367aa0b2b8d1947aaf7
3
+ metadata.gz: 69067673035541114de7ece6594d659e3b1f664f8392e6760dacabac00636526
4
+ data.tar.gz: 77a12103407b1d58bd90d51c6148c5b7f2832869b93074aa89e303e714060c7e
5
5
  SHA512:
6
- metadata.gz: b64be44bf23886c0053cbb3c1f0e45c49fa2a666b53ef12f64bfa19f858009f94bff7ce093b8441b15f4da2b34c7551c5d41903d4f915cbf8723da05303013d6
7
- data.tar.gz: 5cb67e455844e8bd0c8556bd86e5764ba3d945e17947e453283760ec1f33c5929c03c5c805b07df189f86f57f85c389801449e133f4675bb25217fef8c125f63
6
+ metadata.gz: a04b0ee4b33048b9f59b0199e0c4bd62a73d730794defce0b37d6697b81ca914fd339320aa39d815a7286c6ce761d179942ab8cf40877f0f2829b590b5ffd84e
7
+ data.tar.gz: bedd8753aeb05ab885fe8288810b576329b02399081e2c9b854d1f2df55b4bc7d5ca67da11e237bc55574cbad1aa9da182aadf32157dbca4cd8553a43c9a3d2b
data/Gemfile CHANGED
@@ -12,7 +12,7 @@ gem "base64"
12
12
  gem "benchmark"
13
13
  gem "benchmark-ips"
14
14
  gem "get_process_mem"
15
- gem "leptris", "~> 1.9.446"
15
+ gem "leptris", "~> 1.9.449"
16
16
  gem "libxml-ruby", ">= 5.0"
17
17
  gem "nokogiri", "~> 1.18"
18
18
  gem "oga"
data/README.adoc CHANGED
@@ -128,7 +128,7 @@ pair serves; cross-import schema sets (`Context#xsd_set`) ride the
128
128
  compile_set face (1.9.386.0 ✓, the floor now requires it).
129
129
  |===
130
130
 
131
- NOTE: moxml's dev Gemfile pins `leptris ~> 1.9.446` (the tested pair; the gemspec itself has zero runtime dependencies). The adapter enforces the floor at load.
131
+ NOTE: moxml's dev Gemfile pins `leptris ~> 1.9.449` (the tested pair; the gemspec itself has zero runtime dependencies). The adapter enforces the floor at load.
132
132
  `ox ~> 2.14`, `libxml-ruby >= 5.0`, and the vendored oga fork — the CI
133
133
  matrix tests exactly those floors.
134
134
 
@@ -772,6 +772,26 @@ namespace_validation_mode: :strict)
772
772
  attr.name
773
773
  end
774
774
 
775
+ # The native's own qualified spelling of an attribute name —
776
+ # the attributes surface's bracket lookup matches it (#582
777
+ # bug 2). Default: the native name is already qualified.
778
+ def attribute_qualified_name(attr)
779
+ attr.name.to_s
780
+ end
781
+
782
+ # The streaming record face behind Moxml::Reader (#582 gap 4):
783
+ # a table answering count/kind/parent/view/attr_* over the
784
+ # document's pre-order stream. Adapters without a streaming
785
+ # face raise loudly — the Reader never falls back to a
786
+ # materializing parse.
787
+ def reader_records(_source)
788
+ raise Moxml::NotImplementedError.new(
789
+ "#{name.split('::').last} does not provide a streaming " \
790
+ "Reader face",
791
+ adapter: name,
792
+ )
793
+ end
794
+
775
795
  # Return the actual native node after an add_child operation.
776
796
  # Override for adapters where node identity may change (e.g., LibXML doc.root=).
777
797
  def actual_native(child_native, _parent_native)
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moxml
4
+ module Adapter
5
+ class Leptris
6
+ # The Reader's uniform record surface (#582 gap 4): one shape
7
+ # over both sources — the engine's bulk drain table (the hot
8
+ # path) and a callback-stream fallback for documents outside
9
+ # the drain subset (the engine returns nil for those, e.g.
10
+ # declaration-bearing documents). The Reader navigates depth
11
+ # and subtree boundaries from parent indices; end markers are
12
+ # synthesized, so the fallback records starts and text only.
13
+ #
14
+ # @private
15
+ class ReaderRecordTable
16
+ def self.open(xml_string)
17
+ native = ::Leptris::XML::SAX::Records.open(xml_string)
18
+ return new(native) if native
19
+
20
+ build_from_callbacks(xml_string)
21
+ end
22
+
23
+ def self.build_from_callbacks(xml_string)
24
+ rows = []
25
+ stack = []
26
+ handler = Object.new
27
+ handler.define_singleton_method(:on_start_element) do |name, attr_hash, _ns_hash|
28
+ attrs = (attr_hash || {}).map do |k, v|
29
+ [k.to_s.dup, v.to_s.dup, v.match?(/[\t\n\r]/)]
30
+ end
31
+ rows << { element: true, view: name.dup,
32
+ parent: stack.last, attrs: attrs }
33
+ stack.push(rows.size - 1)
34
+ end
35
+ handler.define_singleton_method(:on_end_element) do |_name|
36
+ stack.pop
37
+ end
38
+ handler.define_singleton_method(:on_characters) do |text|
39
+ unless text.empty?
40
+ rows << { element: false, view: text.dup,
41
+ parent: stack.last, attrs: [] }
42
+ end
43
+ end
44
+ %i[on_start_document on_end_document on_comment on_cdata
45
+ on_processing_instruction on_error].each do |ignored|
46
+ handler.define_singleton_method(ignored) do |*_args|
47
+ # the Reader ignores lifecycle callbacks
48
+ end
49
+ end
50
+
51
+ bridge = LeptrisSAXBridge.new(handler)
52
+ ::Leptris::XML::SAX::Parser.new(bridge).parse(xml_string)
53
+ new(CallbackTable.new(rows))
54
+ end
55
+
56
+ def initialize(native)
57
+ @native = native
58
+ end
59
+
60
+ def count
61
+ @native.count
62
+ end
63
+
64
+ def element?(index)
65
+ if @native.is_a?(CallbackTable)
66
+ @native.element?(index)
67
+ else
68
+ @native.kind(index) == ::Leptris::XML::SAX::Records::KIND_ELEMENT
69
+ end
70
+ end
71
+
72
+ def view(index)
73
+ @native.view(index)
74
+ end
75
+
76
+ def parent(index)
77
+ @native.parent(index)
78
+ end
79
+
80
+ def attr_first(index)
81
+ @native.attr_first(index)
82
+ end
83
+
84
+ def attr_count_of(index)
85
+ @native.attr_count_of(index)
86
+ end
87
+
88
+ def attr_name(attr_index)
89
+ @native.attr_name(attr_index)
90
+ end
91
+
92
+ def attr_value(attr_index)
93
+ @native.attr_value(attr_index)
94
+ end
95
+
96
+ def attr_value_has_ws?(attr_index)
97
+ @native.attr_value_has_ws?(attr_index)
98
+ end
99
+
100
+ def free
101
+ @native.free
102
+ end
103
+
104
+ # The callback-stream table: same surface, Ruby rows.
105
+ class CallbackTable
106
+ attr_reader :rows
107
+
108
+ def initialize(rows)
109
+ @rows = rows
110
+ @attr_offsets = []
111
+ offset = 0
112
+ @rows.each_with_index do |row, i|
113
+ @attr_offsets[i] = offset
114
+ offset += row[:attrs].size
115
+ end
116
+ end
117
+
118
+ def count
119
+ @rows.size
120
+ end
121
+
122
+ def element?(index)
123
+ @rows[index][:element]
124
+ end
125
+
126
+ def view(index)
127
+ @rows[index][:view]
128
+ end
129
+
130
+ def parent(index)
131
+ @rows[index][:parent]
132
+ end
133
+
134
+ def attr_first(index)
135
+ @attr_offsets[index]
136
+ end
137
+
138
+ def attr_count_of(index)
139
+ @rows[index][:attrs].size
140
+ end
141
+
142
+ def attr_name(attr_index)
143
+ flat_attrs[attr_index][0]
144
+ end
145
+
146
+ def attr_value(attr_index)
147
+ flat_attrs[attr_index][1]
148
+ end
149
+
150
+ def attr_value_has_ws?(attr_index)
151
+ flat_attrs[attr_index][2]
152
+ end
153
+
154
+ def free; end
155
+
156
+ private
157
+
158
+ def flat_attrs
159
+ @flat_attrs ||= @rows.flat_map { |row| row[:attrs] }
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -1387,6 +1387,7 @@ module Moxml
1387
1387
  autoload :Serialize, "moxml/adapter/leptris/serialize"
1388
1388
  autoload :LeptrisSAXBridge, "moxml/adapter/leptris/sax_bridge"
1389
1389
  autoload :SaxRecordReplay, "moxml/adapter/leptris/sax_record_replay"
1390
+ autoload :ReaderRecordTable, "moxml/adapter/leptris/reader_record_table"
1390
1391
  extend Serialize
1391
1392
  extend DocumentParts
1392
1393
  extend Markers
@@ -2390,6 +2391,23 @@ module Moxml
2390
2391
  return real && namespace(real)
2391
2392
  end
2392
2393
 
2394
+ # XPath results mint ResultAttr (parent-backed): the same
2395
+ # prefix-against-owner contract as the Attr branch below,
2396
+ # anchored on the parent element — ResultAttr carries no
2397
+ # namespace/element faces of its own (#582 bug 1: the Attr
2398
+ # branch's node.namespace crashed it).
2399
+ if node.is_a?(::Leptris::XML::ResultAttr)
2400
+ if (prefix = prefix_part(node.name))
2401
+ resolved = resolve_prefix_ns(node.parent, prefix)
2402
+ return resolved if resolved
2403
+ return implicit_xml_namespace(node) if prefix == "xml"
2404
+
2405
+ return nil
2406
+ end
2407
+
2408
+ return nil
2409
+ end
2410
+
2393
2411
  # The binding resolves Attr#namespace to the declaration but
2394
2412
  # without the prefix; when the qualified name carries one,
2395
2413
  # prefer the in-scope declaration that binds it so wrappers
@@ -2458,6 +2476,8 @@ module Moxml
2458
2476
  owner = if node.is_a?(::Leptris::XML::Attr) ||
2459
2477
  node.is_a?(SnapshotAttribute)
2460
2478
  node.element
2479
+ elsif node.is_a?(::Leptris::XML::ResultAttr)
2480
+ node.parent
2461
2481
  else
2462
2482
  node
2463
2483
  end
@@ -3283,6 +3303,12 @@ module Moxml
3283
3303
  def attribute_name(attr)
3284
3304
  return attr.name if attr.is_a?(SnapshotAttribute)
3285
3305
 
3306
+ # XPath-result ResultAttr: detached (no owner scope to
3307
+ # resolve against), so the qualified name IS the answer —
3308
+ # the prefix has no other carrier (#582 bug 1: to_s must
3309
+ # render `x:id="Y"` for //a/@x:id).
3310
+ return attr.name.to_s.dup.force_encoding("UTF-8") if attr.is_a?(::Leptris::XML::ResultAttr)
3311
+
3286
3312
  # leptris Attr names are qualified; the wrapper composes the
3287
3313
  # prefix, so expose the local part.
3288
3314
  return attr.name.split(":", 2)[1] if attr.name.include?(":")
@@ -3290,6 +3316,15 @@ module Moxml
3290
3316
  attr.name.to_s.dup.force_encoding("UTF-8")
3291
3317
  end
3292
3318
 
3319
+ # The native's own qualified spelling — the snapshot's
3320
+ # qualified_name, or the binding's already-qualified name
3321
+ # (#582 bug 2: the attributes surface's bracket lookup).
3322
+ def attribute_qualified_name(attr)
3323
+ return attr.qualified_name if attr.is_a?(SnapshotAttribute)
3324
+
3325
+ attr.name.to_s.dup.force_encoding("UTF-8")
3326
+ end
3327
+
3293
3328
  # Self-gating by method_defined? — older exts fall through to
3294
3329
  # the to_binding bridge below.
3295
3330
  SET_ATTRIBUTE_NATIVE =
@@ -4687,6 +4722,15 @@ module Moxml
4687
4722
  handler.on_error(Moxml::ParseError.new(e.message))
4688
4723
  end
4689
4724
 
4725
+ # The Reader.s streaming face (#582 gap 4): one uniform record
4726
+ # surface over the engine.s bulk drain table (leptris#1298) or
4727
+ # the callback stream for documents outside the drain subset
4728
+ # (nil table - e.g. declaration-bearing documents).
4729
+ def reader_records(source)
4730
+ xml_string = source.is_a?(IO) || source.is_a?(::StringIO) ? source.read : source.to_s
4731
+ ReaderRecordTable.open(xml_string)
4732
+ end
4733
+
4690
4734
  private
4691
4735
 
4692
4736
  # The variable-length \s* gap after \A defeats Onigmo's anchor
@@ -11,6 +11,15 @@ module Moxml
11
11
  @name ||= adapter.attribute_name(@native)
12
12
  end
13
13
 
14
+ # The namespace-qualified spelling of the attribute's name
15
+ # (#582 bug 2): the bracket lookup on the attributes surface
16
+ # matches Nokogiri's qualified keys — the native's own spelling
17
+ # (the snapshot's, the binding's), not a re-composition, so
18
+ # undeclared-prefix lenient documents still resolve.
19
+ def qualified_name
20
+ @qualified_name ||= adapter.attribute_qualified_name(@native)
21
+ end
22
+
14
23
  def name=(new_name)
15
24
  # Mutation return contract (Adapter::Base): returns the native
16
25
  # to keep tracking — same object for in-place adapters, fresh
@@ -120,10 +129,13 @@ module Moxml
120
129
  end
121
130
 
122
131
  def to_s
123
- if namespace&.prefix
124
- "#{namespace.prefix}:#{name}=\"#{value}\""
125
- else
132
+ # A qualified name renders as-is (detached xpath results carry
133
+ # the prefix in the name itself, #582 bug 1); otherwise the
134
+ # namespace composes the prefix.
135
+ if name.include?(":") || namespace&.prefix.nil?
126
136
  "#{name}=\"#{value}\""
137
+ else
138
+ "#{namespace.prefix}:#{name}=\"#{value}\""
127
139
  end
128
140
  end
129
141
 
data/lib/moxml/builder.rb CHANGED
@@ -7,17 +7,40 @@ module Moxml
7
7
  attr_reader :document
8
8
  alias_method :doc, :document
9
9
 
10
- def initialize(context, strict: nil)
11
- @context = context
12
- @current = @document = context.create_document
10
+ def initialize(context, strict: nil, fragment: false)
11
+ # A Document wrapper builds INTO it (#582 bug 3): the context
12
+ # rides the wrapper, the DSL attaches to the passed document.
13
+ # A Context mints a fresh document (the historical path).
14
+ if context.is_a?(Moxml::Context)
15
+ @context = context
16
+ @current = @document = context.create_document
17
+ else
18
+ @context = context.context
19
+ @current = @document = context
20
+ end
13
21
  @namespaces = {}
22
+ # Fragment mode (#584): multi-root builds serialize as
23
+ # concatenated markup. The markup-accumulating face flushes
24
+ # through a single-root document parse, and the document model
25
+ # guards one root — so fragments ride the tree path (face off)
26
+ # inside an invisible scaffold root; to_xml serializes the
27
+ # scaffold's children, which the scaffold never shows.
28
+ @fragment = fragment
29
+ @fragment_root = nil
14
30
  # Markup-accumulating face (leptris 1.9.313, #374 lever 2):
15
31
  # leaf DSL ops write markup into one String and a single
16
32
  # native crossing flushes the whole subtree at build end.
17
33
  # Tree-only ops (declaration/doctype/PI/entity/namespace)
18
34
  # flush first and run on the document; the face retires after
19
35
  # its first flush.
20
- @face = context.config.adapter.builder_face(@document.native)
36
+ if @fragment
37
+ @fragment_root = @document.create_element("moxml-fragment")
38
+ @document.root = @fragment_root
39
+ @current = @fragment_root
40
+ @face = nil
41
+ else
42
+ @face = @context.config.adapter.builder_face(@document.native)
43
+ end
21
44
  @face_stack = []
22
45
  @face_counts = Hash.new(0)
23
46
  @face_deferred = []
@@ -26,7 +49,7 @@ module Moxml
26
49
  # silently becoming empty elements. Per-build override
27
50
  # (Context#build(strict:)), else the context's
28
51
  # config.builder_strict.
29
- @strict = strict.nil? ? context.config.builder_strict : strict
52
+ @strict = strict.nil? ? @context.config.builder_strict : strict
30
53
  end
31
54
 
32
55
  def strict?
@@ -46,6 +69,22 @@ module Moxml
46
69
  @document
47
70
  end
48
71
 
72
+ # Fragment serialization (#584): every top-level node serialized
73
+ # and concatenated — no root, no declaration. Fragment mode
74
+ # only; document builds keep the document's own to_xml.
75
+ def to_xml
76
+ unless @fragment
77
+ raise Moxml::AdapterError.new(
78
+ "Builder#to_xml is the fragment-mode serialization; " \
79
+ "serialize the built document instead (builder.document.to_xml)",
80
+ adapter: @context.config.adapter.name,
81
+ operation: "serialize",
82
+ )
83
+ end
84
+
85
+ @fragment_root.children.map(&:to_xml).join
86
+ end
87
+
49
88
  # Namespace-scoped tag syntax (#371, Nokogiri's xml["p"].tag
50
89
  # parity): xml["w"].p creates <w:p>. The proxy creates through
51
90
  # the same face as plain method_missing — caller-context
@@ -137,6 +176,14 @@ module Moxml
137
176
  eval_block: true)
138
177
  end
139
178
 
179
+ # Nokogiri Builder parity: `n << "text"` inside an element block
180
+ # appends escaped text (the metanorma converter helpers use this
181
+ # shape through `node.send(name, **attrs) { |n| n << val }`).
182
+ def <<(content)
183
+ text(content)
184
+ self
185
+ end
186
+
140
187
  def text(content)
141
188
  return face_content { @face.text(content) } if @face
142
189
 
@@ -326,7 +373,16 @@ module Moxml
326
373
  end
327
374
 
328
375
  def run_builder_block(block, eval_block)
329
- eval_block ? instance_eval(&block) : block.call
376
+ eval_block ? instance_eval(&block) : yield_scope(block)
377
+ end
378
+
379
+ # #598: a one-arg element block receives the Builder itself with
380
+ # its position on the new element - the face stack (face path)
381
+ # and the @current switch (tree path) already place writes there,
382
+ # so `n << val` appends and `n.child` nests, the nokogiri
383
+ # NodeBuilder contract the metanorma converters build through.
384
+ def yield_scope(block)
385
+ block.arity.zero? ? block.call : block.call(self)
330
386
  end
331
387
 
332
388
  def face_escape(value)
@@ -398,7 +454,7 @@ block: nil, eval_block: true)
398
454
  previous = @current
399
455
  @current = el
400
456
  begin
401
- eval_block ? instance_eval(&block) : block.call
457
+ eval_block ? instance_eval(&block) : yield_scope(block)
402
458
  ensure
403
459
  @current = previous
404
460
  end
data/lib/moxml/element.rb CHANGED
@@ -172,14 +172,11 @@ module Moxml
172
172
  end
173
173
 
174
174
  def attributes
175
- # Primed like Node.wrap: the adapter and type are known at
176
- # mint, so the first name/value/attribute? access skips the
177
- # context hop and the adapter's type probe.
178
- @attributes ||= adapter.attributes(@native).map do |attr|
179
- a = Wrappers::Attribute.new(attr, context, adapter, :attribute)
180
- a.parent_node = self
181
- a
182
- end
175
+ # A NodeSet (not a bare Array) so the surface answers Nokogiri's
176
+ # hash-style lookup — `attributes["xmi:idref"]` (#582 bug 2).
177
+ # The RAW adapter output rides the lazy wrap path: Node.wrap
178
+ # mints properly-typed Attribute wrappers per item.
179
+ @attributes ||= NodeSet.new(adapter.attributes(@native), context, self)
183
180
  end
184
181
 
185
182
  # Nokogiri-compatible: delete(name) removes the attribute
@@ -12,6 +12,12 @@ module Moxml
12
12
  adapter.namespace_uri(@native)
13
13
  end
14
14
 
15
+ # Nokogiri parity (#582 gap 5): the migration idiom reads
16
+ # node.namespace&.href — same value as #uri.
17
+ def href
18
+ uri
19
+ end
20
+
15
21
  def ==(other)
16
22
  other.is_a?(Namespace) && prefix == other.prefix && uri == other.uri
17
23
  end
@@ -95,9 +95,24 @@ module Moxml
95
95
  wrapped_buffer[actual] ||= wrap_with_parent((@nodes || @lazy)[actual])
96
96
  when Range
97
97
  self.class.new(native_nodes[index], @context)
98
+ when String
99
+ # Nokogiri parity for the attributes surface (#582 bug 2):
100
+ # lookup by qualified name first, then by local name, so
101
+ # `node.attributes["xmi:idref"]` finds its attribute instead
102
+ # of tripping the Integer-only index.
103
+ find do |n|
104
+ n.name == index ||
105
+ (n.is_a?(Moxml::Attribute) && n.qualified_name == index)
106
+ end
98
107
  end
99
108
  end
100
109
 
110
+ # Array parity: the attributes surface used to be a bare Array,
111
+ # and consumers (c14n's path keys) call #index on it (#582).
112
+ def index(...)
113
+ to_a.index(...)
114
+ end
115
+
101
116
  def first(n = nil)
102
117
  if n.nil?
103
118
  native_size.zero? ? nil : self[0]
@@ -0,0 +1,234 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moxml
4
+ # Streaming pull reader (#582 gap 4): Nokogiri::XML::Reader parity
5
+ # for memory-conscious consumers — the walk visits start elements,
6
+ # text, and synthesized end markers without materializing the
7
+ # document tree. The stream rides the adapter's record face
8
+ # (`reader_records`); engines without one raise
9
+ # NotImplementedError (loud degradation, never a silent lane
10
+ # change).
11
+ #
12
+ # reader = Moxml::Reader.new(xml, :leptris)
13
+ # while reader.read
14
+ # if reader.node_type == Moxml::Reader::TYPE_ELEMENT &&
15
+ # reader.name == "book" && reader["id"] == "b1"
16
+ # subtree = reader.outer_xml
17
+ # end
18
+ # end
19
+ #
20
+ # Depth is the element nesting level (the root is 0). End markers
21
+ # are synthesized from the records' parent indices — the stream's
22
+ # pre-order with parents makes the close boundary exact.
23
+ # empty_element? answers for elements the stream carries no
24
+ # children for. outer_xml is reconstructed from the records (the
25
+ # entity spelling may differ from the source bytes).
26
+ class Reader
27
+ # Nokogiri-compatible node type values.
28
+ TYPE_ELEMENT = 1
29
+ TYPE_TEXT = 3
30
+ TYPE_END_ELEMENT = 15
31
+
32
+ attr_reader :node_type, :name, :value, :node_depth
33
+
34
+ def initialize(source, adapter_name = nil)
35
+ @context = adapter_name ? Context.new(adapter_name) : Context.new
36
+ @records = @context.config.adapter.reader_records(source)
37
+ @index = -1
38
+ @pending_end = nil
39
+ @current_depth = 0
40
+ @empty_element = false
41
+ @node_type = nil
42
+ @name = nil
43
+ @value = nil
44
+ @node_depth = 0
45
+ end
46
+
47
+ def read
48
+ if @pending_end
49
+ record_end(@pending_end)
50
+ @pending_end = nil
51
+ return self
52
+ end
53
+
54
+ @index += 1
55
+ return nil if @index >= @records.count
56
+
57
+ if @records.element?(@index)
58
+ record_element(@index)
59
+ # the element's last child closes it: chain the synthesized end
60
+ @pending_end = @index if last_child_boundary?(@index)
61
+ else
62
+ record_text(@index)
63
+ owner = @records.parent(@index)
64
+ @pending_end = owner if last_child_boundary?(owner) &&
65
+ !@records.element?(owner)
66
+ end
67
+ self
68
+ end
69
+
70
+ def attributes
71
+ pairs = {}
72
+ each_attribute do |attr_name, attr_value|
73
+ pairs[attr_name] = attr_value
74
+ end
75
+ pairs
76
+ end
77
+
78
+ def [](attr_name)
79
+ return nil unless @node_type == TYPE_ELEMENT
80
+
81
+ pairs = attributes
82
+ pairs[attr_name] || pairs[attr_name.to_s.split(":", 2)[1]]
83
+ end
84
+
85
+ def empty_element?
86
+ @empty_element
87
+ end
88
+
89
+ def element?
90
+ @node_type == TYPE_ELEMENT
91
+ end
92
+
93
+ def end_element?
94
+ @node_type == TYPE_END_ELEMENT
95
+ end
96
+
97
+ def text?
98
+ @node_type == TYPE_TEXT
99
+ end
100
+
101
+ # The current element's subtree, reconstructed from the stream.
102
+ # The entity spelling may differ from the source bytes.
103
+ def outer_xml
104
+ return nil unless @node_type == TYPE_ELEMENT
105
+
106
+ out = +""
107
+ serialize_subtree(@index, out)
108
+ out
109
+ end
110
+
111
+ private
112
+
113
+ def record_element(index)
114
+ @node_type = TYPE_ELEMENT
115
+ @name = @records.view(index)
116
+ @value = nil
117
+ @node_depth = record_depth(index)
118
+ @empty_element = !has_children?(index)
119
+ @current_depth = @node_depth
120
+ end
121
+
122
+ def record_text(index)
123
+ @node_type = TYPE_TEXT
124
+ @name = nil
125
+ @value = @records.view(index)
126
+ @node_depth = record_depth(@records.parent(index)) + 1
127
+ @empty_element = false
128
+ end
129
+
130
+ def record_end(index)
131
+ @node_type = TYPE_END_ELEMENT
132
+ @name = @records.view(index)
133
+ @value = nil
134
+ @node_depth = record_depth(index)
135
+ @empty_element = false
136
+ end
137
+
138
+ def record_depth(index)
139
+ depth = 0
140
+ walk = index
141
+ while (parent = @records.parent(walk))
142
+ depth += 1
143
+ walk = parent
144
+ end
145
+ depth
146
+ end
147
+
148
+ def has_children?(index)
149
+ child_scan(index) { return true }
150
+ false
151
+ end
152
+
153
+ def last_child_boundary?(index)
154
+ # the deepest open element's next record is outside the subtree
155
+ last = index
156
+ scan = index + 1
157
+ count = @records.count
158
+ while scan < count && @records.parent(scan) &&
159
+ inside_subtree?(index, scan)
160
+ last = scan
161
+ scan += 1
162
+ end
163
+ last == index
164
+ end
165
+
166
+ def inside_subtree?(root, index)
167
+ walk = index
168
+ while (parent = @records.parent(walk))
169
+ return true if parent == root
170
+
171
+ walk = parent
172
+ end
173
+ false
174
+ end
175
+
176
+ def each_attribute
177
+ k = @records.attr_first(@index)
178
+ last = k + @records.attr_count_of(@index)
179
+ while k < last
180
+ v = @records.attr_value(k)
181
+ value = @records.attr_value_has_ws?(k) ? v.tr("\t\n\r", " ") : v
182
+ yield @records.attr_name(k), value
183
+ k += 1
184
+ end
185
+ end
186
+
187
+ def child_scan(index)
188
+ count = @records.count
189
+ scan = index + 1
190
+ while scan < count
191
+ parent = @records.parent(scan)
192
+ break if parent.nil?
193
+ break unless inside_subtree?(index, scan)
194
+
195
+ yield scan if parent == index
196
+ scan += 1
197
+ end
198
+ end
199
+
200
+ def serialize_subtree(index, out)
201
+ if @records.element?(index)
202
+ out << "<#{@records.view(index)}"
203
+ each_attribute_on(index) do |name, value|
204
+ out << %( #{name}=") << escape(value, true) << ?"
205
+ end
206
+ if has_children?(index)
207
+ out << ">"
208
+ child_scan(index) { |child| serialize_subtree(child, out) }
209
+ out << "</#{@records.view(index)}>"
210
+ else
211
+ out << "/>"
212
+ end
213
+ else
214
+ out << escape(@records.view(index), false)
215
+ end
216
+ end
217
+
218
+ def each_attribute_on(index)
219
+ k = @records.attr_first(index)
220
+ last = k + @records.attr_count_of(index)
221
+ while k < last
222
+ v = @records.attr_value(k)
223
+ value = @records.attr_value_has_ws?(k) ? v.tr("\t\n\r", " ") : v
224
+ yield @records.attr_name(k), value
225
+ k += 1
226
+ end
227
+ end
228
+
229
+ def escape(text, attribute)
230
+ escaped = text.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
231
+ attribute ? escaped.gsub('"', "&quot;") : escaped
232
+ end
233
+ end
234
+ end
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.19"
4
+ VERSION = "0.6.21"
5
5
  end
data/lib/moxml.rb CHANGED
@@ -12,6 +12,20 @@ module Moxml
12
12
  Context.new(adapter).parse(xml, options)
13
13
  end
14
14
 
15
+ # Fragment builder (#584): several sibling top-level nodes
16
+ # serializing as standalone concatenated markup — no root, no
17
+ # declaration. The metanorma #noko shape.
18
+ #
19
+ # Moxml.fragment(:leptris) do
20
+ # element("p") { text("a") }
21
+ # element("p") { text("b") }
22
+ # end.to_xml # => "<p>a</p><p>b</p>"
23
+ def fragment(adapter = nil, &block)
24
+ builder = Builder.new(Context.new(adapter), fragment: true)
25
+ builder.build(&block) if block
26
+ builder
27
+ end
28
+
15
29
  def configure
16
30
  yield Config.default if block_given?
17
31
  end
@@ -71,6 +85,7 @@ module Moxml
71
85
  autoload :NodeSet, "moxml/node_set"
72
86
  autoload :LazyNodeSet, "moxml/lazy_node_set"
73
87
  autoload :LazyBulkRows, "moxml/lazy_bulk_rows"
88
+ autoload :Reader, "moxml/reader"
74
89
  autoload :Document, "moxml/document"
75
90
  autoload :Element, "moxml/element"
76
91
  autoload :Text, "moxml/text"
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml/moxml#582 — the ea gem's Nokogiri→Moxml migration findings:
6
+ # the xpath-result attribute crash, the namespaced-attribute surface,
7
+ # and Builder construction from a document wrapper.
8
+ RSpec.describe "leptris ea-migration findings (#582)" do
9
+ let(:ctx) { Moxml.new(:leptris) }
10
+
11
+ describe "bug 1: xpath-result attribute to_s" do
12
+ it "renders an attribute returned by at_xpath without crashing" do
13
+ doc = ctx.parse('<r><a id="X"/></r>')
14
+ attr = doc.at_xpath("//a/@id")
15
+
16
+ expect(attr.to_s).to eq('id="X"')
17
+ end
18
+
19
+ it "renders a prefixed xpath-result attribute with its prefix" do
20
+ doc = ctx.parse('<r xmlns:x="urn:x"><a x:id="Y"/></r>')
21
+ attr = doc.at_xpath("//a/@x:id")
22
+
23
+ expect(attr.to_s).to eq('x:id="Y"')
24
+ end
25
+ end
26
+
27
+ describe "bug 2: namespaced attribute surface" do
28
+ it "answers string bracket access with the named attribute" do
29
+ doc = ctx.parse('<a id="1" xmi:id="X1" xmi:idref="R9"/>')
30
+
31
+ expect(doc.root.attributes["xmi:idref"].value).to eq("R9")
32
+ expect(doc.root.attributes["id"].value).to eq("1")
33
+ end
34
+
35
+ it "answers string bracket access on both adapter lanes identically" do
36
+ %i[leptris nokogiri].each do |engine|
37
+ doc = Moxml.new(engine).parse('<a id="1" xmi:id="X1" xmi:idref="R9"/>')
38
+
39
+ expect(doc.root.attributes["xmi:idref"].value).to eq("R9")
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "gap 5: namespace data at node level" do
45
+ it "answers the Nokogiri href idiom on the leptris lane" do
46
+ doc = ctx.parse('<r xmlns:x="urn:x"><a x:id="Y"/></r>')
47
+ child = doc.root.children.first
48
+
49
+ expect(child.namespace&.href).to be_nil # <a> is unprefixed
50
+ expect(child.attribute("x:id").namespace&.href).to eq("urn:x")
51
+ end
52
+ end
53
+
54
+ describe "bug 3: Builder from a document wrapper" do
55
+ it "builds into a passed document wrapper" do
56
+ doc = ctx.create_document
57
+ builder = Moxml::Builder.new(doc)
58
+ builder.build do
59
+ element("root") do
60
+ element("child")
61
+ end
62
+ end
63
+ result = doc.to_xml
64
+
65
+ expect(result).to include("<root")
66
+ expect(result).to include("child")
67
+ expect(doc.root.name).to eq("root")
68
+ expect(doc.root.children.first.name).to eq("child")
69
+ end
70
+
71
+ it "keeps the context-first construction path working" do
72
+ builder = Moxml::Builder.new(ctx)
73
+ doc = builder.build { element("solo") }
74
+
75
+ expect(doc.root.name).to eq("solo")
76
+ end
77
+ end
78
+ end
@@ -242,7 +242,17 @@ RSpec.describe Moxml::Builder do
242
242
  expect(builder.respond_to?(:to_ary)).to be false
243
243
  expect(builder.respond_to?(:to_hash)).to be false
244
244
  expect(builder.respond_to?(:to_str)).to be false
245
- expect(builder.respond_to?(:to_xml)).to be false
245
+ end
246
+
247
+ # #584: to_xml is a REAL method now (the fragment serialization;
248
+ # nokogiri-builder API parity for #noko) — it cannot silently
249
+ # become a tag, which is the #371 law's intent. The
250
+ # method_missing guard for the reserved pattern stays: an
251
+ # unknown tag never materializes under a reserved name.
252
+ it "answers to_xml as the real serialization, never a tag" do
253
+ builder = described_class.new(context)
254
+ expect(builder.respond_to?(:to_xml)).to be true
255
+ expect { builder.create_tag("to_xml") }.not_to raise_error
246
256
  end
247
257
  end
248
258
 
@@ -308,4 +318,16 @@ RSpec.describe Moxml::Builder do
308
318
  expect(builder.document.root.name).to eq("root")
309
319
  end
310
320
  end
321
+
322
+ describe "Builder#<<" do
323
+ it "appends escaped text inside an element block" do
324
+ builder = Moxml.fragment(:leptris)
325
+ builder.build do
326
+ element("p") do |n|
327
+ n << "A & B"
328
+ end
329
+ end
330
+ expect(builder.to_xml).to eq("<p>A &amp; B</p>")
331
+ end
332
+ end
311
333
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml/moxml#598: element blocks yield nil — nokogiri's Builder
6
+ # yields a scope bound to the created element, and the metanorma
7
+ # converter suite's 82+ call sites build through exactly that shape
8
+ # (`node.send(name, **attrs) { |n| n << val }`). The yield hands the
9
+ # Builder itself with its position on the new element (the face
10
+ # stack and the tree-path @current switch already place writes there
11
+ # for the zero-arg style), so `n <<` appends and `n.child` nests.
12
+ RSpec.describe "the yielded element scope (#598)" do
13
+ describe "fragment mode (tree path)" do
14
+ it "yields a scope whose << appends into the element" do
15
+ out = Moxml.fragment(:leptris) do |xml|
16
+ xml.title { |n| n << "T" }
17
+ end.to_xml
18
+
19
+ expect(out).to eq("<title>T</title>")
20
+ end
21
+
22
+ it "nests one-arg scopes" do
23
+ out = Moxml.fragment(:leptris) do |xml|
24
+ xml.p { |n| n.em { |e| e << "deep" } }
25
+ end.to_xml
26
+
27
+ expect(out).to eq("<p><em>deep</em></p>")
28
+ end
29
+
30
+ it "keeps the zero-arg style working" do
31
+ out = Moxml.fragment(:leptris) do |xml|
32
+ xml.p { text("plain") }
33
+ end.to_xml
34
+
35
+ expect(out).to eq("<p>plain</p>")
36
+ end
37
+ end
38
+
39
+ describe "document mode (face path)" do
40
+ it "yields a scope whose << appends into the element" do
41
+ ctx = Moxml.new(:leptris)
42
+ builder = Moxml::Builder.new(ctx)
43
+ doc = builder.build do |xml|
44
+ xml.root { |_n| xml.title { |n2| n2 << "T" } }
45
+ end
46
+
47
+ expect(doc.to_xml).to include("<title>T</title>")
48
+ end
49
+
50
+ it "nests one-arg scopes through the face" do
51
+ ctx = Moxml.new(:leptris)
52
+ builder = Moxml::Builder.new(ctx)
53
+ doc = builder.build do |xml|
54
+ xml.root { |_n| xml.p { |n2| n2.em { |e| e << "deep" } } }
55
+ end
56
+
57
+ # the face path pretty-prints - the structure is the contract
58
+ expect(doc.to_xml).to include("<p>")
59
+ expect(doc.to_xml).to include("<em>deep</em>")
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml/moxml#584: fragment / multi-root builder mode — several
6
+ # sibling top-level elements serializing as standalone concatenated
7
+ # markup, no root, no declaration (the metanorma stack's #noko
8
+ # shape, 73k calls per compile).
9
+ RSpec.describe "Moxml.fragment" do
10
+ let(:ctx) { Moxml.new(:leptris) }
11
+
12
+ it "serializes sibling elements concatenated, no root or declaration" do
13
+ builder = Moxml.fragment(:leptris)
14
+ builder.build do
15
+ element("p") { text("a") }
16
+ element("p") { text("b") }
17
+ end
18
+
19
+ expect(builder.to_xml).to eq("<p>a</p><p>b</p>")
20
+ end
21
+
22
+ it "carries attributes and nesting byte-exactly" do
23
+ builder = Moxml.fragment(:leptris)
24
+ builder.build do
25
+ element("p", id: "1") do
26
+ element("em") { text("deep") }
27
+ end
28
+ text("tail")
29
+ end
30
+
31
+ expect(builder.to_xml).to eq(%(<p id="1"><em>deep</em></p>tail))
32
+ end
33
+
34
+ it "takes the block at construction" do
35
+ out = Moxml.fragment(:leptris) do
36
+ element("x")
37
+ end
38
+
39
+ # the empty-element form is the lane serialization (self-closed
40
+ # comes from the face path only) - the fragment contract is the
41
+ # concatenated structure
42
+ expect(out.to_xml).to eq("<x></x>")
43
+ end
44
+
45
+ it "answers empty fragments with an empty string" do
46
+ builder = Moxml.fragment(:leptris)
47
+
48
+ expect(builder.to_xml).to eq("")
49
+ end
50
+
51
+ it "mixes element and text top-level content (mixed fragment)" do
52
+ builder = Moxml.fragment(:leptris)
53
+ builder.build do
54
+ text("lead")
55
+ element("span") { text("mid") }
56
+ end
57
+
58
+ expect(builder.to_xml).to eq("lead<span>mid</span>")
59
+ end
60
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml/moxml#582 gap 4: the streaming Reader — Nokogiri::XML::Reader
6
+ # parity for memory-conscious consumers (ea's large-XMI extractors):
7
+ # a pull walk over the adapter's streaming face with node types,
8
+ # names, values, attributes, depth, empty-element detection, and the
9
+ # subtree serialization. Engines without a streaming face raise
10
+ # loudly (the loud-degradation clause).
11
+ RSpec.describe Moxml::Reader do
12
+ let(:xml) do
13
+ <<~XML
14
+ <?xml version="1.0"?>
15
+ <catalog>
16
+ <book id="b1" lang="en">
17
+ <title>A title</title>
18
+ <meta empty="yes"/>
19
+ </book>
20
+ <book id="b2"/>
21
+ </catalog>
22
+ XML
23
+ end
24
+
25
+ describe "the pull walk" do
26
+ it "visits elements, text, and end markers in document order" do
27
+ reader = described_class.new(xml, :leptris)
28
+ visits = []
29
+ while reader.read
30
+ visits << [reader.node_type, reader.name, reader.value, reader.node_depth]
31
+ end
32
+
33
+ visits.map { |_v| described_class::TYPE_ELEMENT }
34
+ expect(visits.first).to eq([described_class::TYPE_ELEMENT, "catalog", nil, 0])
35
+
36
+ # the second book's start element carries its attributes
37
+ book2 = visits.find { |v| v[1] == "book" && v[3] == 1 && v != visits[1] }
38
+ expect(book2).not_to be_nil
39
+ end
40
+
41
+ it "exposes attributes on start elements" do
42
+ reader = described_class.new(xml, :leptris)
43
+ found = nil
44
+ while reader.read
45
+ if reader.node_type == described_class::TYPE_ELEMENT &&
46
+ reader.name == "book" && reader["id"] == "b1"
47
+ found = reader.attributes
48
+ break
49
+ end
50
+ end
51
+
52
+ expect(found).to eq({ "id" => "b1", "lang" => "en" })
53
+ end
54
+
55
+ it "surfaces text values" do
56
+ reader = described_class.new(xml, :leptris)
57
+ title_text = nil
58
+ while reader.read
59
+ if reader.node_type == described_class::TYPE_TEXT &&
60
+ reader.value&.include?("A title")
61
+ title_text = reader.value
62
+ end
63
+ end
64
+
65
+ expect(title_text).to include("A title")
66
+ end
67
+
68
+ it "flags empty elements" do
69
+ reader = described_class.new(xml, :leptris)
70
+ empties = []
71
+ while reader.read
72
+ empties << reader.name if reader.empty_element?
73
+ end
74
+
75
+ expect(empties).to include("meta")
76
+ expect(empties).to include("book")
77
+ end
78
+
79
+ it "serializes the current subtree with outer_xml" do
80
+ reader = described_class.new(xml, :leptris)
81
+ snippet = nil
82
+ while reader.read
83
+ if reader.node_type == described_class::TYPE_ELEMENT &&
84
+ reader.name == "book" && reader["id"] == "b1"
85
+ snippet = reader.outer_xml
86
+ break
87
+ end
88
+ end
89
+
90
+ expect(snippet).to include("<book")
91
+ expect(snippet).to include('id="b1"')
92
+ expect(snippet).to include("A title")
93
+ expect(snippet).to include("</book>")
94
+ end
95
+
96
+ it "ends with read returning nil" do
97
+ reader = described_class.new(xml, :leptris)
98
+ count = 0
99
+ count += 1 while reader.read
100
+
101
+ expect(count).to be_positive
102
+ expect(reader.read).to be_nil
103
+ end
104
+ end
105
+
106
+ describe "engine gating" do
107
+ it "raises loudly on engines without a streaming face" do
108
+ expect { described_class.new(xml, :ox) }
109
+ .to raise_error(Moxml::NotImplementedError, /streaming|Reader/i)
110
+ end
111
+ end
112
+ 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.19
4
+ version: 0.6.21
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-26 00:00:00.000000000 Z
11
+ date: 2026-11-27 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
@@ -170,6 +170,7 @@ files:
170
170
  - lib/moxml/adapter/leptris/document_parts.rb
171
171
  - lib/moxml/adapter/leptris/markers.rb
172
172
  - lib/moxml/adapter/leptris/materialize.rb
173
+ - lib/moxml/adapter/leptris/reader_record_table.rb
173
174
  - lib/moxml/adapter/leptris/sax_bridge.rb
174
175
  - lib/moxml/adapter/leptris/sax_record_replay.rb
175
176
  - lib/moxml/adapter/leptris/serialize.rb
@@ -232,6 +233,7 @@ files:
232
233
  - lib/moxml/node_set.rb
233
234
  - lib/moxml/plan.rb
234
235
  - lib/moxml/processing_instruction.rb
236
+ - lib/moxml/reader.rb
235
237
  - lib/moxml/reference.rb
236
238
  - lib/moxml/reference/normalizer.rb
237
239
  - lib/moxml/reference/parser.rb
@@ -494,6 +496,7 @@ files:
494
496
  - spec/moxml/adapter/leptris_doc_parts_spec.rb
495
497
  - spec/moxml/adapter/leptris_document_children_older_binding_spec.rb
496
498
  - spec/moxml/adapter/leptris_document_declaration_spec.rb
499
+ - spec/moxml/adapter/leptris_ea_migration_spec.rb
497
500
  - spec/moxml/adapter/leptris_inbody_comment_spec.rb
498
501
  - spec/moxml/adapter/leptris_native_parent_axis_spec.rb
499
502
  - spec/moxml/adapter/leptris_parse_lint_spec.rb
@@ -526,6 +529,7 @@ files:
526
529
  - spec/moxml/attribute_spec.rb
527
530
  - spec/moxml/builder_namespaces_spec.rb
528
531
  - spec/moxml/builder_spec.rb
532
+ - spec/moxml/builder_yield_spec.rb
529
533
  - spec/moxml/c14n/adapter_delegation_spec.rb
530
534
  - spec/moxml/c14n/api_spec.rb
531
535
  - spec/moxml/c14n/c14n_spec.rb
@@ -562,6 +566,7 @@ files:
562
566
  - spec/moxml/entity_registry_spec.rb
563
567
  - spec/moxml/entity_spec.rb
564
568
  - spec/moxml/error_spec.rb
569
+ - spec/moxml/fragment_spec.rb
565
570
  - spec/moxml/html_fragment_spec.rb
566
571
  - spec/moxml/lazy_parse_spec.rb
567
572
  - spec/moxml/materialize_namespace_uri_spec.rb
@@ -580,6 +585,7 @@ files:
580
585
  - spec/moxml/plan_conformance_spec.rb
581
586
  - spec/moxml/plan_spec.rb
582
587
  - spec/moxml/processing_instruction_spec.rb
588
+ - spec/moxml/reader_spec.rb
583
589
  - spec/moxml/readonly_parse_spec.rb
584
590
  - spec/moxml/recover_mode_spec.rb
585
591
  - spec/moxml/reference_spec.rb