Commits

Anonymous committed a8d6e54

Make .each() behaves like jQuery's each:
- if callback returns false, iteration stopped
- arguments passed for callback are index and element, not pyquery instance
- if callback only accept 1 parameter, index is passed
- callback can accept zero parameter
- you can now use 'this' inside the callback

THIS COMMIT MAY BREAK EXISTING CODES THAT RELY ON PREVIOUS BEHAVIOR
tests and .make_links_absolute() have been updated to the new behavior

Comments (0)

Files changed (2)

pyquery/pyquery.py

     def each(self, func):
         """apply func on each nodes
         """
-        for e in self:
-            func(self.__class__([e]))
+        try:
+            for i, element in enumerate(self):
+                func.func_globals['this'] = element
+                if callback(func, i, element) == False:
+                    break
+        finally:
+            if 'this' in func.func_globals:
+                del func.func_globals['this']
         return self
 
     def map(self, func):
                 raise ValueError('You need a base URL to make your links'
                  'absolute. It can be provided by the base_url parameter.')
 
-        self('a').each(lambda a:
-                       a.attr('href', urljoin(base_url, a.attr('href'))))
+        self('a').each(lambda: self(this).attr('href', urljoin(base_url, self(this).attr('href'))))
         return self
 
     def test_each(self):
         doc = self.klass(self.html)
-        doc('span').each(lambda e: e.wrap("<em></em>"))
+        doc('span').each(lambda: doc(this).wrap("<em></em>"))
         assert len(doc('em')) == 3
 
     def test_map(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.