Home Features Philosophy Docs Blog Errors Security Examples FAQ
DJE-010 Error Template

Textarea newlines stripped by whitespace minifier

Error message

Textarea content loses newlines and formatting after render

The _strip_comments_and_whitespace() function in mixins/template.py used re.sub(r'\s+', ' ') which collapsed all whitespace including newlines inside <textarea> elements. This caused multi-line textarea content to appear as a single line after server-side rendering. Fixed in v0.3.0 by adding <textarea> to the preserved blocks alongside <pre> and <code>.

bug-fixed template textarea whitespace

Affected versions: <0.3.0

Solution

Before (problematic)
# In mixins/template.py (old behavior)
def _strip_comments_and_whitespace(html):
    # This collapsed ALL whitespace, including in textareas
    html = re.sub(r"\s+", " ", html)
    return html
After (fixed)
# In mixins/template.py (fixed)
def _strip_comments_and_whitespace(html):
    # Preserve content in <pre>, <code>, and <textarea> blocks
    preserved = {}
    for tag in ("pre", "code", "textarea"):
        pattern = re.compile(
            rf"(<{tag}[^>]*>)(.*?)(</{tag}>)",
            re.DOTALL
        )
        # ... preserve and restore blocks