From d712127f34d5fc6d656144b446c2c58c93372443 Mon Sep 17 00:00:00 2001 From: Mathew May Date: Fri, 6 Oct 2017 15:03:17 +0800 Subject: [PATCH] MDL-65758 library: Add Moodle modifications for Mustache --- lib/amd/build/mustache.min.js | 2 +- lib/amd/src/mustache.js | 115 ++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 6 deletions(-) diff --git a/lib/amd/build/mustache.min.js b/lib/amd/build/mustache.min.js index ab7d884a978..c795aedcc4e 100644 --- a/lib/amd/build/mustache.min.js +++ b/lib/amd/build/mustache.min.js @@ -1 +1 @@ -!function(a,b){"object"==typeof exports&&exports&&"string"!=typeof exports.nodeName?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a.Mustache={},b(a.Mustache))}(this,function(a){function b(a){return"function"==typeof a}function c(a){return q(a)?"array":typeof a}function d(a){return a.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function e(a,b){return null!=a&&"object"==typeof a&&b in a}function f(a,b){return null!=a&&"object"!=typeof a&&a.hasOwnProperty&&a.hasOwnProperty(b)}function g(a,b){return r.call(a,b)}function h(a){return!g(s,a)}function i(a){return String(a).replace(/[&<>"'`=\/]/g,function(a){return t[a]})}function j(b,c){function e(){if(r&&!s)for(;p.length;)delete o[p.pop()];else p=[];r=!1,s=!1}function f(a){if("string"==typeof a&&(a=a.split(v,2)),!q(a)||2!==a.length)throw new Error("Invalid tags: "+a);g=new RegExp(d(a[0])+"\\s*"),i=new RegExp("\\s*"+d(a[1])),j=new RegExp("\\s*"+d("}"+a[1]))}if(!b)return[];var g,i,j,n=[],o=[],p=[],r=!1,s=!1;f(c||a.tags);for(var t,z,A,B,C,D,E=new m(b);!E.eos();){if(t=E.pos,A=E.scanUntil(g))for(var F=0,G=A.length;F0?f[f.length-1][4]:d;break;default:e.push(b)}return d}function m(a){this.string=a,this.tail=a,this.pos=0}function n(a,b){this.view=a,this.cache={".":this.view},this.parent=b}function o(){this.cache={}}var p=Object.prototype.toString,q=Array.isArray||function(a){return"[object Array]"===p.call(a)},r=RegExp.prototype.test,s=/\S/,t={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="},u=/\s*/,v=/\s+/,w=/\s*=/,x=/\s*\}/,y=/#|\^|\/|>|\{|&|=|!/;m.prototype.eos=function(){return""===this.tail},m.prototype.scan=function(a){var b=this.tail.match(a);if(!b||0!==b.index)return"";var c=b[0];return this.tail=this.tail.substring(c.length),this.pos+=c.length,c},m.prototype.scanUntil=function(a){var b,c=this.tail.search(a);switch(c){case-1:b=this.tail,this.tail="";break;case 0:b="";break;default:b=this.tail.substring(0,c),this.tail=this.tail.substring(c)}return this.pos+=b.length,b},n.prototype.push=function(a){return new n(a,this)},n.prototype.lookup=function(a){var c,d=this.cache;if(d.hasOwnProperty(a))c=d[a];else{for(var g,h,i,j=this,k=!1;j;){if(a.indexOf(".")>0)for(g=j.view,h=a.split("."),i=0;null!=g&&i"===g?h=this.renderPartial(f,b,c,e):"&"===g?h=this.unescapedValue(f,b):"name"===g?h=this.escapedValue(f,b):"text"===g&&(h=this.rawValue(f)),void 0!==h&&(i+=h);return i},o.prototype.renderSection=function(a,c,d,e){function f(a){return g.render(a,c,d)}var g=this,h="",i=c.lookup(a[1]);if(i){if(q(i))for(var j=0,k=i.length;j"'`=\/]/g,function(a){return t[a]})}function j(b,c){function e(){if(r&&!s)for(;p.length;)delete o[p.pop()];else p=[];r=!1,s=!1}function f(a){if("string"==typeof a&&(a=a.split(v,2)),!q(a)||2!==a.length)throw new Error("Invalid tags: "+a);g=new RegExp(d(a[0])+"\\s*"),i=new RegExp("\\s*"+d(a[1])),j=new RegExp("\\s*"+d("}"+a[1]))}if(!b)return[];var g,i,j,n=[],o=[],p=[],r=!1,s=!1;f(c||a.tags);for(var t,z,A,B,C,D,E=new m(b);!E.eos();){if(t=E.pos,A=E.scanUntil(g))for(var F=0,G=A.length;F0?f[f.length-1][4]:d;break;default:e.push(b)}return d}function m(a){this.string=a,this.tail=a,this.pos=0}function n(a,b){this.view=a,this.blocks={},this.cache={".":this.view},this.parent=b}function o(){this.cache={}}var p=Object.prototype.toString,q=Array.isArray||function(a){return"[object Array]"===p.call(a)},r=RegExp.prototype.test,s=/\S/,t={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="},u=/\s*/,v=/\s+/,w=/\s*=/,x=/\s*\}/,y=/#|\^|\/|>|\{|&|=|!|\$|0)for(g=j.view,h=a.split("."),i=0;null!=g&&i"===g?h=this.renderPartial(f,b,c,e):"<"===g?h=this.renderBlock(f,b,c,d):"$"===g?h=this.renderBlockVariable(f,b,c,d):"&"===g?h=this.unescapedValue(f,b):"name"===g?h=this.escapedValue(f,b):"text"===g&&(h=this.rawValue(f)),void 0!==h&&(i+=h);return i},o.prototype.renderSection=function(a,c,d,e){function f(a){return g.render(a,c,d)}var g=this,h="",i=c.lookup(a[1]);if(i){if(q(i))for(var j=0,k=i.length;j|\{|&|=|!/; + var tagRe = /#|\^|\/|>|\{|&|=|!|\$|') value = this.renderPartial(token, context, partials, tags); + else if (symbol === '<') value = this.renderBlock(token, context, partials, originalTemplate); + else if (symbol === '$') value = this.renderBlockVariable(token, context, partials, originalTemplate); else if (symbol === '&') value = this.unescapedValue(token, context); else if (symbol === 'name') value = this.escapedValue(token, context); else if (symbol === 'text') value = this.rawValue(token); @@ -600,6 +676,34 @@ return this.renderTokens(this.parse(value, tags), context, partials, value); }; + Writer.prototype.renderBlock = function renderBlock (token, context, partials, originalTemplate) { + if (!partials) return; + + var value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; + if (value != null) + // Ignore any wrongly set block vars before we started. + context.clearBlockVars(); + // We are only rendering to record the default block variables. + this.renderTokens(token[4], context, partials, originalTemplate); + // Now we render and return the result. + var result = this.renderTokens(this.parse(value), context, partials, value); + // Don't leak the block variables outside this include. + context.clearBlockVars(); + return result; + }; + + Writer.prototype.renderBlockVariable = function renderBlockVariable (token, context, partials, originalTemplate) { + var value = token[1]; + + var exists = context.getBlockVar(value); + if (!exists) { + context.setBlockVar(value, originalTemplate.slice(token[3], token[5])); + return this.renderTokens(token[4], context, partials, originalTemplate); + } else { + return this.renderTokens(this.parse(exists), context, partials, exists); + } + }; + Writer.prototype.unescapedValue = function unescapedValue (token, context) { var value = context.lookup(token[1]); if (value != null) @@ -680,3 +784,4 @@ return mustache; })); +/* jshint ignore:end */