从SharePoint 2010富文本编辑器(RTE)中删除粘贴上的所有span标记

时间:2012-07-19 15:48:36

标签: sharepoint sharepoint-2010 richtextbox

我让用户从随机网页复制内容并粘贴到SharePoint 2010 RTE中。他们不知道的是,他们还从原始网页复制样式,RTE将其转换为样式的span标记,并且这些span标记覆盖了页面上的默认文本样式。粘贴后,我想自动删除所有/所有span标签,但保留任何剩余标记。我意识到会有其他相关问题,但删除span标签会让我更接近我需要的位置。

我找到了一个代码段here,可以在粘贴时删除所有标记。

//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");

是否有人能够修改上述代码才能删除span标记?

2 个答案:

答案 0 :(得分:0)

SharePoint RTE只不过是一个内容丰富的ContentEditable div。解决方案是将函数onpaste绑定到编辑器,然后直接操作html。

$(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,""));
    });
});

答案 1 :(得分:0)

您可以使用Sharepoint粘贴方法并修改html,如下所示:

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);
}
相关问题