Skip to content

Instantly share code, notes, and snippets.

@jasonbyrne
Created October 14, 2015 18:21
Show Gist options
  • Save jasonbyrne/ed3572949091347bca90 to your computer and use it in GitHub Desktop.
Save jasonbyrne/ed3572949091347bca90 to your computer and use it in GitHub Desktop.
Plugin for Summernote WYSIWYG editor that cleans up the pasted in content
/**
* Summernote PasteClean
*
* This is a plugin for Summernote (www.summernote.org) WYSIWYG editor.
* It will clean up the content your editors may paste in for unknown sources
* into your CMS. It strips Word special characters, style attributes, script
* tags, and other annoyances so that the content can be clean HTML with
* no unknown hitchhiking scripts or styles.
*
* @author Jason Byrne, FloSports <[email protected]>
*
*/
(function (factory) {
/* global define */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals: jQuery
factory(window.jQuery);
}
}(function ($) {
var badTags = [
'font',
'style',
'embed',
'param',
'script',
'html',
'body',
'head',
'meta',
'title',
'link',
'iframe',
'applet',
'noframes',
'noscript',
'form',
'input',
'select',
'option',
'std',
'xml:',
'st1:',
'o:',
'w:',
'v:'
],
badAttributes = [
'style',
'start',
'charset'
];
$.summernote.addPlugin({
name: 'pasteClean',
init: function(layoutInfo) {
var $note = layoutInfo.holder();
$note.on('summernote.paste', function(e, evt) {
evt.preventDefault();
// Capture pasted data
var text = evt.originalEvent.clipboardData.getData('text/plain'),
html = evt.originalEvent.clipboardData.getData('text/html');
// Clean up html input
if (html) {
// Regular expressions
var tagStripper = new RegExp('<[ /]*(' + badTags.join('|') + ')[^>]*>', 'gi'),
attributeStripper = new RegExp(' (' + badAttributes.join('|') + ')(="[^"]*"|=\'[^\']*\'|=[^ ]+)?', 'gi'),
commentStripper = new RegExp('<!--(.*)-->', 'g');
// clean it up
html = html.toString()
// Remove comments
.replace(commentStripper, '')
// Remove unwanted tags
.replace(tagStripper, '')
// remove unwanted attributes
.replace(attributeStripper, ' ')
// remove Word classes
.replace(/( class=(")?Mso[a-zA-Z]+(")?)/g, ' ')
// remove whitespace (space and tabs) before tags
.replace(/[\t ]+\</g, "<")
// remove whitespace between tags
.replace(/\>[\t ]+\</g, "><")
// remove whitespace after tags
.replace(/\>[\t ]+$/g, ">")
// smart single quotes and apostrophe
.replace(/[\u2018\u2019\u201A]/g, "'")
// smart double quotes
.replace(/[\u201C\u201D\u201E]/g, '"')
// ellipsis
.replace(/\u2026/g, '...')
// dashes
.replace(/[\u2013\u2014]/g, '-');
}
// Do the paste
var $dom = $('<div class="pasted"/>').html(html || text);
$note.summernote('insertNode', $dom[0]);
return false;
});
}
});
}));
@hiteshaggarwal
Copy link

Grate Script!
Above script really doesn't work. I modified plugin and working for me. Thank you

https://gist.github.com/hiteshaggarwal/388cd3fae7331aa0415c63e0a883e8f5

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