From e50101322f4caebeded229e91df0e8f7b4fc2c88 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Sat, 21 May 2022 15:47:19 +0100 Subject: [PATCH] Require $$ for custom widgets, and that overridden JS widgets must exist See https://github.com/Jermolene/TiddlyWiki5/pull/6666#issuecomment-1133637763 --- core/modules/parsers/wikiparser/rules/html.js | 3 --- core/modules/widgets/widget.js | 10 ++++--- .../transclude/CustomWidget-ActionWidget.tid | 8 +++--- .../data/transclude/CustomWidget-Fail.tid | 26 +++++++++++++++++++ .../data/transclude/CustomWidget-Simple.tid | 12 ++++----- .../CustomWidget-VariableAttribute.tid | 8 +++--- 6 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 editions/test/tiddlers/tests/data/transclude/CustomWidget-Fail.tid diff --git a/core/modules/parsers/wikiparser/rules/html.js b/core/modules/parsers/wikiparser/rules/html.js index 7fc4bb96e..64469e3b2 100644 --- a/core/modules/parsers/wikiparser/rules/html.js +++ b/core/modules/parsers/wikiparser/rules/html.js @@ -93,9 +93,6 @@ exports.parseTag = function(source,pos,options) { return null; } node.tag = token.match[1]; - if(node.tag.slice(1).indexOf("$") !== -1) { - return null; - } if(node.tag.charAt(0) === "$") { node.type = node.tag.substr(1); } diff --git a/core/modules/widgets/widget.js b/core/modules/widgets/widget.js index bc6c4b3fe..9fcda8e51 100755 --- a/core/modules/widgets/widget.js +++ b/core/modules/widgets/widget.js @@ -435,10 +435,14 @@ options include: Widget.prototype.makeChildWidget = function(parseTreeNode,options) { var self = this; options = options || {}; - // Check whether this node type is defined by a custom macro definition + // Check whether this node type is defined by a custom widget definition var variableDefinitionName = "$" + parseTreeNode.type, - variableInfo = this.variables[variableDefinitionName]; - if(!parseTreeNode.isNotRemappable && variableInfo && variableInfo.value && variableInfo.isWidgetDefinition) { + variableInfo = this.variables[variableDefinitionName], + isOverrideable = function() { + // Widget is overrideable if it has a double dollar user defined name, or if it is an existing JS widget + return parseTreeNode.type.charAt(0) === "$" || !!self.widgetClasses[parseTreeNode.type]; + }; + if(!parseTreeNode.isNotRemappable && isOverrideable() && variableInfo && variableInfo.value && variableInfo.isWidgetDefinition) { var newParseTreeNode = { type: "transclude", children: [ diff --git a/editions/test/tiddlers/tests/data/transclude/CustomWidget-ActionWidget.tid b/editions/test/tiddlers/tests/data/transclude/CustomWidget-ActionWidget.tid index 3cddb63cf..0be77a9a3 100644 --- a/editions/test/tiddlers/tests/data/transclude/CustomWidget-ActionWidget.tid +++ b/editions/test/tiddlers/tests/data/transclude/CustomWidget-ActionWidget.tid @@ -12,15 +12,15 @@ title: Output title: Actions \whitespace trim - -\widget $action-mywidget(one:'Jaguar') + +\widget $$action-mywidget(one:'Jaguar') \whitespace trim <$action-setfield $tiddler="Result" $field="text" $value=<>/> \end -<$action-mywidget one="Dingo"> +<$$action-mywidget one="Dingo"> Crocodile - + + title: ExpectedResult diff --git a/editions/test/tiddlers/tests/data/transclude/CustomWidget-Fail.tid b/editions/test/tiddlers/tests/data/transclude/CustomWidget-Fail.tid new file mode 100644 index 000000000..15be21b05 --- /dev/null +++ b/editions/test/tiddlers/tests/data/transclude/CustomWidget-Fail.tid @@ -0,0 +1,26 @@ +title: Transclude/CustomWidget/Fail +description: Custom widget failed definition +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\whitespace trim + +\widget $non-existent-widget(one:'Jaguar') +\whitespace trim +<$text text=<>/> +<$slot $name="ts-body"> + Whale + +\end +<$non-existent-widget one="Dingo"> + Crocodile + +<$non-existent-widget one="BumbleBee"> + Squirrel + ++ +title: ExpectedResult + +

Undefined widget 'non-existent-widget'Undefined widget 'non-existent-widget'

\ No newline at end of file diff --git a/editions/test/tiddlers/tests/data/transclude/CustomWidget-Simple.tid b/editions/test/tiddlers/tests/data/transclude/CustomWidget-Simple.tid index 86d181042..f98b2079a 100644 --- a/editions/test/tiddlers/tests/data/transclude/CustomWidget-Simple.tid +++ b/editions/test/tiddlers/tests/data/transclude/CustomWidget-Simple.tid @@ -12,20 +12,20 @@ title: Output title: TiddlerOne \whitespace trim - -\widget $mywidget(one:'Jaguar') + +\widget $$mywidget(one:'Jaguar') \whitespace trim <$text text=<>/> <$slot $name="ts-body"> Whale \end -<$mywidget one="Dingo"> +<$$mywidget one="Dingo"> Crocodile - -<$mywidget one="BumbleBee"> + +<$$mywidget one="BumbleBee"> Squirrel - + + title: ExpectedResult diff --git a/editions/test/tiddlers/tests/data/transclude/CustomWidget-VariableAttribute.tid b/editions/test/tiddlers/tests/data/transclude/CustomWidget-VariableAttribute.tid index a7e0e62e3..63fd5f87a 100644 --- a/editions/test/tiddlers/tests/data/transclude/CustomWidget-VariableAttribute.tid +++ b/editions/test/tiddlers/tests/data/transclude/CustomWidget-VariableAttribute.tid @@ -12,17 +12,17 @@ title: Output title: TiddlerOne \whitespace trim - -\widget $mywidget($variable:'Jaguar') + +\widget $$mywidget($variable:'Jaguar') \whitespace trim <$text text=<<$variable>>/> <$slot $name="ts-body"> Whale \end -<$mywidget $variable="Dingo"> +<$$mywidget $variable="Dingo"> Crocodile - + + title: ExpectedResult