Bug 1497940 - add nsGenericHTMLElement::AttachAndSetUAShadowRoot helper method, r=smaug

This commit is contained in:
Alexander Surkov 2018-11-02 12:18:20 +08:00
parent 9a352187c1
commit 3395e926b6
6 changed files with 52 additions and 36 deletions

View File

@ -1200,26 +1200,15 @@ Element::GetShadowRootByMode() const
return shadowRoot;
}
// https://dom.spec.whatwg.org/#dom-element-attachshadow
already_AddRefed<ShadowRoot>
Element::AttachShadow(const ShadowRootInit& aInit, ErrorResult& aError)
bool
Element::CanAttachShadowDOM() const
{
/**
* 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
* a valid custom element name, "article", "aside", "blockquote",
* "body", "div", "footer", "h1", "h2", "h3", "h4", "h5", "h6",
* "header", "main" "nav", "p", "section", or "span",
* then throw a "NotSupportedError" DOMException.
* If context objects local name is not
* a valid custom element name, "article", "aside", "blockquote",
* "body", "div", "footer", "h1", "h2", "h3", "h4", "h5", "h6",
* "header", "main" "nav", "p", "section", or "span",
* return false.
*/
nsAtom* nameAtom = NodeInfo()->NameAtom();
if (!(nsContentUtils::IsCustomElementName(nameAtom, NodeInfo()->NamespaceID()) ||
@ -1241,12 +1230,37 @@ Element::AttachShadow(const ShadowRootInit& aInit, ErrorResult& aError)
nameAtom == nsGkAtoms::p ||
nameAtom == nsGkAtoms::section ||
nameAtom == nsGkAtoms::span)) {
return false;
}
return true;
}
// https://dom.spec.whatwg.org/#dom-element-attachshadow
already_AddRefed<ShadowRoot>
Element::AttachShadow(const ShadowRootInit& aInit, ErrorResult& aError)
{
/**
* 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;
}
/**
* 3. If context object is a shadow host, then throw
* 2. If context objects local name is not valid to attach shadow DOM to,
* then throw a "NotSupportedError" DOMException.
*/
if (!CanAttachShadowDOM()) {
aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return nullptr;
}
/**
* 2. If context object is a shadow host, then throw
* an "InvalidStateError" DOMException.
*/
if (GetShadowRoot() || GetXBLBinding()) {

View File

@ -1267,6 +1267,7 @@ public:
// Shadow DOM v1
already_AddRefed<ShadowRoot> AttachShadow(const ShadowRootInit& aInit,
ErrorResult& aError);
bool CanAttachShadowDOM() const;
already_AddRefed<ShadowRoot> AttachShadowWithoutNameChecks(ShadowRootMode aMode);
void UnattachShadow();

View File

@ -4932,20 +4932,6 @@ HTMLMediaElement::AssertReadyStateIsNothing()
#endif
}
void
HTMLMediaElement::AttachAndSetUAShadowRoot()
{
if (GetShadowRoot()) {
MOZ_ASSERT(GetShadowRoot()->IsUAWidget());
return;
}
// Add a closed shadow root to host video controls
RefPtr<ShadowRoot> shadowRoot =
AttachShadowWithoutNameChecks(ShadowRootMode::Closed);
shadowRoot->SetIsUAWidget();
}
nsresult
HTMLMediaElement::InitializeDecoderAsClone(ChannelMediaDecoder* aOriginal)
{

View File

@ -1903,9 +1903,6 @@ private:
// For debugging bug 1407148.
void AssertReadyStateIsNothing();
// Attach UA Shadow Root if it is not attached.
void AttachAndSetUAShadowRoot();
// Contains the unique id of the sink device and the device info.
// The initial value is ("", nullptr) and the default output device is used.
// It can contain an invalid id and info if the device has been

View File

@ -2892,6 +2892,21 @@ nsGenericHTMLElement::IsEventAttributeNameInternal(nsAtom *aName)
return nsContentUtils::IsEventAttributeName(aName, EventNameType_HTML);
}
void
nsGenericHTMLElement::AttachAndSetUAShadowRoot()
{
MOZ_DIAGNOSTIC_ASSERT(!CanAttachShadowDOM(),
"Cannot be used to attach UI shadow DOM");
if (GetShadowRoot()) {
MOZ_ASSERT(GetShadowRoot()->IsUAWidget());
return;
}
RefPtr<ShadowRoot> shadowRoot =
AttachShadowWithoutNameChecks(ShadowRootMode::Closed);
shadowRoot->SetIsUAWidget();
}
/**
* Construct a URI from a string, as an element.src attribute
* would be set to. Helper for the media elements.

View File

@ -256,6 +256,9 @@ public:
return IsNodeInternal(aFirst, aArgs...);
}
// Attach UA Shadow Root if it is not attached.
void AttachAndSetUAShadowRoot();
protected:
virtual ~nsGenericHTMLElement() {}