From 7a606a11d31f855244de2df2993e06f5fb305fbc Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 3 Nov 2014 22:58:08 +0100 Subject: [PATCH] Parsing support for the :last-child pseudo class. --- lib/oga/css/parser.y | 13 +++++++++++++ .../css/parser/pseudo_classes/last_child_spec.rb | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 spec/oga/css/parser/pseudo_classes/last_child_spec.rb diff --git a/lib/oga/css/parser.y b/lib/oga/css/parser.y index 1d28d9f..8f6dbd9 100644 --- a/lib/oga/css/parser.y +++ b/lib/oga/css/parser.y @@ -501,6 +501,19 @@ end ) end + ## + # Generates the AST for the `:last-child` selector. + # + # @return [AST::Node] + # + def on_pseudo_class_last_child + return s( + :eq, + s(:call, 'count', s(:axis, 'following-sibling', s(:test, nil, '*'))), + s(:int, 0) + ) + end + private ## diff --git a/spec/oga/css/parser/pseudo_classes/last_child_spec.rb b/spec/oga/css/parser/pseudo_classes/last_child_spec.rb new file mode 100644 index 0000000..ae94ed1 --- /dev/null +++ b/spec/oga/css/parser/pseudo_classes/last_child_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Oga::CSS::Parser do + context ':last-child pseudo class' do + example 'parse the :last-child pseudo class' do + parse_css(':last-child').should == parse_xpath( + 'descendant-or-self::*[count(following-sibling::*) = 0]' + ) + end + end +end