From fd0b985ac566317375ce33f4344dfe2f68793a91 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 16 Apr 2022 18:02:27 +0100 Subject: [PATCH] action-setfield shouldn't write to the current tiddler if the $tiddler attribute is present but has evaluated to a missing attribute Fixes #5916 --- core/modules/widgets/action-setfield.js | 26 ++++++----- core/modules/widgets/widget.js | 9 +++- .../tiddlers/tests/test-action-widgets.js | 43 +++++++++++++++++++ 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/core/modules/widgets/action-setfield.js b/core/modules/widgets/action-setfield.js index 98079f00c..f853484cd 100644 --- a/core/modules/widgets/action-setfield.js +++ b/core/modules/widgets/action-setfield.js @@ -35,7 +35,7 @@ SetFieldWidget.prototype.render = function(parent,nextSibling) { Compute the internal state of the widget */ SetFieldWidget.prototype.execute = function() { - this.actionTiddler = this.getAttribute("$tiddler",this.getVariable("currentTiddler")); + this.actionTiddler = this.getAttribute("$tiddler") || (!this.hasParseTreeNodeAttribute("$tiddler") && this.getVariable("currentTiddler")); this.actionField = this.getAttribute("$field"); this.actionIndex = this.getAttribute("$index"); this.actionValue = this.getAttribute("$value"); @@ -46,11 +46,7 @@ SetFieldWidget.prototype.execute = function() { Refresh the widget by ensuring our attributes are up to date */ SetFieldWidget.prototype.refresh = function(changedTiddlers) { - var changedAttributes = this.computeAttributes(); - if(changedAttributes["$tiddler"] || changedAttributes["$field"] || changedAttributes["$index"] || changedAttributes["$value"]) { - this.refreshSelf(); - return true; - } + // Nothing to refresh return this.refreshChildren(changedTiddlers); }; @@ -60,15 +56,17 @@ Invoke the action associated with this widget SetFieldWidget.prototype.invokeAction = function(triggeringWidget,event) { var self = this, options = {}; - options.suppressTimestamp = !this.actionTimestamp; - if((typeof this.actionField == "string") || (typeof this.actionIndex == "string") || (typeof this.actionValue == "string")) { - this.wiki.setText(this.actionTiddler,this.actionField,this.actionIndex,this.actionValue,options); - } - $tw.utils.each(this.attributes,function(attribute,name) { - if(name.charAt(0) !== "$") { - self.wiki.setText(self.actionTiddler,name,undefined,attribute,options); + if(this.actionTiddler) { + options.suppressTimestamp = !this.actionTimestamp; + if((typeof this.actionField == "string") || (typeof this.actionIndex == "string") || (typeof this.actionValue == "string")) { + this.wiki.setText(this.actionTiddler,this.actionField,this.actionIndex,this.actionValue,options); } - }); + $tw.utils.each(this.attributes,function(attribute,name) { + if(name.charAt(0) !== "$") { + self.wiki.setText(self.actionTiddler,name,undefined,attribute,options); + } + }); + } return true; // Action was invoked }; diff --git a/core/modules/widgets/widget.js b/core/modules/widgets/widget.js index c6b49f04a..0aefbada8 100755 --- a/core/modules/widgets/widget.js +++ b/core/modules/widgets/widget.js @@ -289,12 +289,19 @@ Widget.prototype.computeAttribute = function(attribute) { }; /* -Check for the presence of an attribute +Check for the presence of an evaluated attribute on the widget. Note that attributes set to a missing variable (ie attr=<>) will be treated as missing */ Widget.prototype.hasAttribute = function(name) { return $tw.utils.hop(this.attributes,name); }; +/* +Check for the presence of a raw attribute on the widget parse tree node. Note that attributes set to a missing variable (ie attr=<>) will NOT be treated as missing +*/ +Widget.prototype.hasParseTreeNodeAttribute = function(name) { + return $tw.utils.hop(this.parseTreeNode.attributes,name); +}; + /* Get the value of an attribute */ diff --git a/editions/test/tiddlers/tests/test-action-widgets.js b/editions/test/tiddlers/tests/test-action-widgets.js index 12311521b..9d706e1a3 100644 --- a/editions/test/tiddlers/tests/test-action-widgets.js +++ b/editions/test/tiddlers/tests/test-action-widgets.js @@ -36,6 +36,49 @@ function setupWiki(wikiOptions) { }; } +it("should handle the action-setfield widget", function() { + var info = setupWiki(); + var invokeActions = function(actions) { + info.widgetNode.invokeActionString(actions,info.widgetNode,null,{}); + }; + var resetTiddlers = function() { + info.wiki.addTiddlers([ + { + title: "Output", + text: "Elephants!" + },{ + title: "Root", + text: "Eagles!" + } + ]); + }; + // Start with a reset + resetTiddlers(); + // Check it + expect(info.wiki.getTiddlerText("Output")).toBe("Elephants!"); + expect(info.wiki.getTiddlerText("Root")).toBe("Eagles!"); + // Missing $tiddler attribute + resetTiddlers(); + invokeActions("<$tiddler tiddler='Root'><$action-setfield $field='text' $value='Hippos!'/>"); + expect(info.wiki.getTiddlerText("Output")).toBe("Elephants!"); + expect(info.wiki.getTiddlerText("Root")).toBe("Hippos!"); + // Blank $tiddler attribute + resetTiddlers(); + invokeActions("<$tiddler tiddler='Root'><$action-setfield $tiddler='' $field='text' $value='Koalas!'/>"); + expect(info.wiki.getTiddlerText("Output")).toBe("Elephants!"); + expect(info.wiki.getTiddlerText("Root")).toBe("Eagles!"); + // Empty $tiddler attribute + resetTiddlers(); + invokeActions("<$tiddler tiddler='Root'><$action-setfield $tiddler={{{}}} $field='text' $value='Sharks!'/>"); + expect(info.wiki.getTiddlerText("Output")).toBe("Elephants!"); + expect(info.wiki.getTiddlerText("Root")).toBe("Eagles!"); + // Missing variable attribute + resetTiddlers(); + invokeActions("<$tiddler tiddler='Root'><$action-setfield $tiddler=<> $field='text' $value='Tigers!'/>"); + expect(info.wiki.getTiddlerText("Output")).toBe("Elephants!"); + expect(info.wiki.getTiddlerText("Root")).toBe("Eagles!"); +}); + it("should handle the action-listops widget", function() { var info = setupWiki(); var invokeActions = function(actions) {