New lines are not preserved
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?
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
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.
Hi @Megamind2301 Why add "\n" to your HTML document? Can you show me the relevant HTML?