Skip to content

Commit

Permalink
striptags collapses spaces correctly (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Feb 2, 2024
2 parents 73e6a48 + 0b6bee0 commit 60a6512
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Version 2.1.5

Unreleased

- Fix ``striptags`` not collapsing spaces. :issue:`417`


Version 2.1.4
-------------
Expand Down
5 changes: 3 additions & 2 deletions src/markupsafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ def striptags(self) -> str:
>>> Markup("Main &raquo;\t<em>About</em>").striptags()
'Main » About'
"""
# collapse spaces
value = " ".join(self.split())
value = str(self)

# Look for comments then tags separately. Otherwise, a comment that
# contains a tag would end early, leaving some of the comment behind.
Expand Down Expand Up @@ -193,6 +192,8 @@ def striptags(self) -> str:

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

# collapse spaces
value = " ".join(value.split())
return self.__class__(value).unescape()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/test_markupsafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_escaping(escape):
Markup(
"<!-- outer comment -->"
"<em>Foo &amp; Bar"
"<!-- inner comment about <em> -->"
" <!-- inner comment about <em> -->\n "
"</em>"
"<!-- comment\nwith\nnewlines\n-->"
"<meta content='tag\nwith\nnewlines'>"
Expand Down

0 comments on commit 60a6512

Please sign in to comment.