Skip to content

Instantly share code, notes, and snippets.

@hiteshaggarwal
Last active September 4, 2024 18:07
Show Gist options
  • Save hiteshaggarwal/388cd3fae7331aa0415c63e0a883e8f5 to your computer and use it in GitHub Desktop.
Save hiteshaggarwal/388cd3fae7331aa0415c63e0a883e8f5 to your computer and use it in GitHub Desktop.

Revisions

  1. hiteshaggarwal revised this gist Feb 13, 2018. 1 changed file with 10 additions and 12 deletions.
    22 changes: 10 additions & 12 deletions summernote-striptags.js
    Original file line number Diff line number Diff line change
    @@ -22,12 +22,10 @@
    }
    }(function ($) {
    $.extend($.summernote.options, {
    striptags: {
    tags: ['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:'],
    attributes: ['style', 'start', 'charset', 'cellpadding', 'border', 'cellspacing', 'width', 'height', 'class'],
    updated: function ($html) {
    return $html;
    }
    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;
    }
    });

    @@ -39,11 +37,11 @@
    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.tags.join('|') + ')[^>]*>', 'gi'), attributeStripper = new RegExp(' (' + $options.striptags.attributes.join('|') + ')(="[^"]*"|=\'[^\']*\'|=[^ ]+)?', 'gi'), commentStripper = new RegExp('<!--(.*)-->', 'g');
    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.striptags.updated($html);
    $html = $options.onAfterStripTags($html);
    $note.summernote('insertNode', $html[0]);
    return false;
    });
    @@ -61,14 +59,14 @@
    ['custom', ['striptags']],
    ],
    striptags: {
    tags: ['style'],
    attributes: ['border', 'style'],
    updated: function ($html) {
    stripTags: ['style'],
    stripAttributes: ['border', 'style'],
    onAfterStripTags: function ($html) {
    $html.find('table').addClass('table');
    return $html;
    }
    }
    });
    });
    })(jQuery);
    */
    */
  2. hiteshaggarwal created this gist Feb 13, 2018.
    74 changes: 74 additions & 0 deletions summernote-striptags.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    /**
    * 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: {
    tags: ['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:'],
    attributes: ['style', 'start', 'charset', 'cellpadding', 'border', 'cellspacing', 'width', 'height', 'class'],
    updated: 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.tags.join('|') + ')[^>]*>', 'gi'), attributeStripper = new RegExp(' (' + $options.striptags.attributes.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.striptags.updated($html);
    $note.summernote('insertNode', $html[0]);
    return false;
    });
    }
    });

    }));

    // How to use
    /*
    (function ($) {
    $(function () {
    $('.summernote').summernote({
    toolbar: [
    ['custom', ['striptags']],
    ],
    striptags: {
    tags: ['style'],
    attributes: ['border', 'style'],
    updated: function ($html) {
    $html.find('table').addClass('table');
    return $html;
    }
    }
    });
    });
    })(jQuery);
    */