summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Richardson <leonard.richardson@canonical.com>2013-06-03 11:10:53 -0400
committerLeonard Richardson <leonard.richardson@canonical.com>2013-06-03 11:10:53 -0400
commitf5ff09d09ef6c60f5d5052fac25d4e128bdce34f (patch)
tree17703f7f04f6009cad66ad22681be1837a35e273
parent57dd5f5898e46fa14c1376dcdf9df1ae83c13072 (diff)
_last_descendant can be optimized in some cases.
-rw-r--r--bs4/element.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/bs4/element.py b/bs4/element.py
index 40b1631..742624d 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -255,8 +255,10 @@ class PageElement(object):
self.previous_sibling = self.next_sibling = None
return self
- def _last_descendant(self):
+ def _last_descendant(self, is_initialized=True):
"Finds the last element beneath this object to be parsed."
+ if is_initialized and self.next_sibling:
+ return self.next_sibling.previous_element
last_child = self
while isinstance(last_child, Tag) and last_child.contents:
last_child = last_child.contents[-1]
@@ -294,11 +296,11 @@ class PageElement(object):
previous_child = self.contents[position - 1]
new_child.previous_sibling = previous_child
new_child.previous_sibling.next_sibling = new_child
- new_child.previous_element = previous_child._last_descendant()
+ new_child.previous_element = previous_child._last_descendant(False)
if new_child.previous_element is not None:
new_child.previous_element.next_element = new_child
- new_childs_last_element = new_child._last_descendant()
+ new_childs_last_element = new_child._last_descendant(False)
if position >= len(self.contents):
new_child.next_sibling = None