1. Purpose
Canon’s diff reports anchor whitespace-only text nodes that have no
counterpart on the other side to a dedicated :whitespace_adjacency
dimension instead of letting them cascade into 3-4 misaligned
:text_content mismatches against neighbouring content nodes.
This is a report-only contract — equivalence verdicts are unchanged. Inputs that were non-equivalent before this feature remain non-equivalent; only the shape of the diff report changes.
2. The problem
Consider an HTML fragment compared as be_html_equivalent_to:
<!-- expected -->
<p>
<span>ISO </span>
<span>20483</span>
,
<i>Cereals and pulses</i>
</p>
<!-- actual -->
<p><span>ISO </span><span>20483</span>, <i>Cereals and pulses</i></p>
Both render identically in a browser — the indentation is structural HTML formatting, not content. Before this feature, the diff report contained four entries:
DIFFERENCE #1 — element_structure: parent <p> "missing children"
DIFFERENCE #2 — text_content: "" vs "20483" (visualised: ↵░░░░)
DIFFERENCE #3 — text_content: "20483" vs ","
DIFFERENCE #4 — text_content: "," vs "Cereals and pulses"
The cascade comes from positional zip() alignment in
Canon::Comparison::XmlComparatorHelpers::ChildComparison: with the
expected side carrying extra whitespace-only text nodes and the actual
side carrying none, every child slides by one slot and gets paired
against the wrong neighbour.
3. The contract
When ChildComparison aligns child sequences and encounters a
whitespace-only text node on one side paired against a non-whitespace
node on the other, it:
-
Treats the whitespace node as a single-side gap in the alignment.
-
Emits one
:whitespace_adjacencydiff entry anchored at the whitespace node itself (not at its mis-paired neighbour). -
Advances only the cursor that carries the whitespace, so the next iteration aligns content against content.
The asymmetric whitespace still produces a non-equivalent verdict — the
:whitespace_adjacency dimension is classified as normative
unconditionally — so any test that previously failed on whitespace
asymmetry continues to fail.
After the new contract, the cascade above collapses to:
DIFFERENCE #1 — whitespace_adjacency: Whitespace before "20483":
present on EXPECTED ("↵░░"), absent on ACTUAL
DIFFERENCE #2 — whitespace_adjacency: Whitespace before ",":
present on EXPECTED ("↵░░"), absent on ACTUAL
DIFFERENCE #3 — text_content: "↵░░,↵░░" vs ", "
4. Direction relative to the partner
The Reason line names the document-order position of the whitespace node relative to the partner content node it was zipped against by the alignment walk. The partner is the next (or, at parent edge, previous) non-whitespace sibling on the whitespace-bearing side, which is what aligns against the corresponding content node on the other side.
before-
The whitespace immediately precedes its next non-whitespace sibling. This is the common case (e.g. indentation between two inline spans where the asymmetric whitespace sits on the leading edge of the partner).
after-
The whitespace trails the previous non-whitespace sibling and has no non-whitespace sibling after it. Emitted at the trailing edge of a parent.
adjacent to-
Degenerate fallback for a whitespace node with no non-whitespace siblings at all. Rarely emitted.
When the alignment partner extracts to an empty / whitespace-only
string (e.g. an element with no text descendants), the direction
phrasing degenerates to Whitespace before "" which carries no
information. In that case Canon falls back to naming the parent
element instead — Whitespace inside <PARENT> — and the
Expected/Actual detail block renders each side’s parent element
compactly per the contract from
issue #112.
An earlier wording (Whitespace surrounding "X") classified the
whitespace node’s position among its own siblings rather than its
direction relative to the partner. That label was misleading when the
whitespace sat between two element siblings but the asymmetry was
one-sided — see issue #137 follow-up.
|
4.1. Examples
4.1.1. "before" — whitespace between inline element siblings
html1 = "<a><span>ISO </span>\n <span>712</span></a>"
html2 = "<a><span>ISO </span><span>712</span></a>"
result = Canon::Comparison.equivalent?(html1, html2,
format: :html5, verbose: true)
# => #<ComparisonResult equivalent=false>
# The stray "\n " between two spans is the only asymmetric node.
# It sits immediately before <span>712</span>, its next non-ws sibling.
result.differences.first.reason
# => "Whitespace before \"712\": present on EXPECTED (\"░\"), absent on ACTUAL"
4.1.2. "after" — trailing whitespace in a whitespace-preserving element
# <code> preserves whitespace, so the trailing newline survives the
# upstream filter and pairs against the extra <b>B</b> on the other side.
html1 = "<code><b>A</b>\n</code>"
html2 = "<code><b>A</b><b>B</b></code>"
result = Canon::Comparison.equivalent?(html1, html2,
format: :html5, verbose: true)
result.differences.first.reason
# => "Whitespace after \"B\": present on EXPECTED (\"↵\"), absent on ACTUAL"
4.1.3. "adjacent to" — sole-child whitespace node
# A whitespace-only text node as the only child of <code>, paired
# against an element on the other side. No non-ws siblings exist.
html1 = "<code>\n</code>"
html2 = "<code><b>A</b></code>"
result = Canon::Comparison.equivalent?(html1, html2,
format: :html5, verbose: true)
result.differences.first.reason
# => "Whitespace adjacent to \"A\": present on EXPECTED (\"↵\"), absent on ACTUAL"
5. Working with :whitespace_adjacency diffs programmatically
Use the dimension field on DiffNode to filter:
result = Canon::Comparison.equivalent?(html1, html2,
format: :html5, verbose: true)
# Find all whitespace-adjacency diffs
ws_diffs = result.differences.select { |d| d.dimension == :whitespace_adjacency }
# These are always normative — they affect the equivalence verdict
ws_diffs.all?(&:normative?) # => true
6. What this contract does NOT do
-
Does not change equivalence outcomes. A non-equivalent comparison before #137 remains non-equivalent after — only the diff-report shape changes.
-
Does not silently filter whitespace. The asymmetric whitespace is always reported; it is just labelled
:whitespace_adjacencyand anchored at the whitespace node, instead of cascading as:text_contentagainst unrelated content nodes. -
Does not affect symmetric whitespace. When both sides carry parallel whitespace-only nodes, those compare normally (no
:whitespace_adjacencyentry, no cascade).
7. Where it runs
The contract is implemented as a re-alignment walk inside
Canon::Comparison::XmlComparatorHelpers::ChildComparison.use_positional_comparison.
It activates whenever the existing positional zip() alignment would
pair a whitespace-only text node against a content node — that is, in
every whitespace context where the upstream filter has not already
dropped the whitespace nodes.
For elements where whitespace is preserved by configuration
(preserve_whitespace_elements) the upstream filter does not drop
indentation, and the re-alignment walk surfaces every asymmetric
whitespace node as a single normative :whitespace_adjacency diff.
8. Related
-
Diff classification — Normative vs informative differences.
-
Match options — Configuring
preserve_whitespace_elements,collapse_whitespace_elements, andstrip_whitespace_elements.
9. History
The cascade behaviour was reported in
issue #137. The fix landed
as a report-only re-alignment in PR #138. PR #141 replaced the
misleading :surrounding/:preceding/:following position labels
with direction-faithful wording (before/after/adjacent to).