summaryrefslogtreecommitdiff
path: root/django/utils/text.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/text.py')
-rw-r--r--django/utils/text.py9
1 files changed, 5 insertions, 4 deletions
diff --git a/django/utils/text.py b/django/utils/text.py
index bad1da6729..29f5c8f2e8 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -126,10 +126,11 @@ class TruncateHTMLParser(HTMLParser):
def handle_endtag(self, tag):
if tag not in self.void_elements:
self.output += f"</{tag}>"
- try:
- self.tags.remove(tag)
- except ValueError:
- pass
+ # Remove from the stack only if the tag matches the most recently
+ # opened tag (LIFO). This avoids O(n) linear scans for unmatched
+ # end tags if `deque.remove()` would be called.
+ if self.tags and self.tags[0] == tag:
+ self.tags.popleft()
def handle_data(self, data):
data, output = self.process(data)