]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/protocol.jsm
Imported Upstream version 1.1+hg7904
[dactyl.git] / common / modules / protocol.jsm
index 98d8e3bcab7d0f466869aacc4ce339b2a6d330f1..d5e6d1a379ca20b28b03223064a3c22db9430363 100644 (file)
@@ -1,14 +1,13 @@
-// Copyright (c) 2008-2011 by Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2008-2014 Kris Maglione <maglione.k@gmail.com>
 //
 // 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("protocol", {
     exports: ["LocaleChannel", "Protocol", "RedirectChannel", "StringChannel", "XMLChannel"],
     require: ["services", "util"]
-}, this);
+});
 
 var systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].getService(Ci.nsIPrincipal);
 
@@ -45,14 +44,18 @@ function NetError(orig, error) {
 
         originalURI: orig,
 
-        asyncOpen: function () { throw error || Cr.NS_ERROR_FILE_NOT_FOUND },
+        asyncOpen: function () { throw error || Cr.NS_ERROR_FILE_NOT_FOUND; },
 
-        open: function () { throw error || Cr.NS_ERROR_FILE_NOT_FOUND }
+        open: function () { throw error || Cr.NS_ERROR_FILE_NOT_FOUND; }
     }).data.QueryInterface(Ci.nsIChannel);
 }
 function RedirectChannel(to, orig, time, message) {
-    let html = <html><head><meta http-equiv="Refresh" content={(time || 0) + ";" + to}/></head>
-                     <body><h2 style="text-align: center">{message || ""}</h2></body></html>.toXMLString();
+    let html = DOM.toXML(
+        ["html", {},
+            ["head", {},
+                ["meta", { "http-equiv": "Refresh", content: (time || 0) + ";" + to }]],
+            ["body", {},
+                ["h2", { style: "text-align: center" }, message || ""]]]);
     return StringChannel(html, "text/html", services.io.newURI(to, null, null));
 }
 
@@ -69,7 +72,7 @@ function Protocol(scheme, classID, contentBase) {
 
         contentBase: contentBase,
 
-        _xpcom_factory: JSMLoader.Factory(Protocol),
+        _xpcom_factory: JSMLoader.Factory(Protocol)
     };
     return Protocol;
 }
@@ -140,9 +143,9 @@ ProtocolBase.prototype = {
 };
 
 function LocaleChannel(pkg, locale, path, orig) {
-    for each (let locale in [locale, "en-US"])
-        for each (let sep in "-/") {
-            var channel = Channel(["resource:/", pkg + sep + locale, path].join("/"), orig, true);
+    for (let locale of [locale, "en-US"])
+        for (let sep of "-/") {
+            var channel = Channel(["resource:/", pkg + sep + locale, path].join("/"), orig, true, true);
             if (channel)
                 return channel;
         }
@@ -182,31 +185,34 @@ function XMLChannel(uri, contentType, noErrorChannel, unprivileged) {
     this.channel.contentType = contentType || channel.contentType;
     this.channel.contentCharset = "UTF-8";
     if (!unprivileged)
-    this.channel.owner = systemPrincipal;
-
-    let stream = services.InputStream(channelStream);
-    let [, pre, doctype, url, extra, open, post] = util.regexp(<![CDATA[
-            ^ ([^]*?)
-            (?:
-                (<!DOCTYPE \s+ \S+ \s+) (?:SYSTEM \s+ "([^"]*)" | ((?:[^[>\s]|\s[^[])*))
-                (\s+ \[)?
-                ([^]*)
-            )?
-            $
-        ]]>, "x").exec(stream.read(4096));
-    this.writes.push(pre);
-    if (doctype) {
-        this.writes.push(doctype + (extra || "") + " [\n");
-        if (url)
-            this.addChannel(url);
-
-        if (!open)
-            this.writes.push("\n]");
-
-        for (let [, pre, url] in util.regexp.iterate(/([^]*?)(?:%include\s+"([^"]*)";|$)/gy, post)) {
-            this.writes.push(pre);
+        this.channel.owner = systemPrincipal;
+
+    let type = this.channel.contentType;
+    if (/^text\/|[\/+]xml$/.test(type)) {
+        let stream = services.InputStream(channelStream);
+        let [, pre, doctype, url, extra, open, post] = util.regexp(literal(/*
+                ^ ([^]*?)
+                (?:
+                    (<!DOCTYPE \s+ \S+ \s+) (?:SYSTEM \s+ "([^"]*)" | ((?:[^[>\s]|\s[^[])*))
+                    (\s+ \[)?
+                    ([^]*)
+                )?
+                $
+            */), "x").exec(stream.read(4096));
+        this.writes.push(pre);
+        if (doctype) {
+            this.writes.push(doctype + (extra || "") + " [\n");
             if (url)
                 this.addChannel(url);
+
+            if (!open)
+                this.writes.push("\n]");
+
+            for (let [, pre, url] in util.regexp.iterate(/([^]*?)(?:%include\s+"([^"]*)";|$)/gy, post)) {
+                this.writes.push(pre);
+                if (url)
+                    this.addChannel(url);
+            }
         }
     }
     this.writes.push(channelStream);
@@ -221,6 +227,7 @@ XMLChannel.prototype = {
             this.writes.push(services.io.newChannel(url, null, this.uri).open());
         }
         catch (e) {
+            util.dump("addChannel('" + url + "'):");
             util.reportError(e);
         }
     },
@@ -252,4 +259,4 @@ XMLChannel.prototype = {
 
 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: