ShadowRoot& ElementShadow::addShadowRoot(Element& shadowHost, ShadowRootType type) { EventDispatchForbiddenScope assertNoEventDispatch; ScriptForbiddenScope forbidScript; if (type == ShadowRootType::V0 && m_shadowRoot) { DCHECK_EQ(m_shadowRoot->type(), ShadowRootType::V0); Deprecation::countDeprecation(shadowHost.document(), UseCounter::ElementCreateShadowRootMultiple); } if (m_shadowRoot) { // TODO(hayato): Is the order, from the youngest to the oldest, important? for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShadowRoot()) root->lazyReattachIfAttached(); } ShadowRoot* shadowRoot = ShadowRoot::create(shadowHost.document(), type); shadowRoot->setParentOrShadowHostNode(&shadowHost); shadowRoot->setParentTreeScope(shadowHost.treeScope()); appendShadowRoot(*shadowRoot); setNeedsDistributionRecalc(); shadowRoot->insertedInto(&shadowHost); shadowHost.setChildNeedsStyleRecalc(); shadowHost.setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::Shadow)); InspectorInstrumentation::didPushShadowRoot(&shadowHost, shadowRoot); return *shadowRoot; }