diff --git a/lib/oga/css/parser.y b/lib/oga/css/parser.y index 97b9c6c..1d28d9f 100644 --- a/lib/oga/css/parser.y +++ b/lib/oga/css/parser.y @@ -488,6 +488,19 @@ end return node end + ## + # Generates the AST for the `:first-child` selector. + # + # @return [AST::Node] + # + def on_pseudo_class_first_child + return s( + :eq, + s(:call, 'count', s(:axis, 'preceding-sibling', s(:test, nil, '*'))), + s(:int, 0) + ) + end + private ## diff --git a/spec/oga/css/parser/pseudo_classes/first_child_spec.rb b/spec/oga/css/parser/pseudo_classes/first_child_spec.rb new file mode 100644 index 0000000..6acb75f --- /dev/null +++ b/spec/oga/css/parser/pseudo_classes/first_child_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Oga::CSS::Parser do + context ':first-child pseudo class' do + example 'parse the :first-child pseudo class' do + parse_css(':first-child').should == parse_xpath( + 'descendant-or-self::*[count(preceding-sibling::*) = 0]' + ) + end + end +end