summaryrefslogtreecommitdiff
path: root/bs4/element.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2020-01-01 13:30:28 -0500
committerLeonard Richardson <leonardr@segfault.org>2020-01-01 13:30:28 -0500
commit981c34917f44d96b5c7fa3314bcf39c772d12a61 (patch)
treea42db5bee6974fe281d12fb8285cec6a50966471 /bs4/element.py
parenta021fc8a1aac56aa4a75c68fee5c4cb6a0e68551 (diff)
API CHANGE - Added PageElement.decomposed, a new property which lets you
check whether you've already called decompose() on a Tag or NavigableString.
Diffstat (limited to 'bs4/element.py')
-rw-r--r--bs4/element.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/bs4/element.py b/bs4/element.py
index e0da4d2..11bf8c3 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -802,6 +802,14 @@ class PageElement(object):
yield i
i = i.parent
+ @property
+ def decomposed(self):
+ """Check whether a PageElement has been decomposed.
+
+ :rtype: bool
+ """
+ return getattr(self, '_decomposed', False) or False
+
# Old non-property versions of the generators, for backwards
# compatibility with BS3.
def nextGenerator(self):
@@ -1211,15 +1219,21 @@ class Tag(PageElement):
This element will be removed from the tree and wiped out; so
will everything beneath it.
+
+ The behavior of a decomposed PageElement is undefined and you
+ should never use one for anything, but if you need to _check_
+ whether an element has been decomposed, you can use the
+ `decomposed` property.
"""
self.extract()
i = self
while i is not None:
- next = i.next_element
+ n = i.next_element
i.__dict__.clear()
i.contents = []
- i = next
-
+ i._decomposed = True
+ i = n
+
def clear(self, decompose=False):
"""Wipe out all children of this PageElement by calling extract()
on them.