compare-xml 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/compare-xml/version.rb +1 -1
- data/lib/compare-xml.rb +32 -21
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ed20155c59d477002fe69c2883e0a8d3b3b9507aa042f95d41cb3693b044e56d
|
|
4
|
+
data.tar.gz: 99c9374d3ac9bd98fe3e4b1fbad74b7bb4e1bea0f55ee475b41f401d8a60123e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ba79f1cd40d906b02752a50cdc37f7c82200077a7f1e7c9e5f530e046c67ad445a4830d87082715c9f96405363c116deb5cbc9d1df276906dc52dcd1098565d9
|
|
7
|
+
data.tar.gz: 9b25dfa936e79f2e15137ceece697f7f909344b628b79e51450598660989ca0b7604b6e44272fcbb4a19676ebd6eb719939e8b1c4b4a6d424edf57aac2786067
|
data/README.md
CHANGED
|
@@ -10,8 +10,6 @@ CompareXML is a fast, lightweight and feature-rich tool that will solve your XML
|
|
|
10
10
|
|
|
11
11
|
Supports **Ruby 2.4 → latest**, with the full Ruby matrix tested daily in CI.
|
|
12
12
|
|
|
13
|
-
[](https://www.buymeacoffee.com/vkononov)
|
|
14
|
-
|
|
15
13
|
**Features**
|
|
16
14
|
|
|
17
15
|
- Fast, light-weight and highly customizable
|
|
@@ -33,7 +31,9 @@ And then execute:
|
|
|
33
31
|
|
|
34
32
|
Or install it yourself as:
|
|
35
33
|
|
|
36
|
-
|
|
34
|
+
```sh
|
|
35
|
+
gem install compare-xml
|
|
36
|
+
```
|
|
37
37
|
|
|
38
38
|
|
|
39
39
|
|
data/lib/compare-xml/version.rb
CHANGED
data/lib/compare-xml.rb
CHANGED
|
@@ -107,6 +107,8 @@ module CompareXML
|
|
|
107
107
|
def compare_nodes(n1, n2, opts, differences, child_opts = {}, diff_children: false)
|
|
108
108
|
if n1.instance_of?(n2.class)
|
|
109
109
|
case n1
|
|
110
|
+
when Nokogiri::XML::ProcessingInstruction
|
|
111
|
+
status = compare_processing_instructions(n1, n2, opts, differences)
|
|
110
112
|
when Nokogiri::XML::Comment
|
|
111
113
|
status = compare_comment_nodes(n1, n2, opts, differences)
|
|
112
114
|
when Nokogiri::HTML::Document
|
|
@@ -137,6 +139,13 @@ module CompareXML
|
|
|
137
139
|
status
|
|
138
140
|
end
|
|
139
141
|
|
|
142
|
+
def compare_processing_instructions(n1, n2, opts, differences)
|
|
143
|
+
return EQUIVALENT if n1.name == n2.name && n1.content == n2.content
|
|
144
|
+
|
|
145
|
+
add_difference(n1, n2, n1, n2, opts, differences)
|
|
146
|
+
UNEQUAL_ELEMENTS
|
|
147
|
+
end
|
|
148
|
+
|
|
140
149
|
##
|
|
141
150
|
# Compares two nodes of type Nokogiri::HTML::Comment.
|
|
142
151
|
#
|
|
@@ -275,7 +284,7 @@ module CompareXML
|
|
|
275
284
|
end
|
|
276
285
|
|
|
277
286
|
##
|
|
278
|
-
# Builds a
|
|
287
|
+
# Builds a structured, option-aware fingerprint for a node so that two nodes with equal
|
|
279
288
|
# fingerprints are guaranteed to be equivalent under the current options. Used to
|
|
280
289
|
# anchor identical subtrees during alignment. Results are memoized per comparison.
|
|
281
290
|
#
|
|
@@ -285,19 +294,16 @@ module CompareXML
|
|
|
285
294
|
|
|
286
295
|
def build_node_fingerprint(node, opts, cache)
|
|
287
296
|
case node
|
|
288
|
-
when Nokogiri::XML::Comment
|
|
289
|
-
|
|
290
|
-
when Nokogiri::XML::Text
|
|
291
|
-
"T:#{opts[:collapse_whitespace] ? collapse(node.content) : node.content}"
|
|
297
|
+
when Nokogiri::XML::Comment, Nokogiri::XML::Text
|
|
298
|
+
[node.class, opts[:collapse_whitespace] ? collapse(node.content) : node.content]
|
|
292
299
|
when Nokogiri::XML::Element
|
|
293
|
-
attrs = node.attribute_nodes
|
|
294
|
-
attrs = attrs.sort_by(
|
|
295
|
-
|
|
296
|
-
children = node.children.reject { |
|
|
297
|
-
|
|
298
|
-
"E:#{element_name(node)}[#{attr_str}](#{child_str})"
|
|
300
|
+
attrs = node.attribute_nodes.reject { |attr| attribute_excluded?(attr, opts) }
|
|
301
|
+
attrs = attrs.sort_by { |attr| attribute_name(attr) } if opts[:ignore_attr_order]
|
|
302
|
+
attributes = attrs.map { |attr| [attribute_name(attr), attr.value] }
|
|
303
|
+
children = opts[:ignore_children] ? [] : node.children.reject { |child| node_excluded?(child, opts) }
|
|
304
|
+
[node.class, element_name(node), attributes, children.map { |child| node_fingerprint(child, opts, cache) }]
|
|
299
305
|
else
|
|
300
|
-
|
|
306
|
+
[node.class, node.to_s]
|
|
301
307
|
end
|
|
302
308
|
end
|
|
303
309
|
|
|
@@ -438,8 +444,8 @@ module CompareXML
|
|
|
438
444
|
# @return type of equivalence (from equivalence constants)
|
|
439
445
|
#
|
|
440
446
|
def compare_sorted_attribute_sets(n1, n2, a1_set, a2_set, opts, differences, status = EQUIVALENT)
|
|
441
|
-
a1_set = a1_set.sort_by(
|
|
442
|
-
a2_set = a2_set.sort_by(
|
|
447
|
+
a1_set = a1_set.sort_by { |attr| attribute_name(attr) }
|
|
448
|
+
a2_set = a2_set.sort_by { |attr| attribute_name(attr) }
|
|
443
449
|
i = j = 0
|
|
444
450
|
|
|
445
451
|
while i < a1_set.length || j < a2_set.length
|
|
@@ -450,10 +456,10 @@ module CompareXML
|
|
|
450
456
|
elsif a2_set[j].nil?
|
|
451
457
|
result = compare_attributes(n1, n2, a1_set[i], nil, opts, differences)
|
|
452
458
|
i += 1
|
|
453
|
-
elsif a1_set[i]
|
|
459
|
+
elsif attribute_name(a1_set[i]) < attribute_name(a2_set[j])
|
|
454
460
|
result = compare_attributes(n1, n2, a1_set[i], nil, opts, differences)
|
|
455
461
|
i += 1
|
|
456
|
-
elsif a1_set[i]
|
|
462
|
+
elsif attribute_name(a1_set[i]) > attribute_name(a2_set[j])
|
|
457
463
|
result = compare_attributes(n1, n2, nil, a2_set[j], opts, differences)
|
|
458
464
|
j += 1
|
|
459
465
|
else
|
|
@@ -493,6 +499,11 @@ module CompareXML
|
|
|
493
499
|
status
|
|
494
500
|
end
|
|
495
501
|
|
|
502
|
+
def attribute_name(attribute)
|
|
503
|
+
href = attribute.namespace&.href
|
|
504
|
+
href ? "{#{href}}#{attribute.name}" : attribute.name
|
|
505
|
+
end
|
|
506
|
+
|
|
496
507
|
##
|
|
497
508
|
# Compares two attributes by name and value.
|
|
498
509
|
#
|
|
@@ -509,18 +520,18 @@ module CompareXML
|
|
|
509
520
|
def compare_attributes(n1, n2, a1, a2, opts, differences, status = EQUIVALENT)
|
|
510
521
|
if a1.nil?
|
|
511
522
|
status = MISSING_ATTRIBUTE
|
|
512
|
-
add_difference(n1, n2, nil, "#{a2
|
|
523
|
+
add_difference(n1, n2, nil, "#{attribute_name(a2)}=\"#{a2.value}\"", opts, differences)
|
|
513
524
|
elsif a2.nil?
|
|
514
525
|
status = MISSING_ATTRIBUTE
|
|
515
|
-
add_difference(n1, n2, "#{a1
|
|
516
|
-
elsif a1
|
|
526
|
+
add_difference(n1, n2, "#{attribute_name(a1)}=\"#{a1.value}\"", nil, opts, differences)
|
|
527
|
+
elsif attribute_name(a1) == attribute_name(a2)
|
|
517
528
|
if a1.value != a2.value
|
|
518
529
|
status = UNEQUAL_ATTRIBUTES
|
|
519
|
-
add_difference(n1, n2, "#{a1
|
|
530
|
+
add_difference(n1, n2, "#{attribute_name(a1)}=\"#{a1.value}\"", "#{attribute_name(a2)}=\"#{a2.value}\"", opts, differences)
|
|
520
531
|
end
|
|
521
532
|
else
|
|
522
533
|
status = UNEQUAL_ATTRIBUTES
|
|
523
|
-
add_difference(n1, n2, "#{a1
|
|
534
|
+
add_difference(n1, n2, "#{attribute_name(a1)}=\"#{a1.value}\"", "#{attribute_name(a2)}=\"#{a2.value}\"", opts, differences)
|
|
524
535
|
end
|
|
525
536
|
status
|
|
526
537
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: compare-xml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vadim Kononov
|
|
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
57
57
|
- !ruby/object:Gem::Version
|
|
58
58
|
version: '0'
|
|
59
59
|
requirements: []
|
|
60
|
-
rubygems_version: 4.0.
|
|
60
|
+
rubygems_version: 4.0.20
|
|
61
61
|
specification_version: 4
|
|
62
62
|
summary: Compare XML and HTML documents or Nokogiri nodes for equality with a diff
|
|
63
63
|
test_files: []
|