From 913eb6a15a3798a453368f71ace0fd453bff1f2f Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 14 Nov 2014 01:02:58 +0100 Subject: [PATCH] Added various extra pseudos to the CSS spec. --- doc/css_selectors.md | 117 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/doc/css_selectors.md b/doc/css_selectors.md index 8c94bab..0d76be1 100644 --- a/doc/css_selectors.md +++ b/doc/css_selectors.md @@ -603,9 +603,126 @@ The corresponding XPath expressions are similar to those used for :nth-last-child(even) => *[((count(following-sibling::*) + 1) mod 2) = 0] :nth-last-child(odd) => *[((count(following-sibling::*) + 1) >= 1) and ((((count(following-sibling::*) + 1) - 1) mod 2) = 0)] +### :nth-of-type(n) + +The `:nth-of-type(n)` selector can be used to select a set of elements that has +a set of preceding siblings with the same name. The arguments that can be used +by this selector are the same as those mentioned in [:nth-child(n)][nth-childn]. + +The matching order of this selector is the same as [:nth-child(n)][nth-childn]. + +Example XML: + + + + + + + + + +Using the CSS expression `root foo:nth-of-type(even)` would return a set +containing the 2nd and 4th `` nodes. + +The corresponding XPath expressions for the various forms of this selector are +as following: + + :nth-of-type(n) => *[position() = n] + :nth-of-type(-n) => *[position() = -n] + :nth-of-type(5) => *[position() = 5] + :nth-of-type(2n) => *[(position() mod 2) = 0] + :nth-of-type(2n+2) => *[(position() >= 2) and (((position() - 2) mod 2) = 0)] + :nth-of-type(2n-6) => *[(position() >= 2) and (((position() - 2) mod 2) = 0)] + :nth-of-type(n+5) => *[(position() >= 5) and (((position() - 5) mod 1) = 0)] + :nth-of-type(-n+6) => *[(position() <= 6) and (((position() - 6) mod 1) = 0)] + :nth-of-type(even) => *[(position() mod 2) = 0] + :nth-of-type(odd) => *[(position() >= 1) and (((position() - 1) mod 2) = 0)] + +### :nth-last-of-type(n) + +The `:nth-last-of-type(n)` selector behaves the same as +[:nth-of-type(n)][nth-last-of-typen] excepts it matches nodes in reverse order +similar to [:nth-last-child(n)][nth-last-childn]. To clarify, this means +matching occurs as following: + + + :nth-last-of-type(1) + + 1 2 3 4 5 6 + +---+ +---+ +---+ +---+ +---+ +---+ + | | | | | | | | | | | X | <- matching direction + +---+ +---+ +---+ +---+ +---+ +---+ + +Example XML: + + + + + + + + + +Using the CSS expression `root foo:nth-of-type(even)` would return a set +containing the 1st and 3rd `` nodes. + +The corresponding XPath expressions for the various forms of this selector are +as following: + + :nth-last-of-type(n) => *[position() = last() - -1] + :nth-last-of-type(-n) => *[position() = last() - -1] + :nth-last-of-type(5) => *[position() = last() - 4] + :nth-last-of-type(2n) => *[((last() - position()+1) mod 2) = 0] + :nth-last-of-type(2n+2) => *[((last() - position()+1) >= 2) and ((((last() - position() + 1) - 2) mod 2) = 0)] + :nth-last-of-type(2n-6) => *[((last() - position()+1) >= 2) and ((((last() - position() + 1) - 2) mod 2) = 0)] + :nth-last-of-type(n+5) => *[((last() - position()+1) >= 5) and ((((last() - position() + 1) - 5) mod 1) = 0)] + :nth-last-of-type(-n+6) => *[((last() - position()+1) <= 6) and ((((last() - position() + 1) - 6) mod 1) = 0)] + :nth-last-of-type(even) => *[((last() - position()+1) mod 2) = 0] + :nth-last-of-type(odd) => *[((last() - position()+1) >= 1) and ((((last() - position() + 1) - 1) mod 2) = 0)] + +### :first-child + +The `:first-child` selector can be used to match a node that is the first child +node of another node (= a node without any preceding nodes). + +Example XML: + + + + + + +Using the CSS selector `root :first-child` would return a set containing only +the `` node. + +The corresponding XPath expression for this selector is as following: + + :first-child => *[count(preceding-sibling::*) = 0] + +### :last-child + +The `:last-child` selector can be used to match a node that is the last child +node of another node (= a node without any following nodes). + +Example XML: + + + + + + +Using the CSS selector `root :last-child` would return a set containing only +the `` node. + +The corresponding XPath expression for this selector is as following: + + :last-child => *[count(following-sibling::*) = 0] + [w3spec]: http://www.w3.org/TR/css3-selectors/ [rfc-2119]: https://www.ietf.org/rfc/rfc2119.txt [kramdown]: http://kramdown.gettalong.org/ [universal-selector]: #universal-selector [ragel]: http://www.colm.net/open-source/ragel/ [nth-childn]: #nth-childn +[nth-last-childn]: #nth-last-childn +[nth-last-of-typen]: #nth-last-of-typen