Commits

Olivier Lauzanne  committed 47c1c48

replace methods

  • Participants
  • Parent commits 480986c

Comments (0)

Files changed (2)

File pyquery/README.txt

     >>> p.attr("id", "hello")
     [<p#hello.hello>]
 
-Same thing the pythonic way::    
+
+Same thing the pythonic way::
 
     >>> p.attr.id = "plop"
     >>> p.attr.id
     >>> p.attr("style")
     'font-size: 17px'
 
-Same thing the pythonic way ('_' characters are translated to '-')::    
+Same thing the pythonic way ('_' characters are translated to '-')::
 
     >>> p.css.font_size = "16px"
     >>> p.attr.style
     >>> d('body').html()
     '\n<p class="hello" id="hello" style="font-size: 17px">...'
 
-
 Doing something for each elements::
 
     >>> p.each(lambda e: e.addClass('hello2'))
     [<p#hello.hello2.hello>]
 
+Remove an element::
+
+    >>> d.remove('p#id')
+    [<html>]
+    >>> d('p#id')
+    []
+
+Replace an element by another::
+
+    >>> p.replaceWith('<p>testing</p>')
+    [<p#hello.hello2.hello>]
+    >>> d('p')
+    [<p>, <p#test>]
+
+Or the other way around::
+
+    >>> d('<h1>arya stark</h1>').replaceAll('p')
+    [<h1>]
+    >>> d('p')
+    []
+    >>> d('h1')
+    [<h1>, <h1>]
+
 And you can get back the modified html::
 
     >>> print d
     <html>
-    ...style="font-size: 17px"...
+    <body>
+    <h1>arya stark</h1><h1>arya stark</h1></body>
     </html>
 
 You can generate html stuff::

File pyquery/pyquery.py

 from copy import deepcopy
 
 def selector_to_xpath(selector):
-    '''JQuery selector to xpath.
+    """JQuery selector to xpath.
     TODO: patch cssselect to add :first, :last, ...
-    '''
+    """
     selector = selector.replace('[@', '[')
     return css_to_xpath(selector)
 
             raise NotImplementedError()
 
 class PyQuery(list):
-    '''See the pyquery module docstring.
-    '''
+    """See the README.txt"""
     def __init__(self, *args, **kwargs):
         html = None
         elements = []
         length = len(args)
         if length == 0:
             raise ValueError('You must provide at least a selector')
-        if len(args) == 1:
+        if len(args) == 1 and not args[0].startswith('<'):
             args += (self,)
-        return self.__class__(*args)
-
+        result = self.__class__(*args)
+        object.__setattr__(result, '_parent', self)
+        return result
     # keep original list api prefixed with _
     _append = list.append
     _extend = list.extend
     def length(self):
         return len(self)
 
+    def size():
+        return len(self)
+
     ##############
     # Attributes #
     ##############
         value.before(self)
         return self
 
+    def replaceWith(self, value):
+        self.before(value)
+        for tag in self:
+            parent = tag.getparent()
+            parent.remove(tag)
+        return self
+
+    def replaceAll(self, expr):
+        self._parent(expr).replaceWith(self)
+        return self
+
     def clone(self):
         self[:] = [deepcopy(tag) for tag in self]
         return self
             tag[:] = []
         return self
 
-    def remove(expr=NoDefault):
+    def remove(self, expr=NoDefault):
         if expr is NoDefault:
             for tag in self:
                 parent = tag.getparent()