From 4a447b26d4e56f05c7174148f48d502c56a44b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 12 Sep 2018 19:54:48 +0000 Subject: [PATCH] Bug 1490791 - Make ShadowRoot stylesheet list work only when the shadow root is connected. r=smaug This avoids the issue of triggering stylesheet loads for disconnected stuff and such, and is more consistent with Chrome and WebKit. This is per the recent CSSWG resolution at https://github.com/w3c/csswg-drafts/issues/3096. Differential Revision: https://phabricator.services.mozilla.com/D5699 --- dom/base/nsStyleLinkElement.cpp | 8 +------- .../tests/shadow-dom/ShadowRoot-interface.html | 4 +++- .../tests/shadow-dom/untriaged/styles/test-003.html | 6 +----- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/dom/base/nsStyleLinkElement.cpp b/dom/base/nsStyleLinkElement.cpp index e71b392adfeb..9bdec644b030 100644 --- a/dom/base/nsStyleLinkElement.cpp +++ b/dom/base/nsStyleLinkElement.cpp @@ -303,11 +303,6 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument, // unload the stylesheet. We want to do this even if updates are // disabled, since otherwise a sheet with a stale linking element pointer // will be hanging around -- not good! - // - // TODO(emilio): We can reach this code with aOldShadowRoot == - // thisContent->GetContainingShadowRoot(), when moving the shadow host - // around. We probably could optimize some of this stuff out, is it worth - // it? if (aOldShadowRoot) { aOldShadowRoot->RemoveSheet(mStyleSheet); } else { @@ -317,8 +312,7 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument, SetStyleSheet(nullptr); } - nsIDocument* doc = thisContent->IsInShadowTree() - ? thisContent->OwnerDoc() : thisContent->GetUncomposedDoc(); + nsIDocument* doc = thisContent->GetComposedDoc(); // Loader could be null during unlink, see bug 1425866. if (!doc || !doc->CSSLoader() || !doc->CSSLoader()->GetEnabled()) { diff --git a/testing/web-platform/tests/shadow-dom/ShadowRoot-interface.html b/testing/web-platform/tests/shadow-dom/ShadowRoot-interface.html index 6b49f93b3ec8..bda361e5987d 100644 --- a/testing/web-platform/tests/shadow-dom/ShadowRoot-interface.html +++ b/testing/web-platform/tests/shadow-dom/ShadowRoot-interface.html @@ -94,8 +94,10 @@ var host = document.createElement('div'); var shadowRoot = host.attachShadow({'mode': mode}); - assert_equals(shadowRoot.styleSheets.length, 0, 'shadowRoot.styleSheets must be empty when the shadow root does not contain any stylesheets'); shadowRoot.innerHTML = ''; + assert_equals(shadowRoot.styleSheets.length, 0, 'shadowRoot.styleSheets must be empty when the shadow root is not connected'); + + document.body.appendChild(host); assert_equals(shadowRoot.styleSheets.length, 2, 'shadowRoot.styleSheets must contain two items when the shadow root has two style elements'); var styles = shadowRoot.querySelectorAll('style'); assert_equals(shadowRoot.styleSheets[0], styles[0].sheet, 'shadowRoot.styleSheets[0] must be the first style element in the shadow root'); diff --git a/testing/web-platform/tests/shadow-dom/untriaged/styles/test-003.html b/testing/web-platform/tests/shadow-dom/untriaged/styles/test-003.html index d5b8f48ab319..ab30e0cc67ae 100644 --- a/testing/web-platform/tests/shadow-dom/untriaged/styles/test-003.html +++ b/testing/web-platform/tests/shadow-dom/untriaged/styles/test-003.html @@ -46,9 +46,6 @@ assert_equals(s.styleSheets.length, 0, 'There should be no style sheets'); }), 'A_06_00_03_T02'); -//TODO Now this tests produces an error on Chromium because styleSheets.length -//returns 0 when the shadow root is orphaned. -//Tracking bug: http://crbug.com/392771 test(unit(function (ctx) { var d = newRenderedHTMLDocument(ctx); var host = d.createElement('div'); @@ -60,8 +57,7 @@ style.textContent = 'div {width: 50%;}'; s.appendChild(style); - // The following line fixes the issue on Chromium, http://crbug.com/392771 - // d.body.appendChild(host); + d.body.appendChild(host); assert_equals(s.styleSheets.length, 1, 'Style sheet is not accessible via styleSheets'); }), 'A_06_00_03_T03');