diff options
author | Leonard Richardson <leonardr@segfault.org> | 2015-06-23 19:50:23 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2015-06-23 19:50:23 -0400 |
commit | 81b0e7160db445f9d8fb8bf09ba306df87cac19e (patch) | |
tree | 49e28714198e01fbf1c96ff717d3a1bcbf569f20 /bs4/__init__.py | |
parent | 5bd09164de9b5a45ffea4171968b8186dcdc6f69 (diff) |
Force object_was_parsed() to keep the tree intact even when an element
from later in the document is moved into place. [bug=1430633]
Diffstat (limited to 'bs4/__init__.py')
-rw-r--r-- | bs4/__init__.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/bs4/__init__.py b/bs4/__init__.py index a53048d..9f602ae 100644 --- a/bs4/__init__.py +++ b/bs4/__init__.py @@ -310,6 +310,34 @@ class BeautifulSoup(Tag): self._most_recent_element = o parent.contents.append(o) + if parent.next_sibling: + # This node is being inserted into an element that has + # already been parsed. Deal with any dangling references. + index = parent.contents.index(o) + if index == 0: + previous_element = parent + previous_sibling = None + else: + previous_element = previous_sibling = parent.contents[index-1] + if index == len(parent.contents)-1: + next_element = parent.next_sibling + next_sibling = None + else: + next_element = next_sibling = parent.contents[index+1] + + o.previous_element = previous_element + if previous_element: + previous_element.next_element = o + o.next_element = next_element + if next_element: + next_element.previous_element = o + o.next_sibling = next_sibling + if next_sibling: + next_sibling.previous_sibling = o + o.previous_sibling = previous_sibling + if previous_sibling: + previous_sibling.next_sibling = o + def _popToTag(self, name, nsprefix=None, inclusivePop=True): """Pops the tag stack up to and including the most recent instance of the given tag. If inclusivePop is false, pops the tag |