1. Purpose
The Semantic Diff Report provides dimension-specific, actionable details for each difference found during comparison. It focuses on what changed and why it matters, rather than just showing every changed line.
The report is automatically shown in verbose mode when differences exist, appearing before the detailed diff output.
2. Parse errors
When Canon’s underlying parser (libxml for XML, HTML5 for HTML) reports errors during input parsing, Canon surfaces them at the top of the diff report in a banner section before any per-difference output. The banner names the offending side and warns that the diff below describes the parsed tree, not the input — content the parser could not represent has been silently dropped from the comparison tree.
This is purely a transparency feature: Canon does not modify the parse to "fix" invalid input. The user is responsible for deciding whether the parse failure was expected (e.g. testing legacy fixtures during a migration) or symptomatic of an upstream bug.
======================================================================
⚠️ PARSE ERRORS
======================================================================
Received side:
Attribute xml:lang redefined
⚠️ The diff below describes the parsed tree, not the input.
Content that the parser could not represent has been
dropped and may appear as "missing" in the report.
======================================================================
Common triggers in HTML / XHTML round-trips:
-
Duplicate attributes (XML strict; HTML5 permissive — only XML mode triggers a banner)
-
Stray processing instructions in fragment context
-
Malformed namespace declarations
-
DOCTYPE in unexpected positions
The banner is rendered when Canon::Comparison::ComparisonResult#parse_errors? is true. Programmatic callers can read parse_errors_expected and parse_errors_received directly off the result.
3. Key Features
-
XPath locations for XML/HTML elements
-
JSON path locations for JSON/YAML data
-
Dimension-specific formatting optimized for each type of difference
-
Colorized output for easy visual scanning
-
Whitespace preservation detection for
<pre>,<code>, etc. -
Actionable change summaries (e.g., "Added: +xmlns:v, +xmlns:o")
4. Report Format
Each difference is displayed as a vertical section:
🔍 DIFFERENCE #1/3 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: attribute_presence
Location: /iso-standard/preface/foreword
⊖ Expected (File 1):
<foreword> with 3 attributes: displayorder, id, semx-id
⊕ Actual (File 2):
<foreword> with 2 attributes: displayorder, id
✨ Changes:
Removed: -semx-id
Where:
STATUS-
Either
[NORMATIVE](green) or[INFORMATIVE](yellow) Dimension-
The match dimension that detected this difference (magenta)
Location-
XPath for XML/HTML, JSON path for JSON/YAML (blue)
Expected-
What was in File 1 (red heading)
Actual-
What was in File 2 (green heading)
Changes-
Actionable summary (yellow)
5. Color Scheme
-
Dimension name: Magenta
-
XPath/JSON path: Blue
-
Expected heading: Red (bold)
-
Actual heading: Green (bold)
-
Changes heading: Yellow (bold)
-
Status [NORMATIVE]: Green (bold)
-
Status [INFORMATIVE]: Yellow (bold)
-
Added items: Green (with
+prefix) -
Removed items: Red (with
-prefix) -
Element names: Magenta
-
Attribute names: Cyan
6. XML/HTML Dimensions
6.1. Attribute Presence
Shows when attributes are missing or extra.
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: attribute_presence
Location: /html/body/div/div
⊖ Expected (File 1):
<div> with 2 attributes: class, id
⊕ Actual (File 2):
<div> with 1 attribute: id
✨ Changes:
Removed: -class
6.2. Attribute Values
Shows when an attribute value differs.
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: attribute_values
Location: /html/body/div[@id="main"]
⊖ Expected (File 1):
<div> class=" container fluid "
⊕ Actual (File 2):
<div> class="container fluid"
✨ Changes:
Whitespace normalization difference
6.3. Text Content
Shows when element text differs.
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: text_content
Location: /html/body/div/table/tbody/tr/td/pre/text
⊖ Expected (File 1):
<text> "
puts \"Hello, world.\"
"
⊕ Actual (File 2):
<text> "puts \"Hello, world.\" "
✨ Changes:
⚠️ Whitespace preserved (inside <pre>, <code>, etc. - whitespace is significant)
The warning appears for text inside whitespace-preserving elements where Canon automatically switches to strict mode.
6.3.1. Parent-context fallback for ambiguous text diffs
For a text_content difference, Canon normally renders the two sides as JSON-quoted strings.
When both sides would collapse to the same (or visually indistinguishable) short string — both empty (""), both whitespace-only, or both equal on the text-node extraction even though the surrounding DOM differs — that rendering conveys nothing.
In this case Canon instead serializes each side’s parent element compactly and visualizes whitespace (· for space, → for tab, ¬ for newline, <NBSP> for non-breaking space) so the structural contrast is visible.
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: text_content
Location: /#document[0]/fmt-title[0]/span[0]/span/text()[0]
Reason: Text: "¬······:¬······"
vs.: ":"
⊖ Expected (File 1):
<span·class="fmt-caption-delim">¬······:¬······<tab/>¬···</span>
⊕ Actual (File 2):
<span·class="fmt-caption-delim">:<tab/></span>
✨ Changes:
Content differs: <span·class="fmt-caption-delim">¬······:¬······<tab/>¬···</span> → <span·class="fmt-caption-delim">:<tab/></span>
This fallback is implemented in Canon::DiffFormatter::DiffDetailFormatterHelpers::DimensionFormatter.format_text_content_details and only triggers when TextUtils.ambiguous_text_pair? returns true and at least one side has a parent element to render.
The same fallback also applies to the whitespace_adjacency dimension (see Whitespace adjacency): when the alignment partner of a stray whitespace node extracts to an empty / whitespace-only string, the Reason line reads Whitespace inside <PARENT> (rather than Whitespace before ""), and the Expected/Actual block surfaces each side’s parent element compactly. See format_whitespace_adjacency_details and Canon::Comparison::XmlComparator#build_whitespace_adjacency_reason.
6.3.2. One-sided text diffs (added or removed text nodes)
When a text_content difference carries a text node on one side and nil on the other (issue #125) — the shape that fragment-length mismatches and child-comparison emit when a text-node child is missing — the renderer mirrors element_structure: the missing side reads (not present), and the present side reads the text-node content (whitespace-visualised) plus a brief parent open-tag hint for context. The full ancestor subtree is not dumped; only the immediate parent’s opening tag is shown, so a missing whitespace text node cannot make the diff look like the entire ancestor differs.
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: text_content
Reason: element missing: text
⊖ Expected (File 1):
text "¬············" in <div id="A">
⊕ Actual (File 2):
(not present)
✨ Changes:
Text removed: text "¬············" in <div id="A">
The Changes: line uses Text removed: or Text added: to mirror the Element removed: / Element added: phrasing of element_structure.
6.3.3. Element-shaped diffs misclassified as text_content
In rare cases an upstream comparator may emit an element-shaped one-sided diff under dimension: :text_content. Without a guard, the one-sided text formatter would call raw_text_value on the element (which returns "" for an empty element such as <br/>) and render text "" in <parent> — meaningless when an element is what’s actually missing.
The formatter detects element-shaped present-side nodes (Canon ElementNode or Nokogiri Element) and delegates to format_element_structure_details, so the rendered output reads <br/> and Element removed: rather than text "" and Text removed:. This is defence in depth — the construction-side fix in XmlComparatorHelpers::ChildComparison ensures element orphans are now tagged :element_structure at source — but a misclassified diff still renders meaningfully if any path slips through.
6.4. Structural Whitespace
Shows whitespace-only differences (usually informative).
🔍 DIFFERENCE #1/1 [INFORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: structural_whitespace
Location: /root/section/p
⊖ Expected (File 1):
<p> "hello␣␣world"
⊕ Actual (File 2):
<p> "hello␣world"
✨ Changes:
Whitespace-only difference (informative)
6.5. Comments
Shows when comment content differs.
🔍 DIFFERENCE #1/1 [INFORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: comments
Location: /html/head
⊖ Expected (File 1):
<!-- Original comment text -->
⊕ Actual (File 2):
<!-- Modified comment text -->
✨ Changes:
Comment content differs
7. JSON/YAML Dimensions
JSON and YAML use path-based reporting:
7.1. Hash Key Differences
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: hash_key
Location: user.email
⊖ Expected (File 1):
user.email = "alice@example.com"
⊕ Actual (File 2):
user.email = nil
✨ Changes:
Key missing
7.2. Primitive Value Differences
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: primitive_value
Location: users[0].age
⊖ Expected (File 1):
users[0].age = 25
⊕ Actual (File 2):
users[0].age = 30
✨ Changes:
Value changed
7.3. Array Differences
🔍 DIFFERENCE #1/1 [NORMATIVE]
──────────────────────────────────────────────────────────────────────
Dimension: array_length
Location: items
⊖ Expected (File 1):
items = [...] (5 items)
⊕ Actual (File 2):
items = [...] (3 items)
✨ Changes:
Array length differs
8. Special Features
8.1. XPath Location Extraction
For XML/HTML differences, the report extracts XPath with:
-
Full path from root:
/html/body/div/section/p -
Position predicates when multiple siblings:
/p[2],/div[3] -
Safe traversal with depth limits to prevent infinite loops
-
Graceful error handling for circular references
8.2. Whitespace Preservation Detection
The report detects when text is inside whitespace-preserving HTML elements and shows a special warning:
✨ Changes: ⚠️ Whitespace preserved (inside <pre>, <code>, etc. - whitespace is significant)
Elements detected:
-
<pre>- Preformatted text -
<code>- Code blocks -
<textarea>- Text input areas -
<script>- JavaScript code -
<style>- CSS style sheets
This helps developers understand why whitespace differences cause test failures.
8.3. Comprehensive Error Handling
The formatter includes multiple layers of error handling:
-
Top-level rescue in formatting functions
-
Safe XPath extraction with depth limits
-
Safe parent traversal with document node checks
-
Graceful fallbacks when node types are unexpected
This ensures the report never crashes, even with unusual DOM structures.
9. Integration
The Semantic Diff Report is integrated at the format_comparison_result() level:
def format_comparison_result(comparison_result, expected, actual)
output = []
# 1. Algorithm name
output << "Algorithm: SEMANTIC TREE DIFF"
# 2. CANON VERBOSE tables (optional)
output << verbose_tables if ENV['CANON_VERBOSE'] == '1'
# 3. Semantic Diff Report (always if diffs exist)
if comparison_result.differences.any?
output << DiffDetailFormatter.format_report(differences)
end
# 4. Main diff output (always)
output << format(...)
output.compact.join("\n")
end
This ensures the Semantic Diff Report is part of the main output, not debug information.
10. When It Appears
The Semantic Diff Report appears automatically when:
-
Comparison finds differences
-
Verbose mode is enabled (
verbose: trueor--verbose) -
Files are not equivalent
It does not appear when:
-
Files are equivalent
-
Verbose mode is disabled
-
No differences exist
11. Practical Uses
11.1. Debugging Test Failures
When a test fails, the report immediately shows:
-
Exactly which element changed
-
What dimension detected the change
-
Whether it’s normative or informative
-
Actionable summary of what to fix
12. See Also
-
Diff Classification - Normative vs informative
-
Colors and Symbols - Visual indicators
-
Verbose Mode - Debug output details
-
Comparison Pipeline - How comparison works