Class: Canon::Comparison::Dimensions::TextContentDimension

Inherits:
BaseDimension
  • Object
show all
Defined in:
lib/canon/comparison/dimensions/text_content_dimension.rb

Overview

Text content dimension

Handles comparison of text content in nodes. Supports :strict, :normalize, and :ignore behaviors.

Behaviors:

  • :strict - Exact text comparison including whitespace

  • :normalize - Collapse whitespace and compare

  • :ignore - Skip text content comparison

Constant Summary

Constants inherited from BaseDimension

BaseDimension::IGNORE, BaseDimension::NORMALIZE, BaseDimension::STRICT

Instance Method Summary collapse

Methods inherited from BaseDimension

#compare, #dimension_name, #equivalent?, #supports_normalization?

Instance Method Details

#compare_normalize(text1, text2) ⇒ Boolean

Normalized text comparison

Collapses whitespace and compares. Two whitespace-only strings that both normalize to empty are equivalent.

Parameters:

  • text1 (String, nil)

    First text

  • text2 (String, nil)

    Second text

Returns:

  • (Boolean)

    true if normalized texts are equal



52
53
54
55
56
57
58
59
60
61
# File 'lib/canon/comparison/dimensions/text_content_dimension.rb', line 52

def compare_normalize(text1, text2) # rubocop:disable Naming/PredicateMethod
  normalized1 = normalize_text(text1)
  normalized2 = normalize_text(text2)

  # Both empty after normalization = equivalent
  # This handles whitespace-only text nodes that normalize to empty
  return true if normalized1.empty? && normalized2.empty?

  normalized1 == normalized2
end

#compare_strict(text1, text2) ⇒ Boolean

Strict text comparison

Parameters:

  • text1 (String, nil)

    First text

  • text2 (String, nil)

    Second text

Returns:

  • (Boolean)

    true if texts are exactly equal



40
41
42
# File 'lib/canon/comparison/dimensions/text_content_dimension.rb', line 40

def compare_strict(text1, text2) # rubocop:disable Naming/PredicateMethod
  text1.to_s == text2.to_s
end

#extract_data(node) ⇒ String?

Extract text content from a node

Parameters:

  • node (Moxml::Node, Nokogiri::XML::Node)

    Node to extract from

Returns:

  • (String, nil)

    Text content or nil if not a text node



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/canon/comparison/dimensions/text_content_dimension.rb', line 23

def extract_data(node)
  return nil unless node

  # Handle Moxml nodes
  if node.is_a?(Moxml::Node)
    extract_from_moxml(node)
  # Handle Nokogiri nodes
  elsif node.is_a?(Nokogiri::XML::Node)
    extract_from_nokogiri(node)
  end
end