aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJayson Harshbarger <hypercubed@gmail.com>2018-09-04 13:51:07 -0600
committerJayson Harshbarger <hypercubed@gmail.com>2018-09-04 13:51:07 -0600
commit82aa13e46232caf39ea8b27f554fd6c667b101cd (patch)
treefe31f6d82c9c165162f0505379b3c5c20025b1df
parentMerge pull request #3 from Hypercubed/patch-1 (diff)
Fix braces parsing in quotes
-rw-r--r--.gitignore3
-rw-r--r--__tests__/index.js14
-rw-r--r--src/index.js56
3 files changed, 37 insertions, 36 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3690334
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+node_modules/
+__tests__/__snapshots__/
+package-lock.json \ No newline at end of file
diff --git a/__tests__/index.js b/__tests__/index.js
index 56158bb..1be433c 100644
--- a/__tests__/index.js
+++ b/__tests__/index.js
@@ -546,3 +546,17 @@ test('defaultValue name', t => {
t.is(r.eaten, 'visible');
});
+test('braces in attr', t => {
+ const toParse = `{ data-json='{"a": 1, "b": 2}' }`;
+ const r = parse(toParse);
+ t.is(r.prop['data-json'], '{"a": 1, "b": 2}');
+ t.is(r.eaten, toParse);
+});
+
+test('nested braces in attr', t => {
+ const toParse = `{ data-json='{"a": 1, "b": { "c": 4 }}' }`;
+ const r = parse(toParse);
+ t.is(r.prop['data-json'], '{"a": 1, "b": { "c": 4 }}');
+ t.is(r.eaten, toParse);
+});
+
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;