diff --git a/lib/oga/css/lexer.rl b/lib/oga/css/lexer.rl index 1878391..d28f6c5 100644 --- a/lib/oga/css/lexer.rl +++ b/lib/oga/css/lexer.rl @@ -175,9 +175,13 @@ module Oga op_ends_with = '$='; op_in = '*='; op_hyphen_in = '|='; - op_child = whitespace* '>' whitespace*; - op_fol_direct = whitespace* '+' whitespace*; - op_fol = whitespace* '~' whitespace*; + + # Whitespace preceding these tokens is _not_ matched to make the parser + # rules more consistent. As such input such as " > x" will result in + # tokens [T_SPACE, T_CHILD, T_IDENT]. + op_child = '>' whitespace*; + op_fol_direct = '+' whitespace*; + op_fol = '~' whitespace*; # Numbers # diff --git a/spec/oga/css/lexer/axes_spec.rb b/spec/oga/css/lexer/axes_spec.rb index 9043aff..2d137e9 100644 --- a/spec/oga/css/lexer/axes_spec.rb +++ b/spec/oga/css/lexer/axes_spec.rb @@ -6,24 +6,51 @@ describe Oga::CSS::Lexer do lex_css('>').should == [[:T_CHILD, nil]] end - example 'lex the > axis with surrounding whitespace' do - lex_css('>').should == [[:T_CHILD, nil]] + example 'lex the expression "> y"' do + lex_css('> y').should == [[:T_CHILD, nil], [:T_IDENT, 'y']] + end + + example 'lex the expression "x > y"' do + lex_css('x > y').should == [ + [:T_IDENT, 'x'], + [:T_SPACE, nil], + [:T_CHILD, nil], + [:T_IDENT, 'y'] + ] end example 'lex the + axis' do lex_css('+').should == [[:T_FOLLOWING_DIRECT, nil]] end - example 'lex the + axis with surrounding whitespace' do - lex_css(' + ').should == [[:T_FOLLOWING_DIRECT, nil]] + example 'lex the expression "+ y"' do + lex_css('+ y').should == [[:T_FOLLOWING_DIRECT, nil], [:T_IDENT, 'y']] + end + + example 'lex the expression "x + y"' do + lex_css('x + y').should == [ + [:T_IDENT, 'x'], + [:T_SPACE, nil], + [:T_FOLLOWING_DIRECT, nil], + [:T_IDENT, 'y'] + ] end example 'lex the ~ axis' do lex_css('~').should == [[:T_FOLLOWING, nil]] end - example 'lex the ~ axis with surrounding whitespace' do - lex_css(' ~ ').should == [[:T_FOLLOWING, nil]] + example 'lex the expression "~ y"' do + lex_css('~ y').should == [[:T_FOLLOWING, nil], [:T_IDENT, 'y']] + end + + example 'lex the expression "x ~ y"' do + lex_css('x ~ y').should == [ + [:T_IDENT, 'x'], + [:T_SPACE, nil], + [:T_FOLLOWING, nil], + [:T_IDENT, 'y'] + ] end end end