]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/highlight.jsm
Imported Upstream version 1.1+hg7904
[dactyl.git] / common / modules / highlight.jsm
index 7d6cfaf03cf61a7cef814116d9128b535cc169b7..501edde272d2406b3f2043df60678caa0ce2f3e5 100644 (file)
@@ -1,16 +1,16 @@
-// Copyright (c) 2008-2011 by Kris Maglione <maglione.k at Gmail>
+// Copyright (c) 2008-2014 Kris Maglione <maglione.k at Gmail>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-/* use strict */
+"use strict";
 
-Components.utils.import("resource://dactyl/bootstrap.jsm");
 defineModule("highlight", {
     exports: ["Highlight", "Highlights", "highlight"],
     require: ["services", "util"]
-}, this);
+});
 
-this.lazyRequire("styles", ["Styles", "styles"]);
+lazyRequire("styles", ["Styles", "styles"]);
+lazyRequire("template", ["template"]);
 
 var Highlight = Struct("class", "selector", "sites",
                        "defaultExtends", "defaultValue",
@@ -57,22 +57,22 @@ Highlight.defaultValue("sites", function ()
 Highlight.defaultValue("style", function ()
     styles.system.add("highlight:" + this.class, this.sites, this.css, this.agent, true));
 
-Highlight.defaultValue("defaultExtends", function () []);
-Highlight.defaultValue("defaultValue", function () "");
+Highlight.defaultValue("defaultExtends", () => []);
+Highlight.defaultValue("defaultValue", () => "");
 Highlight.defaultValue("extends", function () this.defaultExtends);
 Highlight.defaultValue("value", function () this.defaultValue);
 
 update(Highlight.prototype, {
     get base() this.baseClass != this.class && highlight.highlight[this.baseClass] || null,
 
-    get bases() array.compact(this.extends.map(function (name) highlight.get(name))),
+    get bases() array.compact(this.extends.map(name => highlight.get(name))),
 
     get inheritedCSS() {
         if (this.gettingCSS)
             return "";
         try {
             this.gettingCSS = true;
-            return this.bases.map(function (b) b.cssText.replace(/;?\s*$/, "; ")).join("");
+            return this.bases.map(b => b.cssText.replace(/;?\s*$/, "; ")).join("");
         }
         finally {
             this.gettingCSS = false;
@@ -100,7 +100,7 @@ var Highlights = Module("Highlight", {
 
     keys: function keys() Object.keys(this.highlight).sort(),
 
-    __iterator__: function () values(this.highlight).sort(function (a, b) String.localeCompare(a.class, b.class))
+    __iterator__: function () values(this.highlight).sort((a, b) => String.localeCompare(a.class, b.class))
                                                     .iterValues(),
 
     _create: function _create(agent, args) {
@@ -125,7 +125,7 @@ var Highlights = Module("Highlight", {
         if (/^[[>+: ]/.test(args[1]))
             obj.selector = this.selector(obj.class) + args[1];
         else if (args[1])
-            obj.selector = this.selector(args[1]);
+            obj.selector = this.selector(args[1].replace(/^,/, ""));
 
         if (old && old.value != old.defaultValue)
             obj.value = old.value;
@@ -134,8 +134,12 @@ var Highlights = Module("Highlight", {
             obj.style.enabled = true;
         else
             this.loaded.__defineSetter__(obj.class, function () {
-                delete this[obj.class];
-                this[obj.class] = true;
+                Object.defineProperty(this, obj.class, {
+                    value: true,
+                    configurable: true,
+                    enumerable: true,
+                    writable: true
+                });
 
                 if (obj.class === obj.baseClass)
                     for (let h in highlight)
@@ -197,14 +201,14 @@ var Highlights = Module("Highlight", {
      * @param {string} group
      */
     highlightNode: function highlightNode(node, group, applyBindings) {
-        node.setAttributeNS(NS.uri, "highlight", group);
+        node.setAttributeNS(NS, "highlight", group);
 
         let groups = group.split(" ");
-        for each (let group in groups)
+        for (let group of groups)
             this.loaded[group] = true;
 
         if (applyBindings)
-            for each (let group in groups) {
+            for (let group of groups) {
                 if (applyBindings.bindings && group in applyBindings.bindings)
                     applyBindings.bindings[group](node, applyBindings);
                 else if (group in template.bindings)
@@ -218,20 +222,21 @@ var Highlights = Module("Highlight", {
      * @param {string} class
      */
     selector: function selector(class_)
-        let (self = this)
-           class_.replace(/(^|[>\s])([A-Z][\w-]+)\b/g,
-            function (m, n1, hl) n1 +
-                (self.highlight[hl] && self.highlight[hl].class != class_
-                    ? self.highlight[hl].selector : "[dactyl|highlight~=" + hl + "]")),
-
-    groupRegexp: util.regexp(<![CDATA[
+        class_.replace(/(^|[>\s])([A-Z][\w-]+)\b/g,
+            (m, n1, hl) => {
+                if (this.highlight[hl] && this.highlight[hl].class != class_)
+                    return n1 + this.highlight[hl].selector;
+                return n1 + "[dactyl|highlight~=" + hl + "]";
+            }),
+
+    groupRegexp: util.regexp(literal(/*
         ^
         (\s* (?:\S|\s\S)+ \s+)
         \{ ([^}]*) \}
         \s*
         $
-    ]]>, "gmx"),
-    sheetRegexp: util.regexp(<![CDATA[
+    */), "gmx"),
+    sheetRegexp: util.regexp(literal(/*
         ^\s*
         !? \*?
              (?P<group>    (?:[^;\s]|\s[^;\s])+ )
@@ -240,7 +245,8 @@ var Highlights = Module("Highlight", {
         (?:; (?P<extends>  (?:[^;\s]|\s[^;\s])+ )? )?
         \s*  (?P<css>      .*)
         $
-    ]]>, "x"),
+    */), "x"),
+    // </css>
 
     /**
      * Bulk loads new CSS rules, in the format of,
@@ -273,15 +279,15 @@ var Highlights = Module("Highlight", {
      * If Star is provided, the style is applied as an agent sheet.
      *
      * The new styles are lazily activated unless Bang or *eager* is
-     * provided. See {@link Util#xmlToDom}.
+     * provided.
      *
      * @param {string} css The rules to load. See {@link Highlights#css}.
      * @param {boolean} eager When true, load all provided rules immediately.
      */
     loadCSS: function loadCSS(css, eager) {
         String.replace(css, /\\\n/g, "")
-              .replace(this.groupRegexp, function (m, m1, m2) m1 + " " + m2.replace(/\n\s*/g, " "))
-              .split("\n").filter(function (s) /\S/.test(s) && !/^\s*\/\//.test(s))
+              .replace(this.groupRegexp, (m, m1, m2) => (m1 + " " + m2.replace(/\n\s*/g, " ")))
+              .split("\n").filter(s => (/\S/.test(s) && !/^\s*\/\//.test(s)))
               .forEach(function (highlight) {
 
             let bang = eager || /^\s*!/.test(highlight);
@@ -322,7 +328,7 @@ var Highlights = Module("Highlight", {
         commands.add(["hi[ghlight]"],
             "Set the style of certain display elements",
             function (args) {
-                let style = <![CDATA[
+                let style = literal(/*
                     ;
                     display: inline-block !important;
                     position: static !important;
@@ -330,7 +336,7 @@ var Highlights = Module("Highlight", {
                     width: 3em !important; min-width: 3em !important; max-width: 3em !important;
                     height: 1em !important; min-height: 1em !important; max-height: 1em !important;
                     overflow: hidden !important;
-                ]]>;
+                */);
                 let clear = args[0] == "clear";
                 if (clear)
                     args.shift();
@@ -349,10 +355,10 @@ var Highlights = Module("Highlight", {
                             ["padding: 0 1em 0 0; vertical-align: top; max-width: 16em; overflow: hidden;",
                              "text-align: center"],
                             ([h.class,
-                              <span style={"text-align: center; line-height: 1em;" + h.value + style}>XXX</span>,
-                              template.map(h.extends, function (s) template.highlight(s), <>,</>),
+                              ["span", { style: "text-align: center; line-height: 1em;" + h.value + style }, "XXX"],
+                              template.map(h.extends, s => template.highlight(s), ","),
                               template.highlightRegexp(h.value, /\b[-\w]+(?=:)|\/\*.*?\*\//g,
-                                                       function (match) <span highlight={match[0] == "/" ? "Comment" : "Key"}>{match}</span>)
+                                                       match => ["span", { highlight: match[0] == "/" ? "Comment" : "Key" }, match])
                              ]
                              for (h in highlight)
                              if (!key || h.class.indexOf(key) > -1))));
@@ -393,7 +399,7 @@ var Highlights = Module("Highlight", {
                         completer: function (context, args) {
                             let group = args[0] && highlight.get(args[0]);
                             if (group)
-                                context.fork("extra", 0, this, function (context) [
+                                context.fork("extra", 0, this, context => [
                                      [String(group.extends), _("option.currentValue")],
                                      [String(group.defaultExtends) || "", _("option.defaultValue")]
                                 ]);
@@ -422,12 +428,13 @@ var Highlights = Module("Highlight", {
             let extRe = RegExp("\\." + config.fileExtension + "$");
 
             context.title = ["Color Scheme", "Runtime Path"];
-            context.keys = { text: function (f) f.leafName.replace(extRe, ""), description: ".parent.path" };
+            context.keys = { text: f => f.leafName.replace(extRe, ""),
+                             description: ".parent.path" };
             context.completions =
                 array.flatten(
                         io.getRuntimeDirectories("colors").map(
-                            function (dir) dir.readDirectory().filter(
-                                function (file) extRe.test(file.leafName))))
+                            dir => dir.readDirectory()
+                                      .filter(file => extRe.test(file.leafName))))
                      .concat([
                         { leafName: "default", parent: { path: /*L*/"Revert to builtin colorscheme" } }
                      ]);
@@ -440,10 +447,10 @@ var Highlights = Module("Highlight", {
         };
     },
     javascript: function initJavascript(dactyl, modules, window) {
-        modules.JavaScript.setCompleter(["get", "set"].map(function (m) highlight[m]),
-            [ function (context, obj, args) Iterator(highlight.highlight) ]);
-        modules.JavaScript.setCompleter(["highlightNode"].map(function (m) highlight[m]),
-            [ null, function (context, obj, args) Iterator(highlight.highlight) ]);
+        modules.JavaScript.setCompleter(["get", "set"].map(m => highlight[m]),
+            [ (context, obj, args) => Iterator(highlight.highlight) ]);
+        modules.JavaScript.setCompleter(["highlightNode"].map(m => highlight[m]),
+            [ null, (context, obj, args) => Iterator(highlight.highlight) ]);
     }
 });
 
@@ -451,4 +458,4 @@ var Highlights = Module("Highlight", {
 
 endModule();
 
-// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: