Created
July 15, 2015 13:29
-
-
Save gerasimua/08d823c5016e25ea9a54 to your computer and use it in GitHub Desktop.
Content editable remove unnecessary tags with content and all html tags on paste
This file contains hidden or 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
$('[contenteditable]').on('paste',function(e) { | |
e.preventDefault(); | |
var text = (e.originalEvent || e).clipboardData.getData('text/html'); | |
var $result = $('<div></div>').append($(text)); | |
// remove unnecesary tags (if paste from word) | |
$result.children('style').remove(); | |
$result.children('meta').remove(); | |
$result.children('link').remove(); | |
var html = $result.html() | |
.replace(/<\/?[^`]+?\/?>/gmi, '\n') //replace all tags | |
.replace(/\n[\s]*\n/gmi, '\n'); //replace many empty lines to one | |
html = $.trim(html); | |
var lines = html.split('\n'); | |
for(var l in lines){ | |
lines[l] = '<div>' + $.trim(lines[l]) + '</div>'; | |
} | |
html = lines.join('\n'); | |
$(this).html(html); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment