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

Bug in convertFromHTML (empty lines are missing) #1082

Open
matiosfree opened this issue Mar 17, 2017 · 10 comments
Open

Bug in convertFromHTML (empty lines are missing) #1082

matiosfree opened this issue Mar 17, 2017 · 10 comments

Comments

@matiosfree
Copy link

matiosfree commented Mar 17, 2017

convertFromHTML works wrong. Let's see examples.

https://jsfiddle.net/m6z0xn4r/176/
There we see 3 different blocks instead of 4. And each block has 1 test row and 1 blank row

https://jsfiddle.net/m6z0xn4r/177/
There we see good pasting text, but third line (empty) is missed.

Actualy, I expect:
The first line //data-offset-key="b7tn6-0-0"
The second line //data-offset-key="2oula-0-0"
empty line //data-offset-key="avbam-0-0"
The fourth line //data-offset-key="3l8as-0-0"

in v0.9 it worked fine

I use DraftJS 0.10 + Webpack 2.0

@matiosfree matiosfree changed the title Bug in convertFromHTML Bug in convertFromHTML (empty lines are missing) Mar 17, 2017
@cpwinn
Copy link

cpwinn commented Mar 17, 2017

I ran into this (or very similar) issue this week also. Though, I could replicate the same problem in v0.9.1 so I figured it was intentional somehow. I've setup a similar test with a range of HTML block elements that I'd expect to get converted to ContentBlocks. Sadly only blocks with text get converted.

Here is my Stack Overflow question on it.
Here is my ver 0.10.0 jsfiddle
Here is my ver 0.9.1 jsfiddle

@maierson
Copy link

Same here (missing empty lines) when converting from previously saved as html content to draft-js content. DraftJS 0.10 + Webpack 2.0

@NoZiL
Copy link

NoZiL commented Mar 24, 2017

I resolved many problems by using https://github.com/sstur/draft-js-import-html instead

@cpwinn
Copy link

cpwinn commented Mar 24, 2017

It looks like draft-js-import-html might get the job done. I might have to manipulate my HTML a bit but at least it supports empty blocks. Also, I'll have to ignore some console warnings until they implement DraftJs 0.10 support but it could be worse.
Thanks for the tip @NoZiL.

@maierson
Copy link

Works here too on first try. Thank you @NoZiL

@maierson
Copy link

maierson commented Mar 26, 2017

Update: turns out that draft-js-import-html doesn't import inline styles from html spans if existing. we ended up going with draft-convert which offers both options when using flat:true flag.

const contentState = convertFromHTML({
        // html restore rules
        htmlToStyle: (nodeName, node, currentStyle) => {
            if (nodeName === 'span' && node.style.color) {
                // restore custom color values 
                // must be present in the editorStyleMap
                const color = replaceAll(node.style.color, " ", "");
                return currentStyle.add(`COL_${color}`);
            } else {
                return currentStyle;
            }
        },
        htmlToEntity: (nodeName, node) => {
            // restore links
            if (nodeName === 'a') {
                return Entity.create(
                    'LINK',
                    'MUTABLE',
                    { url: node.href }
                )
            }
        }
    })(htmlString, { flat: true });

@PManager1
Copy link

i get this error : : 'convertFromHTML' is not defined no-undef

convertFromHTML

@thibaudcolas
Copy link
Contributor

This is probably the same issue as #738 and #523.

@micchyboy
Copy link

This one worked for my issues https://github.com/jpuri/html-to-draftjs

Was able to read multiple new lines and text-align perfectly

@prawana-perera
Copy link

If anyone is still looking, I also ended up using draft-convert, I also had to preserve links as well (can preserve more stuff using the decordator)...and my code was as follows:

const Link = (props) => {
    const {url} = props.contentState.getEntity(props.entityKey).getData();
    return (
        <a href={url} target="_blank">
            {props.children}
        </a>
    );
};

const  findLinkEntities = (contentBlock, callback, contentState) => {
    contentBlock.findEntityRanges(
        (character) => {
            const entityKey = character.getEntity();
            return (
                entityKey != null && contentState.getEntity(entityKey).getType() === 'LINK'
            );
        },
        callback
    );
};

const decorator = new CompositeDecorator([
    {
        strategy: findLinkEntities,
        component: Link,
    }
]);
const html = '...whatever...html';

const blocksFromHTML = convertFromHTML({
            htmlToEntity: (nodeName, node, createEntity) => {
                if (nodeName === 'a') {
                    return createEntity('LINK', 'MUTABLE', { url: node.href });
                }
            }
        })(html);

const contentState = ContentState.createFromBlockArray(
            blocksFromHTML.getBlocksAsArray(),
            blocksFromHTML.getEntityMap
        );

editorState = EditorState.createWithContent(contentState, decorator);
...
...
<Editor
                    editorState={editorState}
                    ...etc
                />

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

8 participants