ianstormtaylor / slate

A completely customizable framework for building rich text editors. (Currently in beta.)
http://slatejs.org
MIT License
29.71k stars 3.24k forks source link

Paste HTML TypeError: Cannot read properties of null (reading 'length') #4580

Open churichard opened 2 years ago

churichard commented 2 years ago

Description When pasting HTML (usually across multiple blocks) in the Paste HTML example, it gives the following error:

TypeError: Cannot read properties of null (reading 'length')

Sandbox https://www.slatejs.org/examples/paste-html

Steps To reproduce the behavior:

  1. Go to https://loripsum.net/
  2. Copy text to clipboard
  3. Go to https://www.slatejs.org/examples/paste-html
  4. Paste the text
  5. Open the console and observe that the error appears

Expectation This error should not appear.

Environment

Context There are also an excessive number of newlines that are added at the end (I think corresponding with the number of blocks that are pasted in). Perhaps this could be related to the issue?

Related to https://github.com/ianstormtaylor/slate/issues/4573

nemanja-tosic commented 2 years ago

The example deserializes HTML from that website into:

[
  {
    "type": "paragraph",
    "children": [
      {
        "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quis suae urbis conservatorem Codrum, quis Erechthei filias non maxime laudat? Quid de Pythagora? Quid enim est a Chrysippo praetermissum in Stoicis? Duo Reges: constructio interrete. Quid enim me prohiberet Epicureum esse, si probarem, quae ille diceret? "
      }
    ]
  },
  {
    "text": "\n\n"
  },
]

which mixes blocks and inlines, meaning that the fragment is invalid. The deserializer is not part of slate core, so the fix is just to the examples. Here's the problematic function: https://github.com/ianstormtaylor/slate/blob/main/site/examples/paste-html.tsx#L42