From 0e6aefb7274c6e35e5678dde2239850d835996c9 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 30 Oct 2014 00:24:31 +0100 Subject: [PATCH] Fixed parsing of nth-child(n) and nth-child(-n) --- lib/oga/css/parser.y | 4 ++-- spec/oga/css/parser/pseudo_classes_spec.rb | 16 +++++----------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/oga/css/parser.y b/lib/oga/css/parser.y index c986e61..ba393ce 100644 --- a/lib/oga/css/parser.y +++ b/lib/oga/css/parser.y @@ -272,7 +272,7 @@ rule nth : T_NTH { s(:nth, s(:int, 1)) } - | T_MINUS T_NTH { s(:nth) } + | T_MINUS T_NTH { s(:nth, s(:int, 1)) } | integer T_NTH { s(:nth, val[0]) } | integer T_NTH integer { s(:nth, val[0], val[2]) } ; @@ -384,7 +384,7 @@ end s(:eq, s(:mod, s(:sub, before_count, offset), s(:int, 2)), s(:int, 0)) ) else - node = s(:mod, before_count, step) + node = s(:eq, s(:mod, before_count, step), s(:int, 0)) end end diff --git a/spec/oga/css/parser/pseudo_classes_spec.rb b/spec/oga/css/parser/pseudo_classes_spec.rb index fad1a4c..cb698bb 100644 --- a/spec/oga/css/parser/pseudo_classes_spec.rb +++ b/spec/oga/css/parser/pseudo_classes_spec.rb @@ -34,7 +34,7 @@ describe Oga::CSS::Parser do example 'parse the x:nth-child(even) pseudo class' do parse_css('x:nth-child(even)').should == parse_xpath( - 'descendant-or-self::x[(count(preceding-sibling::*) + 1) mod 2]' + 'descendant-or-self::x[((count(preceding-sibling::*) + 1) mod 2) = 0]' ) end @@ -46,20 +46,14 @@ describe Oga::CSS::Parser do end example 'parse the x:nth-child(n) pseudo class' do - parse_css('x:nth-child(n)').should == s( - :pseudo, - s(:test, nil, 'x'), - 'nth-child', - s(:nth) + parse_css('x:nth-child(n)').should == parse_xpath( + 'descendant-or-self::x[((count(preceding-sibling::*) + 1) mod 1) = 0]' ) end example 'parse the x:nth-child(-n) pseudo class' do - parse_css('x:nth-child(-n)').should == s( - :pseudo, - s(:test, nil, 'x'), - 'nth-child', - s(:nth) + parse_css('x:nth-child(-n)').should == parse_xpath( + 'descendant-or-self::x[((count(preceding-sibling::*) + 1) mod 1) = 0]' ) end