Bug 1467722: Do not throw when we don't have a style, but just return an empty style. r=heycam
authorEmilio Cobos Álvarez <emilio@crisal.io>
Fri, 08 Jun 2018 11:21:59 +0200
changeset 479701 c2b039dd2ace878328009a25e080d8703aa53b04
parent 479700 7a18800b893dee5ba5d051de33f7a10daa8fb9c2
child 479702 1034f8be097fd6d2f8eda8f7f7d277b92d91ff41
push id1757
push userffxbld-merge
push dateFri, 24 Aug 2018 17:02:43 +0000
treeherdermozilla-release@736023aebdb1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersheycam
bugs1467722
milestone62.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 1467722: Do not throw when we don't have a style, but just return an empty style. r=heycam We return '0' for the length, and "" for every declaration. This matches other browsers and the spec in the "no style" behavior. Of course we don't claim not to have a style for every case the spec says, but that will come later, given that's a much more risky change. This doesn't make any case where we returned something useful return something less useful, but stops null from getting returned, and returns the empty string which matches other browsers when they cannot return a style. MozReview-Commit-ID: 7Sc7HL5CgZU
layout/style/nsComputedDOMStyle.cpp
testing/web-platform/tests/css/cssom/getComputedStyle-detached-subtree.html
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -409,24 +409,26 @@ nsComputedDOMStyle::SetCssText(const nsA
                                ErrorResult& aRv)
 {
   aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
 }
 
 uint32_t
 nsComputedDOMStyle::Length()
 {
-  uint32_t length = GetComputedStyleMap()->Length();
-
   // Make sure we have up to date style so that we can include custom
   // properties.
   UpdateCurrentStyleSources(false);
-  if (mComputedStyle) {
-    length += Servo_GetCustomPropertiesCount(mComputedStyle);
-  }
+  if (!mComputedStyle) {
+    return 0;
+  }
+
+  uint32_t length =
+    GetComputedStyleMap()->Length() +
+    Servo_GetCustomPropertiesCount(mComputedStyle);
 
   ClearCurrentStyleSources();
 
   return length;
 }
 
 css::Rule*
 nsComputedDOMStyle::GetParentRule()
@@ -449,17 +451,17 @@ nsComputedDOMStyle::GetPropertyValue(con
     if (!entry) {
       return NS_OK;
     }
   }
 
   const bool layoutFlushIsNeeded = entry && entry->IsLayoutFlushNeeded();
   UpdateCurrentStyleSources(layoutFlushIsNeeded);
   if (!mComputedStyle) {
-    return NS_ERROR_NOT_AVAILABLE;
+    return NS_OK;
   }
 
   auto cleanup = mozilla::MakeScopeExit([&] {
     ClearCurrentStyleSources();
   });
 
   if (!entry) {
     MOZ_ASSERT(nsCSSProps::IsCustomPropertyName(aPropertyName));
@@ -756,17 +758,16 @@ nsComputedDOMStyle::GetCSSImageURLs(cons
   if (prop == eCSSProperty_UNKNOWN) {
     aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
     return;
   }
 
   UpdateCurrentStyleSources(false);
 
   if (!mComputedStyle) {
-    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
     return;
   }
 
   CollectImageURLsForProperty(prop, *mComputedStyle, aImageURLs);
   ClearCurrentStyleSources();
 }
 
 // nsDOMCSSDeclaration abstract methods which should never be called
--- a/testing/web-platform/tests/css/cssom/getComputedStyle-detached-subtree.html
+++ b/testing/web-platform/tests/css/cssom/getComputedStyle-detached-subtree.html
@@ -7,32 +7,36 @@
 <script src=/resources/testharnessreport.js></script>
 <div id="host">
   <div id="non-slotted">
     <div id="non-slotted-descendant"></div>
   </div>
 </div>
 <iframe srcdoc="<html></html>" style="display: none"></iframe>
 <script>
-function testNoComputedStyle(element, description) {
+function testNoComputedStyle(element, description, global) {
   test(function() {
     assert_true(!!element);
-    let style = getComputedStyle(element);
+    let style = (global ? global : window).getComputedStyle(element);
     assert_true(!!style);
     assert_true(style.length === 0);
     assert_equals(style.color, "");
   }, `getComputedStyle returns no style for ${description}`);
 }
 
 let detached = document.createElement('div');
 testNoComputedStyle(detached, "detached element");
 
 testNoComputedStyle(document.querySelector('iframe').contentDocument.documentElement,
                     "element in non-rendered iframe (display: none)");
 
+testNoComputedStyle(document.querySelector('iframe').contentDocument.documentElement,
+                    "element in non-rendered iframe (display: none) from iframe's window",
+                    document.querySelector('iframe').contentWindow);
+
 host.attachShadow({ mode: "open" });
 testNoComputedStyle(document.getElementById('non-slotted'),
                     "element outside the flat tree");
 
 testNoComputedStyle(document.getElementById('non-slotted-descendant'),
                     "descendant outside the flat tree");
 
 let shadowRoot = detached.attachShadow({ mode: "open" });