From 0b6bee071fbd8d3171fb1ac4fb669baace808438 Mon Sep 17 00:00:00 2001 From: Jeff Dairiki Date: Wed, 24 Jan 2024 16:53:37 -0800 Subject: [PATCH] collapse spaces after stripping tags --- CHANGES.rst | 2 ++ src/markupsafe/__init__.py | 5 +++-- tests/test_markupsafe.py | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d1074d33..8232d78c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,8 @@ Version 2.1.5 Unreleased +- Fix ``striptags`` not collapsing spaces. :issue:`417` + Version 2.1.4 ------------- diff --git a/src/markupsafe/__init__.py b/src/markupsafe/__init__.py index 1e0f0228..e8903bcd 100644 --- a/src/markupsafe/__init__.py +++ b/src/markupsafe/__init__.py @@ -158,8 +158,7 @@ def striptags(self) -> str: >>> Markup("Main »\tAbout").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. @@ -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 diff --git a/tests/test_markupsafe.py b/tests/test_markupsafe.py index ea9a9187..94bea387 100644 --- a/tests/test_markupsafe.py +++ b/tests/test_markupsafe.py @@ -73,7 +73,7 @@ def test_escaping(escape): Markup( "" "Foo & Bar" - "" + " \n " "" "" ""