diff --git a/core/modules/widgets/macrocall.js b/core/modules/widgets/macrocall.js index e49eadfe0e..2c4adc3553 100644 --- a/core/modules/widgets/macrocall.js +++ b/core/modules/widgets/macrocall.js @@ -51,7 +51,8 @@ MacroCallWidget.prototype.execute = function() { var positionalName = 0, parseTreeNodes = [{ 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],"$type",this.parseType); diff --git a/core/modules/widgets/transclude.js b/core/modules/widgets/transclude.js index dabd6d832c..90b640c421 100755 --- a/core/modules/widgets/transclude.js +++ b/core/modules/widgets/transclude.js @@ -171,99 +171,101 @@ TranscludeWidget.prototype.getTransclusionTarget = function() { } var parser; // Get the parse tree - if(this.transcludeVariable) { - // Transcluding a variable - var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}), - srcVariable = variableInfo && variableInfo.srcVariable; - if(srcVariable) { - if(srcVariable.isFunctionDefinition) { - // Function to return parameters by name or position - var fnGetParam = function(name,index) { - // Parameter names starting with dollar must be escaped to double dollars - if(name.charAt(0) === "$") { - name = "$" + name; - } - // Look for the parameter by name - if(self.hasAttribute(name)) { - return self.getAttribute(name); - // Look for the parameter by index - } else if(self.hasAttribute(index + "")) { - return self.getAttribute(index + ""); - } else { - return undefined; - } - }, - result = this.evaluateVariable(this.transcludeVariable,{params: fnGetParam})[0] || ""; - parser = { - tree: [{ - type: "text", - 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: [{ + if(this.hasAttribute("$variable")) { + if(this.transcludeVariable) { + // Transcluding a variable + var variableInfo = this.getVariableInfo(this.transcludeVariable,{params: this.getOrderedTransclusionParameters()}), + srcVariable = variableInfo && variableInfo.srcVariable; + if(variableInfo.text) { + if(srcVariable.isFunctionDefinition) { + // Function to return parameters by name or position + var fnGetParam = function(name,index) { + // Parameter names starting with dollar must be escaped to double dollars + if(name.charAt(0) === "$") { + name = "$" + name; + } + // Look for the parameter by name + if(self.hasAttribute(name)) { + return self.getAttribute(name); + // Look for the parameter by index + } else if(self.hasAttribute(index + "")) { + return self.getAttribute(index + ""); + } else { + return undefined; + } + }, + result = this.evaluateVariable(this.transcludeVariable,{params: fnGetParam})[0] || ""; + parser = { + tree: [{ type: "text", text: result - }] - } - } - } else { - 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; + }], + 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", + text: result + }] } - $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) - }); + } else { + 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) + }); + } } } } diff --git a/editions/test/tiddlers/tests/data/transclude/Macro-Missing.tid b/editions/test/tiddlers/tests/data/transclude/Macro-Missing.tid new file mode 100644 index 0000000000..8a1a837e56 --- /dev/null +++ b/editions/test/tiddlers/tests/data/transclude/Macro-Missing.tid @@ -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 + +
Fallback content
Fallback content
Fallback content
Fallback content
Fallback content
Fallback content
\ No newline at end of file