Improved fix for #38 - Better merging of tags

This commit is contained in:
Patrick Steele-Idem 2015-02-25 11:32:01 -07:00
parent 505f58e136
commit 05c2938e3b

View File

@ -19,6 +19,14 @@ var forEachEntry = require('raptor-util').forEachEntry;
var ok = require('assert').ok; var ok = require('assert').ok;
var makeClass = require('raptor-util').makeClass; var makeClass = require('raptor-util').makeClass;
function inheritProps(sub, sup) {
forEachEntry(sup, function (k, v) {
if (!sub[k]) {
sub[k] = v;
}
});
}
function Taglib(id) { function Taglib(id) {
ok(id, '"id" expected'); ok(id, '"id" expected');
this.id = id; this.id = id;
@ -85,8 +93,8 @@ Taglib.Tag = makeClass({
this.template = null; this.template = null;
this.attributes = {}; this.attributes = {};
this.transformers = {}; this.transformers = {};
this.nestedVariables = {}; this.nestedVariables = null;
this.importedVariables = {}; this.importedVariables = null;
this.patternAttributes = []; this.patternAttributes = [];
this.bodyFunction = null; this.bodyFunction = null;
}, },
@ -100,29 +108,29 @@ Taglib.Tag = makeClass({
subTag[k] = v; subTag[k] = v;
} }
}); });
function inheritProps(sub, sup) {
forEachEntry(sup, function (k, v) {
if (!sub[k]) {
sub[k] = v;
}
});
}
[ [
'attributes', 'attributes',
'transformers', 'transformers',
'nestedVariables', 'nestedVariables',
'importedVariables' 'importedVariables',
'bodyFunction'
].forEach(function (propName) { ].forEach(function (propName) {
inheritProps(subTag[propName], superTag[propName]); inheritProps(subTag[propName], superTag[propName]);
}); });
subTag.patternAttributes = superTag.patternAttributes.concat(subTag.patternAttributes); subTag.patternAttributes = superTag.patternAttributes.concat(subTag.patternAttributes);
}, },
forEachVariable: function (callback, thisObj) { forEachVariable: function (callback, thisObj) {
forEachEntry(this.nestedVariables, function (key, variable) { if (!this.nestedVariables) {
callback.call(thisObj, variable); return;
}); }
this.nestedVariables.vars.forEach(callback, thisObj);
}, },
forEachImportedVariable: function (callback, thisObj) { forEachImportedVariable: function (callback, thisObj) {
if (!this.importedVariables) {
return;
}
forEachEntry(this.importedVariables, function (key, importedVariable) { forEachEntry(this.importedVariables, function (key, importedVariable) {
callback.call(thisObj, importedVariable); callback.call(thisObj, importedVariable);
}); });
@ -172,10 +180,19 @@ Taglib.Tag = makeClass({
} }
}, },
addNestedVariable: function (nestedVariable) { addNestedVariable: function (nestedVariable) {
var key = nestedVariable.nameFromAttribute ? 'attr:' + nestedVariable.nameFromAttribute : nestedVariable.name; if (!this.nestedVariables) {
this.nestedVariables[key] = nestedVariable; this.nestedVariables = {
__noMerge: true,
vars: []
};
}
this.nestedVariables.vars.push(nestedVariable);
}, },
addImportedVariable: function (importedVariable) { addImportedVariable: function (importedVariable) {
if (!this.importedVariables) {
this.importedVariables = {};
}
var key = importedVariable.targetProperty; var key = importedVariable.targetProperty;
this.importedVariables[key] = importedVariable; this.importedVariables[key] = importedVariable;
}, },
@ -186,7 +203,6 @@ Taglib.Tag = makeClass({
}, },
setBodyFunction: function(name, params) { setBodyFunction: function(name, params) {
this.bodyFunction = { this.bodyFunction = {
__noMerge: true,
name: name, name: name,
params: params params: params
}; };