Bug 1470242 - Automatically upgrade Custom Elements inside of XBL anon content before the XBL constructor runs draft
authorTimothy Guan-tin Chien <timdream@gmail.com>
Thu, 21 Jun 2018 17:42:33 -0700
changeset 809409 ebb4907724a8a165dc4a436de01eb4f0859a0cca
parent 809377 27e90ec610a4c8f6b2a73d79cb1a4df38e822e6a
push id113674
push userbmo:timdream@gmail.com
push dateFri, 22 Jun 2018 03:20:44 +0000
bugs1470242
milestone62.0a1
Bug 1470242 - Automatically upgrade Custom Elements inside of XBL anon content before the XBL constructor runs MozReview-Commit-ID: 7bgOVEik1gJ
dom/xbl/nsXBLBinding.cpp
--- a/dom/xbl/nsXBLBinding.cpp
+++ b/dom/xbl/nsXBLBinding.cpp
@@ -25,16 +25,18 @@
 #include "nsIXMLContentSink.h"
 #include "nsContentCID.h"
 #include "mozilla/dom/XMLDocument.h"
 #include "jsapi.h"
 #include "nsXBLService.h"
 #include "nsIXPConnect.h"
 #include "nsIScriptContext.h"
 #include "nsCRT.h"
+#include "mozilla/dom/CustomElementRegistry.h"
+#include "mozilla/dom/DocGroup.h"
 
 // Event listeners
 #include "mozilla/EventListenerManager.h"
 #include "nsIDOMEventListener.h"
 #include "nsAttrName.h"
 
 #include "nsGkAtoms.h"
 
@@ -625,18 +627,39 @@ nsXBLBinding::AttributeChanged(nsAtom* a
 }
 
 void
 nsXBLBinding::ExecuteAttachedHandler()
 {
   if (mNextBinding)
     mNextBinding->ExecuteAttachedHandler();
 
-  if (AllowScripts())
+  if (AllowScripts()) {
+    if (nsContentUtils::IsCustomElementsEnabled()) {
+      // Trigger custom element construction in the anonymous content
+      // before calling into XBL binding constructor.
+      nsIDocument *document = mBoundElement->OwnerDoc();
+      {
+        nsAutoMicroTask mt;
+      }
+      dom::AutoCEReaction autoCEReaction(
+        document->GetDocGroup()->CustomElementReactionsStack(), nullptr);
+      nsINodeList* anonymousChildren =
+        document->BindingManager()->GetAnonymousNodesFor(mBoundElement);
+      if (anonymousChildren) {
+        uint32_t length = anonymousChildren->Length();
+        for (uint32_t i = 0; i < length; ++i) {
+          nsINode* node = anonymousChildren->Item(i);
+          document->GetInnerWindow()->CustomElements()->Upgrade(*node);
+        }
+      }
+    }
+
     mPrototypeBinding->BindingAttached(mBoundElement);
+  }
 }
 
 void
 nsXBLBinding::ExecuteDetachedHandler()
 {
   if (AllowScripts())
     mPrototypeBinding->BindingDetached(mBoundElement);