getSelectionRange progressively slow in IE

Hi,

The performance of getSelectionRange (wildly) varies between Firefox and IE. Firefox returns almost instantaneously when it calls getSelectionRange regardless of where the caret is with respect to the beginning of the text. In IE, the farther the caret is positioned away from the beginning of the text, the slower the return from getSelectionRange call is. CPU usage (a T9500 processor) goes 50%.

Sample code below.

Trygetsel.class

package test.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class Trygetsel implements EntryPoint
{
	private DynamicForm form = new DynamicForm();
	private TextAreaItem textAreaItem = new TextAreaItem("text", "Large amount of text (>1K)");
	private int [] selectionRange = null;

	public void onModuleLoad()
	{
		textAreaItem.setWidth(400);
		textAreaItem.setHeight(300);
		textAreaItem.addClickHandler(new ClickHandler()
		{			
			@Override
			public void onClick(ClickEvent aEvent)
			{
				selectionRange = textAreaItem.getSelectionRange();

				if (selectionRange == null)
				{
					return;
				}
				
				System.out.println(selectionRange [0]);
				System.out.println(selectionRange [1]);
				
				String text = (String) textAreaItem.getValue();

				if (text == null || text.length() == 0)
				{
					return;
				}
				
				String selectedText = text.substring(selectionRange [0], selectionRange [1]);
				
				System.out.println(selectedText);
			}
		});

		form.setWidth(400);
		form.setHeight(300);
		form.setTitleOrientation(TitleOrientation.TOP);
		
		form.setItems(textAreaItem);
		
	    RootPanel.get().add(form);
	}
}

Trygetsel.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='trygetsel'>
  <inherits name='com.google.gwt.user.User'/>
  <inherits name='com.smartgwt.SmartGwt'/>
  <inherits name="com.smartclient.theme.enterpriseblue.EnterpriseBlueResources"/>  
  <entry-point class='test.client.Trygetsel'/>
  <source path='client'/>
</module>

Trygetsel.html

<!doctype html>

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=7">

    <link type="text/css" rel="stylesheet" href="Trygetsel.css">

    <title>Try getSelectionRange</title>
    
    <script> var isomorphicDir = "trygetsel/sc/"; </script>
    <script type="text/javascript" language="javascript" src="trygetsel/trygetsel.nocache.js"></script>
  </head>

  <body>

    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
    
    <noscript>
      <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
        Your web browser must have JavaScript enabled
        in order for this application to display correctly.
      </div>
    </noscript>

  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    
  <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>Trygetsel.html</welcome-file>
  </welcome-file-list>

</web-app>

To test, make sure to paste a large amount of text (>1K, the more, the merrier) and change caret position from top to middle, top to bottom, bottom to middle, etc.

Is this an IE bug? Is there a work-around?

Software used:
-SmartGWT 2.4
-IE 8.0.7600.16385
-FF 3.6.13

thanks and regards,
rtsan

So far as we know, the only way of detecting the selection range reliably in IE involves iteration over character ranges. If you find a more efficient approach, we’d be happy to integrate it.