user3731362
user3731362

Reputation: 71

TinyMCE Editor removing whitespace

I am using version 4.1.5 of TnyMCE Editor and when i do assign the HTML content with white space as below

<h1>Hello this is text with                    whiteSpace</h1>

its removing the white space in TinyMce Editor. How we can keep the white space in Tiny MCE Editor?

enter image description here

Regards Vik

Upvotes: 1

Views: 3797

Answers (2)

Charlie
Charlie

Reputation: 23858

Here is a function which can convert your string to an HTML compatible version.

function encodeWhiteSpaces(str) {
   return s.split('').map(function(c) {
    if (c === ' ') 
        return '&nbsp;'
    else
        return c;
   }).join('');
}

The string is converted to an array(split), then created a new arrray (map) with all white spaces converted to &nbsp;, finally join the array back to a string (join).


Also you can use string replace method to convert all the white spaces to &nbsp;

s.replace(" ","&nbsp;");

Upvotes: 2

Thariama
Thariama

Reputation: 50840

It is as Rory already stated. Browsers ignore several spaces. In order to solve this issue you will have to insert/encode your spaces with non-breaking spaces between the regular spaces.

Example:

"      " // six spaces
" &nbsp; &nbsp; &nbsp;" // needed encoding

Upvotes: 0

Related Questions