Olivier Lauzanne avatar Olivier Lauzanne committed 59706fa

before method

Comments (0)

Files changed (3)

pyquery/README.txt

     >>> d('#test').html()
     '<p class="hello" ...</p>...hello...python...'
 
+Insert an element after another::
+
+    >>> p.insertAfter(d('#test'))
+    [<p#hello.hello>]
+    >>> d('#test').html()
+    '<a href="http://python.org">python</a> !...
+
 And you can get back the modified html::
 
     >>> print d

pyquery/pyquery.py

     # Manipulating #
     ################
 
-    def append(self, value):
+    def _get_root(self, value):
         is_pyquery_results = isinstance(value, PyQueryResults)
         is_string = isinstance(value, basestring)
         assert is_string or is_pyquery_results
             root_text = root.text
         else:
             root_text = ''
+        return root, root_text
+
+    def append(self, value):
+        root, root_text = self._get_root(value)
         for i, tag in enumerate(self):
             if len(tag) > 0: # if the tag has children
                 last_child = tag[-1]
                 root = deepcopy(list(root))
             tag.extend(root)
             root = tag[-len(root):]
-
         return self
 
     def appendTo(self, value):
         return self
 
     def prepend(self, value):
-        is_pyquery_results = isinstance(value, PyQueryResults)
-        is_string = isinstance(value, basestring)
-        assert is_string or is_pyquery_results
-        if is_string:
-            root = etree.fromstring('<root>' + value + '</root>')
-        elif is_pyquery_results:
-            root = value
-        if hasattr(root, 'text') and isinstance(root.text, basestring):
-            root_text = root.text
-        else:
-            root_text = ''
+        root, root_text = self._get_root(value)
         for i, tag in enumerate(self):
             if not tag.text:
                 tag.text = ''
                 root = deepcopy(list(root))
             tag[:0] = root
             root = tag[:len(root)]
-
         return self
 
     def prependTo(self, value):
         value.prepend(self)
         return self
+
+    def after(self, value):
+        root, root_text = self._get_root(value)
+        for i, tag in enumerate(self):
+            if not tag.tail:
+                tag.tail = ''
+            tag.tail += root_text
+            if i > 0:
+                root = deepcopy(list(root))
+            parent = tag.getparent()
+            index = parent.index(tag) + 1
+            parent[index:index-1] = root
+            root = parent[index:len(root)]
+        return self
+
+    def insertAfter(self, value):
+        value.after(self)
+        return self
+
+    def before(self, value):
+        return self #TODO
+        root, root_text = self._get_root(value)
+        for i, tag in enumerate(self):
+            if not tag.tail:
+                tag.previous()
+            if i > 0:
+                root = deepcopy(list(root))
+            parent = tag.getparent()
+            index = parent.index(tag) + 1
+            parent[index:index-1] = root
+            root = parent[index:len(root)]
+        return self
 
 def test():
     import doctest
-    doctest.testfile('README.txt', optionflags=doctest.ELLIPSIS)
+    fails, total = doctest.testfile('README.txt', optionflags=doctest.ELLIPSIS)
+    if fails == 0:
+        print 'OK'
 
 if __name__ == '__main__':
     test()
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.