Skip to content

Fix issue #15 where escaped escape chars were treated as escape chars #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Changelog entries are classified using the following labels _(from [keep-a-chang

</details>

## [Unreleased]

**Fixed**

- Fix issue #15 where escaped escape chars were treated as escape chars

## [6.1.0] - 2019-04-22

**Added**
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -59,6 +60,7 @@ module.exports = (input, options = {}, fn) => {

// handle escaped characters
if (value === '\\') {
precedingEscapeCount++;
if (peek() === '\\') {
append(value + next());
} else {
Expand All @@ -68,6 +70,8 @@ module.exports = (input, options = {}, fn) => {
append(next());
}
continue;
} else if (string[i - 1] !== '\\') {
precedingEscapeCount = 0;
}

// handle quoted strings
Expand Down
4 changes: 4 additions & 0 deletions test/options.quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\\\\\\\\"']);
});
});