moxml 0.5.8 → 0.5.9

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: 77a959afbe83b0cc46a158571f792d07888f7bf29f3a65c1ea88be867892ffcc
4
- data.tar.gz: 1a5a9515ba125977cd26e33b878e7ecaaed1a788e450e79f356c54a117c9e6c7
3
+ metadata.gz: 011d9a8566b2697d8c01a17a1a4f702ace315361dae3b246f8ed5ad5fb79b3e8
4
+ data.tar.gz: 286839c9aca15e4027d4b01bcdfb432c2036f10234fb408cf10f88e5dcbeac0d
5
5
  SHA512:
6
- metadata.gz: ff6e651e53a55b5ed4aed1e68eff9fa40e9a58aed81a3709ce101fb315bddbef928dcd1a8b31317fcf7dd42e65b92158191647365bc7c43fba46677551bd82d6
7
- data.tar.gz: 3c3ad8fac372d42a2dd9d3e4660bc30987015975031fed6b80c1bfeea864f47ed36676d125132384b72d7ba51a3a1ef743877efd5611fd0ecc2cda96efae29e7
6
+ metadata.gz: f53698ba5eee597fb6bda3355c1f7dfe0a33442cd3e67a6aedc73a155a23e80f049f2fc63aed6871b6cb2e85723719d7e7f318cc140ef178e5dde4bcd493a095
7
+ data.tar.gz: 44d1afe3880ebc55dd214a0a7f2b48c69151504b6c5a16646afd88cf4a885184132db9a590552c2531bfda8ea84a92e8fac50a7cfae75379e215582acf0f5bac
@@ -30,7 +30,9 @@ module Moxml
30
30
 
31
31
  def parse(xml, options = {}, _context = nil)
32
32
  xml_string = xml.is_a?(IO) || xml.is_a?(StringIO) ? xml.read : xml.to_s
33
- processed = preprocess_entities(xml_string)
33
+ # The marker flag rides preprocess's own `&` scan — no
34
+ # second full-buffer probe (issue #132 parse-side note).
35
+ processed, entity_markers = Entity.preprocess_with_marker_flag(xml_string)
34
36
 
35
37
  # readonly: true (issue #133): the binding memoizes reads and
36
38
  # refuses mutations — the parse-and-read lifecycle for
@@ -49,7 +51,7 @@ module Moxml
49
51
  doc = Document.new(native_doc, ctx)
50
52
 
51
53
  record_source_declaration(native_doc, processed)
52
- attachments.set(native_doc, :entity_markers, processed.include?(Entity::MARKER))
54
+ attachments.set(native_doc, :entity_markers, entity_markers)
53
55
 
54
56
  doc
55
57
  end
@@ -310,17 +312,21 @@ module Moxml
310
312
  end
311
313
 
312
314
  def node_type(node)
315
+ # Frequency-ordered: elements and text dominate every real
316
+ # document, and Node.wrap dispatches here once per cold
317
+ # wrap. CDATA must precede Text (CDATA < Text in the
318
+ # binding).
313
319
  case node
320
+ when ::Leptris::XML::Element then :element
321
+ when ::Leptris::XML::CDATA then :cdata
322
+ when ::Leptris::XML::Text, CustomizedLeptris::TextSegment then :text
323
+ when ::Leptris::XML::Attr then :attribute
324
+ when ::Leptris::XML::Comment then :comment
325
+ when ::Leptris::XML::ProcessingInstruction, CustomizedLeptris::DocumentPI then :processing_instruction
314
326
  when ::Leptris::XML::Document then :document
315
327
  when ::Leptris::XML::DocType, CustomizedLeptris::Doctype then :doctype
316
328
  when CustomizedLeptris::Declaration then :declaration
317
329
  when CustomizedLeptris::EntityReference then :entity_reference
318
- when ::Leptris::XML::CDATA then :cdata
319
- when ::Leptris::XML::Comment then :comment
320
- when ::Leptris::XML::ProcessingInstruction, CustomizedLeptris::DocumentPI then :processing_instruction
321
- when ::Leptris::XML::Text, CustomizedLeptris::TextSegment then :text
322
- when ::Leptris::XML::Element then :element
323
- when ::Leptris::XML::Attr then :attribute
324
330
  else :unknown
325
331
  end
326
332
  end
data/lib/moxml/entity.rb CHANGED
@@ -55,9 +55,12 @@ module Moxml
55
55
  module_function
56
56
 
57
57
  # Replace non-standard entity references with markers before
58
- # parsing. Always returns a UTF-8 encoded string.
59
- def preprocess_entities(xml)
60
- return "" if xml.nil?
58
+ # parsing. Always returns a UTF-8 encoded string, and reports
59
+ # whether the result can contain markers: the flag rides the
60
+ # same `&` scan, so callers needing it (leptris parse) avoid a
61
+ # second full-buffer multibyte probe.
62
+ def preprocess_with_marker_flag(xml)
63
+ return ["", false] if xml.nil?
61
64
 
62
65
  str = if xml.encoding == Encoding::BINARY
63
66
  # Binary strings are assumed to be UTF-8. If the bytes are
@@ -78,11 +81,23 @@ module Moxml
78
81
  # Fast path: no `&` means no entity references to mark — skip
79
82
  # the regex scan and string allocation entirely. The vast
80
83
  # majority of XML payloads contain no entity references.
81
- return str unless str.include?("&")
84
+ return [str, false] unless str.include?("&")
82
85
 
83
- str.gsub(NAME_RE) do |match|
84
- STANDARD_ENTITIES.include?(::Regexp.last_match(1)) ? match : "#{MARKER}#{::Regexp.last_match(1)};"
86
+ marked = false
87
+ processed = str.gsub(NAME_RE) do |match|
88
+ name = ::Regexp.last_match(1)
89
+ if STANDARD_ENTITIES.include?(name)
90
+ match
91
+ else
92
+ marked = true
93
+ "#{MARKER}#{name};"
94
+ end
85
95
  end
96
+ [processed, marked]
97
+ end
98
+
99
+ def preprocess_entities(xml)
100
+ preprocess_with_marker_flag(xml)[0]
86
101
  end
87
102
 
88
103
  # Resolve numeric (&#NN; / &#xNN;) and the five standard named
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.5.8"
4
+ VERSION = "0.5.9"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moxml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.5.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.