diff options
author | Leonard Richardson <leonardr@segfault.org> | 2016-07-18 20:20:00 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2016-07-18 20:20:00 -0400 |
commit | 3c769d98b64ba347dd7cb7e42d7d8b4ce1f8ed3c (patch) | |
tree | d267c11bf5578f6f852db076a933b15b669d2f23 | |
parent | 1288307d878e2c878be193fcb43d107ce952ade5 (diff) | |
download | beautifulsoup4-3c769d98b64ba347dd7cb7e42d7d8b4ce1f8ed3c.tar.gz |
The argument to now works correctly, though it's
not implemented very efficiently. [bug=1520530]
-rw-r--r-- | NEWS.txt | 3 | ||||
-rw-r--r-- | bs4/element.py | 5 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 11 |
3 files changed, 14 insertions, 5 deletions
@@ -21,6 +21,9 @@ BeautifulSoup object being instantiated without a parser being specified. [bug=1574647] +* The `limit` argument to `select()` now works correctly, though it's + not implemented very efficiently. [bug=1520530] + * Fixed a Python 3 ByteWarning when a URL was passed in as though it were markup. Thanks to James Salter for a patch and test. [bug=1533762] diff --git a/bs4/element.py b/bs4/element.py index 84c4a6e..7a3aa52 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -1537,13 +1537,12 @@ class Tag(PageElement): # don't include it in the context more than once. new_context.append(candidate) new_context_ids.add(id(candidate)) - if limit and len(new_context) >= limit: - break elif self._select_debug: print " FAILURE %s %s" % (candidate.name, repr(candidate.attrs)) - current_context = new_context + if limit and len(current_context) >= limit: + current_context = current_context[:limit] if self._select_debug: print "Final verdict:" diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 6b2a123..8a05990 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1676,8 +1676,8 @@ class TestSoupSelector(TreeTest): def setUp(self): self.soup = BeautifulSoup(self.HTML, 'html.parser') - def assertSelects(self, selector, expected_ids): - el_ids = [el['id'] for el in self.soup.select(selector)] + def assertSelects(self, selector, expected_ids, **kwargs): + el_ids = [el['id'] for el in self.soup.select(selector, **kwargs)] el_ids.sort() expected_ids.sort() self.assertEqual(expected_ids, el_ids, @@ -1720,6 +1720,13 @@ class TestSoupSelector(TreeTest): for selector in ('html div', 'html body div', 'body div'): self.assertSelects(selector, ['data1', 'main', 'inner', 'footer']) + + def test_limit(self): + self.assertSelects('html div', ['main'], limit=1) + self.assertSelects('html body div', ['inner', 'main'], limit=2) + self.assertSelects('body div', ['data1', 'main', 'inner', 'footer'], + limit=10) + def test_tag_no_match(self): self.assertEqual(len(self.soup.select('del')), 0) |