Re-enabled parsing of CSS predicates.

This commit is contained in:
Yorick Peterse 2014-10-20 00:39:12 +02:00
parent d4150fd0f5
commit 174d33c597
4 changed files with 36 additions and 41 deletions

View File

@ -71,7 +71,7 @@ rule
;
attribute_test
: node_name { s(:test, *val[0]) }
: node_name { s(:axis, 'attribute', s(:test, *val[0])) }
;
node_name
@ -91,7 +91,7 @@ rule
predicate_members
: attribute_test
#| operator
| operator
;
class
@ -116,19 +116,19 @@ rule
}
;
# 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]) }
# | op_members T_STARTS_WITH op_members { s(:starts_with, val[0], val[2]) }
# | op_members T_ENDS_WITH op_members { s(:ends_with, val[0], val[2]) }
# | op_members T_IN op_members { s(:in, val[0], val[2]) }
# | op_members T_HYPHEN_IN op_members { s(:hyphen_in, val[0],val[2]) }
# ;
#
# op_members
# : node_test
# | string
# ;
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]) }
| op_members T_STARTS_WITH op_members { s(:starts_with, val[0], val[2]) }
| op_members T_ENDS_WITH op_members { s(:ends_with, val[0], val[2]) }
| op_members T_IN op_members { s(:in, val[0], val[2]) }
| op_members T_HYPHEN_IN op_members { s(:hyphen_in, val[0],val[2]) }
;
op_members
: attribute_test
| string
;
#
# axis
# # x > y
@ -188,14 +188,14 @@ rule
# | integer T_NTH { s(:nth, val[0]) }
# | integer T_NTH integer { s(:nth, val[0], val[2]) }
# ;
#
# string
# : T_STRING { s(:string, val[0]) }
# ;
#
# integer
# : T_INT { s(:int, val[0].to_i) }
# ;
string
: T_STRING { s(:string, val[0]) }
;
integer
: T_INT { s(:int, val[0].to_i) }
;
end
---- inner

View File

@ -3,14 +3,12 @@ require 'spec_helper'
describe Oga::CSS::Parser do
context 'paths' do
example 'parse a single path' do
parse_css('foo').should == s(:test, nil, 'foo')
parse_css('foo').should == parse_xpath('descendant-or-self::foo')
end
example 'parse a path using two selectors' do
parse_css('foo bar').should == s(
:path,
s(:test, nil, 'foo'),
s(:test, nil, 'bar')
parse_css('foo bar').should == parse_xpath(
'descendant-or-self::foo/descendant-or-self::bar'
)
end
end

View File

@ -3,23 +3,20 @@ require 'spec_helper'
describe Oga::CSS::Parser do
context 'predicates' do
example 'parse a predicate' do
parse_css('foo[bar]').should == s(:test, nil, 'foo', s(:test, nil, 'bar'))
parse_css('foo[bar]').should == parse_xpath(
'descendant-or-self::foo[@bar]'
)
end
example 'parse a node test followed by a node test with a predicate' do
parse_css('foo bar[baz]').should == s(
:path,
s(:test, nil, 'foo'),
s(:test, nil, 'bar', s(:test, nil, 'baz'))
parse_css('foo bar[baz]').should == parse_xpath(
'descendant-or-self::foo/descendant-or-self::bar[@baz]'
)
end
example 'parse a predicate testing an attribute value' do
parse_css('foo[bar="baz"]').should == s(
:test,
nil,
'foo',
s(:eq, s(:test, nil, 'bar'), s(:string, 'baz'))
parse_css('foo[bar="baz"]').should == parse_xpath(
'descendant-or-self::foo[@bar="baz"]'
)
end
end

View File

@ -3,15 +3,15 @@ require 'spec_helper'
describe Oga::CSS::Parser do
context 'wildcards' do
example 'parse a wildcard name test' do
parse_css('*').should == s(:test, nil, '*')
parse_css('*').should == parse_xpath('descendant-or-self::*')
end
example 'parse a wildcard namespace test' do
parse_css('*|foo').should == s(:test, '*', 'foo')
parse_css('*|foo').should == parse_xpath('descendant-or-self::*:foo')
end
example 'parse a wildcard namespace and name test' do
parse_css('*|*').should == s(:test, '*', '*')
parse_css('*|*').should == parse_xpath('descendant-or-self::*:*')
end
end
end