Bug 620936 - setSelectionRange should not fail with offsets larger than the value of the editable field; r=roc a=blocking-betaN+
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 05 Jan 2011 20:55:31 -0500
changeset 60292 81cd70b6db84f7fff19f3f01e5d83989e7b27f62
parent 60291 caac4406a720c3efcccd062ad110d631f6192e92
child 60293 016e12c47d299557bab3f5eca8512e23528ecb74
push idunknown
push userunknown
push dateunknown
reviewersroc, blocking-betaN
bugs620936
milestone2.0b10pre
Bug 620936 - setSelectionRange should not fail with offsets larger than the value of the editable field; r=roc a=blocking-betaN+
layout/forms/nsTextControlFrame.cpp
layout/forms/test/Makefile.in
layout/forms/test/test_bug620936.html
--- a/layout/forms/nsTextControlFrame.cpp
+++ b/layout/forms/nsTextControlFrame.cpp
@@ -1097,17 +1097,17 @@ nsTextControlFrame::OffsetToDOMPoint(PRI
     if (length == 2 && PRUint32(aOffset) == textLength) {
       // If we're at the end of the text node and we have a trailing BR node,
       // set the selection on the BR node.
       NS_IF_ADDREF(*aResult = rootNode);
       *aPosition = 1;
     } else {
       // Otherwise, set the selection on the textnode itself.
       NS_IF_ADDREF(*aResult = firstNode);
-      *aPosition = aOffset;
+      *aPosition = PR_MIN(aOffset, textLength);
     }
   } else {
     NS_IF_ADDREF(*aResult = rootNode);
     *aPosition = 0;
   }
 
   return NS_OK;
 }
--- a/layout/forms/test/Makefile.in
+++ b/layout/forms/test/Makefile.in
@@ -70,12 +70,13 @@ include $(topsrcdir)/config/rules.mk
 		test_bug563642.html \
 		test_bug564115.html \
 		bug564115_window.html \
 		test_bug377624.html \
 		test_bug571352.html \
 		test_bug572406.html \
 		test_bug572649.html \
 		test_bug36619.html \
+		test_bug620936.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $^ $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/layout/forms/test/test_bug620936.html
@@ -0,0 +1,36 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=620936
+-->
+<head>
+  <title>Test for Bug 620936</title>
+  <script type="application/javascript" src="/MochiKit/packed.js"></script>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=620936">Mozilla Bug 620936</a>
+<p id="display"></p>
+<div id="content">
+  <input value="foo">
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test for Bug 620936 **/
+SimpleTest.waitForExplicitFinish();
+addLoadEvent(function() {
+  var i = document.querySelector("input");
+  i.focus();
+  i.setSelectionRange(100, 100);
+  is(i.selectionStart, 3, "The selection should be set to the end of the text");
+  is(i.selectionEnd, 3, "The selection should be set to the end of the text");
+  SimpleTest.finish();
+});
+
+</script>
+</pre>
+</body>
+</html>