From 0343df1eb80d14705fe4de904ab1ad7a2f8df554 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Tue, 15 Mar 2016 20:24:55 -0400 Subject: [PATCH 01/27] more accurate paramToJson in parameter_hunter --- core/lib/parameter_hunter.js | 48 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 9567f2a46..49bc8c244 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -19,32 +19,38 @@ var parameter_hunter = function () { pattern_assembler = new pa(); function paramToJson(pString) { - var paramStringWellFormed = ''; - var paramStringTmp; var colonPos; var delimitPos; - var quotePos; + var paramKey; var paramString = pString; + var paramStringWellFormed = ''; + var paramStringTmp; + var quotePos; do { - //if param key is wrapped in single quotes, replace with double quotes. - paramString = paramString.replace(/(^\s*[\{|\,]\s*)'([^']+)'(\s*\:)/, '$1"$2"$3'); - - //if params key is not wrapped in any quotes, wrap in double quotes. - paramString = paramString.replace(/(^\s*[\{|\,]\s*)([^\s"'\:]+)(\s*\:)/, '$1"$2"$3'); - //move param key to paramStringWellFormed var. colonPos = paramString.indexOf(':'); //except to prevent infinite loops. if (colonPos === -1) { colonPos = paramString.length - 1; - } - else { + } else { colonPos += 1; } - paramStringWellFormed += paramString.substring(0, colonPos); + + paramKey = paramString.substring(0, colonPos); + + //if param key is wrapped in single quotes, replace with double quotes. + paramKey = paramKey.replace(/(^\s*[\{|\,]\s*)'([^']+)'(\s*\:)/, '$1"$2"$3'); + + //if params key is not wrapped in any quotes, wrap in double quotes. + paramKey = paramKey.replace(/(^\s*[\{|\,]\s*)([^\s"'\:]+)(\s*\:)/, '$1"$2"$3'); + + //this is just here to match the escaping scheme in Pattern Lab PHP. + paramKey = paramKey.replace(/\\/g, ''); + + paramStringWellFormed += paramKey; paramString = paramString.substring(colonPos, paramString.length).trim(); //if param value is wrapped in single quotes, replace with double quotes. @@ -54,8 +60,7 @@ var parameter_hunter = function () { //except for unclosed quotes to prevent infinite loops. if (quotePos === -1) { quotePos = paramString.length - 1; - } - else { + } else { quotePos += 2; } @@ -75,42 +80,37 @@ var parameter_hunter = function () { //move param key to paramStringWellFormed var. paramStringWellFormed += paramStringTmp; paramString = paramString.substring(quotePos, paramString.length).trim(); - } //if param value is wrapped in double quotes, just move to paramStringWellFormed var. - else if (paramString[0] === '"') { + } else if (paramString[0] === '"') { quotePos = paramString.search(/[^\\]"/); //except for unclosed quotes to prevent infinite loops. if (quotePos === -1) { quotePos = paramString.length - 1; - } - else { + } else { quotePos += 2; } //move param key to paramStringWellFormed var. paramStringWellFormed += paramString.substring(0, quotePos); paramString = paramString.substring(quotePos, paramString.length).trim(); - } //if param value is not wrapped in quotes, move everthing up to the delimiting comma to paramStringWellFormed var. - else { + } else { delimitPos = paramString.indexOf(','); //except to prevent infinite loops. if (delimitPos === -1) { delimitPos = paramString.length - 1; } - else { - delimitPos += 1; - } + paramStringWellFormed += paramString.substring(0, delimitPos); paramString = paramString.substring(delimitPos, paramString.length).trim(); } //break at the end. - if (paramString.length === 1) { + if (paramString.length <= 1) { paramStringWellFormed += paramString.trim(); paramString = ''; break; From 92e9a00ea906eb3dc890c984226a99d8dda6e8e9 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Tue, 15 Mar 2016 20:32:41 -0400 Subject: [PATCH 02/27] updating unit tests for parameter_hunter paramToJson --- test/parameter_hunter_tests.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/test/parameter_hunter_tests.js b/test/parameter_hunter_tests.js index 96d345792..f87fd0752 100644 --- a/test/parameter_hunter_tests.js +++ b/test/parameter_hunter_tests.js @@ -208,12 +208,12 @@ var patternlab = patternlabClosure(); var parameter_hunter = new ph(); - currentPattern.template = "{{> molecules-single-comment(\"description\": 'true not{\"true\"') }}"; + currentPattern.template = "{{> molecules-single-comment(\"description\": 'true not \"true\"') }}"; currentPattern.extendedTemplate = currentPattern.template; currentPattern.parameteredPartials[0] = currentPattern.template; parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not{"true"

'); + test.equals(currentPattern.extendedTemplate, '

true not "true"

'); test.done(); }, @@ -223,12 +223,27 @@ var patternlab = patternlabClosure(); var parameter_hunter = new ph(); - currentPattern.template = "{{> molecules-single-comment(\"description\": \"true not}\\\"true\\\"\") }}"; + currentPattern.template = "{{> molecules-single-comment(\"description\": \"true not \\\"true\\\"\") }}"; currentPattern.extendedTemplate = currentPattern.template; currentPattern.parameteredPartials[0] = currentPattern.template; parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not}"true"

'); + test.equals(currentPattern.extendedTemplate, '

true not "true"

'); + + test.done(); + }, + + 'parameter hunter parses parameters with combination of quoting schemes for keys and values' : function(test){ + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); + + currentPattern.template = "{{> molecules-single-comment(description: true, 'foo': false, \"bar\": false, 'single': true, 'singlesingle': 'true', 'singledouble': \"true\", \"double\": true, \"doublesingle\": 'true', \"doubledouble\": \"true\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; + + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

'); test.done(); } From 695471cff1792d2dddcab996e31ce959734d8fca Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Wed, 16 Mar 2016 06:35:17 -0400 Subject: [PATCH 03/27] minor edit --- core/lib/parameter_hunter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 49bc8c244..850a12143 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -15,8 +15,8 @@ var parameter_hunter = function () { var extend = require('util')._extend, pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), - style_modifier_hunter = new smh(), - pattern_assembler = new pa(); + pattern_assembler = new pa(), + style_modifier_hunter = new smh(); function paramToJson(pString) { var colonPos; From 8b3551fbe8ddaaf120ab4d058142bf41fd915e13 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Thu, 17 Mar 2016 18:32:18 -0400 Subject: [PATCH 04/27] another param string parser --- core/lib/parameter_hunter.js | 207 ++++++++++++++++++++--------------- package.gulp.json | 1 + package.json | 1 + 3 files changed, 123 insertions(+), 86 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 850a12143..6496275f4 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -13,6 +13,7 @@ var parameter_hunter = function () { var extend = require('util')._extend, + JSON5 = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), pattern_assembler = new pa(), @@ -20,105 +21,139 @@ var parameter_hunter = function () { function paramToJson(pString) { var colonPos; - var delimitPos; - var paramKey; - var paramString = pString; - var paramStringWellFormed = ''; - var paramStringTmp; - var quotePos; - + var keyCandidate = ''; + var keys = []; + var paramString; + var paramStringPrepared; + var regex; + var value; + var values = []; + var wrapper; + + //replace all escaped double-quotes with escaped unicode. + paramString = pString.replace(/\\"/g, '\\u0022'); + + //replace all escaped single-quotes with escaped unicode. + paramString = paramString.replace(/\\'/g, '\\u0027'); + + //with escaped quotes out of the way, crawl through paramString looking for + //keys and values. do { - //move param key to paramStringWellFormed var. - colonPos = paramString.indexOf(':'); - - //except to prevent infinite loops. - if (colonPos === -1) { - colonPos = paramString.length - 1; - } else { - colonPos += 1; - } - - paramKey = paramString.substring(0, colonPos); - - //if param key is wrapped in single quotes, replace with double quotes. - paramKey = paramKey.replace(/(^\s*[\{|\,]\s*)'([^']+)'(\s*\:)/, '$1"$2"$3'); - - //if params key is not wrapped in any quotes, wrap in double quotes. - paramKey = paramKey.replace(/(^\s*[\{|\,]\s*)([^\s"'\:]+)(\s*\:)/, '$1"$2"$3'); - - //this is just here to match the escaping scheme in Pattern Lab PHP. - paramKey = paramKey.replace(/\\/g, ''); - - paramStringWellFormed += paramKey; - paramString = paramString.substring(colonPos, paramString.length).trim(); - - //if param value is wrapped in single quotes, replace with double quotes. - if (paramString[0] === '\'') { - quotePos = paramString.search(/[^\\]'/); - - //except for unclosed quotes to prevent infinite loops. - if (quotePos === -1) { - quotePos = paramString.length - 1; - } else { - quotePos += 2; + //check if searching for a key + if (paramString[0] === '{' || paramString[0] === ',' || keyCandidate) { + paramString = paramString.substring([1], paramString.length).trim(); + + //find what, if any, type of quote wraps the key. + switch (paramString[0]) { + case '"': + wrapper = '"'; + break; + case '\'': + wrapper = '\''; + break; + default: + wrapper = ''; } - //prepare param value for move to paramStringWellFormed var. - paramStringTmp = paramString.substring(0, quotePos); - - //unescape any escaped single quotes. - paramStringTmp = paramStringTmp.replace(/\\'/g, '\''); - - //escape any double quotes. - paramStringTmp = paramStringTmp.replace(/"/g, '\\"'); - - //replace the delimiting single quotes with double quotes. - paramStringTmp = paramStringTmp.replace(/^'/, '"'); - paramStringTmp = paramStringTmp.replace(/'$/, '"'); - - //move param key to paramStringWellFormed var. - paramStringWellFormed += paramStringTmp; - paramString = paramString.substring(quotePos, paramString.length).trim(); - - //if param value is wrapped in double quotes, just move to paramStringWellFormed var. - } else if (paramString[0] === '"') { - quotePos = paramString.search(/[^\\]"/); - - //except for unclosed quotes to prevent infinite loops. - if (quotePos === -1) { - quotePos = paramString.length - 1; + //find index of next colon. try to determine if that delimits a key. + colonPos = paramString.indexOf(':'); + + if (colonPos) { + if (keyCandidate) { + keyCandidate += ':' + paramString.substring(0, colonPos).trim(); + } else { + keyCandidate = paramString.substring(0, colonPos).trim(); + } + + if (keyCandidate[keyCandidate.length - 1] === wrapper) { + keys.push(keyCandidate); + keyCandidate = ''; + } else if (wrapper === '' && keyCandidate[keyCandidate.length - 1] !== '"' && keyCandidate[keyCandidate.length - 1] !== '\'') { + keys.push(keyCandidate); + keyCandidate = ''; + } + + //if we have a persistent keyCandidate, continue looking for a key + if (keyCandidate) { + continue; + + //truncate the beginning from paramString and continue looking + //for a value + } else { + paramString = paramString.substring(colonPos, paramString.length); + } + + //if there are no more colons, and we're looking for a key, there is + //probably a problem. stop any further processing. } else { - quotePos += 2; + paramString = ''; + break; } + } - //move param key to paramStringWellFormed var. - paramStringWellFormed += paramString.substring(0, quotePos); - paramString = paramString.substring(quotePos, paramString.length).trim(); + //now, search for a value. + if (paramString[0] === ':' && !keyCandidate) { + paramString = paramString.substring([1], paramString.length).trim(); + + //since a quote of same type as its wrappers would be escaped, and we + //escaped those even further with their unicode, it is safe to look for + //wrapper pairs and conclude that their contents are values. + switch (paramString[0]) { + case '"': + regex = /^"(.|\s)*?"/; + break; + case '\'': + regex = /^'(.|\s)*?'/; + break; + //if there is no value wrapper, regex for alphanumerics. + default: + regex = /^\w*/; + } + values.push(paramString.match(regex)[0].trim()); - //if param value is not wrapped in quotes, move everthing up to the delimiting comma to paramStringWellFormed var. - } else { - delimitPos = paramString.indexOf(','); + //truncate the beginning from paramString and continue either + //looking for a key, or returning + paramString = paramString.replace(regex, '').trim(); - //except to prevent infinite loops. - if (delimitPos === -1) { - delimitPos = paramString.length - 1; + //exit do while if the final char is '}' + if (paramString === '}') { + paramString = ''; + break; } - paramStringWellFormed += paramString.substring(0, delimitPos); - paramString = paramString.substring(delimitPos, paramString.length).trim(); - } - - //break at the end. - if (paramString.length <= 1) { - paramStringWellFormed += paramString.trim(); + //if there are no more colons, and we're looking for a value, there is + //probably a problem. stop any further processing. + } else { paramString = ''; break; } - } while (paramString); - return paramStringWellFormed; + //build paramStringPrepared string for JSON5 parsing. + paramStringPrepared = '{'; + for (var i = 0; i < keys.length; i++) { + if (keys[i][0] !== '"' && keys[i][0] !== '\'') { + paramStringPrepared += '"'; + } + paramStringPrepared += keys[i]; + if (keys[i][keys[i].length - 1] !== '"' && keys[i][keys[i].length - 1] !== '\'') { + paramStringPrepared += '"'; + } + paramStringPrepared += ':' + values[i]; + if (i < keys.length - 1) { + paramStringPrepared += ','; + } + } + paramStringPrepared += '}'; + + //unescape unicodes for double quotes. + paramString = pString.replace(/\\u0022/g, '\u0022'); + + //unescape unicodes for single quotes. + paramString = paramString.replace(/\\u0027/g, '\u0027'); + + return paramStringPrepared; } function findparameters(pattern, patternlab) { @@ -140,16 +175,16 @@ var parameter_hunter = function () { //strip out the additional data, convert string to JSON. var leftParen = pMatch.indexOf('('); - var rightParen = pMatch.indexOf(')'); + var rightParen = pMatch.lastIndexOf(')'); var paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}'; - var paramStringWellFormed = paramToJson(paramString); + var paramStringPrepared = paramToJson(paramString); var paramData = {}; var globalData = {}; var localData = {}; try { - paramData = JSON.parse(paramStringWellFormed); + paramData = JSON5.parse(paramStringPrepared); globalData = JSON.parse(JSON.stringify(patternlab.data)); localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {})); } catch (e) { diff --git a/package.gulp.json b/package.gulp.json index be9f8ddc4..2fbb4fb9e 100644 --- a/package.gulp.json +++ b/package.gulp.json @@ -9,6 +9,7 @@ "fs-extra": "^0.26.5", "glob": "^7.0.0", "html-entities": "^1.2.0", + "json5": "^0.5.0", "mustache": "^2.2.1" }, "devDependencies": { diff --git a/package.json b/package.json index 588f19d60..0a8d07103 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "fs-extra": "^0.26.2", "glob": "^7.0.0", "html-entities": "^1.2.0", + "json5": "^0.5.0", "matchdep": "^1.0.0", "mustache": "^2.2.0" }, From 77a2089e0b1524af35d9c9df92b0a6e58aeed143 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Thu, 17 Mar 2016 21:40:35 -0400 Subject: [PATCH 05/27] bugs fixed, unit tests working --- core/lib/parameter_hunter.js | 72 +++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 6496275f4..88d1added 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -13,7 +13,6 @@ var parameter_hunter = function () { var extend = require('util')._extend, - JSON5 = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), pattern_assembler = new pa(), @@ -26,25 +25,24 @@ var parameter_hunter = function () { var paramString; var paramStringPrepared; var regex; - var value; var values = []; var wrapper; - //replace all escaped double-quotes with escaped unicode. + //replace all escaped double-quotes with escaped unicode paramString = pString.replace(/\\"/g, '\\u0022'); - //replace all escaped single-quotes with escaped unicode. + //replace all escaped single-quotes with escaped unicode paramString = paramString.replace(/\\'/g, '\\u0027'); //with escaped quotes out of the way, crawl through paramString looking for - //keys and values. + //keys and values do { //check if searching for a key if (paramString[0] === '{' || paramString[0] === ',' || keyCandidate) { paramString = paramString.substring([1], paramString.length).trim(); - //find what, if any, type of quote wraps the key. + //find what, if any, type of quote wraps the key switch (paramString[0]) { case '"': wrapper = '"'; @@ -56,7 +54,7 @@ var parameter_hunter = function () { wrapper = ''; } - //find index of next colon. try to determine if that delimits a key. + //find index of next colon. try to determine if that delimits a key colonPos = paramString.indexOf(':'); if (colonPos) { @@ -92,13 +90,13 @@ var parameter_hunter = function () { } } - //now, search for a value. + //now, search for a value if (paramString[0] === ':' && !keyCandidate) { paramString = paramString.substring([1], paramString.length).trim(); //since a quote of same type as its wrappers would be escaped, and we //escaped those even further with their unicode, it is safe to look for - //wrapper pairs and conclude that their contents are values. + //wrapper pairs and conclude that their contents are values switch (paramString[0]) { case '"': regex = /^"(.|\s)*?"/; @@ -106,7 +104,8 @@ var parameter_hunter = function () { case '\'': regex = /^'(.|\s)*?'/; break; - //if there is no value wrapper, regex for alphanumerics. + + //if there is no value wrapper, regex for alphanumerics default: regex = /^\w*/; } @@ -130,27 +129,64 @@ var parameter_hunter = function () { } } while (paramString); - //build paramStringPrepared string for JSON5 parsing. + //build paramStringPrepared string for JSON parsing paramStringPrepared = '{'; for (var i = 0; i < keys.length; i++) { - if (keys[i][0] !== '"' && keys[i][0] !== '\'') { + + //keys + //replace single-quote wrappers with double-quotes + if (keys[i][0] === '\'' && keys[i][keys[i].length - 1] === '\'') { paramStringPrepared += '"'; + + //any enclosed double-quotes must be escaped + paramStringPrepared += keys[i].substring(1, keys[i].length - 1).replace(/"/g, '\\"'); + paramStringPrepared += '"'; + } else { + + //open wrap with double-quotes if no wrapper + if (keys[i][0] !== '"' && keys[i][0] !== '\'') { + paramStringPrepared += '"'; + + //this is to clean up vestiges from Pattern Lab PHP's escaping scheme + keys[i] = keys[i].replace(/\\/g, ''); + } + + paramStringPrepared += keys[i]; + + //close wrap with double-quotes if no wrapper + if (keys[i][keys[i].length - 1] !== '"' && keys[i][keys[i].length - 1] !== '\'') { + paramStringPrepared += '"'; + } } - paramStringPrepared += keys[i]; - if (keys[i][keys[i].length - 1] !== '"' && keys[i][keys[i].length - 1] !== '\'') { + + //colon delimiter. + paramStringPrepared += ':'; + values[i]; + + //values + //replace single-quote wrappers with double-quotes + if (values[i][0] === '\'' && values[i][values[i].length - 1] === '\'') { paramStringPrepared += '"'; + + //any enclosed double-quotes must be escaped + paramStringPrepared += values[i].substring(1, values[i].length - 1).replace(/"/g, '\\"'); + paramStringPrepared += '"'; + + //for everything else, just add the colon and the value however it's wrapped + } else { + paramStringPrepared += values[i]; } - paramStringPrepared += ':' + values[i]; + + //comma delimiter if (i < keys.length - 1) { paramStringPrepared += ','; } } paramStringPrepared += '}'; - //unescape unicodes for double quotes. + //unescape unicodes for double-quotes paramString = pString.replace(/\\u0022/g, '\u0022'); - //unescape unicodes for single quotes. + //unescape unicodes for single-quotes paramString = paramString.replace(/\\u0027/g, '\u0027'); return paramStringPrepared; @@ -184,7 +220,7 @@ var parameter_hunter = function () { var localData = {}; try { - paramData = JSON5.parse(paramStringPrepared); + paramData = JSON.parse(paramStringPrepared); globalData = JSON.parse(JSON.stringify(patternlab.data)); localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {})); } catch (e) { From e3bb31e87f4f180a4fbbd70257a927d1da3397df Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Thu, 17 Mar 2016 21:43:36 -0400 Subject: [PATCH 06/27] variable name change --- core/lib/parameter_hunter.js | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 88d1added..86b8e07b6 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -23,7 +23,7 @@ var parameter_hunter = function () { var keyCandidate = ''; var keys = []; var paramString; - var paramStringPrepared; + var paramStringWellFormed; var regex; var values = []; var wrapper; @@ -129,59 +129,59 @@ var parameter_hunter = function () { } } while (paramString); - //build paramStringPrepared string for JSON parsing - paramStringPrepared = '{'; + //build paramStringWellFormed string for JSON parsing + paramStringWellFormed = '{'; for (var i = 0; i < keys.length; i++) { //keys //replace single-quote wrappers with double-quotes if (keys[i][0] === '\'' && keys[i][keys[i].length - 1] === '\'') { - paramStringPrepared += '"'; + paramStringWellFormed += '"'; //any enclosed double-quotes must be escaped - paramStringPrepared += keys[i].substring(1, keys[i].length - 1).replace(/"/g, '\\"'); - paramStringPrepared += '"'; + paramStringWellFormed += keys[i].substring(1, keys[i].length - 1).replace(/"/g, '\\"'); + paramStringWellFormed += '"'; } else { //open wrap with double-quotes if no wrapper if (keys[i][0] !== '"' && keys[i][0] !== '\'') { - paramStringPrepared += '"'; + paramStringWellFormed += '"'; //this is to clean up vestiges from Pattern Lab PHP's escaping scheme keys[i] = keys[i].replace(/\\/g, ''); } - paramStringPrepared += keys[i]; + paramStringWellFormed += keys[i]; //close wrap with double-quotes if no wrapper if (keys[i][keys[i].length - 1] !== '"' && keys[i][keys[i].length - 1] !== '\'') { - paramStringPrepared += '"'; + paramStringWellFormed += '"'; } } //colon delimiter. - paramStringPrepared += ':'; + values[i]; + paramStringWellFormed += ':'; + values[i]; //values //replace single-quote wrappers with double-quotes if (values[i][0] === '\'' && values[i][values[i].length - 1] === '\'') { - paramStringPrepared += '"'; + paramStringWellFormed += '"'; //any enclosed double-quotes must be escaped - paramStringPrepared += values[i].substring(1, values[i].length - 1).replace(/"/g, '\\"'); - paramStringPrepared += '"'; + paramStringWellFormed += values[i].substring(1, values[i].length - 1).replace(/"/g, '\\"'); + paramStringWellFormed += '"'; //for everything else, just add the colon and the value however it's wrapped } else { - paramStringPrepared += values[i]; + paramStringWellFormed += values[i]; } //comma delimiter if (i < keys.length - 1) { - paramStringPrepared += ','; + paramStringWellFormed += ','; } } - paramStringPrepared += '}'; + paramStringWellFormed += '}'; //unescape unicodes for double-quotes paramString = pString.replace(/\\u0022/g, '\u0022'); @@ -189,7 +189,7 @@ var parameter_hunter = function () { //unescape unicodes for single-quotes paramString = paramString.replace(/\\u0027/g, '\u0027'); - return paramStringPrepared; + return paramStringWellFormed; } function findparameters(pattern, patternlab) { @@ -213,14 +213,14 @@ var parameter_hunter = function () { var leftParen = pMatch.indexOf('('); var rightParen = pMatch.lastIndexOf(')'); var paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}'; - var paramStringPrepared = paramToJson(paramString); + var paramStringWellFormed = paramToJson(paramString); var paramData = {}; var globalData = {}; var localData = {}; try { - paramData = JSON.parse(paramStringPrepared); + paramData = JSON.parse(paramStringWellFormed); globalData = JSON.parse(JSON.stringify(patternlab.data)); localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {})); } catch (e) { From df0e80459c378a19008d56a227479e8be093c796 Mon Sep 17 00:00:00 2001 From: James Nash Date: Fri, 11 Mar 2016 17:20:14 +0000 Subject: [PATCH 07/27] Added (failing for now) test to highlight the bug. --- .gitignore | 1 + test/parameter_hunter_tests.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/.gitignore b/.gitignore index 74ab03195..a1c22e42c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ +npm-debug.log .DS_Store latest-change.txt patternlab.json diff --git a/test/parameter_hunter_tests.js b/test/parameter_hunter_tests.js index f87fd0752..f1e813f3c 100644 --- a/test/parameter_hunter_tests.js +++ b/test/parameter_hunter_tests.js @@ -245,8 +245,29 @@ parameter_hunter.find_parameters(currentPattern, patternlab); test.equals(currentPattern.extendedTemplate, '

true

'); + test.done(); + }, + + 'parameter hunter finds and extends templates with multiple parameters' : function(test){ + + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); + + patternlab.patterns[0].template = "

{{foo}}

{{bar}}

{{baz}}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + + currentPattern.template = "{{> molecules-single-comment(foo: true, bar: \"Hello World\", baz: 42) }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; + + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

Hello World

42

'); + test.done(); } + + }; }()); From 695a936d514f0f88d75d6c707bb0adc07da0b396 Mon Sep 17 00:00:00 2001 From: James Nash Date: Fri, 11 Mar 2016 17:42:57 +0000 Subject: [PATCH 08/27] Added another test for another bug (parenthesis in value confuses patternhunter) --- test/parameter_hunter_tests.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/parameter_hunter_tests.js b/test/parameter_hunter_tests.js index f1e813f3c..68057c93c 100644 --- a/test/parameter_hunter_tests.js +++ b/test/parameter_hunter_tests.js @@ -248,6 +248,21 @@ test.done(); }, + 'parameter hunter parses parameters with values containing a closing parenthesis' : function(test){ + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); + + currentPattern.template = "{{> molecules-single-comment(description: 'Hello ) World') }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; + + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

Hello ) World

'); + + test.done(); + }, + 'parameter hunter finds and extends templates with multiple parameters' : function(test){ var currentPattern = currentPatternClosure(); From 2a297f4a3e56e0ed129748201db1f7c45f4051f6 Mon Sep 17 00:00:00 2001 From: James Nash Date: Mon, 14 Mar 2016 16:46:25 +0000 Subject: [PATCH 09/27] Added additional parameter hunter tests. --- test/parameter_hunter_tests.js | 45 +++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/test/parameter_hunter_tests.js b/test/parameter_hunter_tests.js index 68057c93c..24b88be1c 100644 --- a/test/parameter_hunter_tests.js +++ b/test/parameter_hunter_tests.js @@ -249,6 +249,7 @@ }, 'parameter hunter parses parameters with values containing a closing parenthesis' : function(test){ + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 var currentPattern = currentPatternClosure(); var patternlab = patternlabClosure(); var parameter_hunter = new ph(); @@ -263,22 +264,60 @@ test.done(); }, - 'parameter hunter finds and extends templates with multiple parameters' : function(test){ + 'parameter hunter parses parameters that follow a non-quoted value' : function(test){ + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); + + patternlab.patterns[0].template = "

{{foo}}

{{bar}}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + + currentPattern.template = "{{> molecules-single-comment(foo: true, bar: \"Hello World\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; + + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

Hello World

'); + test.done(); + }, + + 'parameter hunter parses parameters whose keys contain escaped quotes' : function(test){ + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 var currentPattern = currentPatternClosure(); var patternlab = patternlabClosure(); var parameter_hunter = new ph(); - patternlab.patterns[0].template = "

{{foo}}

{{bar}}

{{baz}}

"; + patternlab.patterns[0].template = "

{{ silly'key }}

{{bar}}

{{ another\"silly-key }}

"; patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; - currentPattern.template = "{{> molecules-single-comment(foo: true, bar: \"Hello World\", baz: 42) }}"; + currentPattern.template = "{{> molecules-single-comment('silly\\\'key': true, bar: \"Hello World\", \"another\\\"silly-key\": 42 ) }}"; currentPattern.extendedTemplate = currentPattern.template; currentPattern.parameteredPartials[0] = currentPattern.template; parameter_hunter.find_parameters(currentPattern, patternlab); test.equals(currentPattern.extendedTemplate, '

true

Hello World

42

'); + test.done(); + }, + + 'parameter hunter skips malformed parameters' : function(test){ + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); + + patternlab.patterns[0].template = "

{{foo}}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + + currentPattern.template = "{{> molecules-single-comment( missing-val: , : missing-key, : , , foo: \"Hello World\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; + + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

Hello World

'); + test.done(); } From c8acb5ad9aebc9051ab5a2906b14b4fadcd7a55e Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 06:32:11 -0400 Subject: [PATCH 10/27] replacing JSON with JSON5 for better error messaging --- core/lib/list_item_hunter.js | 28 ++++++++++++++++++++++++---- core/lib/parameter_hunter.js | 6 ++++-- core/lib/pattern_assembler.js | 12 +++++++++++- core/lib/patternlab.js | 9 ++++++++- test/parameter_hunter_tests.js | 4 +++- 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/core/lib/list_item_hunter.js b/core/lib/list_item_hunter.js index 2b08ef2f1..ab6b7585d 100644 --- a/core/lib/list_item_hunter.js +++ b/core/lib/list_item_hunter.js @@ -13,6 +13,7 @@ var list_item_hunter = function () { var extend = require('util')._extend, + JSON = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), pattern_assembler = new pa(), @@ -44,7 +45,13 @@ var list_item_hunter = function () { } //check for a local listitems.json file - var listData = JSON.parse(JSON.stringify(patternlab.listitems)); + var listData; + try { + listData = JSON.parse(JSON.stringify(patternlab.listitems)); + } catch (err) { + console.log('There was an error parsing JSON for ' + pattern.abspath); + console.log(err); + } listData = pattern_assembler.merge_data(listData, pattern.listitems); //iterate over each copied block, rendering its contents along with pattenlab.listitems[i] @@ -54,8 +61,15 @@ var list_item_hunter = function () { //combine listItem data with pattern data with global data var itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2" - var globalData = JSON.parse(JSON.stringify(patternlab.data)); - var localData = JSON.parse(JSON.stringify(pattern.jsonFileData)); + var globalData; + var localData; + try { + globalData = JSON.parse(JSON.stringify(patternlab.data)); + localData = JSON.parse(JSON.stringify(pattern.jsonFileData)); + } catch (err) { + console.log('There was an error parsing JSON for ' + pattern.abspath); + console.log(err); + } var allData = pattern_assembler.merge_data(globalData, localData); allData = pattern_assembler.merge_data(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup @@ -71,7 +85,13 @@ var list_item_hunter = function () { var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab); //create a copy of the partial so as to not pollute it after the get_pattern_by_key call. - var cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern)); + var cleanPartialPattern; + try { + cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern)); + } catch (err) { + console.log('There was an error parsing JSON for ' + pattern.abspath); + console.log(err); + } //if partial has style modifier data, replace the styleModifier value if (foundPartials[j].indexOf(':') > -1) { diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 86b8e07b6..b8df4e960 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -13,6 +13,7 @@ var parameter_hunter = function () { var extend = require('util')._extend, + JSON = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), pattern_assembler = new pa(), @@ -223,8 +224,9 @@ var parameter_hunter = function () { paramData = JSON.parse(paramStringWellFormed); globalData = JSON.parse(JSON.stringify(patternlab.data)); localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {})); - } catch (e) { - console.log(e); + } catch (err) { + console.log('There was an error parsing JSON for ' + pattern.abspath); + console.log(err); } var allData = pattern_assembler.merge_data(globalData, localData); diff --git a/core/lib/pattern_assembler.js b/core/lib/pattern_assembler.js index 869b1986c..9c1b4ed78 100644 --- a/core/lib/pattern_assembler.js +++ b/core/lib/pattern_assembler.js @@ -332,6 +332,7 @@ var pattern_assembler = function () { } function parseDataLinksHelper(patternlab, obj, key) { + var JSON = require('json5'); var linkRE, dataObjAsString, linkMatches, expandedLink; linkRE = /link\.[A-z0-9-_]+/g; @@ -349,7 +350,16 @@ var pattern_assembler = function () { } } } - return JSON.parse(dataObjAsString); + + var dataObj; + try { + dataObj = JSON.parse(dataObjAsString); + } catch (err) { + console.log('There was an error parsing JSON for ' + key); + console.log(err); + } + + return dataObj; } //look for pattern links included in data files. diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index cc910a5bc..1fea221d2 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -12,6 +12,7 @@ var patternlab_engine = function (config) { 'use strict'; var path = require('path'), + JSON = require('json5'), fs = require('fs-extra'), diveSync = require('diveSync'), of = require('./object_factory'), @@ -191,7 +192,13 @@ var patternlab_engine = function (config) { pattern.lineageR = lineageRArray; //render the pattern, but first consolidate any data we may have - var allData = JSON.parse(JSON.stringify(patternlab.data)); + var allData; + try { + allData = JSON.parse(JSON.stringify(patternlab.data)); + } catch (err) { + console.log('There was an error parsing JSON for ' + pattern.abspath); + console.log(err); + } allData = pattern_assembler.merge_data(allData, pattern.jsonFileData); //also add the cachebuster value. slight chance this could collide with a user that has defined cacheBuster as a value diff --git a/test/parameter_hunter_tests.js b/test/parameter_hunter_tests.js index 24b88be1c..a96636652 100644 --- a/test/parameter_hunter_tests.js +++ b/test/parameter_hunter_tests.js @@ -311,12 +311,14 @@ patternlab.patterns[0].template = "

{{foo}}

"; patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + currentPattern.abspath = __filename; currentPattern.template = "{{> molecules-single-comment( missing-val: , : missing-key, : , , foo: \"Hello World\") }}"; currentPattern.extendedTemplate = currentPattern.template; currentPattern.parameteredPartials[0] = currentPattern.template; + console.log('\nPattern Lab should catch JSON.parse() errors and output useful debugging information...'); parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

Hello World

'); + test.equals(currentPattern.extendedTemplate, '

'); test.done(); } From 934844195b381bb34d23cf1d979aa5d6e0d988ef Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 08:07:15 -0400 Subject: [PATCH 11/27] working paramToJson --- core/lib/parameter_hunter.js | 67 +++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index b8df4e960..722217b73 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -19,22 +19,70 @@ var parameter_hunter = function () { pattern_assembler = new pa(), style_modifier_hunter = new smh(); + /** + * This function is really to accommodate the lax JSON-like syntax allowed by + * Pattern Lab PHP for paramater submissions to partials. Unfortunately, no + * easily searchable library was discovered for this. What we had to do was + * write a custom script to crawl through the parameter string, and wrap the + * keys and values in double-quotes as necessary. + * The steps on a high-level are as follows: + * * Further escape all escaped quotes, commas, and colons. Use the string + * representation of their unicodes for this. This has the added bonus + * of being interpreted correctly by JSON5.parse() without modification. + * This will be useful later in the function. + * * Once escaped quotes are out of the way, we know the remaining quotes + * are either key/value wrappers or wrapped within those wrappers. We know + * that remaining commas and colons are either delimiters, or wrapped + * within quotes to not be recognized as such. + * * A do/while loop crawls paramString to write keys to a keys array and + * values to a values array. + * * Start by parsing the first key. Determine the type of wrapping quote, + * if any. + * * By knowing the open wrapper, we know that the next quote of that kind + * (if the key is wrapped in quotes), HAS to be the close wrapper. + * Similarly, if the key is unwrapped, we know the next colon HAS to be + * the delimiter between key and value. + * * Save the key to the keys array. + * * Next, search for a value. It will either be the next block of text + * wrapped in quotes, or a string of alphanumerics. + * * Save the value to the values array. + * * The do/while loop truncates the paramString value while parsing. Its + * condition for completion is when the paramString is whittled down to an + * empty string. + * * After the keys and values arrays are built, a for loop iterates through + * them to build the final paramStringWellFormed string. + * * No quote substitution had been done prior to this loop. In this loop, + * all keys are ensured to be wrapped in double-quotes. String values are + * also ensured to be wrapped in double-quotes. + * * Unescape escaped unicodes except for double-quotes. Everything beside + * double-quotes will be safely wrapped in double-quotes. + * * Return paramStringWellFormed. + * + * @param {string} pString + * @returns {string} paramStringWellFormed + */ function paramToJson(pString) { var colonPos; var keyCandidate = ''; var keys = []; - var paramString; + var paramString = pString; // to not reassign param var paramStringWellFormed; var regex; var values = []; var wrapper; //replace all escaped double-quotes with escaped unicode - paramString = pString.replace(/\\"/g, '\\u0022'); + paramString = paramString.replace(/\\"/g, '\\u0022'); //replace all escaped single-quotes with escaped unicode paramString = paramString.replace(/\\'/g, '\\u0027'); + //replace all escaped commas with escaped unicode + paramString = paramString.replace(/\\,/g, '\\u0044'); + + //replace all escaped colons with escaped unicode + paramString = paramString.replace(/\\:/g, '\\u0058'); + //with escaped quotes out of the way, crawl through paramString looking for //keys and values do { @@ -148,7 +196,11 @@ var parameter_hunter = function () { if (keys[i][0] !== '"' && keys[i][0] !== '\'') { paramStringWellFormed += '"'; - //this is to clean up vestiges from Pattern Lab PHP's escaping scheme + //this is to clean up vestiges from Pattern Lab PHP's escaping scheme. + //F.Y.I. Pattern Lab PHP would allow special characters like question + //marks in parameter keys so long as the key was unwrapped and the + //special character escaped with a backslash. In Node, we need to wrap + //those keys and unescape those characters. keys[i] = keys[i].replace(/\\/g, ''); } @@ -184,11 +236,10 @@ var parameter_hunter = function () { } paramStringWellFormed += '}'; - //unescape unicodes for double-quotes - paramString = pString.replace(/\\u0022/g, '\u0022'); - - //unescape unicodes for single-quotes - paramString = paramString.replace(/\\u0027/g, '\u0027'); + //unescape escaped unicode except for double-quotes + paramStringWellFormed = paramStringWellFormed.replace(/\\u0027/g, '\''); + paramStringWellFormed = paramStringWellFormed.replace(/\\u0044/g, ','); + paramStringWellFormed = paramStringWellFormed.replace(/\\u0058/g, ':'); return paramStringWellFormed; } From cf080af15fdc72985bd19657d6be0f2e2895dcd9 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 08:08:28 -0400 Subject: [PATCH 12/27] stricter unit tests --- test/parameter_hunter_tests.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parameter_hunter_tests.js b/test/parameter_hunter_tests.js index a96636652..3a9abb25a 100644 --- a/test/parameter_hunter_tests.js +++ b/test/parameter_hunter_tests.js @@ -208,12 +208,12 @@ var patternlab = patternlabClosure(); var parameter_hunter = new ph(); - currentPattern.template = "{{> molecules-single-comment(\"description\": 'true not \"true\"') }}"; + currentPattern.template = "{{> molecules-single-comment(\"description\": 'true not{\"true\"') }}"; currentPattern.extendedTemplate = currentPattern.template; currentPattern.parameteredPartials[0] = currentPattern.template; parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not "true"

'); + test.equals(currentPattern.extendedTemplate, '

true not{"true"

'); test.done(); }, @@ -223,12 +223,12 @@ var patternlab = patternlabClosure(); var parameter_hunter = new ph(); - currentPattern.template = "{{> molecules-single-comment(\"description\": \"true not \\\"true\\\"\") }}"; + currentPattern.template = "{{> molecules-single-comment(\"description\": \"true not}\\\"true\\\"\") }}"; currentPattern.extendedTemplate = currentPattern.template; currentPattern.parameteredPartials[0] = currentPattern.template; parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not "true"

'); + test.equals(currentPattern.extendedTemplate, '

true not}"true"

'); test.done(); }, From 68a6c35894c5f1fa1d023a4d392fdd88245b6dc7 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 08:16:24 -0400 Subject: [PATCH 13/27] comment updates --- core/lib/parameter_hunter.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 722217b73..7b3b802dc 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -21,20 +21,20 @@ var parameter_hunter = function () { /** * This function is really to accommodate the lax JSON-like syntax allowed by - * Pattern Lab PHP for paramater submissions to partials. Unfortunately, no + * Pattern Lab PHP for parameter submissions to partials. Unfortunately, no * easily searchable library was discovered for this. What we had to do was * write a custom script to crawl through the parameter string, and wrap the * keys and values in double-quotes as necessary. * The steps on a high-level are as follows: * * Further escape all escaped quotes, commas, and colons. Use the string * representation of their unicodes for this. This has the added bonus - * of being interpreted correctly by JSON5.parse() without modification. - * This will be useful later in the function. + * of being interpreted correctly by JSON5.parse() without further + * modification. This will be useful later in the function. * * Once escaped quotes are out of the way, we know the remaining quotes * are either key/value wrappers or wrapped within those wrappers. We know * that remaining commas and colons are either delimiters, or wrapped * within quotes to not be recognized as such. - * * A do/while loop crawls paramString to write keys to a keys array and + * * A do-while loop crawls paramString to write keys to a keys array and * values to a values array. * * Start by parsing the first key. Determine the type of wrapping quote, * if any. @@ -46,7 +46,7 @@ var parameter_hunter = function () { * * Next, search for a value. It will either be the next block of text * wrapped in quotes, or a string of alphanumerics. * * Save the value to the values array. - * * The do/while loop truncates the paramString value while parsing. Its + * * The do-while loop truncates the paramString value while parsing. Its * condition for completion is when the paramString is whittled down to an * empty string. * * After the keys and values arrays are built, a for loop iterates through @@ -55,7 +55,7 @@ var parameter_hunter = function () { * all keys are ensured to be wrapped in double-quotes. String values are * also ensured to be wrapped in double-quotes. * * Unescape escaped unicodes except for double-quotes. Everything beside - * double-quotes will be safely wrapped in double-quotes. + * double-quotes will be wrapped in double-quotes without need for escape. * * Return paramStringWellFormed. * * @param {string} pString From 37ad825dbef5c62b45a6f49ab653c9a018d0fe44 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 08:21:32 -0400 Subject: [PATCH 14/27] simplfying paramToJson further --- core/lib/parameter_hunter.js | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 7b3b802dc..0a1cc604b 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -63,7 +63,6 @@ var parameter_hunter = function () { */ function paramToJson(pString) { var colonPos; - var keyCandidate = ''; var keys = []; var paramString = pString; // to not reassign param var paramStringWellFormed; @@ -88,7 +87,7 @@ var parameter_hunter = function () { do { //check if searching for a key - if (paramString[0] === '{' || paramString[0] === ',' || keyCandidate) { + if (paramString[0] === '{' || paramString[0] === ',') { paramString = paramString.substring([1], paramString.length).trim(); //find what, if any, type of quote wraps the key @@ -107,29 +106,10 @@ var parameter_hunter = function () { colonPos = paramString.indexOf(':'); if (colonPos) { - if (keyCandidate) { - keyCandidate += ':' + paramString.substring(0, colonPos).trim(); - } else { - keyCandidate = paramString.substring(0, colonPos).trim(); - } - - if (keyCandidate[keyCandidate.length - 1] === wrapper) { - keys.push(keyCandidate); - keyCandidate = ''; - } else if (wrapper === '' && keyCandidate[keyCandidate.length - 1] !== '"' && keyCandidate[keyCandidate.length - 1] !== '\'') { - keys.push(keyCandidate); - keyCandidate = ''; - } - - //if we have a persistent keyCandidate, continue looking for a key - if (keyCandidate) { - continue; - - //truncate the beginning from paramString and continue looking - //for a value - } else { - paramString = paramString.substring(colonPos, paramString.length); - } + keys.push(paramString.substring(0, colonPos).trim()); + + //truncate the beginning from paramString and look for a value + paramString = paramString.substring(colonPos, paramString.length); //if there are no more colons, and we're looking for a key, there is //probably a problem. stop any further processing. @@ -140,7 +120,7 @@ var parameter_hunter = function () { } //now, search for a value - if (paramString[0] === ':' && !keyCandidate) { + if (paramString[0] === ':') { paramString = paramString.substring([1], paramString.length).trim(); //since a quote of same type as its wrappers would be escaped, and we From a7e52cd58d154008084b1d751f5a73b9e2331c6f Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 08:26:07 -0400 Subject: [PATCH 15/27] simplfying paramToJson further --- core/lib/.parameter_hunter.js.swp | Bin 0 -> 24576 bytes core/lib/parameter_hunter.js | 15 +-------------- 2 files changed, 1 insertion(+), 14 deletions(-) create mode 100644 core/lib/.parameter_hunter.js.swp diff --git a/core/lib/.parameter_hunter.js.swp b/core/lib/.parameter_hunter.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..c8f5955ab581f632b38bc5142bf28bda293fa006 GIT binary patch literal 24576 zcmeI4TZ|;vS;s4}6DM}OHsrxP06y8^^vt-YYj#;*;$;bO*6T~W7rbj*W@o)rcc1Pq zPjywhs(NR~>j_E_!U{Km5JU47WWF!uiQ zF3;0iF23^XWH1hN)QOT_5M8Xbe}AUa%s)8_Qr-7YhPlrCNs#9{jiaEu8Yg`{|9w^O z&qu+k_IpXH{V43xAPvTvXU>kMF>jqmvWtz`+-o?{Z~zWW^TFz^S9{;Sf8Uz$26xCZP2 zU)o(7MBd%+6047?t^e6{EODtH)N3qJK0&-)SZFt`T%-c_Er1#SUfywdYN z1nvdbffwEk9pFK51bp>Pp7&AkyL?{U8Nrz&f}d{38LAe*nJ-#vlSWgO}ms*T5fu z9|FSHEpQ&pz7#K0%y2SK!+5B?`3r~Yw>znxL_tq4`RnVq`NOtqt*=kl)^57BwS3S# z>HDsQ3Zh8qtQSmlU-gnGi8Hks=A-i3G!A=|T6mW}V7nUdz=2m9a6ArP^MI{t!2P#e ze83FeMSAtoX+k;4Yn|xc+PM2KK5jpmb}wbzb3v-k1<@4Z4yn^;tgz%_s)zb~^|hu7 z59%m7lB8qZUwE5-CRS$-D&JRmqT(c{yCBQLVQiJg$W}(#&|C8Z8=W|jWYYOy8u#)r zaZjB}?#YsP$@RXhE=V^kzWHB$SE}=AiZWfujKXf$)w3emH8#_!HMInro3;fVtNTok zi~H#m&6JvFHTvDvYBLQcqT*q^qd`7Orn!nWcL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Sa-DsDwpwv~+c(=A%7zhJ&qA zv{F}hgmQM7mMmr$wwG(8D5rkr?j>}G3U&Iz>zO_fz7@eNMdtE+Y@_y|toSC{E(WU5 zgLGzY7hLeaxK~MvVdihvNsxPXG4AYi(X2yvR!t?6w``)=$QS6kXMw21ZSafYg z8|s`?l)c*BI?I-@aMZ%7`T|trTCHzsRjJ9?L;(Dy)9ah-tLvS0|9xj}T=s_&4XAX? z?XzpEH=)v4`jHicu~Vf@O|{JPU*2g*ayYw7F20n-;J5i9uD(%1f5 z?C&Ire%a2*P!=hQBC_Frb+GF%>Z~9aQ$a?hbb^I{Dc)yJ%oXP(3)5{8d|J(R~Nl|%vb{T-`M)|3* zWRe#D!&v}3M8W-Cxk|v|AWL>hP~lWXy@-xf>g60)J>*(x+QoFbE?m)S3Cy5+r``Ju O2Nq Date: Fri, 18 Mar 2016 08:27:59 -0400 Subject: [PATCH 16/27] comment update --- core/lib/.parameter_hunter.js.swp | Bin 24576 -> 0 bytes core/lib/parameter_hunter.js | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 core/lib/.parameter_hunter.js.swp diff --git a/core/lib/.parameter_hunter.js.swp b/core/lib/.parameter_hunter.js.swp deleted file mode 100644 index c8f5955ab581f632b38bc5142bf28bda293fa006..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 24576 zcmeI4TZ|;vS;s4}6DM}OHsrxP06y8^^vt-YYj#;*;$;bO*6T~W7rbj*W@o)rcc1Pq zPjywhs(NR~>j_E_!U{Km5JU47WWF!uiQ zF3;0iF23^XWH1hN)QOT_5M8Xbe}AUa%s)8_Qr-7YhPlrCNs#9{jiaEu8Yg`{|9w^O z&qu+k_IpXH{V43xAPvTvXU>kMF>jqmvWtz`+-o?{Z~zWW^TFz^S9{;Sf8Uz$26xCZP2 zU)o(7MBd%+6047?t^e6{EODtH)N3qJK0&-)SZFt`T%-c_Er1#SUfywdYN z1nvdbffwEk9pFK51bp>Pp7&AkyL?{U8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id4(Q~ay`-&$B1bJ`J5GHSm#)>Lk5CP!t4+ zN{Znqe=-{uqgAGu%4}HVFvukda~YK_ev!cPRjg`d&t=0Kt;HM;@}RO-*>%s+P?2Ai zA3?X!=?;3b(#Y8E>kbC6(nXPsQx5@_aXIE_OitM7+I6BZ-e6oCb6dKwJ@X(TxxloT z;f`&~oD8RxG~y5|Wkb~Wv35(3vMVYYmBnhCCTFvSH;M!0 zcdE77{S#dkN+KnQrZ-ndN%4~~O!UwE+QlfDX>iokS#p1FrE-#5s2r3@5A|8;aH%GE zwk(IaRGXV9+a>m6*)Pwx*@p4=PF+`}=G3Jjd{2m6N6BzWr)h0OQ@38;Yn?KuD5)(Z zj6qAI9_9dO>i3Mkw3HZ%HQ%6{@o`k^aGPMS*|^F$wSa-DsDwpwv~+c(=A%7zhJ&qA zv{F}hgmQM7mMmr$wwG(8D5rkr?j>}G3U&Iz>zO_fz7@eNMdtE+Y@_y|toSC{E(WU5 zgLGzY7hLeaxK~MvVdihvNsxPXG4AYi(X2yvR!t?6w``)=$QS6kXMw21ZSafYg z8|s`?l)c*BI?I-@aMZ%7`T|trTCHzsRjJ9?L;(Dy)9ah-tLvS0|9xj}T=s_&4XAX? z?XzpEH=)v4`jHicu~Vf@O|{JPU*2g*ayYw7F20n-;J5i9uD(%1f5 z?C&Ire%a2*P!=hQBC_Frb+GF%>Z~9aQ$a?hbb^I{Dc)yJ%oXP(3)5{8d|J(R~Nl|%vb{T-`M)|3* zWRe#D!&v}3M8W-Cxk|v|AWL>hP~lWXy@-xf>g60)J>*(x+QoFbE?m)S3Cy5+r``Ju O2Nq Date: Fri, 18 Mar 2016 09:01:14 -0400 Subject: [PATCH 17/27] tightened parameter key search --- core/lib/parameter_hunter.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 893048ab5..b39466681 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -62,12 +62,14 @@ var parameter_hunter = function () { * @returns {string} paramStringWellFormed */ function paramToJson(pString) { - var colonPos; + var colonPos = -1; var keys = []; var paramString = pString; // to not reassign param var paramStringWellFormed; + var quotePos = -1; var regex; var values = []; + var wrapper; //replace all escaped double-quotes with escaped unicode paramString = paramString.replace(/\\"/g, '\\u0022'); @@ -89,15 +91,37 @@ var parameter_hunter = function () { if (paramString[0] === '{' || paramString[0] === ',') { paramString = paramString.substring([1], paramString.length).trim(); - //find index of next colon. try to determine if that delimits a key - colonPos = paramString.indexOf(':'); + //search for end quote if wrapped in quotes. else search for colon. + switch (paramString[0]) { + //need to search for end quote pos in case the quotes wrap a colon + case '"': + case '\'': + wrapper = paramString[0]; + quotePos = paramString.indexOf(wrapper, 1); + break; + + default: + colonPos = paramString.indexOf(':'); + } + + if (quotePos > -1) { + keys.push(paramString.substring(0, quotePos + 1).trim()); + + //truncate the beginning from paramString and look for a value + paramString = paramString.substring(quotePos + 1, paramString.length).trim(); + + //unset quotePos + quotePos = -1; - if (colonPos) { + } else if (colonPos > -1) { keys.push(paramString.substring(0, colonPos).trim()); //truncate the beginning from paramString and look for a value paramString = paramString.substring(colonPos, paramString.length); + //unset colonPos + colonPos = -1; + //if there are no more colons, and we're looking for a key, there is //probably a problem. stop any further processing. } else { From 83f6a4f7d23419fff19022fc736617a37705e945 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 09:06:53 -0400 Subject: [PATCH 18/27] comment update --- core/lib/parameter_hunter.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index b39466681..3012a17e8 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -92,7 +92,9 @@ var parameter_hunter = function () { paramString = paramString.substring([1], paramString.length).trim(); //search for end quote if wrapped in quotes. else search for colon. + //everything up to that position will be saved in the keys array. switch (paramString[0]) { + //need to search for end quote pos in case the quotes wrap a colon case '"': case '\'': From ed1312889ea12ae24871b64e290a6a2b39357f78 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 09:41:54 -0400 Subject: [PATCH 19/27] accommodating decimals and exponentials as valid numeric values --- core/lib/parameter_hunter.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 3012a17e8..0d88a0670 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -43,8 +43,8 @@ var parameter_hunter = function () { * Similarly, if the key is unwrapped, we know the next colon HAS to be * the delimiter between key and value. * * Save the key to the keys array. - * * Next, search for a value. It will either be the next block of text - * wrapped in quotes, or a string of alphanumerics. + * * Next, search for a value. It will either be the next block wrapped in + * quotes, or a string of alphanumerics, decimal points, or hyphens. * * Save the value to the values array. * * The do-while loop truncates the paramString value while parsing. Its * condition for completion is when the paramString is whittled down to an @@ -147,9 +147,10 @@ var parameter_hunter = function () { regex = /^'(.|\s)*?'/; break; - //if there is no value wrapper, regex for alphanumerics + //if there is no value wrapper, regex for alphanumerics. also regex + //for points for decimals and hyphens for exponentials. default: - regex = /^\w*/; + regex = /^[\w\-\.]*/; } values.push(paramString.match(regex)[0].trim()); From 7bcea0afe7b193510c83a42773c1fbc10497ad29 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 09:46:21 -0400 Subject: [PATCH 20/27] comment update --- core/lib/parameter_hunter.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 0d88a0670..ce5342d23 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -44,7 +44,7 @@ var parameter_hunter = function () { * the delimiter between key and value. * * Save the key to the keys array. * * Next, search for a value. It will either be the next block wrapped in - * quotes, or a string of alphanumerics, decimal points, or hyphens. + * quotes, or a string of alphanumerics, decimal points, or minus signs. * * Save the value to the values array. * * The do-while loop truncates the paramString value while parsing. Its * condition for completion is when the paramString is whittled down to an @@ -147,8 +147,8 @@ var parameter_hunter = function () { regex = /^'(.|\s)*?'/; break; - //if there is no value wrapper, regex for alphanumerics. also regex - //for points for decimals and hyphens for exponentials. + //if there is no value wrapper, regex for alphanumerics, decimal + //points, and minus signs for exponential notation. default: regex = /^[\w\-\.]*/; } From 2317cae29be7450f5abea87b43a79696a673aa0e Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 13:15:57 -0400 Subject: [PATCH 21/27] fixing array instead of number typo --- core/lib/parameter_hunter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index ce5342d23..38682b686 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -134,7 +134,7 @@ var parameter_hunter = function () { //now, search for a value if (paramString[0] === ':') { - paramString = paramString.substring([1], paramString.length).trim(); + paramString = paramString.substring(1, paramString.length).trim(); //since a quote of same type as its wrappers would be escaped, and we //escaped those even further with their unicode, it is safe to look for From 3583251b7fed4d098e695cdfd8da258c762bc86a Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 15:13:34 -0400 Subject: [PATCH 22/27] more comments --- core/lib/parameter_hunter.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 38682b686..27dc0f51f 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -136,10 +136,16 @@ var parameter_hunter = function () { if (paramString[0] === ':') { paramString = paramString.substring(1, paramString.length).trim(); - //since a quote of same type as its wrappers would be escaped, and we - //escaped those even further with their unicode, it is safe to look for - //wrapper pairs and conclude that their contents are values + //the only reason we're using regexes here, instead of indexOf(), is + //because we don't know if the next delimiter is going to be a comma or + //a closing curly brace. since it's not much of a performance hit to + //use regexes as sparingly as here, and it's much more concise and + //readable, we'll use a regex for match() and replace() instead of + //performing conditional logic with indexOf(). switch (paramString[0]) { + //since a quote of same type as its wrappers would be escaped, and we + //escaped those even further with their unicodes, it is safe to look + //for wrapper pairs and conclude that their contents are values case '"': regex = /^"(.|\s)*?"/; break; From 704346ada3536bb50853f781bc7fbd807270dc0d Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 15:15:29 -0400 Subject: [PATCH 23/27] spacing to pass eslint --- core/lib/parameter_hunter.js | 1 + 1 file changed, 1 insertion(+) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 27dc0f51f..7601078ef 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -143,6 +143,7 @@ var parameter_hunter = function () { //readable, we'll use a regex for match() and replace() instead of //performing conditional logic with indexOf(). switch (paramString[0]) { + //since a quote of same type as its wrappers would be escaped, and we //escaped those even further with their unicodes, it is safe to look //for wrapper pairs and conclude that their contents are values From a0d33d3fb7903f005056536e7aa5702d5b32d8d1 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Fri, 18 Mar 2016 20:21:22 -0400 Subject: [PATCH 24/27] removing unnecessary escape --- core/lib/parameter_hunter.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 7601078ef..98cc0651e 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -77,9 +77,6 @@ var parameter_hunter = function () { //replace all escaped single-quotes with escaped unicode paramString = paramString.replace(/\\'/g, '\\u0027'); - //replace all escaped commas with escaped unicode - paramString = paramString.replace(/\\,/g, '\\u0044'); - //replace all escaped colons with escaped unicode paramString = paramString.replace(/\\:/g, '\\u0058'); @@ -239,7 +236,6 @@ var parameter_hunter = function () { //unescape escaped unicode except for double-quotes paramStringWellFormed = paramStringWellFormed.replace(/\\u0027/g, '\''); - paramStringWellFormed = paramStringWellFormed.replace(/\\u0044/g, ','); paramStringWellFormed = paramStringWellFormed.replace(/\\u0058/g, ':'); return paramStringWellFormed; From 4690f792d9a7d9908aa052f41ae6cb670bd695c4 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Tue, 29 Mar 2016 07:14:26 -0400 Subject: [PATCH 25/27] fixing substring param typo --- core/lib/parameter_hunter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 98cc0651e..a6d130d0a 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -86,7 +86,7 @@ var parameter_hunter = function () { //check if searching for a key if (paramString[0] === '{' || paramString[0] === ',') { - paramString = paramString.substring([1], paramString.length).trim(); + paramString = paramString.substring(1, paramString.length).trim(); //search for end quote if wrapped in quotes. else search for colon. //everything up to that position will be saved in the keys array. From eedaecc874bb9632ab924d80f1569ba10497d22b Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Tue, 29 Mar 2016 07:22:36 -0400 Subject: [PATCH 26/27] renaming json5 instances --- core/lib/list_item_hunter.js | 10 +++++----- core/lib/parameter_hunter.js | 8 ++++---- core/lib/pattern_assembler.js | 6 +++--- core/lib/patternlab.js | 12 ++++++------ 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/core/lib/list_item_hunter.js b/core/lib/list_item_hunter.js index ab6b7585d..2f361f2ec 100644 --- a/core/lib/list_item_hunter.js +++ b/core/lib/list_item_hunter.js @@ -13,7 +13,7 @@ var list_item_hunter = function () { var extend = require('util')._extend, - JSON = require('json5'), + JSON5 = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), pattern_assembler = new pa(), @@ -47,7 +47,7 @@ var list_item_hunter = function () { //check for a local listitems.json file var listData; try { - listData = JSON.parse(JSON.stringify(patternlab.listitems)); + listData = JSON5.parse(JSON5.stringify(patternlab.listitems)); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.abspath); console.log(err); @@ -64,8 +64,8 @@ var list_item_hunter = function () { var globalData; var localData; try { - globalData = JSON.parse(JSON.stringify(patternlab.data)); - localData = JSON.parse(JSON.stringify(pattern.jsonFileData)); + globalData = JSON5.parse(JSON5.stringify(patternlab.data)); + localData = JSON5.parse(JSON5.stringify(pattern.jsonFileData)); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.abspath); console.log(err); @@ -87,7 +87,7 @@ var list_item_hunter = function () { //create a copy of the partial so as to not pollute it after the get_pattern_by_key call. var cleanPartialPattern; try { - cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern)); + cleanPartialPattern = JSON5.parse(JSON5.stringify(partialPattern)); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.abspath); console.log(err); diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index a6d130d0a..55bff52a2 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -13,7 +13,7 @@ var parameter_hunter = function () { var extend = require('util')._extend, - JSON = require('json5'), + JSON5 = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), pattern_assembler = new pa(), @@ -269,9 +269,9 @@ var parameter_hunter = function () { var localData = {}; try { - paramData = JSON.parse(paramStringWellFormed); - globalData = JSON.parse(JSON.stringify(patternlab.data)); - localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {})); + paramData = JSON5.parse(paramStringWellFormed); + globalData = JSON5.parse(JSON5.stringify(patternlab.data)); + localData = JSON5.parse(JSON5.stringify(pattern.jsonFileData || {})); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.abspath); console.log(err); diff --git a/core/lib/pattern_assembler.js b/core/lib/pattern_assembler.js index 9c1b4ed78..c83dda364 100644 --- a/core/lib/pattern_assembler.js +++ b/core/lib/pattern_assembler.js @@ -332,11 +332,11 @@ var pattern_assembler = function () { } function parseDataLinksHelper(patternlab, obj, key) { - var JSON = require('json5'); + var JSON5 = require('json5'); var linkRE, dataObjAsString, linkMatches, expandedLink; linkRE = /link\.[A-z0-9-_]+/g; - dataObjAsString = JSON.stringify(obj); + dataObjAsString = JSON5.stringify(obj); linkMatches = dataObjAsString.match(linkRE); if (linkMatches) { @@ -353,7 +353,7 @@ var pattern_assembler = function () { var dataObj; try { - dataObj = JSON.parse(dataObjAsString); + dataObj = JSON5.parse(dataObjAsString); } catch (err) { console.log('There was an error parsing JSON for ' + key); console.log(err); diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index 1fea221d2..5a7591725 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -12,7 +12,7 @@ var patternlab_engine = function (config) { 'use strict'; var path = require('path'), - JSON = require('json5'), + JSON5 = require('json5'), fs = require('fs-extra'), diveSync = require('diveSync'), of = require('./object_factory'), @@ -181,20 +181,20 @@ var patternlab_engine = function (config) { //json stringify lineage and lineageR var lineageArray = []; for (var i = 0; i < pattern.lineage.length; i++) { - lineageArray.push(JSON.stringify(pattern.lineage[i])); + lineageArray.push(JSON5.stringify(pattern.lineage[i])); } pattern.lineage = lineageArray; var lineageRArray = []; for (var i = 0; i < pattern.lineageR.length; i++) { - lineageRArray.push(JSON.stringify(pattern.lineageR[i])); + lineageRArray.push(JSON5.stringify(pattern.lineageR[i])); } pattern.lineageR = lineageRArray; //render the pattern, but first consolidate any data we may have var allData; try { - allData = JSON.parse(JSON.stringify(patternlab.data)); + allData = JSON5.parse(JSON5.stringify(patternlab.data)); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.abspath); console.log(err); @@ -561,11 +561,11 @@ var patternlab_engine = function (config) { //patternPaths var patternPathsTemplate = fs.readFileSync(path.resolve(paths.source.patternlabFiles, 'templates/partials/patternPaths.mustache'), 'utf8'); - var patternPathsPartialHtml = pattern_assembler.renderPattern(patternPathsTemplate, {'patternPaths': JSON.stringify(patternlab.patternPaths)}); + var patternPathsPartialHtml = pattern_assembler.renderPattern(patternPathsTemplate, {'patternPaths': JSON5.stringify(patternlab.patternPaths)}); //viewAllPaths var viewAllPathsTemplate = fs.readFileSync(path.resolve(paths.source.patternlabFiles, 'templates/partials/viewAllPaths.mustache'), 'utf8'); - var viewAllPathsPartialHtml = pattern_assembler.renderPattern(viewAllPathsTemplate, {'viewallpaths': JSON.stringify(patternlab.viewAllPaths)}); + var viewAllPathsPartialHtml = pattern_assembler.renderPattern(viewAllPathsTemplate, {'viewallpaths': JSON5.stringify(patternlab.viewAllPaths)}); //render the patternlab template, with all partials var patternlabSiteHtml = pattern_assembler.renderPattern(patternlabSiteTemplate, { From 7fa7f6b14c6db1de4190290ba7368170487325eb Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Tue, 29 Mar 2016 07:38:16 -0400 Subject: [PATCH 27/27] comment update --- core/lib/parameter_hunter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 55bff52a2..13d8f280e 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -26,7 +26,7 @@ var parameter_hunter = function () { * write a custom script to crawl through the parameter string, and wrap the * keys and values in double-quotes as necessary. * The steps on a high-level are as follows: - * * Further escape all escaped quotes, commas, and colons. Use the string + * * Further escape all escaped quotes and colons. Use the string * representation of their unicodes for this. This has the added bonus * of being interpreted correctly by JSON5.parse() without further * modification. This will be useful later in the function.