Merge branch 'fix-regex-normalizer' into 'develop'

fix regex shortcode problem

Closes #675

See merge request pleroma/pleroma-fe!963
This commit is contained in:
kaniini 2019-10-01 06:29:31 +00:00
commit f3fdc92f15
2 changed files with 14 additions and 1 deletions

View file

@ -196,9 +196,11 @@ export const parseAttachment = (data) => {
return output return output
} }
export const addEmojis = (string, emojis) => { export const addEmojis = (string, emojis) => {
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g
return emojis.reduce((acc, emoji) => { return emojis.reduce((acc, emoji) => {
const regexSafeShortCode = emoji.shortcode.replace(matchOperatorsRegex, '\\$&')
return acc.replace( return acc.replace(
new RegExp(`:${emoji.shortcode}:`, 'g'), new RegExp(`:${regexSafeShortCode}:`, 'g'),
`<img src='${emoji.url}' alt='${emoji.shortcode}' title='${emoji.shortcode}' class='emoji' />` `<img src='${emoji.url}' alt='${emoji.shortcode}' title='${emoji.shortcode}' class='emoji' />`
) )
}, string) }, string)

View file

@ -345,5 +345,16 @@ describe('API Entities normalizer', () => {
const result = addEmojis('Admin add the :tenshi: emoji', emojis) const result = addEmojis('Admin add the :tenshi: emoji', emojis)
expect(result).to.equal('Admin add the :tenshi: emoji') expect(result).to.equal('Admin add the :tenshi: emoji')
}) })
it('Doesn\'t blow up on regex special characters', () => {
const emojis = makeMockEmojiMasto([{
shortcode: 'c++'
}, {
shortcode: '[a-z] {|}*'
}])
const result = addEmojis('This post has :c++: emoji and :[a-z] {|}*: emoji', emojis)
expect(result).to.include('title=\'c++\'')
expect(result).to.include('title=\'[a-z] {|}*\'')
})
}) })
}) })