diff options
-rw-r--r-- | NEWS.txt | 3 | ||||
-rw-r--r-- | bs4/element.py | 3 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 6 |
3 files changed, 11 insertions, 1 deletions
@@ -43,6 +43,9 @@ corresponding problem on the Beautiful Soup end that was previously invisible. [bug=984936] +* Fixed an exception when an overspecified CSS selector didn't match + anything. Code by Stefaan Lippens. [bug=1168167] + = 4.1.3 (20120820) = * Skipped a test under Python 2.6 and Python 3.1 to avoid a spurious diff --git a/bs4/element.py b/bs4/element.py index 67f2a79..2834fcb 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -638,6 +638,9 @@ class PageElement(object): tag, id = token.split('#', 1) if tag == "": tag = True + if len(current_context) == 0: + # No match. + return [] el = current_context[0].find(tag, {'id': id}) if el is None: return [] # No match diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 5f9e24b..ac60aa1 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1513,7 +1513,7 @@ class TestSoupSelector(TreeTest): </head> <body> -<div id="main"> +<div id="main" class="fancy"> <div id="inner"> <h1 id="header1">An H1</h1> <p>Some text</p> @@ -1782,3 +1782,7 @@ class TestSoupSelector(TreeTest): # The <div id="inner"> tag was selected. The <div id="footer"> # tag was not. self.assertSelectsIDs(selected, ['inner']) + + def test_overspecified_child_id(self): + self.assertSelects(".fancy #inner", ['inner']) + self.assertSelects(".normal #inner", []) |