From 0bc8695c2ad0896af43e906059c64e5ff8add76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 26 Apr 2022 17:31:23 +0000 Subject: [PATCH] Bug 1737455 - Don't clear adoptedStyleSheets when adopting from a template document to its owner or vice versa. r=edgar See https://github.com/WICG/construct-stylesheets/issues/133, which is where this change was discussed (and then made to Chromium apparently). I filed https://github.com/w3c/csswg-drafts/issues/7229 to put some actual spec text here. Also, tweak the wpt which is supposed to test so that it fails without this change. Differential Revision: https://phabricator.services.mozilla.com/D144564 --- dom/base/Document.h | 4 +++ dom/base/DocumentOrShadowRoot.cpp | 5 ---- dom/base/DocumentOrShadowRoot.h | 2 -- dom/base/ShadowRoot.cpp | 11 +++++++ dom/base/ShadowRoot.h | 5 +--- .../CSSStyleSheet-template-adoption.html.ini | 4 --- .../CSSStyleSheet-template-adoption.html | 29 +++++++++++++------ 7 files changed, 36 insertions(+), 24 deletions(-) delete mode 100644 testing/web-platform/meta/css/cssom/CSSStyleSheet-template-adoption.html.ini diff --git a/dom/base/Document.h b/dom/base/Document.h index 78a12cf46f76..ba73133087ff 100644 --- a/dom/base/Document.h +++ b/dom/base/Document.h @@ -2889,6 +2889,10 @@ class Document : public nsINode, */ Document* GetTemplateContentsOwner(); + Document* GetTemplateContentsOwnerIfExists() const { + return mTemplateContentsOwner.get(); + } + /** * Returns true if this document is a static clone of a normal document. * diff --git a/dom/base/DocumentOrShadowRoot.cpp b/dom/base/DocumentOrShadowRoot.cpp index 647c0614e328..744d053cab8b 100644 --- a/dom/base/DocumentOrShadowRoot.cpp +++ b/dom/base/DocumentOrShadowRoot.cpp @@ -620,11 +620,6 @@ int32_t DocumentOrShadowRoot::StyleOrderIndexOfSheet( return mStyleSheets.IndexOf(&aSheet); } -void DocumentOrShadowRoot::GetAdoptedStyleSheets( - nsTArray>& aAdoptedStyleSheets) const { - aAdoptedStyleSheets = mAdoptedStyleSheets.Clone(); -} - void DocumentOrShadowRoot::TraverseSheetRefInStylesIfApplicable( StyleSheet& aSheet, nsCycleCollectionTraversalCallback& cb) { if (!aSheet.IsApplicable()) { diff --git a/dom/base/DocumentOrShadowRoot.h b/dom/base/DocumentOrShadowRoot.h index ad8f4ca8abe2..08422d317f9e 100644 --- a/dom/base/DocumentOrShadowRoot.h +++ b/dom/base/DocumentOrShadowRoot.h @@ -89,8 +89,6 @@ class DocumentOrShadowRoot : public RadioGroupManager { StyleSheetList* StyleSheets(); - void GetAdoptedStyleSheets(nsTArray>&) const; - void RemoveStyleSheet(StyleSheet&); Element* GetElementById(const nsAString& aElementId); diff --git a/dom/base/ShadowRoot.cpp b/dom/base/ShadowRoot.cpp index dea45b90e761..67fc00dbacbe 100644 --- a/dom/base/ShadowRoot.cpp +++ b/dom/base/ShadowRoot.cpp @@ -101,6 +101,17 @@ JSObject* ShadowRoot::WrapNode(JSContext* aCx, return mozilla::dom::ShadowRoot_Binding::Wrap(aCx, this, aGivenProto); } +void ShadowRoot::NodeInfoChanged(Document* aOldDoc) { + DocumentFragment::NodeInfoChanged(aOldDoc); + Document* newDoc = OwnerDoc(); + const bool fromOrToTemplate = + aOldDoc->GetTemplateContentsOwnerIfExists() == newDoc || + newDoc->GetTemplateContentsOwnerIfExists() == aOldDoc; + if (!fromOrToTemplate) { + ClearAdoptedStyleSheets(); + } +} + void ShadowRoot::CloneInternalDataFrom(ShadowRoot* aOther) { if (aOther->IsUAWidget()) { SetIsUAWidget(); diff --git a/dom/base/ShadowRoot.h b/dom/base/ShadowRoot.h index 1fe0576c96a0..c96b1005d9f8 100644 --- a/dom/base/ShadowRoot.h +++ b/dom/base/ShadowRoot.h @@ -185,10 +185,7 @@ class ShadowRoot final : public DocumentFragment, JSObject* WrapNode(JSContext*, JS::Handle aGivenProto) final; - void NodeInfoChanged(Document* aOldDoc) override { - DocumentFragment::NodeInfoChanged(aOldDoc); - ClearAdoptedStyleSheets(); - } + void NodeInfoChanged(Document* aOldDoc) override; void AddToIdTable(Element* aElement, nsAtom* aId); void RemoveFromIdTable(Element* aElement, nsAtom* aId); diff --git a/testing/web-platform/meta/css/cssom/CSSStyleSheet-template-adoption.html.ini b/testing/web-platform/meta/css/cssom/CSSStyleSheet-template-adoption.html.ini deleted file mode 100644 index 61d8c152115e..000000000000 --- a/testing/web-platform/meta/css/cssom/CSSStyleSheet-template-adoption.html.ini +++ /dev/null @@ -1,4 +0,0 @@ -[CSSStyleSheet-template-adoption.html] - [adoptedStyleSheets won'te be cleared when adopting into/from