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

<pre>

Preformatted text blocks

<code>

Code snippets

<textarea>

Form input fields

<script>

JavaScript code

<style>

CSS stylesheets

Inside these elements, ALL whitespace is preserved even when text_content: :normalize is set.

Example 1. Example: Whitespace preservation in <pre>
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.

Example 2. Example: Whitespace normalization in <div>
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:

Example 3. HTML4 (case-insensitive)
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)
Example 4. HTML5 (case-sensitive)
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.8. Usage Examples

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.12. Preprocessing Options

HTML supports several preprocessing modes:

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.16. Advanced Examples

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