Fixed CSS eval specs for nth-(first|last)-of-type.

This commit is contained in:
Yorick Peterse 2014-11-15 18:27:26 +01:00
parent b464815577
commit 448ff56e38
2 changed files with 71 additions and 51 deletions

View File

@ -3,48 +3,57 @@ require 'spec_helper'
describe 'CSS selector evaluation' do describe 'CSS selector evaluation' do
context ':nth-last-of-type pseudo class' do context ':nth-last-of-type pseudo class' do
before do before do
@document = parse('<root><a1 /><a2 /><a3 /><a4 /></root>') @document = parse(<<-EOF.strip)
<root>
<a id="1" />
<a id="2" />
<a id="3" />
<b>
<a id="4" />
</b>
</root>
EOF
@root = @document.children[0] @root = @document.children[0]
@a1 = @root.children[0] @a1 = @root.at_xpath('a[1]')
@a2 = @root.children[1] @a2 = @root.at_xpath('a[2]')
@a3 = @root.children[2] @a3 = @root.at_xpath('a[3]')
@a4 = @root.children[3] @a4 = @root.at_xpath('b/a')
end end
example 'return a node set containing the last node' do example 'return a node set containing the first child node' do
evaluate_css(@document, 'root :nth-last-of-type(1)') evaluate_css(@document, 'root a:nth-last-of-type(1)')
.should == node_set(@a4)
end
example 'return a node set containing even nodes' do
evaluate_css(@document, 'root :nth-last-of-type(even)')
.should == node_set(@a1, @a3)
end
example 'return a node set containing odd nodes' do
evaluate_css(@document, 'root :nth-last-of-type(odd)')
.should == node_set(@a2, @a4)
end
example 'return a node set containing every 2 nodes starting at node 3' do
evaluate_css(@document, 'root :nth-last-of-type(2n+2)')
.should == node_set(@a1, @a3)
end
example 'return a node set containing all nodes' do
evaluate_css(@document, 'root :nth-last-of-type(n)')
.should == @root.children
end
example 'return a node set containing the first two nodes' do
evaluate_css(@document, 'root :nth-last-of-type(-n+2)')
.should == node_set(@a3, @a4) .should == node_set(@a3, @a4)
end end
example 'return a node set containing even nodes' do
evaluate_css(@document, 'root a:nth-last-of-type(even)')
.should == node_set(@a2)
end
example 'return a node set containing odd nodes' do
evaluate_css(@document, 'root a:nth-last-of-type(odd)')
.should == node_set(@a1, @a3, @a4)
end
example 'return a node set containing every 2 nodes starting at node 2' do
evaluate_css(@document, 'root a:nth-last-of-type(2n+2)')
.should == node_set(@a2)
end
example 'return a node set containing all nodes' do
evaluate_css(@document, 'root a:nth-last-of-type(n)')
.should == node_set(@a1, @a2, @a3, @a4)
end
example 'return a node set containing the first two nodes' do
evaluate_css(@document, 'root a:nth-last-of-type(-n+2)')
.should == node_set(@a2, @a3, @a4)
end
example 'return a node set containing all nodes starting at node 2' do example 'return a node set containing all nodes starting at node 2' do
evaluate_css(@document, 'root :nth-last-of-type(n+2)') evaluate_css(@document, 'root a:nth-last-of-type(n+2)')
.should == node_set(@a1, @a2, @a3) .should == node_set(@a1, @a2)
end end
end end
end end

View File

@ -3,46 +3,57 @@ require 'spec_helper'
describe 'CSS selector evaluation' do describe 'CSS selector evaluation' do
context ':nth-of-type pseudo class' do context ':nth-of-type pseudo class' do
before do before do
@document = parse('<root><a1 /><a2 /><a3 /><a4 /></root>') @document = parse(<<-EOF.strip)
<root>
<a id="1" />
<a id="2" />
<a id="3" />
<b>
<a id="4" />
</b>
</root>
EOF
@root = @document.children[0] @root = @document.children[0]
@a1 = @root.children[0] @a1 = @root.at_xpath('a[1]')
@a2 = @root.children[1] @a2 = @root.at_xpath('a[2]')
@a3 = @root.children[2] @a3 = @root.at_xpath('a[3]')
@a4 = @root.children[3] @a4 = @root.at_xpath('b/a')
end end
example 'return a node set containing the first child node' do example 'return a node set containing the first child node' do
evaluate_css(@document, 'root :nth-of-type(1)').should == node_set(@a1) evaluate_css(@document, 'root a:nth-of-type(1)')
.should == node_set(@a1, @a4)
end end
example 'return a node set containing even nodes' do example 'return a node set containing even nodes' do
evaluate_css(@document, 'root :nth-of-type(even)') evaluate_css(@document, 'root a:nth-of-type(even)')
.should == node_set(@a2, @a4) .should == node_set(@a2)
end end
example 'return a node set containing odd nodes' do example 'return a node set containing odd nodes' do
evaluate_css(@document, 'root :nth-of-type(odd)') evaluate_css(@document, 'root a:nth-of-type(odd)')
.should == node_set(@a1, @a3) .should == node_set(@a1, @a3, @a4)
end end
example 'return a node set containing every 2 nodes starting at node 2' do example 'return a node set containing every 2 nodes starting at node 2' do
evaluate_css(@document, 'root :nth-of-type(2n+2)') evaluate_css(@document, 'root a:nth-of-type(2n+2)')
.should == node_set(@a2, @a4) .should == node_set(@a2)
end end
example 'return a node set containing all nodes' do example 'return a node set containing all nodes' do
evaluate_css(@document, 'root :nth-of-type(n)').should == @root.children evaluate_css(@document, 'root a:nth-of-type(n)')
.should == node_set(@a1, @a2, @a3, @a4)
end end
example 'return a node set containing the first two nodes' do example 'return a node set containing the first two nodes' do
evaluate_css(@document, 'root :nth-of-type(-n+2)') evaluate_css(@document, 'root a:nth-of-type(-n+2)')
.should == node_set(@a1, @a2) .should == node_set(@a1, @a2, @a4)
end end
example 'return a node set containing all nodes starting at node 2' do example 'return a node set containing all nodes starting at node 2' do
evaluate_css(@document, 'root :nth-of-type(n+2)') evaluate_css(@document, 'root a:nth-of-type(n+2)')
.should == node_set(@a2, @a3, @a4) .should == node_set(@a2, @a3)
end end
end end
end end