From 11127c5a7d3074acd4a0ec6da79588315a673758 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sat, 12 Feb 2011 20:52:18 -0500 Subject: [PATCH] Accept more plausible URLs rather than falling back to horrid defsearch. Closes issue #338. --- common/content/dactyl.js | 55 ++++++++++++++++++++--------------- common/modules/addons.jsm | 2 +- common/modules/completion.jsm | 17 ++++------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/common/content/dactyl.js b/common/content/dactyl.js index 86ee8f06..7b0f96b5 100644 --- a/common/content/dactyl.js +++ b/common/content/dactyl.js @@ -1281,7 +1281,6 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), { * @param {string} str * @returns {string[]} */ - stringToURLArray: deprecated("dactyl.parseURLs", "parseURLs"), parseURLs: function parseURLs(str) { let urls; @@ -1290,7 +1289,24 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), { else urls = [str]; + let re = util.regexp(+ (:\d+)? (/ .*)?) $ + ]]>, "g", { + domain: util.regexp(String.replace(, /U/g, "\\u")) + }); + return urls.map(function (url) { + url = url.trim(); + if (/^(\.{0,2}|~)(\/|$)/.test(url)) { try { // Try to find a matching file. @@ -1301,35 +1317,26 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), { catch (e) {} } - // strip each 'URL' - makes things simpler later on - url = url.trim(); + // If it starts with a valid protocol, pass it through. + let proto = /^([-\w]+):/.exec(url); + if (proto && "@mozilla.org/network/protocol;1?name=" + proto[1] in Cc) + return url.replace(/\s+/g, ""); - // Look for a valid protocol - let proto = url.match(/^([-\w]+):/); - if (proto && Cc["@mozilla.org/network/protocol;1?name=" + proto[1]]) - // Handle as URL, but remove spaces. Useful for copied/'p'asted URLs. - return url.replace(/\s*\n+\s*/g, ""); - - // Ok, not a valid proto. If it looks like URL-ish (foo.com/bar), - // let Gecko figure it out. - if (/^[a-zA-Z0-9-.]+(?:\/|$)/.test(url) && /[.\/]/.test(url) && !/\s/.test(url) || /^[a-zA-Z0-9-.]+:\d+(?:\/|$)/.test(url)) - return url; - - // TODO: it would be clearer if the appropriate call to - // getSearchURL was made based on whether or not the first word was - // indeed an SE alias rather than seeing if getSearchURL can - // process the call usefully and trying again if it fails - - // check for a search engine match in the string, then try to - // search for the whole string in the default engine - let searchURL = bookmarks.getSearchURL(url, false) || bookmarks.getSearchURL(url, true); + // Check for a matching search keyword. + let searchURL = bookmarks.getSearchURL(url, false); if (searchURL) return searchURL; - // Hmm. No defsearch? Let the host app deal with it, then. - return url; + // If it looks like URL-ish (foo.com/bar), let Gecko figure it out. + if (re.test(url)) + return util.createURI(url).spec; + + // Pass it off to the default search engine or, failing + // that, let Gecko deal with it as is. + return bookmarks.getSearchURL(url, true) || util.createURI(url).spec; }); }, + stringToURLArray: deprecated("dactyl.parseURLs", "parseURLs"), get assert() util.assert, diff --git a/common/modules/addons.jsm b/common/modules/addons.jsm index 6b29b72e..3235b568 100644 --- a/common/modules/addons.jsm +++ b/common/modules/addons.jsm @@ -384,7 +384,7 @@ var Addons = Module("addons", { }, { argCount: "1", completer: function (context) { - context.filters.push(function ({ item }) item.isDirectory() || /\.xpi$/.test(item.leafName)); + context.filters.push(function ({ isdir, text }) isdir || /\.xpi$/.test(text)); completion.file(context); }, literal: 0 diff --git a/common/modules/completion.jsm b/common/modules/completion.jsm index 3374ec2d..428cf92b 100644 --- a/common/modules/completion.jsm +++ b/common/modules/completion.jsm @@ -899,32 +899,27 @@ var Completion = Module("completion", { // depending on the 'complete' option // if the 'complete' argument is passed like "h", it temporarily overrides the complete option url: function url(context, complete) { - let numLocationCompletions = 0; // how many async completions did we already return to the caller? - let start = 0; - let skip = 0; if (this.options["urlseparator"]) - skip = context.filter.match("^.*" + this.options["urlseparator"]); // start after the last 'urlseparator' - + var skip = RegExp("^.*" + this.options["urlseparator"] + "\\s*").exec(context.filter); if (skip) context.advance(skip[0].length); - if (complete == null) - complete = this.options["complete"]; - - if (/^about:/.test(context.filter)) { + if (/^about:/.test(context.filter)) context.fork("about", 6, this, function (context) { context.generate = function () { const PREFIX = "@mozilla.org/network/protocol/about;1?what="; return [[k.substr(PREFIX.length), ""] for (k in Cc) if (k.indexOf(PREFIX) == 0)]; }; }); - } + + if (complete == null) + complete = this.options["complete"]; // Will, and should, throw an error if !(c in opts) Array.forEach(complete, function (c) { let completer = this.urlCompleters[c]; - context.fork.apply(context, [c, 0, this, completer.completer].concat(completer.args)); + context.forkapply(c, 0, this, completer.completer, completer.args); }, this); },