Bug 839103 - Part 10: Add StyleSheetApplicableStateChange event. r=bz
authorCameron McCormack <cam@mcc.id.au>
Thu, 16 May 2013 17:13:36 +1000
changeset 132081 cf13d8dac8d950a48dc82b5262746941afe40ff2
parent 132080 e7b4aa5f253ec4e4ae2060dd695dd7186fde3490
child 132082 4b2d807f7fb42a47d969fe3e93181260e1f67f57
push id24683
push userryanvm@gmail.com
push dateThu, 16 May 2013 20:16:53 +0000
treeherdermozilla-central@8d7691da4700 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs839103
milestone24.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 839103 - Part 10: Add StyleSheetApplicableStateChange event. r=bz
browser/base/content/test/browser_bug839103.js
content/base/src/nsDocument.cpp
dom/interfaces/events/moz.build
dom/interfaces/events/nsIDOMStyleSheetApplicableStateChangeEvent.idl
dom/tests/mochitest/general/test_interfaces.html
js/xpconnect/src/event_impl_gen.conf.in
--- a/browser/base/content/test/browser_bug839103.js
+++ b/browser/base/content/test/browser_bug839103.js
@@ -1,30 +1,30 @@
 const gTestRoot = getRootDirectory(gTestPath);
 const gStyleSheet = "bug839103.css";
 
 var gTab = null;
+var needsInitialApplicableStateEvent = false;
+var needsInitialApplicableStateEventFor = null;
 
 function test() {
   waitForExplicitFinish();
   gBrowser.addEventListener("StyleSheetAdded", initialStylesheetAdded, true);
   gTab = gBrowser.selectedTab = gBrowser.addTab(gTestRoot + "test_bug839103.html");
   gTab.linkedBrowser.addEventListener("load", tabLoad, true);
 }
 
 function initialStylesheetAdded(evt) {
   gBrowser.removeEventListener("StyleSheetAdded", initialStylesheetAdded, true);
   ok(true, "received initial style sheet event");
   is(evt.type, "StyleSheetAdded", "evt.type has expected value");
   is(evt.target, gBrowser.contentDocument, "event targets correct document");
   ok(evt.stylesheet, "evt.stylesheet is defined");
   ok(evt.stylesheet.toString().contains("CSSStyleSheet"), "evt.stylesheet is a stylesheet");
   ok(evt.documentSheet, "style sheet is a document sheet");
-  is(typeof evt.sheetIsApplicable, "boolean", "evt.sheetIsApplicable is a boolean");
-  is(evt.rule, null, "evt.rule is null");
 }
 
 function tabLoad(evt) {
   gTab.linkedBrowser.removeEventListener(evt.type, tabLoad, true);
   executeSoon(continueTest);
 }
 
 var gLinkElement = null;
@@ -37,59 +37,80 @@ function unexpectedContentEvent(evt) {
 // Now add a stylesheet on the fly and make sure we see it.
 function continueTest() {
   info("continuing test");
 
   let doc = gBrowser.contentDocument;
   doc.styleSheetChangeEventsEnabled = true;
   doc.addEventListener("StyleSheetAdded", unexpectedContentEvent, false);
   doc.addEventListener("StyleSheetRemoved", unexpectedContentEvent, false);
+  doc.addEventListener("StyleSheetApplicableStateChanged", unexpectedContentEvent, false);
   doc.defaultView.addEventListener("StyleSheetAdded", unexpectedContentEvent, false);
   doc.defaultView.addEventListener("StyleSheetRemoved", unexpectedContentEvent, false);
+  doc.defaultView.addEventListener("StyleSheetApplicableStateChanged", unexpectedContentEvent, false);
   let link = doc.createElement('link');
   link.setAttribute('rel', 'stylesheet');
   link.setAttribute('type', 'text/css');
   link.setAttribute('href', gTestRoot + gStyleSheet);
   gLinkElement = link;
 
   gBrowser.addEventListener("StyleSheetAdded", dynamicStylesheetAdded, true);
+  gBrowser.addEventListener("StyleSheetApplicableStateChanged", dynamicStylesheetApplicableStateChanged, true);
   doc.body.appendChild(link);
 }
 
 function dynamicStylesheetAdded(evt) {
   gBrowser.removeEventListener("StyleSheetAdded", dynamicStylesheetAdded, true);
   ok(true, "received dynamic style sheet event");
   is(evt.type, "StyleSheetAdded", "evt.type has expected value");
   is(evt.target, gBrowser.contentDocument, "event targets correct document");
   ok(evt.stylesheet, "evt.stylesheet is defined");
   ok(evt.stylesheet.toString().contains("CSSStyleSheet"), "evt.stylesheet is a stylesheet");
   ok(evt.documentSheet, "style sheet is a document sheet");
-  is(typeof evt.sheetIsApplicable, "boolean", "evt.sheetIsApplicable is a boolean");
-  is(evt.rule, null, "evt.rule is null");
+}
 
+function dynamicStylesheetApplicableStateChanged(evt) {
+  ok(true, "received dynamic style sheet applicable state change event");
+  is(evt.type, "StyleSheetApplicableStateChanged", "evt.type has expected value");
+  is(evt.target, gBrowser.contentDocument, "event targets correct document");
+  is(evt.stylesheet, gLinkElement.sheet, "evt.stylesheet has the right value");
+  is(evt.applicable, true, "evt.applicable has the right value");
+  gBrowser.removeEventListener("StyleSheetApplicableStateChanged", dynamicStylesheetApplicableStateChanged, true);
+  gBrowser.addEventListener("StyleSheetApplicableStateChanged", dynamicStylesheetApplicableStateChangedToFalse, true);
+  gLinkElement.disabled = true;
+}
+
+function dynamicStylesheetApplicableStateChangedToFalse(evt) {
+  gBrowser.removeEventListener("StyleSheetApplicableStateChanged", dynamicStylesheetApplicableStateChangedToFalse, true);
+  is(evt.type, "StyleSheetApplicableStateChanged", "evt.type has expected value");
+  ok(true, "received dynamic style sheet applicable state change event after media=\"\" changed");
+  is(evt.target, gBrowser.contentDocument, "event targets correct document");
+  is(evt.stylesheet, gLinkElement.sheet, "evt.stylesheet has the right value");
+  is(evt.applicable, false, "evt.applicable has the right value");
   gBrowser.addEventListener("StyleSheetRemoved", dynamicStylesheetRemoved, true);
   gBrowser.contentDocument.body.removeChild(gLinkElement);
 }
 
 function dynamicStylesheetRemoved(evt) {
   gBrowser.removeEventListener("StyleSheetRemoved", dynamicStylesheetRemoved, true);
   ok(true, "received dynamic style sheet removal");
   is(evt.type, "StyleSheetRemoved", "evt.type has expected value");
   is(evt.target, gBrowser.contentDocument, "event targets correct document");
   ok(evt.stylesheet, "evt.stylesheet is defined");
   ok(evt.stylesheet.toString().contains("CSSStyleSheet"), "evt.stylesheet is a stylesheet");
   ok(evt.stylesheet.href.contains(gStyleSheet), "evt.stylesheet is the removed stylesheet");
-  is(typeof evt.sheetIsApplicable, "boolean", "evt.sheetIsApplicable is a boolean");
   is(evt.rule, null, "evt.rule is null");
   executeSoon(concludeTest);
 }
 
 function concludeTest() {
   let doc = gBrowser.contentDocument;
   doc.removeEventListener("StyleSheetAdded", unexpectedContentEvent, false);
   doc.removeEventListener("StyleSheetRemoved", unexpectedContentEvent, false);
+  doc.removeEventListener("StyleSheetApplicableStateChanged", unexpectedContentEvent, false);
   doc.defaultView.removeEventListener("StyleSheetAdded", unexpectedContentEvent, false);
   doc.defaultView.removeEventListener("StyleSheetRemoved", unexpectedContentEvent, false);
+  doc.defaultView.removeEventListener("StyleSheetApplicableStateChanged", unexpectedContentEvent, false);
   gBrowser.removeCurrentTab();
   gLinkElement = null;
   gTab = null;
   finish();
 }
--- a/content/base/src/nsDocument.cpp
+++ b/content/base/src/nsDocument.cpp
@@ -133,16 +133,17 @@
 #include "nsIDocumentLoader.h"
 #include "nsIContentViewer.h"
 #include "nsIXMLContentSink.h"
 #include "nsIXULDocument.h"
 #include "nsIPrompt.h"
 #include "nsIPropertyBag2.h"
 #include "nsIDOMPageTransitionEvent.h"
 #include "nsIDOMStyleSheetChangeEvent.h"
+#include "nsIDOMStyleSheetApplicableStateChangeEvent.h"
 #include "nsJSUtils.h"
 #include "nsFrameLoader.h"
 #include "nsEscape.h"
 #include "nsObjectLoadingContent.h"
 #include "nsHtml5TreeOpExecutor.h"
 #include "nsIDOMElementReplaceEvent.h"
 #include "mozilla/dom/HTMLMediaElement.h"
 #ifdef MOZ_WEBRTC
@@ -3769,18 +3770,16 @@ nsDocument::NotifyStyleSheetRemoved(nsIS
     DO_STYLESHEET_NOTIFICATION(NS_NewDOMStyleSheetChangeEvent,
                                nsIDOMStyleSheetChangeEvent,
                                InitStyleSheetChangeEvent,
                                "StyleSheetRemoved",
                                aDocumentSheet);
   }
 }
 
-#undef DO_STYLESHEET_NOTIFICATION
-
 void
 nsDocument::AddStyleSheet(nsIStyleSheet* aSheet)
 {
   NS_PRECONDITION(aSheet, "null arg");
   mStyleSheets.AppendObject(aSheet);
   aSheet->SetOwningDocument(this);
 
   if (aSheet->IsApplicable()) {
@@ -3890,17 +3889,27 @@ nsDocument::SetStyleSheetApplicableState
   }
 
   // We have to always notify, since this will be called for sheets
   // that are children of sheets in our style set, as well as some
   // sheets for nsHTMLEditor.
 
   NS_DOCUMENT_NOTIFY_OBSERVERS(StyleSheetApplicableStateChanged,
                                (this, aSheet, aApplicable));
-}
+
+  if (StyleSheetChangeEventsEnabled()) {
+    DO_STYLESHEET_NOTIFICATION(NS_NewDOMStyleSheetApplicableStateChangeEvent,
+                               nsIDOMStyleSheetApplicableStateChangeEvent,
+                               InitStyleSheetApplicableStateChangeEvent,
+                               "StyleSheetApplicableStateChanged",
+                               aApplicable);
+  }
+}
+
+#undef DO_STYLESHEET_NOTIFICATION
 
 // These three functions are a lot like the implementation of the
 // corresponding API for regular stylesheets.
 
 int32_t
 nsDocument::GetNumberOfCatalogStyleSheets() const
 {
   return mCatalogSheets.Count();
--- a/dom/interfaces/events/moz.build
+++ b/dom/interfaces/events/moz.build
@@ -42,16 +42,17 @@ XPIDL_SOURCES += [
     'nsIDOMPaintRequestList.idl',
     'nsIDOMPopStateEvent.idl',
     'nsIDOMPopupBlockedEvent.idl',
     'nsIDOMProgressEvent.idl',
     'nsIDOMScrollAreaEvent.idl',
     'nsIDOMSimpleGestureEvent.idl',
     'nsIDOMSmartCardEvent.idl',
     'nsIDOMStyleSheetChangeEvent.idl',
+    'nsIDOMStyleSheetApplicableStateChangeEvent.idl',
     'nsIDOMTouchEvent.idl',
     'nsIDOMTransitionEvent.idl',
     'nsIDOMUIEvent.idl',
     'nsIDOMUserProximityEvent.idl',
     'nsIDOMWheelEvent.idl',
 ]
 
 XPIDL_MODULE = 'dom_events'
new file mode 100644
--- /dev/null
+++ b/dom/interfaces/events/nsIDOMStyleSheetApplicableStateChangeEvent.idl
@@ -0,0 +1,27 @@
+/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "nsIDOMEvent.idl"
+
+interface nsIDOMCSSStyleSheet;
+
+[scriptable, builtinclass, uuid(75f27960-fd1a-4e05-8b8b-55d97ea22a9a)]
+interface nsIDOMStyleSheetApplicableStateChangeEvent : nsIDOMEvent
+{
+  readonly attribute nsIDOMCSSStyleSheet stylesheet;
+  readonly attribute boolean applicable;
+  [noscript] void initStyleSheetApplicableStateChangeEvent(in DOMString aTypeArg,
+                                                           in boolean aCanBubbleArg,
+                                                           in boolean aCancelableArg,
+                                                           in nsIDOMCSSStyleSheet aStyleSheet,
+                                                           in boolean aApplicable);
+};
+
+dictionary StyleSheetApplicableStateChangeEventInit : EventInit
+{
+  nsIDOMCSSStyleSheet stylesheet;
+  boolean applicable;
+};
+
--- a/dom/tests/mochitest/general/test_interfaces.html
+++ b/dom/tests/mochitest/general/test_interfaces.html
@@ -542,16 +542,17 @@ var interfaceNamesInGlobalScope =
     "GamepadEvent",
     "GamepadButtonEvent",
     "GamepadAxisMoveEvent",
     "SpeechRecognitionEvent",
     "SpeechRecognitionError",
     "SpeechSynthesisEvent",
     "PushManager",
     "StyleSheetChangeEvent",
+    "StyleSheetApplicableStateChangeEvent",
     "MozMobileMessageThread",
     "PaymentRequestInfo",
   ]
 // IMPORTANT: Do not change this list without review from a DOM peer!
 
 // If your interface is named nsIDOMSomeInterface and you don't mean to expose
 // it to every webpage, simply change its name to nsISomeInterface to fix this problem.
 
--- a/js/xpconnect/src/event_impl_gen.conf.in
+++ b/js/xpconnect/src/event_impl_gen.conf.in
@@ -20,16 +20,17 @@ simple_events = [
     'HashChangeEvent',
     'CloseEvent',
     'MozContactChangeEvent',
     'DeviceOrientationEvent',
     'DeviceLightEvent',
     'MozApplicationEvent',
     'SmartCardEvent',
     'StyleSheetChangeEvent',
+    'StyleSheetApplicableStateChangeEvent',
 #ifdef MOZ_B2G_BT
     'BluetoothDeviceEvent',
 #endif
 #ifdef MOZ_B2G_RIL
     'CallEvent',
     'CFStateChangeEvent',
     'DataErrorEvent',
     'ICCCardLockErrorEvent',