mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 23:38:42 +00:00
fix(webui): strip styles and preserve multiline text on paste in chat input (#371)
* fix(webui): strip styles and preserve multiline text on paste in chat input * fix(db): remove custom log wait strategy in test global setup to prevent postgres startup race condition
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { PostgreSqlContainer } from '@testcontainers/postgresql'
|
||||
import { exec } from 'node:child_process'
|
||||
import { promisify } from 'node:util'
|
||||
import { Wait } from 'testcontainers'
|
||||
|
||||
const execAsync = promisify(exec)
|
||||
|
||||
@@ -9,13 +8,7 @@ let dbContainer: import('@testcontainers/postgresql').StartedPostgreSqlContainer
|
||||
|
||||
export async function globalTestSetup() {
|
||||
console.log('Starting Testcontainers for global setup...')
|
||||
dbContainer = await new PostgreSqlContainer('pgvector/pgvector:pg18')
|
||||
.withWaitStrategy(
|
||||
// casting to any due to testcontainers wait strategy type mismatch
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
Wait.forLogMessage('database system is ready to accept connections', 1) as any,
|
||||
)
|
||||
.start()
|
||||
dbContainer = await new PostgreSqlContainer('pgvector/pgvector:pg18').start()
|
||||
|
||||
const databaseUrl = dbContainer.getConnectionUri()
|
||||
console.log('Database started at:', databaseUrl)
|
||||
|
||||
@@ -141,3 +141,111 @@ describe('ChatInput mention navigation', () => {
|
||||
expect(mentionNode.textContent).toContain('David Human')
|
||||
})
|
||||
})
|
||||
|
||||
describe('ChatInput paste handling', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('pastes plain text only and strips HTML formatting when styled HTML is in clipboard', async () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
})
|
||||
|
||||
const mockSendMessage = vi.fn()
|
||||
const mockChangeText = vi.fn()
|
||||
|
||||
const { container } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatInput
|
||||
projectId="proj-1"
|
||||
onSendMessage={mockSendMessage}
|
||||
onChangeText={mockChangeText}
|
||||
/>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
const editor = container.querySelector('[contenteditable="true"]') as HTMLDivElement
|
||||
expect(editor).toBeTruthy()
|
||||
|
||||
// Focus editor and set selection
|
||||
editor.focus()
|
||||
const range = document.createRange()
|
||||
range.selectNodeContents(editor)
|
||||
range.collapse(false)
|
||||
const sel = window.getSelection()!
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(range)
|
||||
|
||||
// Simulate pasting HTML with plain text fallback
|
||||
fireEvent.paste(editor, {
|
||||
clipboardData: {
|
||||
getData: (format: string) => {
|
||||
if (format === 'text/plain') return 'Clean plain text'
|
||||
if (format === 'text/html')
|
||||
return '<span style="color: red; font-size: 24px;"><b>Clean plain text</b></span>'
|
||||
return ''
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(editor.innerHTML).not.toContain('<span')
|
||||
expect(editor.innerHTML).not.toContain('style=')
|
||||
expect(editor.textContent).toBe('Clean plain text')
|
||||
expect(mockChangeText).toHaveBeenCalledWith('Clean plain text')
|
||||
|
||||
// Press Enter to send
|
||||
fireEvent.keyDown(editor, { key: 'Enter' })
|
||||
expect(mockSendMessage).toHaveBeenCalledWith(
|
||||
'Clean plain text',
|
||||
[],
|
||||
[],
|
||||
undefined,
|
||||
undefined,
|
||||
[],
|
||||
)
|
||||
})
|
||||
|
||||
it('preserves multiple lines when multiline text is pasted', async () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
})
|
||||
|
||||
const mockSendMessage = vi.fn()
|
||||
|
||||
const { container } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatInput projectId="proj-1" onSendMessage={mockSendMessage} />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
const editor = container.querySelector('[contenteditable="true"]') as HTMLDivElement
|
||||
expect(editor).toBeTruthy()
|
||||
|
||||
editor.focus()
|
||||
const range = document.createRange()
|
||||
range.selectNodeContents(editor)
|
||||
range.collapse(false)
|
||||
const sel = window.getSelection()!
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(range)
|
||||
|
||||
const multilineText = 'First line\nSecond line\nThird line'
|
||||
|
||||
fireEvent.paste(editor, {
|
||||
clipboardData: {
|
||||
getData: (format: string) => {
|
||||
if (format === 'text/plain') return multilineText
|
||||
return `<p>First line</p><p>Second line</p><p>Third line</p>`
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(editor.textContent).toBe(multilineText)
|
||||
|
||||
// Send message and verify multiline content is preserved in onSendMessage
|
||||
fireEvent.keyDown(editor, { key: 'Enter' })
|
||||
expect(mockSendMessage).toHaveBeenCalledWith(multilineText, [], [], undefined, undefined, [])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -432,6 +432,52 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
editorRef.current.focus()
|
||||
}
|
||||
|
||||
const handlePaste = (e: React.ClipboardEvent<HTMLDivElement>) => {
|
||||
e.preventDefault()
|
||||
const text = e.clipboardData.getData('text/plain')
|
||||
if (!text) return
|
||||
|
||||
let selection = window.getSelection()
|
||||
if (
|
||||
!selection ||
|
||||
selection.rangeCount === 0 ||
|
||||
!editorRef.current?.contains(selection.anchorNode)
|
||||
) {
|
||||
if (editorRef.current) {
|
||||
editorRef.current.focus()
|
||||
const range = document.createRange()
|
||||
range.selectNodeContents(editorRef.current)
|
||||
range.collapse(false)
|
||||
selection = window.getSelection()
|
||||
selection?.removeAllRanges()
|
||||
selection?.addRange(range)
|
||||
}
|
||||
}
|
||||
|
||||
const success =
|
||||
typeof document.execCommand === 'function' &&
|
||||
document.queryCommandSupported?.('insertText') &&
|
||||
document.execCommand('insertText', false, text)
|
||||
|
||||
if (!success) {
|
||||
const sel = window.getSelection()
|
||||
if (sel && sel.rangeCount > 0) {
|
||||
const range = sel.getRangeAt(0)
|
||||
range.deleteContents()
|
||||
const textNode = document.createTextNode(text)
|
||||
range.insertNode(textNode)
|
||||
range.setStartAfter(textNode)
|
||||
range.collapse(true)
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(range)
|
||||
} else if (editorRef.current) {
|
||||
editorRef.current.innerText += text
|
||||
}
|
||||
}
|
||||
|
||||
handleInput()
|
||||
}
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
||||
if (e.nativeEvent.isComposing) return
|
||||
|
||||
@@ -889,10 +935,11 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
|
||||
suppressContentEditableWarning
|
||||
onInput={handleInput}
|
||||
onKeyDown={handleKeyDown}
|
||||
onPaste={handlePaste}
|
||||
data-placeholder={
|
||||
placeholder || (replyingTo ? m.reply_placeholder() : m.message_placeholder())
|
||||
}
|
||||
className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block ${paddingLeftClass}`}
|
||||
className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block whitespace-pre-wrap break-words ${paddingLeftClass}`}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user