Support for parsing the :root pseudo class.

This commit is contained in:
Yorick Peterse 2014-10-26 22:20:23 +01:00
parent 32764c9a14
commit 39c0f7147c
2 changed files with 47 additions and 9 deletions

View File

@ -237,10 +237,10 @@ rule
pseudo_class
# :root
: pseudo_name { s(:pseudo, nil, val[0]) }
: pseudo_name { on_pseudo_class(val[0]) }
# :nth-child(2)
| pseudo_name pseudo_args { s(:pseudo, nil, val[0], val[1]) }
| pseudo_name pseudo_args { on_pseudo_class(val[0], val[1]) }
;
pseudo_name
@ -253,19 +253,34 @@ rule
pseudo_arg
: integer
#| odd
#| even
#| nth
| odd
| even
| nth
| selector
;
nth
: T_NTH { s(:nth) }
| T_MINUS T_NTH { s(:nth) }
| integer T_NTH { s(:nth, val[0]) }
| integer T_NTH integer { s(:nth, val[0], val[2]) }
;
odd
: T_ODD { s(:nth, s(:int, 2), s(:int, 1)) }
;
even
: T_EVEN { s(:nth, s(:int, 2)) }
;
string
: T_STRING { s(:string, val[0]) }
;
integer
: T_INT { s(:int, val[0].to_i) }
;
: T_INT { s(:int, val[0].to_i) }
;
end
---- inner
@ -313,4 +328,23 @@ end
return ast
end
##
# @param [String] name
# @param [AST::Node] arg
# @return [AST::Node]
#
def on_pseudo_class(name, arg = nil)
handler = "on_pseudo_class_#{name.gsub('-', '_')}"
return arg ? send(handler, arg) : send(handler)
end
##
# Generates the AST for the `root` pseudo class.
#
# @return [AST::Node]
#
def on_pseudo_class_root
return s(:call, 'not', s(:axis, 'parent', s(:test, nil, '*')))
end
# vim: set ft=racc:

View File

@ -3,11 +3,15 @@ require 'spec_helper'
describe Oga::CSS::Parser do
context 'pseudo classes' do
example 'parse the x:root pseudo class' do
parse_css('x:root').should == s(:pseudo, s(:test, nil, 'x'), 'root')
parse_css('x:root').should == parse_xpath(
'descendant-or-self::x[not(parent::*)]'
)
end
example 'parse the :root pseudo class' do
parse_css(':root').should == s(:pseudo, nil, 'root')
parse_css(':root').should == parse_xpath(
'descendant-or-self::*[not(parent::*)]'
)
end
example 'parse the x:nth-child(1) pseudo class' do