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

<br> tags inside <p> gets removed when using "convertFromHTML"

Open RudraPSinha opened this issue 3 years ago • 9 comments

I'm having some problems with the convertFromHTML, it is removing <br> tags when used inside <p>

For example, html: <p>Paragraph 1</p><p><br/></p><p>Paragraph 2</p><p><br/></p><p>Paragraph 3</p>

Should render in the editor:

Paragraph 1

Paragraph 2

Paragraph 3

But instead, it ignores the line breaks and renders:

Paragraph 1 Paragraph 2 Paragraph 3

It is possible that I am doing something wrong but I could not figure what. Anyone else facing this issue?

RudraPSinha avatar Nov 16 '20 03:11 RudraPSinha

This looks like the same issue as #1154 (since copy-paste uses convertFromHTML under the hood).

thibaudcolas avatar Nov 20 '20 11:11 thibaudcolas

https://github.com/facebook/draft-js/blob/dc4351e7d6c8fbfb37dfa0a899d26f8bec8981f0/src/model/encoding/convertFromHTMLToContentBlocks.js#L713

that's the reason, it should be:

```javascript
      if (
        blockConfig.type !== 'unstyled' ||
        blockConfig.text !== '' ||
        (blockConfig.text === '' &&
          !(blockConfig.childConfigs && blockConfig.childConfigs.length))) {
        return [blockConfig];
      }

gogoyqj avatar Jan 11 '21 07:01 gogoyqj

+1

alqamabinsadiq avatar Apr 11 '21 11:04 alqamabinsadiq

I face this problem too

yinhowlew avatar May 11 '21 14:05 yinhowlew

Any solution?

spmsupun avatar Aug 20 '21 10:08 spmsupun

Workaround is to define <br> tag in blockRenderMap:

const blockRenderMap = DefaultDraftBlockRenderMap.set('br', { element: 'br' });
const blocksFromHTML = convertFromHTML(testString, getSafeBodyFromHTML, blockRenderMap)

chris-rudmin avatar Dec 04 '21 19:12 chris-rudmin

What is getSafeBodyFromHTML in this case?

altyaper avatar Apr 14 '22 23:04 altyaper

same question as @altyaper , What is getSafeBodyFromHTML in this case?

mohamedBENKHOUYA avatar May 06 '22 12:05 mohamedBENKHOUYA

@altyaper @mohamedBENKHOUYA This: https://github.com/facebook/draft-js/blob/main/src/model/paste/getSafeBodyFromHTML.js

chris-rudmin avatar May 06 '22 14:05 chris-rudmin