Created
November 13, 2013 14:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(/"/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 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I <3 buttWipe.js