From 8457cfdf3391385c9434c2d9f96e89b200da1279 Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Wed, 7 Nov 2007 12:00:34 +0000 Subject: move String#toURLArray to v.util.stringToURLArray - think of the kittens! --- content/commands.js | 77 --------------------------------------------------- content/util.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ content/vimperator.js | 2 +- 3 files changed, 78 insertions(+), 78 deletions(-) diff --git a/content/commands.js b/content/commands.js index a53d858e..a9c4035b 100644 --- a/content/commands.js +++ b/content/commands.js @@ -1886,81 +1886,4 @@ vimperator.Commands = function() //{{{ //}}} } //}}} -// takes a string like 'google bla, www.osnews.com' -// and returns an array ['www.google.com/search?q=bla', 'www.osnews.com'] -String.prototype.toURLArray = function() // {{{ -{ - var urls = this.split(/\s*\,\s+/); - - begin: for (var url = 0; url < urls.length; url++) - { - var new_url = vimperator.buffer.URL; - var matches; - - // strip each 'URL' - makes things simpler later on - urls[url] = urls[url].replace(/^\s+/, '').replace(/\s+$/, ''); - - // FIXME: not really that good (doesn't handle .. in the middle) - // check for ./ and ../ (or even .../) to go to a file in the upper directory - if (matches = urls[url].match(/^(?:\.$|\.\/(\S*))/)) - { - var tail = matches[1] ? matches[1] : ""; - urls[url] = new_url.replace(/(.+\/)[^\/]*/, "$1" + tail); // NOTE: escape / in character sets so as not to break Vim syntax highlighting - continue; - } - else if (matches = urls[url].match(/^(?:\.\.$|\.\.\/(\S*))/)) - { - var tail = matches[1] ? matches[1] : ""; - urls[url] = new_url.replace(/(.+\/)[^\/]*/, "$1../" + tail); - continue; - } - else if (matches = urls[url].match(/^(?:\.\.\.$|\.\.\.\/(\S*))/)) - { - var location = window.content.document.location; - var tail = matches[1] ? matches[1] : ""; - urls[url] = location.protocol + "//" + location.host + "/" + tail; - continue; - } - - // if the string doesn't look like a valid URL (i.e. contains a space - // or does not contain any of: .:/) try opening it with a search engine - // or keyword bookmark - if (/\s/.test(urls[url]) || !/[.:\/]/.test(urls[url])) - { - matches = urls[url].match(/^(\S+)(?:\s+(.+))?$/); - - var alias = matches[1]; - var text = matches[2] ? matches[2] : null; - - // 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 - much - // like the comments below ;-) - - // check if the first word is a search engine - var search_url = vimperator.bookmarks.getSearchURL(text, alias); - if (search_url/* && search_url.length >= 1*/) - { - urls[url] = search_url; - continue; - } - else // the first word was not a search engine, search for the whole string in the default engine - { - search_url = vimperator.bookmarks.getSearchURL(urls[url], null); - if (search_url/* && search_url.length >= 1*/) - { - urls[url] = search_url; - continue; - } - } - } - - // if we are here let Firefox handle the url and hope it does - // something useful with it :) - } - - return urls; -} // }}} - // vim: set fdm=marker sw=4 ts=4 et: diff --git a/content/util.js b/content/util.js index 7f45e386..4327d47c 100644 --- a/content/util.js +++ b/content/util.js @@ -81,6 +81,83 @@ vimperator.util = { return arg; }, + // takes a string like 'google bla, www.osnews.com' + // and returns an array ['www.google.com/search?q=bla', 'www.osnews.com'] + stringToURLArray: function(str) + { + var urls = str.split(/\s*\,\s+/); + + begin: for (var url = 0; url < urls.length; url++) + { + var new_url = vimperator.buffer.URL; + var matches; + + // strip each 'URL' - makes things simpler later on + urls[url] = urls[url].replace(/^\s+/, '').replace(/\s+$/, ''); + + // FIXME: not really that good (doesn't handle .. in the middle) + // check for ./ and ../ (or even .../) to go to a file in the upper directory + if (matches = urls[url].match(/^(?:\.$|\.\/(\S*))/)) + { + var tail = matches[1] || ""; + urls[url] = new_url.replace(/(.+\/)[^\/]*/, "$1" + tail); // NOTE: escape / in character sets so as not to break Vim syntax highlighting + continue; + } + else if (matches = urls[url].match(/^(?:\.\.$|\.\.\/(\S*))/)) + { + var tail = matches[1] || ""; + urls[url] = new_url.replace(/(.+\/)[^\/]*/, "$1../" + tail); + continue; + } + else if (matches = urls[url].match(/^(?:\.\.\.$|\.\.\.\/(\S*))/)) + { + var location = window.content.document.location; + var tail = matches[1] || ""; + urls[url] = location.protocol + "//" + location.host + "/" + tail; + continue; + } + + // if the string doesn't look like a valid URL (i.e. contains a space + // or does not contain any of: .:/) try opening it with a search engine + // or keyword bookmark + if (/\s/.test(urls[url]) || !/[.:\/]/.test(urls[url])) + { + matches = urls[url].match(/^(\S+)(?:\s+(.+))?$/); + + var alias = matches[1]; + var text = matches[2] || null; + + // 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 - much + // like the comments below ;-) + + // check if the first word is a search engine + var search_url = vimperator.bookmarks.getSearchURL(text, alias); + if (search_url/* && search_url.length >= 1*/) + { + urls[url] = search_url; + continue; + } + else // the first word was not a search engine, search for the whole string in the default engine + { + search_url = vimperator.bookmarks.getSearchURL(urls[url], null); + if (search_url/* && search_url.length >= 1*/) + { + urls[url] = search_url; + continue; + } + } + } + + // if we are here let Firefox handle the url and hope it does + // something useful with it :) + } + + return urls; + }, + highlightURL: function(str, force) { if (force || /^[a-zA-Z]+:\/\/.*\//.test(str)) diff --git a/content/vimperator.js b/content/vimperator.js index 25e86a95..c72123fc 100644 --- a/content/vimperator.js +++ b/content/vimperator.js @@ -423,7 +423,7 @@ const vimperator = (function() //{{{ // convert the string to an array of converted URLs // -> see String.prototype.toURLArray for more details if (typeof urls == "string") - urls = urls.toURLArray(); + urls = vimperator.util.stringToURLArray(urls); if (urls.length == 0) return false; -- cgit v1.2.3