Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/markupsafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,28 @@ def striptags(self, /) -> str:

value = f"{value[:start]}{value[end + 3 :]}"

# remove tags using the same method
while (start := value.find("<")) != -1:
if (end := value.find(">", start)) == -1:
break

value = f"{value[:start]}{value[end + 1 :]}"
# Remove tags. Unlike the comment mark, the tag start mark is a single
# character, so removing a tag can never join two characters into a new
# start mark. That means one left-to-right pass finds exactly the same
# tags as repeatedly searching from the beginning would, while copying
# the remainder of the string once instead of once per tag.
if (start := value.find("<")) != -1 and (end := value.find(">", start)) != -1:
chunks = []
pos = 0

while True:
chunks.append(value[pos:start])
pos = end + 1

if (start := value.find("<", pos)) == -1:
break

# an unclosed tag ends the search, keeping the rest as-is
if (end := value.find(">", start)) == -1:
break

chunks.append(value[pos:])
value = "".join(chunks)

# collapse spaces
value = " ".join(value.split())
Expand Down
Loading