Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed unnecessary NewLine from Textarea From Widget #5987

Closed
wants to merge 1 commit into from
Closed

Removed unnecessary NewLine from Textarea From Widget #5987

wants to merge 1 commit into from

Conversation

edelbluth
Copy link

When building a form with Django and using a Textarea Widget, the value rendering is always prepended by a NewLine (\r\n).
This makes the textarea never empty when it is, and changes the value for the following submissions.

This commit fixes this behaviour with just a trivial change.

Explanation:

When there is no (or an empty) value defined for the textarea, it shall be rendered as

<textarea></textarea>

and not as

<textarea>
</textarea>

At least in XHTML5 documents, this is a major difference.

Let's see what happens with a value:

This\nis\na\nvalue

This is currently rendered as this:

<textarea>
This
is
a
value</textarea>

Which renders in the browser to:

+-------------------------------------------+
|                                           |  <- empty line!
| This                                      |
| is                                        |
| a                                         |
| value                                     |
+-------------------------------------------+

After applying my patch, the rendering changes to this:

<textarea>This
is
a
value</textarea>

Which (correctly) renders in the browser to:

+-------------------------------------------+
| This                                      |
| is                                        |
| a                                         |
| value                                     |
+-------------------------------------------+

As a workaround to get around with the current implementation, I've overridden the render method of the original Widget class:

from django import forms
from django.forms.utils import flatatt
from django.utils.encoding import force_text
from django.utils.html import format_html

class FixedTextarea(forms.Textarea):

    def render(self, name, value, attrs=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, name=name)
        return format_html('<textarea{}>{}</textarea>',
                           flatatt(final_attrs),
                           force_text(value))

When building a form with Django and using a Textarea Widget, the value rendering is always prepended by a NewLine (`\r\n`).
This makes the textarea never empty when it is, and changes the value for the following submissions.

This commit fixes that behaviour.

Explanation:

When there is no (or an empty) value defined for the textarea, it shall be rendered as

```html
<textarea></textarea>
```

and not as

```html
<textarea>
</textarea>
```

At least in XHTML5 documents, this is a major difference.

Let's see what happens with a value:

```text
This\nis\na\nvalue
```

This is currently rendered as this:

```html
<textarea>
This
is
a
value</textarea>
```

Which renders in the browser to:

```text
+-------------------------------------------+
|                                           |  <- empty line!
| This                                      |
| is                                        |
| a                                         |
| value                                     |
+-------------------------------------------+
```

After applying my patch, the rendering changes to this:

```html
<textarea>This
is
a
value</textarea>
```

Which (correctly) renders in the browser to:

```text
+-------------------------------------------+
| This                                      |
| is                                        |
| a                                         |
| value                                     |
+-------------------------------------------+
```

As a workaround to get around with the current implementation, I've overridden the `render` method of the original Widget class:

```python
from django import forms
from django.forms.utils import flatatt
from django.utils.encoding import force_text
from django.utils.html import format_html

class FixedTextarea(forms.Textarea):

    def render(self, name, value, attrs=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, name=name)
        return format_html('<textarea{}>{}</textarea>',
                           flatatt(final_attrs),
                           force_text(value))
```
@aaugustin
Copy link
Member

This essentially reverts 78f6669 which will reintroduce Trac 8627.

I don't know why the test introduced in that commit doesn't catch the regression, though.

@timgraham
Copy link
Member

Please see https://code.djangoproject.com/ticket/24871 for discussion of the issue.

@timgraham timgraham closed this Jan 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants