Skip to content

Commit 25d3b7f

Browse files
author
Brian Muenzenmeyer
committed
Merge pull request #276 from e2tha-e/replacing-eval-with-JSON.parse
more robust parameter to json conversion
2 parents 083238e + 2f57f65 commit 25d3b7f

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

builder/parameter_hunter.js

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,93 @@
2020
style_modifier_hunter = new smh(),
2121
pattern_assembler = new pa();
2222

23+
function paramToJson(paramString) {
24+
var paramStringWellFormed = '';
25+
var paramStringTmp;
26+
var colonPos;
27+
var delimitPos;
28+
var quotePos;
29+
30+
do {
31+
//if param key is wrapped in single quotes, replace with double quotes.
32+
paramString = paramString.replace(/(^\s*[\{|\,]\s*)'([^']+)'(\s*\:)/, '$1"$2"$3');
33+
//if params key is not wrapped in any quotes, wrap in double quotes.
34+
paramString = paramString.replace(/(^\s*[\{|\,]\s*)([^\s"'\:]+)(\s*\:)/, '$1"$2"$3');
35+
//move param key to paramStringWellFormed var.
36+
colonPos = paramString.indexOf(':');
37+
//except to prevent infinite loops.
38+
if (colonPos === -1) {
39+
colonPos = paramString.length - 1;
40+
}
41+
else {
42+
colonPos += 1;
43+
}
44+
paramStringWellFormed += paramString.substring(0, colonPos);
45+
paramString = paramString.substring(colonPos, paramString.length).trim();
46+
47+
//if param value is wrapped in single quotes, replace with double quotes.
48+
if (paramString[0] === '\'') {
49+
quotePos = paramString.search(/[^\\]'/);
50+
//except for unclosed quotes to prevent infinite loops.
51+
if (quotePos === -1) {
52+
quotePos = paramString.length - 1;
53+
}
54+
else {
55+
quotePos += 2;
56+
}
57+
//prepare param value for move to paramStringWellFormed var.
58+
paramStringTmp = paramString.substring(0, quotePos);
59+
//unescape any escaped single quotes.
60+
paramStringTmp = paramStringTmp.replace(/\\'/g, '\'');
61+
//escape any double quotes.
62+
paramStringTmp = paramStringTmp.replace(/"/g, '\\"');
63+
//replace the delimiting single quotes with double quotes.
64+
paramStringTmp = paramStringTmp.replace(/^'/, '"');
65+
paramStringTmp = paramStringTmp.replace(/'$/, '"');
66+
//move param key to paramStringWellFormed var.
67+
paramStringWellFormed += paramStringTmp;
68+
paramString = paramString.substring(quotePos, paramString.length).trim();
69+
}
70+
//if param value is wrapped in double quotes, just move to paramStringWellFormed var.
71+
else if (paramString[0] === '"') {
72+
quotePos = paramString.search(/[^\\]"/);
73+
//except for unclosed quotes to prevent infinite loops.
74+
if (quotePos === -1) {
75+
quotePos = paramString.length - 1;
76+
}
77+
else {
78+
quotePos += 2;
79+
}
80+
//move param key to paramStringWellFormed var.
81+
paramStringWellFormed += paramString.substring(0, quotePos);
82+
paramString = paramString.substring(quotePos, paramString.length).trim();
83+
}
84+
//if param value is not wrapped in quotes, move everthing up to the delimiting comma to paramStringWellFormed var.
85+
else {
86+
delimitPos = paramString.indexOf(',');
87+
//except to prevent infinite loops.
88+
if (delimitPos === -1) {
89+
delimitPos = paramString.length - 1;
90+
}
91+
else {
92+
delimitPos += 1;
93+
}
94+
paramStringWellFormed += paramString.substring(0, delimitPos);
95+
paramString = paramString.substring(delimitPos, paramString.length).trim();
96+
}
97+
98+
//break at the end.
99+
if (paramString.length === 1) {
100+
paramStringWellFormed += paramString.trim();
101+
paramString = '';
102+
break;
103+
}
104+
105+
} while(paramString);
106+
107+
return paramStringWellFormed;
108+
}
109+
23110
function findparameters(pattern, patternlab){
24111

25112
if(pattern.parameteredPartials && pattern.parameteredPartials.length > 0){
@@ -39,12 +126,7 @@
39126
var leftParen = pMatch.indexOf('(');
40127
var rightParen = pMatch.indexOf(')');
41128
var paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}';
42-
//if param keys are wrapped in single quotes, replace with double quotes.
43-
var paramStringWellFormed = paramString.replace(/(')([^']+)(')(\s*\:)/g, '"$2"$4');
44-
//if params keys are not wrapped in any quotes, wrap in double quotes.
45-
var paramStringWellFormed = paramStringWellFormed.replace(/([\{|,]\s*)([^\s"'\:]+)(\s*\:)/g, '$1"$2"$3');
46-
//if param values are wrapped in single quotes, replace with double quotes.
47-
var paramStringWellFormed = paramStringWellFormed.replace(/(\:\s*)(')([^']+)(')/g, '$1"$3"');
129+
var paramStringWellFormed = paramToJson(paramString);
48130

49131
var paramData = {};
50132
var globalData = {};

test/parameter_hunter_tests.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,32 +158,32 @@
158158
test.done();
159159
},
160160

161-
'parameter hunter parses parameters with single-quoted keys and single-quoted values' : function(test){
161+
'parameter hunter parses parameters with single-quoted keys and single-quoted values wrapping internal escaped single-quotes' : function(test){
162162
var currentPattern = currentPatternClosure();
163163
var patternlab = patternlabClosure();
164164
var parameter_hunter = new ph();
165165

166-
currentPattern.template = "{{> molecules-single-comment('description': 'true') }}";
166+
currentPattern.template = "{{> molecules-single-comment('description': 'true not,\\'true\\'') }}";
167167
currentPattern.extendedTemplate = currentPattern.template;
168168
currentPattern.parameteredPartials[0] = currentPattern.template;
169169

170170
parameter_hunter.find_parameters(currentPattern, patternlab);
171-
test.equals(currentPattern.extendedTemplate, '<p>true</p>');
171+
test.equals(currentPattern.extendedTemplate, '<p>true not,&#39;true&#39;</p>');
172172

173173
test.done();
174174
},
175175

176-
'parameter hunter parses parameters with single-quoted keys and double-quoted values' : function(test){
176+
'parameter hunter parses parameters with single-quoted keys and double-quoted values wrapping internal single-quotes' : function(test){
177177
var currentPattern = currentPatternClosure();
178178
var patternlab = patternlabClosure();
179179
var parameter_hunter = new ph();
180180

181-
currentPattern.template = "{{> molecules-single-comment('description': \"true\") }}";
181+
currentPattern.template = "{{> molecules-single-comment('description': \"true not:'true'\") }}";
182182
currentPattern.extendedTemplate = currentPattern.template;
183183
currentPattern.parameteredPartials[0] = currentPattern.template;
184184

185185
parameter_hunter.find_parameters(currentPattern, patternlab);
186-
test.equals(currentPattern.extendedTemplate, '<p>true</p>');
186+
test.equals(currentPattern.extendedTemplate, '<p>true not:&#39;true&#39;</p>');
187187

188188
test.done();
189189
},
@@ -203,32 +203,32 @@
203203
test.done();
204204
},
205205

206-
'parameter hunter parses parameters with double-quoted keys and single-quoted values' : function(test){
206+
'parameter hunter parses parameters with double-quoted keys and single-quoted values wrapping internal double-quotes' : function(test){
207207
var currentPattern = currentPatternClosure();
208208
var patternlab = patternlabClosure();
209209
var parameter_hunter = new ph();
210210

211-
currentPattern.template = "{{> molecules-single-comment(\"description\": 'true') }}";
211+
currentPattern.template = "{{> molecules-single-comment(\"description\": 'true not{\"true\"') }}";
212212
currentPattern.extendedTemplate = currentPattern.template;
213213
currentPattern.parameteredPartials[0] = currentPattern.template;
214214

215215
parameter_hunter.find_parameters(currentPattern, patternlab);
216-
test.equals(currentPattern.extendedTemplate, '<p>true</p>');
216+
test.equals(currentPattern.extendedTemplate, '<p>true not{&quot;true&quot;</p>');
217217

218218
test.done();
219219
},
220220

221-
'parameter hunter parses parameters with double-quoted keys and double-quoted values' : function(test){
221+
'parameter hunter parses parameters with double-quoted keys and double-quoted values wrapping internal escaped double-quotes' : function(test){
222222
var currentPattern = currentPatternClosure();
223223
var patternlab = patternlabClosure();
224224
var parameter_hunter = new ph();
225225

226-
currentPattern.template = "{{> molecules-single-comment(\"description\": \"true\") }}";
226+
currentPattern.template = "{{> molecules-single-comment(\"description\": \"true not}\\\"true\\\"\") }}";
227227
currentPattern.extendedTemplate = currentPattern.template;
228228
currentPattern.parameteredPartials[0] = currentPattern.template;
229229

230230
parameter_hunter.find_parameters(currentPattern, patternlab);
231-
test.equals(currentPattern.extendedTemplate, '<p>true</p>');
231+
test.equals(currentPattern.extendedTemplate, '<p>true not}&quot;true&quot;</p>');
232232

233233
test.done();
234234
}

0 commit comments

Comments
 (0)