equivalent-xml 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,8 +1,3 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- group :development do
4
- gem "nokogiri"
5
- gem "bundler", "~> 1.0.0"
6
- gem "rcov", ">= 0"
7
- gem "rspec"
8
- end
3
+ gemspec
@@ -1,61 +1,63 @@
1
1
  module EquivalentXml
2
2
 
3
- class << self
3
+ ELEMENT_NODE = 1
4
+ ATTRIBUTE_NODE = 2
5
+ TEXT_NODE = 3
6
+ CDATA_SECTION_NODE = 4
7
+ ENTITY_REFERENCE_NODE = 5
8
+ ENTITY_NODE = 6
9
+ PROCESSING_INSTRUCTION_NODE = 7
10
+ COMMENT_NODE = 8
11
+ DOCUMENT_NODE = 9
12
+ DOCUMENT_TYPE_NODE = 10
13
+ DOCUMENT_FRAGMENT_NODE = 11
14
+ NOTATION_NODE = 12
4
15
 
5
- ELEMENT_NODE = 1
6
- ATTRIBUTE_NODE = 2
7
- TEXT_NODE = 3
8
- CDATA_SECTION_NODE = 4
9
- ENTITY_REFERENCE_NODE = 5
10
- ENTITY_NODE = 6
11
- PROCESSING_INSTRUCTION_NODE = 7
12
- COMMENT_NODE = 8
13
- DOCUMENT_NODE = 9
14
- DOCUMENT_TYPE_NODE = 10
15
- DOCUMENT_FRAGMENT_NODE = 11
16
- NOTATION_NODE = 12
16
+ class << self
17
17
 
18
18
  DEFAULT_OPTS = { :element_order => false, :normalize_whitespace => true }
19
19
 
20
- def equivalent?(node_1, node_2, opts = {})
20
+ def equivalent?(node_1, node_2, opts = {}, &block)
21
21
  opts = DEFAULT_OPTS.merge(opts)
22
- self.compare_nodes(node_1, node_2, opts)
22
+ self.compare_nodes(node_1, node_2, opts, &block)
23
23
  end
24
24
 
25
- def compare_nodes(node_1, node_2, opts)
25
+ def compare_nodes(node_1, node_2, opts, &block)
26
+ yield(node_1, node_2) if block_given?
27
+
26
28
  if (node_1.class != node_2.class) or self.same_namespace?(node_1,node_2) == false
27
29
  false
28
30
  else
29
31
  case node_1.node_type
30
32
  when DOCUMENT_NODE
31
- self.compare_documents(node_1,node_2,opts)
33
+ self.compare_documents(node_1,node_2,opts,&block)
32
34
  when ELEMENT_NODE
33
- self.compare_elements(node_1,node_2,opts)
35
+ self.compare_elements(node_1,node_2,opts,&block)
34
36
  when ATTRIBUTE_NODE
35
- self.compare_attributes(node_1,node_2,opts)
37
+ self.compare_attributes(node_1,node_2,opts,&block)
36
38
  when CDATA_SECTION_NODE
37
- self.compare_cdata(node_1,node_2,opts)
39
+ self.compare_cdata(node_1,node_2,opts,&block)
38
40
  when TEXT_NODE
39
- self.compare_text(node_1,node_2,opts)
41
+ self.compare_text(node_1,node_2,opts,&block)
40
42
  else
41
- self.compare_children(node_1,node_2,opts)
43
+ self.compare_children(node_1,node_2,opts,&block)
42
44
  end
43
45
  end
44
46
  end
45
47
 
46
- def compare_documents(node_1, node_2, opts)
47
- self.equivalent?(node_1.root,node_2.root,opts)
48
+ def compare_documents(node_1, node_2, opts, &block)
49
+ self.equivalent?(node_1.root,node_2.root,opts,&block)
48
50
  end
49
51
 
50
- def compare_elements(node_1, node_2, opts)
51
- (node_1.name == node_2.name) && self.compare_children(node_1,node_2,opts)
52
+ def compare_elements(node_1, node_2, opts, &block)
53
+ (node_1.name == node_2.name) && self.compare_children(node_1,node_2,opts,&block)
52
54
  end
53
55
 
54
- def compare_attributes(node_1, node_2, opts)
56
+ def compare_attributes(node_1, node_2, opts, &block)
55
57
  (node_1.name == node_2.name) && (node_1.value == node_2.value)
56
58
  end
57
59
 
58
- def compare_text(node_1, node_2, opts)
60
+ def compare_text(node_1, node_2, opts, &block)
59
61
  if opts[:normalize_whitespace]
60
62
  node_1.text.strip.gsub(/\s+/,' ') == node_2.text.strip.gsub(/\s+/,' ')
61
63
  else
@@ -63,11 +65,11 @@ module EquivalentXml
63
65
  end
64
66
  end
65
67
 
66
- def compare_cdata(node_1, node_2, opts)
68
+ def compare_cdata(node_1, node_2, opts, &block)
67
69
  node_1.text == node_2.text
68
70
  end
69
71
 
70
- def compare_children(node_1, node_2, opts)
72
+ def compare_children(node_1, node_2, opts, &block)
71
73
  ignore_proc = lambda do |child|
72
74
  child.is_a?(Nokogiri::XML::Comment) ||
73
75
  child.is_a?(Nokogiri::XML::ProcessingInstruction) ||
@@ -86,7 +88,7 @@ module EquivalentXml
86
88
  result
87
89
  end
88
90
 
89
- def compare_nodesets(nodeset_1, nodeset_2, opts)
91
+ def compare_nodesets(nodeset_1, nodeset_2, opts, &block)
90
92
  local_set_1 = nodeset_1.dup
91
93
  local_set_2 = nodeset_2.dup
92
94
 
@@ -95,7 +97,7 @@ module EquivalentXml
95
97
  end
96
98
 
97
99
  local_set_1.each do |search_node|
98
- found_node = local_set_2.find { |test_node| self.equivalent?(search_node,test_node,opts) }
100
+ found_node = local_set_2.find { |test_node| self.equivalent?(search_node,test_node,opts,&block) }
99
101
  if found_node.nil?
100
102
  return false
101
103
  else
@@ -98,4 +98,10 @@ describe EquivalentXml do
98
98
  EquivalentXml.equivalent?(doc1,doc2).should == true
99
99
  end
100
100
 
101
+ it "should properly handle a mixture of text and element nodes" do
102
+ doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><phrase>This phrase <b>has bold text</b> in it.</phrase></doc>")
103
+ doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><phrase>This phrase in <b>has bold text</b> it.</phrase></doc>")
104
+ EquivalentXml.equivalent?(doc1,doc2).should == false
105
+ end
106
+
101
107
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: equivalent-xml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael B. Klein