addon-sdk/source/lib/sdk/stylesheet/style.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 module.metadata = {
michael@0 7 "stability": "experimental"
michael@0 8 };
michael@0 9
michael@0 10 const { Cc, Ci } = require("chrome");
michael@0 11 const { Class } = require("../core/heritage");
michael@0 12 const { ns } = require("../core/namespace");
michael@0 13 const { URL } = require('../url');
michael@0 14 const events = require("../system/events");
michael@0 15 const { loadSheet, removeSheet, isTypeValid } = require("./utils");
michael@0 16 const { isString } = require("../lang/type");
michael@0 17 const { attachTo, detachFrom, getTargetWindow } = require("../content/mod");
michael@0 18
michael@0 19 const { freeze, create } = Object;
michael@0 20 const LOCAL_URI_SCHEMES = ['resource', 'data'];
michael@0 21
michael@0 22 function isLocalURL(item) {
michael@0 23 try {
michael@0 24 return LOCAL_URI_SCHEMES.indexOf(URL(item).scheme) > -1;
michael@0 25 }
michael@0 26 catch(e) {}
michael@0 27
michael@0 28 return false;
michael@0 29 }
michael@0 30
michael@0 31 function Style({ source, uri, type }) {
michael@0 32 source = source == null ? null : freeze([].concat(source));
michael@0 33 uri = uri == null ? null : freeze([].concat(uri));
michael@0 34 type = type == null ? "author" : type;
michael@0 35
michael@0 36 if (source && !source.every(isString))
michael@0 37 throw new Error('Style.source must be a string or an array of strings.');
michael@0 38
michael@0 39 if (uri && !uri.every(isLocalURL))
michael@0 40 throw new Error('Style.uri must be a local URL or an array of local URLs');
michael@0 41
michael@0 42 if (type && !isTypeValid(type))
michael@0 43 throw new Error('Style.type must be "agent", "user" or "author"');
michael@0 44
michael@0 45 return freeze(create(Style.prototype, {
michael@0 46 "source": { value: source, enumerable: true },
michael@0 47 "uri": { value: uri, enumerable: true },
michael@0 48 "type": { value: type, enumerable: true }
michael@0 49 }));
michael@0 50 };
michael@0 51
michael@0 52 exports.Style = Style;
michael@0 53
michael@0 54 attachTo.define(Style, function (style, window) {
michael@0 55 if (style.uri) {
michael@0 56 for (let uri of style.uri)
michael@0 57 loadSheet(window, uri, style.type);
michael@0 58 }
michael@0 59
michael@0 60 if (style.source) {
michael@0 61 let uri = "data:text/css;charset=utf-8,";
michael@0 62
michael@0 63 uri += encodeURIComponent(style.source.join(""));
michael@0 64
michael@0 65 loadSheet(window, uri, style.type);
michael@0 66 }
michael@0 67 });
michael@0 68
michael@0 69 detachFrom.define(Style, function (style, window) {
michael@0 70 if (style.uri)
michael@0 71 for (let uri of style.uri)
michael@0 72 removeSheet(window, uri);
michael@0 73
michael@0 74 if (style.source) {
michael@0 75 let uri = "data:text/css;charset=utf-8,";
michael@0 76
michael@0 77 uri += encodeURIComponent(style.source.join(""));
michael@0 78
michael@0 79 removeSheet(window, uri, style.type);
michael@0 80 }
michael@0 81 });

mercurial