Module: Canon::DiffFormatter::DiffDetailFormatterHelpers::DimensionFormatter

Defined in:
lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb

Overview

Dimension-specific formatting

Formats details for specific comparison dimensions.

Class Method Summary collapse

Class Method Details

.extract_dimension(diff) ⇒ Symbol

Extract dimension from diff

Parameters:

  • diff (DiffNode, Hash)

    Difference node

Returns:

  • (Symbol)

    Dimension



477
478
479
480
481
482
483
484
485
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 477

def self.extract_dimension(diff)
  if diff.respond_to?(:dimension)
    diff.dimension
  elsif diff.is_a?(Hash)
    diff[:dimension] || diff[:diff_code] || :unknown
  else
    :unknown
  end
end

.extract_namespace_declarations_from_node(node) ⇒ Hash

Extract namespace declarations from a node

Parameters:

  • node (Object)

    Node to extract from

Returns:

  • (Hash)

    Namespace declarations



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 515

def self.extract_namespace_declarations_from_node(node)
  return {} unless node

  declarations = {}

  # Handle Canon::Xml::Node (uses namespace_nodes)
  if node.respond_to?(:namespace_nodes)
    node.namespace_nodes.each do |ns|
      next if ns.prefix == "xml" && ns.uri == "http://www.w3.org/XML/1998/namespace"

      prefix = ns.prefix || ""
      declarations[prefix] = ns.uri
    end
    return declarations
  end

  # Handle Nokogiri/Moxml nodes (use attributes)
  raw_attrs = node.respond_to?(:attribute_nodes) ? node.attribute_nodes : node.attributes

  if raw_attrs.is_a?(Array)
    raw_attrs.each do |attr|
      name = attr.respond_to?(:name) ? attr.name : attr.to_s
      value = attr.respond_to?(:value) ? attr.value : attr.to_s

      if namespace_declaration?(name)
        prefix = name == "xmlns" ? "" : name.split(":", 2)[1]
        declarations[prefix] = value
      end
    end
  elsif raw_attrs.respond_to?(:each)
    raw_attrs.each do |key, val|
      name = if key.is_a?(String)
               key
             else
               (key.respond_to?(:name) ? key.name : key.to_s)
             end
      value = if val.respond_to?(:value)
                val.value
              elsif val.respond_to?(:content)
                val.content
              else
                val.to_s
              end

      if namespace_declaration?(name)
        prefix = name == "xmlns" ? "" : name.split(":", 2)[1]
        declarations[prefix] = value
      end
    end
  end

  declarations
end

.extract_node1(diff) ⇒ Object

Extract node1 from diff

Parameters:

  • diff (DiffNode, Hash)

    Difference node

Returns:

  • (Object)

    Node1



491
492
493
494
495
496
497
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 491

def self.extract_node1(diff)
  if diff.respond_to?(:node1)
    diff.node1
  elsif diff.is_a?(Hash)
    diff[:node1]
  end
end

.extract_node2(diff) ⇒ Object

Extract node2 from diff

Parameters:

  • diff (DiffNode, Hash)

    Difference node

Returns:

  • (Object)

    Node2



503
504
505
506
507
508
509
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 503

def self.extract_node2(diff)
  if diff.respond_to?(:node2)
    diff.node2
  elsif diff.is_a?(Hash)
    diff[:node2]
  end
end

.format_attribute_order_details(diff, use_color) ⇒ Array

Format attribute order differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 285

def self.format_attribute_order_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  order1 = NodeUtils.get_attribute_names_in_order(node1)
  order2 = NodeUtils.get_attribute_names_in_order(node2)

  detail1 = order1.map do |a|
    ColorHelper.colorize(a, :red, use_color)
  end.join(", ")
  detail2 = order2.map do |a|
    ColorHelper.colorize(a, :green, use_color)
  end.join(", ")

  changes = "Order differs: #{ColorHelper.colorize(order1.join(', '),
                                                   :red, use_color)}" \
            "#{ColorHelper.colorize(order2.join(', '), :green,
                                    use_color)}"

  [detail1, detail2, changes]
end

.format_attribute_presence_details(diff, use_color) ⇒ Array

Format attribute presence differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 191

def self.format_attribute_presence_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  attrs1 = NodeUtils.get_attribute_names(node1).sort
  attrs2 = NodeUtils.get_attribute_names(node2).sort

  missing = attrs1 - attrs2
  extra = attrs2 - attrs1

  # Format the attribute lists
  detail1 = if attrs1.empty?
              ColorHelper.colorize("(no attributes)", :red, use_color)
            else
              attrs1.map do |a|
                ColorHelper.colorize(a, :red, use_color)
              end.join(", ")
            end

  detail2 = if attrs2.empty?
              ColorHelper.colorize("(no attributes)", :green, use_color)
            else
              attrs2.map do |a|
                ColorHelper.colorize(a, :green, use_color)
              end.join(", ")
            end

  # Build changes description
  changes_parts = []
  if missing.any?
    missing_str = missing.map do |a|
      ColorHelper.colorize("-#{a}", :red, use_color)
    end.join(", ")
    changes_parts << "Missing: #{missing_str}"
  end
  if extra.any?
    extra_str = extra.map do |a|
      ColorHelper.colorize("+#{a}", :green, use_color)
    end.join(", ")
    changes_parts << "Extra: #{extra_str}"
  end

  changes = changes_parts.join(" | ")

  [detail1, detail2, changes]
end

.format_attribute_values_details(diff, use_color) ⇒ Array

Format attribute value differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 246

def self.format_attribute_values_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  differing = NodeUtils.find_all_differing_attributes(node1, node2)

  # Format all differing attributes
  attrs1_parts = []
  attrs2_parts = []
  changes_parts = []

  differing.each do |attr_name|
    val1 = NodeUtils.get_attribute_value(node1, attr_name)
    val2 = NodeUtils.get_attribute_value(node2, attr_name)

    attrs1_parts << "#{attr_name}=#{format_json_value(val1)}"
    attrs2_parts << "#{attr_name}=#{format_json_value(val2)}"
    changes_parts << "#{attr_name}: #{ColorHelper.colorize(
      format_json_value(val1), :red, use_color
    )}" \
                     "#{ColorHelper.colorize(format_json_value(val2),
                                             :green, use_color)}"
  end

  detail1 = attrs1_parts.join(", ")
  detail2 = attrs2_parts.join(", ")
  changes = changes_parts.join(" | ")

  [detail1, detail2, changes]
end

.format_comments_details(diff, use_color) ⇒ Array

Format comment differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 376

def self.format_comments_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  text1 = NodeUtils.get_node_text(node1)
  text2 = NodeUtils.get_node_text(node2)

  detail1 = ColorHelper.colorize(format_json_value(text1), :red,
                                 use_color)
  detail2 = ColorHelper.colorize(format_json_value(text2), :green,
                                 use_color)

  changes = "Comment differs: #{detail1}#{detail2}"

  [detail1, detail2, changes]
end

.format_dimension_details(diff, use_color) ⇒ String

Format dimension details based on diff type

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (String)

    Formatted dimension details



15
16
17
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
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 15

def self.format_dimension_details(diff, use_color)
  dimension = extract_dimension(diff)

  case dimension
  when :namespace_uri
    format_namespace_uri_details(diff, use_color)
  when :namespace_declarations
    format_namespace_declarations_details(diff, use_color)
  when :element_structure
    format_element_structure_details(diff, use_color)
  when :attribute_presence
    format_attribute_presence_details(diff, use_color)
  when :attribute_values
    format_attribute_values_details(diff, use_color)
  when :attribute_order
    format_attribute_order_details(diff, use_color)
  when :text_content
    format_text_content_details(diff, use_color)
  when :structural_whitespace
    format_structural_whitespace_details(diff, use_color)
  when :comments
    format_comments_details(diff, use_color)
  when :hash_diff
    format_hash_diff_details(diff, use_color)
  else
    format_fallback_details(diff, use_color)
  end
end

.format_element_structure_details(diff, use_color) ⇒ Array

Format element structure differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 166

def self.format_element_structure_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  name1 = NodeUtils.get_element_name_for_display(node1)
  name2 = NodeUtils.get_element_name_for_display(node2)

  detail1 = "<#{ColorHelper.colorize(name1, :red, use_color)}>"
  detail2 = "<#{ColorHelper.colorize(name2, :green, use_color)}>"

  changes = "Element differs: #{ColorHelper.colorize(name1, :red,
                                                     use_color)}" \
            "#{ColorHelper.colorize(name2, :green, use_color)}"

  [detail1, detail2, changes]
end

.format_fallback_details(diff, use_color) ⇒ Array

Format fallback details for unknown dimensions

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 434

def self.format_fallback_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  detail1 = ColorHelper.colorize(NodeUtils.format_node_brief(node1),
                                 :red, use_color)
  detail2 = ColorHelper.colorize(NodeUtils.format_node_brief(node2),
                                 :green, use_color)

  dimension = extract_dimension(diff)
  changes = "#{dimension.to_s.capitalize} differs: #{detail1}#{detail2}"

  [detail1, detail2, changes]
end

.format_hash_diff_details(diff, use_color) ⇒ Array

Format hash differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 401

def self.format_hash_diff_details(diff, use_color)
  require_relative "color_helper"

  detail1 = if diff.is_a?(Hash) && diff[:value1]
              ColorHelper.colorize(format_json_value(diff[:value1]),
                                   :red, use_color)
            else
              ColorHelper.colorize("(no value)", :red, use_color)
            end

  detail2 = if diff.is_a?(Hash) && diff[:value2]
              ColorHelper.colorize(format_json_value(diff[:value2]),
                                   :green, use_color)
            else
              ColorHelper.colorize("(no value)", :green, use_color)
            end

  path_str = if diff.is_a?(Hash) && diff[:path]
               " at #{diff[:path]}"
             else
               ""
             end

  changes = "Value differs#{path_str}: #{detail1}#{detail2}"

  [detail1, detail2, changes]
end

.format_json_value(value) ⇒ String

Format JSON value for display

Parameters:

  • value (Object)

    Value to format

Returns:

  • (String)

    Formatted value



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 456

def self.format_json_value(value)
  require "json"

  case value
  when String
    # Use JSON.generate for proper, well-tested escaping
    JSON.generate(value)
  when Numeric, TrueClass, FalseClass, NilClass
    value.to_s
  else
    # Use JSON.pretty_generate for complex types
    JSON.pretty_generate(value)
  end
rescue StandardError
  value.inspect
end

.format_namespace_declarations_details(diff, use_color) ⇒ Array

Format namespace declaration differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 83

def self.format_namespace_declarations_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"
  require_relative "text_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  # Extract namespace declarations from both nodes
  ns_decls1 = extract_namespace_declarations_from_node(node1)
  ns_decls2 = extract_namespace_declarations_from_node(node2)

  element_name = NodeUtils.get_element_name_for_display(node1) || "element"

  # Format namespace declarations for display
  detail1 = if ns_decls1.empty?
              "<#{element_name}> #{ColorHelper.colorize(
                '(no namespace declarations)', :red, use_color
              )}"
            else
              ns_str = ns_decls1.map do |prefix, uri|
                attr_name = prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
                "#{attr_name}=\"#{uri}\""
              end.join(" ")
              "<#{element_name}> #{ns_str}"
            end

  detail2 = if ns_decls2.empty?
              "<#{element_name}> #{ColorHelper.colorize(
                '(no namespace declarations)', :green, use_color
              )}"
            else
              ns_str = ns_decls2.map do |prefix, uri|
                attr_name = prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
                "#{attr_name}=\"#{uri}\""
              end.join(" ")
              "<#{element_name}> #{ns_str}"
            end

  # Analyze changes
  missing = ns_decls1.keys - ns_decls2.keys
  extra = ns_decls2.keys - ns_decls1.keys
  changed = ns_decls1.select do |k, v|
    ns_decls2[k] && ns_decls2[k] != v
  end.keys

  # Format changes
  changes_parts = []
  if missing.any?
    missing_str = missing.map do |prefix|
      attr_name = prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
      ColorHelper.colorize("-#{attr_name}=\"#{ns_decls1[prefix]}\"",
                           :red, use_color)
    end.join(", ")
    changes_parts << "Removed: #{missing_str}"
  end
  if extra.any?
    extra_str = extra.map do |prefix|
      attr_name = prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
      ColorHelper.colorize("+#{attr_name}=\"#{ns_decls2[prefix]}\"",
                           :green, use_color)
    end.join(", ")
    changes_parts << "Added: #{extra_str}"
  end
  if changed.any?
    changed_str = changed.map do |prefix|
      attr_name = prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
      "#{ColorHelper.colorize(attr_name, :cyan, use_color)}: " \
      "\"#{ns_decls1[prefix]}\" → \"#{ns_decls2[prefix]}\""
    end.join(", ")
    changes_parts << "Changed: #{changed_str}"
  end

  changes = changes_parts.join(" | ")

  [detail1, detail2, changes]
end

.format_namespace_uri_details(diff, use_color) ⇒ Array

Format namespace URI differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 49

def self.format_namespace_uri_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  # Use NamespaceHelper for consistent formatting
  ns1_display = Canon::Xml::NamespaceHelper.format_namespace(
    NodeUtils.get_namespace_uri_for_display(node1),
  )
  ns2_display = Canon::Xml::NamespaceHelper.format_namespace(
    NodeUtils.get_namespace_uri_for_display(node2),
  )

  element_name = NodeUtils.get_element_name_for_display(node1) || "element"

  detail1 = "<#{element_name}> #{ColorHelper.colorize(ns1_display,
                                                      :cyan, use_color)}"
  detail2 = "<#{element_name}> #{ColorHelper.colorize(ns2_display,
                                                      :cyan, use_color)}"

  changes = "Namespace differs: #{ColorHelper.colorize(ns1_display,
                                                       :red, use_color)}" \
            "#{ColorHelper.colorize(ns2_display, :green, use_color)}"

  [detail1, detail2, changes]
end

.format_structural_whitespace_details(diff, use_color) ⇒ Array

Format structural whitespace differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 350

def self.format_structural_whitespace_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"
  require_relative "text_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  text1 = NodeUtils.get_node_text(node1)
  text2 = NodeUtils.get_node_text(node2)

  detail1 = ColorHelper.colorize(TextUtils.visualize_whitespace(text1),
                                 :red, use_color)
  detail2 = ColorHelper.colorize(TextUtils.visualize_whitespace(text2),
                                 :green, use_color)

  changes = "Whitespace differs: #{detail1}#{detail2}"

  [detail1, detail2, changes]
end

.format_text_content_details(diff, use_color) ⇒ Array

Format text content differences

Parameters:

  • diff (DiffNode, Hash)

    Difference node

  • use_color (Boolean)

    Whether to use colors

Returns:

  • (Array)

    Tuple of [detail1, detail2, changes]



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 315

def self.format_text_content_details(diff, use_color)
  require_relative "color_helper"
  require_relative "node_utils"
  require_relative "text_utils"

  node1 = extract_node1(diff)
  node2 = extract_node2(diff)

  text1 = NodeUtils.get_node_text(node1)
  text2 = NodeUtils.get_node_text(node2)

  if NodeUtils.inside_preserve_element?(node1) || NodeUtils.inside_preserve_element?(node2)
    detail1 = ColorHelper.colorize(
      TextUtils.visualize_whitespace(text1), :red, use_color
    )
    detail2 = ColorHelper.colorize(
      TextUtils.visualize_whitespace(text2), :green, use_color
    )
  else
    detail1 = ColorHelper.colorize(format_json_value(text1), :red,
                                   use_color)
    detail2 = ColorHelper.colorize(format_json_value(text2), :green,
                                   use_color)
  end

  changes = "Content differs: #{detail1}#{detail2}"

  [detail1, detail2, changes]
end

.namespace_declaration?(name) ⇒ Boolean

Check if an attribute name is a namespace declaration

Parameters:

  • name (String)

    Attribute name

Returns:

  • (Boolean)

    true if namespace declaration



573
574
575
# File 'lib/canon/diff_formatter/diff_detail_formatter/dimension_formatter.rb', line 573

def self.namespace_declaration?(name)
  name == "xmlns" || name.to_s.start_with?("xmlns:")
end