Class: Canon::Comparison::HtmlCompareProfile

Inherits:
CompareProfile show all
Defined in:
lib/canon/comparison/html_compare_profile.rb

Overview

HtmlCompareProfile extends CompareProfile with HTML-specific comparison policies

HTML has different semantics than XML:

  1. Comments are presentational (default to :ignore unless explicitly :strict)

  2. Whitespace preservation required in specific elements

  3. Case sensitivity differs between HTML4 and HTML5

  4. Self-closing tags handled differently

This class provides HTML-specific policy decisions while maintaining the separation of concerns established by CompareProfile.

Instance Attribute Summary collapse

Attributes inherited from CompareProfile

#match_options

Instance Method Summary collapse

Methods inherited from CompareProfile

#normative_dimension?, #supports_formatting_detection?, #track_dimension?

Constructor Details

#initialize(match_options, html_version: :html5) ⇒ HtmlCompareProfile

Returns a new instance of HtmlCompareProfile.

Parameters:

  • match_options (ResolvedMatchOptions, Hash)

    The match options to use

  • html_version (Symbol) (defaults to: :html5)

    The HTML version (:html4 or :html5)



24
25
26
27
# File 'lib/canon/comparison/html_compare_profile.rb', line 24

def initialize(match_options, html_version: :html5)
  super(match_options)
  @html_version = html_version
end

Instance Attribute Details

#html_versionObject (readonly)

Returns the value of attribute html_version.



20
21
22
# File 'lib/canon/comparison/html_compare_profile.rb', line 20

def html_version
  @html_version
end

Instance Method Details

#affects_equivalence?(dimension) ⇒ Boolean

Override for HTML-specific comment handling

In HTML, comments are presentational content (not part of the DOM semantics) unless explicitly set to :strict. This differs from XML where comments may carry semantic meaning.

HTML default for comments is :ignore, so comments don’t affect equivalence unless the user explicitly sets comments: :strict

Parameters:

  • dimension (Symbol)

    The match dimension to check

Returns:

  • (Boolean)

    true if differences affect equivalence



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/canon/comparison/html_compare_profile.rb', line 40

def affects_equivalence?(dimension)
  # Comments in HTML: default is :ignore (presentational)
  # Only affect equivalence if explicitly set to :strict
  if dimension == :comments
    # Check if comments key exists in options
    if match_options.is_a?(Hash)
      # If comments key doesn't exist, default to false (HTML default: ignore)
      return false unless match_options.key?(:comments)

      # If key exists, check if it's :strict
      return match_options[:comments] == :strict
    elsif match_options.respond_to?(:behavior_for)
      behavior = behavior_for(dimension)
      # In HTML, only :strict makes comments affect equivalence
      return behavior == :strict
    end
    # Default: comments don't affect equivalence in HTML
    return false
  end

  # All other dimensions use base class behavior
  super
end

#case_sensitive?Boolean

Check if element names should be compared case-sensitively

HTML4 is case-insensitive, HTML5 is case-sensitive

Returns:

  • (Boolean)

    true if case-sensitive comparison



80
81
82
# File 'lib/canon/comparison/html_compare_profile.rb', line 80

def case_sensitive?
  @html_version == :html5
end

#preserve_whitespace?(element_name) ⇒ Boolean

Check if whitespace should be preserved for a given element

HTML has specific elements where whitespace is significant: <pre>, <code>, <textarea>, <script>, <style>

Parameters:

  • element_name (String)

    The element name to check

Returns:

  • (Boolean)

    true if whitespace should be preserved



71
72
73
# File 'lib/canon/comparison/html_compare_profile.rb', line 71

def preserve_whitespace?(element_name)
  html_preserve_elements.include?(element_name.to_s.downcase)
end