Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove style attributes from tags on SummerNote onPaste?

Is it possible to remove all the style attributes when the user paste html content into Summernote texteditor?

For Example : (Input)

 <p style="font-weight:500; color:#000;">Hello World </p>
 <div style="display:block"> I am a Div content </div>

Expected Output after paste:

 <p>Hello World </p>
 <div> I am a Div content </div>

I want to Implement something like in Javascript/Jquery

$('tag').removeAttr("style");

Is there any in-built options or feature in SummerNote text editor to do this?

Thanks In advance

like image 994
Satheesh Narayanan Avatar asked Apr 19 '17 09:04

Satheesh Narayanan


1 Answers

I've finally done it adapting an another solution (how to paste as plain text: summernote doesn't allow to format pasted text after writing onpaste event)

After succeed to paste in plain text using the solution above, just had to change text to html and remove style as you suggested:

$('.summernote').summernote({
  callbacks: {
    onPaste: function (e) {
      var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('text/html');
      e.preventDefault();
      var div = $('<div />');
      div.append(bufferText);
      div.find('*').removeAttr('style');
      setTimeout(function () {
        document.execCommand('insertHtml', false, div.html());
      }, 10);
    }
  }
}

I don't know browser compatibility of this solution, i only tested on Chrome. But i think it's not so hard to modify.

like image 112
Giovane Tomaz Avatar answered Nov 09 '22 12:11

Giovane Tomaz