From eb0955875ba6ba77a90b1e411c42be0df6de950d Mon Sep 17 00:00:00 2001 From: Jez Ng Date: Tue, 4 Sep 2012 15:23:29 -0400 Subject: Style fixes. Errors were detected using coffeelint. --- background_scripts/commands.coffee | 6 +++--- background_scripts/completion.coffee | 4 ++-- background_scripts/main.coffee | 26 +++++++++++++------------- content_scripts/link_hints.coffee | 22 +++++++++++----------- content_scripts/vimium_frontend.coffee | 14 +++++++------- content_scripts/vomnibar.coffee | 1 - lib/dom_utils.coffee | 3 ++- test_harnesses/automated/automated.coffee | 19 ++++++++++++------- 8 files changed, 50 insertions(+), 45 deletions(-) diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index e3001f4f..69c37ac4 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -43,9 +43,9 @@ Commands = # On the other hand, and are different named keys - for one of # them you have to press "shift" as well. normalizeKey: (key) -> - key.replace(/<[acm]-/ig, (match) -> match.toLowerCase()) - .replace(/<([acm]-)?([a-zA-Z0-9]{2,5})>/g, (match, optionalPrefix, keyName) -> - "<" + (if optionalPrefix then optionalPrefix else "") + keyName.toLowerCase() + ">") + key.replace(/<[acm]-/ig, (match) -> match.toLowerCase()) + .replace(/<([acm]-)?([a-zA-Z0-9]{2,5})>/g, (match, optionalPrefix, keyName) -> + "<" + (if optionalPrefix then optionalPrefix else "") + keyName.toLowerCase() + ">") parseCustomKeyMappings: (customKeyMappings) -> lines = customKeyMappings.split("\n") diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 5920db0b..5539f7c7 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -26,7 +26,7 @@ class Suggestion generateHtml: -> return @html if @html - relevancyHtml = if @showRelevancy then "#{@computeRelevancy() + ''}" else "" + relevancyHtml = if @showRelevancy then "#{@computeRelevancy()}" else "" # NOTE(philc): We're using these vimium-specific class names so we don't collide with the page's CSS. @html = "
@@ -90,7 +90,7 @@ class BookmarkCompleter performSearch: -> results = @bookmarks.filter (bookmark) => - RankingUtils.matches(@currentSearch.queryTerms, bookmark.url, bookmark.title) + RankingUtils.matches(@currentSearch.queryTerms, bookmark.url, bookmark.title) suggestions = results.map (bookmark) => new Suggestion(@currentSearch.queryTerms, "bookmark", bookmark.url, bookmark.title, @computeRelevancy) onComplete = @currentSearch.onComplete diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 707febc9..5ae80235 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -265,17 +265,17 @@ selectTab = (callback, direction) -> chrome.tabs.getAllInWindow(null, (tabs) -> return unless tabs.length > 1 chrome.tabs.getSelected(null, (currentTab) -> - switch direction - when "next" - toSelect = tabs[(currentTab.index + 1 + tabs.length) % tabs.length] - when "previous" - toSelect = tabs[(currentTab.index - 1 + tabs.length) % tabs.length] - when "first" - toSelect = tabs[0] - when "last" - toSelect = tabs[tabs.length - 1] - selectionChangedHandlers.push(callback) - chrome.tabs.update(toSelect.id, { selected: true }))) + switch direction + when "next" + toSelect = tabs[(currentTab.index + 1 + tabs.length) % tabs.length] + when "previous" + toSelect = tabs[(currentTab.index - 1 + tabs.length) % tabs.length] + when "first" + toSelect = tabs[0] + when "last" + toSelect = tabs[tabs.length - 1] + selectionChangedHandlers.push(callback) + chrome.tabs.update(toSelect.id, { selected: true }))) updateOpenTabs = (tab) -> openTabs[tab.id] = { url: tab.url, positionIndex: tab.index, windowId: tab.windowId } @@ -367,7 +367,7 @@ updatePositionsAndWindowsForAllTabsInWindow = (windowId) -> splitKeyIntoFirstAndSecond = (key) -> if (key.search(namedKeyRegex) == 0) - { first: RegExp.$1, second: RegExp.$2 } + { first: RegExp.$1, second: RegExp.$2 } else { first: key[0], second: key.slice(1) } @@ -409,7 +409,7 @@ generateCompletionKeys = (keysToCheck) -> for key of Commands.keyToCommandRegistry splitKey = splitKeyIntoFirstAndSecond(key) if (splitKey.first == command) - completionKeys.push(splitKey.second) + completionKeys.push(splitKey.second) completionKeys diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 1d5d668a..831408e2 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -34,10 +34,10 @@ LinkHints = # We use translate() instead of lower-case() because Chrome only supports XPath 1.0. # clickableElementsXPath: DomUtils.makeXPath( - ["a", "area[@href]", "textarea", "button", "select", - "input[not(@type='hidden' or @disabled or @readonly)]", - "*[@onclick or @tabindex or @role='link' or @role='button' or contains(@class, 'button') or " + - "@contenteditable='' or translate(@contenteditable, 'TRUE', 'true')='true']"]) + ["a", "area[@href]", "textarea", "button", "select", + "input[not(@type='hidden' or @disabled or @readonly)]", + "*[@onclick or @tabindex or @role='link' or @role='button' or contains(@class, 'button') or " + + "@contenteditable='' or translate(@contenteditable, 'TRUE', 'true')='true']"]) # We need this as a top-level function because our command system doesn't yet support arguments. activateModeToOpenInNewTab: -> @activateMode(true, false, false) @@ -80,8 +80,8 @@ LinkHints = else if (withQueue) HUD.show("Open multiple links in a new tab") @linkActivator = (link) -> - # When "clicking" on a link, dispatch the event with the appropriate meta key (CMD on Mac, CTRL on windows) - # to open it in a new tab if necessary. + # When "clicking" on a link, dispatch the event with the appropriate meta key (CMD on Mac, CTRL on + # windows) to open it in a new tab if necessary. DomUtils.simulateClick(link, { metaKey: KeyboardUtils.platform == "Mac", ctrlKey: KeyboardUtils.platform != "Mac" }) @@ -211,7 +211,7 @@ LinkHints = # showMarker: (linkMarker, matchingCharCount) -> linkMarker.style.display = "" - # TODO(philc): + # TODO(philc): for j in [0...linkMarker.childNodes.length] if (j < matchingCharCount) linkMarker.childNodes[j].classList.add("matchingCharacter") @@ -269,8 +269,8 @@ alphabetHints = digitsNeeded = Math.ceil(@logXOfBase(linkCount, linkHintCharacters.length)) # Short hints are the number of hints we can possibly show which are (digitsNeeded - 1) digits in length. shortHintCount = Math.floor( - (Math.pow(linkHintCharacters.length, digitsNeeded) - linkCount) / - linkHintCharacters.length) + (Math.pow(linkHintCharacters.length, digitsNeeded) - linkCount) / + linkHintCharacters.length) longHintCount = linkCount - shortHintCount hintStrings = [] @@ -299,8 +299,8 @@ alphabetHints = result # - # Converts a number like "8" into a hint string like "JK". This is used to sequentially generate all of - # the hint text. The hint string will be "padded with zeroes" to ensure its length is equal to numHintDigits. + # Converts a number like "8" into a hint string like "JK". This is used to sequentially generate all of the + # hint text. The hint string will be "padded with zeroes" to ensure its length is equal to numHintDigits. # numberToHintString: (number, numHintDigits, characterSet) -> base = characterSet.length diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index ab3b1fbf..adec11c5 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -295,8 +295,8 @@ extend window, chrome.extension.sendRequest({ handler: "openUrlInNewTab", url: url, selected: true }) copyCurrentUrl: -> - # TODO(ilya): When the following bug is fixed, revisit this approach of sending back to the background page - # to copy. + # TODO(ilya): When the following bug is fixed, revisit this approach of sending back to the background + # page to copy. # http://code.google.com/p/chromium/issues/detail?id=55188 chrome.extension.sendRequest { handler: "getCurrentTabUrl" }, (url) -> chrome.extension.sendRequest { handler: "copyToClipboard", data: url } @@ -895,9 +895,9 @@ window.showHelpDialog = (html, fid) -> this.dialogElement.style.maxHeight = window.innerHeight - 80 this.showAdvancedCommands(this.getShowAdvancedCommands()) - # + # # Advanced commands are hidden by default so they don't overwhelm new and casual users. - # + # toggleAdvancedCommands: (event) -> event.preventDefault() showAdvanced = VimiumHelpDialog.getShowAdvancedCommands() @@ -958,9 +958,9 @@ HUD = HUD.displayElement().style.display = "" showUpgradeNotification: (version) -> - HUD.upgradeNotificationElement().innerHTML = "Vimium has been updated to " + - "" + - version + ".x" + HUD.upgradeNotificationElement().innerHTML = "Vimium has been updated to + + #{version}.x" links = HUD.upgradeNotificationElement().getElementsByTagName("a") links[0].addEventListener("click", HUD.onUpdateLinkClicked, false) links[1].addEventListener "click", (event) -> diff --git a/content_scripts/vomnibar.coffee b/content_scripts/vomnibar.coffee index 9c1e7a31..1b2af1c4 100644 --- a/content_scripts/vomnibar.coffee +++ b/content_scripts/vomnibar.coffee @@ -172,7 +172,6 @@ class VomnibarUI @input = document.querySelector("#vomnibar input") @input.addEventListener "input", => @update() - console.log("@input:", @input); @completionList = document.querySelector("#vomnibar ul") @completionList.style.display = "none" diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index d4a4d379..e7fa9c2f 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -85,7 +85,8 @@ DomUtils = if (clientRect.width == 0 || clientRect.height == 0) for child in element.children computedStyle = window.getComputedStyle(child, null) - # Ignore child elements which are not floated and not absolutely positioned for parent elements with zero width/height + # Ignore child elements which are not floated and not absolutely positioned for parent elements with + # zero width/height if (computedStyle.getPropertyValue('float') == 'none' && computedStyle.getPropertyValue('position') != 'absolute') continue childClientRect = @getVisibleClientRect(child) diff --git a/test_harnesses/automated/automated.coffee b/test_harnesses/automated/automated.coffee index b5e5af35..283e8004 100644 --- a/test_harnesses/automated/automated.coffee +++ b/test_harnesses/automated/automated.coffee @@ -2,7 +2,7 @@ # Dispatching keyboard events via the DOM would require async tests, # which tend to be more complicated. Here we create mock events and # invoke the handlers directly. -# +# mockKeyboardEvent = (keyChar) -> event = {} event.charCode = (if keyCodes[keyChar] isnt undefined then keyCodes[keyChar] else keyChar.charCodeAt(0)) @@ -14,14 +14,14 @@ mockKeyboardEvent = (keyChar) -> # # Retrieve the hint markers as an array object. -# +# getHintMarkers = -> Array::slice.call document.getElementsByClassName("vimiumHintMarker"), 0 # # Generate tests that are common to both default and filtered # link hinting modes. -# +# createGeneralHintTests = (isFilteredMode) -> context "Link hints", @@ -124,7 +124,8 @@ context "Filtered link hints", context "Image hints", setup -> - testContent = "alt text" + "alt text" + "" + "" + testContent = "alt textalt text + " + "" document.getElementById("test-div").innerHTML = testContent LinkHints.activateMode() @@ -142,7 +143,10 @@ context "Filtered link hints", context "Input hints", setup -> - testContent = "" + "" + "" + "" + "" + testContent = " + + + " document.getElementById("test-div").innerHTML = testContent LinkHints.activateMode() @@ -161,7 +165,8 @@ context "Filtered link hints", context "Input focus", setup -> - testContent = "" + "" + "" + testContent = " + " document.getElementById("test-div").innerHTML = testContent tearDown -> @@ -182,7 +187,7 @@ Tests.outputMethod = (args...) -> # escape html newOutput = newOutput.replace(/&/g, "&").replace(//g, ">") # highlight the source of the error - newOutput = newOutput.replace(/\/([^:/]+):([0-9]+):([0-9]+)/, "/$1:$2:$3") + newOutput = newOutput.replace /\/([^:/]+):([0-9]+):([0-9]+)/, "/$1:$2:$3" document.getElementById("output-div").innerHTML += "
" + newOutput + "
" console.log.apply console, args -- cgit v1.2.3 From 7c7b64fd6d8254ac4d2d815ab1777f3643c65e90 Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Sun, 19 Aug 2012 10:18:51 +0200 Subject: More idiomatic CoffeeScript for Utils.isUrl --- lib/utils.coffee | 100 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index 598b631a..e52c4160 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -43,60 +43,68 @@ Utils = else partialUrl - # Tries to detect, whether :str is a valid URL. + # Tries to detect if :str is a valid URL. isUrl: (str) -> - # more or less RFC compliant URL host part parsing. This should be sufficient - # for our needs + # More or less RFC compliant URL host part parsing. This should be + # sufficient for our needs urlRegex = new RegExp( - '^(?:([^:]+)(?::([^:]+))?@)?' + # user:password (optional) => \1, \2 - '([^:]+|\\[[^\\]]+\\])' + # host name (IPv6 addresses in square brackets allowed) => \3 - '(?::(\\d+))?$' # port number (optional) => \4 + # user:password (optional) => \1, \2 + '^(?:([^:]+)(?::([^:]+))?@)?' + + # host name (IPv6 addresses in square brackets allowed) => \3 + '([^:]+|\\[[^\\]]+\\])' + + # port number (optional) => \4 + '(?::(\\d+))?$' ) - # these are all official ASCII TLDs that are longer than 3 characters - # (including the inofficial .onion TLD used by TOR) - longTlds = [ 'arpa', 'asia', 'coop', 'info', 'jobs', 'local', 'mobi', 'museum', 'name', 'onion' ] + # Official ASCII TLDs that are longer than 3 characters + longTlds = [ + 'arpa' + 'asia' + 'coop' + 'info' + 'jobs' + 'local' + 'mobi' + 'museum' + 'name' + 'onion' # Inofficial .onion TLD used by TOR + ] + + specialHostNames = ['localhost'] + + # Starts with a scheme: URL + return true if /^[a-z]{3,}:\/\//.test str + + # Must not contain spaces + return false if ' ' in str + + # Try to parse the URL into its meaningful parts. If matching fails we're + # pretty sure that we don't have some kind of URL here. + match = urlRegex.exec (str.split '/')[0] + return false unless match + hostName = match[3] - # are there more? - specialHostNames = [ 'localhost' ] + # Allow known special host names + return true if hostName in specialHostNames - # it starts with a scheme, so it's definitely an URL - if (/^[a-z]{3,}:\/\//.test(str)) - return true + # Allow IPv6 addresses (need to be wrapped in brackets as required by + # RFC). It is sufficient to check for a colon, as the regex wouldn't + # match colons in the host name unless it's an v6 address + return true if ':' in hostName - # spaces => definitely not a valid URL - if (str.indexOf(' ') >= 0) - return false + # At this point we have to make a decision. As a heuristic, we check + # if the input has dots in it. If yes, and if the last part could be a + # TLD, treat it as an URL + dottedParts = hostName.split '.' - # assuming that this is an URL, try to parse it into its meaningful parts. If matching fails, we're - # pretty sure that we don't have some kind of URL here. - match = urlRegex.exec(str.split('/')[0]) - if (!match) - return false - hostname = match[3] - - # allow known special host names - if (specialHostNames.indexOf(hostname) >= 0) - return true - - # allow IPv6 addresses (need to be wrapped in brackets, as required by RFC). It is sufficient to check - # for a colon here, as the regex wouldn't match colons in the host name unless it's an v6 address - if (hostname.indexOf(':') >= 0) - return true - - # at this point we have to make a decision. As a heuristic, we check if the input has dots in it. If - # yes, and if the last part could be a TLD, treat it as an URL. - dottedParts = hostname.split('.') - lastPart = dottedParts[dottedParts.length-1] - if (dottedParts.length > 1 && ((lastPart.length >= 2 && lastPart.length <= 3) || - longTlds.indexOf(lastPart) >= 0)) - return true - - # also allow IPv4 addresses - if (/^(\d{1,3}\.){3}\d{1,3}$/.test(hostname)) - return true - - # fallback: no URL + if dottedParts.length > 1 + lastPart = dottedParts.pop() + return true if 2 <= lastPart.length <= 3 or lastPart in longTlds + + # Allow IPv4 addresses + return true if /^(\d{1,3}\.){3}\d{1,3}$/.test hostName + + # Fallback: no URL return false # Creates a search URL from the given :query. -- cgit v1.2.3 From c9a11f75ac986e38a84487bc05ec573bb4ba13fb Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Tue, 4 Sep 2012 22:11:02 +0200 Subject: Tests for Utils.isUrl --- tests/utils_test.coffee | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/utils_test.coffee b/tests/utils_test.coffee index c5e5d002..f3810114 100644 --- a/tests/utils_test.coffee +++ b/tests/utils_test.coffee @@ -1,6 +1,28 @@ require "./test_helper.js" extend(global, require "../lib/utils.js") +context "isUrl", + should "accept valid URLs", -> + assert.isTrue Utils.isUrl "www.google.com" + assert.isTrue Utils.isUrl "www.bbc.co.uk" + assert.isTrue Utils.isUrl "yahoo.com" + assert.isTrue Utils.isUrl "nunames.nu" + assert.isTrue Utils.isUrl "user:pass@ftp.xyz.com/test" + + assert.isTrue Utils.isUrl "localhost/index.html" + assert.isTrue Utils.isUrl "127.0.0.1:8192/test.php" + + # IPv6 + assert.isTrue Utils.isUrl "[::]:9000" + + # Long TLDs + assert.isTrue Utils.isUrl "illinois.state.museum" + assert.isTrue Utils.isUrl "eqt5g4fuenphqinx.onion" + + should "reject invalid URLs", -> + assert.isFalse Utils.isUrl "a.x" + assert.isFalse Utils.isUrl "www-domain-tld" + context "convertToUrl", should "detect and clean up valid URLs", -> assert.equal "http://www.google.com/", Utils.convertToUrl("http://www.google.com/") -- cgit v1.2.3 From 164f7919ba625717a41948726c387b31a494aff9 Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Sun, 19 Aug 2012 10:32:20 +0200 Subject: More idiomatic CoffeeScript --- lib/utils.coffee | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index e52c4160..9ddab62f 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -1,6 +1,7 @@ Utils = getCurrentVersion: -> - # Chromium #15242 will make this XHR request to access the manifest unnecessary. + # Chromium #15242 will make this XHR request to access the manifest + # unnecessary manifestRequest = new XMLHttpRequest() manifestRequest.open("GET", chrome.extension.getURL("manifest.json"), false) manifestRequest.send(null) @@ -25,10 +26,9 @@ Utils = escapeHtml: (string) -> string.replace(//g, ">") # Generates a unique ID - createUniqueId: (-> + createUniqueId: do -> id = 0 - return -> id += 1 - )() + -> id += 1 hasChromePrefix: (url) -> chromePrefixes = [ 'about', 'view-source' ] @@ -109,21 +109,26 @@ Utils = # Creates a search URL from the given :query. createSearchUrl: (query) -> - # we need to escape explictely to encode characters like "+" correctly + # Escape explicitely to encode characters like "+" correctly "http://www.google.com/search?q=" + encodeURIComponent(query) - # Converts :string into a google search if it's not already a URL. - # We don't bother with escaping characters as Chrome will do that for us. + # Converts :string into a Google search if it's not already a URL. We + # don't bother with escaping characters as Chrome will do that for us. convertToUrl: (string) -> string = string.trim() - # special-case about:[url] and view-source:[url] - if Utils.hasChromePrefix string then string + + # Special-case about:[url] and view-source:[url] + if Utils.hasChromePrefix string + string + else if Utils.isUrl string + Utils.createFullUrl string else - if (Utils.isUrl(string)) then Utils.createFullUrl(string) else Utils.createSearchUrl(string) + Utils.createSearchUrl string -# This creates a new function out of an existing function, where the new function takes fewer arguments. -# This allows us to pass around functions instead of functions + a partial list of arguments. -Function.prototype.curry = -> +# This creates a new function out of an existing function, where the new +# function takes fewer arguments. This allows us to pass around functions +# instead of functions + a partial list of arguments. +Function::curry = -> fixedArguments = Array.copy(arguments) fn = this -> fn.apply(this, fixedArguments.concat(Array.copy(arguments))) @@ -132,19 +137,20 @@ Array.copy = (array) -> Array.prototype.slice.call(array, 0) String::startsWith = (str) -> @indexOf(str) == 0 -# A very simple method for defining a new class (constructor and methods) using a single hash. -# No support for inheritance is included because we really shouldn't need it. +# A very simple method for defining a new class (constructor and methods) +# using a single hash. No support for inheritance is included because we +# really shouldn't need it. # TODO(philc): remove this. Class = extend: (properties) -> newClass = -> - this.init.apply(this, arguments) if (this.init) + @init.apply(this, arguments) if @init null newClass.prototype = properties newClass.constructor = newClass newClass -globalRoot = if window? then window else global +globalRoot = window ? global globalRoot.extend = (hash1, hash2) -> for key of hash2 hash1[key] = hash2[key] -- cgit v1.2.3 From f9af8019e58a0a9cdc53c3cc96a7938376d089f5 Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Wed, 5 Sep 2012 09:18:13 +0200 Subject: Wrap lines at 110 chars --- lib/utils.coffee | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index 9ddab62f..c43a283c 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -45,8 +45,7 @@ Utils = # Tries to detect if :str is a valid URL. isUrl: (str) -> - # More or less RFC compliant URL host part parsing. This should be - # sufficient for our needs + # More or less RFC compliant URL host part parsing. This should be sufficient for our needs urlRegex = new RegExp( # user:password (optional) => \1, \2 '^(?:([^:]+)(?::([^:]+))?@)?' + @@ -78,8 +77,8 @@ Utils = # Must not contain spaces return false if ' ' in str - # Try to parse the URL into its meaningful parts. If matching fails we're - # pretty sure that we don't have some kind of URL here. + # Try to parse the URL into its meaningful parts. If matching fails we're pretty sure that we don't have + # some kind of URL here. match = urlRegex.exec (str.split '/')[0] return false unless match hostName = match[3] @@ -87,14 +86,12 @@ Utils = # Allow known special host names return true if hostName in specialHostNames - # Allow IPv6 addresses (need to be wrapped in brackets as required by - # RFC). It is sufficient to check for a colon, as the regex wouldn't - # match colons in the host name unless it's an v6 address + # Allow IPv6 addresses (need to be wrapped in brackets as required by RFC). It is sufficient to check for + # a colon, as the regex wouldn't match colons in the host name unless it's an v6 address return true if ':' in hostName - # At this point we have to make a decision. As a heuristic, we check - # if the input has dots in it. If yes, and if the last part could be a - # TLD, treat it as an URL + # At this point we have to make a decision. As a heuristic, we check if the input has dots in it. If yes, + # and if the last part could be a TLD, treat it as an URL dottedParts = hostName.split '.' if dottedParts.length > 1 @@ -112,8 +109,8 @@ Utils = # Escape explicitely to encode characters like "+" correctly "http://www.google.com/search?q=" + encodeURIComponent(query) - # Converts :string into a Google search if it's not already a URL. We - # don't bother with escaping characters as Chrome will do that for us. + # Converts :string into a Google search if it's not already a URL. We don't bother with escaping characters + # as Chrome will do that for us. convertToUrl: (string) -> string = string.trim() @@ -125,9 +122,8 @@ Utils = else Utils.createSearchUrl string -# This creates a new function out of an existing function, where the new -# function takes fewer arguments. This allows us to pass around functions -# instead of functions + a partial list of arguments. +# This creates a new function out of an existing function, where the new function takes fewer arguments. This +# allows us to pass around functions instead of functions + a partial list of arguments. Function::curry = -> fixedArguments = Array.copy(arguments) fn = this -- cgit v1.2.3 From edb1d3c8a5a5d9af7349cb87dda7a722b5210360 Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Wed, 5 Sep 2012 09:19:34 +0200 Subject: Tidy up if-statement --- lib/utils.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index c43a283c..597e5f6c 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -38,7 +38,7 @@ Utils = # Completes a partial URL (without scheme) createFullUrl: (partialUrl) -> - if (!/^[a-z]{3,}:\/\//.test(partialUrl)) + unless /^[a-z]{3,}:\/\//.test partialUrl "http://" + partialUrl else partialUrl -- cgit v1.2.3 From 5901cee9b9b7df3ad07e3cf895bf74fb65b276cc Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Thu, 6 Sep 2012 10:33:33 +0200 Subject: Align regex comments on the same line as the expression --- lib/utils.coffee | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index 597e5f6c..c5bfcc26 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -47,12 +47,9 @@ Utils = isUrl: (str) -> # More or less RFC compliant URL host part parsing. This should be sufficient for our needs urlRegex = new RegExp( - # user:password (optional) => \1, \2 - '^(?:([^:]+)(?::([^:]+))?@)?' + - # host name (IPv6 addresses in square brackets allowed) => \3 - '([^:]+|\\[[^\\]]+\\])' + - # port number (optional) => \4 - '(?::(\\d+))?$' + '^(?:([^:]+)(?::([^:]+))?@)?' + # user:password (optional) => \1, \2 + '([^:]+|\\[[^\\]]+\\])' + # host name (IPv6 addresses in square brackets allowed) => \3 + '(?::(\\d+))?$' # port number (optional) => \4 ) # Official ASCII TLDs that are longer than 3 characters -- cgit v1.2.3 From 560ceb7828a1911f6227f36180726094f8e7fbfa Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Thu, 6 Sep 2012 10:33:58 +0200 Subject: Lay out long TLDs on one line --- lib/utils.coffee | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index c5bfcc26..00dce5b4 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -52,19 +52,8 @@ Utils = '(?::(\\d+))?$' # port number (optional) => \4 ) - # Official ASCII TLDs that are longer than 3 characters - longTlds = [ - 'arpa' - 'asia' - 'coop' - 'info' - 'jobs' - 'local' - 'mobi' - 'museum' - 'name' - 'onion' # Inofficial .onion TLD used by TOR - ] + # Official ASCII TLDs that are longer than 3 characters + inofficial .onion TLD used by TOR + longTlds = ['arpa', 'asia', 'coop', 'info', 'jobs', 'local', 'mobi', 'museum', 'name', 'onion'] specialHostNames = ['localhost'] -- cgit v1.2.3 From cf5d98a8cadff87a54ffbf3fc55dbff4b12831bc Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Thu, 6 Sep 2012 10:35:53 +0200 Subject: Move instant tests to the top of the function: return early if possible --- lib/utils.coffee | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index 00dce5b4..49481445 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -45,6 +45,12 @@ Utils = # Tries to detect if :str is a valid URL. isUrl: (str) -> + # Starts with a scheme: URL + return true if /^[a-z]{3,}:\/\//.test str + + # Must not contain spaces + return false if ' ' in str + # More or less RFC compliant URL host part parsing. This should be sufficient for our needs urlRegex = new RegExp( '^(?:([^:]+)(?::([^:]+))?@)?' + # user:password (optional) => \1, \2 @@ -57,12 +63,6 @@ Utils = specialHostNames = ['localhost'] - # Starts with a scheme: URL - return true if /^[a-z]{3,}:\/\//.test str - - # Must not contain spaces - return false if ' ' in str - # Try to parse the URL into its meaningful parts. If matching fails we're pretty sure that we don't have # some kind of URL here. match = urlRegex.exec (str.split '/')[0] -- cgit v1.2.3 From 95d538d0458ae6206a791edead92cd0e4b1e3990 Mon Sep 17 00:00:00 2001 From: Johan Liesén Date: Thu, 6 Sep 2012 10:37:47 +0200 Subject: Remove unused Class --- lib/utils.coffee | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index 49481445..39279760 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -119,19 +119,6 @@ Array.copy = (array) -> Array.prototype.slice.call(array, 0) String::startsWith = (str) -> @indexOf(str) == 0 -# A very simple method for defining a new class (constructor and methods) -# using a single hash. No support for inheritance is included because we -# really shouldn't need it. -# TODO(philc): remove this. -Class = - extend: (properties) -> - newClass = -> - @init.apply(this, arguments) if @init - null - newClass.prototype = properties - newClass.constructor = newClass - newClass - globalRoot = window ? global globalRoot.extend = (hash1, hash2) -> for key of hash2 @@ -140,4 +127,3 @@ globalRoot.extend = (hash1, hash2) -> root = exports ? window root.Utils = Utils -root.Class = Class -- cgit v1.2.3 From e740ea52b646f1b7cd0ef13e15571b20adaa3bd8 Mon Sep 17 00:00:00 2001 From: Jez Ng Date: Thu, 6 Sep 2012 10:43:43 -0400 Subject: Touching up. --- lib/utils.coffee | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index 39279760..a2221ba4 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -1,7 +1,6 @@ Utils = getCurrentVersion: -> - # Chromium #15242 will make this XHR request to access the manifest - # unnecessary + # Chromium #15242 will make this XHR request to access the manifest unnecessary manifestRequest = new XMLHttpRequest() manifestRequest.open("GET", chrome.extension.getURL("manifest.json"), false) manifestRequest.send(null) @@ -54,8 +53,8 @@ Utils = # More or less RFC compliant URL host part parsing. This should be sufficient for our needs urlRegex = new RegExp( '^(?:([^:]+)(?::([^:]+))?@)?' + # user:password (optional) => \1, \2 - '([^:]+|\\[[^\\]]+\\])' + # host name (IPv6 addresses in square brackets allowed) => \3 - '(?::(\\d+))?$' # port number (optional) => \4 + '([^:]+|\\[[^\\]]+\\])' + # host name (IPv6 addresses in square brackets allowed) => \3 + '(?::(\\d+))?$' # port number (optional) => \4 ) # Official ASCII TLDs that are longer than 3 characters + inofficial .onion TLD used by TOR @@ -92,7 +91,7 @@ Utils = # Creates a search URL from the given :query. createSearchUrl: (query) -> - # Escape explicitely to encode characters like "+" correctly + # Escape explicitly to encode characters like "+" correctly "http://www.google.com/search?q=" + encodeURIComponent(query) # Converts :string into a Google search if it's not already a URL. We don't bother with escaping characters -- cgit v1.2.3