Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do when IE's moveToElementText spits out an Invalid Argument exception

We've written a plugin to the Xinha text editor to handle footnotes. You can take a look at: http://www.nicholasbs.com/xinha/examples/Newbie.html

In order to handle some problems with the way Webkit and IE handle links at the end of lines (there's no way to use the cursor to get out of the link on the same line) we insert a blank element and move the selection to that, than collapse right. This works fine in Webkit and Gecko, but for some reason moveToElementText is spitting out an Invalid Argument exception. It doesn't matter which element we pass to it, the function seems to be completely broken. In other code paths, however, this function seems to work.

To reproduce the error using the link above, click in the main text input area, type anything, then click on the yellow page icon with the green plus sign, type anything into the lightbox dialog, and click on Insert. An example of the code that causes the problem is below:

  if (Xinha.is_ie)
  {
    var mysel = editor.getSelection();
    var myrange = doc.body.createTextRange();
    myrange.moveToElementText(newel);
  } else
  {
    editor.selectNodeContents(newel, false);
  }

The code in question lives in svn at: https://svn.openplans.org/svn/xinha_dev/InsertNote

This plugin is built against a branch of Xinha available from svn at: http://svn.xinha.webfactional.com/branches/new-dialogs

like image 791
Douglas Mayle Avatar asked Sep 23 '08 17:09

Douglas Mayle


1 Answers

It's not visible in the snippet above, but newel has been appended to the dom using another element that was itself appended to the DOM. When inserting a dom element, you have to re-retrieve your handle if you wish to reference its siblings, since the handle is invalid (I'm not sure, but I think it refers to a DOM element inside of a document fragment and not the one inside of the document.) After re-retrieving the handle from the insert operation, moveToElementText stopped throwing an exception.

like image 176
Douglas Mayle Avatar answered Oct 31 '22 19:10

Douglas Mayle