Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that addLinkAttributes is always called with a valid url parameter #11134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,19 @@ class LinkAnnotationElement extends AnnotationElement {
const { data, linkService, } = this;
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 (!data.url) {
if (data.action) {
} else if (data.action) {
this._bindNamedAction(link, data.action);
} else {
this._bindLink(link, data.dest);
}
}

this.container.appendChild(link);
return this.container;
Expand Down
7 changes: 4 additions & 3 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ const LinkTargetStringMap = [
* @param {ExternalLinkParameters} params
*/
function addLinkAttributes(link, { url, target, rel, enabled = true, } = {}) {
const urlNullRemoved = (url ? removeNullCharacters(url) : '');
assert(url && typeof url === 'string',
'addLinkAttributes: A valid "url" parameter must provided.');

const urlNullRemoved = removeNullCharacters(url);
if (enabled) {
link.href = link.title = urlNullRemoved;
} else {
Expand All @@ -365,15 +368,13 @@ function addLinkAttributes(link, { url, target, rel, enabled = true, } = {}) {
};
}

if (url) {
const LinkTargetValues = Object.values(LinkTarget);
const targetIndex =
LinkTargetValues.includes(target) ? target : LinkTarget.NONE;
link.target = LinkTargetStringMap[targetIndex];

link.rel = (typeof rel === 'string' ? rel : DEFAULT_LINK_REL);
}
}

// Gets the file name from a given URL.
function getFilenameFromUrl(url) {
Expand Down