TinyMCE Removes Non-breaking Spaces

December 17, 2009

There are two reasons that TinyMCE might be removing non-breaking space entities ( ) from your HTML source code.  The first one is a common mistake people make when populating textareas.  This is covered in the TinyMCE FAQ, so there’s really no need to elaborate on this particular reason.

The second reason, however, is a bit more complicated, and only occurs if you have the entity_encoding property set to raw:

tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	...
	entity_encoding: 'raw'
});

If your configuration looks similar to this, you’ll most likely see all of your   entities get converted to regular spaces when you switch between HTML view and WYSIWYG view.  The easy solution would be to set entity_encoding to either named or numeric.  Fortunately, if you’re using ISO-8859-1 for your character encoding, you’re all set.  Unfortunately, if you’re using UTF-8 you’ll need to take an additional step to prevent many of your non-English characters from appearing as HTML entities.

The solution is simple.  Set your entity_encoding to named and overwrite the entities property:

tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	...
	entity_encoding: 'named',
	entities: '160,nbsp'
});

The above is the same thing as using raw entity encoding, except that your non-breaking space entities will be preserved.

By Cory LaViska

Cory LaViska

Cory has worked on the web for over a decade. Currently, he runs Surreal CMS and a handful of online tools for web developers. He’s also responsible for the blog you’re reading right now.

Discussion

This discussion is now closed. If you want to leave feedback, you’re welcome to do so on Twitter.

  1. You are my lifesaver, this is exactly what i was looking for. If someone would need a wordpress function to do it without hacking core, just let me know and i will send it to you

    May 16th, 2012Tomáš Kapler