feat: 完善内容管理通用富文本编辑器

This commit is contained in:
2026-08-27 17:01:48 +08:00
parent ed81f24892
commit d93486cd9a
21 changed files with 1245 additions and 30 deletions

22
tests/rich-text.test.ts Normal file
View File

@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest'
import { sanitizeRichText } from '../server/utils/rich-text'
describe('sanitizeRichText', () => {
it('keeps supported article formatting', () => {
const html = '<h2 style="text-align:center">标题</h2><p><strong>重点</strong><span style="font-size:20px;color:#17364e">正文</span></p>'
expect(sanitizeRichText(html)).toBe(html)
})
it('removes scripts, event handlers and unsafe links', () => {
const html = '<p onclick="alert(1)">正文<script>alert(1)</script><a href="javascript:alert(1)">危险链接</a></p>'
const cleaned = sanitizeRichText(html)
expect(cleaned).not.toContain('script')
expect(cleaned).not.toContain('onclick')
expect(cleaned).not.toContain('javascript:')
})
it('adds isolation attributes to new-window links', () => {
expect(sanitizeRichText('<a href="https://example.com" target="_blank">示例</a>'))
.toContain('rel="noopener noreferrer"')
})
})