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

[api-minor] Add a wrapper around the addLinkAttributes-function, in the API, to the PDFLinkService implementations

This patch helps reduce some duplication, given that we now have a few essentially identical `addLinkAttributes` call-sites in the code-base.
To prevent runtime errors in the Annotation/XFA-layer code, we'll warn if a custom/incomplete `PDFLinkService` is being used (limited to GENERIC builds).
This commit is contained in:
Jonas Jenwald 2021-09-30 13:30:55 +02:00
parent bb9c905c5d
commit 8cb6efec2d
9 changed files with 67 additions and 45 deletions

View file

@ -13,13 +13,6 @@
* limitations under the License.
*/
import {
addLinkAttributes,
DOMSVGFactory,
getFilenameFromUrl,
LinkTarget,
PDFDateString,
} from "./display_utils.js";
import {
AnnotationBorderStyleType,
AnnotationType,
@ -30,6 +23,11 @@ import {
Util,
warn,
} from "../shared/util.js";
import {
DOMSVGFactory,
getFilenameFromUrl,
PDFDateString,
} from "./display_utils.js";
import { AnnotationStorage } from "./annotation_storage.js";
import { ColorConverters } from "../shared/scripting_utils.js";
@ -443,14 +441,15 @@ class LinkAnnotationElement extends AnnotationElement {
const link = document.createElement("a");
if (data.url) {
addLinkAttributes(link, {
url: data.url,
target: data.newWindow
? LinkTarget.BLANK
: linkService.externalLinkTarget,
rel: linkService.externalLinkRel,
enabled: linkService.externalLinkEnabled,
});
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
!linkService.addLinkAttributes
) {
warn(
"LinkAnnotationElement.render - missing `addLinkAttributes`-method on the `linkService`-instance."
);
}
linkService.addLinkAttributes?.(link, data.url, data.newWindow);
} else if (data.action) {
this._bindNamedAction(link, data.action);
} else if (data.dest) {

View file

@ -338,7 +338,7 @@ const LinkTarget = {
/**
* Adds various attributes (href, title, target, rel) to hyperlinks.
* @param {HTMLLinkElement} link - The link element.
* @param {HTMLAnchorElement} link - The link element.
* @param {ExternalLinkParameters} params
*/
function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) {
@ -633,7 +633,6 @@ function getXfaPageViewport(xfaPage, { scale = 1, rotation = 0 }) {
export {
addLinkAttributes,
DEFAULT_LINK_REL,
deprecated,
DOMCanvasFactory,
DOMCMapReaderFactory,

View file

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { addLinkAttributes, LinkTarget } from "./display_utils.js";
import { warn } from "../shared/util.js";
import { XfaText } from "./xfa_text.js";
class XfaLayer {
@ -119,14 +119,19 @@ class XfaLayer {
}
if (isHTMLAnchorElement) {
addLinkAttributes(html, {
url: attributes.href,
target: attributes.newWindow
? LinkTarget.BLANK
: linkService.externalLinkTarget,
rel: linkService.externalLinkRel,
enabled: linkService.externalLinkEnabled,
});
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
!linkService.addLinkAttributes
) {
warn(
"XfaLayer.setAttribute - missing `addLinkAttributes`-method on the `linkService`-instance."
);
}
linkService.addLinkAttributes?.(
html,
attributes.href,
attributes.newWindow
);
}
// Set the value after the others to be sure overwrite