Reworked handling of relative vs absolute XPaths.
This commit is contained in:
parent
a733869f53
commit
2298ef618b
|
@ -16,31 +16,27 @@ preclow
|
|||
|
||||
rule
|
||||
xpath
|
||||
: expressions { s(:xpath, *val[0]) }
|
||||
| /* none */ { s(:xpath) }
|
||||
;
|
||||
|
||||
expressions
|
||||
: expressions expression { val[0] << val[1] }
|
||||
| expression { val }
|
||||
: T_SLASH expression { s(:absolute, val[1]) }
|
||||
| expression { val[0] }
|
||||
| /* none */ { nil }
|
||||
;
|
||||
|
||||
expression
|
||||
: path
|
||||
| node_test
|
||||
: node_tests
|
||||
| operator
|
||||
| axis
|
||||
| string
|
||||
| number
|
||||
;
|
||||
|
||||
path
|
||||
: T_SLASH node_test { s(:path, val[1]) }
|
||||
node_tests
|
||||
: node_test { val[0] }
|
||||
| node_test T_SLASH node_tests { val[0].append(val[2]) }
|
||||
;
|
||||
|
||||
node_test
|
||||
: node_name { s(:node_test, val[0]) }
|
||||
| node_name predicate { s(:node_test, val[0], *val[1]) }
|
||||
: node_name { s(:test, val[0]) }
|
||||
| node_name predicate { s(:test, val[0], val[1]) }
|
||||
;
|
||||
|
||||
node_name
|
||||
|
@ -52,7 +48,7 @@ rule
|
|||
;
|
||||
|
||||
predicate
|
||||
: T_LBRACK expressions T_RBRACK { val[1] }
|
||||
: T_LBRACK xpath T_RBRACK { s(:predicate, val[1]) }
|
||||
;
|
||||
|
||||
operator
|
||||
|
@ -61,7 +57,7 @@ rule
|
|||
;
|
||||
|
||||
axis
|
||||
: T_AXIS T_IDENT { s(:axis, val[0], val[1]) }
|
||||
: T_AXIS node_name { s(:axis, val[0], val[1]) }
|
||||
;
|
||||
|
||||
string
|
||||
|
|
|
@ -4,104 +4,92 @@ describe Oga::XPath::Parser do
|
|||
context 'full axes' do
|
||||
example 'parse the ancestor axis' do
|
||||
parse_xpath('/ancestor::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'ancestor', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'ancestor', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the ancestor-or-self axis' do
|
||||
parse_xpath('/ancestor-or-self::A').should == s(
|
||||
:xpath,
|
||||
s(
|
||||
:path,
|
||||
s(:node_test, s(:axis, 'ancestor-or-self', s(:name, nil, 'A')))
|
||||
)
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'ancestor-or-self', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the attribute axis' do
|
||||
parse_xpath('/attribute::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'attribute', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'attribute', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the child axis' do
|
||||
parse_xpath('/child::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'child', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'child', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the descendant axis' do
|
||||
parse_xpath('/descendant::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'descendant', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'descendant', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the descendant-or-self axis' do
|
||||
parse_xpath('/descendant-or-self::A').should == s(
|
||||
:xpath,
|
||||
s(
|
||||
:path,
|
||||
s(:node_test, s(:axis, 'descendant-or-self', s(:name, nil, 'A')))
|
||||
)
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'descendant-or-self', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the following axis' do
|
||||
parse_xpath('/following::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'following', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'following', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the following-sibling axis' do
|
||||
parse_xpath('/following-sibling::A').should == s(
|
||||
:xpath,
|
||||
s(
|
||||
:path,
|
||||
s(:node_test, s(:axis, 'following-sibling', s(:name, nil, 'A')))
|
||||
)
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'following-sibling', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the namespace axis' do
|
||||
parse_xpath('/namespace::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'namespace', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'namespace', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the parent axis' do
|
||||
parse_xpath('/parent::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'parent', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'parent', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the preceding axis' do
|
||||
parse_xpath('/preceding::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'preceding', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'preceding', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the preceding-sibling axis' do
|
||||
parse_xpath('/preceding-sibling::A').should == s(
|
||||
:xpath,
|
||||
s(
|
||||
:path,
|
||||
s(:node_test, s(:axis, 'preceding-sibling', s(:name, nil, 'A')))
|
||||
)
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'preceding-sibling', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the self axis' do
|
||||
parse_xpath('/self::A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'self', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'self', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -109,32 +97,29 @@ describe Oga::XPath::Parser do
|
|||
context 'short axes' do
|
||||
example 'parse the @attribute axis' do
|
||||
parse_xpath('/@A').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'attribute', s(:name, nil, 'A'))))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'attribute', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the // axis' do
|
||||
parse_xpath('//A').should == s(
|
||||
:xpath,
|
||||
s(
|
||||
:path,
|
||||
s(:node_test, s(:axis, 'descendant-or-self', s(:name, nil, 'A')))
|
||||
)
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'descendant-or-self', s(:name, nil, 'A')))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the .. axis' do
|
||||
parse_xpath('/..').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'parent')))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'parent'))
|
||||
)
|
||||
end
|
||||
|
||||
example 'parse the . axis' do
|
||||
parse_xpath('/.').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:axis, 'self')))
|
||||
:absolute,
|
||||
s(:test, s(:axis, 'self'))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,21 +3,17 @@ require 'spec_helper'
|
|||
describe Oga::XPath::Parser do
|
||||
context 'paths' do
|
||||
example 'parse an absolute path' do
|
||||
parse_xpath('/foo').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:name, nil, 'foo')))
|
||||
)
|
||||
parse_xpath('/A').should == s(:absolute, s(:test, s(:name, nil, 'A')))
|
||||
end
|
||||
|
||||
example 'parse a relative path' do
|
||||
parse_xpath('foo').should == s(:xpath, s(:node_test, s(:name, nil, 'foo')))
|
||||
parse_xpath('A').should == s(:test, s(:name, nil, 'A'))
|
||||
end
|
||||
|
||||
example 'parse an expression using two paths' do
|
||||
parse_xpath('/foo/bar').should == s(
|
||||
:xpath,
|
||||
s(:path, s(:node_test, s(:name, nil, 'foo'))),
|
||||
s(:path, s(:node_test, s(:name, nil, 'bar')))
|
||||
parse_xpath('/A/B').should == s(
|
||||
:absolute,
|
||||
s(:test, s(:name, nil, 'A'), s(:test, s(:name, nil, 'B')))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,12 +4,12 @@ describe Oga::XPath::Parser do
|
|||
context 'predicates' do
|
||||
example 'parse a single predicate' do
|
||||
parse_xpath('/foo[@class="bar"]').should == s(
|
||||
:xpath,
|
||||
:absolute,
|
||||
s(
|
||||
:path,
|
||||
s(
|
||||
:node_test,
|
||||
:test,
|
||||
s(:name, nil, 'foo'),
|
||||
s(
|
||||
:predicate,
|
||||
s(
|
||||
:eq,
|
||||
s(:axis, 'attribute', s(:name, nil, 'class')),
|
||||
|
@ -22,12 +22,12 @@ describe Oga::XPath::Parser do
|
|||
|
||||
example 'parse a predicate using the or operator' do
|
||||
parse_xpath('/foo[@class="bar" or @class="baz"]').should == s(
|
||||
:xpath,
|
||||
:absolute,
|
||||
s(
|
||||
:path,
|
||||
s(
|
||||
:node_test,
|
||||
:test,
|
||||
s(:name, nil, 'foo'),
|
||||
s(
|
||||
:predicate,
|
||||
s(
|
||||
:or,
|
||||
s(
|
||||
|
|
Loading…
Reference in New Issue