Commits

Gael Pasgrimaud  committed 14d27d7 Merge

merge / fix tests

  • Participants
  • Parent commits e64f5e8, 8376a53

Comments (0)

Files changed (2)

File pyquery/pyquery.py

         elements = [child for tag in self for child in tag.getchildren()]
         return self._filter_only(selector, elements)
 
+    def closest(self, selector=None):
+        """
+            >>> d = PyQuery('<div class="hello"><p>This is a <strong class="hello">test</strong></p></div>')
+            >>> d('strong').closest('div')
+            [<div.hello>]
+            >>> d('strong').closest('.hello')
+            [<strong.hello>]
+            >>> d('strong').closest('form')
+            []
+        """
+        try:
+            current = self[0]
+        except IndexError:
+            current = None
+        while current is not None and not self.__class__(current).is_(selector):
+            current = current.getparent()
+        return self.__class__(current, **dict(parent=self))
+
     def filter(self, selector):
         """Filter elements in self using selector (string or function).
 
     def replaceWith(self, value):
         """replace nodes by value
         """
-        self.before(value)
-        for tag in self:
-            parent = tag.getparent()
-            parent.remove(tag)
+        if callable(value):
+            for i, element in enumerate(self):
+                self.__class__(element).before(value(i, element) + (element.tail or ''))
+                parent = element.getparent()
+                parent.remove(element)
+        else:
+            for tag in self:
+                self.__class__(tag).before(value + (tag.tail or ''))
+                parent = tag.getparent()
+                parent.remove(tag)
         return self
 
     def replaceAll(self, expr):

File pyquery/test.py

         assert len(self.klass('div', self.html).find('span').end()) == 2
         assert len(self.klass('#node2', self.html).find('span').end()) == 1
 
+    def test_closest(self):
+        assert len(self.klass('#node1 span', self.html).closest('body')) == 1
+        assert self.klass('#node2', self.html).closest('.node3').attr('id') == 'node2'
+        assert self.klass('.node3', self.html).closest('form') == []
 
 class TestOpener(unittest.TestCase):
 
     xml = "<div>I'm valid XML</div>"
     html = '''
     <div class="portlet">
-        Behind you, a three-headed HTML&dash;Entity!
+      <a href="/toto">TestimageMy link text</a>
+      <a href="/toto2">imageMy link text 2</a>
+      Behind you, a three-headed HTML&dash;Entity!
     </div>
     '''
-    
     def test_parser_persistance(self):
         d = pq(self.xml, parser='xml')
         self.assertRaises(etree.XMLSyntaxError, lambda: d.after(self.html))
         d = pq(self.xml, parser='html')
         d.after(self.html) # this should not fail
-        
+
+    def test_replaceWith(self):
+        expected = '''<div class="portlet">
+      <a href="/toto">TestimageMy link text</a>
+      <a href="/toto2">imageMy link text 2</a>
+      Behind you, a three-headed HTML&amp;dash;Entity!
+    </div>'''
+        d = pq(self.html)
+        d('img').replaceWith('image')
+        val = d.__html__()
+        assert val == expected, (repr(val), repr(expected))
+
+    def test_replaceWith_with_function(self):
+        expected = '''<div class="portlet">
+      TestimageMy link text
+      imageMy link text 2
+      Behind you, a three-headed HTML&amp;dash;Entity!
+    </div>'''
+        d = pq(self.html)
+        d('a').replaceWith(lambda i, e: pq(e).html())
+        val = d.__html__()
+        assert val == expected, (repr(val), repr(expected))
+
 if __name__ == '__main__':
     fails, total = unittest.main()
     if fails == 0: