diff --git a/lib/oga/css/parser.y b/lib/oga/css/parser.y index 2bd6bea..44d1008 100644 --- a/lib/oga/css/parser.y +++ b/lib/oga/css/parser.y @@ -14,11 +14,12 @@ rule expression : path | node_test - | node_operators + | axis ; path_member : node_test + | axis ; path_members @@ -51,8 +52,8 @@ rule ; predicate_members - : node_test { val[0] } - | operator { val[0] } + : node_test + | operator ; operator @@ -64,12 +65,23 @@ rule | op_members T_HYPHEN_IN op_members { s(:hyphen_in, val[0],val[2]) } ; - node_operators - : node_test T_CHILD node_test { s(:child, val[0], val[2]) } - | node_test T_FOLLOWING node_test { s(:following, val[0], val[2]) } + axis + # x > y + : node_test T_CHILD node_test + { + s(:axis, 'child', val[0], val[2]) + } + + # x + y + | node_test T_FOLLOWING node_test + { + s(:axis, 'following', val[0], val[2]) + } + + # x ~ y | node_test T_FOLLOWING_DIRECT node_test { - s(:following_direct, val[0], val[2]) + s(:axis, 'following-direct', val[0], val[2]) } ; diff --git a/spec/oga/css/parser/axes_spec.rb b/spec/oga/css/parser/axes_spec.rb new file mode 100644 index 0000000..c13ef71 --- /dev/null +++ b/spec/oga/css/parser/axes_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe Oga::CSS::Parser do + context 'axes' do + example 'parse the > axis' do + parse_css('x > y').should == s( + :axis, + 'child', + s(:test, nil, 'x'), + s(:test, nil, 'y') + ) + end + + example 'parse the + axis' do + parse_css('x + y').should == s( + :axis, + 'following-direct', + s(:test, nil, 'x'), + s(:test, nil, 'y') + ) + end + + example 'parse the ~ axis' do + parse_css('x ~ y').should == s( + :axis, + 'following', + s(:test, nil, 'x'), + s(:test, nil, 'y') + ) + end + + example 'parse the ~ axis followed by another node test' do + parse_css('x ~ y z').should == s( + :path, + s(:axis, 'following', s(:test, nil, 'x'), s(:test, nil, 'y')), + s(:test, nil, 'z') + ) + end + end +end diff --git a/spec/oga/css/parser/operators_spec.rb b/spec/oga/css/parser/operators_spec.rb index e33e042..8f19344 100644 --- a/spec/oga/css/parser/operators_spec.rb +++ b/spec/oga/css/parser/operators_spec.rb @@ -55,29 +55,5 @@ describe Oga::CSS::Parser do s(:hyphen_in, s(:test, nil, 'a'), s(:string, 'b')) ) end - - example 'parse the > operator' do - parse_css('x > y').should == s( - :child, - s(:test, nil, 'x'), - s(:test, nil, 'y') - ) - end - - example 'parse the + operator' do - parse_css('x + y').should == s( - :following_direct, - s(:test, nil, 'x'), - s(:test, nil, 'y') - ) - end - - example 'parse the ~ operator' do - parse_css('x ~ y').should == s( - :following, - s(:test, nil, 'x'), - s(:test, nil, 'y') - ) - end end end