From 5f76cad961dff06c894c1792e20f42a17ae05b80 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 20 Sep 2017 12:00:42 -0400 Subject: [PATCH] fix escaping of %-prefixed names --- mocha.opts | 1 - src/generators/dom/index.ts | 23 +++++++++++--------- src/utils/stringify.ts | 2 +- test/runtime/samples/escaped-text/_config.js | 6 ++++- test/runtime/samples/escaped-text/main.html | 4 +++- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/mocha.opts b/mocha.opts index af6b17a845..427b029758 100644 --- a/mocha.opts +++ b/mocha.opts @@ -1,2 +1 @@ ---bail test/test.js \ No newline at end of file diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index cc7997455d..2ee1a2eef5 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -344,18 +344,21 @@ export default function dom( let result = builder .toString() - .replace(/%(\w+(?:-\w+)?)/gm, (match: string, name: string) => { - return generator.templateVars.get(name); - }) - .replace(/(@+)(\w*)/g, (match: string, sigil: string, name: string) => { - if (sigil !== '@') return sigil.slice(1) + name; - - if (name in shared) { - if (options.dev && `${name}Dev` in shared) name = `${name}Dev`; - usedHelpers.add(name); + .replace(/(%+|@+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => { + if (sigil === '@') { + if (name in shared) { + if (options.dev && `${name}Dev` in shared) name = `${name}Dev`; + usedHelpers.add(name); + } + + return generator.alias(name); + } + + if (sigil === '%') { + return generator.templateVars.get(name); } - return generator.alias(name); + return sigil.slice(1) + name; }); let helpers; diff --git a/src/utils/stringify.ts b/src/utils/stringify.ts index ab83bec5b4..26037827c7 100644 --- a/src/utils/stringify.ts +++ b/src/utils/stringify.ts @@ -3,7 +3,7 @@ export function stringify(data: string, options = {}) { } export function escape(data: string, { onlyEscapeAtSymbol = false } = {}) { - return data.replace(onlyEscapeAtSymbol ? /(@+)/g : /(@+|#+)/g, (match: string) => { + return data.replace(onlyEscapeAtSymbol ? /(%+|@+)/g : /(%+|@+|#+)/g, (match: string) => { return match + match[0]; }); } diff --git a/test/runtime/samples/escaped-text/_config.js b/test/runtime/samples/escaped-text/_config.js index 93a393d8cf..43644dde82 100644 --- a/test/runtime/samples/escaped-text/_config.js +++ b/test/runtime/samples/escaped-text/_config.js @@ -1,3 +1,7 @@ export default { - html: `@@x` + html: ` + @@x + %1 + %%2 + ` }; \ No newline at end of file diff --git a/test/runtime/samples/escaped-text/main.html b/test/runtime/samples/escaped-text/main.html index 6a173c026f..603b331ca6 100644 --- a/test/runtime/samples/escaped-text/main.html +++ b/test/runtime/samples/escaped-text/main.html @@ -1 +1,3 @@ -@@x \ No newline at end of file +@@x +%1 +%%2 \ No newline at end of file