draft-js icon indicating copy to clipboard operation
draft-js copied to clipboard

Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

Open pizn opened this issue 7 years ago • 22 comments

The current behavior:

draftjs


macOS + Chrome 60

  • Select some text cross two lines
  • With IME to input some word
  • It will remove the node, and insert text, but got error

The same issues with #1089, but nobody care.

pizn avatar Aug 04 '17 03:08 pizn

This looks like a duplicate of #1301

mmmoussa avatar Aug 04 '17 22:08 mmmoussa

Hi - just to get more info, what IME are you using? Is it an app or something that is part of MacOS? If so what version of macOS are you using?

flarnie avatar Aug 04 '17 22:08 flarnie

@mmmoussa, @flarnie my os is macOS 10.12.5 and I use pinyin IME.

I found IME is the big question with React WYSIWYG Editor, not only draft-js. Maybe you can try to use pinyin IME to test.


I try to debugger the problem. I found that something wrong with composition events.

The onCompositionStart event will get the selection's text data, when keydown, the keydown event data will replace selection's data -- At this time, the DOM is removed. And then onCompositionEnd event will set current IME data into the target DOM -- At this time, it will throw the exception.


Others, Chrome's composition events is different with Firefox's composition events.

pizn avatar Aug 05 '17 05:08 pizn

@mmmoussa @flarnie I need your help 🆘

pizn avatar Aug 07 '17 14:08 pizn

has this been solved yet? i have similar issue with chinese character on draft js, trying to create a custom hashtag decorator.

hashtag decorators not working after two or three chinese character subsequently. and when I press backspace key to delete the node, Failed to execute 'removeChild' on 'Node';

but error only occurs on chrome.

gjulivan avatar Apr 05 '18 08:04 gjulivan

Been getting reports of this specific "Failed to execute 'removeChild' on 'Node'" also specific to Mac + latest Chrome (69).

octatone avatar Oct 16 '18 09:10 octatone

@octatone Did you gathered more insights into this? I'm experiencing the same issue with my draft.js editor.

roundrobin avatar Nov 08 '18 15:11 roundrobin

I found the problem. The issue is related to an exception that's causing react to blow off and is not shown in the stack trace. In my case, it was related to font awesome (the js part of it) messing with the DOM and react not being able to reconcile properly.

I switched to an icon font that doesn't need JS at all. It's anyway a silly idea, I should have known better.

Hope this helps.

roundrobin avatar Nov 08 '18 17:11 roundrobin

may this helps:

getSelectedBlocks = () => {

  const selectionState =this.state.editorState.getSelection()
  const contentState = this.state.editorState.getCurrentContent()

  const startKey = selectionState.getStartKey()
  const endKey = selectionState.getEndKey()
  const isSameBlock = startKey === endKey
  const startingBlock = contentState.getBlockForKey(startKey)
  const selectedBlocks = [startingBlock]

  if (!isSameBlock) {
    let blockKey = startKey

    while (blockKey !== endKey) {
      const nextBlock = contentState.getBlockAfter(blockKey)
      selectedBlocks.push(nextBlock)
      blockKey = nextBlock.getKey()
    }
  }

  return selectedBlocks

}

handleCompositionStart = () => {

  if (this.getSelectedBlocks().length > 1) {

    // if multi blocks in selection, remove selection range when composition start 
    const nextEditorState = EditorState.push(
      this.state.editorState,
      Modifier.removeRange( this.state.editorState.getCurrentContent(),  this.state.editorState.getSelection(), 'backward'),
      'remove-range'
    )

    this.setState({
      editorState: nextEditorState
    })

  }

}
<div onCompositionStart={this.handleCompositionStart}><Editor {/*....*/}/></div>

margox avatar Nov 14 '18 14:11 margox

try to catch componentDidCatch(error, info) { // You can also log the error to an error reporting service // logErrorToMyService(error, info); console.log('componentDidCatch:', this.getContent()); this.setState({ editorState: EditorState.createEmpty(draftDecorator) }); }`

Peyton-lee avatar Dec 11 '18 14:12 Peyton-lee

See this workaround. https://github.com/facebook/react/issues/11538#issuecomment-417504600

pizn avatar Dec 17 '18 08:12 pizn

I found a simple solution, just add componentDidCatch method to your react component class:

class MyComponent extends Component {
    componentDidCatch() {
        this.forceUpdate();
    }

    render() {
        return <Editor .../>
    }
}

This can not fix the bug, but can prevent react component crash.

catouse avatar Mar 14 '19 09:03 catouse

Hi everyone! On my project I completely get rid from next error "Failed to execute 'removeChild' on 'Node' with next solution I hope it will help anyone who will face with the same problem.

import React from 'react';
import {Editor, EditorState, Modifier} from 'draft-js';

const HANDLED = 'handled';

class AwesomeFixEditor extends React.Component {

    state = {
        editorState: EditorState.createEmpty()
    };

    handleOnChange = editorState => this.setState({editorState});

    handleBeforeInput = (chars, editorState) => {
        const currentContentState = editorState.getCurrentContent();
        const selectionState = editorState.getSelection();

        this.handleOnChange(EditorState.push(
            editorState,
            Modifier.replaceText(
                currentContentState,
                selectionState,
                chars
            )
        ));

        return HANDLED;
    };

    render() {
        const {editorState} = this.state;

        return (
            <Editor
                editorState={editorState}
                handleBeforeInput={this.handleBeforeInput}
                onChange={this.handleOnChange} />
        );
    }
}

ArkadiyLis avatar Mar 26 '19 07:03 ArkadiyLis

Hi everyone! On my project I completely get rid from next error "Failed to execute 'removeChild' on 'Node' with next solution I hope it will help anyone who will face with the same problem.

import React from 'react';
import {Editor, EditorState, Modifier} from 'draft-js';

const HANDLED = 'handled';

class AwesomeFixEditor extends React.Component {

    state = {
        editorState: EditorState.createEmpty()
    };

    handleOnChange = editorState => this.setState({editorState});

    handleBeforeInput = (chars, editorState) => {
        const currentContentState = editorState.getCurrentContent();
        const selectionState = editorState.getSelection();

        this.handleOnChange(EditorState.push(
            editorState,
            Modifier.replaceText(
                currentContentState,
                selectionState,
                chars
            )
        ));

        return HANDLED;
    };

    render() {
        const {editorState} = this.state;

        return (
            <Editor
                editorState={editorState}
                handleBeforeInput={this.handleBeforeInput}
                onChange={this.handleOnChange} />
        );
    }
}

Using removes my toggled inline styles... how can i fix this?

hjpunzalan avatar Sep 23 '19 16:09 hjpunzalan

@hjpunzalan I haven't tried this particular block of code, but replaceText accepts an optional 4th argument where you can put the inline style(s) to be retained: https://draftjs.org/docs/api-reference-modifier#replacetext I think the styles have to be passed as an Immutable Map

jkhaui avatar Nov 10 '19 14:11 jkhaui

@hjpunzalan Your solution is not working with mention plugin. Actually, when I start typing my suggestion for "@" and then backspace the suggested value, the screen will become blank for mobile-only but working on a laptop(Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.). Can you suggest how we can use resolve this?

PulkitKoolKanya avatar Jun 19 '20 10:06 PulkitKoolKanya

I have the same issue @hjpunzalan @PulkitKoolKanya could you help me please

albertodarblade avatar Dec 03 '20 15:12 albertodarblade

Someone knows the solution?

bitbasher-dev avatar Mar 25 '21 01:03 bitbasher-dev

Bump. Anyone?

icellan avatar Aug 28 '21 16:08 icellan

@icellan @albertodarblade @hjpunzalan @PulkitKoolKanya Has anyone managed to solve the problem? I have the same error now

mifist avatar Jan 13 '22 23:01 mifist

@mifist No. We removed js-draft from our projects and replaced it with https://github.com/outline/rich-markdown-editor Users are very happy with the replacement.

icellan avatar Jan 14 '22 10:01 icellan

class MyComponent extends Component {
    componentDidCatch() {
        this.forceUpdate();
    }

    render() {
        return <Editor .../>
    }
}

Yes this solution work for me! I created me custom MentionEditor and wrapped it on this code.

mifist avatar Jan 14 '22 10:01 mifist