Commit d04680eb authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

ElementInternals: Implement disabledFeatures=['shadow']

Add support of disabledFeatures=['shadow'] in a custom element
definition. It disables Element.attachShadow().

Specification PR: https://github.com/whatwg/dom/pull/760
The behavior is behind "ElemnetInternals" runtime flag.

Bug: 905922
Change-Id: Ib950883b5c995945eb60bf9b92c715ad3996dfa4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1609022
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarHayato Ito <hayato@chromium.org>
Reviewed-by: default avatarMason Freed <masonfreed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661327}
parent 5e61f16d
......@@ -2892,6 +2892,23 @@ ShadowRoot* Element::attachShadow(const ShadowRootInit* shadow_root_init_dict,
return nullptr;
}
// Checking IsCustomElement() here is just an optimization because
// IsValidName() is not cheap.
if (RuntimeEnabledFeatures::ElementInternalsEnabled() && IsCustomElement() &&
(CustomElement::IsValidName(localName()) || !IsValue().IsNull())) {
auto* registry = CustomElement::Registry(*this);
auto* definition =
registry ? registry->DefinitionForName(IsValue().IsNull() ? localName()
: IsValue())
: nullptr;
if (definition && definition->DisableShadow()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotSupportedError,
"attachShadow() is disabled by disabledFeatures static field.");
return nullptr;
}
}
if (GetShadowRoot()) {
exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
"Shadow root cannot be created on a host "
......
......@@ -34,6 +34,7 @@ CustomElementDefinition::CustomElementDefinition(
observed_attributes_(observed_attributes),
has_style_attribute_changed_callback_(
observed_attributes.Contains(html_names::kStyleAttr.LocalName())),
disable_shadow_(disabled_features.Contains(String("shadow"))),
disable_internals_(disabled_features.Contains(String("internals"))),
is_form_associated_(form_association_flag == FormAssociationFlag::kYes) {}
......
......@@ -122,6 +122,7 @@ class CORE_EXPORT CustomElementDefinition
bool HasDefaultStyleSheets() const {
return !default_style_sheets_.IsEmpty();
}
bool DisableShadow() const { return disable_shadow_; }
bool DisableInternals() const { return disable_internals_; }
bool IsFormAssociated() const { return is_form_associated_; }
......@@ -162,6 +163,7 @@ class CORE_EXPORT CustomElementDefinition
HashSet<AtomicString> observed_attributes_;
bool has_style_attribute_changed_callback_;
bool added_default_style_sheet_ = false;
bool disable_shadow_ = false;
bool disable_internals_ = false;
bool is_form_associated_ = false;
......
This is a testharness.js-based test.
PASS Element.attachShadow must create an instance of ShadowRoot for autonomous custom elements
PASS Element.attachShadow must create an instance of ShadowRoot for undefined autonomous custom elements
FAIL Element.attachShadow for an autonomous custom element with disabledFeatures=["shadow"] should throw a NotSupportedError assert_throws: Definition, not a host function "() => {
document.createElement('shadow-disabled-element').attachShadow({mode: 'closed'});
}" did not throw
FAIL Element.attachShadow for a customized built-in element with disabledFeatures=["shadow"] should throw a NotSupportedError assert_throws: Definition, not a host function "() => {
h2.attachShadow({mode: 'closed'});
}" did not throw
PASS Element.attachShadow for a custom element with disabledFeatures=["SHADOW"] should not throw a NotSupportedError
PASS Element.attachShadow must throw a NotSupportedError for customized built-in elements
Harness: the test ran to completion.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment