1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-07-05 03:22:51 +00:00

Fix positional parameters in widget.evaluateVariable()

This should clear up the remaining anomalies in #7009, let me know how you get on @btheado
This commit is contained in:
jeremy@jermolene.com 2023-01-23 16:34:08 +00:00
parent 34afe4e143
commit 683414f51e
2 changed files with 38 additions and 13 deletions

View File

@ -172,8 +172,8 @@ Widget.prototype.getVariable = function(name,options) {
/* /*
Maps actual parameters onto formal parameters, returning an array of {name:,value:} objects Maps actual parameters onto formal parameters, returning an array of {name:,value:} objects
formalParams - {name:,default:} (default value is optional) formalParams - Array of {name:,default:} (default value is optional)
actualParams - {name:,value:} (name is optional) actualParams - Array of string values or {name:,value:} (name is optional)
*/ */
Widget.prototype.resolveVariableParameters = function(formalParams,actualParams) { Widget.prototype.resolveVariableParameters = function(formalParams,actualParams) {
formalParams = formalParams || []; formalParams = formalParams || [];
@ -187,7 +187,7 @@ Widget.prototype.resolveVariableParameters = function(formalParams,actualParams)
paramInfo = formalParams[p]; paramInfo = formalParams[p];
paramValue = undefined; paramValue = undefined;
for(var m=0; m<actualParams.length; m++) { for(var m=0; m<actualParams.length; m++) {
if(actualParams[m].name === paramInfo.name) { if(typeof actualParams[m] !== "string" && actualParams[m].name === paramInfo.name) {
paramValue = actualParams[m].value; paramValue = actualParams[m].value;
} }
} }
@ -196,7 +196,8 @@ Widget.prototype.resolveVariableParameters = function(formalParams,actualParams)
nextAnonParameter++; nextAnonParameter++;
} }
if(paramValue === undefined && nextAnonParameter < actualParams.length) { if(paramValue === undefined && nextAnonParameter < actualParams.length) {
paramValue = actualParams[nextAnonParameter++].value; var param = actualParams[nextAnonParameter++];
paramValue = typeof param === "string" ? param : param.value;
} }
// If we've still not got a value, use the default, if any // If we've still not got a value, use the default, if any
paramValue = paramValue || paramInfo["default"] || ""; paramValue = paramValue || paramInfo["default"] || "";
@ -317,6 +318,7 @@ Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
}, },
makeFakeWidgetWithVariables: self.makeFakeWidgetWithVariables, makeFakeWidgetWithVariables: self.makeFakeWidgetWithVariables,
evaluateVariable: self.evaluateVariable, evaluateVariable: self.evaluateVariable,
resolveVariableParameters: self.resolveVariableParameters,
wiki: self.wiki wiki: self.wiki
}; };
}; };
@ -351,15 +353,8 @@ Widget.prototype.evaluateVariable = function(name,options) {
}); });
if($tw.utils.isArray(params)) { if($tw.utils.isArray(params)) {
// Parameters are an array of values or {name:, value:} pairs // Parameters are an array of values or {name:, value:} pairs
$tw.utils.each(params,function(param,index) { $tw.utils.each(this.resolveVariableParameters(variableInfo.srcVariable.params,params),function(param) {
if(typeof param === "string") {
var paramInfo = variableInfo.srcVariable.params[index];
if(paramInfo) {
variables[paramInfo.name] = param;
}
} else {
variables[param.name] = param.value; variables[param.name] = param.value;
}
}); });
} else if(typeof params === "function") { } else if(typeof params === "function") {
// Parameters are passed via a function // Parameters are passed via a function

View File

@ -0,0 +1,30 @@
title: Transclude/Parameterised/Positional/Variables
description: Positional parameterised transclusion of variables
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
\function myfunction(alpha:"apple",beta:"banana",gamma:"grenadine") [<alpha>]
\define mymacro(alpha:"apple",beta:"banana",gamma:"grenadine") $beta$
\function f(a) [<a>]
(Functions:
<$text text={{{ [<myfunction gamma:"unused" f1>] }}}/>
,
<$text text=<<myfunction gamma:"unused" f1>>/>
,
<<myfunction gamma:"unused" f1>>
)(Macros:
<$text text={{{ [<mymacro gamma:"unused" f1>] }}}/>
,
<$text text=<<mymacro gamma:"unused" f1>>/>
,
<<mymacro gamma:"unused" f1>>
)
+
title: ExpectedResult
<p>(Functions:f1,f1,f1)(Macros:banana,banana,banana)</p>