summaryrefslogtreecommitdiff
path: root/bs4/element.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2013-05-08 12:06:35 -0400
committerLeonard Richardson <leonardr@segfault.org>2013-05-08 12:06:35 -0400
commitb225c0cedf7e17b9780df7cab99dd6cd0c0a88e7 (patch)
tree568b7efe22f1a8d12014d960bef391983cb56a1a /bs4/element.py
parent0d6c2120fcca45dcc251cb7dd01ed70112956bbf (diff)
Aaand... it's now trivial to implement sibling selectors.
Diffstat (limited to 'bs4/element.py')
-rw-r--r--bs4/element.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/bs4/element.py b/bs4/element.py
index 75a76bb..6feced9 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -708,6 +708,17 @@ class PageElement(object):
# Run the next token as a CSS selector against the
# direct children of each tag in the current context.
recursive_candidate_generator = lambda tag: tag.children
+ elif token == '~':
+ # Run the next token as a CSS selector against the
+ # siblings of each tag in the current context.
+ recursive_candidate_generator = lambda tag: tag.next_siblings
+ elif token == '+':
+ # For each tag in the current context, run the next
+ # token as a CSS selector against the tag's next
+ # sibling that's a tag.
+ def next_tag_sibling(tag):
+ yield tag.find_next_sibling(True)
+ recursive_candidate_generator = next_tag_sibling
elif self.tag_name_re.match(token):
# Just a tag name.
@@ -761,7 +772,7 @@ class PageElement(object):
yield child
_use_candidate_generator = default_candidate_generator
else:
- _use_candidate_generator = lambda x: x.descendants
+ _use_candidate_generator = lambda tag: tag.descendants
else:
_use_candidate_generator = _candidate_generator