Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid HTML escaping when setting editor value #91

Open
carlosabalde opened this issue Nov 1, 2020 · 4 comments
Open

Avoid HTML escaping when setting editor value #91

carlosabalde opened this issue Nov 1, 2020 · 4 comments

Comments

@carlosabalde
Copy link

Hi,

As mentioned in #51, after that PR was merged, I don't find a way to properly initialise a ReactSummernote component with HTML content. For example, if this.props.value is set to Hello <b>world</b>! and I use children={ this.props.value }, the editor is initialised with the HTML escaped version (i.e. Hello &lt;b&gt;world&lt;/b&gt;!).

I guess I'm missing something obvious here, but I still not able to find the solution. Help? :)

@wonieeVicky
Copy link

I have the same Issue.
So I use dangeroulySetInnerHTML in {chilren} props :)

<ReactSummernote
      children={<div dangerouslySetInnerHTML={{ __html: value }}></div>}

@carlosabalde
Copy link
Author

In the beginning I tried that approach too, but it adds and extra div wrapper to edited contents and that was not acceptable. Then I 'fixed' this setting the initial value using jQuery during componentDidMount(). That worked fine, but then a few more bugs popped up. At the end I switched to a different React WYSIWYG editor. I liked React Summernote simplicity and cleanness, but it looks like the project is slightly abandoned.

In any case, thanks a lot for the hint! :)

@DenisKhay
Copy link

DenisKhay commented Dec 11, 2020

You also can just unescape the string before use it in the editor

const unescapeString = (str: string): string => {
    const newString = new DOMParser().parseFromString(str ?? '', 'text/html').documentElement.textContent;
    return newString != null ? DomPurify.sanitize(newString) : str;
}

@aldosolis
Copy link

You can do the following
<ReactSummernote onInit={({summernote}) => summernote('code', yourcodegoeshere)} />

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

No branches or pull requests

4 participants