MSHTML PasteHTML function duplicating Tag

G

Guest

Hi,

I'm developing an HTML Editor Control using VB.Net 2003 for an application
that used to use the DHTML Editor Control that is no longer supported. Well,
it's been fun but I've hit a wall with an intermittent problem working with
editing Hyperlinks.

As you can see the following HTML code contains two links each in a
paragraph tag.
In the Editor, when I highlight the first link and execute code to select,
turn into a TxtRange object, update the tag href and then use the
txtRange.PasteHTML function the output duplicates the link. (see second html)

<HTML><HEAD>
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>
<P><A title=Tip href="http://test.com/">Test</A></P>
<P><A title=tip2 href="http://test2.com/">Test2</A></P></BODY></HTML>

HTML showing duplicate

<HTML><HEAD>
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>
<P><A title=Tip href="http://test.com/">Test</A><A title=Tip
href="http://test.com/"></A></P>
<P><A title=tip2 href="http://test2.com/">Test2</A></P></BODY></HTML>

As you can see above the link got duplicated.. This problem only appears to
happen when the anchor tag resides inside a paragraph tag with no space
between the </a> and </p>. Is there a bug in the MSHTML dll or am I missing
something in the code??

Here is the Code or rather a snippet of the part that's important to the
problem

Dim Doc As mshtml.IHTMLDocument2 = DirectCast(EditorHTML.Document,
mshtml.IHTMLDocument2)
Dim oSelection As mshtml.IHTMLSelectionObject = Doc.selection
Dim oDocLink As mshtml.HTMLAnchorElementClass
Dim oTxtRange As mshtml.IHTMLTxtRange

If oSelection.type = "Text" Then

oTxtRange = DirectCast(oSelection.createRange(), mshtml.IHTMLTxtRange)
sHTMLText = oTxtRange.htmlText
If InStr(sHTMLText, "<a", CompareMethod.Text) = 0 Then sHTMLText = "<A>" &
sHTMLText & "</A>"
oDocLink = DirectCast(Doc.createElement(sHTMLText),
mshtml.HTMLAnchorElementClass)
oDocLink.innerHTML = oTxtRange.text

...This is where the dialog window pops up and allows the Link to be edited.

oTxtRange.pasteHTML(oDocLink.outerHTML)

End If

Thanks
Jason
 
J

Jeffrey Tan[MSFT]

Hi Jason,

Thanks for your post!

Can you provide a little sample project to demonstrate the problem? It will
be helpful to the understanding. Also, you'd better post the details steps
to reproduce the problem.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Jeffery,

The editor is part of a larger application I am building...If the following
information does not help you understand my problem I will try to create a
standalone version to reproduce the problem.

I have created an Editor using the WebBrowser component
A section within the document contains the following HTML
<P><A href="http://test.com">testing</A></P>
<P> </P>

In the editor I highlight the link (the word testing)

The following code then executes

Dim Doc As mshtml.IHTMLDocument2 =
DirectCast(Web.Document.DomDocument, mshtml.IHTMLDocument2)
Dim oSelection As mshtml.IHTMLSelectionObject = Doc.Selection
Dim oDocLink As mshtml.HTMLAnchorElementClass = Nothing
Dim oControlRange As mshtml.IHTMLControlRange = Nothing
Dim oTxtRange As mshtml.IHTMLTxtRange = Nothing
Dim sHTMLText As String = ""

oTxtRange = DirectCast(oSelection.createRange(), mshtml.IHTMLTxtRange)

'I'm not setting these..I'm showing you the value for each of the
properties. These values are as expected.
'oTextRange.text="testing"
'oTextRange.HTMLtext="<A href="http://test.com">testing</A>"

'As you can see from the text below that All I am doing is pasting back
exactly the same HTMLtext I got from the Selection object..
sHTMLText = oTxtRange.htmlText
oTxtRange.pasteHTML(sHTMLText)
Return

what is returned in the HTML Doc is:
<P><A href="http://test.com">testing</A><A href="http://test.com"></A></P>
<P> </P>

as you can see, the link tag is duplicated but with no text between the <A>
and </A>.

Now if I pass just a piece of text to the PasteHTML function like

oTxtRange.pasteHTML("New Link")
then I get
<P><A href="http://test.com">New Link</A></P>
<P> </P>

In this case, no duplicate...

My only option at this time is to simple execute
oTxtRange = DirectCast(oSelection.createRange(), mshtml.IHTMLTxtRange)
oTxtRange.execCommand("CreateLink", True, Nothing)

instead of using my own code to do the updating. I really don't want to use
this because the default Hyperlink dialog does not allow me to update any
other attributes of the tag like class or style etc...

Is there a bug here in mshtml?? or am I mis-understanding the way pasteHTML
works?


Again if you are unable to understand my problem then I will try and put
together a small app to show you the problem..

Thanks
Jason


"Jeffrey Tan[MSFT]" said:
Hi Jason,

Thanks for your post!

Can you provide a little sample project to demonstrate the problem? It will
be helpful to the understanding. Also, you'd better post the details steps
to reproduce the problem.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Jason,

Thanks for your feedback.

No, I can understand your problem in the first reply. But for efficient
purpose, I recommend you create a simple sample project for demonstration.
With reproduce project, it is better for us to do research. Thanks for your
understanding.

You can attach the project as an attachment in the further reply.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

I think I've found a workaround (or may be that's the way it's supposed to
work)

I am exececuting the "unlink" command right before I paste the HTML. If I
find that it does not work I'll put together a test app for you...

Thanks
Jason
 
J

Jeffrey Tan[MSFT]

Ok, if you have any further problem, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top