1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-23 23:46:52 +00:00

Transclude widget should use fallback content if variable is missing or blank

Fixes #7424
This commit is contained in:
Jeremy Ruston 2023-05-11 16:26:18 +01:00
parent db6b4f17e8
commit 8aa0db59a3
3 changed files with 133 additions and 90 deletions

View File

@ -51,7 +51,8 @@ MacroCallWidget.prototype.execute = function() {
var positionalName = 0, var positionalName = 0,
parseTreeNodes = [{ parseTreeNodes = [{
type: "transclude", type: "transclude",
isBlock: this.parseTreeNode.isBlock isBlock: this.parseTreeNode.isBlock,
children: this.parseTreeNode.children
}]; }];
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"$variable",this.macroName); $tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"$variable",this.macroName);
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"$type",this.parseType); $tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"$type",this.parseType);

View File

@ -171,99 +171,101 @@ TranscludeWidget.prototype.getTransclusionTarget = function() {
} }
var parser; var parser;
// Get the parse tree // Get the parse tree
if(this.transcludeVariable) { if(this.hasAttribute("$variable")) {
// Transcluding a variable if(this.transcludeVariable) {
var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}), // Transcluding a variable
srcVariable = variableInfo && variableInfo.srcVariable; var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}),
if(srcVariable) { srcVariable = variableInfo && variableInfo.srcVariable;
if(srcVariable.isFunctionDefinition) { if(variableInfo.text) {
// Function to return parameters by name or position if(srcVariable.isFunctionDefinition) {
var fnGetParam = function(name,index) { // Function to return parameters by name or position
// Parameter names starting with dollar must be escaped to double dollars var fnGetParam = function(name,index) {
if(name.charAt(0) === "$") { // Parameter names starting with dollar must be escaped to double dollars
name = "$" + name; if(name.charAt(0) === "$") {
} name = "$" + name;
// Look for the parameter by name }
if(self.hasAttribute(name)) { // Look for the parameter by name
return self.getAttribute(name); if(self.hasAttribute(name)) {
// Look for the parameter by index return self.getAttribute(name);
} else if(self.hasAttribute(index + "")) { // Look for the parameter by index
return self.getAttribute(index + ""); } else if(self.hasAttribute(index + "")) {
} else { return self.getAttribute(index + "");
return undefined; } else {
} return undefined;
}, }
result = this.evaluateVariable(this.transcludeVariable,{params: fnGetParam})[0] || ""; },
parser = { result = this.evaluateVariable(this.transcludeVariable,{params: fnGetParam})[0] || "";
tree: [{ parser = {
type: "text", tree: [{
text: result
}],
source: result,
type: "text/vnd.tiddlywiki"
};
if(parseAsInline) {
parser.tree[0] = {
type: "text",
text: result
};
} else {
parser.tree[0] = {
type: "element",
tag: "p",
children: [{
type: "text", type: "text",
text: result text: result
}] }],
} source: result,
} type: "text/vnd.tiddlywiki"
} else { };
var cacheKey = (parseAsInline ? "inlineParser" : "blockParser") + (this.transcludeType || ""); if(parseAsInline) {
if(variableInfo.isCacheable && srcVariable[cacheKey]) { parser.tree[0] = {
parser = srcVariable[cacheKey]; type: "text",
} else { text: result
parser = this.wiki.parseText(this.transcludeType,variableInfo.text || "",{parseAsInline: parseAsInline, configTrimWhiteSpace: srcVariable.configTrimWhiteSpace}); };
if(variableInfo.isCacheable) { } else {
srcVariable[cacheKey] = parser; parser.tree[0] = {
} type: "element",
} tag: "p",
} children: [{
if(parser) { type: "text",
// Add parameters widget for procedures and custom widgets text: result
if(srcVariable.isProcedureDefinition || srcVariable.isWidgetDefinition) { }]
parser = {
tree: [
{
type: "parameters",
children: parser.tree
}
],
source: parser.source,
type: parser.type
}
$tw.utils.each(srcVariable.params,function(param) {
var name = param.name;
// Parameter names starting with dollar must be escaped to double dollars
if(name.charAt(0) === "$") {
name = "$" + name;
} }
$tw.utils.addAttributeToParseTreeNode(parser.tree[0],name,param["default"])
});
} else {
// For macros and ordinary variables, wrap the parse tree in a vars widget assigning the parameters to variables named "__paramname__"
parser = {
tree: [
{
type: "vars",
children: parser.tree
}
],
source: parser.source,
type: parser.type
} }
$tw.utils.each(variableInfo.params,function(param) { } else {
$tw.utils.addAttributeToParseTreeNode(parser.tree[0],"__" + param.name + "__",param.value) var cacheKey = (parseAsInline ? "inlineParser" : "blockParser") + (this.transcludeType || "");
}); if(variableInfo.isCacheable && srcVariable[cacheKey]) {
parser = srcVariable[cacheKey];
} else {
parser = this.wiki.parseText(this.transcludeType,variableInfo.text || "",{parseAsInline: parseAsInline, configTrimWhiteSpace: srcVariable.configTrimWhiteSpace});
if(variableInfo.isCacheable) {
srcVariable[cacheKey] = parser;
}
}
}
if(parser) {
// Add parameters widget for procedures and custom widgets
if(srcVariable.isProcedureDefinition || srcVariable.isWidgetDefinition) {
parser = {
tree: [
{
type: "parameters",
children: parser.tree
}
],
source: parser.source,
type: parser.type
}
$tw.utils.each(srcVariable.params,function(param) {
var name = param.name;
// Parameter names starting with dollar must be escaped to double dollars
if(name.charAt(0) === "$") {
name = "$" + name;
}
$tw.utils.addAttributeToParseTreeNode(parser.tree[0],name,param["default"])
});
} else {
// For macros and ordinary variables, wrap the parse tree in a vars widget assigning the parameters to variables named "__paramname__"
parser = {
tree: [
{
type: "vars",
children: parser.tree
}
],
source: parser.source,
type: parser.type
}
$tw.utils.each(variableInfo.params,function(param) {
$tw.utils.addAttributeToParseTreeNode(parser.tree[0],"__" + param.name + "__",param.value)
});
}
} }
} }
} }

View File

@ -0,0 +1,40 @@
title: Transclude/Macro/Missing
description: Transcluding a missing or blank variable
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
<$macrocall $name="missingmacro">
Fallback content
</$macrocall>
<$transclude $variable="missingmacro">
Fallback content
</$transclude>
<$macrocall $name="">
Fallback content
</$macrocall>
<$transclude $variable="">
Fallback content
</$transclude>
<$let emptyVariable="">
<$macrocall $name="emptyVariable">
Fallback content
</$macrocall>
<$transclude $variable="emptyVariable">
Fallback content
</$transclude>
</$let>
+
title: ExpectedResult
<p>Fallback content</p><p>Fallback content</p><p>Fallback content</p><p>Fallback content</p><p>Fallback content</p><p>Fallback content</p>