From d26b48feb454de0d5b2a3a9bcffaa7c5d9d604b5 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 29 Jun 2015 18:59:01 +0200 Subject: [PATCH] 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 --- lib/oga/css/parser.rll | 23 ++++++++++++++++++++--- spec/oga/css/evaluator/paths_spec.rb | 4 ++++ spec/oga/css/parser/paths_spec.rb | 15 ++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/oga/css/parser.rll b/lib/oga/css/parser.rll index 464f175..b85587d 100644 --- a/lib/oga/css/parser.rll +++ b/lib/oga/css/parser.rll @@ -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] } ; diff --git a/spec/oga/css/evaluator/paths_spec.rb b/spec/oga/css/evaluator/paths_spec.rb index 1871c7b..6b34337 100644 --- a/spec/oga/css/evaluator/paths_spec.rb +++ b/spec/oga/css/evaluator/paths_spec.rb @@ -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 diff --git a/spec/oga/css/parser/paths_spec.rb b/spec/oga/css/parser/paths_spec.rb index 6ca297f..5307050 100644 --- a/spec/oga/css/parser/paths_spec.rb +++ b/spec/oga/css/parser/paths_spec.rb @@ -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