Scott Smerchek avatar Scott Smerchek committed 7b09db7

added support for jquery's :contains selector

Comments (0)

Files changed (2)

pyquery/cssselectpatch.py

         return xpath
 
     def _xpath_header(self, xpath):
-        """Matches all header elelements (h1, ..., h6)"""
+        """Matches all header elelements (h1, ..., h6)
+        """
         # this seems kind of brute-force, is there a better way?
         xpath.add_condition("(name(.) = 'h1' or name(.) = 'h2' or name (.) = 'h3') "
         + "or (name(.) = 'h4' or name (.) = 'h5' or name(.) = 'h6')")
         return xpath
 
+    def _xpath_parent(self, xpath):
+        """Match all elements that contain other elements
+        """
+        xpath.add_condition("count(child::*) > 0")
+        return xpath
+
+    def _xpath_empty(self, xpath):
+        """Match all elements that do not contain other elements
+        """
+        xpath.add_condition("count(child::*) = 0")
+        return xpath
+
 cssselect.Pseudo = JQueryPseudo
 
 class JQueryFunction(Function):
         xpath.add_post_condition('position() < %s' % int(expr+1))
         return xpath
 
+    def _xpath_contains(self, xpath, expr):
+        """Matches all elements that contain the given text
+        """
+        xpath.add_post_condition("contains(text(), '%s')" % str(expr))
+        return xpath
+
 cssselect.Function = JQueryFunction
 
 class AdvancedXPathExpr(XPathExpr):
         #test on other elements
         e = self.klass(self.html5)
         assert len(e(":header")) == 6
+        assert len(e(":parent")) == 2
+        assert len(e(":empty")) == 6
+        assert len(e(":contains('Heading')")) == 6
 
 class TestTraversal(unittest.TestCase):
     klass = pq
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.