Skip to content

Commit

Permalink
normalize linefeeds for textareas
Browse files Browse the repository at this point in the history
As the native FormData implementation converts linefeeds (LF) provided through user input to CRLF, the polyfill should do the same.
  • Loading branch information
visualjerk authored and jimmywarting committed Dec 21, 2018
1 parent 737b80e commit 6954459
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 18 deletions.
12 changes: 11 additions & 1 deletion FormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ if (typeof FormData === 'undefined' || !FormData.prototype.keys) {
: [name + '', value + '']
}

// normalize linefeeds for textareas
// https://html.spec.whatwg.org/multipage/form-elements.html#textarea-line-break-normalisation-transformation
function normalizeLinefeeds(value) {
if (typeof value === "string") {
value = value.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n")
}
return value
}

function each (arr, cb) {
for (let i = 0; i < arr.length; i++) {
cb(arr[i])
Expand Down Expand Up @@ -141,7 +150,8 @@ if (typeof FormData === 'undefined' || !FormData.prototype.keys) {
} else if (elm.type === 'checkbox' || elm.type === 'radio') {
if (elm.checked) self.append(elm.name, elm.value)
} else {
self.append(elm.name, elm.value)
const value = elm.type === 'textarea' ? normalizeLinefeeds(elm.value) : elm.value
self.append(elm.name, value)
}
})
}
Expand Down
18 changes: 9 additions & 9 deletions formdata.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formdata-polyfill",
"version": "3.0.13",
"version": "3.0.14",
"description": "HTML5 `FormData` polyfill for Browsers.",
"main": "formdata.min.js",
"scripts": {
Expand Down
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,56 @@ window.File = new Proxy(nativeFile, {
})
})

describe('linebreaks', () => {
// Native FormData normalizes linefeeds in textareas to CRLF
// In order to be consistent the polyfill should do the same
it('Should convert LF to CRLF for textareas', () => {
// This can not be tested with 'create_form' as the function ignores \n
const form = document.createElement('form')
const textarea = document.createElement('textarea')
textarea.name = 'key'
textarea.value = '\n'
form.appendChild(textarea)
const fd = new FormData(form)
const value = fd.get('key')
assert.equal('\r\n', value)
})

it('Should convert CR to CRLF for textareas', () => {
const form = document.createElement('form')
const textarea = document.createElement('textarea')
textarea.name = 'key'
textarea.value = '\r'
form.appendChild(textarea)
const fd = new FormData(form)
const value = fd.get('key')
assert.equal('\r\n', value)
})

it('Should normalize mixed linefeeds to CRLF for textareas', () => {
const form = document.createElement('form')
const textarea = document.createElement('textarea')
textarea.name = 'key'
textarea.value = 'a\n\ra\r\na\n\r\n\r\n\r\n\na\r\r'
form.appendChild(textarea)
const fd = new FormData(form)
const value = fd.get('key')
assert.equal('a\r\n\r\na\r\na\r\n\r\n\r\n\r\n\r\na\r\n\r\n', value)
})

it('Should not convert LF to CRLF when provided by append', () => {
const fd = create_formdata(['key', '\n'])
const value = fd.get('key')
assert.equal('\n', value)
})

it('Should not convert CR to CRLF when provided by append', () => {
const fd = create_formdata(['key', '\r'])
const value = fd.get('key')
assert.equal('\r', value)
})
})

describe('disabled', () => {
it('Shold not include disabled fields', () => {
const fd = create_form(
Expand Down

0 comments on commit 6954459

Please sign in to comment.