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

View File

@ -803,8 +803,8 @@ class AnnotationElement {
const fields = []; const fields = [];
if (this._fieldObjects) { if (this._fieldObjects) {
const fieldObj = this._fieldObjects[name]; const fieldObj = this._fieldObjects[name] || [];
if (fieldObj) {
for (const { page, id, exportValues } of fieldObj) { for (const { page, id, exportValues } of fieldObj) {
if (page === -1) { if (page === -1) {
continue; continue;
@ -815,16 +815,13 @@ class AnnotationElement {
const exportValue = const exportValue =
typeof exportValues === "string" ? exportValues : null; typeof exportValues === "string" ? exportValues : null;
const domElement = document.querySelector( const domElement = document.querySelector(`[data-element-id="${id}"]`);
`[data-element-id="${id}"]`
);
if (domElement && !GetElementsByNameSet.has(domElement)) { if (domElement && !GetElementsByNameSet.has(domElement)) {
warn(`_getElementsByName - element not allowed: ${id}`); warn(`_getElementsByName - element not allowed: ${id}`);
continue; continue;
} }
fields.push({ id, exportValue, domElement }); fields.push({ id, exportValue, domElement });
} }
}
return fields; return fields;
} }
// Fallback to a regular DOM lookup, to ensure that the standalone // Fallback to a regular DOM lookup, to ensure that the standalone