This class is used to implement the css pseudo classes (:first, :last, ...) that are not defined in the css standard, but are defined in the jquery API.
| 42 | |
| 43 | |
| 44 | class JQueryTranslator(cssselect_xpath.HTMLTranslator): |
| 45 | """This class is used to implement the css pseudo classes |
| 46 | (:first, :last, ...) that are not defined in the css standard, |
| 47 | but are defined in the jquery API. |
| 48 | """ |
| 49 | |
| 50 | xpathexpr_cls = XPathExpr |
| 51 | |
| 52 | def xpath_first_pseudo(self, xpath): |
| 53 | """Matches the first selected element:: |
| 54 | |
| 55 | >>> from pyquery import PyQuery |
| 56 | >>> d = PyQuery('<div><p class="first"></p><p></p></div>') |
| 57 | >>> d('p:first') |
| 58 | [<p.first>] |
| 59 | |
| 60 | .. |
| 61 | """ |
| 62 | xpath.add_post_condition('position() = 1') |
| 63 | return xpath |
| 64 | |
| 65 | def xpath_last_pseudo(self, xpath): |
| 66 | """Matches the last selected element:: |
| 67 | |
| 68 | >>> from pyquery import PyQuery |
| 69 | >>> d = PyQuery('<div><p></p><p class="last"></p></div>') |
| 70 | >>> d('p:last') |
| 71 | [<p.last>] |
| 72 | |
| 73 | .. |
| 74 | """ |
| 75 | xpath.add_post_condition('position() = last()') |
| 76 | return xpath |
| 77 | |
| 78 | def xpath_even_pseudo(self, xpath): |
| 79 | """Matches even elements, zero-indexed:: |
| 80 | |
| 81 | >>> from pyquery import PyQuery |
| 82 | >>> d = PyQuery('<div><p></p><p class="last"></p></div>') |
| 83 | >>> d('p:even') |
| 84 | [<p>] |
| 85 | |
| 86 | .. |
| 87 | """ |
| 88 | # the first element is 1 in xpath and 0 in python and js |
| 89 | xpath.add_post_condition('position() mod 2 = 1') |
| 90 | return xpath |
| 91 | |
| 92 | def xpath_odd_pseudo(self, xpath): |
| 93 | """Matches odd elements, zero-indexed:: |
| 94 | |
| 95 | >>> from pyquery import PyQuery |
| 96 | >>> d = PyQuery('<div><p></p><p class="last"></p></div>') |
| 97 | >>> d('p:odd') |
| 98 | [<p.last>] |
| 99 | |
| 100 | .. |
| 101 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…