Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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()
{
 Type.registerNamespace("RTE");
 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;
  }
 }
}

_spBodyOnLoadFunctionNames.push("disableMarkupPasteForRTE");

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

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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,""));
    });
});
share|improve this answer
add comment

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

RTE.Cursor.$1u_0--;
RTE.Cursor.$1G_0 = true;
RTE.Cursor.$A2();
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_3.moveToElementText($v_1);
$v_3.select();
$v_0.execCommand('paste', false, null);

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

//paste back your new html
$v_2.pasteHTML(newhtml);

RTE.Cursor.updateRangeToCurrentSelectionWithOptions(true);
var $v_4 = RTE.Cursor.get_range();
var $v_5 = $v_4.$1N();
$v_4.deleteContent();
$v_5.select();
RTE.Cursor.$S_0.style.display = 'none';
RTE.Cursor.$2U_0 = true;
RTE.Cursor.$28_0 = true;
RTE.Cursor.$Ei_0();
RTE.Cursor.$59();
RTE.Cursor.$A2();
window.setTimeout(RTE.Cursor.$9w, 0);
if (RTE.Cursor.$1u_0 > 0) {
    window.setTimeout(RTE.Cursor.$5p_0, 10);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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