diff --git a/lib/main.js b/lib/main.js index 8541f359..5a183274 100644 --- a/lib/main.js +++ b/lib/main.js @@ -66,8 +66,12 @@ function parse (src) { // Expand newlines if double quoted if (maybeQuote === '"') { - value = value.replace(/\\n/g, '\n') - value = value.replace(/\\r/g, '\r') + value = value.replace(/\\([\s\S])/g, function (_, ch) { + if (ch === 'n') return '\n' + if (ch === 'r') return '\r' + if (ch === '\\') return '\\' + return '\\' + ch + }) } // Add to object diff --git a/tests/.env b/tests/.env index 5af4b98c..df512684 100644 --- a/tests/.env +++ b/tests/.env @@ -23,6 +23,9 @@ DOUBLE_AND_SINGLE_QUOTES_INSIDE_BACKTICKS=`double "quotes" and single 'quotes' w EXPAND_NEWLINES="expand\nnew\nlines" DONT_EXPAND_UNQUOTED=dontexpand\nnewlines DONT_EXPAND_SQUOTED='dontexpand\nnewlines' +ESCAPED_BACKSLASH_N="keep\\nraw" +ESCAPED_BACKSLASH_R="keep\\rraw" +ESCAPED_DOUBLE_BACKSLASH="keep\\\\slashes" # COMMENTS=work INLINE_COMMENTS=inline comments # work #very #well INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work diff --git a/tests/test-parse.js b/tests/test-parse.js index 3537b929..09da460f 100644 --- a/tests/test-parse.js +++ b/tests/test-parse.js @@ -52,6 +52,11 @@ t.equal(parsed.EXPAND_NEWLINES, 'expand\nnew\nlines', 'expands newlines but only t.equal(parsed.DONT_EXPAND_UNQUOTED, 'dontexpand\\nnewlines', 'expands newlines but only if double quoted') t.equal(parsed.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines', 'expands newlines but only if double quoted') +t.equal(parsed.ESCAPED_BACKSLASH_N, 'keep\\nraw', 'treats \\\\n in double quotes as literal backslash-n, not newline') + +t.equal(parsed.ESCAPED_BACKSLASH_R, 'keep\\rraw', 'treats \\\\r in double quotes as literal backslash-r, not carriage return') + +t.equal(parsed.ESCAPED_DOUBLE_BACKSLASH, 'keep\\\\slashes', 'collapses \\\\\\\\ in double quotes to \\\\') t.notOk(parsed.COMMENTS, 'ignores commented lines')