nokogiri-diff 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +3 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +8 -0
- data/LICENSE.txt +20 -0
- data/README.md +74 -0
- data/Rakefile +35 -0
- data/gemspec.yml +23 -0
- data/lib/nokogiri/diff.rb +1 -0
- data/lib/nokogiri/diff/xml.rb +2 -0
- data/lib/nokogiri/diff/xml/document.rb +25 -0
- data/lib/nokogiri/diff/xml/node.rb +98 -0
- data/nokogiri-diff.gemspec +10 -0
- data/spec/diff_spec.rb +284 -0
- data/spec/spec_helper.rb +2 -0
- metadata +156 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --title "nokogiri-diff Documentation" --protected
|
data/ChangeLog.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
### 0.1.0 / 2010-11-29
|
2
|
+
|
3
|
+
* Initial release:
|
4
|
+
* Performs a breadth-first comparison between children nodes.
|
5
|
+
* Compares XML/HTML Elements, Attributes, Text nodes and DTD nodes.
|
6
|
+
* Allows calculating differences between documents, or just enumerating
|
7
|
+
the added or removed nodes.
|
8
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Hal Brodigan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# nokogiri-diff
|
2
|
+
|
3
|
+
* [Source](http://github.com/postmodern/nokogiri-diff)
|
4
|
+
* [Issues](http://github.com/postmodern/nokogiri-diff/issues)
|
5
|
+
* [Documentation](http://rubydoc.info/gems/nokogiri-diff/frames)
|
6
|
+
* Postmodern (postmodern.mod3 at gmail.com)
|
7
|
+
|
8
|
+
## Description
|
9
|
+
|
10
|
+
nokogiri-diff adds the ability to calculate the differences (added or
|
11
|
+
removed nodes) between two XML/HTML documents.
|
12
|
+
|
13
|
+
## Features
|
14
|
+
|
15
|
+
* Performs a breadth-first comparison between children nodes.
|
16
|
+
* Compares XML/HTML Elements, Attributes, Text nodes and DTD nodes.
|
17
|
+
* Allows calculating differences between documents, or just enumerating the
|
18
|
+
added or removed nodes.
|
19
|
+
|
20
|
+
## Examples
|
21
|
+
|
22
|
+
Enumerate over the the differences between two HTML documents:
|
23
|
+
|
24
|
+
require 'nokogiri/diff'
|
25
|
+
|
26
|
+
doc1 = Nokogiri::HTML('<div><p>one</p> two </div>')
|
27
|
+
doc2 = Nokogiri::HTML('<div><p id="1">one</p> <p>three</p></div>')
|
28
|
+
|
29
|
+
doc1.diff(doc2) do |change,node|
|
30
|
+
puts "#{change} #{node.to_html}".ljust(30) + node.parent.path
|
31
|
+
end
|
32
|
+
|
33
|
+
# <div>
|
34
|
+
# <p>one</p> two </div> /
|
35
|
+
# <p>one</p> /div
|
36
|
+
# - two /div
|
37
|
+
# + /div
|
38
|
+
# + <p>three</p> /div
|
39
|
+
# + id="1" /div/p[1]
|
40
|
+
# one /div/p
|
41
|
+
|
42
|
+
Only find the added nodes:
|
43
|
+
|
44
|
+
doc1.diff(doc2, :added => true) do |change,node|
|
45
|
+
puts node.to_html.ljust(30) + node.parent.path
|
46
|
+
end
|
47
|
+
|
48
|
+
# /div
|
49
|
+
# <p>three</p> /div
|
50
|
+
# id="1" /div/p[1]
|
51
|
+
|
52
|
+
Only find the removed nodes:
|
53
|
+
|
54
|
+
doc1.diff(doc2, :removed => true) do |change,node|
|
55
|
+
puts node.to_html.ljust(30) + node.parent.path
|
56
|
+
end
|
57
|
+
|
58
|
+
# two /div
|
59
|
+
|
60
|
+
## Requirements
|
61
|
+
|
62
|
+
* [ruby](http://www.ruby-lang.org/) >= 1.8.7
|
63
|
+
* [tdiff](http://github.com/postmodern/tdiff) ~> 0.3.2
|
64
|
+
* [nokogiri](http://nokogiri.rubyforge.org/) ~> 1.4.1
|
65
|
+
|
66
|
+
## Install
|
67
|
+
|
68
|
+
$ gem install nokogiri-diff
|
69
|
+
|
70
|
+
## Copyright
|
71
|
+
|
72
|
+
Copyright (c) 2010 Hal Brodigan
|
73
|
+
|
74
|
+
See {file:LICENSE.txt} for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
gem 'ore-tasks', '~> 0.3.0'
|
6
|
+
require 'ore/tasks'
|
7
|
+
|
8
|
+
Ore::Tasks.new
|
9
|
+
rescue LoadError => e
|
10
|
+
STDERR.puts e.message
|
11
|
+
STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
|
12
|
+
end
|
13
|
+
|
14
|
+
begin
|
15
|
+
gem 'rspec', '~> 2.2.0'
|
16
|
+
require 'rspec/core/rake_task'
|
17
|
+
|
18
|
+
RSpec::Core::RakeTask.new
|
19
|
+
rescue LoadError => e
|
20
|
+
task :spec do
|
21
|
+
abort "Please run `gem install rspec` to install RSpec."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
task :default => :spec
|
25
|
+
|
26
|
+
begin
|
27
|
+
gem 'yard', '~> 0.6.0'
|
28
|
+
require 'yard'
|
29
|
+
|
30
|
+
YARD::Rake::YardocTask.new
|
31
|
+
rescue LoadError => e
|
32
|
+
task :yard do
|
33
|
+
abort "Please run `gem install yard` to install YARD."
|
34
|
+
end
|
35
|
+
end
|
data/gemspec.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
name: nokogiri-diff
|
2
|
+
version: 0.1.0
|
3
|
+
summary: Calculate the differences between two XML/HTML documents.
|
4
|
+
description:
|
5
|
+
Nokogiri::Diff adds the ability to calculate the differences (added
|
6
|
+
or removed nodes) between two XML/HTML documents.
|
7
|
+
|
8
|
+
license: MIT
|
9
|
+
authors: Postmodern
|
10
|
+
email: postmodern.mod3@gmail.com
|
11
|
+
homepage: http://github.com/postmodern/nokogiri-diff
|
12
|
+
has_yard: true
|
13
|
+
|
14
|
+
required_ruby_version: >= 1.8.7
|
15
|
+
|
16
|
+
dependencies:
|
17
|
+
tdiff: ~> 0.3.2
|
18
|
+
nokogiri: ~> 1.4.1
|
19
|
+
|
20
|
+
development_dependencies:
|
21
|
+
ore-tasks: ~> 0.3.0
|
22
|
+
rspec: ~> 2.2.0
|
23
|
+
yard: ~> 0.6.0
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'nokogiri/diff/xml'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'nokogiri/diff/xml/node'
|
2
|
+
|
3
|
+
class Nokogiri::XML::Document < Nokogiri::XML::Node
|
4
|
+
|
5
|
+
#
|
6
|
+
# Overrides `tdiff` to only compare the child nodes of the document.
|
7
|
+
#
|
8
|
+
def tdiff(tree,&block)
|
9
|
+
return enum_for(:tdiff,tree) unless block
|
10
|
+
|
11
|
+
tdiff_recursive(tree,&block)
|
12
|
+
return self
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# Overrides `tdiff_unordered` to only compare the child nodes of the document.
|
17
|
+
#
|
18
|
+
def tdiff_unordered(tree,&block)
|
19
|
+
return enum_for(:tdiff_unordered,tree) unless block
|
20
|
+
|
21
|
+
tdiff_recursive_unordered(tree,&block)
|
22
|
+
return self
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'tdiff'
|
3
|
+
|
4
|
+
class Nokogiri::XML::Node
|
5
|
+
|
6
|
+
include TDiff
|
7
|
+
include TDiff::Unordered
|
8
|
+
|
9
|
+
#
|
10
|
+
# Compares two XML/HTML nodes.
|
11
|
+
#
|
12
|
+
# @param [Nokogiri::XML::Node] node1
|
13
|
+
#
|
14
|
+
# @param [Nokogiri::XML::Node] node2
|
15
|
+
#
|
16
|
+
# @return [Boolean]
|
17
|
+
# Specifies whether the two nodes are equal.
|
18
|
+
#
|
19
|
+
def tdiff_equal(node)
|
20
|
+
if (self.class == node.class)
|
21
|
+
case node
|
22
|
+
when Nokogiri::XML::Attr
|
23
|
+
(self.name == node.name && self.value == node.value)
|
24
|
+
when Nokogiri::XML::Element, Nokogiri::XML::DTD
|
25
|
+
self.name == node.name
|
26
|
+
when Nokogiri::XML::Text
|
27
|
+
self.text == node.text
|
28
|
+
else
|
29
|
+
false
|
30
|
+
end
|
31
|
+
else
|
32
|
+
false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# Enumerates over the children of a XML/HTML node.
|
38
|
+
#
|
39
|
+
# @param [Nokogiri::XML::Node] node
|
40
|
+
#
|
41
|
+
# @yield [child]
|
42
|
+
# The given block will be passed every child of the node.
|
43
|
+
#
|
44
|
+
# @yieldparam [Nokogiri::XML::Node] node
|
45
|
+
# A child node.
|
46
|
+
#
|
47
|
+
def tdiff_each_child(node,&block)
|
48
|
+
if node.kind_of?(Nokogiri::XML::Element)
|
49
|
+
node.attribute_nodes.each(&block)
|
50
|
+
end
|
51
|
+
|
52
|
+
node.children.each(&block)
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Finds the differences between the node and another node.
|
57
|
+
#
|
58
|
+
# @param [Nokogiri::XML::Node] other
|
59
|
+
# The other node to compare against.
|
60
|
+
#
|
61
|
+
# @param [Hash] options
|
62
|
+
# Additional options for filtering changes.
|
63
|
+
#
|
64
|
+
# @option options [Boolean] :added
|
65
|
+
# Yield nodes that were added.
|
66
|
+
#
|
67
|
+
# @option options [Boolean] :removed
|
68
|
+
# Yield nodes that were removed.
|
69
|
+
#
|
70
|
+
# @yield [change, node]
|
71
|
+
# The given block will be passed each changed node.
|
72
|
+
#
|
73
|
+
# @yieldparam [' ', '-', '+'] change
|
74
|
+
# Indicates whether the node stayed the same, was removed or added.
|
75
|
+
#
|
76
|
+
# @yieldparam [Nokogiri::XML::Node] node
|
77
|
+
# The changed node.
|
78
|
+
#
|
79
|
+
# @return [Enumerator]
|
80
|
+
# If no block was given, an Enumerator object will be returned.
|
81
|
+
#
|
82
|
+
def diff(other,options={},&block)
|
83
|
+
return enum_for(:diff,other,options) unless block
|
84
|
+
|
85
|
+
if (options[:added] || options[:removed])
|
86
|
+
tdiff_unordered(other) do |change,node|
|
87
|
+
if (change == '+' && options[:added])
|
88
|
+
yield change, node
|
89
|
+
elsif (change == '-' && options[:removed])
|
90
|
+
yield change, node
|
91
|
+
end
|
92
|
+
end
|
93
|
+
else
|
94
|
+
tdiff(other,&block)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
data/spec/diff_spec.rb
ADDED
@@ -0,0 +1,284 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'nokogiri/diff'
|
3
|
+
|
4
|
+
describe "nokogiri/diff" do
|
5
|
+
let(:contents) { '<div><p>one</p></div>' }
|
6
|
+
let(:doc) { Nokogiri::XML(contents) }
|
7
|
+
|
8
|
+
let(:added_text) { Nokogiri::XML('<div><p>one</p>two</div>') }
|
9
|
+
let(:added_element) { Nokogiri::XML('<div><p>one</p><p>two</p></div>') }
|
10
|
+
let(:added_attr) { Nokogiri::XML('<div><p id="1">one</p></div>') }
|
11
|
+
|
12
|
+
let(:changed_text) { Nokogiri::XML('<div><p>two</p></div>') }
|
13
|
+
let(:changed_element) { Nokogiri::XML('<div><span>one</span></div>') }
|
14
|
+
let(:changed_attr_name) { Nokogiri::XML('<div><p i="1">one</p></div>') }
|
15
|
+
let(:changed_attr_value) { Nokogiri::XML('<div><p id="2">one</p></div>') }
|
16
|
+
|
17
|
+
let(:removed_text) { Nokogiri::XML('<div><p></p>two</div>') }
|
18
|
+
let(:removed_element) { Nokogiri::XML('<div></div>') }
|
19
|
+
let(:removed_attr) { Nokogiri::XML('<div><p>one</p></div>') }
|
20
|
+
|
21
|
+
it "should add #diff to Nokogiri::XML::Docuemnt" do
|
22
|
+
doc.should respond_to(:diff)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should add #diff to Nokogiri::XML::Element" do
|
26
|
+
added_element.at('div').should respond_to(:diff)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should add #diff to Nokogiri::XML::Text" do
|
30
|
+
added_text.at('p/text()').should respond_to(:diff)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should add #diff to Nokogiri::XML::Attr" do
|
34
|
+
added_attr.at('p/@id').should respond_to(:diff)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not compare the Document objects" do
|
38
|
+
change = doc.diff(doc).first
|
39
|
+
|
40
|
+
change[0].should == ' '
|
41
|
+
change[1].should == doc.root
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should determine when two different documents are identical" do
|
45
|
+
doc.diff(Nokogiri::XML(contents)).all? { |change,node|
|
46
|
+
change == ' '
|
47
|
+
}.should == true
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should search down within Nokogiri::XML::Document objects" do
|
51
|
+
doc.diff(changed_text).any? { |change,node|
|
52
|
+
change != ' '
|
53
|
+
}.should == true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should determine when text nodes are added" do
|
57
|
+
changes = doc.at('div').diff(added_text.at('div')).to_a
|
58
|
+
|
59
|
+
changes.length.should == 4
|
60
|
+
|
61
|
+
changes[0][0].should == ' '
|
62
|
+
changes[0][1].should == doc.at('div')
|
63
|
+
|
64
|
+
changes[1][0].should == ' '
|
65
|
+
changes[1][1].should == doc.at('//p')
|
66
|
+
|
67
|
+
changes[2][0].should == '+'
|
68
|
+
changes[2][1].should == added_text.at('//div/text()')
|
69
|
+
|
70
|
+
changes[3][0].should == ' '
|
71
|
+
changes[3][1].should == doc.at('//p/text()')
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should determine when elements are added" do
|
75
|
+
changes = doc.at('div').diff(added_element.at('div')).to_a
|
76
|
+
|
77
|
+
changes.length.should == 5
|
78
|
+
|
79
|
+
changes[0][0].should == ' '
|
80
|
+
changes[0][1].should == doc.at('div')
|
81
|
+
|
82
|
+
changes[1][0].should == '+'
|
83
|
+
changes[1][1].should == added_element.at('//p[1]')
|
84
|
+
|
85
|
+
changes[2][0].should == ' '
|
86
|
+
changes[2][1].should == doc.at('//p')
|
87
|
+
|
88
|
+
changes[3][0].should == '-'
|
89
|
+
changes[3][1].should == doc.at('//p/text()')
|
90
|
+
|
91
|
+
changes[4][0].should == '+'
|
92
|
+
changes[4][1].should == added_element.at('//p[2]/text()')
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should determine when attributes are added" do
|
96
|
+
changes = doc.at('p').diff(added_attr.at('p')).to_a
|
97
|
+
|
98
|
+
changes.length.should == 3
|
99
|
+
|
100
|
+
changes[0][0].should == ' '
|
101
|
+
changes[0][1].should == doc.at('p')
|
102
|
+
|
103
|
+
changes[1][0].should == '+'
|
104
|
+
changes[1][1].should == added_attr.at('//p/@id')
|
105
|
+
|
106
|
+
changes[2][0].should == ' '
|
107
|
+
changes[2][1].should == doc.at('//p/text()')
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should determine when text nodes differ" do
|
111
|
+
changes = doc.at('p').diff(changed_text.at('p')).to_a
|
112
|
+
|
113
|
+
changes.length.should == 3
|
114
|
+
|
115
|
+
changes[0][0].should == ' '
|
116
|
+
changes[0][1].should == doc.at('p')
|
117
|
+
|
118
|
+
changes[1][0].should == '-'
|
119
|
+
changes[1][1].should == doc.at('//p/text()')
|
120
|
+
|
121
|
+
changes[2][0].should == '+'
|
122
|
+
changes[2][1].should == changed_text.at('//p/text()')
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should determine when element names differ" do
|
126
|
+
changes = doc.at('div').diff(changed_element.at('div')).to_a
|
127
|
+
|
128
|
+
changes.length.should == 3
|
129
|
+
|
130
|
+
changes[0][0].should == ' '
|
131
|
+
changes[0][1].should == doc.at('div')
|
132
|
+
|
133
|
+
changes[1][0].should == '-'
|
134
|
+
changes[1][1].should == doc.at('p')
|
135
|
+
|
136
|
+
changes[2][0].should == '+'
|
137
|
+
changes[2][1].should == changed_element.at('span')
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should determine when attribute names differ" do
|
141
|
+
changes = added_attr.at('p').diff(changed_attr_name.at('p')).to_a
|
142
|
+
|
143
|
+
changes.length.should == 4
|
144
|
+
|
145
|
+
changes[0][0].should == ' '
|
146
|
+
changes[0][1].should == added_attr.at('p')
|
147
|
+
|
148
|
+
changes[1][0].should == '-'
|
149
|
+
changes[1][1].should == added_attr.at('//p/@id')
|
150
|
+
|
151
|
+
changes[2][0].should == '+'
|
152
|
+
changes[2][1].should == changed_attr_name.at('//p/@i')
|
153
|
+
|
154
|
+
changes[3][0].should == ' '
|
155
|
+
changes[3][1].should == added_attr.at('//p/text()')
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should determine when attribute values differ" do
|
159
|
+
changes = added_attr.at('p').diff(changed_attr_value.at('p')).to_a
|
160
|
+
|
161
|
+
changes.length.should == 4
|
162
|
+
|
163
|
+
changes[0][0].should == ' '
|
164
|
+
changes[0][1].should == added_attr.at('p')
|
165
|
+
|
166
|
+
changes[1][0].should == '-'
|
167
|
+
changes[1][1].should == added_attr.at('//p/@id')
|
168
|
+
|
169
|
+
changes[2][0].should == '+'
|
170
|
+
changes[2][1].should == changed_attr_value.at('//p/@id')
|
171
|
+
|
172
|
+
changes[3][0].should == ' '
|
173
|
+
changes[3][1].should == added_attr.at('//p/text()')
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should determine when text nodes are removed" do
|
177
|
+
changes = added_text.at('div').diff(removed_text.at('div')).to_a
|
178
|
+
|
179
|
+
changes.length.should == 4
|
180
|
+
|
181
|
+
changes[0][0].should == ' '
|
182
|
+
changes[0][1].should == added_text.at('div')
|
183
|
+
|
184
|
+
changes[1][0].should == ' '
|
185
|
+
changes[1][1].should == added_text.at('p')
|
186
|
+
|
187
|
+
changes[2][0].should == ' '
|
188
|
+
changes[2][1].should == added_text.at('//div/text()')
|
189
|
+
|
190
|
+
changes[3][0].should == '-'
|
191
|
+
changes[3][1].should == added_text.at('//p/text()')
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should determine when elements are removed" do
|
195
|
+
changes = added_element.at('div').diff(removed_element.at('div')).to_a
|
196
|
+
|
197
|
+
changes.length.should == 3
|
198
|
+
|
199
|
+
changes[0][0].should == ' '
|
200
|
+
changes[0][1].should == added_element.at('div')
|
201
|
+
|
202
|
+
changes[1][0].should == '-'
|
203
|
+
changes[1][1].should == added_element.at('//p[1]')
|
204
|
+
|
205
|
+
changes[2][0].should == '-'
|
206
|
+
changes[2][1].should == added_element.at('//p[2]')
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should determine when attributes are removed" do
|
210
|
+
changes = added_attr.at('div').diff(removed_attr.at('div')).to_a
|
211
|
+
|
212
|
+
changes.length.should == 4
|
213
|
+
|
214
|
+
changes[0][0].should == ' '
|
215
|
+
changes[0][1].should == added_attr.at('div')
|
216
|
+
|
217
|
+
changes[1][0].should == ' '
|
218
|
+
changes[1][1].should == added_attr.at('p')
|
219
|
+
|
220
|
+
changes[2][0].should == '-'
|
221
|
+
changes[2][1].should == added_attr.at('//p/@id')
|
222
|
+
|
223
|
+
changes[3][0].should == ' '
|
224
|
+
changes[3][1].should == added_attr.at('//p/text()')
|
225
|
+
end
|
226
|
+
|
227
|
+
context ":added" do
|
228
|
+
it "should determine only when text nodes are added" do
|
229
|
+
changes = doc.at('div').diff(added_text.at('div'), :added => true).to_a
|
230
|
+
|
231
|
+
changes.length.should == 1
|
232
|
+
|
233
|
+
changes[0][0].should == '+'
|
234
|
+
changes[0][1].should == added_text.at('//div/text()')
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should determine only when elements are added" do
|
238
|
+
changes = doc.at('div').diff(added_element.at('div'), :added => true).to_a
|
239
|
+
|
240
|
+
changes.length.should == 1
|
241
|
+
|
242
|
+
changes[0][0].should == '+'
|
243
|
+
changes[0][1].should == added_element.at('//div/p[2]')
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should determine only when attributes are added" do
|
247
|
+
changes = doc.at('div').diff(added_attr.at('div'), :added => true).to_a
|
248
|
+
|
249
|
+
changes.length.should == 1
|
250
|
+
|
251
|
+
changes[0][0].should == '+'
|
252
|
+
changes[0][1].should == added_attr.at('//p/@id')
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
context ":removed" do
|
257
|
+
it "should determine only when text nodes are removed" do
|
258
|
+
changes = doc.at('div').diff(removed_text.at('div'), :removed => true).to_a
|
259
|
+
|
260
|
+
changes.length.should == 1
|
261
|
+
|
262
|
+
changes[0][0].should == '-'
|
263
|
+
changes[0][1].should == doc.at('//p/text()')
|
264
|
+
end
|
265
|
+
|
266
|
+
it "should determine only when elements are removed" do
|
267
|
+
changes = doc.at('div').diff(removed_element.at('div'), :removed => true).to_a
|
268
|
+
|
269
|
+
changes.length.should == 1
|
270
|
+
|
271
|
+
changes[0][0].should == '-'
|
272
|
+
changes[0][1].should == doc.at('//div/p')
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should determine only when attributes are removed" do
|
276
|
+
changes = added_attr.at('div').diff(removed_attr.at('div'), :removed => true).to_a
|
277
|
+
|
278
|
+
changes.length.should == 1
|
279
|
+
|
280
|
+
changes[0][0].should == '-'
|
281
|
+
changes[0][1].should == added_attr.at('//p/@id')
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nokogiri-diff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Postmodern
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-29 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: tdiff
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 3
|
31
|
+
- 2
|
32
|
+
version: 0.3.2
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: nokogiri
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 4
|
46
|
+
- 1
|
47
|
+
version: 1.4.1
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: ore-tasks
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
- 3
|
61
|
+
- 0
|
62
|
+
version: 0.3.0
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rspec
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 2
|
75
|
+
- 2
|
76
|
+
- 0
|
77
|
+
version: 2.2.0
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: yard
|
82
|
+
prerelease: false
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
- 6
|
91
|
+
- 0
|
92
|
+
version: 0.6.0
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id005
|
95
|
+
description: Nokogiri::Diff adds the ability to calculate the differences (added or removed nodes) between two XML/HTML documents.
|
96
|
+
email: postmodern.mod3@gmail.com
|
97
|
+
executables: []
|
98
|
+
|
99
|
+
extensions: []
|
100
|
+
|
101
|
+
extra_rdoc_files:
|
102
|
+
- README.md
|
103
|
+
- ChangeLog.md
|
104
|
+
- LICENSE.txt
|
105
|
+
files:
|
106
|
+
- .document
|
107
|
+
- .rspec
|
108
|
+
- .yardopts
|
109
|
+
- ChangeLog.md
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- gemspec.yml
|
114
|
+
- lib/nokogiri/diff.rb
|
115
|
+
- lib/nokogiri/diff/xml.rb
|
116
|
+
- lib/nokogiri/diff/xml/document.rb
|
117
|
+
- lib/nokogiri/diff/xml/node.rb
|
118
|
+
- nokogiri-diff.gemspec
|
119
|
+
- spec/diff_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
has_rdoc: yard
|
122
|
+
homepage: http://github.com/postmodern/nokogiri-diff
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
segments:
|
136
|
+
- 1
|
137
|
+
- 8
|
138
|
+
- 7
|
139
|
+
version: 1.8.7
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
segments:
|
146
|
+
- 0
|
147
|
+
version: "0"
|
148
|
+
requirements: []
|
149
|
+
|
150
|
+
rubyforge_project: nokogiri-diff
|
151
|
+
rubygems_version: 1.3.7
|
152
|
+
signing_key:
|
153
|
+
specification_version: 3
|
154
|
+
summary: Calculate the differences between two XML/HTML documents.
|
155
|
+
test_files:
|
156
|
+
- spec/diff_spec.rb
|