From 660ff3f2175e77a34f272002262ee145334847b7 Mon Sep 17 00:00:00 2001 From: Derek Miller Date: Fri, 25 Oct 2019 10:42:28 -0700 Subject: [PATCH] Fix issue #15 where escaped escape chars were treated as escape chars --- CHANGELOG.md | 6 ++++++ index.js | 6 +++++- test/options.quotes.js | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38b08a7..aa66287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,12 @@ Changelog entries are classified using the following labels _(from [keep-a-chang +## [Unreleased] + +**Fixed** + +- Fix issue #15 where escaped escape chars were treated as escape chars + ## [6.1.0] - 2019-04-22 **Added** diff --git a/index.js b/index.js index 8e2078d..71a43dd 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ module.exports = (input, options = {}, fn) => { let string = input; let value, node; let i = -1; + let precedingEscapeCount = 0; state.bos = () => i === 0; state.eos = () => i === string.length; @@ -46,7 +47,7 @@ module.exports = (input, options = {}, fn) => { const closeIndex = (value, startIdx) => { let idx = string.indexOf(value, startIdx); - if (idx > -1 && string[idx - 1] === '\\') { + if (idx > -1 && precedingEscapeCount % 2 === 1) { idx = closeIndex(value, idx + 1); } return idx; @@ -59,6 +60,7 @@ module.exports = (input, options = {}, fn) => { // handle escaped characters if (value === '\\') { + precedingEscapeCount++; if (peek() === '\\') { append(value + next()); } else { @@ -68,6 +70,8 @@ module.exports = (input, options = {}, fn) => { append(next()); } continue; + } else if (string[i - 1] !== '\\') { + precedingEscapeCount = 0; } // handle quoted strings diff --git a/test/options.quotes.js b/test/options.quotes.js index 50f6539..fa5c9f5 100644 --- a/test/options.quotes.js +++ b/test/options.quotes.js @@ -85,4 +85,8 @@ describe('options.quotes', function() { assert.deepEqual(split("brian's"), ["brian's"]); assert.deepEqual(split("a.'b.c"), ['a', "'b", 'c']); }); + + it('should ignore escaped escape chars before quote', function() { + assert.deepEqual(split('\\\\\\\\"hello.world\\\\\\\\"', options), ['\\\\\\\\"hello.world\\\\\\\\"']); + }); });