diff --git a/core/modules/widgets/button.js b/core/modules/widgets/button.js index 633514f45..603520fe0 100644 --- a/core/modules/widgets/button.js +++ b/core/modules/widgets/button.js @@ -18,10 +18,6 @@ var Popup = require("$:/core/modules/utils/dom/popup.js"); var ButtonWidget = function(parseTreeNode,options) { this.initialise(parseTreeNode,options); - // Check if any parent is a button. Custom recursion detection for buttons in buttons - if(!this.hasVariable("tv-is-button","yes")) { - this.setVariable("tv-is-button", "yes"); - } }; /* @@ -29,6 +25,20 @@ Inherit from the base widget class */ ButtonWidget.prototype = new Widget(); +/* +Detect nested buttons +*/ +ButtonWidget.prototype.isNestedButton = function() { + var pointer = this.parentWidget; + while(pointer) { + if(pointer instanceof ButtonWidget) { + return true; + } + pointer = pointer.parentWidget; + } + return false; +} + /* Render this widget into the DOM */ @@ -43,14 +53,14 @@ ButtonWidget.prototype.render = function(parent,nextSibling) { this.execute(); // Check "button in button". Return early with an error message // This check also prevents fatal recursion errors using the transclusion widget - if(this.parentWidget && this.parentWidget.hasVariable("tv-is-button","yes")) { + if(this.isNestedButton()) { var domNode = this.document.createElement("span"); var textNode = this.document.createTextNode($tw.language.getString("Error/RecursiveButton")); domNode.appendChild(textNode); domNode.className = "tc-error"; parent.insertBefore(domNode,nextSibling); this.domNodes.push(domNode); - return; + return; // an error message } // Create element if(this.buttonTag && $tw.config.htmlUnsafeElements.indexOf(this.buttonTag) === -1) {