Bug 1451199. Fix the handling of .onchange in MediaQueryList to match the handling of addEventListener('change'). r=emilio
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 05 Apr 2018 08:34:46 -0400
changeset 777893 8052c4e8c9c6e49a6d39d174e8391631f7cc02a1
parent 777892 26420aad431e5089cf4f9ec61115d10b30f11904
child 777894 4dfc990e1b9cd13dea1d3fdc03ccb0348836a9f2
child 777984 110f32790d38a258cab722064aae40736478ef51
push id105320
push userbmo:dharvey@mozilla.com
push dateThu, 05 Apr 2018 13:17:43 +0000
reviewersemilio
bugs1451199
milestone61.0a1
Bug 1451199. Fix the handling of .onchange in MediaQueryList to match the handling of addEventListener('change'). r=emilio MozReview-Commit-ID: 4YvJ0m0BOnQ
layout/style/MediaQueryList.cpp
layout/style/MediaQueryList.h
layout/style/test/mochitest.ini
layout/style/test/test_bug1451199-1.html
layout/style/test/test_bug1451199-2.html
--- a/layout/style/MediaQueryList.cpp
+++ b/layout/style/MediaQueryList.cpp
@@ -86,30 +86,25 @@ MediaQueryList::AddListener(EventListene
 
   AddEventListenerOptionsOrBoolean options;
   options.SetAsBoolean() = false;
 
   AddEventListener(ONCHANGE_STRING, aListener, options, false, aRv);
 }
 
 void
-MediaQueryList::AddEventListener(const nsAString& aType,
-                                 EventListener* aCallback,
-                                 const AddEventListenerOptionsOrBoolean& aOptions,
-                                 const dom::Nullable<bool>& aWantsUntrusted,
-                                 ErrorResult& aRv)
+MediaQueryList::EventListenerAdded(nsAtom* aType)
 {
-  if (!mMatchesValid) {
-    MOZ_ASSERT(!HasListeners(),
-               "when listeners present, must keep mMatches current");
+  // HasListeners() might still be false if the added thing wasn't a
+  // listener we care about.
+  if (!mMatchesValid && HasListeners()) {
     RecomputeMatches();
   }
 
-  DOMEventTargetHelper::AddEventListener(aType, aCallback, aOptions,
-                                         aWantsUntrusted, aRv);
+  DOMEventTargetHelper::EventListenerAdded(aType);
 }
 
 void
 MediaQueryList::RemoveListener(EventListener* aListener, ErrorResult& aRv)
 {
   if (!aListener) {
     return;
   }
--- a/layout/style/MediaQueryList.h
+++ b/layout/style/MediaQueryList.h
@@ -49,23 +49,18 @@ public:
   JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   // WebIDL methods
   void GetMedia(nsAString& aMedia);
   bool Matches();
   void AddListener(EventListener* aListener, ErrorResult& aRv);
   void RemoveListener(EventListener* aListener, ErrorResult& aRv);
 
-  using nsIDOMEventTarget::AddEventListener;
-
-  virtual void AddEventListener(const nsAString& aType,
-                                EventListener* aCallback,
-                                const AddEventListenerOptionsOrBoolean& aOptions,
-                                const Nullable<bool>& aWantsUntrusted,
-                                ErrorResult& aRv) override;
+  using DOMEventTargetHelper::EventListenerAdded;
+  void EventListenerAdded(nsAtom* aType) override;
 
   IMPL_EVENT_HANDLER(change)
 
   bool HasListeners();
 
   void Disconnect();
 
 private:
--- a/layout/style/test/mochitest.ini
+++ b/layout/style/test/mochitest.ini
@@ -169,16 +169,18 @@ support-files = file_bug1375944.html Ahe
 support-files = bug1382568-iframe.html
 [test_bug1394302.html]
 [test_bug1443344-1.html]
 scheme = https
 support-files = file_bug1443344.css
 [test_bug1443344-2.html]
 scheme = https
 support-files = file_bug1443344.css
+[test_bug1451199-1.html]
+[test_bug1451199-2.html]
 [test_cascade.html]
 [test_ch_ex_no_infloops.html]
 [test_change_hint_optimizations.html]
 [test_clip-path_polygon.html]
 [test_color_rounding.html]
 [test_compute_data_with_start_struct.html]
 skip-if = toolkit == 'android'
 [test_computed_style.html]
new file mode 100644
--- /dev/null
+++ b/layout/style/test/test_bug1451199-1.html
@@ -0,0 +1,42 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1451199
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1451199</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript">
+
+  /** Test for Bug 1451199 **/
+  SimpleTest.waitForExplicitFinish();
+
+  addLoadEvent(function() {
+    // We have to be careful to not check l.matches until the very end
+    // of the test.
+    var l = frames[0].matchMedia("(orientation: portrait)");
+    var iframe = document.querySelector("iframe");
+    iframe.width = "50";
+    iframe.contentDocument.documentElement.offsetWidth; // Flush layout
+
+    l.onchange = function() {
+      is(l.matches, false,
+         "Should not match portrait by the time we get notified");
+      SimpleTest.finish();
+    };
+    iframe.width = "200";
+  });
+  </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1451199">Mozilla Bug 1451199</a>
+<p id="display"><iframe height="100" width="200"></iframe></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+</pre>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/layout/style/test/test_bug1451199-2.html
@@ -0,0 +1,42 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1451199
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1451199</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript">
+
+  /** Test for Bug 1451199 **/
+  SimpleTest.waitForExplicitFinish();
+
+  addLoadEvent(function() {
+    // We have to be careful to not check l.matches until the very end
+    // of the test.
+    var l = frames[0].matchMedia("(orientation: portrait)");
+    var iframe = document.querySelector("iframe");
+    iframe.width = "50";
+    iframe.contentDocument.documentElement.offsetWidth; // Flush layout
+
+    l.addEventListener("change", function() {
+      is(l.matches, false,
+         "Should not match portrait by the time we get notified");
+      SimpleTest.finish();
+    });
+    iframe.width = "200";
+  });
+  </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1451199">Mozilla Bug 1451199</a>
+<p id="display"><iframe height="100" width="200"></iframe></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+</pre>
+</body>
+</html>