How to implement text replacement with IHTMLTxtRange?
I'm using an IHTMLTxtRange object instance to replace the current user's selection in the browser.
Today, i came across the following problem. This is my markup:
<body><p><a href="http://www.google.com">http://www.google.com</a></p></body>
The user selection (html), as informed by the IHMLTxtRange instance (range), is:
<a href="http://www.google.com">http://www.google.com</a>
Whenever i execute range.replaceHTML("test");
the result is:
<body><p><a href="http://www.google.com">test</a></p></body>
This behavior seems to apply also to scenarios like this:
<body><p><b>some text</b></p></body>
Can anyone help me to de开发者_运维百科velop a consistent selection replacement strategy? I mean, that text selection works as expected.
Thanks
Update 10/05/2012: Clarified experienced problem
Coming back now I realize that the problem was not explicited correctly. I expected to replace the whole string <a href="http://www.google.com">http://www.google.com</a>
with test
. Instead, the innerText
of the anchor was replaced, leaving it´s markup untouched.
Solution Review
Finally, I've found the answer to this one through the Internet Explorer Extension Development forums.
As a side note, from the the answer I received, this problem also seems to happen when using the pasteHTML
method on an IHTMLTxtRange
object. There's even a closed issue describing this problem in the Microsoft Connect website.
Workaround
I fixed the problem, based on the answer I got, by checking whether the current element in the range is an anchor: if that's the case, the parent element of the current element is removed and the desired text is inserted in its place.
I had to do this check also for styling elements: <B>
, <I>
and <FONT>
tags.
Be sure to check the question I got answered for more information.
精彩评论