compare-xml 0.62 → 0.65

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1978c95c8d1916bed53ccf29bf69092b8cba6eb8
4
- data.tar.gz: 905a08ef43f5d338e5e46b037772a608ae7660cd
3
+ metadata.gz: 48d97a18ea1ad3981e16acd41c03d8abf90c3996
4
+ data.tar.gz: ebfda907e73d068f77b6f6bf795207a707040a68
5
5
  SHA512:
6
- metadata.gz: bd58cfd19159ca82c7519e3cb0a39bc3f31c3497451c445bab3b68207ae43c1cb1032e0bfa3669f5bdc1ddc86276879e6208841e45be4edccc0e775945ca9e78
7
- data.tar.gz: 378902c6096df0533849c07158a1b599d872b8d3e6d23035f247eabcd736cbb505fe0dbed5cda2e88c7dae36fc65dd83f4e14b45a9d7115a19ef6199c6f46de0
6
+ metadata.gz: 8b2328fac7d1ac1672dd8ba64ecfce353681d2e07aba8fe632fb27281ea2c58267764d3e10108993a8f2f0914502b5137293ab1f0fed12442ef9bf8865cef2fe
7
+ data.tar.gz: 0e12b9c47b2c6d79ea6e826cee70d538310d9d0203d500a4856fb904794e115528698a7c848d2f5154be8f0bf6fd5d81279b6dafa555a8d6dbd2dfd9040cd52a
@@ -0,0 +1,42 @@
1
+ AllCops:
2
+ # Exclude:
3
+ # - config/routes.rb
4
+ # - db/migrate/*
5
+ # - db/schema.rb
6
+ TargetRubyVersion: 2.5
7
+
8
+ Metrics/AbcSize:
9
+ Enabled: false
10
+
11
+ Metrics/CyclomaticComplexity:
12
+ Enabled: false
13
+
14
+ Metrics/LineLength:
15
+ Enabled: false
16
+
17
+ Metrics/MethodLength:
18
+ Enabled: false
19
+
20
+ Metrics/ModuleLength:
21
+ Enabled: false
22
+
23
+ Metrics/ParameterLists:
24
+ Enabled: false
25
+
26
+ Metrics/PerceivedComplexity:
27
+ Enabled: false
28
+
29
+ Naming/FileName:
30
+ Enabled: false
31
+
32
+ Naming/MethodName:
33
+ Enabled: false
34
+
35
+ Style/Documentation:
36
+ Enabled: false
37
+
38
+ Style/FrozenStringLiteralComment:
39
+ Enabled: false
40
+
41
+ Style/Semicolon:
42
+ Enabled: false
data/README.md CHANGED
@@ -66,28 +66,28 @@ CompareXML has a variety of options that can be invoked as an optional argument,
66
66
  CompareXML.equivalent?(doc1, doc2, {collapse_whitespace: false, verbose: true, ...})
67
67
  ```
68
68
 
69
- - `collapse_whitespace: {true|false}` default: **`true`**     [show examples ](#collapse_whitespace)
69
+ - `collapse_whitespace: {true|false}` default: **`true`**     [show examples ](#collapse_whitespace)
70
70
  - when `true`, trims and collapses whitespace
71
71
 
72
- - `ignore_attr_order: {true|false}` default: **`true`**     [show examples ](#ignore_attr_order)
72
+ - `ignore_attr_order: {true|false}` default: **`true`**     [show examples ](#ignore_attr_order)
73
73
  - when `true`, ignores attribute order within tags
74
74
 
75
- - `ignore_attr_content: [string1, string2, ...]` default: **`[]`**     [show examples ](#ignore_attr_content)
75
+ - `ignore_attr_content: [string1, string2, ...]` default: **`[]`**     [show examples ](#ignore_attr_content)
76
76
  - when provided, ignores all attributes that contain substrings `string`, `string2`, etc.
77
77
 
78
- - `ignore_attrs: [css_selector1, css_selector1, ...]` default: **`[]`**     [show examples ](#ignore_attrs)
78
+ - `ignore_attrs: [css_selector1, css_selector1, ...]` default: **`[]`**     [show examples ](#ignore_attrs)
79
79
  - when provided, ignores specific *attributes* using [CSS selectors](http://www.w3schools.com/cssref/css_selectors.asp)
80
80
 
81
- - `ignore_comments: {true|false}` default: **`true`**     [show examples ](#ignore_comments)
81
+ - `ignore_comments: {true|false}` default: **`true`**     [show examples ](#ignore_comments)
82
82
  - when `true`, ignores comments, such as `<!-- comment -->`
83
83
 
84
- - `ignore_nodes: [css_selector1, css_selector1, ...]` default: **`[]`** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ](#ignore_nodes)
84
+ - `ignore_nodes: [css_selector1, css_selector1, ...]` default: **`[]`** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ](#ignore_nodes)
85
85
  - when provided, ignores specific *nodes* using [CSS selectors](http://www.w3schools.com/cssref/css_selectors.asp)
86
86
 
87
- - `ignore_text_nodes: {true|false}` default: **`false`**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ](#ignore_text_nodes)
87
+ - `ignore_text_nodes: {true|false}` default: **`false`**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ](#ignore_text_nodes)
88
88
  - when `true`, ignores all text content within a document
89
89
 
90
- - `verbose: {true|false}` default: **`false`**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ](#verbose)
90
+ - `verbose: {true|false}` default: **`false`**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[show examples ](#verbose)
91
91
  - when `true`, instead of a boolean, `CompareXML.equivalent?` returns an array of discrepancies.
92
92
 
93
93
 
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
1
  require 'bundler/gem_tasks'
2
- task :default => :spec
2
+ task default: :spec
@@ -11,4 +11,4 @@ require 'compare-xml/xml'
11
11
  # Pry.start
12
12
 
13
13
  require 'irb'
14
- IRB.start
14
+ IRB.start
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'compare-xml/version'
@@ -9,9 +8,9 @@ Gem::Specification.new do |spec|
9
8
  spec.authors = ['Vadim Kononov']
10
9
  spec.email = ['vadim@poetic.com']
11
10
 
12
- spec.summary = %q{A customizable tool that compares two instances of Nokogiri::XML::Node for equality or equivalency.}
13
- spec.description = %q{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.}
14
- spec.homepage = 'http://vkononov.github.io/compare-xml'
11
+ spec.summary = 'A customizable tool that compares two instances of Nokogiri::XML::Node for equality or equivalency.'
12
+ spec.description = '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.'
13
+ spec.homepage = 'https://github.com/vkononov/compare-xml'
15
14
  spec.license = 'MIT'
16
15
 
17
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -19,7 +18,5 @@ Gem::Specification.new do |spec|
19
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
19
  spec.require_paths = ['lib']
21
20
 
22
- spec.add_development_dependency 'bundler', '~> 1.11'
23
- spec.add_development_dependency 'rake', '~> 11.1'
24
- spec.add_runtime_dependency 'nokogiri', '~> 1.6'
21
+ spec.add_runtime_dependency 'nokogiri', '~> 1.8'
25
22
  end
@@ -2,45 +2,50 @@ require 'compare-xml/version'
2
2
  require 'nokogiri'
3
3
 
4
4
  module CompareXML
5
-
6
5
  # default options used by the module; all of these can be overridden
7
6
  DEFAULTS_OPTS = {
8
- # when true, trims and collapses whitespace in text nodes and comments to a single space
9
- # when false, all whitespace is preserved as it is without any changes
10
- collapse_whitespace: true,
7
+ # when true, trims and collapses whitespace in text nodes and comments to a single space
8
+ # when false, all whitespace is preserved as it is without any changes
9
+ collapse_whitespace: true,
11
10
 
12
- # when true, attribute order is not important (all attributes are sorted before comparison)
13
- # when false, attributes are compared in order and comparison stops on the first mismatch
14
- ignore_attr_order: true,
11
+ # when true, attribute order is not important (all attributes are sorted before comparison)
12
+ # when false, attributes are compared in order and comparison stops on the first mismatch
13
+ ignore_attr_order: true,
15
14
 
16
- # contains an array of user specified strings that is used to ignore any attributes
17
- # whose content contains a string from this array (e.g. "good automobile" contains "mobile")
18
- ignore_attr_content: [],
15
+ # when true, children of elements are always compared
16
+ # when false, children of elements are not compared if the root is different
17
+ force_children: false,
19
18
 
20
- # contains an array of user-specified CSS rules used to perform attribute exclusions
21
- # for this to work, a CSS rule MUST contain the attribute to be excluded,
22
- # i.e. a[href] will exclude all "href" attributes contained in <a> tags.
23
- ignore_attrs: [],
19
+ # contains an array of user specified strings that is used to ignore any attributes
20
+ # whose content contains a string from this array (e.g. "good automobile" contains "mobile")
21
+ ignore_attr_content: [],
24
22
 
25
- # when true ignores XML and HTML comments
26
- # when false, all comments are compared to their counterparts
27
- ignore_comments: true,
23
+ # contains an array of user-specified CSS rules used to perform attribute exclusions
24
+ # for this to work, a CSS rule MUST contain the attribute to be excluded,
25
+ # i.e. a[href] will exclude all "href" attributes contained in <a> tags.
26
+ ignore_attrs: [],
28
27
 
29
- # contains an array of user-specified CSS rules used to perform node exclusions
30
- ignore_nodes: [],
28
+ # contains an array of user specified strings that is used to ignore any attributes
29
+ # whose name contains a string from this array (e.g. "good automobile" contains "mobile")
30
+ ignore_attrs_by_name: [],
31
31
 
32
- # when true, ignores all text nodes (although blank text nodes are always ignored)
33
- # when false, all text nodes are compared to their counterparts (except the empty ones)
34
- ignore_text_nodes: false,
32
+ # when true ignores XML and HTML comments
33
+ # when false, all comments are compared to their counterparts
34
+ ignore_comments: true,
35
35
 
36
- # when true, provides a list of all error messages encountered in comparisons
37
- # when false, execution stops when the first error is encountered with no error messages
38
- verbose: false
39
- }
36
+ # contains an array of user-specified CSS rules used to perform node exclusions
37
+ ignore_nodes: [],
40
38
 
39
+ # when true, ignores all text nodes (although blank text nodes are always ignored)
40
+ # when false, all text nodes are compared to their counterparts (except the empty ones)
41
+ ignore_text_nodes: false,
41
42
 
42
- class << self
43
+ # when true, provides a list of all error messages encountered in comparisons
44
+ # when false, execution stops when the first error is encountered with no error messages
45
+ verbose: false
46
+ }.freeze
43
47
 
48
+ class << self
44
49
  # used internally only in order to differentiate equivalence for inequivalence
45
50
  EQUIVALENT = 1
46
51
 
@@ -63,16 +68,19 @@ module CompareXML
63
68
  # @param [Nokogiri::XML::Element] n1 left node element
64
69
  # @param [Nokogiri::XML::Element] n2 right node element
65
70
  # @param [Hash] opts user-overridden options
71
+ # @param [Hash] childopts user-overridden options used for the child nodes
72
+ # @param [Bool] diffchildren use different options for the child nodes
66
73
  #
67
74
  # @return true if equal, [Array] differences otherwise
68
75
  #
69
- def equivalent?(n1, n2, opts = {})
70
- opts, differences = DEFAULTS_OPTS.merge(opts), []
71
- result = compareNodes(n1, n2, opts, differences)
76
+ def equivalent?(n1, n2, opts = {}, childopts = {}, diffchildren = false)
77
+ opts = DEFAULTS_OPTS.merge(opts)
78
+ childopts = DEFAULTS_OPTS.merge(childopts)
79
+ differences = []
80
+ result = diffchildren ? compareNodes(n1, n2, opts, differences, childopts, diffchildren) : compareNodes(n1, n2, opts, differences)
72
81
  opts[:verbose] ? differences : result == EQUIVALENT
73
82
  end
74
83
 
75
-
76
84
  private
77
85
 
78
86
  ##
@@ -83,27 +91,26 @@ module CompareXML
83
91
  # @param [Nokogiri::XML::Node] n2 right node
84
92
  # @param [Hash] opts user-overridden options
85
93
  # @param [Array] differences inequivalence messages
94
+ # @param [Hash] childopts user-overridden options used for the child nodes
95
+ # @param [Bool] diffchildren use different options for the child nodes
86
96
  # @param [int] status comparison status code (EQUIVALENT by default)
87
97
  #
88
98
  # @return type of equivalence (from equivalence constants)
89
99
  #
90
- def compareNodes(n1, n2, opts, differences, status = EQUIVALENT)
100
+ def compareNodes(n1, n2, opts, differences, childopts = {}, diffchildren = false, status = EQUIVALENT)
91
101
  if n1.class == n2.class
92
102
  case n1
93
- when Nokogiri::XML::Comment
94
- compareCommentNodes(n1, n2, opts, differences)
95
- when Nokogiri::HTML::Document
96
- compareDocumentNodes(n1, n2, opts, differences)
97
- when Nokogiri::XML::Element
98
- status = compareElementNodes(n1, n2, opts, differences)
99
- when Nokogiri::XML::Text
100
- status = compareTextNodes(n1, n2, opts, differences)
101
- else
102
- if n1.is_a?(Nokogiri::XML::Node) || n1.is_a?(Nokogiri::XML::NodeSet)
103
- status = compareChildren(n1.children, n2.children, opts, differences)
104
- else
105
- raise 'Comparison only allowed between objects of type Nokogiri::XML::Node and Nokogiri::XML::NodeSet.'
106
- end
103
+ when Nokogiri::XML::Comment
104
+ status = compareCommentNodes(n1, n2, opts, differences)
105
+ when Nokogiri::HTML::Document
106
+ status = diffchildren ? compareDocumentNodes(n1, n2, opts, differences, childopts, diffchildren) : compareDocumentNodes(n1, n2, opts, differences)
107
+ when Nokogiri::XML::Element
108
+ status = diffchildren ? compareElementNodes(n1, n2, opts, differences, childopts, diffchildren) : compareElementNodes(n1, n2, opts, differences)
109
+ when Nokogiri::XML::Text
110
+ status = compareTextNodes(n1, n2, opts, differences)
111
+ else
112
+ 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)
113
+ status = compareChildren(n1.children, n2.children, opts, differences)
107
114
  end
108
115
  elsif n1.nil? || n2.nil?
109
116
  status = MISSING_NODE
@@ -121,7 +128,6 @@ module CompareXML
121
128
  status
122
129
  end
123
130
 
124
-
125
131
  ##
126
132
  # Compares two nodes of type Nokogiri::HTML::Comment.
127
133
  #
@@ -135,8 +141,12 @@ module CompareXML
135
141
  #
136
142
  def compareCommentNodes(n1, n2, opts, differences, status = EQUIVALENT)
137
143
  return true if opts[:ignore_comments]
138
- t1, t2 = n1.content, n2.content
139
- t1, t2 = collapse(t1), collapse(t2) if opts[:collapse_whitespace]
144
+ t1 = n1.content
145
+ t2 = n2.content
146
+ if opts[:collapse_whitespace]
147
+ t1 = collapse(t1)
148
+ t2 = collapse(t2)
149
+ end
140
150
  unless t1 == t2
141
151
  status = UNEQUAL_COMMENTS
142
152
  addDifference(n1.parent, n2.parent, t1, t2, opts, differences)
@@ -144,7 +154,6 @@ module CompareXML
144
154
  status
145
155
  end
146
156
 
147
-
148
157
  ##
149
158
  # Compares two nodes of type Nokogiri::HTML::Document.
150
159
  #
@@ -152,21 +161,22 @@ module CompareXML
152
161
  # @param [Nokogiri::XML::Document] n2 right document
153
162
  # @param [Hash] opts user-overridden options
154
163
  # @param [Array] differences inequivalence messages
164
+ # @param [Hash] childopts user-overridden options used for the child nodes
165
+ # @param [Bool] diffchildren use different options for the child nodes
155
166
  # @param [int] status comparison status code (EQUIVALENT by default)
156
167
  #
157
168
  # @return type of equivalence (from equivalence constants)
158
169
  #
159
- def compareDocumentNodes(n1, n2, opts, differences, status = EQUIVALENT)
170
+ def compareDocumentNodes(n1, n2, opts, differences, childopts = {}, diffchildren = false, status = EQUIVALENT)
160
171
  if n1.name == n2.name
161
- status = compareChildren(n1.children, n2.children, opts, differences)
172
+ status = diffchildren ? compareChildren(n1.children, n2.children, childopts, differences, diffchildren) : compareChildren(n1.children, n2.children, opts, differences)
162
173
  else
163
- status == UNEQUAL_DOCUMENTS
174
+ status = UNEQUAL_DOCUMENTS
164
175
  addDifference(n1, n2, n1, n2, opts, differences)
165
176
  end
166
177
  status
167
178
  end
168
179
 
169
-
170
180
  ##
171
181
  # Compares two sets of Nokogiri::XML::NodeSet elements.
172
182
  #
@@ -174,11 +184,12 @@ module CompareXML
174
184
  # @param [Nokogiri::XML::NodeSet] n2_set right set of Nokogiri::XML::Node elements
175
185
  # @param [Hash] opts user-overridden options
176
186
  # @param [Array] differences inequivalence messages
187
+ # @param [Bool] diffchildren use different options for the child nodes
177
188
  # @param [int] status comparison status code (EQUIVALENT by default)
178
189
  #
179
190
  # @return type of equivalence (from equivalence constants)
180
191
  #
181
- def compareChildren(n1_set, n2_set, opts, differences, status = EQUIVALENT)
192
+ def compareChildren(n1_set, n2_set, opts, differences, diffchildren = false, status = EQUIVALENT)
182
193
  i = 0; j = 0
183
194
  while i < n1_set.length || j < n2_set.length
184
195
  if !n1_set[i].nil? && nodeExcluded?(n1_set[i], opts)
@@ -186,12 +197,12 @@ module CompareXML
186
197
  elsif !n2_set[j].nil? && nodeExcluded?(n2_set[j], opts)
187
198
  j += 1 # increment counter if right node is excluded
188
199
  else
189
- result = compareNodes(n1_set[i], n2_set[j], opts, differences)
200
+ result = diffchildren ? compareNodes(n1_set[i], n2_set[j], opts, differences, opts, diffchildren) : compareNodes(n1_set[i], n2_set[j], opts, differences)
190
201
  status = result unless result == EQUIVALENT
191
202
 
192
203
  # return false so that this subtree could halt comparison on error
193
204
  # but neighbours of parents' subtrees could still be compared (in verbose mode)
194
- return false if status == UNEQUAL_NODES_TYPES || status == UNEQUAL_ELEMENTS
205
+ return false if [UNEQUAL_NODES_TYPES, UNEQUAL_ELEMENTS].include? status
195
206
 
196
207
  # stop execution if a single error is found (unless in verbose mode)
197
208
  break unless status == EQUIVALENT || opts[:verbose]
@@ -203,7 +214,6 @@ module CompareXML
203
214
  end
204
215
  end
205
216
 
206
-
207
217
  ##
208
218
  # Compares two nodes of type Nokogiri::XML::Element.
209
219
  # - compares element attributes
@@ -213,15 +223,17 @@ module CompareXML
213
223
  # @param [Nokogiri::XML::Element] n2 right node element
214
224
  # @param [Hash] opts user-overridden options
215
225
  # @param [Array] differences inequivalence messages
226
+ # @param [Hash] childopts user-overridden options used for the child nodes
227
+ # @param [Bool] diffchildren use different options for the child nodes
216
228
  # @param [int] status comparison status code (EQUIVALENT by default)
217
229
  #
218
230
  # @return type of equivalence (from equivalence constants)
219
231
  #
220
- def compareElementNodes(n1, n2, opts, differences, status = EQUIVALENT)
232
+ def compareElementNodes(n1, n2, opts, differences, childopts = {}, diffchildren = false, status = EQUIVALENT)
221
233
  if n1.name == n2.name
222
234
  result = compareAttributeSets(n1, n2, n1.attribute_nodes, n2.attribute_nodes, opts, differences)
223
- return result unless result == EQUIVALENT
224
- result = compareChildren(n1.children, n2.children, opts, differences)
235
+ return result unless result == EQUIVALENT || opts[:force_children] == true
236
+ result = diffchildren ? compareChildren(n1.children, n2.children, childopts, differences, diffchildren) : compareChildren(n1.children, n2.children, opts, differences)
225
237
  status = result unless result == EQUIVALENT
226
238
  else
227
239
  status = UNEQUAL_ELEMENTS
@@ -230,7 +242,6 @@ module CompareXML
230
242
  status
231
243
  end
232
244
 
233
-
234
245
  ##
235
246
  # Compares two nodes of type Nokogiri::XML::Text.
236
247
  #
@@ -244,8 +255,12 @@ module CompareXML
244
255
  #
245
256
  def compareTextNodes(n1, n2, opts, differences, status = EQUIVALENT)
246
257
  return true if opts[:ignore_text_nodes]
247
- t1, t2 = n1.content, n2.content
248
- t1, t2 = collapse(t1), collapse(t2) if opts[:collapse_whitespace]
258
+ t1 = n1.content
259
+ t2 = n2.content
260
+ if opts[:collapse_whitespace]
261
+ t1 = collapse(t1)
262
+ t2 = collapse(t2)
263
+ end
249
264
  unless t1 == t2
250
265
  status = UNEQUAL_TEXT_CONTENTS
251
266
  addDifference(n1.parent, n2.parent, t1, t2, opts, differences)
@@ -253,7 +268,6 @@ module CompareXML
253
268
  status
254
269
  end
255
270
 
256
-
257
271
  ##
258
272
  # Compares two sets of Nokogiri::XML::Element attributes.
259
273
  #
@@ -275,7 +289,6 @@ module CompareXML
275
289
  end
276
290
  end
277
291
 
278
-
279
292
  ##
280
293
  # Compares two sets of Nokogiri::XML::Node attributes by sorting them first.
281
294
  # When the attributes are sorted, only attributes of the same type are compared
@@ -292,7 +305,8 @@ module CompareXML
292
305
  # @return type of equivalence (from equivalence constants)
293
306
  #
294
307
  def compareSortedAttributeSets(n1, n2, a1_set, a2_set, opts, differences, status = EQUIVALENT)
295
- a1_set, a2_set = a1_set.sort_by { |a| a.name }, a2_set.sort_by { |a| a.name }
308
+ a1_set = a1_set.sort_by(&:name)
309
+ a2_set = a2_set.sort_by(&:name)
296
310
  i = j = 0
297
311
 
298
312
  while i < a1_set.length || j < a2_set.length
@@ -315,7 +329,6 @@ module CompareXML
315
329
  status
316
330
  end
317
331
 
318
-
319
332
  ##
320
333
  # Compares two sets of Nokogiri::XML::Element attributes without sorting them.
321
334
  # As a result attributes of different types may be compared, and even if all
@@ -341,7 +354,6 @@ module CompareXML
341
354
  status
342
355
  end
343
356
 
344
-
345
357
  ##
346
358
  # Compares two attributes by name and value.
347
359
  #
@@ -363,6 +375,7 @@ module CompareXML
363
375
  status = MISSING_ATTRIBUTE
364
376
  addDifference(n1, n2, "#{a1.name}=\"#{a1.value}\"", nil, opts, differences)
365
377
  elsif a1.name == a2.name
378
+ return status if attrNameExcluded?(a1.name, a2.name, opts)
366
379
  return status if attrsExcluded?(a1, a2, opts)
367
380
  return status if attrContentExcluded?(a1, a2, opts)
368
381
  if a1.value != a2.value
@@ -376,7 +389,6 @@ module CompareXML
376
389
  status
377
390
  end
378
391
 
379
-
380
392
  ##
381
393
  # Determines if a node should be excluded from the comparison. When a node is excluded,
382
394
  # it is completely ignored, as if it did not exist.
@@ -399,7 +411,6 @@ module CompareXML
399
411
  false
400
412
  end
401
413
 
402
-
403
414
  ##
404
415
  # Checks whether two given attributes should be excluded, based on a user-specified css rule.
405
416
  # If true, only the specified attributes are ignored; all remaining attributes are still compared.
@@ -421,7 +432,6 @@ module CompareXML
421
432
  false
422
433
  end
423
434
 
424
-
425
435
  ##
426
436
  # Checks whether two given attributes should be excluded, based on their content.
427
437
  # Checks whether both attributes contain content that should be excluded, and
@@ -434,15 +444,37 @@ module CompareXML
434
444
  # @return true if excluded, false otherwise
435
445
  #
436
446
  def attrContentExcluded?(a1, a2, opts)
437
- a1_excluded, a2_excluded = false, false
447
+ a1_excluded = false
448
+ a2_excluded = false
438
449
  opts[:ignore_attr_content].each do |content|
439
- a1_excluded = a1_excluded || a1.value.include?(content)
440
- a2_excluded = a2_excluded || a2.value.include?(content)
450
+ a1_excluded ||= a1.value.include?(content)
451
+ a2_excluded ||= a2.value.include?(content)
441
452
  return true if a1_excluded && a2_excluded
442
453
  end
443
454
  false
444
455
  end
445
456
 
457
+ ##
458
+ # Checks whether two given attributes should be excluded, based on their content.
459
+ # Checks whether both attributes contain content that should be excluded, and
460
+ # returns true only if an excluded string is contained in both attribute values.
461
+ #
462
+ # @param [Nokogiri::XML::Attr] a1 left attribute
463
+ # @param [Nokogiri::XML::Attr] a2 right attribute
464
+ # @param [Hash] opts user-overridden options
465
+ #
466
+ # @return true if excluded, false otherwise
467
+ #
468
+ def attrNameExcluded?(a1, a2, opts)
469
+ a1_excluded = false
470
+ a2_excluded = false
471
+ opts[:ignore_attrs_by_name].each do |name|
472
+ a1_excluded ||= a1.to_s.include?(name)
473
+ a2_excluded ||= a2.to_s.include?(name)
474
+ return true if a1_excluded && a2_excluded
475
+ end
476
+ false
477
+ end
446
478
 
447
479
  ##
448
480
  # Strips the whitespace (from beginning and end) and collapses it,
@@ -458,12 +490,9 @@ module CompareXML
458
490
  # @return collapsed string
459
491
  #
460
492
  def addDifference(node1, node2, diff1, diff2, opts, differences)
461
- if opts[:verbose]
462
- differences << {node1: node1, node2: node2, diff1: diff1, diff2: diff2}
463
- end
493
+ differences << { node1: node1, node2: node2, diff1: diff1, diff2: diff2 } if opts[:verbose]
464
494
  end
465
495
 
466
-
467
496
  ##
468
497
  # Strips the whitespace (from beginning and end) and collapses it,
469
498
  # i.e. multiple spaces, new lines and tabs are all collapsed to a single space.
@@ -476,7 +505,5 @@ module CompareXML
476
505
  text = text.to_s unless text.is_a? String
477
506
  text.strip.gsub(/\s+/, ' ')
478
507
  end
479
-
480
508
  end
481
-
482
- end
509
+ end
@@ -1,3 +1,3 @@
1
1
  module CompareXML
2
- VERSION = '0.62'
2
+ VERSION = '0.65'.freeze
3
3
  end
metadata CHANGED
@@ -1,57 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compare-xml
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.62'
4
+ version: '0.65'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Kononov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-10 00:00:00.000000000 Z
11
+ date: 2018-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.11'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.11'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '11.1'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '11.1'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: nokogiri
43
15
  requirement: !ruby/object:Gem::Requirement
44
16
  requirements:
45
17
  - - "~>"
46
18
  - !ruby/object:Gem::Version
47
- version: '1.6'
19
+ version: '1.8'
48
20
  type: :runtime
49
21
  prerelease: false
50
22
  version_requirements: !ruby/object:Gem::Requirement
51
23
  requirements:
52
24
  - - "~>"
53
25
  - !ruby/object:Gem::Version
54
- version: '1.6'
26
+ version: '1.8'
55
27
  description: CompareXML is a fast, lightweight and feature-rich tool that will solve
56
28
  your XML/HTML comparison or diffing needs. its purpose is to compare two instances
57
29
  of Nokogiri::XML::Node or Nokogiri::XML::NodeSet for equality or equivalency.
@@ -62,6 +34,7 @@ extensions: []
62
34
  extra_rdoc_files: []
63
35
  files:
64
36
  - ".gitignore"
37
+ - ".rubocop.yml"
65
38
  - Gemfile
66
39
  - LICENSE.txt
67
40
  - README.md
@@ -72,7 +45,7 @@ files:
72
45
  - img/diffing.png
73
46
  - lib/compare-xml.rb
74
47
  - lib/compare-xml/version.rb
75
- homepage: http://vkononov.github.io/compare-xml
48
+ homepage: https://github.com/vkononov/compare-xml
76
49
  licenses:
77
50
  - MIT
78
51
  metadata: {}
@@ -92,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
65
  version: '0'
93
66
  requirements: []
94
67
  rubyforge_project:
95
- rubygems_version: 2.5.2
68
+ rubygems_version: 2.6.13
96
69
  signing_key:
97
70
  specification_version: 4
98
71
  summary: A customizable tool that compares two instances of Nokogiri::XML::Node for