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

#behavior_for, #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)



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

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.



16
17
18
# File 'lib/canon/comparison/html_compare_profile.rb', line 16

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.

Parameters:

  • dimension (Symbol)

    The match dimension to check

Returns:

  • (Boolean)

    true if differences affect equivalence



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/canon/comparison/html_compare_profile.rb', line 32

def affects_equivalence?(dimension)
  if dimension == :comments
    if match_options.is_a?(Hash)
      return false unless match_options.key?(:comments)

      return match_options[:comments] == :strict
    elsif match_options.is_a?(ResolvedMatchOptions)
      return behavior_for(dimension) == :strict
    end
    return false
  end

  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



76
77
78
# File 'lib/canon/comparison/html_compare_profile.rb', line 76

def case_sensitive?
  @html_version == :html5
end

#normative_dimension?(dimension) ⇒ Boolean

Override normative classification for HTML-specific comment handling.

Delegates to the parent class for all dimensions, which in turn delegates to Dimension objects. For :comments, applies the same HTML-specific rule as affects_equivalence? — comments default to non-normative in HTML.

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/canon/comparison/html_compare_profile.rb', line 52

def normative_dimension?(dimension)
  if dimension == :comments
    return affects_equivalence?(:comments)
  end

  super
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



67
68
69
# File 'lib/canon/comparison/html_compare_profile.rb', line 67

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