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 link = document.createElement('a');
addLinkAttributes(link, {
url: data.url,
target: (data.newWindow ?
LinkTarget.BLANK : linkService.externalLinkTarget),
rel: linkService.externalLinkRel,
enabled: linkService.externalLinkEnabled,
});
if (!data.url) {
if (data.action) {
this._bindNamedAction(link, data.action);
} else {
this._bindLink(link, data.dest);
}
if (data.url) {
addLinkAttributes(link, {
url: data.url,
target: (data.newWindow ?
LinkTarget.BLANK : linkService.externalLinkTarget),
rel: linkService.externalLinkRel,
enabled: linkService.externalLinkEnabled,
});
} else if (data.action) {
this._bindNamedAction(link, data.action);
} else {
this._bindLink(link, data.dest);
}
this.container.appendChild(link);

View File

@ -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 {
@ -365,14 +368,12 @@ 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];
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);
}
link.rel = (typeof rel === 'string' ? rel : DEFAULT_LINK_REL);
}
// Gets the file name from a given URL.