optical_diff 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -0
- data/lib/optical_diff.rb +11 -1
- data/lib/optical_diff/version.rb +1 -1
- data/spec/lib/optical_diff_spec.rb +20 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -18,6 +18,8 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
### Ignore specified html element by css selector or xpath
|
22
|
+
|
21
23
|
```ruby
|
22
24
|
html1 = "<html><body>text</body></html>"
|
23
25
|
html2 = "<html><body>text<p class = 'random_text'>qwerty</p></body></html>"
|
@@ -25,6 +27,17 @@ diff = OpticalDiff.diff(html1, html2, :ignore => 'p.random_text')
|
|
25
27
|
diff.changed? #=> false
|
26
28
|
```
|
27
29
|
|
30
|
+
### Replace text
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
html1 = "<html><body><a href='/link?abcde'>text</a></body></html>"
|
34
|
+
html2 = "<html><body><a href='/link?fghij'>text</a></body></html>"
|
35
|
+
diff = OpticalDiff.diff(html1, html2, :replace => [[/\?.+?'/, "'"]])
|
36
|
+
diff.changed? #=> false
|
37
|
+
```
|
38
|
+
|
39
|
+
``:replace`` is always processed before ``:ignore``
|
40
|
+
|
28
41
|
## Contributing
|
29
42
|
|
30
43
|
1. Fork it
|
data/lib/optical_diff.rb
CHANGED
@@ -6,14 +6,24 @@ class OpticalDiff
|
|
6
6
|
class << self
|
7
7
|
def diff(html1, html2, options = {})
|
8
8
|
options = {
|
9
|
+
:replace => [],
|
9
10
|
:ignore => []
|
10
11
|
}.merge(options)
|
11
12
|
|
12
|
-
|
13
|
+
replaced_htmls = [html1, html2].map {|html| replace(html, options[:replace]) }
|
14
|
+
parsed_htmls = replaced_htmls.map {|html| Nokogiri::HTML.parse(html) }
|
13
15
|
stripped_htmls = parsed_htmls.map {|html| strip_ignore_elements(html, options[:ignore]) }
|
14
16
|
DiffResult.new(Diffy::Diff.new(*stripped_htmls.map(&:to_html)))
|
15
17
|
end
|
16
18
|
|
19
|
+
def replace(page, patterns = [])
|
20
|
+
page = page.dup
|
21
|
+
patterns.each do |pattern, replace|
|
22
|
+
page.gsub!(pattern, replace)
|
23
|
+
end
|
24
|
+
page
|
25
|
+
end
|
26
|
+
|
17
27
|
def strip_ignore_elements(page, ignore = [])
|
18
28
|
ignore.each do |selector|
|
19
29
|
page.search(selector).remove
|
data/lib/optical_diff/version.rb
CHANGED
@@ -34,5 +34,25 @@ describe OpticalDiff do
|
|
34
34
|
subject { OpticalDiff.diff(html1, html3, :ignore => ['.ignore', '//p[contains(text(), "xpath")]']) }
|
35
35
|
it { should_not be_changed }
|
36
36
|
end
|
37
|
+
|
38
|
+
context 'by regex filter' do
|
39
|
+
let(:html3) { "<html><body>abcde<p class='ignore'>ignore</p><p>xpath</p></body></html>" }
|
40
|
+
subject { OpticalDiff.diff(html1, html2, :filter => [/ignore/], :ignore => ['.ignore', '//p[contains(text(), "xpath")]']) }
|
41
|
+
it { should_not be_changed }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'replace' do
|
46
|
+
let(:html1) { "<html><body><a href='/link?efghi'>abcde</a>zzzzz</body></html>" }
|
47
|
+
let(:html2) { "<html><body><a href='/link?jklmn'>abcde</a>zzzzz</body></html>" }
|
48
|
+
|
49
|
+
subject { OpticalDiff.diff(html1, html2, :replace => [[/\?.+?'/, "'"], [/zzzzz/, '']]) }
|
50
|
+
it { should_not be_changed }
|
51
|
+
|
52
|
+
context 'with ignore option' do
|
53
|
+
let(:html3) { "<html><body><a href='/link?opqrs'>abcde</a><p class='ignore'>ignore</p><p>xpath</p></body></html>" }
|
54
|
+
subject { OpticalDiff.diff(html1, html3, :replace => [[/\?.+?'/, "'"], [/zzzzz/, '']], :ignore => ['.ignore', '//p[contains(text(), "xpath")]']) }
|
55
|
+
it { should_not be_changed }
|
56
|
+
end
|
37
57
|
end
|
38
58
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optical_diff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Yusuke Mito
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-09-
|
18
|
+
date: 2012-09-25 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: diffy
|