suneditor icon indicating copy to clipboard operation
suneditor copied to clipboard

New lines are not preserved

Open megamind2301 opened this issue 6 years ago • 3 comments

I recently implemented this library, moving my textareas to Editor. Earlier, new lines were used with \n inside textarea element. After implementing editor, these new lines are removed by the editor.

convertContentsForEditor(core.js) also removes \n from the text, hence removing older formatting of client.

Q1. Should it remove formatting on it's own? Q2. Is there any way to customize this behavior?

megamind2301 avatar Jul 24 '20 05:07 megamind2301

Hi @Megamind2301

\n is not applied in HTML document. The reason to remove \n is to minimize the data size by reducing meaningless spaces between HTML tags. If you use textarea.value as the initial value. Use <br> instead of \n

JiHong88 avatar Jul 24 '20 10:07 JiHong88

Hi @JiHong88 ,

I tried customizing convertContentsForEditor function, when initializing the library. Updated definition of this function by removing .replace(/\n/g, '') line. It did in-fact took care of that problem, and applied \n in HTML document.

You can find below snippet of updated function definition

        lib.core.convertContentsForEditor = (contents)=>{
            contents = contents
                //.replace(/\n/g, '') /*Only needed to remove this line*/
                .replace(/<(script|style).*>(\n|.)*<\/(script|style)>/g, '')
                .replace(lib.core.editorTagsWhitelistRegExp, '');

            const dom = lib.core._d.createRange().createContextualFragment(contents);
            try {
                lib.util._consistencyCheckOfHTML(dom, lib.core._htmlCheckWhitelistRegExp);
            } catch (error) {
                console.warn('[SUNEDITOR.convertContentsForEditor.consistencyCheck.fail] ' + error);
            }

            const domTree = dom.childNodes;
            let cleanHTML = '';
            for (let i = 0, len = domTree.length; i < len; i++) {
                cleanHTML += lib.core._makeLine(domTree[i], true);
            }

            if (cleanHTML.length === 0) return '<p><br></p>';

            cleanHTML = lib.util.htmlRemoveWhiteSpace(cleanHTML);
            return lib.util._tagConvertor(cleanHTML);
        }

But, I am not sure if this is a correct way to do that. Library should handle that internally. \n should not be removed from text node of html.

megamind2301 avatar Jul 24 '20 15:07 megamind2301

Hi @Megamind2301 Why add "\n" to your HTML document? Can you show me the relevant HTML?

JiHong88 avatar Jul 31 '20 09:07 JiHong88