Merge pull request #21653 from Snuffleupagus/jsActions-_getElementsByName-shorten

Shorten `Catalog.prototype.jsActions getter` and `AnnotationElement.prototype._getElementsByName` a tiny bit
This commit is contained in:
Tim van der Meij 2026-07-28 20:23:07 +02:00 committed by GitHub
commit 0299cdcab9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 31 deletions

View File

@ -1241,13 +1241,9 @@ class Catalog {
let javaScript = null;
function appendIfJavaScriptDict(name, jsDict) {
if (!(jsDict instanceof Dict)) {
if (!(jsDict instanceof Dict) || !isName(jsDict.get("S"), "JavaScript")) {
return;
}
if (!isName(jsDict.get("S"), "JavaScript")) {
return;
}
let js = jsDict.get("JS");
if (js instanceof BaseStream) {
js = js.getString();
@ -1260,7 +1256,7 @@ class Catalog {
);
// Skip empty entries, similar to the `_collectJS` function.
if (js) {
(javaScript ||= new Map()).set(name, js);
(javaScript ??= new Map()).set(name, js);
}
}
@ -1291,14 +1287,10 @@ class Catalog {
);
if (javaScript) {
actions ||= Object.create(null);
actions ??= Object.create(null);
for (const [key, val] of javaScript) {
if (key in actions) {
actions[key].push(val);
} else {
actions[key] = [val];
}
(actions[key] ??= []).push(val);
}
}
return shadow(this, "jsActions", actions);

View File

@ -803,27 +803,24 @@ class AnnotationElement {
const fields = [];
if (this._fieldObjects) {
const fieldObj = this._fieldObjects[name];
if (fieldObj) {
for (const { page, id, exportValues } of fieldObj) {
if (page === -1) {
continue;
}
if (id === skipId) {
continue;
}
const exportValue =
typeof exportValues === "string" ? exportValues : null;
const fieldObj = this._fieldObjects[name] || [];
const domElement = document.querySelector(
`[data-element-id="${id}"]`
);
if (domElement && !GetElementsByNameSet.has(domElement)) {
warn(`_getElementsByName - element not allowed: ${id}`);
continue;
}
fields.push({ id, exportValue, domElement });
for (const { page, id, exportValues } of fieldObj) {
if (page === -1) {
continue;
}
if (id === skipId) {
continue;
}
const exportValue =
typeof exportValues === "string" ? exportValues : null;
const domElement = document.querySelector(`[data-element-id="${id}"]`);
if (domElement && !GetElementsByNameSet.has(domElement)) {
warn(`_getElementsByName - element not allowed: ${id}`);
continue;
}
fields.push({ id, exportValue, domElement });
}
return fields;
}