-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emboss.js
645 lines (644 loc) · 18.9 KB
/
Emboss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
'use strict';
(function()
{
// Library Initialisation
if (window.Emboss)
{
throw 'EmbossError: Emboss could not be initialised due to name conflict.';
}
else
{
window.Emboss = new Emboss();
};
// Library
function Emboss()
{
var embossBlocksExpression = /({{.*?}})/i;
var keywords = {
print: 'print',
if: 'if',
elseif: 'elseif',
else: 'else',
for: 'for',
each: 'each',
import: 'import',
execute: 'execute',
ignore: 'ignore',
neglect: 'neglect'
};
var secondOrderKeywords = {
in: 'in',
count: 'count',
key: 'key',
context: 'context'
};
var childlessKeywords = {
print: 'print',
import: 'import',
execute: 'execute'
};
var basicVariableExpression = /^[a-z_][\w]*$/i;
var bloatspace = /[\f\n\r\t\v\u00A0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u2028\u2029\u202f\u205f\u3000]/g;
var precompiledTemplates = {};
var strictScoping = true;
// Getters and Setters
Object.defineProperty(this, 'strictScoping',
{
get: function()
{
return strictScoping;
},
set: function(value)
{
if (typeof value === 'boolean')
{
strictScoping = value;
}
else
{
throwError('strictScoping must be boolean.', null, null, true);
};
}
});
// Public
this.compile = function(source)
{
var validatedSource = validateTemplateSource(source);
var tree = getBlockTree(validatedSource.validatedSource, validatedSource.templateID);
var precompiledTemplate = generatePrecompiledTemplate(tree, validatedSource.templateID);
return precompiledTemplate;
};
this.render = function(template, data)
{
data = validateTemplateData(data);
if (typeof template === 'function')
{
return internalRender(template, data);
}
else if (template instanceof HTMLScriptElement || typeof template === 'string')
{
return internalRender(this.compile(template), data);
}
else
{
throwError('Template must be HTMLScriptElement, function, or string.');
};
};
this.bulkPrecompile = function()
{
var bulkPrecompiledTemplates = [];
var scriptElements = document.getElementsByTagName('script');
for (var scriptIndex = 0; scriptIndex < scriptElements.length; scriptIndex++)
{
var scriptElement = scriptElements.item(scriptIndex);
if (scriptElement.getAttribute('type') == 'text/x-html-emboss')
{
var ignore = false;
if (scriptElement.dataset.embossIgnore)
{
if (scriptElement.dataset.embossIgnore === 'true')
{
ignore = true;
};
};
if (!ignore)
{
var precompiledTemplateAssigned = 'Emboss.storePrecompiledTemplate(' + this.compile(scriptElement) + ', "' + scriptElement.id + '");';
bulkPrecompiledTemplates.push(precompiledTemplateAssigned);
};
};
};
var saveAnchor = document.createElement('a');
var blob = new Blob(bulkPrecompiledTemplates, {type: 'application/javascript'});
saveAnchor.href = URL.createObjectURL(blob);
saveAnchor.download = 'precompiledTemplates.js';
saveAnchor.click();
};
this.storePrecompiledTemplate = function(precompiledTemplate, id)
{
if (typeof precompiledTemplate !== 'function')
{
throwError('Precompiled template must be of type function to store.', null, null, true);
};
if (precompiledTemplates[id])
{
console.log('EmbossWarning: Stored precompiled template overwritten due to ID conflict.', null, null, true);
};
precompiledTemplates[id] = precompiledTemplate;
};
this.getPrecompiledTemplate = function(id)
{
return precompiledTemplates[id];
};
// Private
// Functionality Methods
function internalRender(compiledTemplate, data)
{
try
{
return compiledTemplate.call({}, data);
}
catch (ErrorEvent)
{
throwError('Error during template render. Ensure JavaScript expressions are valid and all integral data is being provided. Error reported by JavaScript: \n' + ErrorEvent.message, null, null, true);
};
};
function validateTemplateSource(source)
{
var validatedSource = null;
var templateID = null;
if (source === undefined || source === null)
{
throwError('Source undefined or null.');
}
else
{
if (source instanceof HTMLScriptElement)
{
validatedSource = source.innerHTML;
templateID = source.id;
}
else if (typeof source === 'string')
{
validatedSource = source;
}
else
{
throwError('Source must be HTMLScriptElement or string.');
};
};
validatedSource = validatedSource.replace(bloatspace, '');
return {validatedSource: validatedSource, templateID: templateID};
};
function validateTemplateData(data)
{
var validatedData = null;
if (typeof data === 'object')
{
validatedData = data;
}
else if (typeof data === 'undefined')
{
validatedData = null;
}
else
{
throwError('Data must be JSON-compatible object.');
};
return validatedData;
};
function getBlockTree(blockSource, templateID)
{
var blocks = blockSource.split(embossBlocksExpression);
while (blocks.indexOf('') !== -1)
{
blocks.splice(blocks.indexOf(''), 1);
};
var blockPointer = null;
var blockStack = [];
var blockTree = [];
for (var blockIndex = 0; blockIndex < blocks.length; blockIndex++)
{
var blockText = blocks[blockIndex];
var pair = getBlockKeywordArgumentPair(blockText);
var keyword = pair.keyword;
var argument = pair.argument;
var closer = pair.closer;
var outputBlockText = false;
if (keywords[keyword])
{
var blockIgnored = false;
var blockNeglected = false;
if (blockStack.length > 0)
{
var frontStackBlock = blockStack[blockStack.length - 1];
if (frontStackBlock.keyword === keywords['ignore'])
{
if (closer && keyword === keywords['ignore'])
{
blockStack.pop();
blockPointer = blockPointer.parent;
};
blockIgnored = true;
}
else if (frontStackBlock.keyword === keywords['neglect'])
{
if (closer && keyword === keywords['neglect'])
{
blockStack.pop();
blockPointer = blockPointer.parent;
}
else
{
outputBlockText = true;
};
blockNeglected = true;
};
};
if (!(blockIgnored || blockNeglected))
{
if (closer)
{
if (blockStack.length === 0)
{
throwError('No opening tag: got closer "' + keyword + '" without opener.');
}
else
{
var openerBlock = blockStack.pop();
if (openerBlock.keyword === keyword)
{
blockPointer = blockPointer.parent;
}
else
{
throwError('Invalid closer: got "' + keyword + '", expected "' + openerBlock.keyword + '".');
};
};
}
else
{
var newBlock = new Block(blockPointer, blockText, keyword, argument, templateID);
if (keyword !== keywords['ignore'])
{
addBlockToTree(blockTree, blockPointer, newBlock);
};
if (keywords[childlessKeywords[keyword]])
{
if (keyword === keywords['import'])
{
var importArguments = conductImportStatement(newBlock);
var importedScriptElement = conductImportStatement(newBlock);
var validatedSource = validateTemplateSource(importArguments.scriptElement);
var importedTree = getBlockTree(validatedSource.validatedSource, importArguments.templateID);
for (var importedTreeBlockIndex = 0; importedTreeBlockIndex < importedTree.length; importedTreeBlockIndex++)
{
var importedBlock = importedTree[importedTreeBlockIndex];
newBlock.appendChildBlock(importedBlock);
};
};
}
else
{
blockStack.push(newBlock);
blockPointer = newBlock;
};
};
};
}
else
{
outputBlockText = true;
};
if (outputBlockText)
{
addBlockToTree(blockTree, blockPointer, blockText);
};
};
if (blockStack.length > 0)
{
var openerBlock = blockStack[blockStack.length - 1];
throwError('No closing tag: got "' + openerBlock.keyword + '" without closer.');
};
return blockTree;
};
function getBlockKeywordArgumentPair(blockText)
{
blockText = blockText.replace(/[{}]/g, '');
var spaceIndex = blockText.indexOf(' ');
var keyword = blockText;
var argument = '';
var closer = false;
if (spaceIndex > -1)
{
keyword = blockText.slice(0, spaceIndex);
argument = blockText.slice(spaceIndex + 1);
};
if (keyword.substring(0, 1) === '/')
{
closer = true;
keyword = keyword.substring(1);
};
return {keyword: keyword, argument: argument, closer: closer};
};
function addBlockToTree(blockTree, blockPointer, block)
{
if (blockPointer)
{
blockPointer.appendChildBlock(block);
}
else
{
blockTree.push(block);
};
};
function conductImportStatement(block)
{
var firstDelimiterIndex = block.argument.indexOf('\'');
var secondDelimiterIndex = -1;
if (firstDelimiterIndex > -1)
{
secondDelimiterIndex = block.argument.indexOf('\'', firstDelimiterIndex + 1);
};
if (firstDelimiterIndex > -1 && secondDelimiterIndex > -1)
{
var templateID = block.argument.substring(firstDelimiterIndex + 1, secondDelimiterIndex);
var scriptElement = document.getElementById(templateID);
if (scriptElement)
{
return {scriptElement: scriptElement, templateID: templateID};
}
else
{
throwError('Could not find script element for import expression.', block);
};
}
else
{
throwError('Missing script ID in import expression.', block);
};
};
function generatePrecompiledTemplate(tree, templateID)
{
var code = 'var EmbossInternalData = {};';
code += 'if (templateData){';
code += 'for (EmbossInternalData.templateDataIndex = 0; EmbossInternalData.templateDataIndex < Object.keys(templateData).length; EmbossInternalData.templateDataIndex++){';
code += 'EmbossInternalData.templateDataKey = Object.keys(templateData)[EmbossInternalData.templateDataIndex];';
code += 'if (EmbossInternalData.templateDataKey === "EmbossInternalData"){throw {message: "EmbossError: Template data keys may not use \'EmbossInternalData\' name."}};';
code += 'eval("var " + EmbossInternalData.templateDataKey + " = " + JSON.stringify(templateData[EmbossInternalData.templateDataKey]) + ";");';
code += '};};';
code += 'var compiledHTML = "";';
code += inspectChildren(tree, {loopIndexIdentifierCount: 0}, null);
code += 'return compiledHTML;';
try
{
var precompiledTemplate = new Function('templateData', code);
}
catch (ErrorEvent)
{
throwError('Error in template. Ensure JavaScript expressions are valid. Error reported by JavaScript: \n' + ErrorEvent.message, null, templateID);
};
return precompiledTemplate;
};
function inspectChildren(children, loops, importScope)
{
var childrenCode = '';
var ifPointer = null;
for (var blockIndex = 0; blockIndex < children.length; blockIndex++)
{
var block = children[blockIndex];
var isBlockConditional = true;
if (block instanceof Block)
{
if (block.keyword === keywords['if'])
{
ifPointer = block;
}
else if (block.keyword === keywords['elseif'] || block.keyword === keywords['else'])
{
if (!ifPointer)
{
throwError('Must be preceded by if tag.', block);
};
}
else
{
isBlockConditional = false;
};
}
else
{
isBlockConditional = false;
};
if (!isBlockConditional)
{
if (ifPointer)
{
ifPointer = null;
childrenCode += ';';
};
};
childrenCode += inspectBlock(block, loops, importScope);
};
return childrenCode;
};
function inspectBlock(block, loops, importScope)
{
var blockCode = [];
if (block instanceof Block)
{
if (block.keyword === keywords['if'])
{
blockCode += 'if (' + block.argument + '){';
blockCode += inspectChildren(block.children, loops, importScope);
blockCode += '}';
}
else if (block.keyword === keywords['elseif'])
{
blockCode += 'else if (' + block.argument + '){';
blockCode += inspectChildren(block.children, loops, importScope);
blockCode += '}';
}
else if (block.keyword === keywords['else'])
{
blockCode += 'else {';
blockCode += inspectChildren(block.children, loops, importScope);
blockCode += '}';
}
else if (block.keyword === keywords['for'])
{
if (strictScoping)
{
blockCode += '(function(){';
};
blockCode += 'for (' + block.argument + '){';
blockCode += inspectChildren(block.children, loops, importScope);
blockCode += '};';
if (strictScoping)
{
blockCode += '})();';
};
}
else if (block.keyword === keywords['each'])
{
var eachArguments = getEachArguments(block);
var loopIndexIdentifier = 'loopIndexIdentifier' + loops.loopIndexIdentifierCount;
loops.loopIndexIdentifierCount++;
if (strictScoping)
{
blockCode += '(function(){';
};
blockCode += 'for (EmbossInternalData.' + loopIndexIdentifier + ' = 0; EmbossInternalData.' + loopIndexIdentifier + ' < Object.keys(' + eachArguments.arrayExpression + ').length; EmbossInternalData.' + loopIndexIdentifier + '++){';
if (eachArguments.keyIdentifier)
{
blockCode += 'var ' + eachArguments.keyIdentifier + ' = [Object.keys(' + eachArguments.arrayExpression + ')[EmbossInternalData.' + loopIndexIdentifier + ']];';
blockCode += 'var ' + eachArguments.itemIdentifier + ' = ' + eachArguments.arrayExpression + '[' + eachArguments.keyIdentifier + '];';
}
else
{
blockCode += 'var ' + eachArguments.itemIdentifier + ' = ' + eachArguments.arrayExpression + '[Object.keys(' + eachArguments.arrayExpression + ')[EmbossInternalData.' + loopIndexIdentifier + ']];';
};
if (eachArguments.countIdentifier)
{
blockCode += 'var ' + eachArguments.countIdentifier + ' = EmbossInternalData.' + loopIndexIdentifier + ';';
};
blockCode += inspectChildren(block.children, loops, importScope);
blockCode += '};';
if (strictScoping)
{
blockCode += '})();';
};
}
else if (block.keyword === keywords['print'])
{
blockCode += 'compiledHTML += ' + block.argument + ';';
}
else if (block.keyword === keywords['neglect'])
{
blockCode += inspectChildren(block.children, loops, importScope);
}
else if (block.keyword === keywords['import'])
{
var contextFound = false;
var firstDelimiterIndex = block.argument.indexOf('\'');
var secondDelimiterIndex = block.argument.indexOf('\'', firstDelimiterIndex);
var contextKeywordIndex = block.argument.indexOf(secondOrderKeywords['context']);
if (contextKeywordIndex > -1)
{
var augmentedImportScope = null;
var contextExpressionIndex = contextKeywordIndex + (secondOrderKeywords['context'].length + 1);
var contextExpression = block.argument.substring(contextExpressionIndex, block.argument.length);
if (contextExpression.trim().length > 0)
{
contextFound = true;
}
else
{
throwError('Context argument missing.', block);
};
};
if (contextFound)
{
if (strictScoping)
{
blockCode += '(function(){';
};
blockCode += 'for (EmbossInternalData.contextDataIndex = 0; EmbossInternalData.contextDataIndex < Object.keys(' + contextExpression + ').length; EmbossInternalData.contextDataIndex++){';
blockCode += 'EmbossInternalData.contextDataKey = Object.keys(' + contextExpression + ')[EmbossInternalData.contextDataIndex];';
blockCode += 'if (EmbossInternalData.contextDataKey === "EmbossInternalData"){throw {message: "EmbossError: Template data keys may not use \'EmbossInternalData\' name."}};';
blockCode += 'eval("var " + EmbossInternalData.contextDataKey + " = " + JSON.stringify(' + contextExpression + '[EmbossInternalData.contextDataKey]) + ";");';
blockCode += '};';
};
blockCode += inspectChildren(block.children, loops, importScope);
if (strictScoping && contextFound)
{
blockCode += '})();';
};
}
else if (block.keyword === keywords['execute'])
{
blockCode += block.argument + ';';
};
}
else if (typeof(block) == 'string')
{
blockCode += 'compiledHTML += ' + JSON.stringify(block) + ';';
}
else
{
throwError('Unknown block type.');
};
return blockCode;
};
function getEachArguments(block)
{
var argument = block.argument.trim();
var frontPartExpression = /^([a-z_][\w]*) in /;
var backPartExpression = /(?: (count|key) ([a-z_][\w]*)(?: (count|key) ([a-z_][\w]*))?)$/;
var frontPart = frontPartExpression.exec(argument);
var backPart = backPartExpression.exec(argument);
var itemIdentifier = null;
var arrayExpression = null;
var countIdentifier = null;
var keyIdentifier = null;
if (frontPart)
{
itemIdentifier = frontPart[1];
if (itemIdentifier)
{
if (backPart)
{
arrayExpression = argument.substring(frontPartExpression.exec(argument)[0].length, backPartExpression.exec(argument).index);
var keywords = [{keyword: backPart[1], identifier: backPart[2]}, {keyword: backPart[3], identifier: backPart[4]}];
for (var index in keywords)
{
var keyword = keywords[index];
if (keyword.keyword)
{
if (keyword.keyword === secondOrderKeywords['count'])
{
countIdentifier = keyword.identifier;
}
else
{
keyIdentifier = keyword.identifier;
};
};
};
}
else
{
arrayExpression = argument.substring(frontPart[0].length, argument.length);
};
}
else
{
throwError('Item identifier not found.', block);
};
}
else
{
throwError('Each arguments not found.', block);
};
return {itemIdentifier: itemIdentifier, arrayExpression: arrayExpression, countIdentifier: countIdentifier, keyIdentifier: keyIdentifier};
};
// Utility Methods
function throwError(errorMessage, block, templateID, omitTemplate)
{
if (!templateID)
{
var templateID = 'Anonymous Template';
};
var blockExpression = '';
if (block)
{
blockExpression = 'Error in block ("' + block.keyword + ' ' + block.argument + '"): ';
templateID = 'Template: ' + block.templateID;
};
if (omitTemplate)
{
var templateID = '';
}
else
{
var templateID = ' (' + templateID + ')';
};
throw ('EmbossError' + templateID + ': ' + blockExpression + errorMessage);
};
// Objects
function Block(parent, text, keyword, argument, templateID)
{
this.parent = parent;
this.text = text;
this.keyword = keyword;
this.argument = argument;
this.templateID = templateID;
this.children = [];
this.appendChildBlock = function(block)
{
this.children.push(block);
};
};
};
}
)();