MDL-56341 mustache: JS engines handles strings wrapped in quote
authorFrédéric Massart <fred@moodle.com>
Tue, 11 Oct 2016 12:03:48 +0000 (14:03 +0200)
committerDamyon Wiese <damyon@moodle.com>
Thu, 20 Oct 2016 04:13:42 +0000 (12:13 +0800)
Prior to the patch the strings wrapped as the following one
where not found and replaced when rendering the template via Javascript

{{#quote}}{{#str}}string,component{{/str}}{{/quote}}

lib/amd/build/templates.min.js
lib/amd/src/templates.js

index 37a25bf..e13f81b 100644 (file)
Binary files a/lib/amd/build/templates.min.js and b/lib/amd/build/templates.min.js differ
index 7f45924..0899444 100644 (file)
@@ -237,7 +237,9 @@ define(['core/mustache',
 
         var index = this.requiredStrings.length;
         this.requiredStrings.push({key: key, component: component, param: param});
-        return '{{_s' + index + '}}';
+
+        // The placeholder must not use {{}} as those can be misinterpreted by the engine.
+        return '[[_s' + index + ']]';
     };
 
     /**
@@ -328,7 +330,7 @@ define(['core/mustache',
      * @return {String} The treated content.
      */
     Renderer.prototype.treatStringsInContent = function(content, strings) {
-        var pattern = /{{_s\d+}}/,
+        var pattern = /\[\[_s\d+\]\]/,
             treated,
             index,
             strIndex,
@@ -345,7 +347,7 @@ define(['core/mustache',
                 treated += content.substring(0, index);
                 content = content.substr(index);
                 strIndex = '';
-                walker = 4;  // 4 is the length of '{{_s'.
+                walker = 4;  // 4 is the length of '[[_s'.
 
                 // Walk the characters to manually extract the index of the string from the placeholder.
                 char = content.substr(walker, 1);
@@ -353,16 +355,16 @@ define(['core/mustache',
                     strIndex += char;
                     walker++;
                     char = content.substr(walker, 1);
-                } while (char != '}');
+                } while (char != ']');
 
                 // Get the string, add it to the treated result, and remove the placeholder from the content to treat.
                 strFinal = strings[parseInt(strIndex, 10)];
                 if (typeof strFinal === 'undefined') {
-                    Log.debug('Could not find string for pattern {{_s' + strIndex + '}}.');
+                    Log.debug('Could not find string for pattern [[_s' + strIndex + ']].');
                     strFinal = '';
                 }
                 treated += strFinal;
-                content = content.substr(6 + strIndex.length);  // 6 is the length of the placeholder without the index: '{{_s}}'.
+                content = content.substr(6 + strIndex.length);  // 6 is the length of the placeholder without the index: '[[_s]]'.
 
                 // Find the next placeholder.
                 index = content.search(pattern);