From 35f5fb46e78aef1feda4d9cfddd93bd356bdca28 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 8 Sep 2014 16:36:19 +0200 Subject: [PATCH] [IMP] qweb-js: reimplement string interpolation compilation as a single pass --- addons/web/static/lib/qweb/qweb2.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/addons/web/static/lib/qweb/qweb2.js b/addons/web/static/lib/qweb/qweb2.js index c7fa24b..983a3d4 100644 --- a/addons/web/static/lib/qweb/qweb2.js +++ b/addons/web/static/lib/qweb/qweb2.js @@ -601,24 +601,26 @@ QWeb2.Element = (function() { return r; }, string_interpolation : function(s) { + var _this = this; if (!s) { return "''"; } - var regex = /^{(.*)}(.*)/, - src = s.split(/#/), - r = []; - for (var i = 0, ilen = src.length; i < ilen; i++) { - var val = src[i], - m = val.match(regex); - if (m) { - r.push("(" + this.format_expression(m[1]) + ")"); - if (m[2]) { - r.push(this.engine.tools.js_escape(m[2])); - } - } else if (!(i === 0 && val === '')) { - r.push(this.engine.tools.js_escape((i === 0 ? '' : '#') + val)); - } + function append_literal(s) { + s && r.push(_this.engine.tools.js_escape(s)); } + + var re = /#{(.*?)}/g, start = 0, r = [], m; + while (m = re.exec(s)) { + // extract literal string between previous and current match + append_literal(s.slice(start, re.lastIndex - m[0].length)); + // extract matched expression + r.push('(' + this.format_expression(m[1]) + ')'); + // update position of new matching + start = re.lastIndex; + } + // remaining text after last expression + append_literal(s.slice(start)); + return r.join(' + '); }, indent : function() { -- 1.7.10.4