Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

c# - IHTMLTxtRange.pastHTML limit?

I have the following problem. I have to find a phrase in html document and surround each word in the phrase in a span element. I'm searching with this code:

        var range = body.createTextRange();
        range.moveToElementText(body);
        var i = 0;
        do
        {
            range.findText(note.Text, range.text.Length, 0);
            i++;
            if (i < note.Occurence)
                range.moveStart("character", 1);
        } while (i != note.Occurence);

Then I surround every word in range.text with a span element and I try to replace original phrase with the one surrounded by spans. Everything works fine if the phrase contains only one or a few words, but when the phrase is big, my spans are not inserted in html at all. for example, when I try to paste the following html in range:

<span id='0' style='background-color:gray' class='Annotation'>?????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>???????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>?????????????</span> <span id='0' style='background-color:gray' class='Annotation'>????</span> <span id='0' style='background-color:gray' class='Annotation'>?????????</span> <span id='0' style='background-color:gray' class='Annotation'>????</span> <span id='0' style='background-color:gray' class='Annotation'>???????????????????,</span> 
<span id='0' style='background-color:gray' class='Annotation'>??????,</span> <span id='0' style='background-color:gray' class='Annotation'>???</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>???????????,</span> <span id='0' style='background-color:gray' class='Annotation'>??????</span> <span id='0' style='background-color:gray' class='Annotation'>???????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> 
<span id='0' style='background-color:gray' class='Annotation'>??????????,???????????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>????????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> 
<span id='0' style='background-color:gray' class='Annotation'>???????????</span> <span id='0' style='background-color:gray' class='Annotation'>???????</span> <span id='0' style='background-color:gray' class='Annotation'>???????????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>?????.</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>????</span> 
<span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>?????</span> <span id='0' style='background-color:gray' class='Annotation'>?????????????????</span> <span id='0' style='background-color:gray' class='Annotation'>?????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????</span> <span id='0' style='background-color:gray' class='Annotation'>?????</span> <span id='0' style='background-color:gray' class='Annotation'>??????,</span> <span id='0' style='background-color:gray' class='Annotation'>??????</span> 
<span id='0' style='background-color:gray' class='Annotation'>?????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span>

span elements are stripped off from the document and I get plain text. Any idea why is this happening? Thanks.

UPDATE

After doing some more tests, looks like passing existing html (without modification) markup to pasteHtml breaks the output (and inserts <br> tags for unknown reason).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like pasteHtml is a bit buggy. I found the solution by acquiring range parent element via range.parentElement() and manipulating it's innerHtml (replace, append etc).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...