IHTMLTxtRange.pastHTML limit?

自作多情 提交于 2019-12-13 08:21:47

问题


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).


回答1:


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).



来源:https://stackoverflow.com/questions/21959096/ihtmltxtrange-pasthtml-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!