diff --git a/dom/base/FragmentOrElement.cpp b/dom/base/FragmentOrElement.cpp index 57b39dccacb8..3f96f568a525 100644 --- a/dom/base/FragmentOrElement.cpp +++ b/dom/base/FragmentOrElement.cpp @@ -1961,16 +1961,17 @@ void FragmentOrElement::SetInnerHTMLInternal(const nsAString& aInnerHTML, nsAutoScriptLoaderDisabler sld(doc); - nsAtom* contextLocalName = NodeInfo()->NameAtom(); - int32_t contextNameSpaceID = GetNameSpaceID(); - + FragmentOrElement* parseContext = this; if (ShadowRoot* shadowRoot = ShadowRoot::FromNode(this)) { - // Fix up the context to be the host of the ShadowRoot. - contextLocalName = shadowRoot->GetHost()->NodeInfo()->NameAtom(); - contextNameSpaceID = shadowRoot->GetHost()->GetNameSpaceID(); + // Fix up the context to be the host of the ShadowRoot. See + // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml setter step 1. + parseContext = shadowRoot->GetHost(); } if (doc->IsHTMLDocument()) { + nsAtom* contextLocalName = parseContext->NodeInfo()->NameAtom(); + int32_t contextNameSpaceID = parseContext->GetNameSpaceID(); + int32_t oldChildCount = target->GetChildCount(); aError = nsContentUtils::ParseFragmentHTML( aInnerHTML, target, contextLocalName, contextNameSpaceID, @@ -1981,14 +1982,14 @@ void FragmentOrElement::SetInnerHTMLInternal(const nsAString& aInnerHTML, oldChildCount); } else { RefPtr df = nsContentUtils::CreateContextualFragment( - target, aInnerHTML, true, aError); + parseContext, aInnerHTML, true, aError); if (!aError.Failed()) { // Suppress assertion about node removal mutation events that can't have // listeners anyway, because no one has had the chance to register // mutation listeners on the fragment that comes from the parser. nsAutoScriptBlockerSuppressNodeRemoved scriptBlocker; - static_cast(target)->AppendChild(*df, aError); + target->AppendChild(*df, aError); mb.NodesAdded(); } } diff --git a/dom/webidl/ShadowRoot.webidl b/dom/webidl/ShadowRoot.webidl index 1d1704ff24af..cfa2448db0e8 100644 --- a/dom/webidl/ShadowRoot.webidl +++ b/dom/webidl/ShadowRoot.webidl @@ -26,6 +26,7 @@ interface ShadowRoot : DocumentFragment Element? getElementById(DOMString elementId); + // https://w3c.github.io/DOM-Parsing/#the-innerhtml-mixin [CEReactions, SetterThrows] attribute [TreatNullAs=EmptyString] DOMString innerHTML; diff --git a/testing/web-platform/tests/shadow-dom/innerHTML-setter.xhtml b/testing/web-platform/tests/shadow-dom/innerHTML-setter.xhtml new file mode 100644 index 000000000000..0122707e2a91 --- /dev/null +++ b/testing/web-platform/tests/shadow-dom/innerHTML-setter.xhtml @@ -0,0 +1,81 @@ + + + + Test for Shadow DOM innerHTML setter in XML + + + + + + + + + + + + +