From 4b7bf74da29e23386bb4032e2e339de1f3f53f13 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 9 Jul 2022 14:29:36 +0200 Subject: [PATCH] Replace element `id`s with custom attributes in the xfaLayer We want to avoid adding regular `id`s to xfaLayer-elements, since that means that they become "linkable" through the URL hash in a way that's not supported/intended. This could end up clashing with "named destinations", and that could easily lead to bugs; see issue 11499 and PR 11503 for some context. Rather than using `id`s, we'll instead use a *custom* `data-element-id` attribute such that it's still possible to access the DOM-elements directly if needed. *Please note:* This is basically the xfaLayer-equivalent of PR 15057. --- src/display/xfa_layer.js | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/display/xfa_layer.js b/src/display/xfa_layer.js index ed3dd7a51..24f13a06f 100644 --- a/src/display/xfa_layer.js +++ b/src/display/xfa_layer.js @@ -107,28 +107,34 @@ class XfaLayer { attributes.name = `${attributes.name}-${intent}`; } for (const [key, value] of Object.entries(attributes)) { - // We don't need to add dataId in the html object but it can - // be useful to know its value when writing printing tests: - // in this case, don't skip dataId to have its value. - if (value === null || value === undefined || key === "dataId") { + if (value === null || value === undefined) { continue; } - if (key !== "style") { - if (key === "textContent") { - html.textContent = value; - } else if (key === "class") { + switch (key) { + case "class": if (value.length) { html.setAttribute(key, value.join(" ")); } - } else { - if (isHTMLAnchorElement && (key === "href" || key === "newWindow")) { - continue; // Handled below. + break; + case "dataId": + // We don't need to add dataId in the html object but it can + // be useful to know its value when writing printing tests: + // in this case, don't skip dataId to have its value. + break; + case "id": + html.setAttribute("data-element-id", value); + break; + case "style": + Object.assign(html.style, value); + break; + case "textContent": + html.textContent = value; + break; + default: + if (!isHTMLAnchorElement || (key !== "href" && key !== "newWindow")) { + html.setAttribute(key, value); } - html.setAttribute(key, value); - } - } else { - Object.assign(html.style, value); } } @@ -140,8 +146,7 @@ class XfaLayer { ); } - // Set the value after the others to be sure overwrite - // any other values. + // Set the value after the others to be sure to overwrite any other values. if (storage && attributes.dataId) { this.setupStorage(html, attributes.dataId, element, storage); }