3

アプリケーションで NicEdit リッチ テキスト エディター (コンテンツ編集可能な div ベース) を使用していますが、ユーザーは単語から貼り付けるのが大好きです。

そのため、貼り付けられる可能性のあるすべてのジャンク タグを取り除きたいと考えました。

これが私が現在行っていることです。

//build regex to match junk tags
var unwantedtags = [ "font", "span", "table", "tbody", "div", "td", "tr", "input", "a", 
    "body", "button", "form", "head", "img", "select", "textarea", "tfoot", "th", "iframe", "object" ];     
var unwantedregexstring= "";
$.each(unwantedtags, function(index, value) {
    if(unwantedregexstring!= "") {
        unwantedregexstring += "|";
    }
    unwantedregexstring+= "<" + value + ">";
    unwantedregexstring+= "|";
    unwantedregexstring+= "<" + value + "\\s[^>]*>";
    unwantedregexstring+= "|";
    unwantedregexstring+= "</" + value + ">";
});
var unwantedRegex = new RegExp(unwantedregexstring, "igm");

//replace junk tags with nothing
function CleanMSWordPaste(mswordtext) {
    return  mswordtext.replace(unwantedRegex, "");
}

//Function that gets Executed on Paste event
function ExecutePaste(){

    //preserve user's selected text
    var oldRng = document.selection.createRange();

    //create paste area off screen and paste there
    $('body').append("<div id='paster' contenteditable='true' style='height:1px;width:1px;position:fixed;left:-100px;top:-100px;'></div>");
    $('#paster').focus();
    $('#paster')[0].document.execCommand('paste', null, null);

    //if html contains junk tags
    if(unwantedRegex.test($('#paster').html())) {
        //replace html with cleaned html
        $('#paster').html(CleanMSWordPaste($('#paster').html()));

        //select all content of paste area
        var rng = document.body.createTextRange();
        rng.moveToElementText($('#paster')[0]);
        rng.select();

        //copy cleaned html
        $('#paster')[0].document.execCommand('copy', null, null);
    }

    //remove paste area from dom
    $('#paster').remove();

    //restore user's selected text
    oldRng.select();

    //preserves scroll position, focuses NicEditor and performs doc.execCommand('paste')
    //performance of this alone is fine.
    ExecCommand('paste');
}

これにはかなりの時間がかかっていることがわかりました (例: Word の 1 ページのテキスト)。これをスピードアップするためにできることはありますか?ある種の正規表現の最適化を考えていますが、そもそも正規表現がどのように機能するかについてはまったく知りません。

4

2 に答える 2

1

できることの 1 つは、への参照を保存すること$('#paster')です。これにより、$() を常に再実行する必要がなくなります。

$paster = $('#paster') //dollar sign in variable name not necessary
                       //I just do it so I know it's a JQuery object.
unwantedRegex.test($paster.html());
$paster.focus();
//etc

また

$('#paster')[0].document.execCommand('copy', null, null);

何が起こっているのかわかりません$('#paster')[].documentdocument.execCommand() を実行できるはずですよね?

于 2013-03-19T15:50:13.857 に答える
1

unwantedregexstring最終的には次のようになります。

'<font>|<font\s[^>]*>|</font>|<span>|<span\s[^>]*>|</span>|...'

私は正規表現エンジンの内部の専門家ではありませんが、少し冗長に見えます。unwantedregexstring代わりに、このように見えるようにアルゴリズムを変更するとどうなりますか?

'</?(font|span|...)\s?.*?>'

<これは、 a の後に任意の/が続き、その後に指定したタグの 1 つが続き、その後に任意の空白文字が続き、その後に 0 個以上の任意の文字ができるだけ少ない文字が続き、終了が検出されるまで検索されます>

于 2013-03-19T15:58:28.060 に答える