Skip to content

Instantly share code, notes, and snippets.

@joelongstreet
Created November 13, 2013 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelongstreet/7449588 to your computer and use it in GitHub Desktop.
Save joelongstreet/7449588 to your computer and use it in GitHub Desktop.
remove endless gobs of garbage produced by the world's worst text editor... MICROSOFT WORD
// Pasting from Microsoft word is an ABSOLUTE DISASTER
// this method removes the endless gobs of garbage produced
// by the world's worst, yet most popular, text editor
// Requires jQuery
var cleanHTML = function(pastedString){
// If this looks like some kind of raunchy ass microsoft bull shit,
// rip off it's effing <HEAD></HEAD>
var headRegex = new RegExp("<head[\\d\\D]*?\/head>", "g");
pastedString = pastedString.replace(headRegex, '');
// Take the body, and ELMINIATE GARBAGE
var pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
var cleaned = pattern.exec(pastedString);
if(cleaned && cleaned.length && cleaned[1]) pastedString = cleaned[1]
// Remove meaningless HTML Comments... you have no idea how
// meaningless these comments are. NO IDEA...
var commentRegex = /<!--[\s\S]*?-->/g;
pastedString = pastedString.replace(commentRegex, '');
// Remove whatever <o:p> and </o:p> is... JUST WHATEVER!!!
pastedString = pastedString.replace(/<o:p>/g, '');
pastedString = pastedString.replace(/<\/o:p>/g, '');
// Take Microsoft's atrocious formatting and correcting it by
// creating a jQuery object, wrapping it in a series of some random
// div(s) and then finally making it HTML again
var tranny = $('<div>' + pastedString + '</div>').html();
tranny = '<div><div>' + tranny.replace(/&quot;/g, '') + '</div></div>';
tranny = $(tranny).find('*').attr('style', '').html();
return tranny
};
// ------ Sample Usage ------ //
var $editor = $('textarea');
$editor[0].onpaste = function(e){
var pasteContent = e.clipboardData.getData('text/html');
if(pasteContent == '') pasteContent = e.clipboardData.getData('text/plain');
var cleanContent = cleanHTML(pasteContent);
$editor.html(cleanContent);
return false
};
@TechnotronicOz
Copy link

I <3 buttWipe.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment