Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pyquery/pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,17 @@ def filter(self, selector):
[<p.hello>]
>>> d('p').filter(lambda i, this: PyQuery(this).text() == 'Hi')
[<p.hello>]

A PyQuery object may also be used, keeping the elements of self that
are also part of it:

>>> d('p').filter(d('.hello'))
[<p.hello>]
"""
if isinstance(selector, self.__class__):
keep = {id(el) for el in selector}
return self._copy([el for el in self if id(el) in keep],
parent=self)
if not hasattr(selector, '__call__'):
return self._filter_only(selector, self)
else:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_pyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ def test_filter(self):
d = pq('<p>Hello <b>warming</b> world</p>')
self.assertEqual(d('strong').filter(lambda el: True), [])

def test_filter_pyquery_object(self):
d = pq('<div page="1">a</div><div page="2">b</div>')
self.assertEqual(d('div').filter(d('[page="2"]')), d('[page="2"]'))
self.assertEqual(d('div').filter(pq('<div page="3">c</div>')), [])

def test_not(self):
assert len(self.klass('div', self.html).not_('.node3')) == 1

Expand Down