fix: use more consistent method for checking external links (#893)

This commit is contained in:
Nolan Lawson 2019-01-13 17:11:27 -08:00 committed by GitHub
parent cb35a088f4
commit cb58a49c04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -80,27 +80,25 @@
mark('hydrateContent') mark('hydrateContent')
let node = this.refs.node let node = this.refs.node
let { originalStatus, uuid } = this.get() let { originalStatus, uuid } = this.get()
let { mentions, tags } = originalStatus
let count = 0 let count = 0
let anchors = node.getElementsByTagName('A') let anchors = Array.from(node.getElementsByTagName('A'))
let mentions = originalStatus.mentions
let tags = originalStatus.tags for (let anchor of anchors) {
for (let i = 0, len = anchors.length; i < len; i++) { // hydrate hashtag
let anchor = anchors[i]
let href = anchor.getAttribute('href')
if (tags && anchor.classList.contains('hashtag')) { if (tags && anchor.classList.contains('hashtag')) {
for (let j = 0, jLen = tags.length; j < jLen; j++) { for (let tag of tags) {
let tag = tags[j] if (anchor.getAttribute('href').endsWith(`/tags/${tag.name}`)) {
if (href.endsWith(`/tags/${tag.name}`)) {
anchor.setAttribute('href', `/tags/${tag.name}`) anchor.setAttribute('href', `/tags/${tag.name}`)
anchor.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`) anchor.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`)
anchor.removeAttribute('target') anchor.removeAttribute('target')
anchor.removeAttribute('rel') anchor.removeAttribute('rel')
} }
} }
// hydrate mention
} else if (mentions && anchor.classList.contains('mention')) { } else if (mentions && anchor.classList.contains('mention')) {
for (let j = 0, jLen = mentions.length; j < jLen; j++) { for (let mention of mentions) {
let mention = mentions[j] if (anchor.getAttribute('href') === mention.url) {
if (href === mention.url) {
anchor.setAttribute('href', `/accounts/${mention.id}`) anchor.setAttribute('href', `/accounts/${mention.id}`)
anchor.setAttribute('title', `@${mention.acct}`) anchor.setAttribute('title', `@${mention.acct}`)
anchor.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`) anchor.setAttribute('focus-key', `status-content-link-${uuid}-${++count}`)
@ -108,11 +106,13 @@
anchor.removeAttribute('rel') 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('target', '_blank')
anchor.setAttribute('rel', 'nofollow noopener')
} }
} }
stop('hydrateContent') stop('hydrateContent')