views:

390

answers:

2

Hello all,

I have a problem as follows: We're using a rich text editor (TinyMCE, but that's not important here, I think) in our application. Now, with Internet Explorer 8, we've noticed that if you type in content that looks like a web address:

www.google.com

...IE helpfully converts it to a link by some native-to-browser functionality. Now if you really want to make it into a link, and choose to edit link properties, and set the href e.g. to

www.google.com/analytics

...then when the javascript sets the href attribute of the anchor tag, also the text of the link changes. The desired result is:

`<a href="http://www.google.com/analytics"&gt;www.google.com&lt;/a&gt;`

but actually is:

`<a href="http://www.google.com/analytics"&gt;www.google.com/analytics&lt;/a&gt;`

Does anyone know a way to work around this?

Update: This behavior has only been observed in Internet Explorer 8 and 7. Firefox, Chrome, and Safari are not affected. The problem can also be observed on the TinyMCE websitehttp://tinymce.moxiecode.com/examples/full.php, so it's probably not a TinyMCE configuration issue.

A: 

Need more information:

  1. Does it behave the same way in other browsers?
  2. Are you typing this into the rich text area, or the html view?
  3. Have you configured TinyMCE properly? Sounds like a bug with your particular implementation -- it is not parsing out the HTML tags properly.
Josh
Hi, the comments dialog doesn't support markdown, I notice.(1) This problem only occurs in Internet Explorer, and I have only tried version 8. (2) I'm typing the text into the rich text area, but it works the same if I first create the link in HTML source and then edit it. (3) I think the problem is not with TinyMCE configuration. You can reproduce the problem on their example website: tinymce.moxiecode.com/examples/full.php
Manne
I see the exact same behavior under IE 7. Are you sure this is not a function of tinyMCE?
Ed Schembor
I see! I tried it in Firefox, Chrome, and Safari and they all do not show this behavior. It almost seems like a 'feature' of TinyMCE actually, that only works in IE right now. I don't see Internet Explorer doing this in other text areas, so I am almost certain that it is a 'feature' of TinyMCE, in which case there is probably a way to turn it off in the configuration. Have a look at their documentation and see. I personally hadn't see this before, but I actually find it useful :)
Josh
I dug through the javascript of TinyMCE and think it's not its feature. The change of the text happens during `elm.setAttribute("href", value);` so thus I thought it was a misbehavior of IE.However, I just tried the same function with FCKEditor, and the problem does not appear there, so it definitely has something to do with TinyMCE.
Manne
+1  A: 

After some research and debugging, I found that the problem is caused by built-in behavior of Internet Explorer. It occurs when setting the href-property of a link, whose text-content appears to be an URL (according to IE). In these cases, IE copies the contents of the href-attribute into the link text.

There might be several workarounds for this, but I found that at least this logic works:

  1. store the innerHTML into a temporary variable,
  2. set the href attribute as usual
  3. if innerHTML has changed, copy back the original innerHTML from the temporary variable.

This seems to work because changing the innerHTML of the link does not cause changing of the href attribute.

In tinyMCE, find the following line in setAllAttribs() of functions.js of the advlink plugin:

setAttrib(elm, 'href', href);

...and replace it with this monster:

if(tinyMCE.isMSIE) {
    var tmp = elm.innerHTML;
    setAttrib(elm, 'href', href);
    if(elm.innerHTML != tmp) // optional, but keeps unnecessary innerHTML set:s away
        elm.innerHTML = tmp;
}
else {
    setAttrib(elm, 'href', href);
}

...and your links will appear as if untouched. I also started a thread on the tinyMCE forums about this. If they post some improvements to my solution or tell it's nonsense, I'll update this question also.

Manne