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,6 +294,7 @@ class LinkAnnotationElement extends AnnotationElement {
const { data, linkService, } = this;
const link = document.createElement('a');
if (data.url) {
addLinkAttributes(link, {
url: data.url,
target: (data.newWindow ?
@ -301,14 +302,11 @@ class LinkAnnotationElement extends AnnotationElement {
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;

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,7 +368,6 @@ function addLinkAttributes(link, { url, target, rel, enabled = true, } = {}) {
};
}
if (url) {
const LinkTargetValues = Object.values(LinkTarget);
const targetIndex =
LinkTargetValues.includes(target) ? target : LinkTarget.NONE;
@ -373,7 +375,6 @@ function addLinkAttributes(link, { url, target, rel, enabled = true, } = {}) {
link.rel = (typeof rel === 'string' ? rel : DEFAULT_LINK_REL);
}
}
// Gets the file name from a given URL.
function getFilenameFromUrl(url) {