Commits

olau...@localhost.localdomain  committed 3c7ab75

clone, empty, remove

  • Participants
  • Parent commits 81f6dae
  • Branches manipulating

Comments (0)

Files changed (2)

File pyquery/README.txt

     >>> p.insertAfter(d('#test'))
     [<p#hello.hello>]
     >>> d('#test').html()
-    '<a href="http://python.org">python</a> !...
+    '<a href="http://python.org">python</a> !...'
+
+Or before::
+
+    >>> p.insertBefore(d('#test'))
+    [<p#hello.hello>]
+    >>> d('body').html()
+    '\n<p class="hello" id="hello" style="font-size: 17px">...'
+
 
 And you can get back the modified html::
 

File pyquery/pyquery.py

                 root = deepcopy(list(root))
             parent = tag.getparent()
             index = parent.index(tag) + 1
-            parent[index:index-1] = root
+            parent[index:index] = root
             root = parent[index:len(root)]
         return 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()
+            previous = tag.getprevious()
+            if previous != None:
+                if not previous.tail:
+                    previous.tail = ''
+                previous.tail += root_text
+            else:
+                parent = tag.getparent()
+                if not parent.text:
+                    parent.text = ''
+                parent.text += root_text
             if i > 0:
                 root = deepcopy(list(root))
             parent = tag.getparent()
-            index = parent.index(tag) + 1
-            parent[index:index-1] = root
+            index = parent.index(tag)
+            parent[index:index] = root
             root = parent[index:len(root)]
         return self
+
+    def insertBefore(self, value):
+        value.before(self)
+        return self
+
+    def clone(self):
+        self[:] = [deepcopy(tag) for tag in self]
+        return self
+
+    def empty(self):
+        for tag in self:
+            tag.text = None
+            tag[:] = []
+        return self
+
+    def remove(expr=None):
+        if expr == None:
+            for tag in self:
+                parent = tag.getparent()
+                parent.remove(tag)
+        if expr != None:
+            results = PyQuery(expr, self)
+            results.remove()
+        return self