Bug 981652 - Dispatch "select" events from HTMLInputElement/HTMLTextAreaElement.setSelectionRange() and selection setters; r=smaug
authorEhsan Akhgari <ehsan.akhgari@gmail.com>
Wed, 12 Mar 2014 21:44:33 -0400
changeset 191521 c866f4f5ec95b2181749cf3557760dcbd5bd418c
parent 191520 86fad085d4b16e8840c8b7344703f6542b12b9a7
child 191522 643296ae30594e18ae09042844cd9186357e92c0
push id474
push userasasaki@mozilla.com
push dateMon, 02 Jun 2014 21:01:02 +0000
treeherdermozilla-release@967f4cf1b31c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs981652
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 981652 - Dispatch "select" events from HTMLInputElement/HTMLTextAreaElement.setSelectionRange() and selection setters; r=smaug
content/html/content/src/HTMLInputElement.cpp
content/html/content/src/HTMLTextAreaElement.cpp
content/html/content/test/forms/test_set_range_text.html
--- a/content/html/content/src/HTMLInputElement.cpp
+++ b/content/html/content/src/HTMLInputElement.cpp
@@ -5074,16 +5074,19 @@ HTMLInputElement::SetSelectionRange(int3
       nsITextControlFrame::SelectionDirection dir = nsITextControlFrame::eForward;
       if (aDirection.WasPassed() && aDirection.Value().EqualsLiteral("backward")) {
         dir = nsITextControlFrame::eBackward;
       }
 
       aRv = textControlFrame->SetSelectionRange(aSelectionStart, aSelectionEnd, dir);
       if (!aRv.Failed()) {
         aRv = textControlFrame->ScrollSelectionIntoView();
+        nsRefPtr<nsAsyncDOMEvent> event =
+          new nsAsyncDOMEvent(this, NS_LITERAL_STRING("select"), true, false);
+        event->PostDOMEvent();
       }
     }
   }
 }
 
 NS_IMETHODIMP
 HTMLInputElement::SetSelectionRange(int32_t aSelectionStart,
                                     int32_t aSelectionEnd,
@@ -5199,21 +5202,16 @@ HTMLInputElement::SetRangeText(const nsA
         aSelectionEnd = newEnd;
       }
     }
     break;
   }
 
   Optional<nsAString> direction;
   SetSelectionRange(aSelectionStart, aSelectionEnd, direction, aRv);
-  if (!aRv.Failed()) {
-    nsRefPtr<nsAsyncDOMEvent> event =
-      new nsAsyncDOMEvent(this, NS_LITERAL_STRING("select"), true, false);
-    event->PostDOMEvent();
-  }
 }
 
 int32_t
 HTMLInputElement::GetSelectionStart(ErrorResult& aRv)
 {
   int32_t selEnd, selStart;
   aRv = GetSelectionRange(&selStart, &selEnd);
 
--- a/content/html/content/src/HTMLTextAreaElement.cpp
+++ b/content/html/content/src/HTMLTextAreaElement.cpp
@@ -882,16 +882,19 @@ HTMLTextAreaElement::SetSelectionRange(u
       nsITextControlFrame::SelectionDirection dir = nsITextControlFrame::eForward;
       if (aDirection.WasPassed() && aDirection.Value().EqualsLiteral("backward")) {
         dir = nsITextControlFrame::eBackward;
       }
 
       rv = textControlFrame->SetSelectionRange(aSelectionStart, aSelectionEnd, dir);
       if (NS_SUCCEEDED(rv)) {
         rv = textControlFrame->ScrollSelectionIntoView();
+        nsRefPtr<nsAsyncDOMEvent> event =
+          new nsAsyncDOMEvent(this, NS_LITERAL_STRING("select"), true, false);
+        event->PostDOMEvent();
       }
     }
   }
 
   if (NS_FAILED(rv)) {
     aError.Throw(rv);
   }
 }
@@ -988,21 +991,16 @@ HTMLTextAreaElement::SetRangeText(const 
         aSelectionEnd = newEnd;
       }
     }
     break;
   }
 
   Optional<nsAString> direction;
   SetSelectionRange(aSelectionStart, aSelectionEnd, direction, aRv);
-  if (!aRv.Failed()) {
-    nsRefPtr<nsAsyncDOMEvent> event =
-      new nsAsyncDOMEvent(this, NS_LITERAL_STRING("select"), true, false);
-    event->PostDOMEvent();
-  }
 }
 
 nsresult
 HTMLTextAreaElement::Reset()
 {
   nsresult rv;
 
   // To get the initial spellchecking, reset value to
--- a/content/html/content/test/forms/test_set_range_text.html
+++ b/content/html/content/test/forms/test_set_range_text.html
@@ -67,18 +67,19 @@ https://bugzilla.mozilla.org/show_bug.cg
     //Supported types should not throw
     for (i = 0; i < SupportedTypes.length; ++i) {
       opThrows = false;
       msg = "input_" + SupportedTypes[i];
       elem = document.getElementById(msg);
       elem.focus();
       try {
         elem.setRangeText("abc");
+        expectedNumOfSelectCalls += 1;
       } catch (ex) {
-          opThrows = true;
+        opThrows = true;
       }
       is(opThrows, false, msg + " should not throw NotSupportedError");
 
       elem.addEventListener("select", function (aEvent) {
         ok(true, "select event should be fired for " + aEvent.target.id);
         if (++numOfSelectCalls == expectedNumOfSelectCalls) {
           SimpleTest.finish();
         } else if (numOfSelectCalls > expectedNumOfSelectCalls) {
@@ -94,70 +95,69 @@ https://bugzilla.mozilla.org/show_bug.cg
       elem.value = "0123456789ABCDEF";
       elem.setSelectionRange(1, 6);
       elem.setRangeText("xyz");
       is(elem.value, "0xyz6789ABCDEF", msg + test);
       is(elem.selectionStart, 1, msg + test);
       is(elem.selectionEnd, 4, msg + test);
       elem.setRangeText("mnk");
       is(elem.value, "0mnk6789ABCDEF", msg + test);
-      expectedNumOfSelectCalls += 2;
+      expectedNumOfSelectCalls += 3;
 
       test = " setRange(replacement), expand";
       elem.value = "0123456789ABCDEF";
       elem.setSelectionRange(1, 2);
       elem.setRangeText("xyz");
       is(elem.value, "0xyz23456789ABCDEF", msg + test);
       is(elem.selectionStart, 1, msg + test);
       is(elem.selectionEnd, 4, msg + test);
       elem.setRangeText("mnk");
       is(elem.value, "0mnk23456789ABCDEF", msg + test);
-      expectedNumOfSelectCalls += 2;
+      expectedNumOfSelectCalls += 3;
 
       test = " setRange(replacement) pure insertion at start";
       elem.value = "0123456789ABCDEF";
       elem.setSelectionRange(0, 0);
       elem.setRangeText("xyz");
       is(elem.value, "xyz0123456789ABCDEF", msg + test);
       is(elem.selectionStart, 0, msg + test);
       is(elem.selectionEnd, 0, msg + test);
       elem.setRangeText("mnk");
       is(elem.value, "mnkxyz0123456789ABCDEF", msg + test);
-      expectedNumOfSelectCalls += 2;
+      expectedNumOfSelectCalls += 3;
 
       test = " setRange(replacement) pure insertion in the middle";
       elem.value = "0123456789ABCDEF";
       elem.setSelectionRange(4, 4);
       elem.setRangeText("xyz");
       is(elem.value, "0123xyz456789ABCDEF", msg + test);
       is(elem.selectionStart, 4, msg + test);
       is(elem.selectionEnd, 4, msg + test);
       elem.setRangeText("mnk");
       is(elem.value, "0123mnkxyz456789ABCDEF", msg + test);
-      expectedNumOfSelectCalls += 2;
+      expectedNumOfSelectCalls += 3;
 
       test = " setRange(replacement) pure insertion at the end";
       elem.value = "0123456789ABCDEF";
       elem.setSelectionRange(16, 16);
       elem.setRangeText("xyz");
       is(elem.value, "0123456789ABCDEFxyz", msg + test);
       is(elem.selectionStart, 16, msg + test);
       is(elem.selectionEnd, 16, msg + test);
       elem.setRangeText("mnk");
       is(elem.value, "0123456789ABCDEFmnkxyz", msg + test);
-      expectedNumOfSelectCalls += 2;
+      expectedNumOfSelectCalls += 3;
 
       //test SetRange(replacement, start, end, mode) with start > end
       try {
         elem.setRangeText("abc", 20, 4);
       } catch (ex) {
         opThrows = (ex.name == "IndexSizeError" && ex.code == DOMException.INDEX_SIZE_ERR);
       }
       is(opThrows, true, msg + " should throw IndexSizeError");
-      expectedNumOfSelectCalls += 1;
 
       //test SelectionMode 'select'
       elem.value = "0123456789ABCDEF";
       elem.setRangeText("xyz", 4, 9, "select");
       is(elem.value, "0123xyz9ABCDEF", msg + ".value == \"0123xyz9ABCDEF\"");
       is(elem.selectionStart, 4, msg + ".selectionStart == 4, with \"select\"");
       is(elem.selectionEnd, 7, msg + ".selectionEnd == 7, with \"select\"");
       expectedNumOfSelectCalls += 1;
@@ -200,44 +200,44 @@ https://bugzilla.mozilla.org/show_bug.cg
 
       //subcase: selection{Start|End} > end
       elem.value = "0123456789";
       elem.setSelectionRange(6, 9);
       elem.setRangeText("Z", 1, 2, "preserve");
       is(elem.value, "0Z23456789", msg + ".value == \"0Z23456789\"");
       is(elem.selectionStart, 6, msg + ".selectionStart == 6, with \"preserve\"");
       is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"preserve\"");
-      expectedNumOfSelectCalls += 1;
+      expectedNumOfSelectCalls += 2;
 
       //subcase: selection{Start|End} < end
       elem.value = "0123456789";
       elem.setSelectionRange(4, 5);
       elem.setRangeText("QRST", 2, 9, "preserve");
       is(elem.value, "01QRST9", msg + ".value == \"01QRST9\"");
       is(elem.selectionStart, 2, msg + ".selectionStart == 2, with \"preserve\"");
       is(elem.selectionEnd, 6, msg + ".selectionEnd == 6, with \"preserve\"");
-      expectedNumOfSelectCalls += 1;
+      expectedNumOfSelectCalls += 2;
 
       //subcase: selectionStart > end, selectionEnd < end
       elem.value = "0123456789";
       elem.setSelectionRange(8, 4);
       elem.setRangeText("QRST", 1, 5);
       is(elem.value, "0QRST56789", msg + ".value == \"0QRST56789\"");
       is(elem.selectionStart, 1, msg + ".selectionStart == 1, with \"default\"");
       is(elem.selectionEnd, 5, msg + ".selectionEnd == 5, with \"default\"");
-      expectedNumOfSelectCalls += 1;
+      expectedNumOfSelectCalls += 2;
 
       //subcase: selectionStart < end, selectionEnd > end
       elem.value = "0123456789";
       elem.setSelectionRange(4, 9);
       elem.setRangeText("QRST", 2, 6);
       is(elem.value, "01QRST6789", msg + ".value == \"01QRST6789\"");
       is(elem.selectionStart, 2, msg + ".selectionStart == 2, with \"default\"");
       is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"default\"");
-      expectedNumOfSelectCalls += 1;
+      expectedNumOfSelectCalls += 2;
     }
   }
 
   addLoadEvent(TestInputs);
 
 </script>
 </pre>
 </body>