Parser: improve quoted string escaping

fork
Brian White 10 years ago
parent d32d09a422
commit 05dd60ebb6

@ -648,7 +648,12 @@ function parseEnvelopeAddresses(list) {
function parseExpr(o, literals, result, start, useBrackets) {
start = start || 0;
var inQuote = false, lastPos = start - 1, isTop = false, val, isBody = false;
var inQuote = false,
lastPos = start - 1,
isTop = false,
isBody = false,
escaping = false,
val;
if (useBrackets === undefined)
useBrackets = true;
@ -669,7 +674,8 @@ function parseExpr(o, literals, result, start, useBrackets) {
}
} else if (o.str[i] === '"')
inQuote = true;
else if (o.str[i] === ' ' || o.str[i] === ')'
else if (o.str[i] === ' '
|| o.str[i] === ')'
|| (useBrackets && o.str[i] === ']')) {
if (i - (lastPos + 1) > 0) {
val = convStr(o.str.substring(lastPos + 1, i), literals);
@ -691,12 +697,13 @@ function parseExpr(o, literals, result, start, useBrackets) {
result.push(innerResult);
}
}
} else if (o.str[i] === '"' &&
(o.str[i - 1] &&
(o.str[i - 1] !== '\\'
|| (o.str[i - 2] && o.str[i - 2] === '\\')
)))
inQuote = false;
} else if (o.str[i] === '\\')
escaping = !escaping;
else if (o.str[i] === '"') {
if (!escaping)
inQuote = false;
escaping = false;
}
if (i + 1 === len && len - (lastPos + 1) > 0)
result.push(convStr(o.str.substring(lastPos + 1), literals));
}

@ -48,6 +48,25 @@ var assert = require('assert'),
expected: ['Who does not think \\"IMAP" is terrible\\bad?'],
what: 'Quoted string with escaped chars #4'
},
{ source: 'ENVELOPE ("Wed, 30 Mar 2014 02:38:23 +0100" "=?ISO-8859-1?Q?##ALLCAPS##123456## - ?= =?ISO-8859-1?Q?[ALERT][P3][ONE.TWO.FR] ?= =?ISO-8859-1?Q?Some Subject Line \\"D:\\\\\\"?=" (("Test Account (Rltvty L)" NIL "account" "test.com")) (("Test Account (Rltvty L)" NIL "account" "test.com")) ((NIL NIL "account" "test.com")) ((NIL NIL "one.two" "test.fr") (NIL NIL "two.three" "test.fr")) NIL NIL NIL "<message@test.eu>")',
expected: [
'ENVELOPE',
[ 'Wed, 30 Mar 2014 02:38:23 +0100',
'=?ISO-8859-1?Q?##ALLCAPS##123456## - ?= =?ISO-8859-1?Q?[ALERT][P3][ONE.TWO.FR] ?= =?ISO-8859-1?Q?Some Subject Line "D:\\"?=',
[ [ 'Test Account (Rltvty L)', null, 'account', 'test.com' ] ],
[ [ 'Test Account (Rltvty L)', null, 'account', 'test.com' ] ],
[ [ null, null, 'account', 'test.com' ] ],
[ [ null, null, 'one.two', 'test.fr' ],
[ null, null, 'two.three', 'test.fr' ]
],
null,
null,
null,
'<message@test.eu>'
]
],
what: 'Triple backslash in quoted string (GH Issue #345)'
},
].forEach(function(v) {
var result;

Loading…
Cancel
Save