compare-xml 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '044582cb43241dc59a65422b8c36a2cc96d1a3120f9648c0950b7ef9fe125be5'
4
- data.tar.gz: 3a33391e6f5a67f305546173a43725a64bd6ed4df12135434453da9bc3982e98
3
+ metadata.gz: f7e6709bb5f5150832a22634027282fb7a2e80571f0a5c8210f7f8982f59bb67
4
+ data.tar.gz: ba0a234e1c095d4d0a97b72c22a8e6831d53b2203acf860965602dc81bdfbb85
5
5
  SHA512:
6
- metadata.gz: 0f3a32d528e4298a96ce59238f7ed556fa110ea7a4d715b651e1e578887f6bde9c6852928d0971c114e6ca9fb77dca8c4e6ca53c9580143cc5d89785ca14d942
7
- data.tar.gz: fc8371bdce1193d15d42092c24b9b44d2d9c51a685331102ba69fd9290cafdd6faf588feab68871b1c7659c42ed1f1656718b97b72fbf0702bde153e3a2caf30
6
+ metadata.gz: c2e9309f2980da1b6faa1e1ecea38293654492e04f216b1aa45e53c93817f2b394811511b4b233a8e74e3d498f6f71df0020b4d55635b5c95b6314aea7701f5b
7
+ data.tar.gz: ac6934cf5624163ea039cdd3985f07b28bd7f60e7f77e9329b70343d87b48e237b790e380316294293073a8d40de8f111d6f44addfb39a477d6b3f5389332f61
data/README.md CHANGED
@@ -1,12 +1,15 @@
1
1
  # CompareXML
2
2
 
3
- [![Gem Version](https://img.shields.io/gem/v/compare-xml.svg)](https://rubygems.org/gems/compare-xml)
4
- [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
- [![Lint Status](https://github.com/vkononov/compare-xml/actions/workflows/lint.yml/badge.svg)](https://github.com/vkononov/compare-xml/actions/workflows/lint.yml)
6
- [![Test Status](https://github.com/vkononov/compare-xml/actions/workflows/test.yml/badge.svg)](https://github.com/vkononov/compare-xml/actions/workflows/test.yml)
3
+ [![Gem Version](https://img.shields.io/gem/v/compare-xml.svg?label=Gem&logo=rubygems&logoColor=white)](https://rubygems.org/gems/compare-xml)
4
+ [![Ruby](https://img.shields.io/badge/Ruby-2.4%20to%204.0-CC342D?logo=ruby&logoColor=white)](https://github.com/vkononov/compare-xml/blob/main/.github/workflows/test.yml)
5
+ [![Test Matrix](https://img.shields.io/github/actions/workflow/status/vkononov/compare-xml/test.yml?branch=main&label=Test%20Matrix&logo=github)](https://github.com/vkononov/compare-xml/actions/workflows/test.yml)
6
+ [![Lint](https://img.shields.io/github/actions/workflow/status/vkononov/compare-xml/lint.yml?branch=main&label=Lint&logo=github)](https://github.com/vkononov/compare-xml/actions/workflows/lint.yml)
7
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/license/MIT)
7
8
 
8
9
  CompareXML is a fast, lightweight and feature-rich tool that will solve your XML/HTML comparison or diffing needs. Its purpose is to compare two instances of `Nokogiri::XML::Node` or `Nokogiri::XML::NodeSet` for equality or equivalency.
9
10
 
11
+ Supports **Ruby 2.4 → latest**, with the full Ruby matrix tested daily in CI.
12
+
10
13
  [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png)](https://www.buymeacoffee.com/vkononov)
11
14
 
12
15
  **Features**
@@ -49,8 +52,8 @@ where `doc1` and `doc2` are instances of `Nokogiri::XML::Node` or `Nokogiri::XML
49
52
  Suppose you have two files `1.html` and `2.html` that you would like to compare. You could do it as follows:
50
53
 
51
54
  ```ruby
52
- doc1 = Nokogiri::HTML(open('1.html'))
53
- doc2 = Nokogiri::HTML(open('2.html'))
55
+ doc1 = Nokogiri::HTML(File.read('1.html'))
56
+ doc2 = Nokogiri::HTML(File.read('2.html'))
54
57
  puts CompareXML.equivalent?(doc1, doc2)
55
58
  ```
56
59
 
@@ -81,7 +84,7 @@ CompareXML.equivalent?(doc1, doc2, {collapse_whitespace: false, verbose: true})
81
84
  - when provided, ignores all attributes that contain substrings `string`, `string2`, etc.
82
85
 
83
86
  - `ignore_attrs: [css_selector1, css_selector1, ...]` default: **`[]`**     [show examples ⇨](#ignore_attrs)
84
- - when provided, ignores specific *attributes* using [CSS selectors](http://www.w3schools.com/cssref/css_selectors.asp)
87
+ - when provided, ignores specific *attributes* using [CSS selectors](https://www.w3schools.com/cssref/css_selectors.php)
85
88
 
86
89
  - `ignore_attrs_by_name: [matcher1, matcher2, ...]` default: **`[]`**     [show examples ⇨](#ignore_attrs_by_name)
87
90
  - when provided, ignores *attributes* whose name matches a `String` (exact) or `Regexp` (pattern)
@@ -90,7 +93,7 @@ CompareXML.equivalent?(doc1, doc2, {collapse_whitespace: false, verbose: true})
90
93
  - when `true`, ignores comments, such as `<!-- comment -->`
91
94
 
92
95
  - `ignore_nodes: [css_selector1, css_selector1, ...]` default: **`[]`** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ⇨](#ignore_nodes)
93
- - when provided, ignores specific *nodes* using [CSS selectors](http://www.w3schools.com/cssref/css_selectors.asp)
96
+ - when provided, ignores specific *nodes* using [CSS selectors](https://www.w3schools.com/cssref/css_selectors.php)
94
97
 
95
98
  - `ignore_text_nodes: {true|false}` default: **`false`**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ⇨](#ignore_text_nodes)
96
99
  - when `true`, ignores all text content within a document
@@ -188,7 +191,7 @@ CompareXML.equivalent?(doc1, doc2, {collapse_whitespace: false, verbose: true})
188
191
 
189
192
  - <a id="ignore_attrs"></a>`ignore_attrs: [css_selector1, css_selector1, ...]` default: **`[]`**
190
193
 
191
- When provided, ignores all **attributes** that satisfy a particular rule using [CSS selectors](http://www.w3schools.com/cssref/css_selectors.asp).
194
+ When provided, ignores all **attributes** that satisfy a particular rule using [CSS selectors](https://www.w3schools.com/cssref/css_selectors.php).
192
195
 
193
196
  **Usage Example:** `CompareXML.equivalent?(doc1, doc2, {ignore_attrs: ['a[rel="nofollow"]', 'input[type="hidden"']})`
194
197
 
@@ -250,7 +253,7 @@ CompareXML.equivalent?(doc1, doc2, {collapse_whitespace: false, verbose: true})
250
253
 
251
254
  - <a id="ignore_nodes"></a>`ignore_nodes: [css_selector1, css_selector1, ...]` default: **`[]`**
252
255
 
253
- When provided, ignores all **nodes** that satisfy a particular rule using [CSS selectors](http://www.w3schools.com/cssref/css_selectors.asp).
256
+ When provided, ignores all **nodes** that satisfy a particular rule using [CSS selectors](https://www.w3schools.com/cssref/css_selectors.php).
254
257
 
255
258
  **Usage Example:** `CompareXML.equivalent?(doc1, doc2, {ignore_nodes: ['script', 'object']})`
256
259
 
@@ -382,6 +385,31 @@ CompareXML.equivalent?(doc1, doc2, {collapse_whitespace: false, verbose: true})
382
385
 
383
386
  ----------
384
387
 
388
+ ## Separate Options for Child Nodes
389
+
390
+ `CompareXML.equivalent?` accepts two further optional arguments that let child nodes be compared under a different set of options than the root:
391
+
392
+ ```ruby
393
+ CompareXML.equivalent?(doc1, doc2, opts = {}, child_opts = {}, diff_children = false)
394
+ ```
395
+
396
+ - `child_opts` - a second options hash, merged over the defaults the same way as `opts`, applied to child nodes
397
+ - `diff_children` - when `true`, child nodes are compared using `child_opts` instead of `opts`
398
+
399
+ `child_opts` is ignored unless `diff_children` is `true`.
400
+
401
+ **Example:** compare the root strictly, but ignore text content within child nodes:
402
+
403
+ ```ruby
404
+ doc1 = Nokogiri::HTML('<html><body><p>Hello</p></body></html>')
405
+ doc2 = Nokogiri::HTML('<html><body><p>Goodbye</p></body></html>')
406
+
407
+ CompareXML.equivalent?(doc1, doc2) # => false
408
+ CompareXML.equivalent?(doc1, doc2, {}, { ignore_text_nodes: true }, true) # => true
409
+ ```
410
+
411
+ ----------
412
+
385
413
  ## Contributing
386
414
 
387
415
  1. Fork it
@@ -400,4 +428,4 @@ This gem was inspired by [Michael B. Klein](https://github.com/mbklein)'s gem [`
400
428
 
401
429
  ## License
402
430
 
403
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
431
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/license/MIT).
@@ -1,3 +1,3 @@
1
1
  module CompareXML
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.0.1'.freeze
3
3
  end
data/lib/compare-xml.rb CHANGED
@@ -115,6 +115,8 @@ module CompareXML
115
115
  status = diff_children ? compare_element_nodes(n1, n2, opts, differences, child_opts, diff_children: diff_children) : compare_element_nodes(n1, n2, opts, differences)
116
116
  when Nokogiri::XML::Text
117
117
  status = compare_text_nodes(n1, n2, opts, differences)
118
+ when Nokogiri::XML::NodeSet
119
+ status = diff_children ? compare_children(n1, n2, child_opts, differences, diff_children: diff_children) : compare_children(n1, n2, opts, differences)
118
120
  else
119
121
  raise 'Comparison only allowed between objects of type Nokogiri::XML::Node and Nokogiri::XML::NodeSet.' unless n1.is_a?(Nokogiri::XML::Node) || n1.is_a?(Nokogiri::XML::NodeSet)
120
122
  status = compare_children(n1.children, n2.children, opts, differences)
@@ -293,7 +295,7 @@ module CompareXML
293
295
  attr_str = attrs.map { |a| "#{a.name}=#{a.value}" }.join(' ')
294
296
  children = node.children.reject { |c| node_excluded?(c, opts) }
295
297
  child_str = children.map { |c| node_fingerprint(c, opts, cache) }.join
296
- "E:#{node.name}[#{attr_str}](#{child_str})"
298
+ "E:#{element_name(node)}[#{attr_str}](#{child_str})"
297
299
  else
298
300
  "O:#{node.class}:#{node}"
299
301
  end
@@ -345,7 +347,7 @@ module CompareXML
345
347
  # @return type of equivalence (from equivalence constants)
346
348
  #
347
349
  def compare_element_nodes(n1, n2, opts, differences, child_opts = {}, diff_children: false, status: EQUIVALENT)
348
- if n1.name == n2.name
350
+ if element_name(n1) == element_name(n2)
349
351
  result = compare_attribute_sets(n1, n2, n1.attribute_nodes, n2.attribute_nodes, opts, differences)
350
352
  return result unless result == EQUIVALENT || opts[:force_children] == true
351
353
  status = result unless result == EQUIVALENT
@@ -353,11 +355,24 @@ module CompareXML
353
355
  status = result unless result == EQUIVALENT
354
356
  else
355
357
  status = UNEQUAL_ELEMENTS
356
- add_difference(n1, n2, n1.name, n2.name, opts, differences)
358
+ add_difference(n1, n2, element_name(n1), element_name(n2), opts, differences)
357
359
  end
358
360
  status
359
361
  end
360
362
 
363
+ ##
364
+ # Returns the element name qualified by its namespace URI, so that elements with
365
+ # the same local name in different namespaces are not treated as equal.
366
+ #
367
+ # @param [Nokogiri::XML::Element] node the element
368
+ #
369
+ # @return [String] the local name, prefixed with "{uri}" when namespaced
370
+ #
371
+ def element_name(node)
372
+ href = node.namespace&.href
373
+ href ? "{#{href}}#{node.name}" : node.name
374
+ end
375
+
361
376
  ##
362
377
  # Compares two nodes of type Nokogiri::XML::Text.
363
378
  #
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.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Kononov
@@ -23,9 +23,8 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '1.6'
26
- description: CompareXML is a fast, lightweight and feature-rich tool that will solve
27
- your XML/HTML comparison or diffing needs. its purpose is to compare two instances
28
- of Nokogiri::XML::Node or Nokogiri::XML::NodeSet for equality or equivalency.
26
+ description: Compares Nokogiri XML and HTML documents, nodes and node sets for equality
27
+ or equivalency. Returns a list of differences and can exclude chosen nodes or attributes.
29
28
  email:
30
29
  - vadim@konoson.com
31
30
  executables: []
@@ -60,6 +59,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
59
  requirements: []
61
60
  rubygems_version: 4.0.10
62
61
  specification_version: 4
63
- summary: A customizable tool that compares two instances of Nokogiri::XML::Node for
64
- equality or equivalency.
62
+ summary: Compare XML and HTML documents or Nokogiri nodes for equality with a diff
65
63
  test_files: []