Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NcRichContenteditable): do not break adjacent mentions #6223

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/mixins/richEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ export default {
*/
parseContent(content) {
let text = content
// Consecutive spaces in HTML tags should collapse
text = text.replace(/>\s+</g, '><')
// Replace break lines with new lines
text = text.replace(/<br>/gmi, '\n')
// Replace some html special characters
Expand Down Expand Up @@ -115,8 +113,10 @@ export default {
: `@"${value}"`
}

// Return template and make sure we strip of new lines and tabs
return this.renderComponentHtml(data, NcMentionBubble).replace(/[\n\t]/gmi, '')
// Return template and make sure we strip off new lines, tabs and consecutive whitespaces
return this.renderComponentHtml(data, NcMentionBubble)
.replace(/[\n\t]/gmi, '')
.replace(/>\s+</g, '><')
},

/**
Expand Down
49 changes: 47 additions & 2 deletions tests/unit/mixins/richEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('richEditor.js', () => {
const output = editor.vm.renderContent(input)

expect(output).toEqual('hard<br>break')

const parsedOutput = editor.vm.parseContent(output)
expect(parsedOutput).toEqual(input)
})

it('no duplicated ampersand (from Linkify)', () => {
Expand All @@ -37,6 +40,9 @@ describe('richEditor.js', () => {
const output = editor.vm.renderContent(input)

expect(output).toEqual('hello &amp;')

const parsedOutput = editor.vm.parseContent(output)
expect(parsedOutput).toEqual(input)
})

it('keeps mentions without user data', () => {
Expand All @@ -45,6 +51,9 @@ describe('richEditor.js', () => {
const output = editor.vm.renderContent(input)

expect(output).toEqual('hello @foobar')

const parsedOutput = editor.vm.parseContent(output)
expect(parsedOutput).toEqual(input)
})

it('keeps mentions with user data', () => {
Expand All @@ -60,10 +69,43 @@ describe('richEditor.js', () => {
},
},
})
const input = 'hello @jdoe'
const input = 'hello @jdoe!\nhow are you?'
const output = editor.vm.renderContent(input)

expect(output).toMatch(/^hello <span.+role="heading" title="J. Doe".+\/span>!<br>how are you\?$/)
expect(output).not.toMatch(/[\n\t]/gmi)
expect(output).not.toMatch(/>\s+</g)

const parsedOutput = editor.vm.parseContent(output)
expect(parsedOutput).toEqual(input)
})

it('keeps adjacent mentions with user data', () => {
const editor = shallowMount(TestEditor, {
propsData: {
userData: {
jdoe: {
id: 'jdoe',
label: 'J. Doe',
source: 'users',
icon: 'icon-user',
},
'guest/47e0a7cf': {
id: 'guest/47e0a7cf',
label: 'J. Guest',
source: 'emails',
icon: 'icon-user',
},
},
},
})
const input = 'hello @jdoe @"guest/47e0a7cf"! how are you?'
const output = editor.vm.renderContent(input)

expect(output).toMatch(/^hello <span.+role="heading" title="J. Doe"/)
expect(output).toMatch(/^hello <span.+role="heading" title="J. Doe".+\/span> <span.+role="heading" title="J. Guest".+\/span>! how are you\?$/)

const parsedOutput = editor.vm.parseContent(output)
expect(parsedOutput).toEqual(input)
})

it('keep mentions with special characters', () => {
Expand Down Expand Up @@ -92,6 +134,9 @@ describe('richEditor.js', () => {
for (const i in inputs) {
const output = editor.vm.renderContent(inputs[i])
expect(output).toEqual(outputs[i])

const parsedOutput = editor.vm.parseContent(output)
expect(parsedOutput).toEqual(inputs[i])
}
})
})
Expand Down
Loading