olauzanne / pyquery (http://pypi.python.org/pypi/pyquery)
A jquery-like library for python
Clone this repository (size: 130.4 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/olauzanne/pyquery/
| commit 93: | 7b09db77df15 |
| parent 92: | f325b1d93893 |
| branch: | default |
added support for jquery's :contains selector
Changed (Δ708 bytes):
raw changeset »
pyquery/cssselectpatch.py (20 lines added, 1 lines removed)
pyquery/test.py (3 lines added, 0 lines removed)
Up to file-list pyquery/cssselectpatch.py:
| … | … | @@ -123,12 +123,25 @@ class JQueryPseudo(Pseudo): |
123 |
123 |
return xpath |
124 |
124 |
|
125 |
125 |
def _xpath_header(self, xpath): |
126 |
"""Matches all header elelements (h1, ..., h6) |
|
126 |
"""Matches all header elelements (h1, ..., h6) |
|
127 |
""" |
|
127 |
128 |
# this seems kind of brute-force, is there a better way? |
128 |
129 |
xpath.add_condition("(name(.) = 'h1' or name(.) = 'h2' or name (.) = 'h3') " |
129 |
130 |
+ "or (name(.) = 'h4' or name (.) = 'h5' or name(.) = 'h6')") |
130 |
131 |
return xpath |
131 |
132 |
|
133 |
def _xpath_parent(self, xpath): |
|
134 |
"""Match all elements that contain other elements |
|
135 |
""" |
|
136 |
xpath.add_condition("count(child::*) > 0") |
|
137 |
return xpath |
|
138 |
||
139 |
def _xpath_empty(self, xpath): |
|
140 |
"""Match all elements that do not contain other elements |
|
141 |
""" |
|
142 |
xpath.add_condition("count(child::*) = 0") |
|
143 |
return xpath |
|
144 |
||
132 |
145 |
cssselect.Pseudo = JQueryPseudo |
133 |
146 |
|
134 |
147 |
class JQueryFunction(Function): |
| … | … | @@ -153,6 +166,12 @@ class JQueryFunction(Function): |
153 |
166 |
xpath.add_post_condition('position() < %s' % int(expr+1)) |
154 |
167 |
return xpath |
155 |
168 |
|
169 |
def _xpath_contains(self, xpath, expr): |
|
170 |
"""Matches all elements that contain the given text |
|
171 |
""" |
|
172 |
xpath.add_post_condition("contains(text(), '%s')" % str(expr)) |
|
173 |
return xpath |
|
174 |
||
156 |
175 |
cssselect.Function = JQueryFunction |
157 |
176 |
|
158 |
177 |
class AdvancedXPathExpr(XPathExpr): |
Up to file-list pyquery/test.py:
| … | … | @@ -179,6 +179,9 @@ class TestSelector(unittest.TestCase): |
179 |
179 |
#test on other elements |
180 |
180 |
e = self.klass(self.html5) |
181 |
181 |
assert len(e(":header")) == 6 |
182 |
assert len(e(":parent")) == 2 |
|
183 |
assert len(e(":empty")) == 6 |
|
184 |
assert len(e(":contains('Heading')")) == 6 |
|
182 |
185 |
|
183 |
186 |
class TestTraversal(unittest.TestCase): |
184 |
187 |
klass = pq |
