- 1. HTML-Specific Comparison Policies
- 1.1. Overview
- 1.2. Default Policies
- 1.3. HTML Version Detection
- 1.4. Whitespace Preservation
- 1.5. Comment Handling
- 1.6. Why comments are ignored by default
- 1.7. Case Sensitivity
- 1.8. Usage Examples
- 1.9. Default HTML comparison
- 1.10. Strict HTML comparison
- 1.11. Mixed policies
- 1.12. Preprocessing Options
- 1.13.
:rendered(default) - 1.14.
:format - 1.15.
:none - 1.16. Advanced Examples
- 1.17. Compare HTML with mixed content
- 1.18. Compare with element-specific preservation
- 1.19. Detect normative vs informative differences
- 1.20. Implementation
- 1.21. See Also
1. HTML-Specific Comparison Policies
1.1. Overview
HTML comparison has specific policies that differ from XML due to HTML’s unique
characteristics and rendering behavior. Canon uses HtmlCompareProfile to
implement these format-specific policies.
1.2. Default Policies
HTML uses the :rendered profile by default:
{
preprocessing: :rendered,
text_content: :normalize,
structural_whitespace: :normalize,
comments: :ignore,
attribute_order: :ignore
}
This reflects how browsers render HTML - whitespace is normalized and comments are presentational.
1.3. HTML Version Detection
Canon automatically detects HTML version:
-
HTML4: Case-insensitive element/attribute names
-
HTML5: Case-sensitive (preserves case)
Detection is based on DOCTYPE or parsing mode.
Whitespace sensitivity does not differ between HTML4 and HTML5 — both apply HTML’s content-model whitespace rules. be_html4_equivalent_to and be_html5_equivalent_to therefore agree on whether two inputs are whitespace-equivalent. Differences between the matchers are limited to genuine HTML4/HTML5 distinctions such as case sensitivity. Internally Canon parses both via Nokogiri::HTML5 to share the content-model logic.
|
1.4. Whitespace Preservation
Certain HTML elements require strict whitespace preservation regardless of the
text_content policy:
| Element | Purpose |
|---|---|
|
Preformatted text blocks |
|
Code snippets |
|
Form input fields |
|
JavaScript code |
|
CSS stylesheets |
Inside these elements, ALL whitespace is preserved even when text_content:
:normalize is set.
html1 = '<pre>Line 1\n Line 2</pre>'
html2 = '<pre>Line 1\nLine 2</pre>'
# Whitespace is preserved - not equivalent
Canon::Comparison.equivalent?(html1, html2, preprocessing: :rendered)
# => false
The indentation difference matters in <pre> elements.
html1 = '<div>Text with spaces</div>'
html2 = '<div>Text with spaces</div>'
# Whitespace is normalized - equivalent
Canon::Comparison.equivalent?(html1, html2, preprocessing: :rendered)
# => true
Multiple spaces are normalized to single spaces in regular elements.
1.5. Comment Handling
HTML comments are presentational by default (like CSS styles):
# Default: comments ignored (informative)
html1 = '<div><!-- comment --><p>Text</p></div>'
html2 = '<div><p>Text</p></div>'
Canon::Comparison.equivalent?(html1, html2)
# => true (comments don't affect equivalence)
# Strict mode: comments compared (normative)
Canon::Comparison.equivalent?(html1, html2, match: { comments: :strict })
# => false (comments affect equivalence)
1.6. Why comments are ignored by default
In HTML, comments serve similar purposes to CSS: * Developer notes * Conditional comments (IE hacks) * Disabled code blocks * Build tool markers
They don’t affect rendering or semantic meaning, so they’re informative by default.
1.7. Case Sensitivity
HTML4 and HTML5 have different case sensitivity rules:
html1 = '<DIV CLASS="test">Content</DIV>'
html2 = '<div class="test">Content</div>'
Canon::Comparison.equivalent?(html1, html2, format: :html4)
# => true (case doesn't matter in HTML4)
html1 = '<DIV CLASS="test">Content</DIV>'
html2 = '<div class="test">Content</div>'
Canon::Comparison.equivalent?(html1, html2, format: :html5)
# => false (case matters in HTML5, though uncommon)
1.9. Default HTML comparison
require 'canon/comparison'
html1 = '<div> <p> Text </p> </div>'
html2 = '<div><p>Text</p></div>'
# Uses HtmlCompareProfile automatically
result = Canon::Comparison.equivalent?(html1, html2)
# => true (whitespace normalized, comments ignored)
1.10. Strict HTML comparison
# All differences matter
result = Canon::Comparison.equivalent?(html1, html2,
match: {
text_content: :strict,
structural_whitespace: :strict,
comments: :strict,
attribute_order: :strict
}
)
# => false (whitespace differences are normative)
1.11. Mixed policies
# Normalize whitespace but compare comments strictly
result = Canon::Comparison.equivalent?(html1, html2,
match: {
text_content: :normalize,
structural_whitespace: :normalize,
comments: :strict
}
)
1.13. :rendered (default)
Simulates browser rendering: - Normalizes whitespace - Preserves whitespace in special elements - Ignores comments
Canon::Comparison.equivalent?(html1, html2, preprocessing: :rendered)
1.14. :format
Pretty-prints before comparison:
- Consistent indentation
- One element per line
- Whitespace-only text nodes in strip-context elements (e.g. <div>) are
removed after formatting, so differences in structural whitespace between
elements do not produce false normative differences
- Good for visual diffs
Canon::Comparison.equivalent?(html1, html2, preprocessing: :format)
1.15. :none
No preprocessing: - Raw comparison - Useful for exact matching
Canon::Comparison.equivalent?(html1, html2, preprocessing: :none)
1.17. Compare HTML with mixed content
html1 = '<p>This is <em>important</em> text.</p>'
html2 = '<p>This is <em>important</em> text.</p>'
result = Canon::Comparison.equivalent?(
html1, html2,
verbose: true,
match: { text_content: :normalize, structural_whitespace: :normalize }
)
result.equivalent? # => true
result.differences # => [#<DiffNode formatting: true, normative: false>]
1.18. Compare with element-specific preservation
html1 = '<div><pre> Code </pre></div>'
html2 = '<div><pre>Code</pre></div>'
# Whitespace preserved in <pre>, normalized in <div>
result = Canon::Comparison.equivalent?(html1, html2)
# => false (whitespace matters in <pre>)
1.19. Detect normative vs informative differences
html1 = '<div class="a" id="1"><!-- v1 --><p>Text</p></div>'
html2 = '<div id="1" class="b"><!-- v2 --><p>Text</p></div>'
result = Canon::Comparison.equivalent?(
html1, html2,
verbose: true,
match: { attribute_order: :ignore, comments: :ignore }
)
# Attribute order: informative (ignored)
# Comments: informative (ignored)
# Attribute value (class): normative (different)
result.equivalent? # => false
result.differences.select(&:normative?) # => [class attribute diff]
result.differences.reject(&:normative?) # => [order diff, comment diff]
1.20. Implementation
See the following files for implementation details:
-
[
lib/canon/comparison/html_compare_profile.rb](../../lib/canon/comparison/html_compare_profile.rb) - HTML-specific profile -
[
lib/canon/comparison/compare_profile.rb](../../lib/canon/comparison/compare_profile.rb) - Base profile -
[
spec/canon/comparison/html_compare_profile_spec.rb](../../spec/canon/comparison/html_compare_profile_spec.rb) - Comprehensive examples
1.21. See Also
-
Match Options - Overview of match system
-
Algorithm-Specific Behavior - How algorithms handle options
-
Diff Classification - Normative vs informative