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

convertFromHTML breaks after converting \n string, issue #1822 #1823

Closed

Conversation

yury-sannikov
Copy link
Contributor

Summary
The core issue is here:
https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L376

A call to the let chunk = {...EMPTY_CHUNK}; create a shallow copy of the EMPTY_CHUNK object. It means that chunk.blocks array (and others) share the same reference to the EMPTY_CHUNK.blocks.

If code hit this line https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L633 EMPTY_CHUNK object will end up having block array with one element of {type: 'unstyled', depth: 0} which caused the aforementioned issue.

Next call to the convertFromHTML will start with polluted EMPTY_CHUNK object. :-(

Test Plan
Open a https://jsfiddle.net/m6z0xn4r/1190/
Put a breakpoint on convertFromHTML and see 2 different results while converting headers

Another way:
Open chrome dev tools and:

  • execute convertFromHTML('\n')
  • execute convertFromHTML('<h1>H</h1>')
    Observe that second call return a block with unstyled type.

Copy link

@vikaskyadav vikaskyadav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please rebase your branch with the latest changes.

@nitayneeman
Copy link

Thanks for the PR @yury-sannikov, I'm facing the same issue.

I'm just wondering what's the status of this PR? Are you planning to fix the conflicts soon? 🤔

@claudiopro
Copy link
Contributor

Thanks for spotting the issue and submitting a fix @yury-sannikov. Please note the convertFromHTMLToContentBlocks.js converter is deprecated and will be replaced by convertFromHTMLToContentBlocks2.js.

May I ask you to update this PR by rebasing the commits on latest master, and write a test case for convertFromHTMLToContentBlocks2.js as well?

@claudiopro claudiopro added the needs rebasing Used to flag PRs that need rebasing label Jun 14, 2019
@claudiopro claudiopro self-assigned this Jun 14, 2019
@claudiopro claudiopro added the bug label Jun 14, 2019
Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claudiopro has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link

@claudiopro merged this pull request in 9246cc7.

jdecked pushed a commit to twitter-forks/draft-js that referenced this pull request Oct 9, 2019
…ive#1822 (facebookarchive#1823)

Summary:
This diff is commandeering a PR based on a previous baseline to rebase it against latest master. This diff discards the original fix which is no longer applicable, having the affected HTML converter been removed in favor of the new implementation by nicolasc. I am still keeping the test case as a guard against regression.

**Summary**
The core issue is here:
https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L376

A call to the `let chunk = {...EMPTY_CHUNK};` create a shallow copy of the `EMPTY_CHUNK` object. It means that `chunk.blocks` array (and others) share the same reference to the `EMPTY_CHUNK.blocks`.

If code hit this line https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L633 `EMPTY_CHUNK` object will end up having `block` array with one element of `{type: 'unstyled', depth: 0}` which caused the aforementioned issue.

Next call to the `convertFromHTML` will start with polluted `EMPTY_CHUNK` object. :-(

**Test Plan**
Open a https://jsfiddle.net/m6z0xn4r/1190/
Put a breakpoint on `convertFromHTML` and see 2 different results while converting headers

Another way:
Open chrome dev tools and:
- execute `convertFromHTML('\n')`
- execute `convertFromHTML('<h1>H</h1>')`
Observe that second call return a block with unstyled type.
Pull Request resolved: facebookarchive#1823

Test Plan: Adds a regression test case, run with `jest --watch`

Reviewed By: gmertk

Differential Revision: D17476663

Pulled By: claudiopro

fbshipit-source-id: 05da4d53114aa0d09de61ce55c27f5166b021138
mmissey pushed a commit to mmissey/draft-js that referenced this pull request Mar 24, 2020
…ive#1822 (facebookarchive#1823)

Summary:
This diff is commandeering a PR based on a previous baseline to rebase it against latest master. This diff discards the original fix which is no longer applicable, having the affected HTML converter been removed in favor of the new implementation by nicolasc. I am still keeping the test case as a guard against regression.

**Summary**
The core issue is here:
https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L376

A call to the `let chunk = {...EMPTY_CHUNK};` create a shallow copy of the `EMPTY_CHUNK` object. It means that `chunk.blocks` array (and others) share the same reference to the `EMPTY_CHUNK.blocks`.

If code hit this line https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L633 `EMPTY_CHUNK` object will end up having `block` array with one element of `{type: 'unstyled', depth: 0}` which caused the aforementioned issue.

Next call to the `convertFromHTML` will start with polluted `EMPTY_CHUNK` object. :-(

**Test Plan**
Open a https://jsfiddle.net/m6z0xn4r/1190/
Put a breakpoint on `convertFromHTML` and see 2 different results while converting headers

Another way:
Open chrome dev tools and:
- execute `convertFromHTML('\n')`
- execute `convertFromHTML('<h1>H</h1>')`
Observe that second call return a block with unstyled type.
Pull Request resolved: facebookarchive#1823

Test Plan: Adds a regression test case, run with `jest --watch`

Reviewed By: gmertk

Differential Revision: D17476663

Pulled By: claudiopro

fbshipit-source-id: 05da4d53114aa0d09de61ce55c27f5166b021138
alicayan008 pushed a commit to alicayan008/draft-js that referenced this pull request Jul 4, 2023
Summary:
This diff is commandeering a PR based on a previous baseline to rebase it against latest master. This diff discards the original fix which is no longer applicable, having the affected HTML converter been removed in favor of the new implementation by nicolasc. I am still keeping the test case as a guard against regression.

**Summary**
The core issue is here:
https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L376

A call to the `let chunk = {...EMPTY_CHUNK};` create a shallow copy of the `EMPTY_CHUNK` object. It means that `chunk.blocks` array (and others) share the same reference to the `EMPTY_CHUNK.blocks`.

If code hit this line https://github.com/facebook/draft-js/blob/master/src/model/encoding/convertFromHTMLToContentBlocks.js#L633 `EMPTY_CHUNK` object will end up having `block` array with one element of `{type: 'unstyled', depth: 0}` which caused the aforementioned issue.

Next call to the `convertFromHTML` will start with polluted `EMPTY_CHUNK` object. :-(

**Test Plan**
Open a https://jsfiddle.net/m6z0xn4r/1190/
Put a breakpoint on `convertFromHTML` and see 2 different results while converting headers

Another way:
Open chrome dev tools and:
- execute `convertFromHTML('\n')`
- execute `convertFromHTML('<h1>H</h1>')`
Observe that second call return a block with unstyled type.
Pull Request resolved: facebookarchive/draft-js#1823

Test Plan: Adds a regression test case, run with `jest --watch`

Reviewed By: gmertk

Differential Revision: D17476663

Pulled By: claudiopro

fbshipit-source-id: 05da4d53114aa0d09de61ce55c27f5166b021138
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug CLA Signed Merged needs rebasing Used to flag PRs that need rebasing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants