Skip to content

Instantly share code, notes, and snippets.

@gerasimua
Created July 15, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerasimua/08d823c5016e25ea9a54 to your computer and use it in GitHub Desktop.
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
$('[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