From 9e8d05f69905cbd3b79c19395f37542def4103b3 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Mon, 23 May 2022 16:40:21 +0100 Subject: [PATCH] Require period prefix for custom filter operator functions To ensure that custom filter operators cannot clash with future core operators. --- core/modules/filters/unknown.js | 76 +++++++++---------- .../custom-operators/NestedParameterised.tid | 10 +-- .../data/custom-operators/Parameterised.tid | 6 +- .../tests/data/custom-operators/Simple.tid | 4 +- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/core/modules/filters/unknown.js b/core/modules/filters/unknown.js index 2eca09a7c..81f868427 100644 --- a/core/modules/filters/unknown.js +++ b/core/modules/filters/unknown.js @@ -20,48 +20,48 @@ var fieldFilterOperatorFn = require("$:/core/modules/filters/field.js").field; Export our filter function */ exports["[unknown]"] = function(source,operator,options) { - var customDefinitionTitle = "[" + operator.operator + "[]]", - customDefinition = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(customDefinitionTitle); - if(customDefinition && customDefinition.srcVariable && customDefinition.srcVariable.isFunctionDefinition) { - var variables = Object.create(null); - $tw.utils.each(customDefinition.srcVariable.params,function(param,index) { - var value = operator.operands[index]; - if(value === undefined) { - value = param["default"] || ""; - } - variables[param.name] = value; - }); - var getVariable = function(name,opts) { - if(name in variables) { - return variables[name]; - } else { - return options.widget.getVariable(name,opts); - }; - }; - var getVariableInfo = function(name,opts) { - if(name in variables) { - return { - text: variables[name] - }; - } else { - return options.widget.getVariableInfo(name,opts); - }; - } - var list = options.wiki.filterTiddlers(customDefinition.srcVariable.value,{getVariable: getVariable,getVariableInfo: getVariableInfo},source); - if(operator.prefix === "!") { - var results = []; - source(function(tiddler,title) { - if(list.indexOf(title) === -1) { - results.push(title); + if(operator.operator.charAt(0) === ".") { + var customDefinition = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(operator.operator); + if(customDefinition && customDefinition.srcVariable && customDefinition.srcVariable.isFunctionDefinition) { + var variables = Object.create(null); + $tw.utils.each(customDefinition.srcVariable.params,function(param,index) { + var value = operator.operands[index]; + if(value === undefined) { + value = param["default"] || ""; } + variables[param.name] = value; }); - return results; - } else { - return list; + var getVariable = function(name,opts) { + if(name in variables) { + return variables[name]; + } else { + return options.widget.getVariable(name,opts); + }; + }; + var getVariableInfo = function(name,opts) { + if(name in variables) { + return { + text: variables[name] + }; + } else { + return options.widget.getVariableInfo(name,opts); + }; + } + var list = options.wiki.filterTiddlers(customDefinition.srcVariable.value,{getVariable: getVariable,getVariableInfo: getVariableInfo},source); + if(operator.prefix === "!") { + var results = []; + source(function(tiddler,title) { + if(list.indexOf(title) === -1) { + results.push(title); + } + }); + return results; + } else { + return list; + } } - } else { - return fieldFilterOperatorFn(source,operator,options); } + return fieldFilterOperatorFn(source,operator,options); }; })(); diff --git a/editions/test/tiddlers/tests/data/custom-operators/NestedParameterised.tid b/editions/test/tiddlers/tests/data/custom-operators/NestedParameterised.tid index cbeb1570d..3e4d610d0 100644 --- a/editions/test/tiddlers/tests/data/custom-operators/NestedParameterised.tid +++ b/editions/test/tiddlers/tests/data/custom-operators/NestedParameterised.tid @@ -6,17 +6,17 @@ tags: [[$:/tags/wiki-test-spec]] title: Output \whitespace trim -\function [dividebysomething[]](first:ignored,factor:0.5) +\function .dividebysomething(first:ignored,factor:0.5) [divide[2]multiply] \end -\function [multiplebysomething[]](first:ignored,factor:2) -[multiply[2]dividebysomething[],] +\function .multiplebysomething(first:ignored,factor:2) +[multiply[2].dividebysomething[],] \end -<$text text={{{ [[123]multiplebysomething[]] }}}/> +<$text text={{{ [[123].multiplebysomething[]] }}}/> - -<$text text={{{ [[123]multiplebysomething[x],[4]] }}}/> +<$text text={{{ [[123].multiplebysomething[x],[4]] }}}/> + title: ExpectedResult diff --git a/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid b/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid index 42ca40ec6..6901710b7 100644 --- a/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid +++ b/editions/test/tiddlers/tests/data/custom-operators/Parameterised.tid @@ -6,13 +6,13 @@ tags: [[$:/tags/wiki-test-spec]] title: Output \whitespace trim -\function [multiplybysomething[]](first:ignored,factor:2) +\function .multiplybysomething(first:ignored,factor:2) [multiply[2]multiply] \end -<$text text={{{ [[123]multiplybysomething[]] }}}/> +<$text text={{{ [[123].multiplybysomething[]] }}}/> - -<$text text={{{ [[123]multiplybysomething[x],[4]] }}}/> +<$text text={{{ [[123].multiplybysomething[x],[4]] }}}/> + title: ExpectedResult diff --git a/editions/test/tiddlers/tests/data/custom-operators/Simple.tid b/editions/test/tiddlers/tests/data/custom-operators/Simple.tid index 076a54dad..73d46d689 100644 --- a/editions/test/tiddlers/tests/data/custom-operators/Simple.tid +++ b/editions/test/tiddlers/tests/data/custom-operators/Simple.tid @@ -5,11 +5,11 @@ tags: [[$:/tags/wiki-test-spec]] title: Output -\function [multiplybytwo[]]() +\function .multiplybytwo() [multiply[2]] \end -<$text text={{{ [[123]multiplybytwo[]] }}}/> +<$text text={{{ [[123].multiplybytwo[]] }}}/> + title: ExpectedResult