[api-minor] Move the addDefaultProtocolToUrl/tryConvertUrlEncoding functionality into the createValidAbsoluteUrl function

Having recently worked with, and reviewed patches touching, this code it seemed that it's probably not a bad idea to move that functionality into `createValidAbsoluteUrl` as new options instead.

For the `addDefaultProtocolToUrl` functionality in particular, the existing helper function was not only moved but slightly improved as well. Looking at the code, I realized that there's a small risk that it would incorrectly match a *relative* URL-string too.

With these changes, the `createValidAbsoluteUrl` call-sites in the `src/core/`-code can be simplified a little bit.

*Please note:* This patch may, indirectly, change the format of the `unsafeUrl`-property returned with relevant Annotations and OutlineItems; hence the `api-minor` tag.
However, I'd argue that it's actually more correct this way since the whole purpose of `unsafeUrl` is/was to return the URL data as-is without any parsing done.
This commit is contained in:
Jonas Jenwald 2021-09-26 14:16:28 +02:00
parent 3b3c487bed
commit e6e04694f4
5 changed files with 42 additions and 48 deletions

View file

@ -448,14 +448,35 @@ function _isValidProtocol(url) {
* Attempts to create a valid absolute URL.
*
* @param {URL|string} url - An absolute, or relative, URL.
* @param {URL|string} baseUrl - An absolute URL.
* @param {URL|string} [baseUrl] - An absolute URL.
* @param {Object} [options]
* @returns Either a valid {URL}, or `null` otherwise.
*/
function createValidAbsoluteUrl(url, baseUrl) {
function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
if (!url) {
return null;
}
try {
if (options && typeof url === "string") {
// Let URLs beginning with "www." default to using the "http://" protocol.
if (options.addDefaultProtocol && url.startsWith("www.")) {
const dots = url.match(/\./g);
// Avoid accidentally matching a *relative* URL pointing to a file named
// e.g. "www.pdf" or similar.
if (dots && dots.length >= 2) {
url = `http://${url}`;
}
}
// According to ISO 32000-1:2008, section 12.6.4.7, URIs should be encoded
// in 7-bit ASCII. Some bad PDFs use UTF-8 encoding; see bug 1122280.
if (options.tryConvertEncoding) {
try {
url = stringToUTF8String(url);
} catch (ex) {}
}
}
const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
if (_isValidProtocol(absoluteUrl)) {
return absoluteUrl;