Fixed parsing of CSS class selectors.

When a class selector is used it should be checked as one of the possible
values, not as _the_ only value (unlike ID selectors).
This commit is contained in:
Yorick Peterse 2014-10-20 00:45:41 +02:00
parent 174d33c597
commit 15ebdb7de4
3 changed files with 13 additions and 7 deletions

View File

@ -97,9 +97,12 @@ rule
class class
: T_DOT T_IDENT : T_DOT T_IDENT
{ {
axis = s(:axis, 'attribute', s(:test, nil, 'class'))
s( s(
:eq, :call,
s(:axis, 'attribute', s(:test, nil, 'class')), 'contains',
s(:call, 'concat', s(:string, ' '), axis, s(:string, ' ')),
s(:string, val[1]) s(:string, val[1])
) )
} }

View File

@ -4,25 +4,27 @@ describe Oga::CSS::Parser do
context 'classes' do context 'classes' do
example 'parse a class selector' do example 'parse a class selector' do
parse_css('.foo').should == parse_xpath( parse_css('.foo').should == parse_xpath(
'descendant-or-self::*[@class="foo"]' 'descendant-or-self::*[contains(concat(" ", @class, " "), "foo")]'
) )
end end
example 'parse a selector for an element with a class' do example 'parse a selector for an element with a class' do
parse_css('foo.bar').should == parse_xpath( parse_css('foo.bar').should == parse_xpath(
'descendant-or-self::foo[@class="bar"]' 'descendant-or-self::foo[contains(concat(" ", @class, " "), "bar")]'
) )
end end
example 'parse a selector using multiple classes' do example 'parse a selector using multiple classes' do
parse_css('.foo.bar').should == parse_xpath( parse_css('.foo.bar').should == parse_xpath(
'descendant-or-self::*[@class="foo" and @class="bar"]' 'descendant-or-self::*[contains(concat(" ", @class, " "), "foo") ' \
'and contains(concat(" ", @class, " "), "bar")]'
) )
end end
example 'parse a selector using a class and an ID' do example 'parse a selector using a class and an ID' do
parse_css('#foo.bar').should == parse_xpath( parse_css('#foo.bar').should == parse_xpath(
'descendant-or-self::*[@id="foo" and @class="bar"]' 'descendant-or-self::*[@id="foo" and ' \
'contains(concat(" ", @class, " "), "bar")]'
) )
end end
end end

View File

@ -22,7 +22,8 @@ describe Oga::CSS::Parser do
example 'parse a selector using an ID and a class' do example 'parse a selector using an ID and a class' do
parse_css('.foo#bar').should == parse_xpath( parse_css('.foo#bar').should == parse_xpath(
'descendant-or-self::*[@class="foo" and @id="bar"]' 'descendant-or-self::*[contains(concat(" ", @class, " "), "foo") ' \
'and @id="bar"]'
) )
end end
end end