Support for parsing CSS class/ID selectors.

This commit is contained in:
Yorick Peterse 2014-10-07 22:57:23 +02:00
parent 6792127600
commit a6b0bd96c8
3 changed files with 46 additions and 1 deletions

View File

@ -6,13 +6,14 @@ class Oga::CSS::Parser
token T_IDENT T_PIPE T_LBRACK T_RBRACK T_COLON T_SPACE T_LPAREN T_RPAREN T_MINUS
token T_EQ T_SPACE_IN T_STARTS_WITH T_ENDS_WITH T_IN T_HYPHEN_IN
token T_CHILD T_FOLLOWING T_FOLLOWING_DIRECT
token T_NTH T_INT T_STRING T_ODD T_EVEN
token T_NTH T_INT T_STRING T_ODD T_EVEN T_DOT T_HASH
options no_result_var
prechigh
left T_COLON
left T_CHILD T_FOLLOWING T_FOLLOWING_DIRECT
left T_DOT T_HASH
preclow
rule
@ -39,6 +40,8 @@ rule
: node_test
| axis
| pseudo_class
| class
| id
;
node_test
@ -66,6 +69,24 @@ rule
| operator
;
class
: class_name { s(:class, val[0]) }
| path_member class_name { s(:class, val[1], val[0]) }
;
class_name
: T_DOT T_IDENT { val[1] }
;
id
: id_name { s(:id, val[0]) }
| path_member id_name { s(:id, val[1], val[0]) }
;
id_name
: T_HASH T_IDENT { val[1] }
;
operator
: op_members T_EQ op_members { s(:eq, val[0], val[2]) }
| op_members T_SPACE_IN op_members { s(:space_in, val[0], val[2]) }

View File

@ -17,5 +17,17 @@ describe Oga::CSS::Parser do
example 'parse a selector using a class and an ID' do
parse_css('#foo.bar').should == s(:class, 'bar', s(:id, 'foo'))
end
example 'parse a selector using a class and a pseudo class' do
parse_css('.foo:root').should == s(:pseudo, 'root', s(:class, 'foo'))
end
example 'parse a selector using a pseudo class and a class' do
parse_css('x:root.foo').should == s(
:class,
'foo',
s(:pseudo, 'root', s(:test, nil, 'x'))
)
end
end
end

View File

@ -13,5 +13,17 @@ describe Oga::CSS::Parser do
example 'parse a selector using an ID and a class' do
parse_css('.foo#bar').should == s(:id, 'bar', s(:class, 'foo'))
end
example 'parse a selector using an ID and a pseudo class' do
parse_css('#foo:root').should == s(:pseudo, 'root', s(:id, 'foo'))
end
example 'parse a selector using a pseudo class and an ID' do
parse_css('x:root#foo').should == s(
:id,
'foo',
s(:pseudo, 'root', s(:test, nil, 'x'))
)
end
end
end