Skip to content

Commit

Permalink
Bug 1280362 - Make browser.js's newGlobal and DocumentWrite work rega…
Browse files Browse the repository at this point in the history
…rdless of the test's behavior. r=arai
  • Loading branch information
jswalden committed Jul 28, 2016
1 parent 6278a9a commit 29b2b7e
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions js/src/tests/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,53 @@
// bizarre things that might break the harness.

(function(global) {
/**********************************************************************
* CACHED PRIMORDIAL FUNCTIONALITY (before a test might overwrite it) *
**********************************************************************/

var ReflectApply = global.Reflect.apply;

var document = global.document;
var documentBody = global.document.body;
var documentDocumentElement = global.document.documentElement;
var DocumentCreateElement = global.document.createElement;
var HTMLDocumentPrototypeWrite = global.HTMLDocument.prototype.write;
var ElementInnerHTMLSetter =
Object.getOwnPropertyDescriptor(global.Element.prototype, "innerHTML").set;
var HTMLIFramePrototypeContentWindowGetter =
Object.getOwnPropertyDescriptor(global.HTMLIFrameElement.prototype, "contentWindow").get;
var HTMLIFramePrototypeRemove = global.HTMLIFrameElement.prototype.remove;
var NodePrototypeAppendChild = global.Node.prototype.appendChild;

/****************************
* GENERAL HELPER FUNCTIONS *
****************************/

function AppendChild(elt, kid) {
ReflectApply(NodePrototypeAppendChild, elt, [kid]);
}

function CreateElement(name) {
return ReflectApply(DocumentCreateElement, document, [name]);
}

function SetInnerHTML(element, html) {
ReflectApply(ElementInnerHTMLSetter, element, [html]);
}

/****************************
* UTILITY FUNCTION EXPORTS *
****************************/

var newGlobal = global.newGlobal;
if (typeof newGlobal !== "function") {
newGlobal = function newGlobal() {
var iframe = global.document.createElement("iframe");
global.document.documentElement.appendChild(iframe);
var win = iframe.contentWindow;
iframe.remove();
var iframe = CreateElement("iframe");
AppendChild(documentDocumentElement, iframe);
var win =
ReflectApply(HTMLIFramePrototypeContentWindowGetter, iframe, []);
ReflectApply(HTMLIFramePrototypeRemove, iframe, []);

// Shim in "evaluate"
win.evaluate = win.eval;
return win;
Expand All @@ -37,11 +73,11 @@
// it.
function DocumentWrite(s) {
try {
var msgDiv = global.document.createElement('div');
msgDiv.innerHTML = s;
global.document.body.appendChild(msgDiv);
var msgDiv = CreateElement('div');
SetInnerHTML(msgDiv, s);
AppendChild(documentBody, msgDiv);
} catch (e) {
global.document.write(s + '<br>\n');
ReflectApply(HTMLDocumentPrototypeWrite, document, [s + "<br>\n"]);
}
}
global.DocumentWrite = DocumentWrite;
Expand Down

0 comments on commit 29b2b7e

Please sign in to comment.