1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

[api-minor] Improve the FileSpec implementation

- Check that the `filename` is actually a string, before parsing it further.
 - Use proper "shadowing" in the `filename` getter.
 - Add a bit more validation of the data in `pickPlatformItem`.
 - Last, but not least, return both the original `filename` and the (path stripped) variant needed in the display-layer and viewer.
This commit is contained in:
Jonas Jenwald 2024-05-01 12:28:33 +02:00
parent 16dbf5dcfd
commit 2b69fb76ac
6 changed files with 47 additions and 46 deletions

View file

@ -18,7 +18,6 @@
/** @typedef {import("./download_manager.js").DownloadManager} DownloadManager */
import { BaseTreeViewer } from "./base_tree_viewer.js";
import { getFilenameFromUrl } from "pdfjs-lib";
import { waitOnEventOrTimeout } from "./event_utils.js";
/**
@ -122,19 +121,13 @@ class PDFAttachmentViewer extends BaseTreeViewer {
let attachmentsCount = 0;
for (const name in attachments) {
const item = attachments[name];
const content = item.content,
description = item.description,
filename = getFilenameFromUrl(
item.filename,
/* onlyStripPath = */ true
);
const div = document.createElement("div");
div.className = "treeItem";
const element = document.createElement("a");
this._bindLink(element, { content, description, filename });
element.textContent = this._normalizeTextContent(filename);
this._bindLink(element, item);
element.textContent = this._normalizeTextContent(item.filename);
div.append(element);
@ -148,7 +141,7 @@ class PDFAttachmentViewer extends BaseTreeViewer {
/**
* Used to append FileAttachment annotations to the sidebar.
*/
#appendAttachment({ filename, content, description }) {
#appendAttachment(item) {
const renderedPromise = this._renderedCapability.promise;
renderedPromise.then(() => {
@ -158,15 +151,12 @@ class PDFAttachmentViewer extends BaseTreeViewer {
const attachments = this._attachments || Object.create(null);
for (const name in attachments) {
if (filename === name) {
if (item.filename === name) {
return; // Ignore the new attachment if it already exists.
}
}
attachments[filename] = {
filename,
content,
description,
};
attachments[item.filename] = item;
this.render({
attachments,
keepRenderedCapability: true,