-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(collaboration): move content validation from beforeTransaction to filterTransaction #7600
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
Open
dearlordylord
wants to merge
2
commits into
ueberdosis:main
Choose a base branch
from
dearlordylord:fix/f1-yjs-before-transaction-ignored
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−35
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@tiptap/extension-collaboration": patch | ||
| --- | ||
|
|
||
| Moved content validation from Yjs `beforeTransaction` (whose return value was ignored) to ProseMirror `filterTransaction`, so invalid collaborative changes are now properly blocked. |
155 changes: 155 additions & 0 deletions
155
packages/extension-collaboration/__tests__/filterInvalidContent.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import { Editor } from '@tiptap/core' | ||
| import Document from '@tiptap/extension-document' | ||
| import Paragraph from '@tiptap/extension-paragraph' | ||
| import Text from '@tiptap/extension-text' | ||
| import type { Plugin } from '@tiptap/pm/state' | ||
| import { ySyncPluginKey } from '@tiptap/y-tiptap' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import * as Y from 'yjs' | ||
|
|
||
| import Collaboration from '../src/index.js' | ||
|
|
||
| describe('filterInvalidContent', () => { | ||
| let editor: Editor | null = null | ||
| let el: HTMLElement | null = null | ||
|
|
||
| afterEach(() => { | ||
| editor?.destroy() | ||
| el?.remove() | ||
| editor = null | ||
| el = null | ||
| }) | ||
|
|
||
| const createCollabEditor = async ( | ||
| opts: { | ||
| onContentError?: (args: { disableCollaboration: () => void }) => void | ||
| } = {}, | ||
| ) => { | ||
| const ydoc = new Y.Doc() | ||
|
|
||
| el = document.createElement('div') | ||
| document.body.appendChild(el) | ||
|
|
||
| editor = new Editor({ | ||
| element: el, | ||
| extensions: [Document, Paragraph, Text, Collaboration.configure({ document: ydoc })], | ||
| enableContentCheck: true, | ||
| onContentError: opts.onContentError ?? (() => {}), | ||
| }) | ||
|
|
||
| await new Promise<void>(resolve => { | ||
| setTimeout(resolve, 10) | ||
| }) | ||
|
|
||
| return { editor, el, ydoc } | ||
| } | ||
|
|
||
| const findFilterPlugin = (e: Editor): Plugin | undefined => | ||
| e.state.plugins.find(p => p.spec.filterTransaction && p.key.includes('filterInvalidContent')) | ||
|
|
||
| it('rejects Yjs transactions that produce invalid content', async () => { | ||
| let contentErrorCalled = false | ||
|
|
||
| const { editor: ed } = await createCollabEditor({ | ||
| onContentError: () => { | ||
| contentErrorCalled = true | ||
| }, | ||
| }) | ||
|
|
||
| const plugin = findFilterPlugin(ed) | ||
|
|
||
| expect(plugin).toBeDefined() | ||
|
|
||
| const tr = ed.state.tr.insertText('x', 1) | ||
|
|
||
| tr.setMeta(ySyncPluginKey, { binding: true }) | ||
|
|
||
| vi.spyOn(tr.doc, 'check').mockImplementation(() => { | ||
| throw new RangeError('Invalid content for node doc') | ||
| }) | ||
|
|
||
| const result = plugin!.spec.filterTransaction!(tr, ed.state) | ||
|
|
||
| expect(result).toBe(false) | ||
| expect(contentErrorCalled).toBe(true) | ||
| expect(ed.storage.collaboration.isDisabled).toBe(true) | ||
| }) | ||
|
|
||
| it('allows local (non-Yjs) transactions through', async () => { | ||
| const { editor: ed } = await createCollabEditor({ | ||
| onContentError: () => { | ||
| throw new Error('contentError should not fire for local transactions') | ||
| }, | ||
| }) | ||
|
|
||
| ed.commands.insertContent('hello') | ||
| expect(ed.getText()).toContain('hello') | ||
| }) | ||
|
|
||
| it('blocks Yjs transactions when isDisabled is true', async () => { | ||
| const { editor: ed } = await createCollabEditor() | ||
|
|
||
| ed.storage.collaboration.isDisabled = true | ||
|
|
||
| const docBefore = ed.state.doc.toJSON() | ||
|
|
||
| const tr = ed.state.tr.insertText('injected', 1) | ||
|
|
||
| tr.setMeta(ySyncPluginKey, { binding: true }) | ||
| ed.view.dispatch(tr) | ||
|
|
||
| expect(ed.state.doc.toJSON()).toEqual(docBefore) | ||
| }) | ||
|
|
||
| it('allows Yjs transactions that do not change the doc', async () => { | ||
| const { editor: ed } = await createCollabEditor({ | ||
| onContentError: () => { | ||
| throw new Error('contentError should not fire for metadata-only transactions') | ||
| }, | ||
| }) | ||
|
|
||
| const plugin = findFilterPlugin(ed) | ||
|
|
||
| expect(plugin).toBeDefined() | ||
|
|
||
| const tr = ed.state.tr | ||
|
|
||
| tr.setMeta(ySyncPluginKey, { binding: true }) | ||
|
|
||
| expect(tr.docChanged).toBe(false) | ||
|
|
||
| const result = plugin!.spec.filterTransaction!(tr, ed.state) | ||
|
|
||
| expect(result).toBe(true) | ||
| }) | ||
|
|
||
| it('emits contentError with disableCollaboration callback', async () => { | ||
| let disableCollab: (() => void) | null = null | ||
|
|
||
| const { editor: ed, ydoc } = await createCollabEditor({ | ||
| onContentError: args => { | ||
| disableCollab = args.disableCollaboration | ||
| }, | ||
| }) | ||
|
|
||
| const plugin = findFilterPlugin(ed) | ||
|
|
||
| expect(plugin).toBeDefined() | ||
|
|
||
| const tr = ed.state.tr.insertText('x', 1) | ||
|
|
||
| tr.setMeta(ySyncPluginKey, { binding: true }) | ||
| vi.spyOn(tr.doc, 'check').mockImplementation(() => { | ||
| throw new RangeError('Invalid content') | ||
| }) | ||
|
|
||
| plugin!.spec.filterTransaction!(tr, ed.state) | ||
|
|
||
| expect(disableCollab).not.toBeNull() | ||
|
|
||
| const destroySpy = vi.spyOn(ydoc, 'destroy') | ||
|
|
||
| disableCollab!() | ||
| expect(destroySpy).toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.