From cb58a49c04a6ebdfdbd59f5482eb861aecea2200 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 13 Jan 2019 17:11:27 -0800 Subject: [PATCH] fix: use more consistent method for checking external links (#893) --- .../_components/status/StatusContent.html | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/routes/_components/status/StatusContent.html b/src/routes/_components/status/StatusContent.html index 40a89d6..9d42419 100644 --- a/src/routes/_components/status/StatusContent.html +++ b/src/routes/_components/status/StatusContent.html @@ -80,27 +80,25 @@ mark('hydrateContent') let node = this.refs.node let { originalStatus, uuid } = this.get() + let { mentions, tags } = originalStatus let count = 0 - let anchors = node.getElementsByTagName('A') - let mentions = originalStatus.mentions - let tags = originalStatus.tags - for (let i = 0, len = anchors.length; i < len; i++) { - let anchor = anchors[i] - let href = anchor.getAttribute('href') + let anchors = Array.from(node.getElementsByTagName('A')) + + for (let anchor of anchors) { + // hydrate hashtag if (tags && anchor.classList.contains('hashtag')) { - for (let j = 0, jLen = tags.length; j < jLen; j++) { - let tag = tags[j] - if (href.endsWith(`/tags/${tag.name}`)) { + for (let tag of tags) { + if (anchor.getAttribute('href').endsWith(`/tags/${tag.name}`)) { anchor.setAttribute('href', `/tags/${tag.name}`) anchor.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`) anchor.removeAttribute('target') anchor.removeAttribute('rel') } } + // hydrate mention } else if (mentions && anchor.classList.contains('mention')) { - for (let j = 0, jLen = mentions.length; j < jLen; j++) { - let mention = mentions[j] - if (href === mention.url) { + for (let mention of mentions) { + if (anchor.getAttribute('href') === mention.url) { anchor.setAttribute('href', `/accounts/${mention.id}`) anchor.setAttribute('title', `@${mention.acct}`) anchor.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`) @@ -108,11 +106,13 @@ anchor.removeAttribute('rel') } } - } else if (anchor.getAttribute('rel') === 'nofollow noopener') { - anchor.setAttribute('title', href) } - if (anchor.getAttribute('href')[0] !== '/' || anchor.getAttribute('href')[1] === '/') { + // hydrate external links + let href = anchor.getAttribute('href') + if (new URL(href, location.href).origin !== location.origin) { + anchor.setAttribute('title', href) anchor.setAttribute('target', '_blank') + anchor.setAttribute('rel', 'nofollow noopener') } } stop('hydrateContent')