1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-25 09:38:06 +02:00

[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

@ -18,7 +18,6 @@ import {
BaseException,
objectSize,
stringToPDFString,
stringToUTF8String,
warn,
} from "../shared/util.js";
import { Dict, isName, isRef, isStream, RefSet } from "./primitives.js";
@ -452,21 +451,6 @@ function validateCSSFont(cssFontInfo) {
return true;
}
// Let URLs beginning with 'www.' default to using the 'http://' protocol.
function addDefaultProtocolToUrl(url) {
return url.startsWith("www.") ? `http://${url}` : 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 Bugzilla 1122280.
function tryConvertUrlEncoding(url) {
try {
return stringToUTF8String(url);
} catch (e) {
return url;
}
}
function recoverJsURL(str) {
// Attempt to recover valid URLs from `JS` entries with certain
// white-listed formats:
@ -496,7 +480,6 @@ function recoverJsURL(str) {
}
export {
addDefaultProtocolToUrl,
collectActions,
encodeToXmlString,
escapePDFName,
@ -513,7 +496,6 @@ export {
readUint32,
recoverJsURL,
toRomanNumerals,
tryConvertUrlEncoding,
validateCSSFont,
XRefEntryException,
XRefParseException,