Douglas Gaskell
Douglas Gaskell

Reputation: 10030

TinyMCE is adding new line ("\n") regardless of apply_source_formatting

TinyMCE is adding \n as well as paragraph tags to my text. Regardless of the apply_source_formatting settings value.

ie.

Source Text:

<blockquote><div class="cite">Person said:</div><div class="message"><p>ffgfgf</p></div></blockquote>

What TinyMCE Re-formats it to:

<blockquote>\n<div class=\"cite\">Person said:</div>\n<div class=\"message\">\n<p>ffgfgf</p>\n</div>\n</blockquote>\n<p id=\"mce_1\">&nbsp;</p>

How can I get TimyMCE to stop adding new line characters like this? It really messes with the formatting on the other end when the text is submitted.

Upvotes: 7

Views: 6838

Answers (1)

oBusk
oBusk

Reputation: 930

Investigating the source code of TinyMce I've found, that in the Writer.js (now Writer.ts) class, the setting that is checked before inserting \n is called indent.

 /**
 * Writes the a end element such as </p>.
 *
 * @method end
 * @param {String} name Name of the element.
 */
end: function(name) {
    var value;

    html.push('</', name, '>');

    if (indent && indentAfter[name] && html.length > 0) {
        value = html[html.length - 1];

        if (value.length > 0 && value !== '\n') {
            html.push('\n');
        }
    }
},

So adding indent: false to the settings object seems to fix it.

tinymce.init({
  selector: 'textarea',  // change this value according to your HTML
  indent: false,
});

From documentation:

This option, which is on by default, adds a newline character — U+000A, \n — between closing and opening block elements when HTML is output using getContent() and when HTML is rendered in the TinyMCE Preview dialog.

Set indent to false to get HTML output from TinyMCE without having any newline characters added.

Upvotes: 20

Related Questions