Bug 1523655 - Allow non-HTML elements to attach UA Shadow Root r=emilio

A normal shadow root cannot be attached to a non-HTML element so UA Shadow Root should always be allowed.

Differential Revision: https://phabricator.services.mozilla.com/D18158

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Timothy Guan-tin Chien 2019-01-31 18:33:30 +00:00
Родитель a694ddb320
Коммит 0e57cd7404
4 изменённых файлов: 29 добавлений и 14 удалений

Просмотреть файл

@ -1095,6 +1095,19 @@ ShadowRoot* Element::GetShadowRootByMode() const {
}
bool Element::CanAttachShadowDOM() const {
/**
* If context objects namespace is not the HTML namespace,
* return false.
*
* Deviate from the spec here to allow shadow dom attachement to
* XUL elements.
*/
if (!IsHTMLElement() &&
!(XRE_IsParentProcess() && IsXULElement() &&
nsContentUtils::AllowXULXBLForPrincipal(NodePrincipal()))) {
return false;
}
/**
* If context objects local name is not
* a valid custom element name, "article", "aside", "blockquote",
@ -1126,15 +1139,6 @@ already_AddRefed<ShadowRoot> Element::AttachShadow(const ShadowRootInit& aInit,
/**
* 1. If context objects namespace is not the HTML namespace,
* then throw a "NotSupportedError" DOMException.
*/
if (!IsHTMLElement() &&
!(XRE_IsParentProcess() && IsXULElement() &&
nsContentUtils::AllowXULXBLForPrincipal(NodePrincipal()))) {
aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
/**
* 2. If context objects local name is not valid to attach shadow DOM to,
* then throw a "NotSupportedError" DOMException.
*/
@ -1144,7 +1148,7 @@ already_AddRefed<ShadowRoot> Element::AttachShadow(const ShadowRootInit& aInit,
}
/**
* 2. If context object is a shadow host, then throw
* 3. If context object is a shadow host, then throw
* an "InvalidStateError" DOMException.
*/
if (GetShadowRoot() || GetXBLBinding()) {

Просмотреть файл

@ -0,0 +1,3 @@
<body>
<!-- XML root element with an known, allow-to-attch-shadow-root HTML tag name in non-HTML namespace should not trigger a crash -->
</body>

Просмотреть файл

@ -11,3 +11,4 @@ load 803586.xhtml
load 994740-1.xhtml
load 1038887.xhtml
load 1405878.xml
load 1523655.xml

Просмотреть файл

@ -43,6 +43,17 @@ nsresult nsXMLPrettyPrinter::PrettyPrint(Document* aDocument,
return NS_OK;
}
// Find the root element
RefPtr<Element> rootElement = aDocument->GetRootElement();
NS_ENSURE_TRUE(rootElement, NS_ERROR_UNEXPECTED);
// nsXMLContentSink should not ask us to pretty print an XML doc that comes
// with a CanAttachShadowDOM() == true root element, but just in case:
if (rootElement->CanAttachShadowDOM()) {
MOZ_DIAGNOSTIC_ASSERT(false, "We shouldn't be getting this root element");
return NS_ERROR_UNEXPECTED;
}
// Ok, we should prettyprint. Let's do it!
*aDidPrettyPrint = true;
nsresult rv = NS_OK;
@ -75,10 +86,6 @@ nsresult nsXMLPrettyPrinter::PrettyPrint(Document* aDocument,
return err.StealNSResult();
}
// Find the root element
RefPtr<Element> rootElement = aDocument->GetRootElement();
NS_ENSURE_TRUE(rootElement, NS_ERROR_UNEXPECTED);
// Attach an UA Widget Shadow Root on it.
rootElement->AttachAndSetUAShadowRoot();
RefPtr<ShadowRoot> shadowRoot = rootElement->GetShadowRoot();