Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

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

Open
RudraPSinha opened this issue Nov 16, 2020 · 9 comments
Open

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

RudraPSinha opened this issue Nov 16, 2020 · 9 comments

Comments

@RudraPSinha
Copy link

RudraPSinha commented Nov 16, 2020

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?

@thibaudcolas
Copy link
Contributor

thibaudcolas commented Nov 20, 2020

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

@gogoyqj
Copy link

gogoyqj commented Jan 11, 2021

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];
      }

@alqamabinsadiq
Copy link

+1

@yinhowlew
Copy link

I face this problem too

@spmsupun
Copy link

Any solution?

@chris-rudmin
Copy link

chris-rudmin commented Dec 4, 2021

Workaround is to define <br> tag in blockRenderMap:

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

@altyaper
Copy link

What is getSafeBodyFromHTML in this case?

@kamisone
Copy link

kamisone commented May 6, 2022

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

@chris-rudmin
Copy link

chris-rudmin commented May 6, 2022

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants