Skip to content

fix: preserve leading newlines in TextArea field (#732) - #767

Open
SHYXIN wants to merge 1 commit into
jowilf:mainfrom
SHYXIN:fix/textarea-leading-newline
Open

fix: preserve leading newlines in TextArea field (#732)#767
SHYXIN wants to merge 1 commit into
jowilf:mainfrom
SHYXIN:fix/textarea-leading-newline

Conversation

@SHYXIN

@SHYXIN SHYXIN commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

TextArea field values with leading newlines (e.g. \n\nline2) lost the first newline when rendered in the edit form. This is because the HTML spec states that browsers ignore the first newline immediately after the <textarea> opening tag.

Root Cause

The original template used {% if data -%} with whitespace-trimming (-%}), which produced:

<textarea...>{{ data }}</textarea>

When data was \n\nline2, the output was <textarea>\n\nline2</textarea>, and the browser consumed the first \n.

Fix

starlette_admin/templates/forms/textarea.html — Removed whitespace-trimming and added an explicit newline before {{ data }}:

<textarea...>
{{ data }}</textarea>

Now the browser consumes the template's newline, preserving the data's leading newlines.

Testing

  • Verified API returns correct value with leading newlines
  • Verified edit page textarea content preserves leading newlines
  • Verified "Save and Continue Editing" cycle preserves content

HTML spec: browsers ignore the first newline immediately after the
<textarea> opening tag. The original template used {% if data -%}
which trimmed whitespace, causing the textarea value to start directly
after <textarea> with no intermediate newline.

The fix removes the whitespace-trimming -%} and adds an explicit
newline + indented {{ data }} on the next line, so the browser
consumes the template's newline instead of the data's leading newline.

Before: <textarea...>{{ data }}</textarea>  → "\n\nline2" becomes "\nline2"
After:  <textarea...>\n{{ data }}</textarea> → "\n\nline2" is preserved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant