Skip to content

Commit

Permalink
Better handling of infinite recursion
Browse files Browse the repository at this point in the history
But it could be better still...
  • Loading branch information
flibbles committed Dec 12, 2023
1 parent bc89805 commit 777176d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 3 additions & 2 deletions core/modules/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Custom errors for TiddlyWiki.
\*/
(function(){

function TranscludeRecursionError(transcludeMarker) {
this.marker = transcludeMarker;
function TranscludeRecursionError(depth) {
this.depth = depth;
this.signatures = Object.create(null);
};

exports.TranscludeRecursionError = TranscludeRecursionError;
Expand Down
8 changes: 7 additions & 1 deletion core/modules/widgets/transclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ TranscludeWidget.prototype.render = function(parent,nextSibling) {
// We need to try and abort as much of the loop as we can, so we will keep "throwing" upward until we find a transclusion that has a different signature.
// 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.
if(error.marker !== this.getVariable("transclusion")) {
var transcludeSignature = this.getVariable("transclusion");
if(this.getAncestorCount() > error.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;
} else if(!error.signatures[transcludeSignature]) {
// Now that we're past the first 50, let's look for the first signature that wasn't in the loop. That'll be where we print the error and resume rendering.
this.children = [this.makeChildWidget({type: "error", attributes: {
"$message": {type: "string", value: $tw.language.getString("Error/RecursiveTransclusion")}
}})];
Expand Down
2 changes: 1 addition & 1 deletion core/modules/widgets/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ Widget.prototype.makeChildWidgets = function(parseTreeNodes,options) {
var self = this;
// Check for too much recursion
if(this.getAncestorCount() > MAX_WIDGET_TREE_DEPTH) {
throw new $tw.utils.TranscludeRecursionError(this.getVariable("transclusion"));
throw new $tw.utils.TranscludeRecursionError(MAX_WIDGET_TREE_DEPTH);
} else {
// Create set variable widgets for each variable
$tw.utils.each(options.variables,function(value,name) {
Expand Down
23 changes: 22 additions & 1 deletion editions/test/tiddlers/tests/test-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe("Widget module", function() {
expect(wrapper.innerHTML).toBe("<span class=\"tc-error\">Recursive transclusion error in transclude widget</span>");
});

it("should handle recursion with branching nodes", function() {
it("should handle single-tiddler recursion with branching nodes", function() {
var wiki = new $tw.Wiki();
// Add a tiddler
wiki.addTiddlers([
Expand All @@ -180,6 +180,27 @@ 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() {
var wiki = new $tw.Wiki();
// Add a tiddler
wiki.addTiddlers([
{title: "TiddlerOne", text: "<$transclude tiddler='TiddlerTwo'/> <$transclude tiddler='TiddlerTwo'/>"},
{title: "TiddlerTwo", text: "<$transclude tiddler='TiddlerOne'/>"}
]);
// Test parse tree
var parseTreeNode = {type: "widget", children: [
{type: "transclude", attributes: {
"tiddler": {type: "string", value: "TiddlerOne"}
}}
]};
// Construct the widget node
var widgetNode = createWidgetNode(parseTreeNode,wiki);
// Render the widget node to the DOM
var wrapper = renderWidgetNode(widgetNode);
// Test the rendering
expect(wrapper.innerHTML).toBe("<span class=\"tc-error\">Recursive transclusion error in transclude widget</span>");
});

it("should deal with SVG elements", function() {
var wiki = new $tw.Wiki();
// Construct the widget node
Expand Down

0 comments on commit 777176d

Please sign in to comment.