From b22137c73ee69cace625a03c6ba6de6a6b1e0742 Mon Sep 17 00:00:00 2001 From: TrevorSSmith1392 Date: Thu, 22 Aug 2013 17:08:01 -0700 Subject: [PATCH] Add nth-child selector Works in the same capacity that "first-child" does, in that it can't be used anywhere but as the root search. ie. "p:first-child" not "body p:first-child" --- Text/HandsomeSoup.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Text/HandsomeSoup.hs b/Text/HandsomeSoup.hs index 9214b83..291a19b 100644 --- a/Text/HandsomeSoup.hs +++ b/Text/HandsomeSoup.hs @@ -76,7 +76,12 @@ _fromSelectors (s:selectors) = foldl (\acc selector -> make acc selector) (make makeAttr (name, value) = hasAttrValue name (==value) makePseudos (p:pseudos) = foldl (\acc pseudo -> acc >>> makePseudo pseudo) (makePseudo p) pseudos makePseudos [] = id - makePseudo "first-child" = take 1 + makePseudo selector + | selector == "first-child" = take 1 + | nthSelector selector = take 1 . drop (n - 1) + where nthSelector selector = take 10 selector == "nth-child(" + getN selector = read $ (takeWhile (/= ')') . drop 10) selector :: Int + n = getN selector -- | Used internally to match attribute selectors like @ [att|=val] @. -- From: http://www.w3.org/TR/CSS2/selector.html