From d04cc757a72d827dcf46ada479df91f544fc6746 Mon Sep 17 00:00:00 2001 From: Jeff Fisher Date: Wed, 21 Jan 2015 20:20:59 -0800 Subject: [PATCH] Fix #8 The dynamic document we create to pre-process HTML will automatically create wrapping document, head, and body elements when HTML is passed in. This resulted in extra and tags getting created when calling innerHTML on elements that are not document elements. --- winstore-jscompat.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/winstore-jscompat.js b/winstore-jscompat.js index db58395..eb6dbfa 100644 --- a/winstore-jscompat.js +++ b/winstore-jscompat.js @@ -57,7 +57,7 @@ return isUnsafe; } - function cleanse(html) { + function cleanse(html, isDocument) { var cleaner = document.implementation.createHTMLDocument("cleaner"); empty(cleaner.documentElement); MSApp.execUnsafeLocalFunction(function () { @@ -120,7 +120,17 @@ } cleanseAttributes(cleaner.documentElement); - return Array.prototype.slice.call(document.adoptNode(cleaner.documentElement).childNodes); + var rootElement; + // If we are setting inner/outerHTML on a document element grab everything. + if (isDocument) { + rootElement = cleaner.documentElement; + } else { + // Otherwise, the cleaner will dynamically create wrapping document and body tags, so take + // from inside the body. + rootElement = cleaner.body; + } + + return Array.prototype.slice.call(document.adoptNode(rootElement).childNodes); } function cleansePropertySetter(property, setter) { @@ -133,7 +143,7 @@ originalSetter.call(this, value); } else { var that = this; - var nodes = cleanse(value); + var nodes = cleanse(value, that.tagName === 'HTML'); MSApp.execUnsafeLocalFunction(function () { setter(propertyDescriptor, that, nodes); });