diff --git a/core/modules/filters/filter.js b/core/modules/filters/filter.js index 8284929a7..9b69fd83a 100644 --- a/core/modules/filters/filter.js +++ b/core/modules/filters/filter.js @@ -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); } } }); diff --git a/core/modules/filters/reduce.js b/core/modules/filters/reduce.js index 831b354a1..50c501f08 100644 --- a/core/modules/filters/reduce.js +++ b/core/modules/filters/reduce.js @@ -27,7 +27,8 @@ exports.reduce = function(source,operator,options) { for(var index=0; index]"); + rootWidget.setVariable("echo","$text$",[{name:"text"}],true); + rootWidget.setVariable("sort4-macro-param","[subfilter]"); expect(wiki.filterTiddlers("[sortsub:number]",anchorWidget).join(",")).toBe("one,hasList,has filter,TiddlerOne,$:/TiddlerTwo,Tiddler Three,$:/ShadowPlugin,a fourth tiddler,filter regexp test"); expect(wiki.filterTiddlers("[!sortsub:number]",anchorWidget).join(",")).toBe("filter regexp test,a fourth tiddler,$:/ShadowPlugin,$:/TiddlerTwo,Tiddler Three,has filter,TiddlerOne,hasList,one"); expect(wiki.filterTiddlers("[sortsub:string]",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]",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]",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]",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]",anchorWidget).join(",")).toBe("a2,a10,a1"); + expect(wiki.filterTiddlers("a1 a10 a2 a3 b10 b3 b1 c9 c11 c1 +[sortsub:alphanumeric]",anchorWidget).join(",")).toBe("a1,a2,a3,a10,b1,b3,b10,c1,c9,c11"); }); it("should handle the toggle operator", function() { diff --git a/editions/test/tiddlers/tests/test-prefixes-filter.js b/editions/test/tiddlers/tests/test-prefixes-filter.js index e32827f9f..62f329d66 100644 --- a/editions/test/tiddlers/tests/test-prefixes-filter.js +++ b/editions/test/tiddlers/tests/test-prefixes-filter.js @@ -343,6 +343,9 @@ describe("'reduce' and 'intersection' filter prefix tests", function() { rootWidget.setVariable("add-price","[get[price]multiply{!!quantity}add]"); rootWidget.setVariable("num-items","[get[quantity]add]"); rootWidget.setVariable("join-with-commas","[compare:number:gt[0]thenaddsuffix[, ]addsuffixelse]"); + rootWidget.setVariable("undefined-variable","[]"); + rootWidget.setVariable("echo","$text$",[{name:"text"}],true); + rootWidget.setVariable("num-items-macro-param","[subfilteradd]"); expect(wiki.filterTiddlers("[tag[shopping]reduce]",anchorWidget).join(",")).toBe("22"); expect(wiki.filterTiddlers("[tag[shopping]reduce]",anchorWidget).join(",")).toBe("27.75"); @@ -351,6 +354,9 @@ describe("'reduce' and 'intersection' filter prefix tests", function() { expect(wiki.filterTiddlers("[tag[non-existent]reduce,[0]]",anchorWidget).join(",")).not.toBe("0"); expect(wiki.filterTiddlers("[tag[non-existent]reduce,[0]]",anchorWidget).length).toBe(0); expect(wiki.filterTiddlers("[tag[non-existent]reduceelse[0]]",anchorWidget).join(",")).toBe("0"); + // #7155 + expect(wiki.filterTiddlers("a +[reduce]",anchorWidget).join(",")).toBe(""); + expect(wiki.filterTiddlers("[tag[shopping]reduce]",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]"); + rootWidget.setVariable("undefined-variable","[]"); + rootWidget.setVariable("echo","$text$",[{name:"text"}],true); + rootWidget.setVariable("larger-than-18-macro-param","[subfilter]"); 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]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine"); expect(wiki.filterTiddlers("[tag[textexample]filter]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine"); + // #7155 + expect(wiki.filterTiddlers("a +[filter]",anchorWidget).join(",")).toBe("a"); + expect(wiki.filterTiddlers("[tag[textexample]filter]",anchorWidget).join(",")).toBe("Cheesecake,Chocolate Cake,Red wine"); }); it("should handle the :sort prefix", function() {