diff options
author | Leonard Richardson <leonard.richardson@canonical.com> | 2012-03-15 11:08:02 -0400 |
---|---|---|
committer | Leonard Richardson <leonard.richardson@canonical.com> | 2012-03-15 11:08:02 -0400 |
commit | c8eb3028cc509eb603e8b113c66f7c4eeee828b9 (patch) | |
tree | 91a76b2921d9ed4a1f37dfc9ac6765d65cb82759 | |
parent | 4b0975ed159bd789db6896da159f654229310701 (diff) |
Fixed a bug where specifying 'text' while searching for a tag only worked if 'text' specified an exact string match. [bug=955942]
-rw-r--r-- | NEWS.txt | 5 | ||||
-rw-r--r-- | bs4/element.py | 4 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 12 |
3 files changed, 19 insertions, 2 deletions
@@ -1,3 +1,8 @@ += 4.0.2 () = + +* Fixed a bug where specifying `text` while searching for a tag only + worked if `text` specified an exact string match. [bug=955942] + = 4.0.1 (20120314) = * This is the first official release of Beautiful Soup 4. There is no diff --git a/bs4/element.py b/bs4/element.py index d2fa19f..4ff9cd4 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -1156,7 +1156,7 @@ class SoupStrainer(object): found = markup else: found = markup_name - if found and self.text and self.text != found.string: + if found and self.text and not self._matches(found.string, self.text): found = None return found searchTag = search_tag @@ -1188,7 +1188,7 @@ class SoupStrainer(object): return found def _matches(self, markup, match_against): - # print "Matching %s against %s" % (markup, match_against) + #print "Matching %s against %s" % (markup, match_against) result = False if isinstance(markup, list) or isinstance(markup, tuple): diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index e9a5763..4d114b7 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -126,6 +126,18 @@ class TestFindAllByName(TreeTest): self.assertSelects( self.tree.find_all('a'), ['First tag.', 'Nested tag.']) + def test_find_all_by_name_and_text(self): + self.assertSelects( + self.tree.find_all('a', text='First tag.'), ['First tag.']) + + self.assertSelects( + self.tree.find_all('a', text=True), ['First tag.', 'Nested tag.']) + + self.assertSelects( + self.tree.find_all('a', text=re.compile("tag")), + ['First tag.', 'Nested tag.']) + + def test_find_all_on_non_root_element(self): # You can call find_all on any node, not just the root. self.assertSelects(self.tree.c.find_all('a'), ['Nested tag.']) |