Skip to content

Commit

Permalink
Merge branch 'master' into minor
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenlehara committed Jun 26, 2018
2 parents ce9f155 + 7da43d4 commit 40951e3
Show file tree
Hide file tree
Showing 21 changed files with 472 additions and 219 deletions.
4 changes: 3 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"start": true,
"stop": true,
"global": true,
"Promise": true
"Promise": true,
"Map": true,
"WeakMap": true
},
"strict": false,
"curly": true,
Expand Down
82 changes: 51 additions & 31 deletions can-stache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var TextSectionBuilder = require('./src/text_section');
var mustacheCore = require('./src/mustache_core');
var mustacheHelpers = require('./helpers/core');
require('./helpers/converter');
var addBindings = require('./src/bindings');
var getIntermediateAndImports = require('can-stache-ast').parse;
var utils = require('./src/utils');
var makeRendererConvertScopes = utils.makeRendererConvertScopes;
Expand Down Expand Up @@ -83,11 +84,12 @@ function stache (filename, template) {
// given section and modify the section to use that renderer.
// For example, if an HTMLSection is passed with mode `#` it knows to
// create a liveBindingBranchRenderer and pass that to section.add.
// jshint maxdepth:5
makeRendererAndUpdateSection = function(section, mode, stache, lineNo){

if(mode === ">") {
// Partials use liveBindingPartialRenderers
section.add(mustacheCore.makeLiveBindingPartialRenderer(stache, copyState({ lineNo: lineNo })));
section.add(mustacheCore.makeLiveBindingPartialRenderer(stache, copyState({ filename: section.filename, lineNo: lineNo })));

} else if(mode === "/") {

Expand All @@ -99,19 +101,24 @@ function stache (filename, template) {
section.endSection();
}

if(section instanceof HTMLSectionBuilder) {
//!steal-remove-start
var last = state.sectionElementStack[state.sectionElementStack.length - 1];
if (last.tag && last.type === "section" && stache !== "" && stache !== last.tag) {
if (filename) {
dev.warn(filename + ":" + lineNo + ": unexpected closing tag {{/" + stache + "}} expected {{/" + last.tag + "}}");
}
else {
dev.warn(lineNo + ": unexpected closing tag {{/" + stache + "}} expected {{/" + last.tag + "}}");
// to avoid "Blocks are nested too deeply" when linting
//!steal-remove-start
if (process.env.NODE_ENV !== 'production') {
if(section instanceof HTMLSectionBuilder) {
var last = state.sectionElementStack[state.sectionElementStack.length - 1];
if (last.tag && last.type === "section" && stache !== "" && stache !== last.tag) {
if (filename) {
dev.warn(filename + ":" + lineNo + ": unexpected closing tag {{/" + stache + "}} expected {{/" + last.tag + "}}");
}
else {
dev.warn(lineNo + ": unexpected closing tag {{/" + stache + "}} expected {{/" + last.tag + "}}");
}
}
}
//!steal-remove-end
}
//!steal-remove-end

if(section instanceof HTMLSectionBuilder) {
state.sectionElementStack.pop();
}
} else if(mode === "else") {
Expand All @@ -133,31 +140,32 @@ function stache (filename, template) {
if(mode === "{" || mode === "&") {

// Adds a renderer function that just reads a value or calls a helper.
section.add(makeRenderer(null,stache, copyState({ lineNo: lineNo })));
section.add(makeRenderer(null,stache, copyState({ filename: section.filename, lineNo: lineNo })));

} else if(mode === "#" || mode === "^" || mode === "<") {
// Adds a renderer function and starts a section.
var renderer = makeRenderer(mode, stache, copyState({ lineNo: lineNo }));
var renderer = makeRenderer(mode, stache, copyState({ filename: section.filename, lineNo: lineNo }));
var sectionItem = {
type: "section"
};
section.startSection(renderer);
section.last().startedWith = mode;

// If we are a directly nested section, count how many we are within
if(section instanceof HTMLSectionBuilder) {
//!steal-remove-start
var tag = typeof renderer.exprData.closingTag === 'function' ?
renderer.exprData.closingTag() : '';
if (process.env.NODE_ENV !== 'production') {
var tag = typeof renderer.exprData.closingTag === 'function' ?
renderer.exprData.closingTag() : '';
sectionItem.tag = tag;
}
//!steal-remove-end

state.sectionElementStack.push({
type: "section",
//!steal-remove-start
tag: tag
//!steal-remove-end
});
state.sectionElementStack.push(sectionItem);
}
} else {
// Adds a renderer function that only updates text.
section.add(makeRenderer(null, stache, copyState({text: true, lineNo: lineNo })));
section.add(makeRenderer(null, stache, copyState({text: true, filename: section.filename, lineNo: lineNo })));
}

}
Expand Down Expand Up @@ -214,7 +222,9 @@ function stache (filename, template) {
// Call directlyNested now as it's stateful.
addAttributesCallback(state.node, function(scope, parentNodeList){
//!steal-remove-start
scope.set('scope.lineNumber', lineNo);
if (process.env.NODE_ENV !== 'production') {
scope.set('scope.lineNumber', lineNo);
}
//!steal-remove-end
viewCallbacks.tagHandler(this,tagName, {
scope: scope,
Expand Down Expand Up @@ -281,7 +291,9 @@ function stache (filename, template) {
var current = state.sectionElementStack[state.sectionElementStack.length - 1];
addAttributesCallback(oldNode, function(scope, parentNodeList){
//!steal-remove-start
scope.set('scope.lineNumber', lineNo);
if (process.env.NODE_ENV !== 'production') {
scope.set('scope.lineNumber', lineNo);
}
//!steal-remove-end
viewCallbacks.tagHandler(this,tagName, {
scope: scope,
Expand Down Expand Up @@ -321,10 +333,12 @@ function stache (filename, template) {
var attrCallback = viewCallbacks.attr(attrName);

//!steal-remove-start
var decodedAttrName = attributeEncoder.decode(attrName);
var weirdAttribute = !!wrappedAttrPattern.test(decodedAttrName) || !!colonWrappedAttrPattern.test(decodedAttrName);
if (weirdAttribute && !attrCallback) {
dev.warn("unknown attribute binding " + decodedAttrName + ". Is can-stache-bindings imported?");
if (process.env.NODE_ENV !== 'production') {
var decodedAttrName = attributeEncoder.decode(attrName);
var weirdAttribute = !!wrappedAttrPattern.test(decodedAttrName) || !!colonWrappedAttrPattern.test(decodedAttrName);
if (weirdAttribute && !attrCallback) {
dev.warn("unknown attribute binding " + decodedAttrName + ". Is can-stache-bindings imported?");
}
}
//!steal-remove-end

Expand All @@ -334,7 +348,9 @@ function stache (filename, template) {
}
state.node.attributes.push(function(scope, nodeList){
//!steal-remove-start
scope.set('scope.lineNumber', lineNo);
if (process.env.NODE_ENV !== 'production') {
scope.set('scope.lineNumber', lineNo);
}
//!steal-remove-end
attrCallback(this,{
attributeName: attrName,
Expand Down Expand Up @@ -410,7 +426,7 @@ function stache (filename, template) {
state.node.attributes = [];
}
if(!mode) {
state.node.attributes.push(mustacheCore.makeLiveBindingBranchRenderer(null, expression, copyState({ lineNo: lineNo })));
state.node.attributes.push(mustacheCore.makeLiveBindingBranchRenderer(null, expression, copyState({ filename: section.filename, lineNo: lineNo })));
} else if( mode === "#" || mode === "^" ) {
if(!state.node.section) {
state.node.section = new TextSectionBuilder();
Expand Down Expand Up @@ -444,7 +460,9 @@ function stache (filename, template) {
// allow the current renderer to be called with {{>scope.view}}
canReflect.setKeyValue(templateContext, 'view', scopifiedRenderer);
//!steal-remove-start
canReflect.setKeyValue(templateContext, 'filename', section.filename);
if (process.env.NODE_ENV !== 'production') {
canReflect.setKeyValue(templateContext, 'filename', section.filename);
}
//!steal-remove-end

return renderer.apply( this, arguments );
Expand Down Expand Up @@ -486,4 +504,6 @@ stache.registerPartial = function(id, partial) {
templates[id] = (typeof partial === "string" ? stache(partial) : partial);
};

stache.addBindings = addBindings;

module.exports = namespace.stache = stache;
22 changes: 22 additions & 0 deletions docs/addBindings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@function can-stache.addBindings addBindings
@description Add a set of view binding callbacks.
@parent can-stache.static

@signature `stache.addBindings(bindings)`

Register a set of view bindings.

```js
const bindings = new Map();
bindings.add(/foo/, function(el, attrData) {
...
});

stache.addBindings(bindings);
```

This will loop over the set of bindings and register them all with [can-view-callbacks].

@param {Map|Object} bindings A key/value pair where the keys are strings or regular expressions and the values are callback functions.

@body
23 changes: 22 additions & 1 deletion docs/helpers/addHelper.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@signature `stache.addHelper(name, helper)`

Registers a helper with stache that always gets passed
the value of its arguments (instead of computes).
the value of its arguments (instead of value observables).
Pass the name of the helper followed by the
function to invoke.

Expand All @@ -21,4 +21,25 @@ stache.addHelper( "upper", function( str ) {
@param {String} name The name of the helper.
@param {can-stache.simpleHelper} helper The helper function.

@signature `stache.addHelper(helpers)`

Register multiple helpers with stache that always get passed
the value of its arguments (instead of value observables).

Pass an object where the key is the name of a helper and the
value is the callback.

```js
stache.addHelper({
upper: function(str) {
return str.toUpperCase();
},
lower: function(str) {
return str.toLowerCase();
}
});
```

@param {{}} helpers an Object of name/callback pairs.

@body
8 changes: 5 additions & 3 deletions expressions/arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Arg.prototype.value = function(){
return this.expr.value.apply(this.expr, arguments);
};
//!steal-remove-start
Arg.prototype.sourceText = function(){
return (this.modifiers.compute ? "~" : "")+ this.expr.sourceText();
};
if (process.env.NODE_ENV !== 'production') {
Arg.prototype.sourceText = function(){
return (this.modifiers.compute ? "~" : "")+ this.expr.sourceText();
};
}
//!steal-remove-end

module.exports = Arg;
28 changes: 18 additions & 10 deletions expressions/bracket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//!steal-remove-start
var canSymbol = require('can-symbol');
if (process.env.NODE_ENV !== 'production') {
var canSymbol = require('can-symbol');
}
//!steal-remove-end
var expressionHelpers = require("../src/expression-helpers");

Expand All @@ -9,26 +11,32 @@ var Bracket = function (key, root, originalKey) {
this.root = root;
this.key = key;
//!steal-remove-start
this[canSymbol.for("can-stache.originalKey")] = originalKey;
if (process.env.NODE_ENV !== 'production') {
this[canSymbol.for("can-stache.originalKey")] = originalKey;
}
//!steal-remove-end
};
Bracket.prototype.value = function (scope, helpers) {
var root = this.root ? this.root.value(scope, helpers) : scope.peek("this");
return expressionHelpers.getObservableValue_fromDynamicKey_fromObservable(this.key.value(scope, helpers), root, scope, helpers, {});
};
//!steal-remove-start
Bracket.prototype.sourceText = function(){
if(this.rootExpr) {
return this.rootExpr.sourceText()+"["+this.key+"]";
} else {
return "["+this.key+"]";
}
};
if (process.env.NODE_ENV !== 'production') {
Bracket.prototype.sourceText = function(){
if(this.rootExpr) {
return this.rootExpr.sourceText()+"["+this.key+"]";
} else {
return "["+this.key+"]";
}
};
}
//!steal-remove-end

Bracket.prototype.closingTag = function() {
//!steal-remove-start
return this[canSymbol.for('can-stache.originalKey')] || '';
if (process.env.NODE_ENV !== 'production') {
return this[canSymbol.for('can-stache.originalKey')] || '';
}
//!steal-remove-end
};

Expand Down
35 changes: 22 additions & 13 deletions expressions/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ Call.prototype.value = function(scope, helperOptions){

// proxyMethods must be false so that the `requiresOptionsArgument` and any
// other flags stored on the function are preserved
var method = this.methodExpr.value(scope, { proxyMethods: false });
var method = this.methodExpr.value(scope, { proxyMethods: false }),
func = canReflect.getValue( method );

var getArgs = callExpression.args(scope , func && func.ignoreArgLookup);

var computeFn = function(newVal){
var func = canReflect.getValue( method );

if(typeof func === "function") {
var args = callExpression.args(scope, func.ignoreArgLookup)(
var args = getArgs(
func.isLiveBound
);

Expand All @@ -84,9 +87,11 @@ Call.prototype.value = function(scope, helperOptions){
}
};
//!steal-remove-start
Object.defineProperty(computeFn, "name", {
value: "{{" + this.sourceText() + "}}"
});
if (process.env.NODE_ENV !== 'production') {
Object.defineProperty(computeFn, "name", {
value: "{{" + this.sourceText() + "}}"
});
}
//!steal-remove-end

if (helperOptions && helperOptions.doNotWrapInObservation) {
Expand All @@ -98,17 +103,21 @@ Call.prototype.value = function(scope, helperOptions){
}
};
//!steal-remove-start
Call.prototype.sourceText = function(){
var args = this.argExprs.map(function(arg){
return arg.sourceText();
});
return this.methodExpr.sourceText()+"("+args.join(",")+")";
};
if (process.env.NODE_ENV !== 'production') {
Call.prototype.sourceText = function(){
var args = this.argExprs.map(function(arg){
return arg.sourceText();
});
return this.methodExpr.sourceText()+"("+args.join(",")+")";
};
}
//!steal-remove-end
Call.prototype.closingTag = function() {
//!steal-remove-start
if(this.methodExpr[sourceTextSymbol]) {
return this.methodExpr[sourceTextSymbol];
if (process.env.NODE_ENV !== 'production') {
if(this.methodExpr[sourceTextSymbol]) {
return this.methodExpr[sourceTextSymbol];
}
}
//!steal-remove-end
return this.methodExpr.key;
Expand Down
16 changes: 9 additions & 7 deletions expressions/hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ Hashes.prototype.value = function(scope, helperOptions){
});
};
//!steal-remove-start
Hashes.prototype.sourceText = function(){
var hashes = [];
canReflect.eachKey(this.hashExprs, function(expr, prop){
hashes.push( prop+"="+expr.sourceText() );
});
return hashes.join(" ");
};
if (process.env.NODE_ENV !== 'production') {
Hashes.prototype.sourceText = function(){
var hashes = [];
canReflect.eachKey(this.hashExprs, function(expr, prop){
hashes.push( prop+"="+expr.sourceText() );
});
return hashes.join(" ");
};
}
//!steal-remove-end

module.exports = Hashes;
Loading

0 comments on commit 40951e3

Please sign in to comment.