canon 0.3.21 → 0.3.23
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/CLAUDE.md +4 -0
- data/lib/canon/comparison/node_inspector.rb +4 -1
- data/lib/canon/comparison/yaml_comparator.rb +1 -1
- data/lib/canon/formatters/html_formatter_base.rb +39 -11
- data/lib/canon/formatters/yaml_formatter.rb +1 -1
- data/lib/canon/validators/yaml_validator.rb +1 -1
- data/lib/canon/version.rb +1 -1
- data/lib/canon/yaml_backend.rb +64 -0
- data/lib/canon/yaml_parsing.rb +36 -0
- data/lib/canon.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7e3fda4e37d9ca6cf584389c0ffec06c70811ec488910377151ed8ee29f20a35
|
|
4
|
+
data.tar.gz: cb73f6f95b16ce10b9ada9f13d1e91eaad0a4bce416d84ddfb49439227f270d5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 48a6dacc76c75c234a34583264f0cd3816c643bda7152ccbeee62cb4057fd370c70b168414bca515addc400de5ea12dacb82b9a356d35e4421596bff0109806b
|
|
7
|
+
data.tar.gz: a33a5b36fbd98f43d4832dbb275737726f5e9dc1786b91fc34313684a8e431724e3f169a426c18af3d90a15be1fb9a4970e561ef25d2f576ff52229cfebc1829
|
data/CLAUDE.md
CHANGED
|
@@ -143,6 +143,10 @@ Engine parity is complete through libleptris 1.9.8 / leptris-ruby 1.9.33 / moxml
|
|
|
143
143
|
|
|
144
144
|
Engine A/B testing: `CANON_XML_BACKEND=nokogiri bundle exec rspec` (or `=moxml` to force leptris when it isn't the resolved default). The default suite must stay green under BOTH values; the only expected pendings are the upstream-tracked ones. The benchmark header (`rake performance:quick`) reports the active engine.
|
|
145
145
|
|
|
146
|
+
### YAML Engines
|
|
147
|
+
|
|
148
|
+
`Canon::YamlBackend` selects the YAML engine: `:psych` (default) or `:yeptris` (FFI over libyeptris, the YAML counterpart of the leptris XML stack — `CANON_YAML_BACKEND=yeptris` opts in; the optional `yeptris` Gemfile group must be enabled). `Canon::YamlParsing` is the single gateway for string loads; `YAML.dump` stays on Psych everywhere — canonical output bytes are canon's product and the writers differ. The default stays `:psych` until the yeptris Psych-parity gaps close (yeptris-ruby#29 empty documents, #30 sexagesimal scalars, #31 >64-bit integers); `spec/canon/yaml_engine_parity_spec.rb` is the executable gate, with upstream-tracked cases pending. Never `require "yeptris/psych"` — it rebinds the global `::Psych` constant for the whole process; only the namespaced `Yeptris::YAML` API is used.
|
|
149
|
+
|
|
146
150
|
### Format Detection
|
|
147
151
|
|
|
148
152
|
`Canon::Comparison::FormatDetector` auto-detects format from string content or object type (Moxml::Node → XML, Nokogiri::HTML → HTML, Hash → JSON, etc.). HTML4 vs HTML5 is determined by DOCTYPE.
|
|
@@ -68,8 +68,11 @@ module Canon
|
|
|
68
68
|
def self.whitespace_only_text?(node)
|
|
69
69
|
return false unless text_node?(node)
|
|
70
70
|
|
|
71
|
+
# This runs per child pair in the realignment walk — the
|
|
72
|
+
# zero-allocation form of `text.strip.empty?` (exactly
|
|
73
|
+
# String#strip's character set; see WhitespacePolicy).
|
|
71
74
|
text = text_content(node)
|
|
72
|
-
!text.empty? && text.
|
|
75
|
+
!text.empty? && text.match?(Canon::Xml::WhitespacePolicy::STRIP_ONLY)
|
|
73
76
|
end
|
|
74
77
|
|
|
75
78
|
# --- Noise classification ---
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "nokogiri" unless RUBY_ENGINE == "opal"
|
|
4
|
+
require "set"
|
|
4
5
|
|
|
5
6
|
module Canon
|
|
6
7
|
module Formatters
|
|
@@ -55,6 +56,14 @@ module Canon
|
|
|
55
56
|
WHITESPACE_SENSITIVE_ELEMENTS = %w[
|
|
56
57
|
pre code textarea script style
|
|
57
58
|
].freeze
|
|
59
|
+
# Set form for the per-sibling hot lookup; Nokogiri lowercases
|
|
60
|
+
# HTML element names, so the downcase fallback only runs for
|
|
61
|
+
# unusual (already-mixed-case) input.
|
|
62
|
+
BLOCK_ELEMENT_SET = BLOCK_ELEMENTS.to_set
|
|
63
|
+
# Compiled once — building the alternation and compiling the
|
|
64
|
+
# regex per format call cost more than the gsub it drives.
|
|
65
|
+
BLOCK_SPACING_PATTERN =
|
|
66
|
+
Regexp.new("(</(?:#{BLOCK_ELEMENTS.join('|')})>)(<(?:#{BLOCK_ELEMENTS.join('|')})[\s>])").freeze
|
|
58
67
|
# Format HTML using canonical form
|
|
59
68
|
# @param html [String] HTML document to canonicalize
|
|
60
69
|
# @return [String] Canonical form of HTML
|
|
@@ -100,6 +109,15 @@ module Canon
|
|
|
100
109
|
next unless node.element?
|
|
101
110
|
next if node.attributes.empty?
|
|
102
111
|
|
|
112
|
+
names = node.attributes.keys
|
|
113
|
+
# Already-sorted un-namespaced is the common case — removing
|
|
114
|
+
# and re-adding every attribute is expensive, so check first.
|
|
115
|
+
# Namespaced attributes must take the slow path: the
|
|
116
|
+
# remove/re-add below flattens their prefix, and skipping
|
|
117
|
+
# would change the canonical output.
|
|
118
|
+
next if names.each_cons(2).all? { |a, b| (a <=> b) <= 0 } &&
|
|
119
|
+
node.attributes.each_value.all? { |a| a.namespace.nil? }
|
|
120
|
+
|
|
103
121
|
sorted_attrs = node.attributes.sort_by { |name, _| name }
|
|
104
122
|
node.attributes.each_key { |name| node.remove_attribute(name) }
|
|
105
123
|
sorted_attrs.each { |name, attr| node[name] = attr.value }
|
|
@@ -122,7 +140,7 @@ module Canon
|
|
|
122
140
|
end
|
|
123
141
|
|
|
124
142
|
# Handle whitespace-only text nodes
|
|
125
|
-
if node.text.
|
|
143
|
+
if node.text.match?(Canon::Xml::WhitespacePolicy::STRIP_ONLY) && node.parent&.element?
|
|
126
144
|
# Check if this text node is between block-level elements
|
|
127
145
|
prev_sibling = node.previous_sibling
|
|
128
146
|
next_sibling = node.next_sibling
|
|
@@ -138,14 +156,17 @@ module Canon
|
|
|
138
156
|
else
|
|
139
157
|
# Collapse multiple whitespace characters into single spaces
|
|
140
158
|
# but preserve leading/trailing single spaces for inline content
|
|
141
|
-
|
|
159
|
+
text = node.text
|
|
160
|
+
normalized = text.gsub(/\s+/, " ")
|
|
142
161
|
# Only strip if the entire parent chain suggests it's appropriate
|
|
143
162
|
# (e.g., at document boundaries)
|
|
144
163
|
if node.parent&.name == "body" &&
|
|
145
164
|
(node.previous_sibling.nil? || node.next_sibling.nil?)
|
|
146
165
|
normalized = normalized.strip
|
|
147
166
|
end
|
|
148
|
-
node.content
|
|
167
|
+
# node.content= re-parses the string — skip it when nothing
|
|
168
|
+
# changed (text with no collapsible whitespace).
|
|
169
|
+
node.content = normalized unless normalized == text
|
|
149
170
|
end
|
|
150
171
|
end
|
|
151
172
|
end
|
|
@@ -154,19 +175,20 @@ module Canon
|
|
|
154
175
|
# @param html [String] Serialized HTML string
|
|
155
176
|
# @return [String] HTML with proper spacing between block elements
|
|
156
177
|
def self.ensure_block_element_spacing(html)
|
|
157
|
-
# Build regex pattern for block element tags
|
|
158
|
-
block_tags = BLOCK_ELEMENTS.join("|")
|
|
159
|
-
|
|
160
178
|
# Add space between closing and opening block element tags
|
|
161
|
-
#
|
|
162
|
-
html.gsub(
|
|
179
|
+
# (pattern compiled once — see BLOCK_SPACING_PATTERN)
|
|
180
|
+
html.gsub(BLOCK_SPACING_PATTERN, '\1 \2')
|
|
163
181
|
end
|
|
164
182
|
|
|
165
183
|
# Check if a node is a block-level element
|
|
166
184
|
# @param node [Nokogiri::XML::Node, nil] Node to check
|
|
167
185
|
# @return [Boolean] true if node is a block element
|
|
168
186
|
def self.block_element?(node)
|
|
169
|
-
node&.element?
|
|
187
|
+
return false unless node&.element?
|
|
188
|
+
|
|
189
|
+
name = node.name
|
|
190
|
+
BLOCK_ELEMENT_SET.include?(name) ||
|
|
191
|
+
BLOCK_ELEMENT_SET.include?(name.downcase)
|
|
170
192
|
end
|
|
171
193
|
|
|
172
194
|
# Check if a node is a whitespace-sensitive element
|
|
@@ -178,8 +200,14 @@ module Canon
|
|
|
178
200
|
# Check if this element or any ancestor is whitespace-sensitive
|
|
179
201
|
current = node
|
|
180
202
|
while current
|
|
181
|
-
if current.element?
|
|
182
|
-
|
|
203
|
+
if current.element?
|
|
204
|
+
name = current.name
|
|
205
|
+
# Nokogiri lowercases HTML names — the downcase fallback
|
|
206
|
+
# only allocates for unusual mixed-case input.
|
|
207
|
+
if WHITESPACE_SENSITIVE_ELEMENTS.include?(name) ||
|
|
208
|
+
WHITESPACE_SENSITIVE_ELEMENTS.include?(name.downcase)
|
|
209
|
+
return true
|
|
210
|
+
end
|
|
183
211
|
end
|
|
184
212
|
# Stop at document root - documents don't have parents
|
|
185
213
|
break if current.is_a?(Nokogiri::XML::Document) || current.is_a?(Nokogiri::HTML5::Document)
|
|
@@ -22,7 +22,7 @@ module Canon
|
|
|
22
22
|
return if input.is_a?(Hash) || input.is_a?(Array) # Already parsed
|
|
23
23
|
return if input.strip.empty?
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
Canon::YamlParsing.safe_load(input)
|
|
26
26
|
rescue Psych::SyntaxError => e
|
|
27
27
|
location = extract_location(e)
|
|
28
28
|
|
data/lib/canon/version.rb
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Canon
|
|
4
|
+
# Selection of the YAML engine: :psych (stdlib) or :yeptris
|
|
5
|
+
# (FFI over libyeptris — the YAML counterpart of the leptris XML
|
|
6
|
+
# stack).
|
|
7
|
+
#
|
|
8
|
+
# Mirrors XmlBackend's discipline (MECE — this module owns selection,
|
|
9
|
+
# YamlParsing owns the calls). The default stays :psych until the
|
|
10
|
+
# yeptris Psych-safe_load parity gaps close (yeptris-ruby#29 empty
|
|
11
|
+
# documents crash, #30 sexagesimal scalars, #31 >64-bit integers);
|
|
12
|
+
# yeptris is 3.1x faster at loading (measured, 2,000-item document),
|
|
13
|
+
# so CANON_YAML_BACKEND=yeptris opts in early and the default flips
|
|
14
|
+
# once the parity spec runs clean.
|
|
15
|
+
#
|
|
16
|
+
# Only the namespaced API (Yeptris::YAML) is ever used — requiring
|
|
17
|
+
# "yeptris/psych" rebinds the global ::Psych constant for the whole
|
|
18
|
+
# process, which a library must never do.
|
|
19
|
+
module YamlBackend
|
|
20
|
+
VALID_BACKENDS = %i[psych yeptris].freeze
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
def active
|
|
24
|
+
@active ||= begin
|
|
25
|
+
wanted = forced || :psych
|
|
26
|
+
# A forced yeptris without a loadable gem/native lib must
|
|
27
|
+
# degrade to Psych, not NameError in the gateway.
|
|
28
|
+
wanted = :psych if wanted == :yeptris && !yeptris_available?
|
|
29
|
+
wanted
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def psych?
|
|
34
|
+
active == :psych
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def yeptris?
|
|
38
|
+
active == :yeptris
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def reset!
|
|
42
|
+
@active = nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def yeptris_available?
|
|
46
|
+
return false if RUBY_ENGINE == "opal"
|
|
47
|
+
|
|
48
|
+
require "yeptris"
|
|
49
|
+
Yeptris::YAML.respond_to?(:load)
|
|
50
|
+
rescue LoadError, StandardError
|
|
51
|
+
false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def forced
|
|
57
|
+
value = ENV["CANON_YAML_BACKEND"].to_s.downcase
|
|
58
|
+
return nil unless VALID_BACKENDS.include?(value.to_sym)
|
|
59
|
+
|
|
60
|
+
value.to_sym
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
module Canon
|
|
6
|
+
# The only place canon talks to a YAML engine. All string loads go
|
|
7
|
+
# through safe_load here (see XmlParsing for the XML equivalent);
|
|
8
|
+
# YAML.dump stays on Psych everywhere — canonical output bytes are
|
|
9
|
+
# canon's product and the writers differ.
|
|
10
|
+
module YamlParsing
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# Psych::safe_load semantics. yeptris is safe-by-default plain
|
|
14
|
+
# data with Symbol/Date/Time built in (aliases resolve), so the
|
|
15
|
+
# permitted-class list is inherently satisfied; parse failures are
|
|
16
|
+
# normalized to Psych::SyntaxError so canon's rescues hold on both
|
|
17
|
+
# engines.
|
|
18
|
+
def safe_load(yaml, permitted_classes: [Symbol, Date, Time],
|
|
19
|
+
aliases: false)
|
|
20
|
+
if YamlBackend.yeptris?
|
|
21
|
+
begin
|
|
22
|
+
::Yeptris::YAML.load(yaml)
|
|
23
|
+
rescue ::Yeptris::ParseError => e
|
|
24
|
+
raise Psych::SyntaxError.new(nil, 0, 0, 0, e.message, "")
|
|
25
|
+
rescue ::FFI::NullPointerError
|
|
26
|
+
# yeptris-ruby#29: empty/comment-only input. Psych returns
|
|
27
|
+
# nil; mirror that until the upstream fix lands.
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
YAML.safe_load(yaml, permitted_classes: permitted_classes,
|
|
32
|
+
aliases: aliases)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
data/lib/canon.rb
CHANGED
|
@@ -5,6 +5,8 @@ require "canon/errors"
|
|
|
5
5
|
require "nokogiri" unless RUBY_ENGINE == "opal"
|
|
6
6
|
require "canon/xml_backend"
|
|
7
7
|
require "canon/xml_parsing"
|
|
8
|
+
require "canon/yaml_backend"
|
|
9
|
+
require "canon/yaml_parsing"
|
|
8
10
|
require "canon/config"
|
|
9
11
|
require "canon/data_model"
|
|
10
12
|
require "canon/xml"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: canon
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.23
|
|
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-09-
|
|
11
|
+
date: 2026-09-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: diff-lcs
|
|
@@ -407,6 +407,8 @@ files:
|
|
|
407
407
|
- lib/canon/xml/xpath_engine.rb
|
|
408
408
|
- lib/canon/xml_backend.rb
|
|
409
409
|
- lib/canon/xml_parsing.rb
|
|
410
|
+
- lib/canon/yaml_backend.rb
|
|
411
|
+
- lib/canon/yaml_parsing.rb
|
|
410
412
|
- lib/tasks/benchmark_runner.rb
|
|
411
413
|
- lib/tasks/performance.rake
|
|
412
414
|
- lib/tasks/performance_comparator.rb
|