gecko-dev/devtools/client/shared/stylesheet-utils.js
Julian Descottes 50a0db7113 Bug 1359090 - extract appendStyleSheet util from theme-switching to dedicated util;r=ochameau
MozReview-Commit-ID: 4MaGKbkyuZ9

--HG--
extra : rebase_source : 622115120ac1349b63175b6de088269e9f17e1ed
2017-04-27 15:26:38 +02:00

44 lines
1.5 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* eslint-env browser */
"use strict";
/*
* Append a stylesheet to the provided XUL document.
*
* @param {Document} xulDocument
* The XUL document where the stylesheet should be appended.
* @param {String} url
* The url of the stylesheet to load.
* @return {Object}
* - styleSheet {XMLStylesheetProcessingInstruction} the instruction node created.
* - loadPromise {Promise} that will resolve/reject when the stylesheet finishes
* or fails to load.
*/
function appendStyleSheet(xulDocument, url) {
let styleSheetAttr = `href="${url}" type="text/css"`;
let styleSheet = xulDocument.createProcessingInstruction(
"xml-stylesheet", styleSheetAttr);
let loadPromise = new Promise((resolve, reject) => {
function onload() {
styleSheet.removeEventListener("load", onload);
styleSheet.removeEventListener("error", onerror);
resolve();
}
function onerror() {
styleSheet.removeEventListener("load", onload);
styleSheet.removeEventListener("error", onerror);
reject("Failed to load theme file " + url);
}
styleSheet.addEventListener("load", onload);
styleSheet.addEventListener("error", onerror);
});
xulDocument.insertBefore(styleSheet, xulDocument.documentElement);
return {styleSheet, loadPromise};
}
exports.appendStyleSheet = appendStyleSheet;