Backed out changeset 39f61de4e7bb (bug 1484478) for bustages in /builds/worker/workspace/build/src/dom/base/ShadowRoot.cpp:526:20
authorshindli <shindli@mozilla.com>
Mon, 20 Aug 2018 14:21:08 +0300
changeset 432420 fb70309402b4aa63cff4ffa42c95daff8af2df2f
parent 432419 ffb2036e3283325cc68ef0c91d906b65b88348d3
child 432421 31ed81aa4f4682946b228f6db2b0d7a67a0975cd
push id106731
push usershindli@mozilla.com
push dateMon, 20 Aug 2018 16:36:25 +0000
treeherdermozilla-inbound@14ef4dec6126 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1484478
milestone63.0a1
backs out39f61de4e7bb164f1ad812852b4f46bd18e3d96d
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
Backed out changeset 39f61de4e7bb (bug 1484478) for bustages in /builds/worker/workspace/build/src/dom/base/ShadowRoot.cpp:526:20
dom/base/CharacterData.cpp
dom/base/Element.cpp
dom/base/FragmentOrElement.cpp
dom/base/ShadowRoot.cpp
dom/base/ShadowRoot.h
dom/base/nsDocument.cpp
dom/base/nsIDocument.h
dom/base/nsINode.cpp
dom/base/nsINode.h
dom/webidl/Node.webidl
layout/style/ServoStyleSet.cpp
--- a/dom/base/CharacterData.cpp
+++ b/dom/base/CharacterData.cpp
@@ -447,18 +447,16 @@ CharacterData::BindToTree(nsIDocument* a
 {
   MOZ_ASSERT(aParent || aDocument, "Must have document if no parent!");
   MOZ_ASSERT(NODE_FROM(aParent, aDocument)->OwnerDoc() == OwnerDoc(),
              "Must have the same owner document");
   MOZ_ASSERT(!aParent || aDocument == aParent->GetUncomposedDoc(),
              "aDocument must be current doc of aParent");
   MOZ_ASSERT(!GetUncomposedDoc() && !IsInUncomposedDoc(),
              "Already have a document.  Unbind first!");
-  MOZ_ASSERT(!IsInComposedDoc(),
-             "Already have a document.  Unbind first!");
   // Note that as we recurse into the kids, they'll have a non-null parent.  So
   // only assert if our parent is _changing_ while we have a parent.
   MOZ_ASSERT(!GetParent() || aParent == GetParent(),
              "Already have a parent.  Unbind first!");
   MOZ_ASSERT(!GetBindingParent() ||
              aBindingParent == GetBindingParent() ||
              (!aBindingParent && aParent &&
               aParent->GetBindingParent() == GetBindingParent()),
@@ -486,50 +484,50 @@ CharacterData::BindToTree(nsIDocument* a
       SetFlags(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE);
     }
     if (aParent->HasFlag(NODE_CHROME_ONLY_ACCESS)) {
       SetFlags(NODE_CHROME_ONLY_ACCESS);
     }
     if (HasFlag(NODE_IS_ANONYMOUS_ROOT)) {
       aParent->SetMayHaveAnonymousChildren();
     }
-  }
-
-  if (aParent && aParent->IsInShadowTree()) {
-    ClearSubtreeRootPointer();
-    SetFlags(NODE_IS_IN_SHADOW_TREE);
-    SetIsConnected(aParent->IsInComposedDoc());
-    MOZ_ASSERT(aParent->GetContainingShadow());
-    ExtendedContentSlots()->mContainingShadow = aParent->GetContainingShadow();
+    if (aParent->IsInShadowTree()) {
+      ClearSubtreeRootPointer();
+      SetFlags(NODE_IS_IN_SHADOW_TREE);
+    }
+    ShadowRoot* parentContainingShadow = aParent->GetContainingShadow();
+    if (parentContainingShadow) {
+      ExtendedContentSlots()->mContainingShadow = parentContainingShadow;
+    }
   }
 
   bool hadParent = !!GetParentNode();
 
   // Set parent
   if (aParent) {
     if (!GetParent()) {
       NS_ADDREF(aParent);
     }
     mParent = aParent;
-  } else {
+  }
+  else {
     mParent = aDocument;
   }
   SetParentIsContent(aParent);
 
   // XXXbz sXBL/XBL2 issue!
 
   // Set document
   if (aDocument) {
     // We no longer need to track the subtree pointer (and in fact we'll assert
     // if we do this any later).
     ClearSubtreeRootPointer();
 
     // XXX See the comment in Element::BindToTree
     SetIsInDocument();
-    SetIsConnected(true);
     if (mText.IsBidi()) {
       aDocument->SetBidiEnabled();
     }
     // Clear the lazy frame construction bits.
     UnsetFlags(NODE_NEEDS_FRAME | NODE_DESCENDANTS_NEED_FRAMES);
   } else if (!IsInShadowTree()) {
     // If we're not in the doc and not in a shadow tree,
     // update our subtree pointer.
@@ -550,33 +548,33 @@ CharacterData::BindToTree(nsIDocument* a
 
   return NS_OK;
 }
 
 void
 CharacterData::UnbindFromTree(bool aDeep, bool aNullParent)
 {
   // Unset frame flags; if we need them again later, they'll get set again.
-  UnsetFlags(NS_CREATE_FRAME_IF_NON_WHITESPACE | NS_REFRAME_IF_WHITESPACE);
+  UnsetFlags(NS_CREATE_FRAME_IF_NON_WHITESPACE |
+             NS_REFRAME_IF_WHITESPACE);
 
   nsIDocument* document = GetComposedDoc();
 
   if (aNullParent) {
     if (this->IsRootOfNativeAnonymousSubtree()) {
       nsNodeUtils::NativeAnonymousChildListChange(this, true);
     }
     if (GetParent()) {
       NS_RELEASE(mParent);
     } else {
       mParent = nullptr;
     }
     SetParentIsContent(false);
   }
   ClearInDocument();
-  SetIsConnected(false);
 
   if (aNullParent || !mParent->IsInShadowTree()) {
     UnsetFlags(NODE_IS_IN_SHADOW_TREE);
 
     // Begin keeping track of our subtree root.
     SetSubtreeRootPointer(aNullParent ? this : mParent->SubtreeRoot());
   }
 
--- a/dom/base/Element.cpp
+++ b/dom/base/Element.cpp
@@ -1253,16 +1253,18 @@ Element::AttachShadowWithoutNameChecks(S
   /**
    * 4. Let shadow be a new shadow root whose node document is
    *    context object’s node document, host is context object,
    *    and mode is init’s mode.
    */
   RefPtr<ShadowRoot> shadowRoot =
     new ShadowRoot(this, aMode, nodeInfo.forget());
 
+  shadowRoot->SetIsComposedDocParticipant(IsInComposedDoc());
+
   if (NodeOrAncestorHasDirAuto()) {
     shadowRoot->SetAncestorHasDirAuto();
   }
 
   /**
    * 5. Set context object’s shadow root to shadow.
    */
   SetShadowRoot(shadowRoot);
@@ -1289,27 +1291,31 @@ Element::UnattachShadow()
 {
   RefPtr<ShadowRoot> shadowRoot = GetShadowRoot();
   if (!shadowRoot) {
     return;
   }
 
   nsAutoScriptBlocker scriptBlocker;
 
-  nsIDocument* doc = GetComposedDoc();
-  if (doc) {
+  if (nsIDocument* doc = GetComposedDoc()) {
     if (nsIPresShell* shell = doc->GetShell()) {
       shell->DestroyFramesForAndRestyle(this);
     }
   }
   MOZ_ASSERT(!GetPrimaryFrame());
 
   // Simply unhook the shadow root from the element.
-  MOZ_ASSERT(!shadowRoot->HasSlots(), "Won't work when shadow root has slots!");
-  shadowRoot->Unbind();
+  MOZ_ASSERT(!GetShadowRoot()->HasSlots(), "Won't work when shadow root has slots!");
+  for (nsIContent* child = shadowRoot->GetFirstChild(); child;
+       child = child->GetNextSibling()) {
+    child->UnbindFromTree(true, false);
+  }
+
+  shadowRoot->SetIsComposedDocParticipant(false);
   SetShadowRoot(nullptr);
 }
 
 void
 Element::GetAttribute(const nsAString& aName, DOMString& aReturn)
 {
   const nsAttrValue* val =
     mAttrs.GetAttr(aName,
@@ -1614,18 +1620,17 @@ nsresult
 Element::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
                     nsIContent* aBindingParent)
 {
   MOZ_ASSERT(aParent || aDocument, "Must have document if no parent!");
   MOZ_ASSERT((NODE_FROM(aParent, aDocument)->OwnerDoc() == OwnerDoc()),
              "Must have the same owner document");
   MOZ_ASSERT(!aParent || aDocument == aParent->GetUncomposedDoc(),
              "aDocument must be current doc of aParent");
-  MOZ_ASSERT(!IsInComposedDoc(), "Already have a document.  Unbind first!");
-  MOZ_ASSERT(!IsInUncomposedDoc(), "Already have a document.  Unbind first!");
+  MOZ_ASSERT(!GetUncomposedDoc(), "Already have a document.  Unbind first!");
   // Note that as we recurse into the kids, they'll have a non-null parent.  So
   // only assert if our parent is _changing_ while we have a parent.
   MOZ_ASSERT(!GetParent() || aParent == GetParent(),
              "Already have a parent.  Unbind first!");
   MOZ_ASSERT(!GetBindingParent() ||
              aBindingParent == GetBindingParent() ||
              (!aBindingParent && aParent &&
               aParent->GetBindingParent() == GetBindingParent()),
@@ -1668,30 +1673,33 @@ Element::BindToTree(nsIDocument* aDocume
       SetFlags(NODE_CHROME_ONLY_ACCESS);
     }
     if (HasFlag(NODE_IS_ANONYMOUS_ROOT)) {
       aParent->SetMayHaveAnonymousChildren();
     }
     if (aParent->IsInShadowTree()) {
       ClearSubtreeRootPointer();
       SetFlags(NODE_IS_IN_SHADOW_TREE);
-      MOZ_ASSERT(aParent->GetContainingShadow());
-      ExtendedDOMSlots()->mContainingShadow = aParent->GetContainingShadow();
+    }
+    ShadowRoot* parentContainingShadow = aParent->GetContainingShadow();
+    if (parentContainingShadow) {
+      ExtendedDOMSlots()->mContainingShadow = parentContainingShadow;
     }
   }
 
   bool hadParent = !!GetParentNode();
 
   // Now set the parent.
   if (aParent) {
     if (!GetParent()) {
       NS_ADDREF(aParent);
     }
     mParent = aParent;
-  } else {
+  }
+  else {
     mParent = aDocument;
   }
   SetParentIsContent(aParent);
 
   // XXXbz sXBL/XBL2 issue!
 
   MOZ_ASSERT(!HasAnyOfFlags(Element::kAllServoDescendantBits));
 
@@ -1707,22 +1715,20 @@ Element::BindToTree(nsIDocument* aDocume
     //                                                   aDocument);
 
     // We no longer need to track the subtree pointer (and in fact we'll assert
     // if we do this any later).
     ClearSubtreeRootPointer();
 
     // Being added to a document.
     SetIsInDocument();
-    SetIsConnected(true);
 
     // Clear the lazy frame construction bits.
     UnsetFlags(NODE_NEEDS_FRAME | NODE_DESCENDANTS_NEED_FRAMES);
   } else if (IsInShadowTree()) {
-    SetIsConnected(aParent->IsInComposedDoc());
     // We're not in a document, but we did get inserted into a shadow tree.
     // Since we won't have any restyle data in the document's restyle trackers,
     // don't let us get inserted with restyle bits set incorrectly.
     //
     // Also clear all the other flags that are cleared above when we do get
     // inserted into a document.
     //
     // See the comment about the restyle bits above, it also applies.
@@ -1817,18 +1823,23 @@ Element::BindToTree(nsIDocument* aDocume
     // XXXbz if we already have a style attr parsed, this won't do
     // anything... need to fix that.
     // If MayHaveStyle() is true, we must be an nsStyledElement
     static_cast<nsStyledElement*>(this)->ReparseStyleAttribute(false, false);
   }
 
   // Call BindToTree on shadow root children.
   if (ShadowRoot* shadowRoot = GetShadowRoot()) {
-    rv = shadowRoot->Bind();
-    NS_ENSURE_SUCCESS(rv, rv);
+    shadowRoot->SetIsComposedDocParticipant(IsInComposedDoc());
+    MOZ_ASSERT(shadowRoot->GetBindingParent() == this);
+    for (nsIContent* child = shadowRoot->GetFirstChild(); child;
+         child = child->GetNextSibling()) {
+      rv = child->BindToTree(nullptr, shadowRoot, this);
+      NS_ENSURE_SUCCESS(rv, rv);
+    }
   }
 
   // FIXME(emilio): Why is this needed? The element shouldn't even be styled in
   // the first place, we should style it properly eventually.
   //
   // Also, if this _is_ needed, then it's wrong and should use GetComposedDoc()
   // to account for Shadow DOM.
   if (aDocument && MayHaveAnimations()) {
@@ -1978,17 +1989,16 @@ Element::UnbindFromTree(bool aDeep, bool
                  presContext->GetViewportScrollStylesOverrideElement(),
                  "Leaving behind a raw pointer to this element (as having "
                  "propagated scrollbar styles) - that's dangerous...");
     }
   }
 #endif
 
   ClearInDocument();
-  SetIsConnected(false);
 
   // Ensure that CSS transitions don't continue on an element at a
   // different place in the tree (even if reinserted before next
   // animation refresh).
   // We need to delete the properties while we're still in document
   // (if we were in document).
   // FIXME (Bug 522599): Need a test for this.
   if (MayHaveAnimations()) {
@@ -2089,17 +2099,22 @@ Element::UnbindFromTree(bool aDeep, bool
       child->UnbindFromTree(true, false);
     }
   }
 
   nsNodeUtils::ParentChainChanged(this);
 
   // Unbind children of shadow root.
   if (ShadowRoot* shadowRoot = GetShadowRoot()) {
-    shadowRoot->Unbind();
+    for (nsIContent* child = shadowRoot->GetFirstChild(); child;
+         child = child->GetNextSibling()) {
+      child->UnbindFromTree(true, false);
+    }
+
+    shadowRoot->SetIsComposedDocParticipant(false);
   }
 
   MOZ_ASSERT(!HasAnyOfFlags(kAllServoDescendantBits));
   MOZ_ASSERT(!document || document->GetServoRestyleRoot() != this);
 }
 
 nsDOMCSSAttributeDeclaration*
 Element::SMILOverrideStyle()
--- a/dom/base/FragmentOrElement.cpp
+++ b/dom/base/FragmentOrElement.cpp
@@ -1460,17 +1460,23 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Fr
   } else if (!tmp->GetParent() && tmp->HasChildren()) {
     ContentUnbinder::Append(tmp);
   } /* else {
     The subtree root will end up to a ContentUnbinder, and that will
     unbind the child nodes.
   } */
 
   if (ShadowRoot* shadowRoot = tmp->GetShadowRoot()) {
-    shadowRoot->Unbind();
+    for (nsIContent* child = shadowRoot->GetFirstChild();
+         child;
+         child = child->GetNextSibling()) {
+      child->UnbindFromTree(true, false);
+    }
+
+    shadowRoot->SetIsComposedDocParticipant(false);
     tmp->ExtendedDOMSlots()->mShadowRoot = nullptr;
   }
 
   nsIDocument* doc = tmp->OwnerDoc();
   doc->BindingManager()->RemovedFromDocument(tmp, doc,
                                              nsBindingManager::eDoNotRunDtor);
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
--- a/dom/base/ShadowRoot.cpp
+++ b/dom/base/ShadowRoot.cpp
@@ -58,27 +58,27 @@ NS_IMPL_ADDREF_INHERITED(ShadowRoot, Doc
 NS_IMPL_RELEASE_INHERITED(ShadowRoot, DocumentFragment)
 
 ShadowRoot::ShadowRoot(Element* aElement, ShadowRootMode aMode,
                        already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
   : DocumentFragment(aNodeInfo)
   , DocumentOrShadowRoot(*this)
   , mMode(aMode)
   , mServoStyles(Servo_AuthorStyles_Create())
+  , mIsComposedDocParticipant(false)
   , mIsUAWidget(false)
 {
   SetHost(aElement);
 
   // Nodes in a shadow tree should never store a value
   // in the subtree root pointer, nodes in the shadow tree
   // track the subtree root using GetContainingShadow().
   ClearSubtreeRootPointer();
 
   SetFlags(NODE_IS_IN_SHADOW_TREE);
-  Bind();
 
   ExtendedDOMSlots()->mBindingParent = aElement;
   ExtendedDOMSlots()->mContainingShadow = this;
 
   // Add the ShadowRoot as a mutation observer on the host to watch
   // for mutations because the insertion points in this ShadowRoot
   // may need to be updated when the host children are modified.
   GetHost()->AddMutationObserver(this);
@@ -86,28 +86,45 @@ ShadowRoot::ShadowRoot(Element* aElement
 
 ShadowRoot::~ShadowRoot()
 {
   if (auto* host = GetHost()) {
     // mHost may have been unlinked.
     host->RemoveMutationObserver(this);
   }
 
-  if (IsInComposedDoc()) {
+  if (IsComposedDocParticipant()) {
     OwnerDoc()->RemoveComposedDocShadowRoot(*this);
   }
 
   MOZ_DIAGNOSTIC_ASSERT(!OwnerDoc()->IsComposedDocShadowRoot(*this));
 
   UnsetFlags(NODE_IS_IN_SHADOW_TREE);
 
   // nsINode destructor expects mSubtreeRoot == this.
   SetSubtreeRootPointer(this);
 }
 
+void
+ShadowRoot::SetIsComposedDocParticipant(bool aIsComposedDocParticipant)
+{
+  bool changed = mIsComposedDocParticipant != aIsComposedDocParticipant;
+  mIsComposedDocParticipant = aIsComposedDocParticipant;
+  if (!changed) {
+    return;
+  }
+
+  nsIDocument* doc = OwnerDoc();
+  if (IsComposedDocParticipant()) {
+    doc->AddComposedDocShadowRoot(*this);
+  } else {
+    doc->RemoveComposedDocShadowRoot(*this);
+  }
+}
+
 JSObject*
 ShadowRoot::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return mozilla::dom::ShadowRoot_Binding::Wrap(aCx, this, aGivenProto);
 }
 
 void
 ShadowRoot::CloneInternalDataFrom(ShadowRoot* aOther)
@@ -120,60 +137,28 @@ ShadowRoot::CloneInternalDataFrom(Shadow
         sheet->Clone(nullptr, nullptr, this, nullptr);
       if (clonedSheet) {
         AppendStyleSheet(*clonedSheet.get());
       }
     }
   }
 }
 
-nsresult
-ShadowRoot::Bind()
-{
-  MOZ_ASSERT(!IsInComposedDoc(), "Forgot to unbind?");
-  if (Host()->IsInComposedDoc()) {
-    SetIsConnected(true);
-    OwnerDoc()->AddComposedDocShadowRoot(*this);
-  }
-
-  for (nsIContent* child = GetFirstChild();
-       child;
-       child = child->GetNextSibling()) {
-    nsresult rv = child->BindToTree(nullptr, this, Host());
-    NS_ENSURE_SUCCESS(rv, rv);
-  }
-
-  return NS_OK;
-}
-
-void
-ShadowRoot::Unbind()
-{
-  if (IsInComposedDoc()) {
-    SetIsConnected(false);
-    OwnerDoc()->RemoveComposedDocShadowRoot(*this);
-  }
-
-  for (nsIContent* child = GetFirstChild();
-       child;
-       child = child->GetNextSibling()) {
-    child->UnbindFromTree(true, false);
-  }
-}
-
 void
 ShadowRoot::InvalidateStyleAndLayoutOnSubtree(Element* aElement)
 {
   MOZ_ASSERT(aElement);
-  nsIDocument* doc = GetComposedDoc();
-  if (!doc) {
+
+  if (!IsComposedDocParticipant()) {
     return;
   }
 
-  nsIPresShell* shell = doc->GetShell();
+  MOZ_ASSERT(GetComposedDoc() == OwnerDoc());
+
+  nsIPresShell* shell = OwnerDoc()->GetShell();
   if (!shell) {
     return;
   }
 
   shell->DestroyFramesForAndRestyle(aElement);
 }
 
 void
@@ -320,21 +305,21 @@ void
 ShadowRoot::RuleChanged(StyleSheet&, css::Rule*) {
   Servo_AuthorStyles_ForceDirty(mServoStyles.get());
   ApplicableRulesChanged();
 }
 
 void
 ShadowRoot::ApplicableRulesChanged()
 {
-  nsIDocument* doc = GetComposedDoc();
-  if (!doc) {
+  if (!IsComposedDocParticipant()) {
     return;
   }
 
+  nsIDocument* doc = OwnerDoc();
   if (nsIPresShell* shell = doc->GetShell()) {
     shell->RecordShadowStyleChange(*this);
   }
 }
 
 void
 ShadowRoot::InsertSheetAt(size_t aIndex, StyleSheet& aSheet)
 {
@@ -518,17 +503,17 @@ ShadowRoot::MaybeReassignElement(Element
   HTMLSlotElement* oldSlot = aElement->GetAssignedSlot();
   SlotAssignment assignment = SlotAssignmentFor(aElement);
 
   if (assignment.mSlot == oldSlot) {
     // Nothing to do here.
     return;
   }
 
-  if (nsIDocument* doc = GetComposedDoc()) {
+  if (IsComposedDocParticipant()) {
     if (nsIPresShell* shell = OwnerDoc()->GetShell()) {
       shell->SlotAssignmentWillChange(*aElement, oldSlot, assignment.mSlot);
     }
   }
 
   if (oldSlot) {
     oldSlot->RemoveAssignedNode(aElement);
     oldSlot->EnqueueSlotChangeEvent();
--- a/dom/base/ShadowRoot.h
+++ b/dom/base/ShadowRoot.h
@@ -84,24 +84,16 @@ public:
   }
 
   /**
    * Clones internal state, for example stylesheets, of aOther to 'this'.
    */
   void CloneInternalDataFrom(ShadowRoot* aOther);
   void InsertSheetAt(size_t aIndex, StyleSheet&);
 
-  // Calls UnbindFromTree for each of our kids, and also flags us as no longer
-  // being connected.
-  void Unbind();
-
-  // Calls BindToTree on each of our kids, and also maybe flags us as being
-  // connected.
-  nsresult Bind();
-
 private:
   void InsertSheetIntoAuthorData(size_t aIndex, StyleSheet&);
 
   void AppendStyleSheet(StyleSheet& aSheet)
   {
     InsertSheetAt(SheetCount(), aSheet);
   }
 
@@ -188,16 +180,23 @@ public:
   nsINode* ImportNodeAndAppendChildAt(nsINode& aParentNode,
                                       nsINode& aNode,
                                       bool aDeep, mozilla::ErrorResult& rv);
 
   nsINode* CreateElementAndAppendChildAt(nsINode& aParentNode,
                                          const nsAString& aTagName,
                                          mozilla::ErrorResult& rv);
 
+  bool IsComposedDocParticipant() const
+  {
+    return mIsComposedDocParticipant;
+  }
+
+  void SetIsComposedDocParticipant(bool aIsComposedDocParticipant);
+
   bool IsUAWidget() const
   {
     return mIsUAWidget;
   }
 
   void SetIsUAWidget()
   {
     mIsUAWidget = true;
@@ -218,16 +217,22 @@ protected:
   UniquePtr<mozilla::ServoStyleRuleMap> mStyleRuleMap;
 
   using SlotArray = AutoTArray<HTMLSlotElement*, 1>;
   // Map from name of slot to an array of all slots in the shadow DOM with with
   // the given name. The slots are stored as a weak pointer because the elements
   // are in the shadow tree and should be kept alive by its parent.
   nsClassHashtable<nsStringHashKey, SlotArray> mSlotMap;
 
+  // Flag to indicate whether the descendants of this shadow root are part of the
+  // composed document. Ideally, we would use a node flag on nodes to
+  // mark whether it is in the composed document, but we have run out of flags
+  // so instead we track it here.
+  bool mIsComposedDocParticipant;
+
   bool mIsUAWidget;
 
   nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
 };
 
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -1514,17 +1514,16 @@ nsIDocument::nsIDocument()
     mCurrentOrientationType(OrientationType::Portrait_primary),
     mServoRestyleRootDirtyBits(0),
     mThrowOnDynamicMarkupInsertionCounter(0),
     mIgnoreOpensDuringUnloadCounter(0),
     mNumTrackersFound(0),
     mNumTrackersBlocked(0)
 {
   SetIsInDocument();
-  SetIsConnected(true);
 }
 
 nsDocument::nsDocument(const char* aContentType)
   : nsIDocument()
 {
   SetContentTypeInternal(nsDependentCString(aContentType));
 
   MOZ_LOG(gDocumentLeakPRLog, LogLevel::Debug, ("DOCUMENT %p created", this));
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -3812,17 +3812,17 @@ protected:
   RefPtr<nsHTMLStyleSheet> mAttrStyleSheet;
   RefPtr<nsHTMLCSSStyleSheet> mStyleAttrStyleSheet;
 
   // Tracking for images in the document.
   RefPtr<mozilla::dom::ImageTracker> mImageTracker;
 
   // A hashtable of ShadowRoots belonging to the composed doc.
   //
-  // See ShadowRoot::Bind and ShadowRoot::Unbind.
+  // See ShadowRoot::SetIsComposedDocParticipant.
   ShadowRootSet mComposedShadowRoots;
 
   using SVGUseElementSet =
     nsTHashtable<nsPtrHashKey<mozilla::dom::SVGUseElement>>;
 
   // The set of <svg:use> elements that need a shadow tree reclone because the
   // tree they map to has changed.
   SVGUseElementSet mSVGUseElementsNeedingShadowTreeUpdate;
--- a/dom/base/nsINode.cpp
+++ b/dom/base/nsINode.cpp
@@ -246,18 +246,18 @@ nsINode::GetTextEditorRootContent(TextEd
     return rootElement;
   }
   return nullptr;
 }
 
 nsINode* nsINode::GetRootNode(const GetRootNodeOptions& aOptions)
 {
   if (aOptions.mComposed) {
-    if (nsIDocument* doc = GetComposedDoc()) {
-      return doc;
+    if (IsInComposedDoc() && GetComposedDoc()) {
+      return OwnerDoc();
     }
 
     nsINode* node = this;
     while(node) {
       node = node->SubtreeRoot();
       ShadowRoot* shadow = ShadowRoot::FromNode(node);
       if (!shadow) {
         break;
@@ -452,24 +452,35 @@ nsINode::InvalidateChildNodes()
 }
 
 void
 nsINode::GetTextContentInternal(nsAString& aTextContent, OOMReporter& aError)
 {
   SetDOMStringToNull(aTextContent);
 }
 
+nsIDocument*
+nsINode::GetComposedDocInternal() const
+{
+  MOZ_ASSERT(HasFlag(NODE_IS_IN_SHADOW_TREE) && IsContent(),
+             "Should only be caled on nodes in the shadow tree.");
+
+  ShadowRoot* containingShadow = AsContent()->GetContainingShadow();
+  return containingShadow && containingShadow->IsComposedDocParticipant() ?
+    OwnerDoc() : nullptr;
+}
+
 DocumentOrShadowRoot*
 nsINode::GetUncomposedDocOrConnectedShadowRoot() const
 {
   if (IsInUncomposedDoc()) {
     return OwnerDoc();
   }
 
-  if (IsInComposedDoc() && IsInShadowTree()) {
+  if (IsInComposedDoc() && HasFlag(NODE_IS_IN_SHADOW_TREE)) {
     return AsContent()->GetContainingShadow();
   }
 
   return nullptr;
 }
 
 #ifdef DEBUG
 void
--- a/dom/base/nsINode.h
+++ b/dom/base/nsINode.h
@@ -638,34 +638,34 @@ public:
    */
 
   nsIDocument* GetUncomposedDoc() const
   {
     return IsInUncomposedDoc() ? OwnerDoc() : nullptr;
   }
 
   /**
-   * Returns true if we're connected, and thus GetComposedDoc() would return a
-   * non-null value.
-   */
-  bool IsInComposedDoc() const
-  {
-    return GetBoolFlag(IsConnected);
-  }
-
-  /**
    * This method returns the owner document if the node is connected to it
    * (as defined in the DOM spec), otherwise it returns null.
    * In other words, returns non-null even in the case the node is in
    * Shadow DOM, if there is a possibly shadow boundary crossing path from
    * the node to its owner document.
    */
   nsIDocument* GetComposedDoc() const
   {
-    return IsInComposedDoc() ? OwnerDoc() : nullptr;
+    return IsInShadowTree() ?
+      GetComposedDocInternal() : GetUncomposedDoc();
+  }
+
+  /**
+   * Returns true if GetComposedDoc() would return a non-null value.
+   */
+  bool IsInComposedDoc() const
+  {
+    return IsInUncomposedDoc() || (IsInShadowTree() && GetComposedDocInternal());
   }
 
   /**
    * Returns OwnerDoc() if the node is in uncomposed document and ShadowRoot if
    * the node is in Shadow DOM and is in composed document.
    */
   mozilla::dom::DocumentOrShadowRoot* GetUncomposedDocOrConnectedShadowRoot() const;
 
@@ -1413,16 +1413,18 @@ public:
   bool Contains(const nsINode* aOther) const;
 
   bool UnoptimizableCCNode() const;
 
 private:
 
   mozilla::dom::SVGUseElement* DoGetContainingSVGUseShadowHost() const;
 
+  nsIDocument* GetComposedDocInternal() const;
+
   nsIContent* GetNextNodeImpl(const nsINode* aRoot,
                               const bool aSkipChildren) const
   {
     // Can't use nsContentUtils::ContentIsDescendantOf here, since we
     // can't include it here.
 #ifdef DEBUG
     if (aRoot) {
       const nsINode* cur = this;
@@ -1494,19 +1496,16 @@ public:
    */
 private:
   enum BooleanFlag {
     // Set if we're being used from -moz-element
     NodeHasRenderingObservers,
     // Set if our parent chain (including this node itself) terminates
     // in a document
     IsInDocument,
-    // Set if we're part of the composed doc.
-    // https://dom.spec.whatwg.org/#connected
-    IsConnected,
     // Set if mParent is an nsIContent
     ParentIsContent,
     // Set if this node is an Element
     NodeIsElement,
     // Set if the element has a non-empty id attribute. This can in rare
     // cases lie for nsXMLElement, such as when the node has been moved between
     // documents with different id mappings.
     ElementHasID,
@@ -1551,16 +1550,18 @@ private:
     // Set if the node is a text node descendant of a node with dir=auto
     // and has a TextNodeDirectionalityMap property listing the elements whose
     // direction it determines.
     NodeHasTextNodeDirectionalityMap,
     // Set if a node in the node's parent chain has dir=auto.
     NodeAncestorHasDirAuto,
     // Set if the node is handling a click.
     NodeHandlingClick,
+    // Set if the node has had :hover selectors matched against it
+    NodeHasRelevantHoverRules,
     // Set if the element has a parser insertion mode other than "in body",
     // per the HTML5 "Parse state" section.
     ElementHasWeirdParserInsertionMode,
     // Parser sets this flag if it has notified about the node.
     ParserHasNotified,
     // Sets if the node is apz aware or we have apz aware listeners.
     MayBeApzAware,
     // Set if the element might have any kind of anonymous content children,
@@ -1674,16 +1675,18 @@ public:
 
   void SetAncestorHasDirAuto() { SetBoolFlag(NodeAncestorHasDirAuto); }
   void ClearAncestorHasDirAuto() { ClearBoolFlag(NodeAncestorHasDirAuto); }
   bool AncestorHasDirAuto() const { return GetBoolFlag(NodeAncestorHasDirAuto); }
 
   // Implemented in nsIContentInlines.h.
   inline bool NodeOrAncestorHasDirAuto() const;
 
+  bool HasRelevantHoverRules() const { return GetBoolFlag(NodeHasRelevantHoverRules); }
+  void SetHasRelevantHoverRules() { SetBoolFlag(NodeHasRelevantHoverRules); }
   void SetParserHasNotified() { SetBoolFlag(ParserHasNotified); };
   bool HasParserNotified() { return GetBoolFlag(ParserHasNotified); }
 
   void SetMayBeApzAware() { SetBoolFlag(MayBeApzAware); }
   bool NodeMayBeApzAware() const
   {
     return GetBoolFlag(MayBeApzAware);
   }
@@ -1692,19 +1695,18 @@ public:
   bool MayHaveAnonymousChildren() const { return GetBoolFlag(ElementMayHaveAnonymousChildren); }
 
   void SetHasCustomElementData() { SetBoolFlag(ElementHasCustomElementData); }
   bool HasCustomElementData() const { return GetBoolFlag(ElementHasCustomElementData); }
 
 protected:
   void SetParentIsContent(bool aValue) { SetBoolFlag(ParentIsContent, aValue); }
   void SetIsInDocument() { SetBoolFlag(IsInDocument); }
+  void SetNodeIsContent() { SetBoolFlag(NodeIsContent); }
   void ClearInDocument() { ClearBoolFlag(IsInDocument); }
-  void SetIsConnected(bool aConnected) { SetBoolFlag(IsConnected, aConnected); }
-  void SetNodeIsContent() { SetBoolFlag(NodeIsContent); }
   void SetIsElement() { SetBoolFlag(NodeIsElement); }
   void SetHasID() { SetBoolFlag(ElementHasID); }
   void ClearHasID() { ClearBoolFlag(ElementHasID); }
   void SetMayHaveStyle() { SetBoolFlag(ElementMayHaveStyle); }
   void SetHasName() { SetBoolFlag(ElementHasName); }
   void ClearHasName() { ClearBoolFlag(ElementHasName); }
   void SetMayHaveContentEditableAttr()
     { SetBoolFlag(ElementMayHaveContentEditableAttr); }
--- a/dom/webidl/Node.webidl
+++ b/dom/webidl/Node.webidl
@@ -29,17 +29,17 @@ interface Node : EventTarget {
   [Constant]
   readonly attribute unsigned short nodeType;
   [Pure]
   readonly attribute DOMString nodeName;
 
   [Pure, Throws, NeedsCallerType, BinaryName="baseURIFromJS"]
   readonly attribute DOMString? baseURI;
 
-  [Pure, BinaryName=isInComposedDoc]
+  [Pure, BinaryName=getComposedDoc]
   readonly attribute boolean isConnected;
   [Pure]
   readonly attribute Document? ownerDocument;
   [Pure]
   Node getRootNode(optional GetRootNodeOptions options);
   [Pure]
   readonly attribute Node? parentNode;
   [Pure]
--- a/layout/style/ServoStyleSet.cpp
+++ b/layout/style/ServoStyleSet.cpp
@@ -171,17 +171,17 @@ ServoStyleSet::Init(nsPresContext* aPres
 template<typename Functor>
 void
 EnumerateShadowRoots(const nsIDocument& aDoc, const Functor& aCb)
 {
   const nsIDocument::ShadowRootSet& shadowRoots = aDoc.ComposedShadowRoots();
   for (auto iter = shadowRoots.ConstIter(); !iter.Done(); iter.Next()) {
     ShadowRoot* root = iter.Get()->GetKey();
     MOZ_ASSERT(root);
-    MOZ_DIAGNOSTIC_ASSERT(root->IsInComposedDoc());
+    MOZ_DIAGNOSTIC_ASSERT(root->IsComposedDocParticipant());
     aCb(*root);
   }
 }
 
 void
 ServoStyleSet::Shutdown()
 {
   // Make sure we drop our cached styles before the presshell arena starts going