<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>7275</bug_id>
          
          <creation_ts>2006-02-15 08:40:34 -0800</creation_ts>
          <short_desc>window.getSelection() doesn&apos;t work in textareas</short_desc>
          <delta_ts>2011-06-01 04:46:48 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>DOM</component>
          <version>420+</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>OS X 10.4</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Major</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Steven Tamm">stamm</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>dgatwood</cc>
    
    <cc>gavin.sharp</cc>
    
    <cc>ian</cc>
    
    <cc>justin.garcia</cc>
    
    <cc>rniwa</cc>
    
    <cc>xan.lopez</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>32710</commentid>
    <comment_count>0</comment_count>
    <who name="Steven Tamm">stamm</who>
    <bug_when>2006-02-15 08:40:34 -0800</bug_when>
    <thetext>window.getSelection() returns null when the selection is in a text area.  This behavior makes it very difficult when there are two text areas on the page.  Since the selection for a text area remains when you leave it, you have no idea which one of the text areas is the current (or last one) selected.

In the latest nightly build, when I try the page below, if you select the &quot;Random Text&quot; and then click dump range, you will get a real range object.  If you select the &quot;My text&quot; inside the text area, you get null for a range object.  You can fake it using getSelectionRange, but that doesn&apos;t work.  

1.  Select &quot;text&quot; inside the text area
2.  Select &quot;random&quot; 
3.  Click &quot;Copy TextArea Sel&quot;.  This contains the last selection.  Even though random was selected.

If I would add another text area, I would have no idea what the user has selected

&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-type&quot; content=&quot;text/html&quot; /&gt;
&lt;title&gt;Simple Selection Test&lt;/title&gt;
&lt;script&gt;
function dumpO(o) {
  var msg = &quot;&quot;;
  var i = 0;
  for (var f in o) {
    try {
      msg += f + &quot; = &quot; + o[f] + &quot;\n&quot;;
    } catch (e) {}
    if (i++ == 10) {
       i=0;
       alert(msg);
       msg = &quot;&quot;;
    }
  }
  if (msg) alert(msg);
}
function getSelectionRange(o) {
  var start = o.selectionStart;
  var end = o.selectionEnd;
  return o.value.substring(start,end);
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;button onclick=&quot;document.getElementById(&apos;r&apos;).innerHTML =
	  window.getSelection();&quot;&gt;&lt;u&gt;Copy Window Sel&lt;/u&gt;&lt;/button&gt;&lt;br&gt;
&lt;button onclick=&quot;document.getElementById(&apos;r&apos;).innerHTML =
		 getSelectionRange(document.getElementById(&apos;text&apos;));&quot;&gt;&lt;u&gt;Copy
		 TextArea Sel&lt;/u&gt;&lt;/button&gt;&lt;br&gt;
&lt;button onclick=&quot;dumpO(window.getSelection());&quot;&gt;&lt;u&gt;Dump Selection&lt;/u&gt;&lt;/button&gt;&lt;br&gt;
&lt;button onclick=&quot;dumpO(window.getSelection().getRangeAt(0));&quot;&gt;&lt;u&gt;Dump Range&lt;/u&gt;&lt;/button&gt;&lt;br&gt;
&lt;button onclick=&quot;document.getElementById(&apos;r&apos;).innerHTML = window.getSelection().getRangeAt(0);&quot;&gt;&lt;u&gt;Copy Range&lt;/u&gt;&lt;/button&gt;&lt;br&gt;
&lt;button onclick=&quot;dumpO(document.getElementById(&apos;text&apos;));&quot;&gt;&lt;u&gt;Dump TextArea&lt;/u&gt;&lt;/button&gt;&lt;br&gt;
&lt;button onclick=&quot;dumpO(window);&quot;&gt;&lt;u&gt;Dump Window&lt;/u&gt;&lt;/button&gt;&lt;br&gt;
&lt;textarea id=&quot;text&quot; name=&quot;text&quot; rows=&quot;20&quot; cols=&quot;80&quot;/&gt;
My text&lt;/textarea&gt;
&lt;div id=&quot;r&quot;&gt;divMe&lt;/div&gt;
Random Text
&lt;/body&gt;
&lt;/html&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>32736</commentid>
    <comment_count>1</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2006-02-15 12:17:40 -0800</bug_when>
    <thetext>Confirming as a difference between Firefox and WebKit, but I am not sure what the correct approach would be.

In Firefox, window.getSelection() returns a collapsed range positioned before the TEXTAREA (HTMLBodyElement, offset 22 in your example). This doesn&apos;t look right to me - that is an entirely different position, and the insertion point can easily be moved there in contenteditable mode.

TEXTAREA has a child text node - perhaps it would be more appropriate to make window.getSelection() return the actual range within this text node?

BTW, you can probably use onfocus/onblur handlers to track the last focused textarea.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>47855</commentid>
    <comment_count>2</comment_count>
    <who name="Steven Tamm">stamm</who>
    <bug_when>2006-06-30 12:11:16 -0700</bug_when>
    <thetext>&gt; BTW, you can probably use onfocus/onblur handlers to track the last focused
textarea.

This didn&apos;t work with 418 because onblur didn&apos;t get called appropriately when you click on a button</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>9096</commentid>
    <comment_count>3</comment_count>
    <who name="David Gatwood">dgatwood</who>
    <bug_when>2007-05-25 16:21:23 -0700</bug_when>
    <thetext>
This fails similarly for anything set to contentEditable, too.  I&apos;ve been smashing my head against a wall over this bug off and on for months.

</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>65626</commentid>
    <comment_count>4</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2007-12-29 13:26:35 -0800</bug_when>
    <thetext>(In reply to comment #3)
&gt; This fails similarly for anything set to contentEditable, too. 

Could you please provide a test case (preferably, in a new bug, to keep this one focused on textareas)? This seems to work correctly to me.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>77735</commentid>
    <comment_count>5</comment_count>
      <attachid>20565</attachid>
    <who name="Eric Seidel (no email)">eric</who>
    <bug_when>2008-04-15 15:16:02 -0700</bug_when>
    <thetext>Created attachment 20565
test case (same as is pasted as text in comments below)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>413297</commentid>
    <comment_count>6</comment_count>
    <who name="Xan Lopez">xan.lopez</who>
    <bug_when>2011-06-01 04:46:48 -0700</bug_when>
    <thetext>FWIW, I recently stumbled upon this bug and I think nowadays we do what comment #1 (from 2006!) says Firefox used to do back then. The thing that makes this fail is DOMSelection.cpp:getRangeAt, which has a chunk like:


    if (Node* shadowAncestor = selectionShadowAncestor(m_frame)) {
        ContainerNode* container = shadowAncestor-&gt;parentNodeGuaranteedHostFree();
        int offset = shadowAncestor-&gt;nodeIndex();
        return Range::create(shadowAncestor-&gt;document(), container, offset, container, offset);
    }

triggered inside text areas (and that makes us return a 0-0 bogus range). If I just *remove* that code, my simple tests seem to work OK, but I&apos;m sure that breaks something else somewhere.

Opinions?</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>20565</attachid>
            <date>2008-04-15 15:16:02 -0700</date>
            <delta_ts>2008-04-15 15:16:02 -0700</delta_ts>
            <desc>test case (same as is pasted as text in comments below)</desc>
            <filename>test.html</filename>
            <type>text/html</type>
            <size>1410</size>
            <attacher name="Eric Seidel (no email)">eric</attacher>
            
              <data encoding="base64">PGh0bWw+CjxoZWFkPgo8bWV0YSBodHRwLWVxdWl2PSJDb250ZW50LXR5cGUiIGNvbnRlbnQ9InRl
eHQvaHRtbCIgLz4KPHRpdGxlPlNpbXBsZSBTZWxlY3Rpb24gVGVzdDwvdGl0bGU+CjxzY3JpcHQ+
CmZ1bmN0aW9uIGR1bX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