Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/components/Link/LinkBubbleView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
</div>
<!-- open link -->
<NcButton
:title="t('text', 'Open link')"
:aria-label="t('text', 'Open link')"
:disabled="!isSafeHref"
:title="openLinkTitle"
:aria-label="openLinkTitle"
variant="tertiary"
@click="openLink(href)">
<template #icon>
Expand Down Expand Up @@ -101,6 +102,7 @@ import { useOpenLinkHandler } from '../../composables/useOpenLinkHandler.ts'
import PreviewOptions from '../Editor/PreviewOptions.vue'

const PROTOCOLS_WITH_PREVIEW = ['http:', 'https:']
const SAFE_PROTOCOLS = ['http:', 'https:', 'mailto:', 'tel:']
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have PROTOCOLS_TO_LINK_TO = ['http:', 'https:', 'mailto:', 'tel:'] in src/marks/Link.ts, how about exporting the variable there and reusing it here?


export default {
name: 'LinkBubbleView',
Expand Down Expand Up @@ -170,6 +172,27 @@ export default {
return false
}
},

isSafeHref() {
try {
const url = new URL(this.href, window.location)
return !!this.href && SAFE_PROTOCOLS.includes(url.protocol)
} catch {
return false
}
},

openLinkTitle() {
if (this.isSafeHref) {
return t('text', 'Open link')
}

if (!this.href) {
return t('text', 'No link available to open')
}

return t('text', 'Cannot open links with unsafe protocols')
},
},

watch: {
Expand Down
Loading