Skip to content

Instantly share code, notes, and snippets.

@hiteshaggarwal
Last active August 12, 2021 06:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save hiteshaggarwal/388cd3fae7331aa0415c63e0a883e8f5 to your computer and use it in GitHub Desktop.
Save hiteshaggarwal/388cd3fae7331aa0415c63e0a883e8f5 to your computer and use it in GitHub Desktop.
/**
* Summernote StripTags
*
* This is a plugin for Summernote (www.summernote.org) WYSIWYG editor.
* To strip unwanted HTML tags and attributes while pasting content in editor.
*
* @author Hitesh Aggarwal, Extenzine
*
*/
(function (factory) {
/* global define */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(window.jQuery);
}
}(function ($) {
$.extend($.summernote.options, {
stripTags: ['font', 'style', 'embed', 'param', 'script', 'html', 'body', 'head', 'meta', 'title', 'link', 'iframe', 'applet', 'noframes', 'noscript', 'form', 'input', 'select', 'option', 'colgroup', 'col', 'std', 'xml:', 'st1:', 'o:', 'w:', 'v:'],
stripAttributes: ['font', 'style', 'embed', 'param', 'script', 'html', 'body', 'head', 'meta', 'title', 'link', 'iframe', 'applet', 'noframes', 'noscript', 'form', 'input', 'select', 'option', 'colgroup', 'col', 'std', 'xml:', 'st1:', 'o:', 'w:', 'v:'],
onAfterStripTags: function ($html) {
return $html;
}
});
$.extend($.summernote.plugins, {
'striptags': function (context) {
var $note = context.layoutInfo.note;
var $options = context.options;
$note.on('summernote.paste', function (e, evt) {
evt.preventDefault();
var text = evt.originalEvent.clipboardData.getData('text/plain'), html = evt.originalEvent.clipboardData.getData('text/html');
if (html) {
var tagStripper = new RegExp('<[ /]*(' + $options.stripTags.join('|') + ')[^>]*>', 'gi'), attributeStripper = new RegExp(' (' + $options.stripAttributes.join('|') + ')(="[^"]*"|=\'[^\']*\'|=[^ ]+)?', 'gi'), commentStripper = new RegExp('<!--(.*)-->', 'g');
html = html.toString().replace(commentStripper, '').replace(tagStripper, '').replace(attributeStripper, ' ').replace(/( class=(")?Mso[a-zA-Z]+(")?)/g, ' ').replace(/[\t ]+\</g, "<").replace(/\>[\t ]+\</g, "><").replace(/\>[\t ]+$/g, ">").replace(/[\u2018\u2019\u201A]/g, "'").replace(/[\u201C\u201D\u201E]/g, '"').replace(/\u2026/g, '...').replace(/[\u2013\u2014]/g, '-');
}
var $html = $('<div/>').html(html || text);
$html = $options.onAfterStripTags($html);
$note.summernote('insertNode', $html[0]);
return false;
});
}
});
}));
// How to use
/*
(function ($) {
$(function () {
$('.summernote').summernote({
toolbar: [
['custom', ['striptags']],
],
striptags: {
stripTags: ['style'],
stripAttributes: ['border', 'style'],
onAfterStripTags: function ($html) {
$html.find('table').addClass('table');
return $html;
}
}
});
});
})(jQuery);
*/
@rendy1287
Copy link

rendy1287 commented May 3, 2018

Its doesn't work with SummerNote v0.8.10 BS4 version, only paste a blank <div></div> content

I am using airMode: true

edit

I am using Google Chrome, the bug coming from Google Chrome auto paste with <!--StartFragment--><!--EndFragment-->

I have already found the solution

change commentStripper to commentStripper = new RegExp('<!--(.*?)-->', 'g');

@oTiMo
Copy link

oTiMo commented Jul 12, 2019

According to your code, you have to set options like this :

$('.summernote').summernote({
  toolbar: [
    ['custom', ['striptags']],
  ],
  stripTags: ['style'],
  stripAttributes: ['border', 'style'],
  onAfterStripTags: function ($html) {
    $html.find('table').addClass('table');
    return $html;
  }
});

@dzpt
Copy link

dzpt commented Sep 18, 2019

@oTiMo It doesn't strip class even after adding it.

@seltix5
Copy link

seltix5 commented Nov 18, 2019

Hello,
Thanks @rendy1287 and @oTiMo your solutions did work for me.
I have other problems, first, the content to paste is wrapped in a DIV. Second, summernote is adding < p >< br >< /p >.
Any one has a solutions?
Thanks!

@vaxjo
Copy link

vaxjo commented Mar 2, 2021

Hello,
Thanks @rendy1287 and @oTiMo your solutions did work for me.
Second, summernote is adding < p >< br >< /p >.
Any one has a solutions?

This is a known bug of the internal pasteHTML command.

summernote/summernote#3370

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