diff options
| author | Matti Pohjanvirta <matti.pohjanvirta@iki.fi> | 2025-04-20 18:22:51 +0300 |
|---|---|---|
| committer | Natalia <124304+nessita@users.noreply.github.com> | 2025-04-23 17:33:02 -0300 |
| commit | e61e3daaf037507211028494d61f24382be31e5a (patch) | |
| tree | 3fb8725598db083cdd098a3d65b66af3b7f2ea3d /django/utils | |
| parent | 07edc976c7308a223e1b1dc3c62bd80b3bfef560 (diff) | |
[4.2.x] Fixed #36341 -- Preserved whitespaces in wordwrap template filter.
Regression in 55d89e25f4115c5674cdd9b9bcba2bb2bb6d820b.
This work improves the django.utils.text.wrap() function to ensure that
empty lines and lines with whitespace only are kept instead of being
dropped.
Thanks Matti Pohjanvirta for the report and fix.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Backport of 1e9db35836d42a3c72f3d1015c2f302eb6fee046 from main.
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/text.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/django/utils/text.py b/django/utils/text.py index 81ae88dc76..b018f2601f 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -102,10 +102,19 @@ def wrap(text, width): width=width, break_long_words=False, break_on_hyphens=False, + replace_whitespace=False, ) result = [] - for line in text.splitlines(True): - result.extend(wrapper.wrap(line)) + for line in text.splitlines(): + wrapped = wrapper.wrap(line) + if not wrapped: + # If `line` contains only whitespaces that are dropped, restore it. + result.append(line) + else: + result.extend(wrapped) + if text.endswith("\n"): + # If `text` ends with a newline, preserve it. + result.append("") return "\n".join(result) |
