diff options
author | janekptacijarabaci <janekptacijarabaci@seznam.cz> | 2018-04-15 11:14:20 +0200 |
---|---|---|
committer | janekptacijarabaci <janekptacijarabaci@seznam.cz> | 2018-04-15 11:14:20 +0200 |
commit | 54cd77a551a0576f7593d24dfc9d82abcf99154f (patch) | |
tree | c9d107853ec9fb16522a15b4148767f30399625f | |
parent | 451cbfc0393ee0f3b8d18f856e6c8ad88d57b172 (diff) | |
download | UXP-54cd77a551a0576f7593d24dfc9d82abcf99154f.tar UXP-54cd77a551a0576f7593d24dfc9d82abcf99154f.tar.gz UXP-54cd77a551a0576f7593d24dfc9d82abcf99154f.tar.lz UXP-54cd77a551a0576f7593d24dfc9d82abcf99154f.tar.xz UXP-54cd77a551a0576f7593d24dfc9d82abcf99154f.zip |
Bug 1100294 - Turn getShortcutOrURIAndPostData() into a task that uses the new keywords API
Issue #121
-rw-r--r-- | application/palemoon/base/content/browser.js | 169 | ||||
-rw-r--r-- | application/palemoon/base/content/urlbarBindings.xml | 2 |
2 files changed, 99 insertions, 72 deletions
diff --git a/application/palemoon/base/content/browser.js b/application/palemoon/base/content/browser.js index be7993961..7a85b18e9 100644 --- a/application/palemoon/base/content/browser.js +++ b/application/palemoon/base/content/browser.js @@ -1899,53 +1899,81 @@ function loadURI(uri, referrer, postData, allowThirdPartyFixup) { } catch (e) {} } -function getShortcutOrURIAndPostData(aURL, aCallback) { - let mayInheritPrincipal = false; - let postData = null; - let shortcutURL = null; - let keyword = aURL; - let param = ""; - - // XXX Bug 1100294 will remove this little hack by using an async version of - // PlacesUtils.getURLAndPostDataForKeyword(). For now we simulate an async - // execution with at least a setTimeout(fn, 0). - let originalCallback = aCallback; - aCallback = data => setTimeout(() => originalCallback(data)); - - let offset = aURL.indexOf(" "); - if (offset > 0) { - keyword = aURL.substr(0, offset); - param = aURL.substr(offset + 1); +/** + * Given a urlbar value, discerns between URIs, keywords and aliases. + * + * @param url + * The urlbar value. + * @param callback (optional, deprecated) + * The callback function invoked when done. This parameter is + * deprecated, please use the Promise that is returned. + * + * @return Promise<{ postData, url, mayInheritPrincipal }> + */ +function getShortcutOrURIAndPostData(url, callback = null) { + if (callback) { + Deprecated.warning("Please use the Promise returned by " + + "getShortcutOrURIAndPostData() instead of passing a " + + "callback", + "https://bugzilla.mozilla.org/show_bug.cgi?id=1100294"); } - let engine = Services.search.getEngineByAlias(keyword); - if (engine) { - let submission = engine.getSubmission(param); - postData = submission.postData; - aCallback({ postData: submission.postData, url: submission.uri.spec, - mayInheritPrincipal: mayInheritPrincipal }); - return; - } + return Task.spawn(function* () { + let mayInheritPrincipal = false; + let postData = null; + let shortcutURL = null; + let keyword = url; + let param = ""; - [shortcutURL, postData] = - PlacesUtils.getURLAndPostDataForKeyword(keyword); + let offset = url.indexOf(" "); + if (offset > 0) { + keyword = url.substr(0, offset); + param = url.substr(offset + 1); + } - if (!shortcutURL) { - aCallback({ postData: postData, url: aURL, - mayInheritPrincipal: mayInheritPrincipal }); - return; - } + let engine = Services.search.getEngineByAlias(keyword); + if (engine) { + let submission = engine.getSubmission(param, null, "keyword"); + postData = submission.postData; + return { postData: submission.postData, url: submission.uri.spec, + mayInheritPrincipal }; + } + + let entry = yield PlacesUtils.keywords.fetch(keyword); + if (entry) { + shortcutURL = entry.url.href; + postData = entry.postData; + } + + if (!shortcutURL) { + return { postData, url, mayInheritPrincipal }; + } + + let escapedPostData = ""; + if (postData) + escapedPostData = unescape(postData); - let escapedPostData = ""; - if (postData) - escapedPostData = unescape(postData); + if (/%s/i.test(shortcutURL) || /%s/i.test(escapedPostData)) { + let charset = ""; + const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/; + let matches = shortcutURL.match(re); - if (/%s/i.test(shortcutURL) || /%s/i.test(escapedPostData)) { - let charset = ""; - const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/; - let matches = shortcutURL.match(re); + if (matches) { + [, shortcutURL, charset] = matches; + } else { + let uri; + try { + // makeURI() throws if URI is invalid. + uri = makeURI(shortcutURL); + } catch (ex) {} + + if (uri) { + // Try to get the saved character-set. + // Will return an empty string if character-set is not found. + charset = yield PlacesUtils.getCharsetForURI(uri); + } + } - let continueOperation = function () { // encodeURIComponent produces UTF-8, and cannot be used for other charsets. // escape() works in those cases, but it doesn't uri-encode +, @, and /. // Therefore we need to manually replace these ASCII characters by their @@ -1968,40 +1996,29 @@ function getShortcutOrURIAndPostData(aURL, aCallback) { // document's principal. mayInheritPrincipal = true; - aCallback({ postData: postData, url: shortcutURL, - mayInheritPrincipal: mayInheritPrincipal }); + return { postData, url: shortcutURL, mayInheritPrincipal }; } - if (matches) { - [, shortcutURL, charset] = matches; - continueOperation(); - } else { - // Try to get the saved character-set. - // makeURI throws if URI is invalid. - // Will return an empty string if character-set is not found. - try { - PlacesUtils.getCharsetForURI(makeURI(shortcutURL)) - .then(c => { charset = c; continueOperation(); }); - } catch (ex) { - continueOperation(); - } + if (param) { + // This keyword doesn't take a parameter, but one was provided. Just return + // the original URL. + postData = null; + + return { postData, url, mayInheritPrincipal }; } - } - else if (param) { - // This keyword doesn't take a parameter, but one was provided. Just return - // the original URL. - postData = null; - aCallback({ postData: postData, url: aURL, - mayInheritPrincipal: mayInheritPrincipal }); - } else { // This URL came from a bookmark, so it's safe to let it inherit the current // document's principal. mayInheritPrincipal = true; - aCallback({ postData: postData, url: shortcutURL, - mayInheritPrincipal: mayInheritPrincipal }); - } + return { postData, url: shortcutURL, mayInheritPrincipal }; + }).then(data => { + if (callback) { + callback(data); + } + + return data; + }); } function getPostDataStream(aStringData, aKeyword, aEncKeyword, aType) { @@ -2746,7 +2763,7 @@ var newTabButtonObserver = { onDrop: function (aEvent) { let url = browserDragAndDrop.drop(aEvent, { }); - getShortcutOrURIAndPostData(url, data => { + getShortcutOrURIAndPostData(url).then(data => { if (data.url) { // allow third-party services to fixup this URL openNewTabWith(data.url, null, data.postData, aEvent, true); @@ -2766,7 +2783,7 @@ var newWindowButtonObserver = { onDrop: function (aEvent) { let url = browserDragAndDrop.drop(aEvent, { }); - getShortcutOrURIAndPostData(url, data => { + getShortcutOrURIAndPostData(url).then(data => { if (data.url) { // allow third-party services to fixup this URL openNewWindowWith(data.url, null, data.postData, true); @@ -5067,7 +5084,7 @@ function middleMousePaste(event) { lastLocationChange = gBrowser.selectedBrowser.lastLocationChange; } - getShortcutOrURIAndPostData(clipboard, data => { + getShortcutOrURIAndPostData(clipboard).then(data => { try { makeURI(data.url); } catch (ex) { @@ -5094,11 +5111,21 @@ function middleMousePaste(event) { event.stopPropagation(); } -function handleDroppedLink(event, url, name) +// handleDroppedLink has the following 2 overloads: +// handleDroppedLink(event, url, name) +// handleDroppedLink(event, links) +function handleDroppedLink(event, urlOrLinks, name) { + let links; + if (Array.isArray(urlOrLinks)) { + links = urlOrLinks; + } else { + links = [{ url: urlOrLinks, name, type: "" }]; + } + let lastLocationChange = gBrowser.selectedBrowser.lastLocationChange; - getShortcutOrURIAndPostData(url, data => { + getShortcutOrURIAndPostData(url).then(data => { if (data.url && lastLocationChange == gBrowser.selectedBrowser.lastLocationChange) loadURI(data.url, null, data.postData, false); diff --git a/application/palemoon/base/content/urlbarBindings.xml b/application/palemoon/base/content/urlbarBindings.xml index ec9feb22d..e859fe804 100644 --- a/application/palemoon/base/content/urlbarBindings.xml +++ b/application/palemoon/base/content/urlbarBindings.xml @@ -418,7 +418,7 @@ } } - getShortcutOrURIAndPostData(url, data => { + getShortcutOrURIAndPostData(url).then(data => { aCallback([data.url, data.postData, data.mayInheritPrincipal]); }); ]]></body> |