Skip to content

Commit

Permalink
Don't native insert in elements with white-space="pre" containing tab…
Browse files Browse the repository at this point in the history
… chars (#5045)

* don't native insert in elements with white-space="pre" containing tab chars

* apply suggestions from code review
  • Loading branch information
BitPhinix authored Jul 15, 2022
1 parent abea3a3 commit 0b2e6c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-seahorses-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Don't native insert inside blocks with whitespace="pre" containing tab chars to work around https://bugs.chromium.org/p/chromium/issues/detail?id=1219139
27 changes: 24 additions & 3 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,39 @@ export const Editable = (props: EditableProps) => {
const [node, offset] = ReactEditor.toDOMPoint(editor, anchor)
const anchorNode = node.parentElement?.closest('a')

if (anchorNode && ReactEditor.hasDOMNode(editor, anchorNode)) {
const { document } = ReactEditor.getWindow(editor)
const window = ReactEditor.getWindow(editor)

if (
native &&
anchorNode &&
ReactEditor.hasDOMNode(editor, anchorNode)
) {
// Find the last text node inside the anchor.
const lastText = document
const lastText = window?.document
.createTreeWalker(anchorNode, NodeFilter.SHOW_TEXT)
.lastChild() as DOMText | null

if (lastText === node && lastText.textContent?.length === offset) {
native = false
}
}

// Chrome has issues with the presence of tab characters inside elements with whiteSpace = 'pre'
// causing abnormal insert behavior: https://bugs.chromium.org/p/chromium/issues/detail?id=1219139
if (
native &&
node.parentElement &&
window?.getComputedStyle(node.parentElement)?.whiteSpace === 'pre'
) {
const block = Editor.above(editor, {
at: anchor.path,
match: n => Editor.isBlock(editor, n),
})

if (block && Node.string(block[0]).includes('\t')) {
native = false
}
}
}

// COMPAT: For the deleting forward/backward input types we don't want
Expand Down

0 comments on commit 0b2e6c7

Please sign in to comment.