diff --git a/mustache.js b/mustache.js index b33e3dd..ee14306 100644 --- a/mustache.js +++ b/mustache.js @@ -383,13 +383,16 @@ Context.prototype.resolvePipelineOperator = function resolvePipelineOperator (value, pipelines){ var self = this; var pipelineResolver = function pipelineResolver (val, func){ - if (self.view.hasOwnProperty(func)){ - return self.view[func](val); - } else if (self.parent && self.parent.view.hasOwnProperty(func)){ - return self.parent.view[func](val); - } else { - return val; + var findFunction = function(instance, functionToFind, valueToPutInFindedFunction, depth){ + if(depth <= 0 || !instance) return null; + if(instance.view.hasOwnProperty(functionToFind)) return instance.view[func](valueToPutInFindedFunction); + + return findFunction(instance.parent, functionToFind, val, depth) } + + var findedFunction = findFunction(self, func, val, 20) + + return findedFunction ? findedFunction : val }; return pipelines.reduce(pipelineResolver,value); }; diff --git a/mustache.min.js b/mustache.min.js index 882f995..e60ea82 100644 --- a/mustache.min.js +++ b/mustache.min.js @@ -1 +1 @@ -(function defineMustache(global,factory){if(typeof exports==="object"&&exports&&typeof exports.nodeName!=="string"){factory(exports)}else if(typeof define==="function"&&define.amd){define(["exports"],factory)}else{global.Mustache={};factory(global.Mustache)}})(this,function mustacheFactory(mustache){var objectToString=Object.prototype.toString;var isArray=Array.isArray||function isArrayPolyfill(object){return objectToString.call(object)==="[object Array]"};function isFunction(object){return typeof object==="function"}function typeStr(obj){return isArray(obj)?"array":typeof obj}function escapeRegExp(string){return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function hasProperty(obj,propName){return obj!=null&&typeof obj==="object"&&propName in obj}function primitiveHasOwnProperty(primitive,propName){return primitive!=null&&typeof primitive!=="object"&&primitive.hasOwnProperty&&primitive.hasOwnProperty(propName)}var regExpTest=RegExp.prototype.test;function testRegExp(re,string){return regExpTest.call(re,string)}var nonSpaceRe=/\S/;function isWhitespace(string){return!testRegExp(nonSpaceRe,string)}var entityMap={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function escapeHtml(string){return String(string).replace(/[&<>"'`=\/]/g,function fromEntityMap(s){return entityMap[s]})}var whiteRe=/\s*/;var spaceRe=/\s+/;var equalsRe=/\s*=/;var curlyRe=/\s*\}/;var tagRe=/#|\^|\/|>|\{|&|=|!/;var pipelineRe=/\|\>?/;function parseTemplate(template,tags){if(!template)return[];var sections=[];var tokens=[];var spaces=[];var hasTag=false;var nonSpace=false;function stripSpace(){if(hasTag&&!nonSpace){while(spaces.length)delete tokens[spaces.pop()]}else{spaces=[]}hasTag=false;nonSpace=false}var openingTagRe,closingTagRe,closingCurlyRe;function compileTags(tagsToCompile){if(typeof tagsToCompile==="string")tagsToCompile=tagsToCompile.split(spaceRe,2);if(!isArray(tagsToCompile)||tagsToCompile.length!==2)throw new Error("Invalid tags: "+tagsToCompile);openingTagRe=new RegExp(escapeRegExp(tagsToCompile[0])+"\\s*");closingTagRe=new RegExp("\\s*"+escapeRegExp(tagsToCompile[1]));closingCurlyRe=new RegExp("\\s*"+escapeRegExp("}"+tagsToCompile[1]))}compileTags(tags||mustache.tags);var scanner=new Scanner(template);var start,type,value,chr,token,openSection;while(!scanner.eos()){start=scanner.pos;value=scanner.scanUntil(openingTagRe);if(value){for(var i=0,valueLength=value.length;i0?sections[sections.length-1][4]:nestedTokens;break;default:collector.push(token)}}return nestedTokens}function Scanner(string){this.string=string;this.tail=string;this.pos=0}Scanner.prototype.eos=function eos(){return this.tail===""};Scanner.prototype.scan=function scan(re){var match=this.tail.match(re);if(!match||match.index!==0)return"";var string=match[0];this.tail=this.tail.substring(string.length);this.pos+=string.length;return string};Scanner.prototype.scanUntil=function scanUntil(re){var index=this.tail.search(re),match;switch(index){case-1:match=this.tail;this.tail="";break;case 0:match="";break;default:match=this.tail.substring(0,index);this.tail=this.tail.substring(index)}this.pos+=match.length;return match};function Context(view,parentContext){this.view=view;this.cache={".":this.view};this.parent=parentContext}Context.prototype.push=function push(view){return new Context(view,this)};Context.prototype.resolvePipelineOperator=function resolvePipelineOperator(value,pipelines){var self=this;var pipelineResolver=function pipelineResolver(val,func){if(self.view.hasOwnProperty(func)){return self.view[func](val)}else if(self.parent&&self.parent.view.hasOwnProperty(func)){return self.parent.view[func](val)}else{return val}};return pipelines.reduce(pipelineResolver,value)};Context.prototype.lookup=function lookup(name){var replacedName=name.replace(new RegExp(spaceRe,"g"),"").split(pipelineRe);name=replacedName.shift();pipelines=replacedName;var cache=this.cache;var value;if(cache.hasOwnProperty(name)){value=cache[name]}else{var context=this,intermediateValue,names,index,lookupHit=false;while(context){if(name.indexOf(".")>0){intermediateValue=context.view;names=name.split(".");index=0;while(intermediateValue!=null&&index")value=this.renderPartial(token,context,partials,tags);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);if(value!==undefined)buffer+=value}return buffer};Writer.prototype.renderSection=function renderSection(token,context,partials,originalTemplate){var self=this;var buffer="";var value=context.lookup(token[1]);function subRender(template){return self.render(template,context,partials)}if(!value)return;if(isArray(value)){for(var j=0,valueLength=value.length;j":">",'"':""","'":"'","/":"/","`":"`","=":"="};function escapeHtml(string){return String(string).replace(/[&<>"'`=\/]/g,function fromEntityMap(s){return entityMap[s]})}var whiteRe=/\s*/;var spaceRe=/\s+/;var equalsRe=/\s*=/;var curlyRe=/\s*\}/;var tagRe=/#|\^|\/|>|\{|&|=|!/;var pipelineRe=/\|\>?/;function parseTemplate(template,tags){if(!template)return[];var sections=[];var tokens=[];var spaces=[];var hasTag=false;var nonSpace=false;function stripSpace(){if(hasTag&&!nonSpace){while(spaces.length)delete tokens[spaces.pop()]}else{spaces=[]}hasTag=false;nonSpace=false}var openingTagRe,closingTagRe,closingCurlyRe;function compileTags(tagsToCompile){if(typeof tagsToCompile==="string")tagsToCompile=tagsToCompile.split(spaceRe,2);if(!isArray(tagsToCompile)||tagsToCompile.length!==2)throw new Error("Invalid tags: "+tagsToCompile);openingTagRe=new RegExp(escapeRegExp(tagsToCompile[0])+"\\s*");closingTagRe=new RegExp("\\s*"+escapeRegExp(tagsToCompile[1]));closingCurlyRe=new RegExp("\\s*"+escapeRegExp("}"+tagsToCompile[1]))}compileTags(tags||mustache.tags);var scanner=new Scanner(template);var start,type,value,chr,token,openSection;while(!scanner.eos()){start=scanner.pos;value=scanner.scanUntil(openingTagRe);if(value){for(var i=0,valueLength=value.length;i0?sections[sections.length-1][4]:nestedTokens;break;default:collector.push(token)}}return nestedTokens}function Scanner(string){this.string=string;this.tail=string;this.pos=0}Scanner.prototype.eos=function eos(){return this.tail===""};Scanner.prototype.scan=function scan(re){var match=this.tail.match(re);if(!match||match.index!==0)return"";var string=match[0];this.tail=this.tail.substring(string.length);this.pos+=string.length;return string};Scanner.prototype.scanUntil=function scanUntil(re){var index=this.tail.search(re),match;switch(index){case-1:match=this.tail;this.tail="";break;case 0:match="";break;default:match=this.tail.substring(0,index);this.tail=this.tail.substring(index)}this.pos+=match.length;return match};function Context(view,parentContext){this.view=view;this.cache={".":this.view};this.parent=parentContext}Context.prototype.push=function push(view){return new Context(view,this)};Context.prototype.resolvePipelineOperator=function resolvePipelineOperator(value,pipelines){var self=this;var pipelineResolver=function pipelineResolver(val,func){var findFunction=function(instance,functionToFind,valueToPutInFindedFunction,depth){if(depth<=0||!instance)return null;if(instance.view.hasOwnProperty(functionToFind))return instance.view[func](valueToPutInFindedFunction);return findFunction(instance.parent,functionToFind,val,depth)};var findedFunction=findFunction(self,func,val,20);return findedFunction?findedFunction:val};return pipelines.reduce(pipelineResolver,value)};Context.prototype.lookup=function lookup(name){var replacedName=name.replace(new RegExp(spaceRe,"g"),"").split(pipelineRe);name=replacedName.shift();pipelines=replacedName;var cache=this.cache;var value;if(cache.hasOwnProperty(name)){value=cache[name]}else{var context=this,intermediateValue,names,index,lookupHit=false;while(context){if(name.indexOf(".")>0){intermediateValue=context.view;names=name.split(".");index=0;while(intermediateValue!=null&&index")value=this.renderPartial(token,context,partials,tags);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);if(value!==undefined)buffer+=value}return buffer};Writer.prototype.renderSection=function renderSection(token,context,partials,originalTemplate){var self=this;var buffer="";var value=context.lookup(token[1]);function subRender(template){return self.render(template,context,partials)}if(!value)return;if(isArray(value)){for(var j=0,valueLength=value.length;j", "homepage": "https://github.com/janl/mustache.js", @@ -44,7 +44,7 @@ "eslint": "2.5.1", "jshint": "^2.9.5", "mocha": "^3.0.2", - "uglify-js": "^3.4.6", + "uglify-js": "^3.4.6" "zuul": "^3.11.0", "zuul-ngrok": "nolanlawson/zuul-ngrok#patch-1" }, diff --git a/test/_files/cli_output.txt b/test/_files/cli_output.txt new file mode 100644 index 0000000..66556fe --- /dev/null +++ b/test/_files/cli_output.txt @@ -0,0 +1 @@ +Howdy LeBron, CLI rox \ No newline at end of file diff --git a/test/_files/pipeline.js b/test/_files/pipeline.js index 3dc4cca..543a57a 100644 --- a/test/_files/pipeline.js +++ b/test/_files/pipeline.js @@ -3,6 +3,15 @@ job: { name: 'Developer' }, + attributes:[ + { + name: 'good', + options: [ + 'outgoing', + 'shy' + ] + } + ], numbers:[1,2,3], sumOne:(value)=>{ return value + 1; diff --git a/test/_files/pipeline.mustache b/test/_files/pipeline.mustache index 7d40575..3eba2da 100644 --- a/test/_files/pipeline.mustache +++ b/test/_files/pipeline.mustache @@ -48,3 +48,10 @@ {{#numbers}} {{. |> sumOne |> setNameFuncion |> lower}} {{/numbers}} +-- Level Array +{{#attributes}} +{{#options}} +{{name |> upper}} +{{. |> upper}} +{{/options}} +{{/attributes}} \ No newline at end of file diff --git a/test/_files/pipeline.txt b/test/_files/pipeline.txt index 1a11b2d..492c4f6 100644 --- a/test/_files/pipeline.txt +++ b/test/_files/pipeline.txt @@ -48,3 +48,8 @@ Developer 2: this is a value 3: this is a value 4: this is a value +-- Level Array +GOOD +OUTGOING +GOOD +SHY