make_css_data_uri: support @-moz-document regexp(...)
[conkeror.git] / modules / stylesheet.js
blob16a05d63bfb33d49050455d5e7bd07aac96bbf07
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2010 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 function register_user_stylesheet (url) {
11     var uri = make_uri(url);
12     var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
13         .getService(Ci.nsIStyleSheetService);
14     sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
17 function unregister_user_stylesheet (url) {
18     var uri = make_uri(url);
19     var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
20         .getService(Ci.nsIStyleSheetService);
21     if (sss.sheetRegistered(uri, sss.USER_SHEET))
22         sss.unregisterSheet(uri, sss.USER_SHEET);
25 function register_agent_stylesheet (url) {
26     var uri = make_uri(url);
27     var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
28         .getService(Ci.nsIStyleSheetService);
29     sss.loadAndRegisterSheet(uri, sss.AGENT_SHEET);
32 function unregister_agent_stylesheet (url) {
33     var uri = make_uri(url);
34     var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
35         .getService(Ci.nsIStyleSheetService);
36     if (sss.sheetRegistered(uri, sss.AGENT_SHEET))
37         sss.unregisterSheet(uri, sss.AGENT_SHEET);
40 function agent_stylesheet_registered_p (url) {
41     var uri = make_uri(url);
42     var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
43         .getService(Ci.nsIStyleSheetService);
44     return sss.sheetRegistered(uri, sss.AGENT_SHEET);
47 function user_stylesheet_registered_p (url) {
48     var uri = make_uri(url);
49     var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
50         .getService(Ci.nsIStyleSheetService);
51     return sss.sheetRegistered(uri, sss.USER_SHEET);
54 /**
55  * make_css_data_uri takes specifications for a css data uri in its
56  * positional and keyword arguments, and returns a new nsIURI object.
57  *
58  * rules: css rules, not including any @namespace or @-moz-document
59  *        headers.  given as a string or an array of strings.
60  *
61  * $namespace: optional string url for a @namespace header.
62  *
63  * $domains: optional string, or array of strings, for a @-moz-document
64  *           domain(...) header.
65  *
66  * $urls: optional string, or array of strings, for a @-moz-document
67  *        url(...) header.
68  *
69  * $url_prefixes: optional string, or array of strings, for a
70  *                @-moz-document url-prefix(...) header.
71  *
72  * $regexps: optional string or RegExp, or array of strings or RegExps,
73  *           for a @-moz-document regexp(...) header.
74  */
75 define_keywords("$namespace", "$domains", "$urls", "$url_prefixes", "$regexps");
76 function make_css_data_uri (rules) {
77     keywords(arguments);
78     var namespace = arguments.$namespace;
79     var domains = arguments.$domains;
80     var urls = arguments.$urls;
81     var url_prefixes = arguments.$url_prefixes;
82     var regexps = arguments.$regexps;
83     rules = make_array(rules).join("\n");
84     var restrictions = Array.prototype.concat(
85         make_array(domains).map(function (x) "domain("+x+")"),
86         make_array(urls).map(function (x) "url("+x+")"),
87         make_array(url_prefixes).map(function (x) "url-prefix("+x+")"),
88         make_array(regexps).map(function (x) {
89             return "regexp(\""+
90                 ((x instanceof RegExp) ? x.source : x)+
91                 "\")";
92         }))
93         .join(",\n");
94     if (restrictions)
95         rules = "@-moz-document "+restrictions+" {\n"+rules+"\n}";
96     if (namespace)
97         rules = "@namespace url("+namespace+");\n"+rules;
98     return make_uri("data:text/css,"+escape(rules));
101 provide("stylesheet");