diff options
author | Leonard Richardson <leonardr@segfault.org> | 2013-08-15 08:31:34 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2013-08-15 08:31:34 -0400 |
commit | dd223ac4913e461951b4325bb3826259d62d29a8 (patch) | |
tree | f4d56982df75243443d12514e54e8e012a26f95c | |
parent | 064439f3b2decfb55b4e118ac4d41851d13c4c6f (diff) |
Make sure the optimized find_all() ResultSets actually contain the right data.
-rw-r--r-- | bs4/element.py | 7 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 10 |
2 files changed, 14 insertions, 3 deletions
diff --git a/bs4/element.py b/bs4/element.py index caa855e..da9afdf 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -484,16 +484,17 @@ class PageElement(object): strainer = SoupStrainer(name, attrs, text, **kwargs) if text is None and not limit and not attrs and not kwargs: - # Optimization to find all tags. if name is True or name is None: + # Optimization to find all tags. result = (element for element in generator if isinstance(element, Tag)) - ResultSet(strainer, result) - # Optimization to find all tags with a given name. + return ResultSet(strainer, result) elif isinstance(name, basestring): + # Optimization to find all tags with a given name. result = (element for element in generator if isinstance(element, Tag) and element.name == name) + return ResultSet(strainer, result) results = ResultSet(strainer) while True: try: diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 1c2c93b..f8515c0 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -70,6 +70,16 @@ class TestFind(TreeTest): soup = self.soup(u'<h1>Räksmörgås</h1>') self.assertEqual(soup.find(text=u'Räksmörgås'), u'Räksmörgås') + def test_find_everything(self): + """Test an optimization that finds all tags.""" + soup = self.soup("<a>foo</a><b>bar</b>") + self.assertEqual(2, len(soup.find_all())) + + def test_find_everything_with_name(self): + """Test an optimization that finds all tags with a given name.""" + soup = self.soup("<a>foo</a><b>bar</b><a>baz</a>") + self.assertEqual(2, len(soup.find_all('a'))) + class TestFindAll(TreeTest): """Basic tests of the find_all() method.""" |