Repeat after me... "tinyMCE is not an HTML editor"
A "Remove MS-Word Formating in tinyMCE" Callback
Ok first let me explain that this is a quick hack. I use tinyMCE to allow my customers to edit most of the content on their websites. One of the things I hate about tinyMCE is that it was created to mimic MicroSoft Word, not to be a HTML editor. With that being said, I hate that tinyMCE is trying to mimic MS-Word. I want to give my clients the ability to edit the content on their site, and I would like that content to be simple HTML, not complicated MS-Word syntax. And one of the things my clients love to do is paste content into the tinyMCE editor window from MS-Word.
Well this little hack does it's best to remove/re-style what they paste in. Just add this function in your javascript (ideally above the tinyMCE.init() declaration:
function cleanUpFormating(inst) {
var content = inst.getBody().innerHTML;
// WORD CLEANUP
content = content.replace(new RegExp('<p.*?class[\s]*=[\s]*["|\']?MsoHeading["|\']?.*?[^>]*>(.*?)<\/p>','gim'),'<p><strong>$1<\/strong><\/p>');
content = content.replace(new RegExp('<p[^>]*>','gim'),'<p>');
content = content.replace(new RegExp('<strong[^>]*>','gim'),'<strong>');
content = content.replace(new RegExp('<em[^>]*>','gim'),'<em>');
content = content.replace(new RegExp('<span[^>]*>','gim'),'');
content = content.replace(new RegExp('<\/span>','gim'),'');
content = content.replace(new RegExp('<font[^>]*>','gim'),'');
content = content.replace(new RegExp('<\/font>','gim'),'');
// REMOVE TAGS
content = content.replace(new RegExp('<img[^>]*>','gim'),'');
content = content.replace(new RegExp('<div[^>]*>(.*?)<\/div>','gim'),'<p>$1<\/p>');
// REMOVE EMPTY TAGS
for (i=0;i<5;i++) {
content = content.replace(new RegExp('<(\w+)[^>]*>\s*?</$1>','gim'),'');
}
// REPLACE NON-ASCII
content = content.replace(new RegExp('[^\0-\x80]','gim'),'');
inst.getBody().innerHTML = content;
}
... and then add this line to your tinyMCE.init() setup values:
onchange_callback : "cleanUpFormating"
Now, everytime the user makes a change, it will clean up the code in the editor window. Shout out below, if you see any problems or see an improvement you can add to it.
P.S. I should be back to my regularly schedule blog postings after I finish some projects.
Anyway, let me know if this gives anyone any problems... again it was a quick hack.
I just found out about this blogpost and I try to add it to tinyMCE 3.2.0.1 (2008-09-17). I can't seem to find where to put the "onchange_callback" statement.
I actually started running into some performance issues with this version, I just posted another article on another way to go about this, using tinyMCE.
http://www.cjboco.com/index.cfm/2008/9/19/Repeat-a...
But if you want to try using this version you just need to place the onchange_callback in with all your other tinyMCE declarations. So it might look something like this:
tinyMCE.init({
mode : "exact",
elements : "PageCopy",
theme : "advanced",
onchange_callback : "cleanUpFormating"
});
Let me know if you still have any problems.