Make hashtag links URL safe

This commit is contained in:
Wolfgang Huß 2019-09-13 11:55:38 +02:00 committed by roschaefer
parent bd0273f071
commit 82d5ac99df
7 changed files with 13 additions and 11 deletions

View File

@ -20,7 +20,7 @@ export default function(content) {
.get()
const hashtags = []
urls.forEach(url => {
const match = exec(url, regX)
const match = exec(decodeURI(url), regX)
if (match != null) {
hashtags.push(match[1])
}

View File

@ -27,14 +27,14 @@ describe('extractHashtags', () => {
expect(extractHashtags(content)).toEqual(['Democracy'])
})
it('ignores Hashtag links with not allowed character combinations', () => {
it('ignores Hashtag links with not allowed character combinations and handles `encodeURI` URLs', () => {
// Allowed are all unicode letters '\pL' and all digits '0-9'. There haveto be at least one letter in it.
const content =
'<p>Something inspirational about <a href="/search/hashtag/AbcDefXyz0123456789!*(),2" class="hashtag" target="_blank">#AbcDefXyz0123456789!*(),2</a>, <a href="/search/hashtag/0123456789" class="hashtag" target="_blank">#0123456789</a>, <a href="/search/hashtag/0123456789a" class="hashtag" target="_blank">#0123456789a</a>, <a href="/search/hashtag/AbcDefXyz0123456789" target="_blank">#AbcDefXyz0123456789</a>, and <a href="/search/hashtag/λαπ" target="_blank">#λαπ</a>.</p>'
'<p>Something inspirational about <a href="/search/hashtag/AbcDefXyz0123456789!*(),2" class="hashtag" target="_blank">#AbcDefXyz0123456789!*(),2</a>, <a href="/search/hashtag/0123456789" class="hashtag" target="_blank">#0123456789</a>, <a href="/search/hashtag/0123456789a" class="hashtag" target="_blank">#0123456789a</a>, <a href="/search/hashtag/AbcDefXyz0123456789" target="_blank">#AbcDefXyz0123456789</a>, and <a href="/search/hashtag/%C4%A7%CF%80%CE%B1%CE%BB" target="_blank">#ħπαλ</a>.</p>'
expect(extractHashtags(content).sort()).toEqual([
'0123456789a',
'AbcDefXyz0123456789',
'λαπ',
'ħπαλ',
])
})
})

View File

@ -129,8 +129,8 @@ describe('hashtags', () => {
)
})
describe('afterwards update the Post by removing a Hashtag, leaving a Hashtag and add a Hashtag', () => {
// The already existing Hashtag has no class at this point.
describe('afterwards update the Post by removing a hashtag, leaving a hashtag and add a hashtag', () => {
// The already existing hashtag has no class at this point.
const postContent =
'<p>Hey Dude, <a class="hashtag" href="/search/hashtag/Elections">#Elections</a> should work equal for everybody!? That seems to be the only way to have equal <a href="/search/hashtag/Liberty">#Liberty</a> for everyone.</p>'

View File

@ -25,7 +25,7 @@ export default class Hashtag extends TipTapMention {
'a',
{
class: this.options.mentionClass,
href: `/search/hashtag/${node.attrs.id}`,
href: `/search/hashtag/${encodeURI(node.attrs.id)}`,
'data-hashtag-id': node.attrs.id,
target: '_blank',
},

View File

@ -5,7 +5,7 @@
{{ scope.index + 1 }}.
</template>
<template slot="id" slot-scope="scope">
<nuxt-link :to="{ path: '/', query: { hashtag: scope.row.id } }">
<nuxt-link :to="{ path: '/', query: { hashtag: encodeURI(scope.row.id) } }">
<b>#{{ scope.row.id | truncate(20) }}</b>
</nuxt-link>
</template>

View File

@ -75,7 +75,8 @@ export default {
MasonryGridItem,
},
data() {
const { hashtag = null } = this.$route.query
let { hashtag = null } = this.$route.query
hashtag = decodeURI(hashtag)
return {
posts: [],
hasMore: true,

View File

@ -5,8 +5,9 @@
<script>
export default {
mounted() {
const { id: hashtag } = this.$route.params
this.$router.push({ path: '/', query: { hashtag } })
let { id: hashtag } = this.$route.params
// 'hashtag' seems automatically 'decodeURI' on macOS Firefox. Don't know if this is always the case.
this.$router.push({ path: '/', query: { hashtag: encodeURI(hashtag) } })
},
}
</script>