moxml 0.5.130 → 0.5.131

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: c2f099a606cb34b83d050cd7127593bc1403b3c2ae0d75f1d2d5afaad0107177
4
- data.tar.gz: 1d1d777052f784a68363fc7efee322aeb53cfea53781927df1c0e424a165327e
3
+ metadata.gz: 2de6081e549de3acc9fe0fe2697d0f44e137673eaa311dc9beec4de6346dfb54
4
+ data.tar.gz: 026a76efc77132825449057e48559626e3f3ec4166d54fb5ea817d51accb830e
5
5
  SHA512:
6
- metadata.gz: 28d300923fb7a5225aae5241ccbf3af90269ce73258ce1351260fb437df8a70f390a889fcccc98556202fcbcda15a0dbc2b1a9a8ab753e54f81d79a4132ab431
7
- data.tar.gz: f8085292e7b0bad93fc80075f1b1e6422021afb972dd11fd9088798b254cff3268068a7ae8786a29511440e7b716c6f48550671bebd772e34c6b509c96357f84
6
+ metadata.gz: 369b3b0ca0527711f87e4b18f9b268c503612c9aed6aa8ea4fcfcb6dd9ad835be5cbb80b9ff00b0606b3df908bb150a0ef22bba6041157bfdb41a0bd8264a153
7
+ data.tar.gz: a86491ce96b541420ddf36fc2cbe1324c2f283d212d0271cb2eb61129bca15258dd7d92dbe7a022c6e3e4055140fb395111c96e8518ebd5b5d882a036186ae33
data/Gemfile CHANGED
@@ -9,7 +9,7 @@ gemspec
9
9
  gem "benchmark"
10
10
  gem "benchmark-ips"
11
11
  gem "get_process_mem"
12
- gem "leptris", "~> 1.9.343"
12
+ gem "leptris", "~> 1.9.345"
13
13
  gem "libxml-ruby", ">= 5.0"
14
14
  gem "nokogiri", "~> 1.18"
15
15
  gem "openssl", "~> 3.0"
@@ -182,6 +182,18 @@ module Moxml
182
182
  defined?(::Leptris::HTML) &&
183
183
  ::Leptris::HTML.respond_to?(:parse_fragment)
184
184
 
185
+ # The engine's recovery mode (moxml#369, engine leptris#1660
186
+ # via binding 1.9.344.0): non-strict parses pass recover:
187
+ # true and keep the best-effort tree (libxml2
188
+ # XML_PARSE_RECOVER semantics — mismatched closers close
189
+ # intermediates, unclosed elements close at EOF) instead of
190
+ # the rescue-to-empty fallback. The kwarg probe pairs the
191
+ # floor (a binding without the kwarg cannot arm).
192
+ RECOVER_MODE =
193
+ Gem::Version.new(::Leptris::VERSION) >= Gem::Version.new("1.9.344.0") &&
194
+ ::Leptris::XML::ParseOptions.instance_method(:initialize)
195
+ .parameters.any? { |_, key| key == :recover }
196
+
185
197
  # Markup-accumulating builder face (leptris-ruby#374 lever 2,
186
198
  # binding 1.9.313): the DSL accumulates markup in a plain
187
199
  # String and ONE native crossing (parse + attach) flushes the
@@ -1031,23 +1043,45 @@ module Moxml
1031
1043
  # multi-pass consumers (comparison, diff, signature).
1032
1044
  # dtdattr: true opts into DTD ATTLIST default materialization
1033
1045
  # (off by default since libleptris 1.9.8, matching libxml2).
1046
+ # #369: non-strict parses RECOVER on engines that carry
1047
+ # the mode (1.9.344+) — the best-effort tree survives
1048
+ # recoverable structural errors (libxml2
1049
+ # XML_PARSE_RECOVER semantics). Strict callers never
1050
+ # recover: malformed input raises. Direct adapter calls
1051
+ # are caller-owned-options by contract (Context applies
1052
+ # config.strict_parsing; bypassing it means the caller
1053
+ # chose the mode).
1054
+ recover = !options[:strict] && RECOVER_MODE
1034
1055
  native_doc = begin
1035
1056
  ::Leptris::XML::Document.parse(
1036
1057
  processed,
1037
1058
  readonly: options[:readonly] == true,
1038
- options: parse_flags(options, context: _context),
1059
+ options: parse_flags(options, recover: recover,
1060
+ context: _context),
1039
1061
  )
1040
1062
  rescue ::Leptris::XML::ParseError => e
1041
- # libleptris has no recovery mode that survives unclosed
1042
- # tags; non-strict callers get an empty document, matching
1043
- # the Libxml adapter's non-strict behavior. The fatal
1044
- # error rides the document as parse diagnostics (issue
1045
- # #147) — otherwise nothing says why it came back empty.
1046
1063
  raise Moxml::ParseError.new(e.message) if options[:strict]
1047
1064
 
1065
+ # Engines below the 1.9.344 recover floor (or the
1066
+ # residual unrecoverable edges, e.g. trailing content
1067
+ # after the root close) have no best-effort tree: the
1068
+ # empty-document fallback stands, with the fatal error
1069
+ # as parse diagnostics (issue #147) — otherwise nothing
1070
+ # says why it came back empty.
1048
1071
  recover_errors = [e.message]
1049
1072
  create_document
1050
1073
  end
1074
+ if recover && native_doc.root.nil? && !recover_errors
1075
+ # The recover path answers a rootless document for
1076
+ # input with nothing to recover into (whitespace-only,
1077
+ # declaration-only) — libxml2's recover behaves the
1078
+ # same. Surface WHY on parse_errors so consumers report
1079
+ # it instead of silently holding an empty tree.
1080
+ recover_errors =
1081
+ ["leptris parse recovered no document root " \
1082
+ "(empty or whitespace-only input)"]
1083
+ end
1084
+
1051
1085
  register_mint_klasses!(native_doc)
1052
1086
  ctx = _context || Context.new(:leptris)
1053
1087
  doc = Wrappers::Document.new(native_doc, ctx)
@@ -1191,7 +1225,7 @@ module Moxml
1191
1225
  ::Leptris::XML::ParseOptions.const_defined?(:SKIP_DUP_DETECTION) &&
1192
1226
  ::Leptris::XML::ParseOptions.const_defined?(:SKIP_SOURCE_POSITIONS)
1193
1227
 
1194
- def parse_flags(options, context: nil)
1228
+ def parse_flags(options, recover: false, context: nil)
1195
1229
  flags = 0
1196
1230
  flags |= ::Leptris::XML::ParseOptions::DTDATTR if options[:dtdattr] == true
1197
1231
  flags |= ::Leptris::XML::ParseOptions::KEEP_ENTITY_REFS if NATIVE_ENTITY_REFS &&
@@ -1203,6 +1237,13 @@ module Moxml
1203
1237
  flags |= po::SKIP_DUP_DETECTION if options[:skip_dup_detection] == true
1204
1238
  flags |= po::SKIP_SOURCE_POSITIONS if options[:skip_source_positions] == true
1205
1239
  end
1240
+ if recover
1241
+ # #369: flags and the recover kwarg ride the same
1242
+ # options struct (leptris-ruby's constructor composes
1243
+ # both).
1244
+ return ::Leptris::XML::ParseOptions.new(flags, recover: true)
1245
+ end
1246
+
1206
1247
  flags.zero? ? nil : ::Leptris::XML::ParseOptions.new(flags)
1207
1248
  end
1208
1249
 
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.130"
4
+ VERSION = "0.5.131"
5
5
  end
@@ -595,11 +595,24 @@ RSpec.describe Moxml::Adapter::Leptris do
595
595
  describe "recover-path parse diagnostics" do
596
596
  let(:ctx) { Moxml.new(:leptris) }
597
597
 
598
- it "records the fatal error on non-strict parses" do
599
- doc = ctx.parse("<root><unclosed>", strict: false)
600
- expect(doc.root).to be_nil
601
- expect(doc.parse_errors).not_to be_empty
602
- expect(doc.parse_errors).to all(be_a(String))
598
+ it "records the fatal error on non-strict parses below the recover floor" do
599
+ unless Moxml::Adapter::Leptris::RECOVER_MODE
600
+ doc = ctx.parse("<root><unclosed>", strict: false)
601
+ expect(doc.root).to be_nil
602
+ expect(doc.parse_errors).not_to be_empty
603
+ expect(doc.parse_errors).to all(be_a(String))
604
+ end
605
+ end
606
+
607
+ it "recovers an unclosed root at the floor (#369)" do
608
+ if Moxml::Adapter::Leptris::RECOVER_MODE
609
+ doc = ctx.parse("<root><unclosed>", strict: false)
610
+ expect(doc.root).not_to be_nil
611
+ # recovery diagnostics ride parse_errors once leptris-ruby
612
+ # #1200's DOM face lands; today the recovered tree is the
613
+ # contract.
614
+ expect(doc.parse_errors).to eq([])
615
+ end
603
616
  end
604
617
 
605
618
  it "answers [] on clean parses" do
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # #369: non-strict parses RECOVER on engines carrying the mode
6
+ # (leptris 1.9.344+, leptris#1660) — the best-effort tree survives
7
+ # recoverable structural errors (libxml2 XML_PARSE_RECOVER
8
+ # semantics) instead of the rescue-to-empty fallback. Strict
9
+ # callers never recover: malformed input raises.
10
+ RSpec.describe "recover mode (#369)" do
11
+ let(:ctx) { Moxml.new(:leptris) }
12
+
13
+ describe "engines with the recover mode" do
14
+ before do
15
+ unless Moxml::Adapter::Leptris::RECOVER_MODE
16
+ skip "binding #{Leptris::VERSION} predates the recover mode"
17
+ end
18
+ end
19
+
20
+ it "recovers a mismatched closer into a tree" do
21
+ doc = ctx.parse("<root><a>x</b></root>", strict: false)
22
+ expect(doc.root).not_to be_nil
23
+ expect(doc.to_xml).to include("<a>x</a>")
24
+ end
25
+
26
+ it "closes unclosed elements at EOF" do
27
+ doc = ctx.parse("<root><a>x</root>", strict: false)
28
+ expect(doc.root).not_to be_nil
29
+ expect(doc.to_xml).to include("<a>x</a>")
30
+ end
31
+
32
+ it "keeps strict parsing raising" do
33
+ expect { ctx.parse("<root><a>x</b></root>", strict: true) }
34
+ .to raise_error(Moxml::ParseError)
35
+ end
36
+
37
+ it "raises by default (Context strict_parsing defaults true)" do
38
+ expect { ctx.parse("<root><a>x</b></root>") }
39
+ .to raise_error(Moxml::ParseError)
40
+ end
41
+
42
+ it "the recovered tree carries no parse errors (diagnostics ride #1200)" do
43
+ doc = ctx.parse("<root><a>x</b></root>", strict: false)
44
+ expect(doc.parse_errors).to eq([])
45
+ end
46
+ end
47
+
48
+ describe "engines below the recover floor" do
49
+ before do
50
+ if Moxml::Adapter::Leptris::RECOVER_MODE
51
+ skip "binding #{Leptris::VERSION} carries the recover mode"
52
+ end
53
+ end
54
+
55
+ it "keeps the empty-document fallback with diagnostics" do
56
+ doc = ctx.parse("<root><a>x</b></root>", strict: false)
57
+ expect(doc.root).to be_nil
58
+ expect(doc.parse_errors).not_to be_empty
59
+ end
60
+ end
61
+ 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.130
4
+ version: 0.5.131
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -531,6 +531,7 @@ files:
531
531
  - spec/moxml/plan_spec.rb
532
532
  - spec/moxml/processing_instruction_spec.rb
533
533
  - spec/moxml/readonly_parse_spec.rb
534
+ - spec/moxml/recover_mode_spec.rb
534
535
  - spec/moxml/sax/namespace_splitter_spec.rb
535
536
  - spec/moxml/sax_entity_parity_spec.rb
536
537
  - spec/moxml/sax_spec.rb