From 174d33c59754e7b350921e055a6ee863860286ef Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 20 Oct 2014 00:39:12 +0200 Subject: [PATCH] Re-enabled parsing of CSS predicates. --- lib/oga/css/parser.y | 46 +++++++++++++------------- spec/oga/css/parser/paths_spec.rb | 8 ++--- spec/oga/css/parser/predicates_spec.rb | 17 ++++------ spec/oga/css/parser/wildcards_spec.rb | 6 ++-- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/lib/oga/css/parser.y b/lib/oga/css/parser.y index cab8162..e113b2c 100644 --- a/lib/oga/css/parser.y +++ b/lib/oga/css/parser.y @@ -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 diff --git a/spec/oga/css/parser/paths_spec.rb b/spec/oga/css/parser/paths_spec.rb index 39af30e..42849ae 100644 --- a/spec/oga/css/parser/paths_spec.rb +++ b/spec/oga/css/parser/paths_spec.rb @@ -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 diff --git a/spec/oga/css/parser/predicates_spec.rb b/spec/oga/css/parser/predicates_spec.rb index cc3df16..003c616 100644 --- a/spec/oga/css/parser/predicates_spec.rb +++ b/spec/oga/css/parser/predicates_spec.rb @@ -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 diff --git a/spec/oga/css/parser/wildcards_spec.rb b/spec/oga/css/parser/wildcards_spec.rb index 3568a91..04f5f3a 100644 --- a/spec/oga/css/parser/wildcards_spec.rb +++ b/spec/oga/css/parser/wildcards_spec.rb @@ -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