From dce815df7bf48598c6b1f2f069c5954af6ef1540 Mon Sep 17 00:00:00 2001 From: John Foerch Date: Fri, 17 Feb 2012 13:57:42 -0500 Subject: [PATCH] possibly_valid_url: allow spaces after first '/' This has the practical effect that read_url input with a space in the path part of an url is still considered url-like. Example usage: "translate.google.com/#en|de|hello world" --- modules/utils.js | 13 +++++++------ tests/simple/utils.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/modules/utils.js b/modules/utils.js index df1bfac..ceea810 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -525,13 +525,14 @@ function url_path_trim (url) { return uri.spec; } -/* possibly_valid_url returns true if the string might be a valid - * thing to pass to nsIWebNavigation.loadURI. Currently just checks - * that there's no whitespace in the middle and that it's not entirely - * whitespace. +/** + * possibly_valid_url returns true if its argument is an url-like string, + * meaning likely a valid thing to pass to nsIWebNavigation.loadURI. */ -function possibly_valid_url (url) { - return !(/\S\s+\S/.test(url)) && !(/^\s*$/.test(url)); +function possibly_valid_url (str) { + // no inner space before first / + return /^\s*[^\/\s]*(\/|\s*$)/.test(str) + && !(/^\s*$/.test(str)); } diff --git a/tests/simple/utils.js b/tests/simple/utils.js index 4efdd08..d94bc21 100644 --- a/tests/simple/utils.js +++ b/tests/simple/utils.js @@ -54,3 +54,39 @@ walnut_run({ assert_objects_equal(make_array(null), [null]); } }); + +walnut_run({ + test_possibly_valid_url_1: function () { + assert_not(possibly_valid_url("")); + }, + test_possibly_valid_url_2: function () { + assert_not(possibly_valid_url(" ")); + }, + test_possibly_valid_url_3: function () { + assert(possibly_valid_url("example")); + }, + test_possibly_valid_url_4: function () { + assert(possibly_valid_url(" example ")); + }, + test_possibly_valid_url_5: function () { + assert_not(possibly_valid_url("example foo")); + }, + test_possibly_valid_url_6: function () { + assert(possibly_valid_url("example/ foo")); + }, + test_possibly_valid_url_7: function () { + assert_not(possibly_valid_url("example /foo")); + }, + test_possibly_valid_url_8: function () { + assert(possibly_valid_url("/example foo")); + }, + test_possibly_valid_url_9: function () { + assert(possibly_valid_url(" /example foo")); + }, + test_possibly_valid_url_10: function () { + assert(possibly_valid_url(" ex/ample foo")); + }, + test_possibly_valid_url_11: function () { + assert(possibly_valid_url("/")); + } +}); -- 2.11.4.GIT