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

modern browsers eat first newline in textarea. #69

Merged
merged 2 commits into from May 4, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/html/form_inputs.html
Expand Up @@ -32,5 +32,10 @@
<form method="POST" id="textarea_input_form">
<textarea name="textarea">&#39;&#x66;&#x6f;&#x6f;&amp;&#x62;&#x61;&#x72;&#39;</textarea>
</form>
<form method="POST" id="textarea_emptyline_form">
<textarea name="textarea">
aaa</textarea>
</form>

</body>
</html>
7 changes: 7 additions & 0 deletions tests/test_forms.py
Expand Up @@ -195,6 +195,13 @@ def test_textarea_entities(self):
self.assertEqual(form.get("textarea").value, "'foo&bar'")
self.assertEqual(form.submit_fields(), [('textarea', "'foo&bar'")])

def test_textarea_emptyfirstline(self):
app = self.callFUT()
res = app.get('/form.html')
form = res.forms.get("textarea_emptyline_form")
self.assertEqual(form.get("textarea").value, "aaa")
self.assertEqual(form.submit_fields(), [('textarea', "aaa")])


class TestFormLint(unittest.TestCase):

Expand Down
8 changes: 7 additions & 1 deletion webtest/forms.py
Expand Up @@ -378,7 +378,13 @@ def _parse_fields(self):
name = attrs.pop('name')

if tag == 'textarea':
attrs['value'] = node.text
if node.text.startswith('\r\n'):
text = node.text[2:]
elif node.text.startswith('\n'):
text = node.text[1:]
else:
text = node.text
attrs['value'] = text
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just do node.text.strip()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nо, we don`t want to delete last newlines chars, and the second newline from begining of the node.text. But strip removes all whitespaces (space, tab, \b) from start and end of the string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node.text.lstrip('\r\n') then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this removes \r\r\r\r\r\r\r or \r\n\r\n\r\n\r\n or \n\n\n\n\n\n\n\n or \n\r\r\r\r\r\n\r\n\r


tag_type = attrs.get('type', 'text').lower()
if tag == 'select':
Expand Down