Bug 1399722 - Don't prevent changing the selection in the editor when setting the value attribute; r=masayuki
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 18 Sep 2017 23:40:42 -0400
changeset 431428 4193c11e97aeab5a258d5499bf4a4d9177b27380
parent 431427 4d924f659b8cbc214e233bb4ea005ceaa84c42a1
child 431429 b3637dfc01ba79a37c2e3a0c4b65eacd1bb0fa56
push id7784
push userryanvm@gmail.com
push dateThu, 21 Sep 2017 00:40:13 +0000
treeherdermozilla-beta@efff4f307675 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmasayuki
bugs1399722
milestone57.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 1399722 - Don't prevent changing the selection in the editor when setting the value attribute; r=masayuki
editor/libeditor/EditorBase.cpp
editor/libeditor/TextEditRules.cpp
editor/libeditor/tests/mochitest.ini
editor/libeditor/tests/test_bug1399722.html
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -2738,23 +2738,24 @@ EditorBase::SetTextImpl(Selection& aSele
   // We don't support undo here, so we don't really need all of the transaction
   // machinery, therefore we can run our transaction directly, breaking all of
   // the rules!
   nsresult rv = aCharData.SetData(aString);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  // Only set selection to insertion point if editor gives permission
-  if (GetShouldTxnSetSelection()) {
+  {
+    // Create a nested scope to not overwrite rv from the outer scope.
     RefPtr<Selection> selection = GetSelection();
     DebugOnly<nsresult> rv = selection->Collapse(&aCharData, aString.Length());
     NS_ASSERTION(NS_SUCCEEDED(rv),
                  "Selection could not be collapsed after insert");
   }
+
   mRangeUpdater.SelAdjDeleteText(&aCharData, 0, length);
   mRangeUpdater.SelAdjInsertText(aCharData, 0, aString);
 
   // Let listeners know what happened
   {
     AutoActionListenerArray listeners(mActionListeners);
     for (auto& listener : listeners) {
       if (length) {
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -868,19 +868,16 @@ TextEditRules::WillSetText(Selection& aS
     return NS_OK;
   }
 
   nsINode* curNode = rootElement->GetFirstChild();
   if (NS_WARN_IF(!EditorBase::IsTextNode(curNode))) {
     return NS_OK;
   }
 
-  // don't change my selection in subtransactions
-  AutoTransactionsConserveSelection dontChangeMySelection(textEditor);
-
   // Even if empty text, we don't remove text node and set empty text
   // for performance
   nsresult rv = textEditor->SetTextImpl(aSelection, tString,
                                         *curNode->GetAsText());
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
--- a/editor/libeditor/tests/mochitest.ini
+++ b/editor/libeditor/tests/mochitest.ini
@@ -248,16 +248,17 @@ skip-if = toolkit == 'android' # bug 131
 [test_bug1332876.html]
 [test_bug1352799.html]
 [test_bug1355792.html]
 [test_bug1358025.html]
 [test_bug1361008.html]
 [test_bug1368544.html]
 [test_bug1385905.html]
 [test_bug1394758.html]
+[test_bug1399722.html]
 
 [test_CF_HTML_clipboard.html]
 subsuite = clipboard
 [test_composition_event_created_in_chrome.html]
 [test_contenteditable_focus.html]
 [test_documentCharacterSet.html]
 [test_dom_input_event_on_htmleditor.html]
 skip-if = toolkit == 'android' # bug 1054087
new file mode 100644
--- /dev/null
+++ b/editor/libeditor/tests/test_bug1399722.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1399722
+-->
+<html>
+<head>
+  <title>Test for Bug 1399722</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/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=1399722">Mozilla Bug 1399722</a>
+<p id="display"></p>
+<div id="content" style="display: none;">
+
+</div>
+
+<input spellcheck="true" onkeypress="if (event.key=='Enter')this.value='';">
+<pre id="test">
+
+<script class="testbody" type="application/javascript">
+SimpleTest.waitForExplicitFinish();
+SimpleTest.waitForFocus(function() {
+  let elm = document.querySelector("input");
+
+  elm.focus();
+  synthesizeKey("A", {});
+  synthesizeKey("B", {});
+  synthesizeKey("KEY_Enter", {});
+  synthesizeKey("C", {});
+  synthesizeKey("D", {});
+  is(elm.value, "CD", "Can type into the textbox successfully after the onkeypress handler deleting the value");
+
+  SimpleTest.finish();
+});
+</script>
+</pre>
+</body>
+</html>