Skip to content

Commit

Permalink
the TransclusionError is a proper error
Browse files Browse the repository at this point in the history
Moved the magic number to be on the error's class. Not sure if that's
a great idea.
  • Loading branch information
flibbles committed Dec 12, 2023
1 parent 777176d commit 19a39f2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
9 changes: 7 additions & 2 deletions core/modules/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ Custom errors for TiddlyWiki.
\*/
(function(){

function TranscludeRecursionError(depth) {
this.depth = depth;
function TranscludeRecursionError() {
Error.apply(this,arguments);
this.signatures = Object.create(null);
};

/* Maximum permitted depth of the widget tree for recursion detection */
TranscludeRecursionError.MAX_WIDGET_TREE_DEPTH = 1000;

TranscludeRecursionError.prototype = Object.create(Error);

exports.TranscludeRecursionError = TranscludeRecursionError;

})();
2 changes: 1 addition & 1 deletion core/modules/widgets/transclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TranscludeWidget.prototype.render = function(parent,nextSibling) {
// Hopefully that will land us just outside where the loop began. That's where we want to issue an error.
// Rendering widgets beneath this point may result in a freezing browser if they explode exponentially.
var transcludeSignature = this.getVariable("transclusion");
if(this.getAncestorCount() > error.depth - 50) {
if(this.getAncestorCount() > $tw.utils.TranscludeRecursionError.MAX_WIDGET_TREE_DEPTH - 50) {
// For the first fifty transcludes we climb up, we simply collect signatures.
// We're assuming that those first 50 will likely include all transcludes involved in the loop.
error.signatures[transcludeSignature] = true;
Expand Down
7 changes: 2 additions & 5 deletions core/modules/widgets/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ Widget base class
/*global $tw: false */
"use strict";

/* Maximum permitted depth of the widget tree for recursion detection */
var MAX_WIDGET_TREE_DEPTH = 1000;

/*
Create a widget object for a parse tree node
parseTreeNode: reference to the parse tree node to be rendered
Expand Down Expand Up @@ -494,8 +491,8 @@ Widget.prototype.makeChildWidgets = function(parseTreeNodes,options) {
this.children = [];
var self = this;
// Check for too much recursion
if(this.getAncestorCount() > MAX_WIDGET_TREE_DEPTH) {
throw new $tw.utils.TranscludeRecursionError(MAX_WIDGET_TREE_DEPTH);
if(this.getAncestorCount() > $tw.utils.TranscludeRecursionError.MAX_WIDGET_TREE_DEPTH) {
throw new $tw.utils.TranscludeRecursionError();
} else {
// Create set variable widgets for each variable
$tw.utils.each(options.variables,function(value,name) {
Expand Down
2 changes: 1 addition & 1 deletion editions/test/tiddlers/tests/test-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("Widget module", function() {
expect(wrapper.innerHTML).toBe("<span class=\"tc-error\">Recursive transclusion error in transclude widget</span> <span class=\"tc-error\">Recursive transclusion error in transclude widget</span>");
});

fit("should handle many-tiddler recursion with branching nodes", function() {
it("should handle many-tiddler recursion with branching nodes", function() {
var wiki = new $tw.Wiki();
// Add a tiddler
wiki.addTiddlers([
Expand Down

0 comments on commit 19a39f2

Please sign in to comment.