draft-js
draft-js copied to clipboard
<br> tags inside <p> gets removed when using "convertFromHTML"
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?
This looks like the same issue as #1154 (since copy-paste uses convertFromHTML
under the hood).
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];
}
+1
I face this problem too
Any solution?
Workaround is to define <br>
tag in blockRenderMap:
const blockRenderMap = DefaultDraftBlockRenderMap.set('br', { element: 'br' });
const blocksFromHTML = convertFromHTML(testString, getSafeBodyFromHTML, blockRenderMap)
What is getSafeBodyFromHTML
in this case?
same question as @altyaper , What is getSafeBodyFromHTML in this case?
@altyaper @mohamedBENKHOUYA This: https://github.com/facebook/draft-js/blob/main/src/model/paste/getSafeBodyFromHTML.js