react-summernote icon indicating copy to clipboard operation
react-summernote copied to clipboard

Avoid HTML escaping when setting editor value

Open carlosabalde opened this issue 5 years ago • 4 comments

Hi,

As mentioned in https://github.com/summernote/react-summernote/pull/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? :)

carlosabalde avatar Nov 01 '20 19:11 carlosabalde

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

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

wonieeVicky avatar Nov 03 '20 05:11 wonieeVicky

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! :)

carlosabalde avatar Nov 03 '20 06:11 carlosabalde

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;
}

DenisKhay avatar Dec 11 '20 09:12 DenisKhay

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

aldosolis avatar May 06 '21 04:05 aldosolis