I have users copying and pasting content from a webpage into a SharePoint 2010 RTE. Unbeknownst to them, they are also copying styles that the RTE converts to span tags. Upon paste, I'd like to automatically remove any/all span tags but keep any remaining markup. I realize that there will be other related issues but removing the span tags will get me much closer to where I need to be.

I've found a code snippet here that removes all markup upon paste.

//Disable the RTE paste option. Restricts to "Paste Plain Text" 
function disableMarkupPasteForRTE()
 if (RTE)
  if(RTE.RichTextEditor != null)
   RTE.RichTextEditor.paste = function() { RTE.Cursor.paste(true); }
   // Handle Ctrl+V short cut options in rich text editor
   RTE.Cursor.$3C_0 = true;


Is anyone able to modify the above code to only remove span tags?

2 Answers 2


The SharePoint RTE is nothing more than a suped-up ContentEditable div. The solution is to bind a function onpaste to the editor and then manipulate the html directly.

$(document).ready(function() {
    $(".ms-rtestate-write").bind('paste', function(e) {
        var text = $(this).html();
        $(this).html(text.replace( /<\s*span.*?>/gi,"").replace( /<\s*\/\s*span\s*.*?>/gi,""));

You can use Sharepoint paste method and modify the html like this:

RTE.Cursor.$1G_0 = true;
RTE.Cursor.$S_0.style.display = '';
var $v_0 = RTE.Cursor.$S_0.contentWindow.document;
var $v_1 = $v_0.body;
var $v_2 = RTE.DomHelper.createRange($v_0);
var $v_3 = new RTE.Range($v_2);
$v_0.execCommand('paste', false, null);

html = $v_2.htmlText;
//modify the html source

//paste back your new html

var $v_4 = RTE.Cursor.get_range();
var $v_5 = $v_4.$1N();
RTE.Cursor.$S_0.style.display = 'none';
RTE.Cursor.$2U_0 = true;
RTE.Cursor.$28_0 = true;
window.setTimeout(RTE.Cursor.$9w, 0);
if (RTE.Cursor.$1u_0 > 0) {
    window.setTimeout(RTE.Cursor.$5p_0, 10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.