diff --git a/lib/oga/css/lexer.rl b/lib/oga/css/lexer.rl index b9fb553..ad13ed0 100644 --- a/lib/oga/css/lexer.rl +++ b/lib/oga/css/lexer.rl @@ -132,9 +132,12 @@ module Oga whitespace = [\t ]+; - action emit_space { - add_token(:T_SPACE) - } + comma = ',' @{ add_token(:T_COMMA) }; + hash = '#' @{ add_token(:T_HASH) }; + dot = '.' @{ add_token(:T_DOT) }; + pipe = '|' @{ add_token(:T_PIPE) }; + lbrack = '[' @{ add_token(:T_LBRACK) }; + rbrack = ']' @{ add_token(:T_RBRACK) }; # Identifiers # @@ -148,8 +151,9 @@ module Oga } main := |* + whitespace | comma | hash | dot | pipe | lbrack | rbrack; + identifier => emit_identifier; - whitespace => emit_space; any; *|; diff --git a/spec/oga/css/lexer/paths_spec.rb b/spec/oga/css/lexer/paths_spec.rb index ad71feb..7d70fa0 100644 --- a/spec/oga/css/lexer/paths_spec.rb +++ b/spec/oga/css/lexer/paths_spec.rb @@ -9,9 +9,59 @@ describe Oga::CSS::Lexer do example 'lex a path with two members' do lex_css('div h3').should == [ [:T_IDENT, 'div'], - [:T_SPACE, nil], [:T_IDENT, 'h3'] ] end + + example 'lex two paths' do + lex_css('foo, bar').should == [ + [:T_IDENT, 'foo'], + [:T_COMMA, nil], + [:T_IDENT, 'bar'] + ] + end + + example 'lex a path selecting an ID' do + lex_css('#foo').should == [ + [:T_HASH, nil], + [:T_IDENT, 'foo'] + ] + end + + example 'lex a path selecting a class' do + lex_css('.foo').should == [ + [:T_DOT, nil], + [:T_IDENT, 'foo'] + ] + end + + example 'lex a wildcard path' do + lex_css('*').should == [[:T_IDENT, '*']] + end + + example 'lex a path containing a namespace name' do + lex_css('foo|bar').should == [ + [:T_IDENT, 'foo'], + [:T_PIPE, nil], + [:T_IDENT, 'bar'] + ] + end + + example 'lex a path containing a namespace wildcard' do + lex_css('*|foo').should == [ + [:T_IDENT, '*'], + [:T_PIPE, nil], + [:T_IDENT, 'foo'] + ] + end + + example 'lex a path containing a simple predicate' do + lex_css('foo[bar]').should == [ + [:T_IDENT, 'foo'], + [:T_LBRACK, nil], + [:T_IDENT, 'bar'], + [:T_RBRACK, nil] + ] + end end end