Skip to content

Commit

Permalink
Add nth-child selector
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
ts-smith committed Aug 23, 2013
1 parent 68c484c commit b22137c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Text/HandsomeSoup.hs
Expand Up @@ -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
Expand Down

0 comments on commit b22137c

Please sign in to comment.