简体   繁体   English

如何加快我的javascript粘贴操作?

[英]How can I speed up my javascript paste operation?

I'm using a NicEdit rich text editor (content editable div based) in my application and users love to paste from word. 我在我的应用程序中使用了一个NicEdit富文本编辑器(基于内容的可编辑div),用户喜欢从单词粘贴。

So I wanted to strip out any and all junk tags that might get pasted in. 所以我想删除任何可能粘贴的垃圾标签。

Here is what I'm currently doing. 这是我目前正在做的事情。

//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');
}

I'm finding that this is taking quite a long time (ex 1 page of text from word). 我发现这需要相当长的时间(来自单词的前1页)。 Is there anything I can do to speed this up? 有什么办法可以加快速度吗? I'm thinking some sort of regex optimization but I don't really have any knowledge of how regexes work in the first place. 我正在考虑某种正则表达式优化,但我并不真正了解正则表达式如何工作。

One thing you can do is save a reference to $('#paster') so you don't have to constantly re-run $(), which is a rather complex function that can be pretty heavy. 你可以做的一件事是保存对$('#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

Also

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

Not sure what's going on that you're using $('#paster')[].document You should just be able to run document.execCommand(), no? 不确定你正在使用$('#paster')[].document你应该只能运行document.execCommand(),不是吗?

It seems that your unwantedregexstring will end up looking something like this: 似乎你的unwantedregexstring最终会看起来像这样:

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

I'm no expert in regexp engine internals, but that looks a bit overly verbose to me. 我不是regexp引擎内部的专家,但对我来说这看起来有点过于冗长。 What if you change your algorithm so that unwantedregexstring looks like this instead? 如果您更改算法以使unwantedregexstring看起来像这样,该怎么办?

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

That will look for a < followed by an optional / followed by one of your specified tags followed by an optional whitespace character followed by zero or more but as few as possible of any character, until the closing > is encountered. 这将查找一个<后跟一个可选的/后跟一个指定的标记,后跟一个可选的空格字符,后跟零或更多,但尽可能少的任何字符,直到遇到结束>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM