-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix composer crash when a list_item has no list around it (MAILSPRING-CLIENT-EV) #2813
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
bengotow
wants to merge
3
commits into
master
Choose a base branch
from
claude/awesome-ritchie-za95zx
base: master
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.
+157
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
100 changes: 100 additions & 0 deletions
100
app/spec/components/composer-editor/base-block-plugins-spec.tsx
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,100 @@ | ||
| import { Editor, Value } from 'slate'; | ||
| import { EditListPlugin } from '../../../src/components/composer-editor/base-block-plugins'; | ||
|
|
||
| const text = (t: string) => ({ object: 'text', leaves: [{ object: 'leaf', text: t, marks: [] }] }); | ||
| const block = (type: string, nodes: any[]) => ({ object: 'block', type, data: {}, nodes }); | ||
| const div = (t: string) => block('div', [text(t)]); | ||
| const listItem = (...nodes: any[]) => block('list_item', nodes); | ||
| const list = (...nodes: any[]) => block('ul_list', nodes); | ||
|
|
||
| // Built without plugins on purpose: the schema would repair an orphaned `list_item`, and the | ||
| // crash we're guarding against only happens in an editor that is no longer normalizing. | ||
| function editorWith(nodes: any[]) { | ||
| return new Editor({ | ||
| value: Value.fromJSON({ | ||
| object: 'value', | ||
| document: { object: 'document', data: {}, nodes }, | ||
| } as any), | ||
| plugins: [], | ||
| onChange: () => {}, | ||
| }); | ||
| } | ||
|
|
||
| function childTypes(editor: Editor) { | ||
| return (editor.value.document.nodes.toArray() as any[]).map((n) => n.type); | ||
| } | ||
|
|
||
| function pressKey(editor: Editor, key: string, textToFocus: string, offset = 0) { | ||
| const target = editor.value.document.getTexts().find((t) => t.text === textToFocus); | ||
| editor.moveTo(target.key as any, offset); | ||
|
|
||
| let passedToNext = false; | ||
| EditListPlugin.onKeyDown( | ||
| { key, shiftKey: false, preventDefault: () => {} } as any, | ||
| editor, | ||
| () => { | ||
| passedToNext = true; | ||
| } | ||
| ); | ||
| return passedToNext; | ||
| } | ||
|
|
||
| describe('EditListPlugin', () => { | ||
| // MAILSPRING-CLIENT-EV: email HTML can contain an <li> with no <ul>/<ol> around it. Slate's | ||
| // `unwrapNodeByPath` lifts such a node's one-element path to the empty root path, which | ||
| // `getDescendant` resolves to null, and `splitNodeByPath` then reads `.type` off null. | ||
| ['Enter', 'Backspace', 'Tab'].forEach((key) => { | ||
| it(`ignores ${key} inside a list_item that is not in a list`, () => { | ||
| const editor = editorWith([div('before'), listItem(div('')), div('after')]); | ||
| let passedToNext = false; | ||
| expect(() => { | ||
| passedToNext = pressKey(editor, key, ''); | ||
| }).not.toThrow(); | ||
| expect(passedToNext).toBe(true); | ||
| expect(childTypes(editor)).toEqual(['div', 'list_item', 'div']); | ||
| }); | ||
| }); | ||
|
|
||
| it('ignores Enter inside an orphaned list_item that is the only node in the document', () => { | ||
| const editor = editorWith([listItem(div(''))]); | ||
| expect(() => pressKey(editor, 'Enter', '')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('still exits the list when Enter is pressed in an empty item', () => { | ||
| const editor = editorWith([ | ||
| div('before'), | ||
| list(listItem(div('x')), listItem(div(''))), | ||
| div('after'), | ||
| ]); | ||
| expect(pressKey(editor, 'Enter', '')).toBe(false); | ||
| expect(childTypes(editor)).toEqual(['div', 'ul_list', 'div', 'div']); | ||
| }); | ||
|
|
||
| it('still splits the item when Enter is pressed mid-text', () => { | ||
| const editor = editorWith([div('before'), list(listItem(div('xy'))), div('after')]); | ||
| expect(pressKey(editor, 'Enter', 'xy', 1)).toBe(false); | ||
| const items = (editor.value.document.nodes.get(1) as any).nodes.toArray() as any[]; | ||
| expect(items.map((n) => n.text)).toEqual(['x', 'y']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Slate withoutNormalizing patch', () => { | ||
| // Required here, not at module scope, since installing it mutates `Editor.prototype` for | ||
| // every other spec in the run and the `EditListPlugin` specs above don't need it — they | ||
| // build their editors with `plugins: []` so nothing ever normalizes. A `describe` body runs | ||
| // once, synchronously, before any of its `it`s, so this installs the patch exactly once. | ||
| require('../../../src/components/composer-editor/patch-slate-normalizing'); | ||
|
|
||
| it('restores normalization when the callback throws', () => { | ||
| const editor = editorWith([div('hello')]); | ||
| expect((editor as any).tmp.normalize).toBe(true); | ||
|
|
||
| expect(() => | ||
| editor.withoutNormalizing(() => { | ||
| throw new Error('simulated crash'); | ||
| }) | ||
| ).toThrow(); | ||
|
|
||
| expect((editor as any).tmp.normalize).toBe(true); | ||
| }); | ||
| }); |
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
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
28 changes: 28 additions & 0 deletions
28
app/src/components/composer-editor/patch-slate-normalizing.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,28 @@ | ||
| /* | ||
| Slate's `Editor#withoutNormalizing` clears `editor.tmp.normalize`, runs the callback, and then | ||
| restores the flag — but it does so without a `try/finally`, so a command that throws from inside | ||
| the callback leaves normalization disabled for the rest of that editor's life. | ||
|
|
||
| Almost every structural Slate command (delete, insertFragment, splitDescendants, unwrapNode, ...) | ||
| runs inside `withoutNormalizing`, and exceptions thrown from a React event handler don't unmount | ||
| anything — we report them and the composer stays open. So a single Slate error means the document | ||
| is never repaired again for the rest of the session, and structurally invalid nodes that the | ||
| schema would normally fix (eg. a `list_item` with no list around it, deserialized from email HTML) | ||
| survive to crash later commands. | ||
|
|
||
| Restore the flag when the callback throws so one error can't cascade. | ||
| */ | ||
| import { Editor } from 'slate'; | ||
|
|
||
| const prototype = (Editor as any).prototype; | ||
| const withoutNormalizing = prototype.withoutNormalizing; | ||
|
|
||
| prototype.withoutNormalizing = function (fn: (editor: Editor) => void) { | ||
| const previous = this.tmp.normalize; | ||
| try { | ||
| return withoutNormalizing.call(this, fn); | ||
| } catch (err) { | ||
| this.tmp.normalize = previous; | ||
| throw err; | ||
| } | ||
| }; |
Oops, something went wrong.
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.