From 4dbd720323b7e252888d5078b2a490649ac5cdab Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 22 Aug 2016 00:22:27 +0200 Subject: [PATCH] Fixed insert order of elements Prepending an element would cause script tags to be prepended first, causing any JavaScript to be executed immediately. This could be an issue if the code relies on the DOM that originally preceeded it. --- .../files/js/WoltLabSuite/Core/Dom/Util.js | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js index d8d2d0cae6..e3370a0471 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Util.js @@ -275,25 +275,39 @@ define(['Environment', 'StringUtil'], function(Environment, StringUtil) { var element = elCreate('div'); this.setInnerHtml(element, html); + var node; if (insertMethod === 'append' || insertMethod === 'after') { while (element.childNodes.length) { + node = element.childNodes[0]; + if (insertMethod === 'append') { - referenceElement.appendChild(element.childNodes[0]); + referenceElement.appendChild(node); } else { - this.insertAfter(element.childNodes[0], referenceElement); + this.insertAfter(node, referenceElement); } } } else if (insertMethod === 'prepend' || insertMethod === 'before') { + var scriptTags = []; for (var i = element.childNodes.length - 1; i >= 0; i--) { + node = element.childNodes[i]; + if (node.nodeType === Node.ELEMENT_NODE && node.nodeName === 'SCRIPT') { + scriptTags.push(node); + continue; + } + if (insertMethod === 'prepend') { - this.prepend(element.childNodes[i], referenceElement); + this.prepend(node, referenceElement); } else { - referenceElement.parentNode.insertBefore(element.childNodes[i], referenceElement); + referenceElement.parentNode.insertBefore(node, referenceElement); } } + + scriptTags.forEach(function (scriptTag) { + document.body.appendChild(scriptTag); + }); } else { throw new Error("Unknown insert method '" + insertMethod + "'."); -- 2.20.1