1 // Copyright (c) 2008-2011 by Kris Maglione <maglione.k at Gmail>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
7 Components.utils.import("resource://dactyl/bootstrap.jsm");
8 defineModule("styles", {
9 exports: ["Style", "Styles", "styles"],
10 require: ["services", "util"],
11 use: ["contexts", "messages", "template"]
14 function cssUri(css) "chrome-data:text/css," + encodeURI(css);
15 var namespace = "@namespace html " + XHTML.uri.quote() + ";\n" +
16 "@namespace xul " + XUL.uri.quote() + ";\n" +
17 "@namespace dactyl " + NS.uri.quote() + ";\n";
19 var Sheet = Struct("name", "id", "sites", "css", "hive", "agent");
20 Sheet.liveProperty = function (name) {
21 let i = this.prototype.members[name];
22 this.prototype.__defineGetter__(name, function () this[i]);
23 this.prototype.__defineSetter__(name, function (val) {
25 val = Array.slice(val);
29 this.enabled = this.enabled;
32 Sheet.liveProperty("agent");
33 Sheet.liveProperty("css");
34 Sheet.liveProperty("sites");
35 update(Sheet.prototype, {
36 formatSites: function (uris)
37 template.map(this.sites,
38 function (filter) <span highlight={uris.some(Styles.matchFilter(filter)) ? "Filter" : ""}>{filter}</span>,
41 remove: function () { this.hive.remove(this); },
43 get uri() "dactyl://style/" + this.id + "/" + this.hive.name + "/" + (this.name || ""),
45 get enabled() this._enabled,
47 if (on != this._enabled || this.fullCSS != this._fullCSS) {
50 else if (!this._fullCSS)
53 let meth = on ? "registerSheet" : "unregisterSheet";
54 styles[meth](this.uri, on ? this.agent : this._agent);
56 this._agent = this.agent;
57 this._enabled = Boolean(on);
58 this._fullCSS = this.fullCSS;
62 match: function (uri) {
64 uri = util.newURI(uri);
65 return this.sites.some(function (site) Styles.matchFilter(site, uri));
69 let filter = this.sites;
72 let preamble = "/* " + this.uri + (this.agent ? " (agent)" : "") + " */\n\n" + namespace + "\n";
74 return preamble + css;
76 let selectors = filter.map(function (part)
77 !/^(?:[a-z-]+[:*]|[a-z-.]+$)/i.test(part) ? "regexp(" + (".*(?:" + part + ").*").quote() + ")" :
78 (/[*]$/.test(part) ? "url-prefix" :
79 /[\/:]/.test(part) ? "url"
81 + '("' + part.replace(/"/g, "%22").replace(/\*$/, "") + '")')
84 return preamble + "@-moz-document " + selectors + " {\n\n" + css + "\n\n}\n";
88 var Hive = Class("Hive", {
89 init: function (name, persist) {
94 this.persist = persist;
97 get modifiable() this.name !== "system",
99 addRef: function (obj) {
100 this.refs.push(Cu.getWeakReference(obj));
103 dropRef: function (obj) {
104 this.refs = this.refs.filter(function (ref) ref.get() && ref.get() !== obj);
105 if (!this.refs.length) {
107 styles.hives = styles.hives.filter(function (h) h !== this, this);
111 cleanup: function cleanup() {
112 for (let sheet in values(this.sheets))
113 sheet.enabled = false;
116 __iterator__: function () Iterator(this.sheets),
118 get sites() array(this.sheets).map(function (s) s.sites).flatten().uniq().array,
121 * Add a new style sheet.
123 * @param {string} name The name given to the style sheet by
124 * which it may be later referenced.
125 * @param {string} filter The sites to which this sheet will
126 * apply. Can be a domain name or a URL. Any URL ending in
127 * "*" is matched as a prefix.
128 * @param {string} css The CSS to be applied.
129 * @param {boolean} agent If true, the sheet is installed as an
131 * @param {boolean} lazy If true, the sheet is not initially enabled.
134 add: function add(name, filter, css, agent, lazy) {
136 if (!isArray(filter))
137 filter = filter.split(",");
138 if (name && name in this.names) {
139 var sheet = this.names[name];
141 sheet.css = String(css);
142 sheet.sites = filter;
145 sheet = Sheet(name, styles._id++, filter.filter(util.identity), String(css), this, agent);
146 this.sheets.push(sheet);
149 styles.allSheets[sheet.id] = sheet;
152 sheet.enabled = true;
155 this.names[name] = sheet;
160 * Get a sheet with a given name or index.
162 * @param {string or number} sheet The sheet to retrieve. Strings indicate
163 * sheet names, while numbers indicate indices.
165 get: function get(sheet) {
166 if (typeof sheet === "number")
167 return this.sheets[sheet];
168 return this.names[sheet];
172 * Find sheets matching the parameters. See {@link #addSheet}
175 * @param {string} name
176 * @param {string} filter
177 * @param {string} css
178 * @param {number} index
180 find: function find(name, filter, css, index) {
181 // Grossly inefficient.
182 let matches = [k for ([k, v] in Iterator(this.sheets))];
184 matches = String(index).split(",").filter(function (i) i in this.sheets, this);
186 matches = matches.filter(function (i) this.sheets[i].name == name, this);
188 matches = matches.filter(function (i) this.sheets[i].css == css, this);
190 matches = matches.filter(function (i) this.sheets[i].sites.indexOf(filter) >= 0, this);
191 return matches.map(function (i) this.sheets[i], this);
195 * Remove a style sheet. See {@link #addSheet} for parameters.
196 * In cases where *filter* is supplied, the given filters are removed from
197 * matching sheets. If any remain, the sheet is left in place.
199 * @param {string} name
200 * @param {string} filter
201 * @param {string} css
202 * @param {number} index
204 remove: function remove(name, filter, css, index) {
206 if (arguments.length == 1) {
207 var matches = [name];
211 if (filter && filter.indexOf(",") > -1)
212 return filter.split(",").reduce(
213 function (n, f) n + self.removeSheet(name, f, index), 0);
215 if (filter == undefined)
219 matches = this.findSheets(name, filter, css, index);
220 if (matches.length == 0)
223 for (let [, sheet] in Iterator(matches.reverse())) {
225 let sites = sheet.sites.filter(function (f) f != filter);
231 sheet.enabled = false;
233 delete this.names[sheet.name];
234 delete styles.allSheets[sheet.id];
236 this.sheets = this.sheets.filter(function (s) matches.indexOf(s) == -1);
237 return matches.length;
242 * Manages named and unnamed user style sheets, which apply to both
243 * chrome and content pages.
245 * @author Kris Maglione <maglione.k@gmail.com>
247 var Styles = Module("Styles", {
248 Local: function (dactyl, modules, window) ({
249 cleanup: function () {}
257 services["dactyl:"].providers["style"] = function styleProvider(uri) {
258 let id = /^\/(\d*)/.exec(uri.path)[1];
259 if (Set.has(styles.allSheets, id))
260 return ["text/css", styles.allSheets[id].fullCSS];
265 cleanup: function cleanup() {
266 for each (let hive in this.hives)
267 util.trapErrors("cleanup", hive);
269 this.user = this.addHive("user", this, true);
270 this.system = this.addHive("system", this, false);
273 addHive: function addHive(name, ref, persist) {
274 let hive = array.nth(this.hives, function (h) h.name === name, 0);
276 hive = Hive(name, persist);
277 this.hives.push(hive);
279 hive.persist = persist;
285 __iterator__: function () Iterator(this.user.sheets.concat(this.system.sheets)),
287 _proxy: function (name, args)
288 let (obj = this[args[0] ? "system" : "user"])
289 obj[name].apply(obj, Array.slice(args, 1)),
291 addSheet: deprecated("Styles#{user,system}.add", function addSheet() this._proxy("add", arguments)),
292 findSheets: deprecated("Styles#{user,system}.find", function findSheets() this._proxy("find", arguments)),
293 get: deprecated("Styles#{user,system}.get", function get() this._proxy("get", arguments)),
294 removeSheet: deprecated("Styles#{user,system}.remove", function removeSheet() this._proxy("remove", arguments)),
296 userSheets: Class.Property({ get: deprecated("Styles#user.sheets", function userSheets() this.user.sheets) }),
297 systemSheets: Class.Property({ get: deprecated("Styles#system.sheets", function systemSheets() this.system.sheets) }),
298 userNames: Class.Property({ get: deprecated("Styles#user.names", function userNames() this.user.names) }),
299 systemNames: Class.Property({ get: deprecated("Styles#system.names", function systemNames() this.system.names) }),
300 sites: Class.Property({ get: deprecated("Styles#user.sites", function sites() this.user.sites) }),
302 list: function list(content, sites, name, hives) {
303 const { commandline, dactyl } = this.modules;
305 hives = hives || styles.hives.filter(function (h) h.modifiable && h.sheets.length);
307 function sheets(group)
309 .filter(function (sheet) (!name || sheet.name === name) &&
310 (!sites || sites.every(function (s) sheet.sites.indexOf(s) >= 0)))
311 .sort(function (a, b) a.name && b.name ? String.localeCompare(a.name, b.name)
312 : !!b.name - !!a.name || a.id - b.id);
314 let uris = util.visibleURIs(content);
317 <tr highlight="Title">
320 <td style="padding-right: 1em;">{_("title.Name")}</td>
321 <td style="padding-right: 1em;">{_("title.Filter")}</td>
322 <td style="padding-right: 1em;">{_("title.CSS")}</td>
324 <col style="min-width: 4em; padding-right: 1em;"/>
325 <col style="min-width: 1em; text-align: center; color: red; font-weight: bold;"/>
326 <col style="padding: 0 1em 0 1ex; vertical-align: top;"/>
327 <col style="padding: 0 1em 0 0; vertical-align: top;"/>
329 template.map(hives, function (hive) let (i = 0)
330 <tr style="height: .5ex;"/> +
331 template.map(sheets(hive), function (sheet)
333 <td highlight="Title">{!i++ ? hive.name : ""}</td>
334 <td>{sheet.enabled ? "" : UTF8("×")}</td>
335 <td>{sheet.name || hive.sheets.indexOf(sheet)}</td>
336 <td>{sheet.formatSites(uris)}</td>
339 <tr style="height: .5ex;"/>)
343 // TODO: Move this to an ItemList to show this automatically
344 if (list.*.length() === list.text().length() + 5)
345 dactyl.echomsg(_("style.none"));
347 commandline.commandOutput(list);
350 registerSheet: function registerSheet(url, agent, reload) {
351 let uri = services.io.newURI(url, null, null);
353 this.unregisterSheet(url, agent);
355 let type = services.stylesheet[agent ? "AGENT_SHEET" : "USER_SHEET"];
356 if (reload || !services.stylesheet.sheetRegistered(uri, type))
357 services.stylesheet.loadAndRegisterSheet(uri, type);
360 unregisterSheet: function unregisterSheet(url, agent) {
361 let uri = services.io.newURI(url, null, null);
362 let type = services.stylesheet[agent ? "AGENT_SHEET" : "USER_SHEET"];
363 if (services.stylesheet.sheetRegistered(uri, type))
364 services.stylesheet.unregisterSheet(uri, type);
367 append: function (dest, src, sort) {
369 for each (let str in [dest, src])
370 for (let prop in Styles.propertyIter(str))
371 props[prop.name] = prop.value;
373 let val = Object.keys(props)[sort ? "sort" : "slice"]()
374 .map(function (prop) prop + ": " + props[prop] + ";")
377 if (/^\s*(\/\*.*?\*\/)/.exec(src))
378 val = RegExp.$1 + " " + val;
382 completeSite: function (context, content, group) {
383 group = group || styles.user;
384 context.anchored = false;
386 context.fork("current", 0, this, function (context) {
387 context.title = ["Current Site"];
388 context.completions = [
389 [content.location.host, /*L*/"Current Host"],
390 [content.location.href, /*L*/"Current URL"]
396 let uris = util.visibleURIs(content);
398 context.generate = function () values(group.sites);
400 context.keys.text = util.identity;
401 context.keys.description = function (site) this.sheets.length + /*L*/" sheet" + (this.sheets.length == 1 ? "" : "s") + ": " +
402 array.compact(this.sheets.map(function (s) s.name)).join(", ");
403 context.keys.sheets = function (site) group.sheets.filter(function (s) s.sites.indexOf(site) >= 0);
404 context.keys.active = function (site) uris.some(Styles.matchFilter(site));
406 Styles.splitContext(context, "Sites");
410 * A curried function which determines which host names match a
411 * given stylesheet filter. When presented with one argument,
412 * returns a matcher function which, given one nsIURI argument,
413 * returns true if that argument matches the given filter. When
414 * given two arguments, returns true if the second argument matches
417 * @param {string} filter The URI filter to match against.
418 * @param {nsIURI} uri The location to test.
419 * @returns {nsIURI -> boolean}
421 matchFilter: function (filter) {
423 var test = function test(uri) true;
424 else if (!/^(?:[a-z-]+:|[a-z-.]+$)/.test(filter)) {
425 let re = util.regexp(filter);
426 test = function test(uri) re.test(uri.spec);
428 else if (/[*]$/.test(filter)) {
429 let re = RegExp("^" + util.regexp.escape(filter.substr(0, filter.length - 1)));
430 test = function test(uri) re.test(uri.spec);
432 else if (/[\/:]/.test(filter))
433 test = function test(uri) uri.spec === filter;
435 test = function test(uri) { try { return util.isSubdomain(uri.host, filter); } catch (e) { return false; } };
436 test.toString = function toString() filter;
437 if (arguments.length < 2)
439 return test(arguments[1]);
442 splitContext: function splitContext(context, title) {
443 for (let item in Iterator({ Active: true, Inactive: false })) {
444 let [name, active] = item;
445 context.split(name, null, function (context) {
446 context.title[0] = /*L*/name + " " + (title || "Sheets");
447 context.filters.push(function (item) !!item.active == active);
452 propertyIter: function (str, always) {
454 for (let match in this.propertyPattern.iterate(str)) {
455 if (match.value || always && match.name || match.wholeMatch === match.preSpace && always && !i++)
457 if (!/;/.test(match.postSpace))
462 propertyPattern: util.regexp(<![CDATA[
464 (?P<preSpace> <space>*)
467 <space>* : \s* (?P<value>
472 (?: <string> | [^)]* )
483 (?P<postSpace> <space>* (?: ; | $) )
486 space: /(?: \s | \/\* .*? \*\/ )/,
487 string: /(?:" (?:[^\\"]|\\.)* (?:"|$) | '(?:[^\\']|\\.)* (?:'|$) )/
491 get property() util.regexp(<![CDATA[
493 (?P<preSpace> <space>*)
496 <space>* : \s* (?P<value>
501 (?P<postSpace> <space>* (?: ; | $) )
504 get function() util.regexp(<![CDATA[
507 (?: <string> | [^)]* )
512 space: /(?: \s | \/\* .*? \*\/ )/,
514 get string() util.regexp(<![CDATA[
516 " (?:[^\\"]|\\.)* (?:"|$) |
517 ' (?:[^\\']|\\.)* (?:'|$)
521 get token() util.regexp(<![CDATA[
526 | (?P<important> !important\b)
534 commands: function (dactyl, modules, window) {
535 const { commands, contexts, styles } = modules;
537 function sheets(context, args, filter) {
538 let uris = util.visibleURIs(window.content);
539 context.compare = modules.CompletionContext.Sort.number;
540 context.generate = function () args["-group"].sheets;
541 context.keys.active = function (sheet) uris.some(sheet.closure.match);
542 context.keys.description = function (sheet) <>{sheet.formatSites(uris)}: {sheet.css.replace("\n", "\\n")}</>
544 context.filters.push(function ({ item }) filter(item));
545 Styles.splitContext(context);
548 function nameFlag(filter) ({
549 names: ["-name", "-n"],
550 description: "The name of this stylesheet",
551 type: modules.CommandOption.STRING,
552 completer: function (context, args) {
553 context.keys.text = function (sheet) sheet.name;
554 context.filters.unshift(function ({ item }) item.name);
555 sheets(context, args, filter);
559 commands.add(["sty[le]"],
560 "Add or list user styles",
562 let [filter, css] = args;
565 styles.list(window.content, filter ? filter.split(",") : null, args["-name"], args.explicitOpts["-group"] ? [args["-group"]] : null);
567 util.assert(args["-group"].modifiable && args["-group"].hive.modifiable,
568 _("group.cantChangeBuiltin", _("style.styles")));
570 if (args["-append"]) {
571 let sheet = args["-group"].get(args["-name"]);
573 filter = array(sheet.sites).concat(filter).uniq().join(",");
574 css = sheet.css + " " + css;
577 let style = args["-group"].add(args["-name"], filter, css, args["-agent"]);
579 if (args["-nopersist"] || !args["-append"] || style.persist === undefined)
580 style.persist = !args["-nopersist"];
584 completer: function (context, args) {
586 let sheet = args["-group"].get(args["-name"]);
587 if (args.completeArg == 0) {
589 context.completions = [[sheet.sites.join(","), "Current Value"]];
590 context.fork("sites", 0, Styles, "completeSite", window.content, args["-group"]);
592 else if (args.completeArg == 1) {
594 context.completions = [
595 [sheet.css, _("option.currentValue")]
597 context.fork("css", 0, modules.completion, "css");
603 { names: ["-agent", "-A"], description: "Apply style as an Agent sheet" },
604 { names: ["-append", "-a"], description: "Append site filter and css to an existing, matching sheet" },
605 contexts.GroupFlag("styles"),
607 { names: ["-nopersist", "-N"], description: "Do not save this style to an auto-generated RC file" }
609 serialize: function ()
611 .filter(function (hive) hive.persist)
613 hive.sheets.filter(function (style) style.persist)
614 .sort(function (a, b) String.localeCompare(a.name || "", b.name || ""))
615 .map(function (style) ({
617 arguments: [style.sites.join(",")],
618 literalArg: style.css,
620 hive.name == "user" ? {} : { "-group": hive.name },
621 style.name ? { "-name": style.name } : {})
628 name: ["stylee[nable]", "stye[nable]"],
629 desc: "Enable a user style sheet",
630 action: function (sheet) sheet.enabled = true,
631 filter: function (sheet) !sheet.enabled
634 name: ["styled[isable]", "styd[isable]"],
635 desc: "Disable a user style sheet",
636 action: function (sheet) sheet.enabled = false,
637 filter: function (sheet) sheet.enabled
640 name: ["stylet[oggle]", "styt[oggle]"],
641 desc: "Toggle a user style sheet",
642 action: function (sheet) sheet.enabled = !sheet.enabled
645 name: ["dels[tyle]"],
646 desc: "Remove a user style sheet",
647 action: function (sheet) sheet.remove(),
649 ].forEach(function (cmd) {
650 commands.add(cmd.name, cmd.desc,
652 dactyl.assert(args.bang ^ !!(args[0] || args[1] || args["-name"] || args["-index"]),
653 _("error.argumentOrBang"));
655 args["-group"].find(args["-name"], args[0], args.literalArg, args["-index"])
656 .forEach(cmd.action);
659 completer: function (context, args) {
660 let uris = util.visibleURIs(window.content);
662 Styles.completeSite(context, window.content, args["-group"]);
664 context.filters.push(function ({ sheets }) sheets.some(cmd.filter));
668 contexts.GroupFlag("styles"),
670 names: ["-index", "-i"],
671 type: modules.CommandOption.INT,
672 completer: function (context, args) {
673 context.keys.text = function (sheet) args["-group"].sheets.indexOf(sheet);
674 sheets(context, args, cmd.filter);
682 contexts: function (dactyl, modules, window) {
683 modules.contexts.Hives("styles",
684 Class("LocalHive", Contexts.Hive, {
685 init: function init(group) {
686 init.superapply(this, arguments);
687 this.hive = styles.addHive(group.name, this, this.persist);
690 get names() this.hive.names,
691 get sheets() this.hive.sheets,
692 get sites() this.hive.sites,
694 __noSuchMethod__: function __noSuchMethod__(meth, args) {
695 return this.hive[meth].apply(this.hive, args);
698 destroy: function () {
699 this.hive.dropRef(this);
703 completion: function (dactyl, modules, window) {
704 const names = Array.slice(util.computedStyle(window.document.createElement("div")));
705 modules.completion.css = function (context) {
706 context.title = ["CSS Property"];
707 context.keys = { text: function (p) p + ":", description: function () "" };
709 for (let match in Styles.propertyIter(context.filter, true))
710 var lastMatch = match;
712 if (lastMatch != null && !lastMatch.value && !lastMatch.postSpace) {
713 context.advance(lastMatch.index + lastMatch.preSpace.length);
714 context.completions = names;
718 javascript: function (dactyl, modules, window) {
719 modules.JavaScript.setCompleter(["get", "add", "remove", "find"].map(function (m) styles.user[m]),
720 [ // Prototype: (name, filter, css, index)
721 function (context, obj, args) this.names,
722 function (context, obj, args) Styles.completeSite(context, window.content),
724 function (context, obj, args) this.sheets
727 template: function () {
728 let patterns = Styles.patterns;
730 template.highlightCSS = function highlightCSS(css) {
731 XML.prettyPrinting = XML.ignoreWhitespace = false;
733 return this.highlightRegexp(css, patterns.property, function (match) <>{
734 match.preSpace}{template.filter(match.name)}: {
736 template.highlightRegexp(match.value, patterns.token, function (match) {
738 return <>{template.filter(match.word)}{
739 template.highlightRegexp(match.function, patterns.string,
740 function (match) <span highlight="String">{match.string}</span>)
742 if (match.important == "!important")
743 return <span highlight="String">{match.important}</span>;
745 return <span highlight="String">{match.string}</span>;
746 return template.highlightRegexp(match.wholeMatch, /^(\d+)(em|ex|px|in|cm|mm|pt|pc)?/g,
747 function (m, n, u) <><span highlight="Number">{n}</span><span highlight="Object">{u || ""}</span></>);
750 }{ match.postSpace }</>
758 // catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);}
760 // vim: set fdm=marker sw=4 ts=4 et ft=javascript: