From c64acead26df27f6a5e0f5487c43354a2227ea84 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 12:57:19 -0700 Subject: [PATCH] Make HTML templates output Javascript, part 1. #829 --- tools/template.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/template.js b/tools/template.js index cda83b9831..2e361ea939 100644 --- a/tools/template.js +++ b/tools/template.js @@ -24,8 +24,8 @@ tools.multiline = function(text) { return text.replace(/\n+/g, '\n').split(/^/m).map(JSON.stringify).join('+').replace(/"\+"/g, '\\\n'); }; -// Convert JSON object to Coffeescript expression (via embedded JS). -var constExpression = data => '`' + JSON.stringify(data).replace(/`/g, '\\`') + '`'; +// Convert JSONify-able object to Javascript expression. +var constExpression = data => JSON.stringify(data).replace(/`/g, '\\`'); function TextStream(text) { this.text = text; @@ -151,11 +151,11 @@ Placeholder.prototype.build = function() { throw new Error(`JavaScript in template is not an expression (${expr})`); } switch(this.type) { - case '$': return `\`E(${expr})\``; // $ : escaped text - case '&': return `\`(${expr}).innerHTML\``; // & : contents of HTML element or template (of form {innerHTML: "safeHTML"}) - case '@': return `\`E.cat(${expr})\``; // @ : contents of array of HTML elements or templates (see src/General/Globals.coffee for E.cat) + case '$': return `E(${expr})`; // $ : escaped text + case '&': return `(${expr}).innerHTML`; // & : contents of HTML element or template (of form {innerHTML: "safeHTML"}) + case '@': return `E.cat(${expr})`; // @ : contents of array of HTML elements or templates (see src/General/Globals.coffee for E.cat) case '?': - return `(if \`(${expr})\` then ${this.args[1] || '""'} else ${this.args[2] || '""'})`; // ? : conditional expression + return `((${expr}) ? ${this.args[1] || '""'} : ${this.args[2] || '""'})`; // ? : conditional expression } throw new Error(`Unrecognized placeholder type (${this.type})`); }; @@ -168,11 +168,11 @@ tools.html = function(template) { if (stream.text) { throw new Error(`Unexpected characters in template (${stream.text}): ${template}`); } - return `(innerHTML: ${output})`; + return `(innerHTML: \`${output}\`)`; }; tools.assert = function(statement) { - return `throw new Error 'Assertion failed: ' + ${constExpression(statement)} unless ${statement}`; + return `throw new Error 'Assertion failed: ' + \`${constExpression(statement)}\` unless ${statement}`; }; function includesDir(templateName) {