From a58c546dcc2a50bb4888541e7efa89c3ba121031 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Sat, 9 Dec 2023 18:52:18 +0000 Subject: [PATCH] Add test --- packages/slate/src/editor/marks.ts | 11 ++++++-- .../Editor/marks/firefox-double-click.tsx | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 packages/slate/test/interfaces/Editor/marks/firefox-double-click.tsx diff --git a/packages/slate/src/editor/marks.ts b/packages/slate/src/editor/marks.ts index e67442fcf7..a2d70ae6e2 100644 --- a/packages/slate/src/editor/marks.ts +++ b/packages/slate/src/editor/marks.ts @@ -19,12 +19,18 @@ export const marks: EditorInterface['marks'] = (editor, options = {}) => { } if (Range.isExpanded(selection)) { + /** + * COMPAT: Make sure hanging ranges (caused by double clicking in Firefox) + * do not adversely affect the returned marks. + */ const isEnd = Editor.isEnd(editor, anchor, anchor.path) if (isEnd) { const after = Editor.after(editor, anchor as Point) - // Editor.after() might return undefined - anchor = after || anchor + if (after) { + anchor = after + } } + const [match] = Editor.nodes(editor, { match: Text.isText, at: { @@ -32,6 +38,7 @@ export const marks: EditorInterface['marks'] = (editor, options = {}) => { focus, }, }) + if (match) { const [node] = match as NodeEntry const { text, ...rest } = node diff --git a/packages/slate/test/interfaces/Editor/marks/firefox-double-click.tsx b/packages/slate/test/interfaces/Editor/marks/firefox-double-click.tsx new file mode 100644 index 0000000000..36334ffc58 --- /dev/null +++ b/packages/slate/test/interfaces/Editor/marks/firefox-double-click.tsx @@ -0,0 +1,28 @@ +/** @jsx jsx */ +import { Editor } from 'slate' +import { jsx } from '../../..' + +/** + * This test verifies that when double clicking a marked word in Firefox, + * Editor.marks for the resulting selection includes the marked word. Double + * clicking a marked word in Firefox results in a selection that starts at the + * end of the previous text node and ends at the end of the marked text node. + */ + +export const input = ( + + + plain + + bold + + + plain + + block two + +) +export const test = editor => { + return Editor.marks(editor) +} +export const output = { bold: true }