Revamp descendant-or-self compiler specs
This commit is contained in:
parent
8f341b40d6
commit
d5aad9c1c9
|
@ -204,15 +204,17 @@ module Oga
|
||||||
# @param [Oga::Ruby::Node] input
|
# @param [Oga::Ruby::Node] input
|
||||||
# @return [Oga::Ruby::Node]
|
# @return [Oga::Ruby::Node]
|
||||||
def on_axis_descendant_or_self(ast, input)
|
def on_axis_descendant_or_self(ast, input)
|
||||||
node = node_literal
|
node = unique_literal(:descendant)
|
||||||
|
|
||||||
process(ast, input)
|
document_or_node(input).if_true do
|
||||||
.if_true { yield input }
|
process(ast, input)
|
||||||
.followed_by do
|
.if_true { yield input }
|
||||||
input.each_node.add_block(node) do
|
.followed_by do
|
||||||
process(ast, node).if_true { yield node }
|
input.each_node.add_block(node) do
|
||||||
|
process(ast, node).if_true { yield node }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [AST::Node] ast
|
# @param [AST::Node] ast
|
||||||
|
|
|
@ -1,90 +1,111 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Oga::XPath::Compiler do
|
describe Oga::XPath::Compiler do
|
||||||
describe 'descendant-or-self axis' do
|
before do
|
||||||
before do
|
@document = parse('<a foo="bar"><b><b><c class="x"></c></b></b></a>')
|
||||||
@document = parse('<a><b><b><c class="x"></c></b></b></a>')
|
|
||||||
|
|
||||||
@a1 = @document.children[0]
|
@a1 = @document.children[0]
|
||||||
@b1 = @a1.children[0]
|
@b1 = @a1.children[0]
|
||||||
@b2 = @b1.children[0]
|
@b2 = @b1.children[0]
|
||||||
@c1 = @b2.children[0]
|
@c1 = @b2.children[0]
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'using the full syntax' do
|
describe 'relative to a document' do
|
||||||
it 'returns a NodeSet containing a direct descendant' do
|
describe 'descendant-or-self::b' do
|
||||||
evaluate_xpath(@document, 'descendant-or-self::b')
|
it 'returns a NodeSet' do
|
||||||
.should == node_set(@b1, @b2)
|
evaluate_xpath(@document).should == node_set(@b1, @b2)
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet containing a nested descendant' do
|
|
||||||
evaluate_xpath(@document, 'descendant-or-self::c').should == node_set(@c1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet using multiple descendant expressions' do
|
|
||||||
evaluate_xpath(@document, 'descendant-or-self::a/descendant-or-self::*')
|
|
||||||
.should == node_set(@a1, @b1, @b2, @c1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet containing a descendant with an attribute' do
|
|
||||||
evaluate_xpath(@document, 'descendant-or-self::c[@class="x"]')
|
|
||||||
.should == node_set(@c1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet containing a descendant relative to a node' do
|
|
||||||
evaluate_xpath(@document, 'a/descendant-or-self::b')
|
|
||||||
.should == node_set(@b1, @b2)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet containing the context node' do
|
|
||||||
evaluate_xpath(@document, 'descendant-or-self::a')
|
|
||||||
.should == node_set(@a1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet containing the context node relative to a node' do
|
|
||||||
evaluate_xpath(@document, 'a/b/b/c/descendant-or-self::c')
|
|
||||||
.should == node_set(@c1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet containing the first descendant' do
|
|
||||||
evaluate_xpath(@document, 'descendant-or-self::b[1]')
|
|
||||||
.should == node_set(@b1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns an empty NodeSet for a non existing descendant' do
|
|
||||||
evaluate_xpath(@document, 'descendant-or-self::foobar')
|
|
||||||
.should == node_set
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'relative to a node' do
|
|
||||||
it 'returns a NodeSet containing a node and its descendants' do
|
|
||||||
evaluate_xpath(@b1, 'descendant-or-self::b')
|
|
||||||
.should == node_set(@b1, @b2)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a NodeSet containing only the node itself' do
|
|
||||||
evaluate_xpath(@c1, 'descendant-or-self::c').should == node_set(@c1)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'using the shorthand syntax' do
|
describe 'descendant-or-self::c' do
|
||||||
it 'returns a NodeSet containing all <b> nodes' do
|
it 'returns a NodeSet' do
|
||||||
evaluate_xpath(@document, '//b').should == node_set(@b1, @b2)
|
evaluate_xpath(@document).should == node_set(@c1)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'returns a NodeSet containing all the descendants of <a>' do
|
describe 'descendant-or-self::a/descendant-or-self::*' do
|
||||||
evaluate_xpath(@document, '//a//*').should == node_set(@b1, @b2, @c1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@a1, @b1, @b2, @c1)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'returns a NodeSet containing all <b> nodes in an <a> node' do
|
describe 'descendant-or-self::c[@class="x"]' do
|
||||||
evaluate_xpath(@document, '//a/b').should == node_set(@b1)
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@c1)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'relative to a node' do
|
describe 'a/descendant-or-self::b' do
|
||||||
it 'returns a NodeSet containing all <b> nodes' do
|
it 'returns a NodeSet' do
|
||||||
evaluate_xpath(@c1, '//b').should == node_set(@b1, @b2)
|
evaluate_xpath(@document).should == node_set(@b1, @b2)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'descendant-or-self::a' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@a1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'a/b/b/c/descendant-or-self::c' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@c1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'descendant-or-self::b[1]' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@b1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'descendant-or-self::foobar' do
|
||||||
|
it 'returns an empty NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '//b' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@b1, @b2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '//a//*' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@b1, @b2, @c1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '//a/b' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set(@b1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'a/@foo/descendant-or-self::*' do
|
||||||
|
it 'returns an empty NodeSet' do
|
||||||
|
evaluate_xpath(@document).should == node_set
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'relative to an element' do
|
||||||
|
describe 'descendant-or-self::b' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@b1).should == node_set(@b1, @b2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'descendant-or-self::c' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@c1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '//b' do
|
||||||
|
it 'returns a NodeSet' do
|
||||||
|
evaluate_xpath(@c1).should == node_set(@b1, @b2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue