Class: Canon::DiffFormatter::ByObject::XmlFormatter

Inherits:
BaseFormatter
  • Object
show all
Defined in:
lib/canon/diff_formatter/by_object/xml_formatter.rb

Overview

XML/HTML tree formatter for by-object diffs Handles DOM node differences with proper path extraction

Instance Attribute Summary

Attributes inherited from BaseFormatter

#use_color, #visualization_map

Instance Method Summary collapse

Methods inherited from BaseFormatter

for_format, #format, #initialize

Constructor Details

This class inherits a constructor from Canon::DiffFormatter::ByObject::BaseFormatter

Instance Method Details

#add_to_tree(tree, path, diff) ⇒ Object

Add a difference to the tree structure Handles DOM nodes by extracting their path

Parameters:

  • tree (Hash)

    Tree structure to add to

  • path (String, Array)

    Path to the difference

  • diff (Hash, DiffNode)

    Difference information



73
74
75
76
77
78
79
80
# File 'lib/canon/diff_formatter/by_object/xml_formatter.rb', line 73

def add_to_tree(tree, path, diff)
  # For DOM differences (Hash format), extract path from node
  if diff.is_a?(Hash) && !diff.key?(:path) && (diff[:node1] || diff[:node2])
    path = extract_dom_path(diff)
  end

  super
end

#render_diff_node(key, diff, prefix, connector) ⇒ String

Render a diff node for XML/HTML DOM differences

Parameters:

  • key (String)

    Node name or path segment

  • diff (Hash)

    Difference information

  • prefix (String)

    Tree prefix for indentation

  • connector (String)

    Box-drawing connector character

Returns:

  • (String)

    Formatted diff node



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/canon/diff_formatter/by_object/xml_formatter.rb', line 18

def render_diff_node(key, diff, prefix, connector)
  output = []

  # Show full path if available (path in cyan, no color on tree structure)
  path_display = if diff.is_a?(Hash) && diff[:path] && !diff[:path].empty?
                   colorize(diff[:path].to_s,
                            theme_color(:informative, :content) || :cyan, :bold)
                 else
                   colorize(key.to_s,
                            theme_color(:informative,
                                        :content) || :cyan)
                 end

  output << "#{prefix}#{connector}#{path_display}:"

  # Determine continuation for nested values
  continuation = connector.start_with?("") ? "" : "    "
  value_prefix = prefix + continuation

  # Handle both DiffNode and Hash formats
  if diff.is_a?(Canon::Diff::DiffNode)
    # DiffNode format - render based on dimension
    render_diffnode(diff, value_prefix, output)
  else
    # Hash format - use diff codes
    diff_code = diff[:diff_code] || diff[:diff1]

    case diff_code
    when Comparison::UNEQUAL_ELEMENTS
      render_unequal_elements(diff, value_prefix, output)
    when Comparison::UNEQUAL_TEXT_CONTENTS
      render_unequal_text(diff, value_prefix, output)
    when Comparison::UNEQUAL_ATTRIBUTES
      render_unequal_attributes(diff, value_prefix, output)
    when Comparison::MISSING_ATTRIBUTE
      render_missing_attribute(diff, value_prefix, output)
    when Comparison::UNEQUAL_COMMENTS
      render_unequal_comments(diff, value_prefix, output)
    when Comparison::MISSING_NODE
      render_missing_node(diff, value_prefix, output)
    else
      # Fallback for unknown diff types
      render_fallback(diff, value_prefix, output)
    end
  end

  output.join("\n")
end