summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bs4/__init__.py20
-rw-r--r--bs4/element.py22
2 files changed, 22 insertions, 20 deletions
diff --git a/bs4/__init__.py b/bs4/__init__.py
index cc4b27f..c78c54e 100644
--- a/bs4/__init__.py
+++ b/bs4/__init__.py
@@ -302,28 +302,18 @@ class BeautifulSoup(Tag):
def object_was_parsed(self, o, parent=None, most_recent_element=None):
"""Add an object to the parse tree."""
parent = parent or self.currentTag
- most_recent_element = most_recent_element or self._most_recent_element
+ previous_element = most_recent_element or self._most_recent_element
+ next_element = previous_sibling = next_sibling = None
if isinstance(o, Tag):
next_element = o.next_element
next_sibling = o.next_sibling
previous_sibling = o.previous_sibling
- previous_element = o.previous_element
+ if not previous_element:
+ previous_element = o.previous_element
- o.setup(parent, most_recent_element)
+ o.setup(parent, previous_element, next_element, previous_sibling, next_sibling)
- if isinstance(o, Tag):
- if next_element:
- o.next_element = next_element
- if next_sibling:
- o.next_sibling = next_sibling
- if previous_sibling:
- o.previous_sibling = previous_sibling
- if previous_element:
- o.previous_element = previous_element
-
- if most_recent_element is not None:
- most_recent_element.next_element = o
self._most_recent_element = o
parent.contents.append(o)
diff --git a/bs4/element.py b/bs4/element.py
index f236216..9dc7b47 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -185,18 +185,30 @@ class PageElement(object):
return self.HTML_FORMATTERS.get(
name, HTMLAwareEntitySubstitution.substitute_xml)
- def setup(self, parent=None, previous_element=None):
+ def setup(self, parent=None, previous_element=None, next_element=None,
+ previous_sibling=None, next_sibling=None):
"""Sets up the initial relations between this element and
other elements."""
self.parent = parent
+
self.previous_element = previous_element
if previous_element is not None:
self.previous_element.next_element = self
+
self.next_element = next_element
- self.previous_sibling = None
- self.next_sibling = None
- if self.parent is not None and self.parent.contents:
- self.previous_sibling = self.parent.contents[-1]
+ if self.next_element:
+ self.next_element.previous_element = self
+
+ self.next_sibling = next_sibling
+ if self.next_sibling:
+ self.next_sibling.previous_sibling = self
+
+ if (not previous_sibling
+ and self.parent is not None and self.parent.contents):
+ previous_sibling = self.parent.contents[-1]
+
+ self.previous_sibling = previous_sibling
+ if previous_sibling:
self.previous_sibling.next_sibling = self
nextSibling = _alias("next_sibling") # BS3