From feda3bc8e6419f0339614e04a22754a2413c5ab8 Mon Sep 17 00:00:00 2001 From: John Beard Date: Thu, 1 Feb 2018 14:29:40 +0000 Subject: [PATCH 1/4] Add URL interpolation function and tests This includes logic to automatically percent-encode the query if it occurs in the the query string of the URL pattern. If this automatic logic is not enough in future, the same function can be extended with an explicit encoding flag as needed. --- src/url_util.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/url_util.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/url_util.test.ts b/src/url_util.test.ts index 717b22c2..88c915a8 100644 --- a/src/url_util.test.ts +++ b/src/url_util.test.ts @@ -306,6 +306,45 @@ function test_url_graft_path() { } } +function test_url_query_interpolation() { + + let cases = [ + [ + // not percent-encoded and appended + "http://example.com", + "a/query", + "http://example.com/a/query" + ], + [ + // not percent-encoded and interpolated + "http://example.com/%s/path", + "a/query", + "http://example.com/a/query/path" + ], + [ + // percent-encoded and appended + "http://example.com/?query=", + "a/query", + "http://example.com/?query=a%2Fquery" + ], + [ + // percent-encoded and interpolated + "http://example.com/?query=%s&q2=v2", + "a/query", + "http://example.com/?query=a%2Fquery&q2=v2" + ] + ] + + for (let [url, qy, exp_res] of cases) { + + let modified = UrlUtil.interpolateSearchItem(new URL(url), qy) + + test(`interpolate ${qy} into ${url} --> ${exp_res}`, + () => expect(modified.href).toEqual(exp_res) + ) + } +} + test_increment() test_root() test_parent() @@ -313,3 +352,4 @@ test_download_filename() test_query_delete() test_query_replace() test_url_graft_path() +test_url_query_interpolation() diff --git a/src/url_util.ts b/src/url_util.ts index db582bc4..9c7fca23 100644 --- a/src/url_util.ts +++ b/src/url_util.ts @@ -341,3 +341,40 @@ export function graftUrlPath(url: URL, newTail: string, level: number) { return newUrl } + +/** + * Interpolates a query or other search item into a URL + * + * If the URL pattern contains "%s", the query is interpolated there. If not, + * it is appended to the end of the pattern. + * + * If the interpolation point is in the query string of the URL, it is + * percent encoded, otherwise it is is inserted verbatim. + * + * @param urlPattern a URL to interpolate/append a query to + * @param query a query to interpolate/append into the URL + * + * @return the URL with the query encoded (if needed) and + * inserted at the relevant point + */ +export function interpolateSearchItem(urlPattern: URL, query: string): URL { + const hasInterpolationPoint = urlPattern.href.includes("%s") + + // percent-encode if theres a %s in the query string, or if we're apppending + // and there's a query string + if (hasInterpolationPoint && (urlPattern.search.includes("%s")) + || urlPattern.search !== "") { + query = encodeURIComponent(query) + } + + let newUrl + + // replace or append as needed + if (hasInterpolationPoint) { + newUrl = new URL(urlPattern.href.replace("%s", query)) + } else { + newUrl = new URL(urlPattern.href + query) + } + + return newUrl +} From e50119d4669bfd4cbd869f7c7fc160f00baebda0 Mon Sep 17 00:00:00 2001 From: John Beard Date: Thu, 1 Feb 2018 16:42:24 +0000 Subject: [PATCH 2/4] Use new UrlUtil interpolate function in excmd Document user-facing interpolation behaviour on excmd `searchsetkeyword`. --- src/excmds.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/excmds.ts b/src/excmds.ts index c9b57f49..091efd31 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -86,8 +86,8 @@ import {ModeName} from './state' import * as keydown from "./keydown_background" //#background_helper import {activeTab, activeTabId, firefoxVersionAtLeast} from './lib/webext' -//#content_helper import * as UrlUtil from "./url_util" + //#background_helper import * as CommandLineBackground from './commandline_background' //#content_helper @@ -122,12 +122,7 @@ function searchURL(provider: string, query: string) { throw new TypeError(`Unknown provider: '${provider}'`) } - // build search URL: either replace "%s" in URL with query or append query to URL - const url = searchurlprovider.includes("%s") ? - new URL(searchurlprovider.replace("%s", encodeURIComponent(query))) : - new URL(searchurlprovider + encodeURIComponent(query)) - - return url + return UrlUtil.interpolateSearchItem(new URL(searchurlprovider), query) } /** If maybeURI doesn't have a schema, affix http:// */ @@ -1285,7 +1280,15 @@ export function bind(key: string, ...bindarr: string[]){ config.set("nmaps",exstring,key) } -/** Set a search engine keyword for use with *open or `set searchengine` */ +/** + * Set a search engine keyword for use with *open or `set searchengine` + * + * @param keyword the keyword to use for this search (e.g. 'esa') + * @param url the URL to interpolate the query into. If %s is found in + * the URL, the query is inserted there, else it is appended. + * If the insertion point is in the "query string" of the URL, + * the query is percent-encoded, else it is verbatim. + * */ //#background export function searchsetkeyword(keyword: string, url: string){ config.set("searchurls",forceURI(url),keyword) From c1ab1ebae92674b0bf247807d93375533b2b4fd9 Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Fri, 2 Feb 2018 13:05:40 +0000 Subject: [PATCH 3/4] Remove superfluous variable --- src/url_util.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/url_util.ts b/src/url_util.ts index 9c7fca23..07377b08 100644 --- a/src/url_util.ts +++ b/src/url_util.ts @@ -367,14 +367,10 @@ export function interpolateSearchItem(urlPattern: URL, query: string): URL { query = encodeURIComponent(query) } - let newUrl - // replace or append as needed if (hasInterpolationPoint) { - newUrl = new URL(urlPattern.href.replace("%s", query)) + return new URL(urlPattern.href.replace("%s", query)) } else { - newUrl = new URL(urlPattern.href + query) + return new URL(urlPattern.href + query) } - - return newUrl } From 7a22bc8c2c4b91b43f1a3640cc44ec19cf6d5df7 Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Fri, 2 Feb 2018 13:17:36 +0000 Subject: [PATCH 4/4] Stop testing on pre-commit --- hooks/pre-commit | 3 --- 1 file changed, 3 deletions(-) delete mode 100755 hooks/pre-commit diff --git a/hooks/pre-commit b/hooks/pre-commit deleted file mode 100755 index b41801ed..00000000 --- a/hooks/pre-commit +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -npm test