Fixes reduce, filter, and sortsub operators undefined variable exception (#7156)

* Added failing tests for #7155

* Pass getVariable options through to the widget method. Fixes #7155

* Whitespace fix

* Added tests to verify macro parameters work inside filter, reduce, and sortsub operators
This commit is contained in:
btheado 2023-01-03 07:48:20 -06:00 committed by GitHub
parent fb8df29948
commit 6f9cf20e77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 6 deletions

View File

@ -21,14 +21,15 @@ exports.filter = function(source,operator,options) {
target = operator.prefix !== "!";
source(function(tiddler,title) {
var list = filterFn.call(options.wiki,options.wiki.makeTiddlerIterator([title]),{
getVariable: function(name) {
getVariable: function(name,opts) {
opts = opts || {};
switch(name) {
case "currentTiddler":
return "" + title;
case "..currentTiddler":
return options.widget.getVariable("currentTiddler");
default:
return options.widget.getVariable(name);
return options.widget.getVariable(name,opts);
}
}
});

View File

@ -27,7 +27,8 @@ exports.reduce = function(source,operator,options) {
for(var index=0; index<results.length; index++) {
var title = results[index],
list = filterFn.call(options.wiki,options.wiki.makeTiddlerIterator([title]),{
getVariable: function(name) {
getVariable: function(name,opts) {
opts = opts || {};
switch(name) {
case "currentTiddler":
return "" + title;
@ -42,7 +43,7 @@ exports.reduce = function(source,operator,options) {
case "length":
return "" + results.length;
default:
return options.widget.getVariable(name);
return options.widget.getVariable(name,opts);
}
}
});

View File

@ -26,14 +26,15 @@ exports.sortsub = function(source,operator,options) {
var r = filterFn.call(options.wiki,function(iterator) {
iterator(options.wiki.getTiddler(title),title);
},{
getVariable: function(name) {
getVariable: function(name,opts) {
opts = opts || {};
switch(name) {
case "currentTiddler":
return "" + title;
case "..currentTiddler":
return options.widget.getVariable("currentTiddler");
default:
return options.widget.getVariable(name);
return options.widget.getVariable(name,opts);
}
}
});

View File

@ -983,6 +983,9 @@ Tests the filtering mechanism.
rootWidget.setVariable("sort2","[get[text]else[]length[]]");
rootWidget.setVariable("sort3","[{!!value}divide{!!cost}]");
rootWidget.setVariable("sort4","[{!!title}]");
rootWidget.setVariable("undefined-variable","[<doesnotexist>]");
rootWidget.setVariable("echo","$text$",[{name:"text"}],true);
rootWidget.setVariable("sort4-macro-param","[subfilter<echo '[{!!title}]'>]");
expect(wiki.filterTiddlers("[sortsub:number<sort1>]",anchorWidget).join(",")).toBe("one,hasList,has filter,TiddlerOne,$:/TiddlerTwo,Tiddler Three,$:/ShadowPlugin,a fourth tiddler,filter regexp test");
expect(wiki.filterTiddlers("[!sortsub:number<sort1>]",anchorWidget).join(",")).toBe("filter regexp test,a fourth tiddler,$:/ShadowPlugin,$:/TiddlerTwo,Tiddler Three,has filter,TiddlerOne,hasList,one");
expect(wiki.filterTiddlers("[sortsub:string<sort1>]",anchorWidget).join(",")).toBe("has filter,TiddlerOne,$:/TiddlerTwo,Tiddler Three,$:/ShadowPlugin,a fourth tiddler,filter regexp test,one,hasList");
@ -993,6 +996,9 @@ Tests the filtering mechanism.
expect(wiki.filterTiddlers("[!sortsub:string<sort2>]",anchorWidget).join(",")).toBe("filter regexp test,$:/TiddlerTwo,Tiddler Three,a fourth tiddler,$:/ShadowPlugin,has filter,hasList,TiddlerOne,one");
expect(wiki.filterTiddlers("[[TiddlerOne]] [[$:/TiddlerTwo]] [[Tiddler Three]] [[a fourth tiddler]] +[!sortsub:number<sort3>]",anchorWidget).join(",")).toBe("$:/TiddlerTwo,Tiddler Three,TiddlerOne,a fourth tiddler");
expect(wiki.filterTiddlers("a1 a10 a2 a3 b10 b3 b1 c9 c11 c1 +[sortsub:alphanumeric<sort4>]",anchorWidget).join(",")).toBe("a1,a2,a3,a10,b1,b3,b10,c1,c9,c11");
// #7155. The order of the output is the same as the input when an undefined variable is used in the subfitler
expect(wiki.filterTiddlers("a2 a10 a1 +[sortsub:alphanumeric<undefined-variable>]",anchorWidget).join(",")).toBe("a2,a10,a1");
expect(wiki.filterTiddlers("a1 a10 a2 a3 b10 b3 b1 c9 c11 c1 +[sortsub:alphanumeric<sort4-macro-param>]",anchorWidget).join(",")).toBe("a1,a2,a3,a10,b1,b3,b10,c1,c9,c11");
});
it("should handle the toggle operator", function() {

View File

@ -343,6 +343,9 @@ describe("'reduce' and 'intersection' filter prefix tests", function() {
rootWidget.setVariable("add-price","[get[price]multiply{!!quantity}add<accumulator>]");
rootWidget.setVariable("num-items","[get[quantity]add<accumulator>]");
rootWidget.setVariable("join-with-commas","[<index>compare:number:gt[0]then<accumulator>addsuffix[, ]addsuffix<currentTiddler>else<currentTiddler>]");
rootWidget.setVariable("undefined-variable","[<doesnotexist>]");
rootWidget.setVariable("echo","$text$",[{name:"text"}],true);
rootWidget.setVariable("num-items-macro-param","[subfilter<echo '[get[quantity]]'>add<accumulator>]");
expect(wiki.filterTiddlers("[tag[shopping]reduce<num-items>]",anchorWidget).join(",")).toBe("22");
expect(wiki.filterTiddlers("[tag[shopping]reduce<add-price>]",anchorWidget).join(",")).toBe("27.75");
@ -351,6 +354,9 @@ describe("'reduce' and 'intersection' filter prefix tests", function() {
expect(wiki.filterTiddlers("[tag[non-existent]reduce<add-price>,[0]]",anchorWidget).join(",")).not.toBe("0");
expect(wiki.filterTiddlers("[tag[non-existent]reduce<add-price>,[0]]",anchorWidget).length).toBe(0);
expect(wiki.filterTiddlers("[tag[non-existent]reduce<add-price>else[0]]",anchorWidget).join(",")).toBe("0");
// #7155
expect(wiki.filterTiddlers("a +[reduce<undefined-variable>]",anchorWidget).join(",")).toBe("");
expect(wiki.filterTiddlers("[tag[shopping]reduce<num-items-macro-param>]",anchorWidget).join(",")).toBe("22");
});
it("should handle the average operator", function() {
@ -392,10 +398,16 @@ describe("'reduce' and 'intersection' filter prefix tests", function() {
rootWidget.setVariable("larger-than-18","[get[text]length[]compare:integer:gteq[18]]");
rootWidget.setVariable("nr","18");
rootWidget.setVariable("larger-than-18-with-var","[get[text]length[]compare:integer:gteq<nr>]");
rootWidget.setVariable("undefined-variable","[<doesnotexist>]");
rootWidget.setVariable("echo","$text$",[{name:"text"}],true);
rootWidget.setVariable("larger-than-18-macro-param","[subfilter<echo '[get[text]length[]compare:integer:gteq[18]]'>]");
expect(wiki.filterTiddlers("[tag[textexample]] :filter[get[text]length[]compare:integer:gteq[18]]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine");
expect(wiki.filterTiddlers("[tag[textexample]]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine,Sparkling water");
expect(wiki.filterTiddlers("[tag[textexample]filter<larger-than-18>]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine");
expect(wiki.filterTiddlers("[tag[textexample]filter<larger-than-18-with-var>]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine");
// #7155
expect(wiki.filterTiddlers("a +[filter<undefined-variable>]",anchorWidget).join(",")).toBe("a");
expect(wiki.filterTiddlers("[tag[textexample]filter<larger-than-18-macro-param>]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine");
});
it("should handle the :sort prefix", function() {