Olivier Lauzanne avatar Olivier Lauzanne committed b45eaa0

some more pseudo classes

Comments (0)

Files changed (4)

pyquery/README.txt

 Making links absolute
 ---------------------
 
-You can make all links on a page absolute which can be usefull for screen
-scrapping::
+You can make links absolute which can be usefull for screen scrapping::
 
-    >>> d = pq(url='http://google.com')
-    >>> d('a').eq(25).attr('href')
-    '/services/'
+    >>> d = pq(url='http://www.w3.org/', parser='html')
+    >>> d('a[title="W3C Activities"]').attr('href')
+    '/Consortium/activities'
     >>> d.make_links_absolute()
     [<html>]
-    >>> d('a').eq(25).attr('href')
-    'http://google.com/services/'
+    >>> d('a[title="W3C Activities"]').attr('href')
+    'http://www.w3.org/Consortium/activities'
 
 
 Testing

pyquery/cssselectpatch.py

         xpath.add_post_condition('position() mod 2 = 0')
         return xpath
 
+    def _xpath_checked(self, xpath):
+        """Matches odd elements, zero-indexed.
+        """
+        xpath.add_condition("@checked and name(.) = 'input'")
+        return xpath
+
+    def _xpath_selected(self, xpath):
+        """Matches all elements that are selected.
+        """
+        xpath.add_condition("@selected and name(.) = 'option'")
+        return xpath
+
+    def _xpath_disabled(self, xpath):
+        """Matches all elements that are disabled.
+        """
+        xpath.add_condition("@disabled")
+        return xpath
+
+    def _xpath_enabled(self, xpath):
+        """Matches all elements that are disabled.
+        """
+        xpath.add_condition("not(@disabled) and name(.) = 'input'")
+        return xpath
+
+    def _xpath_file(self, xpath):
+        """Matches all input elements of type file.
+        """
+        xpath.add_condition("@type = 'file' and name(.) = 'input'")
+        return xpath
+
 cssselect.Pseudo = JQueryPseudo
 
 class JQueryFunction(Function):

pyquery/pyquery.py

 from copy import deepcopy
 from urlparse import urljoin
 
-def fromstring(context):
+def fromstring(context, parser=None):
     """use html parser if we don't have clean xml
     """
-    try:
+    if parser == None:
+        try:
+            return etree.fromstring(context)
+        except etree.XMLSyntaxError:
+            return etree.fromstring(context, etree.HTMLParser())
+    elif parser == 'xml':
         return etree.fromstring(context)
-    except etree.XMLSyntaxError:
+    elif parser == 'html':
         return etree.fromstring(context, etree.HTMLParser())
+    else:
+        ValueError('No such parser: "%s"' % parser)
 
 class NoDefault(object):
     def __repr__(self):
         html = None
         elements = []
         self._base_url = None
+        parser = kwargs.get('parser')
 
         if 'parent' in kwargs:
             self._parent = kwargs.pop('parent')
                 self._base_url = url
             else:
                 raise ValueError('Invalid keyword arguments %s' % kwargs)
-            elements = [fromstring(html)]
+            elements = [fromstring(html, parser)]
         else:
             # get nodes
 
             # get context
             if isinstance(context, basestring):
                 try:
-                    elements = [fromstring(context)]
+                    elements = [fromstring(context, parser)]
                 except Exception, e:
                     raise ValueError('%r, %s' % (e, context))
             elif isinstance(context, self.__class__):
            </html>
            """
 
+    html4 = """
+           <html>
+            <body>
+              <form action="/">
+                <input name="enabled" type="text" value="test"/>
+                <input name="disabled" type="text" value="disabled" disabled="disabled"/>
+                <input name="file" type="file" />
+                <select name="select">
+                  <option value="">Choose something</option>
+                  <option value="one">One</option>
+                  <option value="two" selected="selected">Two</option>
+                  <option value="three">Three</option>
+                </select>
+                <input name="radio" type="radio" value="one"/>
+                <input name="radio" type="radio" value="two" checked="checked"/>
+                <input name="radio" type="radio" value="three"/>
+              </form>
+            </body>
+           </html>
+           """
+
     def test_selector_from_doc(self):
         doc = etree.fromstring(self.html)
         assert len(self.klass(doc)) == 1
         self.assertEqual(e('div:lt(1)').text(), 'node1')
         self.assertEqual(e('div:eq(2)').text(), 'node3')
 
+        #test on the form
+        e = self.klass(self.html4)
+        assert len(e(':disabled')) == 1
+        assert len(e('input:enabled')) == 5
+        assert len(e(':selected')) == 1
+        assert len(e(':checked')) == 1
+        assert len(e(':file')) == 1
+
 class TestTraversal(unittest.TestCase):
     klass = pq
     html = """
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.