From 82aa13e46232caf39ea8b27f554fd6c667b101cd Mon Sep 17 00:00:00 2001 From: Jayson Harshbarger Date: Tue, 4 Sep 2018 13:51:07 -0600 Subject: Fix braces parsing in quotes --- src/index.js | 56 ++++++++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/index.js b/src/index.js index c47fb38..5e12966 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ 'use strict'; -// A valid output which mean nothing has been parsed. +// A valid output which means nothing has been parsed. // Used as error return / invalid output const nothingHappend = { prop: { @@ -12,7 +12,7 @@ const nothingHappend = { }; const defaultConfig = { - defaultValue: () => undefined, // It's a function + defaultValue: () => undefined, // Its a function }; // Main function @@ -30,7 +30,7 @@ function parse(value, indexNext, userConfig) { const prop = {key: undefined /* {} */, class: undefined /* [] */, id: undefined}; - /* They is at leat one label and at best two */ + /* They are at leat one label and at best two */ /* ekqsdf <- one label * qsdfqsfd=qsdfqsdf <- two */ let labelFirst = ''; @@ -99,7 +99,7 @@ function parse(value, indexNext, userConfig) { return shouldStop(); }; - // In quote, every character is valid exept the unescaped quotes and CR or LF + // In quote, every character is valid except the unescaped quotes and CR or LF // Same function for single and double quote const eatInQuote = quote => { eaten = ''; @@ -132,13 +132,13 @@ function parse(value, indexNext, userConfig) { return shouldStop(); }; - // It's realy commun to eat only one character so let's make it a function - const eatOne = c => { + // It's really common to eat only one character so let's make it a function + const eatOne = (c, skipStopCheck) => { // Miam ! letsEat += c; indexNext++; - return shouldStop(); + return skipStopCheck ? false : shouldStop(); }; const addAttribute = () => { @@ -157,6 +157,7 @@ function parse(value, indexNext, userConfig) { break; case 'key': + if (!labelFirst) { return nothingHappend; } @@ -178,7 +179,7 @@ function parse(value, indexNext, userConfig) { /** *********************** Start parsing ************************ */ - // Let's check for trelling spaces first + // Let's check for leading spaces first eat(' \t\v'); if (value[indexNext] === '{') { @@ -217,35 +218,22 @@ function parse(value, indexNext, userConfig) { } if (value.charAt(indexNext) === '"') { - if (eatOne('"')) { - break; - } + eatOne('"', true); + eatInQuote('"', true); - if (eatInQuote('"')) { - break; - } - - if (value.charAt(indexNext) === '"') { - if (eatOne('"')) { - break; - } - } else { + if (value.charAt(indexNext) !== '"') { return nothingHappend; - } - } else if (value.charAt(indexNext) === '\'') { - if (eatOne('\'')) { - break; - } - if (eatInQuote('\'')) { + } else if (eatOne('"')) { break; } + } else if (value.charAt(indexNext) === `'`) { + eatOne(`'`, true); + eatInQuote(`'`, true); - if (value.charAt(indexNext) === '\'') { - if (eatOne('\'')) { - break; - } - } else { + if (value.charAt(indexNext) !== `'`) { return nothingHappend; + } else if (eatOne(`'`)) { + break; } } else if (eatUntil(' \t\n\r\v=}')) { break; @@ -265,11 +253,7 @@ function parse(value, indexNext, userConfig) { } } - if (errorDetected) { - return nothingHappend; - } - - return {prop, eaten: letsEat}; + return errorDetected ? nothingHappend : {prop, eaten: letsEat}; } module.exports = parse; -- cgit v1.2.3