xpath 2.0.0 → 3.2.0
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 +7 -0
- data/README.md +6 -64
- data/lib/xpath/dsl.rb +141 -79
- data/lib/xpath/expression.rb +4 -2
- data/lib/xpath/literal.rb +2 -0
- data/lib/xpath/renderer.rb +34 -82
- data/lib/xpath/union.rb +4 -2
- data/lib/xpath/version.rb +3 -1
- data/lib/xpath.rb +4 -4
- data/spec/fixtures/simple.html +11 -3
- data/spec/spec_helper.rb +7 -0
- data/spec/union_spec.rb +15 -16
- data/spec/xpath_spec.rb +336 -123
- metadata +50 -80
- data/lib/xpath/html.rb +0 -175
- data/spec/html_spec.rb +0 -308
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -1
data/spec/union_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe XPath::Union do
|
@@ -5,60 +7,57 @@ describe XPath::Union do
|
|
5
7
|
let(:doc) { Nokogiri::HTML(template) }
|
6
8
|
|
7
9
|
describe '#expressions' do
|
8
|
-
it
|
10
|
+
it 'should return the expressions' do
|
9
11
|
@expr1 = XPath.generate { |x| x.descendant(:p) }
|
10
12
|
@expr2 = XPath.generate { |x| x.descendant(:div) }
|
11
13
|
@collection = XPath::Union.new(@expr1, @expr2)
|
12
|
-
@collection.expressions.should
|
14
|
+
@collection.expressions.should eq [@expr1, @expr2]
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
18
|
describe '#each' do
|
17
|
-
it
|
19
|
+
it 'should iterate through the expressions' do
|
18
20
|
@expr1 = XPath.generate { |x| x.descendant(:p) }
|
19
21
|
@expr2 = XPath.generate { |x| x.descendant(:div) }
|
20
22
|
@collection = XPath::Union.new(@expr1, @expr2)
|
21
23
|
exprs = []
|
22
24
|
@collection.each { |expr| exprs << expr }
|
23
|
-
exprs.should
|
25
|
+
exprs.should eq [@expr1, @expr2]
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
27
29
|
describe '#map' do
|
28
|
-
it
|
30
|
+
it 'should map the expressions' do
|
29
31
|
@expr1 = XPath.generate { |x| x.descendant(:p) }
|
30
32
|
@expr2 = XPath.generate { |x| x.descendant(:div) }
|
31
33
|
@collection = XPath::Union.new(@expr1, @expr2)
|
32
|
-
@collection.map
|
34
|
+
@collection.map(&:expression).should eq %i[descendant descendant]
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
38
|
describe '#to_xpath' do
|
37
|
-
it
|
39
|
+
it 'should create a valid xpath expression' do
|
38
40
|
@expr1 = XPath.generate { |x| x.descendant(:p) }
|
39
41
|
@expr2 = XPath.generate { |x| x.descendant(:div).where(x.attr(:id) == 'foo') }
|
40
42
|
@collection = XPath::Union.new(@expr1, @expr2)
|
41
43
|
@results = doc.xpath(@collection.to_xpath)
|
42
|
-
@results[0][:title].should
|
43
|
-
@results[1].text.should
|
44
|
-
@results[2].text.should
|
44
|
+
@results[0][:title].should eq 'fooDiv'
|
45
|
+
@results[1].text.should eq 'Blah'
|
46
|
+
@results[2].text.should eq 'Bax'
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
48
|
-
|
49
50
|
describe '#where and others' do
|
50
|
-
it
|
51
|
+
it 'should be delegated to the individual expressions' do
|
51
52
|
@expr1 = XPath.generate { |x| x.descendant(:p) }
|
52
53
|
@expr2 = XPath.generate { |x| x.descendant(:div) }
|
53
54
|
@collection = XPath::Union.new(@expr1, @expr2)
|
54
55
|
@xpath1 = @collection.where(XPath.attr(:id) == 'foo').to_xpath
|
55
56
|
@xpath2 = @collection.where(XPath.attr(:id) == 'fooDiv').to_xpath
|
56
57
|
@results = doc.xpath(@xpath1)
|
57
|
-
@results[0][:title].should
|
58
|
+
@results[0][:title].should eq 'fooDiv'
|
58
59
|
@results = doc.xpath(@xpath2)
|
59
|
-
@results[0][:id].should
|
60
|
+
@results[0][:id].should eq 'fooDiv'
|
60
61
|
end
|
61
62
|
end
|
62
|
-
|
63
63
|
end
|
64
|
-
|