Compare commits

..

8 Commits

Author SHA1 Message Date
Tim van der Meij
a40bf37184
Merge pull request #19866 from Snuffleupagus/updateUrlHash
Avoid (most) string parsing when removing/replacing the hash property of a URL
2025-04-26 12:21:38 +02:00
Jonas Jenwald
c2d88540b4
Merge pull request #19868 from Snuffleupagus/MathClamp-4
Use the `MathClamp` helper function more in the `web/` folder (PR 19617 follow-up)
2025-04-26 12:07:30 +02:00
Tim van der Meij
6891602e7f
Merge pull request #19858 from luckymore/master
Tweak the Node.js version listed in "engines", to ensure that process.getBuiltinModule is available
2025-04-26 11:45:27 +02:00
Tim van der Meij
2fc325e4f8
Merge pull request #19814 from loganrosen/beginner-bug-readme
Fix beginner bugs link in README
2025-04-26 11:29:41 +02:00
Jonas Jenwald
af3d13c22d Use the MathClamp helper function more in the web/ folder (PR 19617 follow-up)
There's one more spot that I missed in PR 19617.
2025-04-26 11:09:52 +02:00
Jonas Jenwald
abc9522886 Avoid (most) string parsing when removing/replacing the hash property of a URL 2025-04-25 23:13:05 +02:00
zhangningning
940b4657fc Tweak the Node.js version listed in "engines", to ensure that process.getBuiltinModule is available 2025-04-24 15:04:15 +08:00
Logan Rosen
ba7c34ff56 Fix beginner bugs link in README 2025-04-13 18:57:56 -04:00
16 changed files with 66 additions and 16 deletions

View File

@ -14,7 +14,7 @@ get involved, visit:
+ [Issue Reporting Guide](https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md) + [Issue Reporting Guide](https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md)
+ [Code Contribution Guide](https://github.com/mozilla/pdf.js/wiki/Contributing) + [Code Contribution Guide](https://github.com/mozilla/pdf.js/wiki/Contributing)
+ [Frequently Asked Questions](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions) + [Frequently Asked Questions](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions)
+ [Good Beginner Bugs](https://github.com/mozilla/pdf.js/issues?direction=desc&labels=good-beginner-bug&page=1&sort=created&state=open) + [Good Beginner Bugs](https://github.com/mozilla/pdf.js/issues?q=is%3Aissue%20state%3Aopen%20label%3Agood-beginner-bug)
+ [Projects](https://github.com/mozilla/pdf.js/projects) + [Projects](https://github.com/mozilla/pdf.js/projects)
Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mozilla.org) for questions or guidance. Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mozilla.org) for questions or guidance.

View File

@ -46,6 +46,7 @@ limitations under the License.
} }
var scheme = url.slice(0, schemeIndex).toLowerCase(); var scheme = url.slice(0, schemeIndex).toLowerCase();
if (schemes.includes(scheme)) { if (schemes.includes(scheme)) {
// NOTE: We cannot use the `updateUrlHash` function in this context.
url = url.split("#", 1)[0]; url = url.split("#", 1)[0];
if (url.charAt(schemeIndex) === ":") { if (url.charAt(schemeIndex) === ":") {
url = encodeURIComponent(url); url = encodeURIComponent(url);

View File

@ -2315,7 +2315,7 @@ function packageJson() {
url: `git+${DIST_GIT_URL}`, url: `git+${DIST_GIT_URL}`,
}, },
engines: { engines: {
node: ">=20.16.0", node: ">=20.16.0 || >=22.3.0",
}, },
scripts: {}, scripts: {},
}; };

2
package-lock.json generated
View File

@ -63,7 +63,7 @@
"yargs": "^17.7.2" "yargs": "^17.7.2"
}, },
"engines": { "engines": {
"node": ">=20.16.0" "node": ">=20.16.0 || >=22.3.0"
} }
}, },
"node_modules/@aashutoshrathi/word-wrap": { "node_modules/@aashutoshrathi/word-wrap": {

View File

@ -62,7 +62,7 @@
"url": "git://github.com/mozilla/pdf.js.git" "url": "git://github.com/mozilla/pdf.js.git"
}, },
"engines": { "engines": {
"node": ">=20.16.0" "node": ">=20.16.0 || >=22.3.0"
}, },
"license": "Apache-2.0" "license": "Apache-2.0"
} }

View File

@ -1607,6 +1607,9 @@ class Catalog {
// NOTE: the destination is relative to the *remote* document. // NOTE: the destination is relative to the *remote* document.
const remoteDest = fetchRemoteDest(action); const remoteDest = fetchRemoteDest(action);
if (remoteDest && typeof url === "string") { if (remoteDest && typeof url === "string") {
// NOTE: We don't use the `updateUrlHash` function here, since
// the `createValidAbsoluteUrl` function (see below) already
// handles parsing and validation of the final URL.
url = /* baseUrl = */ url.split("#", 1)[0] + "#" + remoteDest; url = /* baseUrl = */ url.split("#", 1)[0] + "#" + remoteDest;
} }
// The 'NewWindow' property, equal to `LinkTarget.BLANK`. // The 'NewWindow' property, equal to `LinkTarget.BLANK`.

View File

@ -14,7 +14,7 @@
*/ */
import { getRGB, isDataScheme, SVG_NS } from "./display_utils.js"; import { getRGB, isDataScheme, SVG_NS } from "./display_utils.js";
import { unreachable, Util, warn } from "../shared/util.js"; import { unreachable, updateUrlHash, Util, warn } from "../shared/util.js";
class BaseFilterFactory { class BaseFilterFactory {
constructor() { constructor() {
@ -143,7 +143,7 @@ class DOMFilterFactory extends BaseFilterFactory {
if (isDataScheme(url)) { if (isDataScheme(url)) {
warn('#createUrl: ignore "data:"-URL for performance reasons.'); warn('#createUrl: ignore "data:"-URL for performance reasons.');
} else { } else {
this.#baseUrl = url.split("#", 1)[0]; this.#baseUrl = updateUrlHash(url, "");
} }
} }
} }

View File

@ -40,6 +40,7 @@ import {
PermissionFlag, PermissionFlag,
ResponseException, ResponseException,
shadow, shadow,
updateUrlHash,
Util, Util,
VerbosityLevel, VerbosityLevel,
} from "./shared/util.js"; } from "./shared/util.js";
@ -140,6 +141,7 @@ globalThis.pdfjsLib = {
SupportedImageMimeTypes, SupportedImageMimeTypes,
TextLayer, TextLayer,
TouchManager, TouchManager,
updateUrlHash,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,
@ -193,6 +195,7 @@ export {
SupportedImageMimeTypes, SupportedImageMimeTypes,
TextLayer, TextLayer,
TouchManager, TouchManager,
updateUrlHash,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,

View File

@ -445,6 +445,28 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
return _isValidProtocol(absoluteUrl) ? absoluteUrl : null; return _isValidProtocol(absoluteUrl) ? absoluteUrl : null;
} }
/**
* Remove, or replace, the hash property of the URL.
*
* @param {URL|string} url - The absolute, or relative, URL.
* @param {string} hash - The hash property (use an empty string to remove it).
* @param {boolean} [allowRel] - Allow relative URLs.
* @returns {string} The resulting URL string.
*/
function updateUrlHash(url, hash, allowRel = false) {
const res = URL.parse(url);
if (res) {
res.hash = hash;
return res.href;
}
// Support well-formed relative URLs, necessary for `web/app.js` in GENERIC
// builds, by optionally falling back to string parsing.
if (allowRel && createValidAbsoluteUrl(url, "http://example.com")) {
return url.split("#", 1)[0] + `${hash ? `#${hash}` : ""}`;
}
return "";
}
function shadow(obj, prop, value, nonSerializable = false) { function shadow(obj, prop, value, nonSerializable = false) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert( assert(
@ -1319,6 +1341,7 @@ export {
toHexUtil, toHexUtil,
UnknownErrorException, UnknownErrorException,
unreachable, unreachable,
updateUrlHash,
utf8StringToString, utf8StringToString,
Util, Util,
VerbosityLevel, VerbosityLevel,

View File

@ -31,6 +31,7 @@ import {
PermissionFlag, PermissionFlag,
ResponseException, ResponseException,
shadow, shadow,
updateUrlHash,
Util, Util,
VerbosityLevel, VerbosityLevel,
} from "../../src/shared/util.js"; } from "../../src/shared/util.js";
@ -117,6 +118,7 @@ const expectedAPI = Object.freeze({
SupportedImageMimeTypes, SupportedImageMimeTypes,
TextLayer, TextLayer,
TouchManager, TouchManager,
updateUrlHash,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,

View File

@ -57,6 +57,7 @@ import {
shadow, shadow,
stopEvent, stopEvent,
TouchManager, TouchManager,
updateUrlHash,
version, version,
} from "pdfjs-lib"; } from "pdfjs-lib";
import { AppOptions, OptionKind } from "./app_options.js"; import { AppOptions, OptionKind } from "./app_options.js";
@ -943,10 +944,18 @@ const PDFViewerApplication = {
setTitleUsingUrl(url = "", downloadUrl = null) { setTitleUsingUrl(url = "", downloadUrl = null) {
this.url = url; this.url = url;
this.baseUrl = url.split("#", 1)[0]; this.baseUrl =
typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")
? updateUrlHash(url, "", /* allowRel = */ true)
: updateUrlHash(url, "");
if (downloadUrl) { if (downloadUrl) {
this._downloadUrl = this._downloadUrl =
downloadUrl === url ? this.baseUrl : downloadUrl.split("#", 1)[0]; // eslint-disable-next-line no-nested-ternary
downloadUrl === url
? this.baseUrl
: typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")
? updateUrlHash(downloadUrl, "", /* allowRel = */ true)
: updateUrlHash(downloadUrl, "");
} }
if (isDataScheme(url)) { if (isDataScheme(url)) {
this._hideViewBookmark(); this._hideViewBookmark();
@ -1309,7 +1318,7 @@ const PDFViewerApplication = {
this.secondaryToolbar?.setPagesCount(pdfDocument.numPages); this.secondaryToolbar?.setPagesCount(pdfDocument.numPages);
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
const baseUrl = location.href.split("#", 1)[0]; const baseUrl = updateUrlHash(location, "");
// Ignore "data:"-URLs for performance reasons, even though it may cause // Ignore "data:"-URLs for performance reasons, even though it may cause
// internal links to not work perfectly in all cases (see bug 1803050). // internal links to not work perfectly in all cases (see bug 1803050).
this.pdfLinkService.setDocument( this.pdfLinkService.setDocument(

View File

@ -393,7 +393,13 @@ const defaultOptions = {
}, },
docBaseUrl: { docBaseUrl: {
/** @type {string} */ /** @type {string} */
value: typeof PDFJSDev === "undefined" ? document.URL.split("#", 1)[0] : "", value:
typeof PDFJSDev === "undefined"
? // NOTE: We cannot use the `updateUrlHash` function here, because of
// the default preferences generation (see `gulpfile.mjs`).
// However, the following line is *only* used in development mode.
document.URL.split("#", 1)[0]
: "",
kind: OptionKind.API, kind: OptionKind.API,
}, },
enableHWA: { enableHWA: {

View File

@ -17,7 +17,7 @@ import { getPdfFilenameFromUrl } from "pdfjs-lib";
async function docProperties(pdfDocument) { async function docProperties(pdfDocument) {
const url = "", const url = "",
baseUrl = url.split("#", 1)[0]; baseUrl = "";
const { info, metadata, contentDispositionFilename, contentLength } = const { info, metadata, contentDispositionFilename, contentLength } =
await pdfDocument.getMetadata(); await pdfDocument.getMetadata();

View File

@ -17,6 +17,7 @@
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */ /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
import { isValidRotation, parseQueryString } from "./ui_utils.js"; import { isValidRotation, parseQueryString } from "./ui_utils.js";
import { updateUrlHash } from "pdfjs-lib";
import { waitOnEventOrTimeout } from "./event_utils.js"; import { waitOnEventOrTimeout } from "./event_utils.js";
// Heuristic value used when force-resetting `this._blockHashChange`. // Heuristic value used when force-resetting `this._blockHashChange`.
@ -383,10 +384,9 @@ class PDFHistory {
let newUrl; let newUrl;
if (this._updateUrl && destination?.hash) { if (this._updateUrl && destination?.hash) {
const baseUrl = document.location.href.split("#", 1)[0]; const { href, protocol } = document.location;
// Prevent errors in Firefox. if (protocol !== "file:") {
if (!baseUrl.startsWith("file://")) { newUrl = updateUrlHash(href, destination.hash);
newUrl = `${baseUrl}#${destination.hash}`;
} }
} }
if (shouldReplace) { if (shouldReplace) {

View File

@ -32,6 +32,7 @@ import {
AnnotationEditorType, AnnotationEditorType,
AnnotationEditorUIManager, AnnotationEditorUIManager,
AnnotationMode, AnnotationMode,
MathClamp,
PermissionFlag, PermissionFlag,
PixelsPerInch, PixelsPerInch,
shadow, shadow,
@ -2294,7 +2295,7 @@ class PDFViewer {
newScale = round((newScale * delta).toFixed(2) * 10) / 10; newScale = round((newScale * delta).toFixed(2) * 10) / 10;
} while (--steps > 0); } while (--steps > 0);
} }
newScale = Math.max(MIN_SCALE, Math.min(MAX_SCALE, newScale)); newScale = MathClamp(newScale, MIN_SCALE, MAX_SCALE);
this.#setScale(newScale, { noScroll: false, drawingDelay, origin }); this.#setScale(newScale, { noScroll: false, drawingDelay, origin });
} }

View File

@ -60,6 +60,7 @@ const {
SupportedImageMimeTypes, SupportedImageMimeTypes,
TextLayer, TextLayer,
TouchManager, TouchManager,
updateUrlHash,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,
@ -113,6 +114,7 @@ export {
SupportedImageMimeTypes, SupportedImageMimeTypes,
TextLayer, TextLayer,
TouchManager, TouchManager,
updateUrlHash,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,