How to remove style attributes from tags on SummerNote onPaste?
Asked Answered
R

1

7

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

Respondence answered 19/4, 2017 at 9:24 Comment(2)
I'm interested in this too. I wonder if github.com/summernote/summernote/issues/303 will help us.Oceanic
Or css-tricks.com/snippets/javascript/remove-inline-stylesOceanic
E
9

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.

Embankment answered 23/5, 2019 at 16:4 Comment(1)
Works fine in FF v77 too. Should not really depend on the browser as long as it's standard JS (or jQuery for a matter of fact).Capps

© 2022 - 2024 — McMap. All rights reserved.