Merge pull request #11134 from Snuffleupagus/addLinkAttributes-assert-url

Ensure that `addLinkAttributes` is always called with a valid `url` parameter
This commit is contained in:
Tim van der Meij 2019-09-11 23:56:06 +02:00 committed by GitHub
commit 12ff252751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 22 deletions

View File

@ -294,20 +294,18 @@ class LinkAnnotationElement extends AnnotationElement {
const { data, linkService, } = this; const { data, linkService, } = this;
const link = document.createElement('a'); const link = document.createElement('a');
addLinkAttributes(link, { if (data.url) {
url: data.url, addLinkAttributes(link, {
target: (data.newWindow ? url: data.url,
LinkTarget.BLANK : linkService.externalLinkTarget), target: (data.newWindow ?
rel: linkService.externalLinkRel, LinkTarget.BLANK : linkService.externalLinkTarget),
enabled: linkService.externalLinkEnabled, rel: linkService.externalLinkRel,
}); enabled: linkService.externalLinkEnabled,
});
if (!data.url) { } else if (data.action) {
if (data.action) { this._bindNamedAction(link, data.action);
this._bindNamedAction(link, data.action); } else {
} else { this._bindLink(link, data.dest);
this._bindLink(link, data.dest);
}
} }
this.container.appendChild(link); this.container.appendChild(link);

View File

@ -354,7 +354,10 @@ const LinkTargetStringMap = [
* @param {ExternalLinkParameters} params * @param {ExternalLinkParameters} params
*/ */
function addLinkAttributes(link, { url, target, rel, enabled = true, } = {}) { 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) { if (enabled) {
link.href = link.title = urlNullRemoved; link.href = link.title = urlNullRemoved;
} else { } else {
@ -365,14 +368,12 @@ function addLinkAttributes(link, { url, target, rel, enabled = true, } = {}) {
}; };
} }
if (url) { const LinkTargetValues = Object.values(LinkTarget);
const LinkTargetValues = Object.values(LinkTarget); const targetIndex =
const targetIndex = LinkTargetValues.includes(target) ? target : LinkTarget.NONE;
LinkTargetValues.includes(target) ? target : LinkTarget.NONE; link.target = LinkTargetStringMap[targetIndex];
link.target = LinkTargetStringMap[targetIndex];
link.rel = (typeof rel === 'string' ? rel : DEFAULT_LINK_REL); link.rel = (typeof rel === 'string' ? rel : DEFAULT_LINK_REL);
}
} }
// Gets the file name from a given URL. // Gets the file name from a given URL.