1. Purpose
Canon’s comparison system uses a 4-layer architecture where each layer has a distinct responsibility. Understanding this pipeline is essential for configuring Canon effectively.
2. The 4-Layer Flow
Canon processes document comparisons through four sequential layers:
graph TD
A[Input Documents] --> B[Layer 1: Preprocessing]
B --> C[Layer 2: Algorithm Selection]
C --> D[Layer 3: Match Options]
D --> E[Layer 4: Diff Formatting]
E --> F[Output]
style B fill:#e1f5ff
style C fill:#fff4e1
style D fill:#ffe1f5
style E fill:#e1ffe1
3. Layer Overview
3.1. Layer 1: Preprocessing
Purpose: Normalize documents before comparison
Options:
* none - No preprocessing (default)
* c14n - Canonicalize using format-specific rules
* normalize - Normalize whitespace
* format - Pretty-print with consistent formatting
When this runs: Before any comparison takes place
Documentation: See Preprocessing
3.2. Layer 2: Algorithm Selection
Purpose: Choose the comparison strategy
Options:
* dom - DOM-based positional comparison (default, stable)
* semantic - Tree-based semantic diff (experimental, intelligent)
Impact: Determines how Layers 3 and 4 behave
Documentation: See Algorithms
3.3. Layer 3: Match Options
Purpose: Configure what to compare and how strictly
Key Concept: This layer is algorithm-specific - each algorithm interprets match options differently.
Components: * Match dimensions (granular control) * Match profiles (preset combinations) * Algorithm-specific behaviors
Documentation: See Match Options and Algorithm-Specific Behavior
3.4. Layer 4: Diff Formatting
Purpose: Control how differences are displayed
Key Concept: This layer is algorithm-specific - each algorithm generates different output types.
Components:
* Diff mode (by_line, by_object)
* Colors and symbols
* Context and grouping
* Character visualization
Documentation: See Diff Formatting and Algorithm-Specific Output
4. DiffNode Data Flow
4.1. How differences flow through the layers
DiffNode objects are created in Layer 2, enriched with metadata, and flow through to Layer 4 for rendering:
graph LR
A[Layer 2: Algorithm] --> B[Create DiffNode]
B --> C[Enrich Metadata]
C --> D[Layer 3: Classification]
D --> E[Layer 4: Rendering]
E --> F[Formatted Output]
C --> C1[PathBuilder]
C --> C2[NodeSerializer]
C1 --> G[path]
C2 --> H[serialized_before/after]
C2 --> I[attributes_before/after]
style A fill:#fff4e1
style C fill:#e1f5ff
style E fill:#e1ffe1
4.2. Layer 2: DiffNode creation
Each algorithm creates DiffNode objects when it finds differences:
# DOM algorithm: Creates DiffNode during element-by-element comparison
diff_node = Canon::Diff::DiffNode.new(
node1: element1,
node2: element2,
dimension: :text_content,
reason: "Text content differs"
)
# Semantic algorithm: Creates DiffNode from tree operations
diff_node = Canon::Diff::DiffNode.new(
node1: nil,
node2: inserted_node,
dimension: :element_structure,
reason: "Element inserted"
)
4.3. Metadata enrichment
After creation, DiffNodes are enriched with metadata for Layer 4 rendering:
# Enriched with:
{
path: "/#document/div[0]/p[1]/span[2]", # Canonical location
serialized_before: "<span>Old text</span>", # Captured state
serialized_after: "<span>New text</span>", # Captured state
attributes_before: {"id" => "old"}, # Normalized attrs
attributes_after: {"id" => "new"} # Normalized attrs
}
Enrichment utilities:
-
PathBuilder: Generates canonical paths with ordinal indices
-
NodeSerializer: Library-agnostic serialization of node content
-
Attribute extraction: Normalized attribute hashes
See DiffNode Enrichment for implementation details.
4.4. Layer 3: Classification
DiffNodes are classified to determine their impact:
diff_node.normative = true # Affects semantic equivalence
diff_node.formatting = true # Purely cosmetic difference
This classification affects whether differences cause equivalent? to return false.
4.5. Layer 4: Rendering
Layer 4 formatters use enriched metadata to display differences:
🔍 DIFFERENCE #1/3 [NORMATIVE]
════════════════════════════════════════════════════════════════════════
Dimension: text_content
Location: /#document/div[0]/p[1]/span[2]
⊖ Expected (File 1):
<span>Old text</span>
⊕ Actual (File 2):
<span>New text</span>
✨ Changes:
Text content changed from "Old text" to "New text"
-
The
Locationfield uses the enrichedpath -
The before/after content uses
serialized_before/after -
Attribute differences use
attributes_before/after
This ensures accurate display regardless of which parsing library was used.
4.6. Benefits of enriched DiffNodes
Library flexibility: Layer 4 works with any parsing library through enriched metadata
Performance: Metadata captured once at diff creation, not recomputed during rendering
Accuracy: Shows actual node state when difference was found, not current state
Debuggability: Ordinal indices in paths make it easy to locate specific elements
5. Complete Example
Here’s a full 4-layer configuration showing all layers working together:
5.1. Ruby API
result = Canon::Comparison.equivalent?(doc1, doc2,
# Layer 1: Preprocessing
preprocessing: :normalize,
# Layer 2: Algorithm
diff_algorithm: :semantic,
# Layer 3: Match Options
match_profile: :spec_friendly,
# Layer 4: Diff Formatting
verbose: true,
diff_mode: :by_object,
use_color: true,
context_lines: 3
)
6. Layer-by-Layer Build-Up
Let’s see how each layer adds to the configuration:
6.1. Just Layer 1
Canon::Comparison.equivalent?(doc1, doc2,
preprocessing: :normalize
)
# Documents are normalized, then compared using default DOM algorithm
# with strict matching and by-line diff output
6.2. Layers 1 + 2
Canon::Comparison.equivalent?(doc1, doc2,
preprocessing: :normalize,
diff_algorithm: :semantic
)
# Documents are normalized, then compared using semantic algorithm
# with strict matching and by-object diff output (semantic's natural mode)
6.3. Layers 1 + 2 + 3
Canon::Comparison.equivalent?(doc1, doc2,
preprocessing: :normalize,
diff_algorithm: :semantic,
match_profile: :spec_friendly
)
# Documents are normalized, compared using semantic algorithm
# with spec_friendly matching (ignores formatting differences)
# Default diff output is by-object
6.4. All 4 Layers
Canon::Comparison.equivalent?(doc1, doc2,
preprocessing: :normalize,
diff_algorithm: :semantic,
match_profile: :spec_friendly,
verbose: true,
diff_mode: :by_line,
use_color: true
)
# Complete configuration with all layers specified
# Documents normalized, semantic comparison, spec-friendly matching,
# traditional line-based diff output with colors
7. Layer Interaction Matrix
This table shows common configuration patterns:
| Layer 1 | Layer 2 | Layer 3 | Layer 4 | Result |
|---|---|---|---|---|
none |
dom |
strict |
by_line |
Traditional exact comparison |
normalize |
dom |
spec_friendly |
by_line |
Test-friendly comparison |
c14n |
dom |
content_only |
by_object |
Canonical structure view |
none |
semantic |
strict |
by_object |
Semantic operations view |
normalize |
semantic |
rendered |
by_line |
Rendered diff with operations |
8. Key Principles
8.1. Layer Independence
Each layer has a distinct purpose: * Layer 1: Document transformation * Layer 2: Comparison strategy * Layer 3: Match criteria * Layer 4: Output presentation
9. Common Patterns
9.1. Testing XML Generation
Canon::Comparison.equivalent?(expected, actual,
preprocessing: :normalize, # Ignore formatting
match_profile: :spec_friendly, # Focus on content
verbose: true # Show differences
)
10. Anti-Patterns to Avoid
11. See Also
-
Architecture - Overall system design
-
Algorithms - Detailed algorithm documentation
-
Algorithm-Specific Behavior - How algorithms interpret match options
-
Algorithm-Specific Output - Different output formats
-
Choosing Configuration - Decision guide for all layers