Skip to content

Commit

Permalink
Handle line breaks and carriage returns correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed May 27, 2024
1 parent a9a1c54 commit f10b340
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/utils/parseFormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ test('should normalize one pair', (t) => {
t.deepEqual(ret, expected)
})

test('should normalize line break', (t) => {
const data = 'value=1&text=Several+words%0D%0Ahere%0D%0Aand+here'
const expected = {
value: 1,
text: 'Several words\nhere\nand here',
}

const ret = parseFormData(data)

t.deepEqual(ret, expected)
})

test('should normalize keys with bracket postfix into array', (t) => {
const data = 'value=1&lines[]=Several+words&lines[]=on+several&lines[]=lines'
const expected = {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/parseFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const parseObject = (value: string) => {
}
}

const fixLineBreak = (value: string) => value.replace(/%0D%0A/g, '%0A')

const parseValue = (value?: string) =>
typeof value === 'string'
? parseObject(decodeURIComponent(value).replace(/\+/g, ' '))
? parseObject(decodeURIComponent(fixLineBreak(value)).replace(/\+/g, ' '))
: undefined

function reducePair(
Expand Down
24 changes: 24 additions & 0 deletions src/utils/stringifyFormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ test('should serialize uri', (t) => {
t.is(ret, expected)
})

test('should serialize line break', (t) => {
const data = {
value: 1,
text: 'Several words\nhere\nand here',
}
const expected = 'value=1&text=Several+words%0D%0Ahere%0D%0Aand+here'

const ret = stringifyFormData(data)

t.is(ret, expected)
})

test('should serialize carriage return and line break', (t) => {
const data = {
value: 1,
text: 'Several words\r\nhere\r\nand here',
}
const expected = 'value=1&text=Several+words%0D%0Ahere%0D%0Aand+here'

const ret = stringifyFormData(data)

t.is(ret, expected)
})

test('should serialize array to several keys with bracket postfix', (t) => {
const data = {
value: 1,
Expand Down
5 changes: 4 additions & 1 deletion src/utils/stringifyFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const formatObject = (value: unknown) =>
? value.toISOString()
: String(value)

const fixLineBreak = (value: unknown) =>
typeof value === 'string' ? value.replace(/(^|[^\r])\n/g, '$1\r\n') : value

const formatValue = (value: unknown) =>
encodeURIComponent(formatObject(value)).replace(/%20/g, '+')
encodeURIComponent(formatObject(fixLineBreak(value))).replace(/%20/g, '+')

function mapEntry([key, value]: [string, unknown]) {
if (Array.isArray(value)) {
Expand Down

0 comments on commit f10b340

Please sign in to comment.