Bug 675579 - Part 2: Dispatch an input event when changing the direction of a plain text element; r=roc
authorEhsan Akhgari <ehsan.akhgari@gmail.com>
Fri, 17 Jan 2014 10:31:18 -0500
changeset 163969 8f1ffc0fe9142b397041bd35f010c6355f648815
parent 163968 3475821c244889b1c09922d83ca44a81971ff118
child 163970 fbd443bb2208ede14fb012cd955d41fbe33b1f4a
push id38598
push usereakhgari@mozilla.com
push dateFri, 17 Jan 2014 15:32:47 +0000
treeherdermozilla-inbound@8f1ffc0fe914 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs675579
milestone29.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 675579 - Part 2: Dispatch an input event when changing the direction of a plain text element; r=roc
editor/libeditor/base/nsEditor.cpp
editor/libeditor/base/nsEditor.h
editor/libeditor/text/tests/test_bug629172.html
--- a/editor/libeditor/base/nsEditor.cpp
+++ b/editor/libeditor/base/nsEditor.cpp
@@ -1846,16 +1846,22 @@ void nsEditor::NotifyEditorObservers(voi
   for (int32_t i = 0; i < mEditorObservers.Count(); i++) {
     mEditorObservers[i]->EditAction();
   }
 
   if (!mDispatchInputEvent) {
     return;
   }
 
+  FireInputEvent();
+}
+
+void
+nsEditor::FireInputEvent()
+{
   // We don't need to dispatch multiple input events if there is a pending
   // input event.  However, it may have different event target.  If we resolved
   // this issue, we need to manage the pending events in an array.  But it's
   // overwork.  We don't need to do it for the very rare case.
 
   nsCOMPtr<nsIContent> target = GetInputEventTargetContent();
   NS_ENSURE_TRUE_VOID(target);
 
@@ -5059,16 +5065,20 @@ nsEditor::SwitchTextDirection()
   } else if (mFlags & nsIPlaintextEditor::eEditorLeftToRight) {
     NS_ASSERTION(!(mFlags & nsIPlaintextEditor::eEditorRightToLeft),
                  "Unexpected mutually exclusive flag");
     mFlags |= nsIPlaintextEditor::eEditorRightToLeft;
     mFlags &= ~nsIPlaintextEditor::eEditorLeftToRight;
     rv = rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("rtl"), true);
   }
 
+  if (NS_SUCCEEDED(rv)) {
+    FireInputEvent();
+  }
+
   return rv;
 }
 
 void
 nsEditor::SwitchTextDirectionTo(uint32_t aDirection)
 {
   // Get the current root direction from its frame
   nsIContent* rootElement = GetExposedRoot();
@@ -5078,24 +5088,28 @@ nsEditor::SwitchTextDirectionTo(uint32_t
 
   // Apply the requested direction
   if (aDirection == nsIPlaintextEditor::eEditorLeftToRight &&
       (mFlags & nsIPlaintextEditor::eEditorRightToLeft)) {
     NS_ASSERTION(!(mFlags & nsIPlaintextEditor::eEditorLeftToRight),
                  "Unexpected mutually exclusive flag");
     mFlags &= ~nsIPlaintextEditor::eEditorRightToLeft;
     mFlags |= nsIPlaintextEditor::eEditorLeftToRight;
-    rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("ltr"), true);
+    rv = rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("ltr"), true);
   } else if (aDirection == nsIPlaintextEditor::eEditorRightToLeft &&
              (mFlags & nsIPlaintextEditor::eEditorLeftToRight)) {
     NS_ASSERTION(!(mFlags & nsIPlaintextEditor::eEditorRightToLeft),
                  "Unexpected mutually exclusive flag");
     mFlags |= nsIPlaintextEditor::eEditorRightToLeft;
     mFlags &= ~nsIPlaintextEditor::eEditorLeftToRight;
-    rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("rtl"), true);
+    rv = rootElement->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("rtl"), true);
+  }
+
+  if (NS_SUCCEEDED(rv)) {
+    FireInputEvent();
   }
 }
 
 #if DEBUG_JOE
 void
 nsEditor::DumpNode(nsIDOMNode *aNode, int32_t indent)
 {
   int32_t i;
--- a/editor/libeditor/base/nsEditor.h
+++ b/editor/libeditor/base/nsEditor.h
@@ -246,16 +246,17 @@ public:
   virtual nsresult UpdateIMEComposition(const nsAString &aCompositionString,
                                         nsIPrivateTextRangeList *aTextRange)=0;
   void EndIMEComposition();
 
   void SwitchTextDirectionTo(uint32_t aDirection);
 
 protected:
   nsresult DetermineCurrentDirection();
+  void FireInputEvent();
 
   /** create a transaction for setting aAttribute to aValue on aElement
     */
   NS_IMETHOD CreateTxnForSetAttribute(mozilla::dom::Element *aElement,
                                       const nsAString &  aAttribute,
                                       const nsAString &  aValue,
                                       ChangeAttributeTxn ** aTxn);
 
--- a/editor/libeditor/text/tests/test_bug629172.html
+++ b/editor/libeditor/text/tests/test_bug629172.html
@@ -43,30 +43,36 @@ SimpleTest.waitForFocus(function() {
       return this[dir];
     }
   };
 
   function testDirection(initialDir) {
     var t = document.createElement("textarea");
     t.setAttribute("dir", initialDir);
     t.value = "test.";
+    var inputEventCount = 0;
+    t.oninput = function() { inputEventCount++; };
     document.getElementById("content").appendChild(t);
     document.body.clientWidth;
     var s1 = snapshotWindow(window);
     ok(compareSnapshots(s1, Screenshots.get(initialDir, false), true)[0],
        "Textarea should appear correctly before switching the direction (" + initialDir + ")");
     t.focus();
+    is(inputEventCount, 0, "input event count must be 0 before");
     synthesizeKey("x", {accelKey: true, shiftKey: true});
     is(t.getAttribute("dir"), initialDir == "ltr" ? "rtl" : "ltr", "The dir attribute must be correctly updated");
+    is(inputEventCount, 1, "input event count must be 1 after");
     t.blur();
     var s2 = snapshotWindow(window);
     ok(compareSnapshots(s2, Screenshots.get(initialDir, true), true)[0],
        "Textarea should appear correctly after switching the direction (" + initialDir + ")");
     t.focus();
+    is(inputEventCount, 1, "input event count must be 1 before");
     synthesizeKey("x", {accelKey: true, shiftKey: true});
+    is(inputEventCount, 2, "input event count must be 2 after");
     is(t.getAttribute("dir"), initialDir == "ltr" ? "ltr" : "rtl", "The dir attribute must be correctly updated");
     t.blur();
     var s3 = snapshotWindow(window);
     ok(compareSnapshots(s3, Screenshots.get(initialDir, false), true)[0],
        "Textarea should appear correctly after switching back the direction (" + initialDir + ")");
     t.parentNode.removeChild(t);
   }