CSS parsing support for commas

The lexer already had the basic plumbing in place but apparently I
completely forgot to also implement the required bits in the parser.

Fixes #121
This commit is contained in:
Yorick Peterse 2015-06-29 18:59:01 +02:00
parent aa1a6ca308
commit d26b48feb4
3 changed files with 36 additions and 6 deletions

View File

@ -19,7 +19,7 @@
%terminals T_IDENT T_PIPE T_LBRACK T_RBRACK T_COLON T_SPACE T_LPAREN T_RPAREN;
%terminals T_MINUS T_EQ T_SPACE_IN T_STARTS_WITH T_ENDS_WITH T_IN T_HYPHEN_IN;
%terminals T_GREATER T_TILDE T_PLUS T_NTH T_INT T_STRING T_ODD T_EVEN T_DOT;
%terminals T_HASH;
%terminals T_HASH T_COMMA;
css
= selectors
@ -27,7 +27,24 @@ css
;
selectors
= selector selectors_follow*
= path selectors_follow*
{
path = val[0]
val[1].each do |chunk|
path = s(:pipe, path, chunk)
end
path
}
;
selectors_follow
= T_COMMA path { val[1] }
;
path
= selector path_follow*
{
# Single selector
if val[1].empty?
@ -58,7 +75,7 @@ selectors
}
;
selectors_follow
path_follow
= T_SPACE selector { val[1] }
;

View File

@ -19,6 +19,10 @@ describe 'CSS selector evaluation' do
evaluate_css(@document, 'a b').should == node_set(@b1, @b2)
end
it 'returns a node set containing the union of multiple paths' do
evaluate_css(@document, 'b, ns1|c').should == node_set(@b1, @b2, @c1)
end
it 'returns a node set containing namespaced nodes' do
evaluate_css(@document, 'a ns1|c').should == node_set(@c1)
end

View File

@ -11,9 +11,18 @@ describe Oga::CSS::Parser do
end
it 'parses a path using two selectors' do
parse_css('foo bar').should == parse_xpath(
'descendant::foo/descendant::bar'
)
parse_css('foo bar').should ==
parse_xpath('descendant::foo/descendant::bar')
end
it 'parses two paths separated by a comma' do
parse_css('foo, bar').should ==
parse_xpath('descendant::foo | descendant::bar')
end
it 'parses three paths separated by a comma' do
parse_css('foo, bar, baz').should ==
parse_xpath('descendant::foo | descendant::bar | descendant::baz')
end
end
end