-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly handle an unpaired quotes
- Loading branch information
Showing
2 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,25 +165,33 @@ const parse = (input, options = {}) => { | |
*/ | ||
|
||
if (value === CHAR_DOUBLE_QUOTE || value === CHAR_SINGLE_QUOTE || value === CHAR_BACKTICK) { | ||
const open = value; | ||
let next; | ||
const leftQuote = value; | ||
// Looking for the nearest unescaped quote of the same type. | ||
// @todo Use negative lookbehind after targeting the [email protected]+ | ||
const hasRightQuote = input.slice(index).search(new RegExp(`[^\\\\]${leftQuote}|^${leftQuote}`)) !== -1; | ||
|
||
if (options.keepQuotes !== true) { | ||
value = ''; | ||
} | ||
let next; | ||
|
||
while (index < length && (next = advance())) { | ||
if (next === CHAR_BACKSLASH) { | ||
value += next + advance(); | ||
continue; | ||
// If there is no right quote, consume an unpaired quote as a regular character. | ||
if (hasRightQuote) { | ||
if (options.keepQuotes !== true) { | ||
value = ''; | ||
} | ||
|
||
if (next === open) { | ||
if (options.keepQuotes === true) value += next; | ||
break; | ||
} | ||
while (index <= length && (next = advance())) { | ||
// Skip escaped quotes. | ||
if (next === CHAR_BACKSLASH) { | ||
value += next + advance(); | ||
continue; | ||
} | ||
|
||
value += next; | ||
if (next === leftQuote) { | ||
if (options.keepQuotes === true) value += next; | ||
break; | ||
} | ||
|
||
value += next; | ||
} | ||
} | ||
|
||
push({ type: 'text', value }); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters