Gael Pasgrimaud avatar Gael Pasgrimaud committed 727e93c

add .contents() .items()

Comments (0)

Files changed (2)

     >>> p.addClass("toto")
     [<p#hello.hello.toto>]
     >>> p.toggleClass("titi toto")
-    [<p#hello.titi.hello>]
+    [<p#hello.hello.titi>]
     >>> p.removeClass("titi")
     [<p#hello.hello>]
 

pyquery/pyquery.py

         return self.__class__(self[:] + other[:])
 
     def extend(self, other):
+        """Extend with anoter PyQuery object"""
         assert isinstance(other, self.__class__)
         self._extend(other[:])
 
+    def items(self, selector=None):
+        """Iter over elements. Return PyQuery objects:
+
+            >>> d = PyQuery('<div><span>foo</span><span>bar</span></div>')
+            >>> [i.text() for i in d.items('span')]
+            ['foo', 'bar']
+            >>> [i.text() for i in d('span').items()]
+            ['foo', 'bar']
+        """
+        elems = selector and self(selector) or self
+        for elem in elems:
+            yield self.__class__(elem)
+
     def __str__(self):
         """xml representation of current nodes::
 
                 result.append(current)
         return self.__class__(result, **dict(parent=self))
 
+    def contents(self):
+        """
+        Return contents (with text nodes):
+
+            >>> d = PyQuery('hello <b>bold</b>')
+            >>> d.contents()  # doctest: +ELLIPSIS
+            ['hello ', <Element b at ...>]
+        """
+        results = []
+        for elem in self:
+            results.extend(elem.xpath('child::text()|child::*'))
+        return self.__class__(results, **dict(parent=self))
+
     def filter(self, selector):
         """Filter elements in self using selector (string or function):
 
 
         """
         for tag in self:
-            values = set(value.split(' '))
-            classes = set((tag.get('class') or '').split())
-            values_to_add = values.difference(classes)
-            classes.difference_update(values)
-            classes = classes.union(values_to_add)
-            classes.difference_update([''])
+            values = value.split(' ')
+            classes = (tag.get('class') or '').split()
+            values_to_add = [v for v in values if v not in classes]
+            values_to_del = [v for v in values if v in classes]
+            classes = [v for v in classes if v not in values_to_del]
+            classes += values_to_add
             tag.set('class', ' '.join(classes))
         return self
 
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.