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

convertFromHTML merged multiple unstyled tags into one #738

Open
NdYAG opened this issue Oct 25, 2016 · 2 comments · May be fixed by #927
Open

convertFromHTML merged multiple unstyled tags into one #738

NdYAG opened this issue Oct 25, 2016 · 2 comments · May be fixed by #927

Comments

@NdYAG
Copy link

NdYAG commented Oct 25, 2016

Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

Based on jsdom, it is easy to reproduce this bug:

const jsdom = require('jsdom')
jsdom.env('', function (err, window) {
  global.window = window
  global.document = window.document
  global.navigator = window.navigator
  global.HTMLElement = window.HTMLElement
  global.HTMLAnchorElement = window.HTMLAnchorElement
  const djs = require('draft-js')
  console.log(djs.convertToRaw(djs.ContentState.createFromBlockArray(djs.convertFromHTML('<h1>Title</h1><div>Hello</div><div>World</div>'))))
  process.exit(0)
})

Providing <h1>Title</h1><div>Hello</div><div>World</div>, convertFromHTML outputs two blocks:

{ entityMap: {},
  blocks:
   [ { key: 'bv0iv',
       text: 'Title',
       type: 'header-one',
       depth: 0,
       inlineStyleRanges: [],
       entityRanges: [],
       data: {} },
     { key: '1j1ko',
       text: 'HelloWorld',
       type: 'unstyled',
       depth: 0,
       inlineStyleRanges: [],
       entityRanges: [],
       data: {} } ] }

instead of three:

{ entityMap: {},
  blocks:
   [ { key: '8ahq8',
       text: 'Title',
       type: 'header-one',
       depth: 0,
       inlineStyleRanges: [],
       entityRanges: [],
       data: {} },
     { key: '18k8c',
       text: 'Hello',
       type: 'unstyled',
       depth: 0,
       inlineStyleRanges: [],
       entityRanges: [],
       data: {} },
     { key: '9p850',
       text: 'World',
       type: 'unstyled',
       depth: 0,
       inlineStyleRanges: [],
       entityRanges: [],
       data: {} } ] }

https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L504
might be the cause of this. Should we add 'div' to workingBlocks to make it be [ 'blockquote', 'figure', 'h1', 'h2', 'h3','h4','h5','h6','li','pre','div'] ?

Draft-js version: 0.9.1

@NdYAG
Copy link
Author

NdYAG commented Oct 25, 2016

related issue: #523

colinjeanne added a commit to textioHQ/draft-js that referenced this issue Jan 9, 2017
This change fixes facebookarchive#738 by ensuring that `convertFromHTMLToContentBlocks` counts unstyled blocks as blocks and by moving the logic to suppress a block's type into that function.

Previously the existence of any tag other than the unstyled tag would cause unstyled blocks not to be parsed as blocks (but, curiously, the aliases of the unstyled tag would be parsed as blocks). This is mean to handle a case like

```
<div><h2>Hello</h2></div>
```

ensuring that the final block is a `header-two` but breaks with examples like

```
<h2>Hello</h2>
<div>world!</div>
<div>Goodbye!</div>
```

which parses as two blocks with the two divs being combined into a single unstyled block.

The core issue is that not counting the unstyled tag when an interesting tag appears anywhere in the document is a global problem to a local issue: the fix is to instead consider each tree under a block individually with unstyled blocks deferring their final type to whatever the next lowest block type is.
@colinjeanne colinjeanne linked a pull request Jan 9, 2017 that will close this issue
DawnWright pushed a commit to WeTransfer/draft-js that referenced this issue May 10, 2017
This change fixes facebookarchive#738 by ensuring that `convertFromHTMLToContentBlocks` counts unstyled blocks as blocks and by moving the logic to suppress a block's type into that function.

Previously the existence of any tag other than the unstyled tag would cause unstyled blocks not to be parsed as blocks (but, curiously, the aliases of the unstyled tag would be parsed as blocks). This is mean to handle a case like

```
<div><h2>Hello</h2></div>
```

ensuring that the final block is a `header-two` but breaks with examples like

```
<h2>Hello</h2>
<div>world!</div>
<div>Goodbye!</div>
```

which parses as two blocks with the two divs being combined into a single unstyled block.

The core issue is that not counting the unstyled tag when an interesting tag appears anywhere in the document is a global problem to a local issue: the fix is to instead consider each tree under a block individually with unstyled blocks deferring their final type to whatever the next lowest block type is.
ukrbublik added a commit to ukrbublik/draft-js-mod that referenced this issue Jan 8, 2018
@thibaudcolas
Copy link
Contributor

thibaudcolas commented Jan 19, 2018

I think this is exactly the same as #523?

ericrockey pushed a commit to WeTransfer/draft-js that referenced this issue Oct 25, 2018
This change fixes facebookarchive#738 by ensuring that `convertFromHTMLToContentBlocks` counts unstyled blocks as blocks and by moving the logic to suppress a block's type into that function.

Previously the existence of any tag other than the unstyled tag would cause unstyled blocks not to be parsed as blocks (but, curiously, the aliases of the unstyled tag would be parsed as blocks). This is mean to handle a case like

```
<div><h2>Hello</h2></div>
```

ensuring that the final block is a `header-two` but breaks with examples like

```
<h2>Hello</h2>
<div>world!</div>
<div>Goodbye!</div>
```

which parses as two blocks with the two divs being combined into a single unstyled block.

The core issue is that not counting the unstyled tag when an interesting tag appears anywhere in the document is a global problem to a local issue: the fix is to instead consider each tree under a block individually with unstyled blocks deferring their final type to whatever the next lowest block type is.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants