Commit b062ae37 authored by Rakina Zata Amni's avatar Rakina Zata Amni Committed by Commit Bot

Make custom element default style accept array of stylesheets

After developer feedback on ergonomics, having a way to add multiple
stylesheets as the default style for a custom element seems to be
preferred. This CL changes custom element default style to handle
multiple stylesheets.

Bug: 824684
Change-Id: Ie0a130db42425396132ed48237e4b24c34243031
Reviewed-on: https://chromium-review.googlesource.com/c/1257468Reviewed-by: default avatarHayato Ito <hayato@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597425}
parent 1965860e
...@@ -191,7 +191,7 @@ HTMLElement* ScriptCustomElementDefinition::CreateAutonomousCustomElementSync( ...@@ -191,7 +191,7 @@ HTMLElement* ScriptCustomElementDefinition::CreateAutonomousCustomElementSync(
if (element->prefix() != tag_name.Prefix()) if (element->prefix() != tag_name.Prefix())
element->SetTagNameForCreateElementNS(tag_name); element->SetTagNameForCreateElementNS(tag_name);
DCHECK_EQ(element->GetCustomElementState(), CustomElementState::kCustom); DCHECK_EQ(element->GetCustomElementState(), CustomElementState::kCustom);
AddDefaultStyle(element); AddDefaultStylesTo(*element);
return ToHTMLElement(element); return ToHTMLElement(element);
} }
......
...@@ -213,10 +213,11 @@ static void MatchCustomElementRules(const Element& element, ...@@ -213,10 +213,11 @@ static void MatchCustomElementRules(const Element& element,
return; return;
if (CustomElementDefinition* definition = if (CustomElementDefinition* definition =
element.GetCustomElementDefinition()) { element.GetCustomElementDefinition()) {
if (CSSStyleSheet* default_style = definition->DefaultStyleSheet()) { if (definition->HasDefaultStyleSheets()) {
collector.CollectMatchingRules( for (CSSStyleSheet* style : definition->DefaultStyleSheets()) {
MatchRequest(element.GetDocument().GetStyleEngine().RuleSetForSheet( collector.CollectMatchingRules(MatchRequest(
*default_style))); element.GetDocument().GetStyleEngine().RuleSetForSheet(*style)));
}
} }
} }
} }
......
...@@ -318,11 +318,13 @@ void StyleEngine::AdoptedStyleSheetsWillChange(TreeScope& tree_scope, ...@@ -318,11 +318,13 @@ void StyleEngine::AdoptedStyleSheetsWillChange(TreeScope& tree_scope,
SetNeedsActiveStyleUpdate(tree_scope); SetNeedsActiveStyleUpdate(tree_scope);
} }
void StyleEngine::AddedCustomElementDefaultStyle(CSSStyleSheet* default_style) { void StyleEngine::AddedCustomElementDefaultStyles(
const HeapVector<Member<CSSStyleSheet>>& default_styles) {
if (!RuntimeEnabledFeatures::CustomElementDefaultStyleEnabled() || if (!RuntimeEnabledFeatures::CustomElementDefaultStyleEnabled() ||
GetDocument().IsDetached()) GetDocument().IsDetached())
return; return;
custom_element_default_style_sheets_.insert(default_style); for (CSSStyleSheet* sheet : default_styles)
custom_element_default_style_sheets_.insert(sheet);
global_rule_set_->MarkDirty(); global_rule_set_->MarkDirty();
} }
......
...@@ -133,7 +133,8 @@ class CORE_EXPORT StyleEngine final ...@@ -133,7 +133,8 @@ class CORE_EXPORT StyleEngine final
void AdoptedStyleSheetsWillChange(TreeScope&, void AdoptedStyleSheetsWillChange(TreeScope&,
StyleSheetList* old_sheets, StyleSheetList* old_sheets,
StyleSheetList* new_sheets); StyleSheetList* new_sheets);
void AddedCustomElementDefaultStyle(CSSStyleSheet* default_style); void AddedCustomElementDefaultStyles(
const HeapVector<Member<CSSStyleSheet>>& default_styles);
void MediaQueriesChangedInScope(TreeScope&); void MediaQueriesChangedInScope(TreeScope&);
void WatchedSelectorsChanged(); void WatchedSelectorsChanged();
void InitialStyleChanged(); void InitialStyleChanged();
......
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
// Spec: https://html.spec.whatwg.org/#elementdefinitionoptions // Spec: https://html.spec.whatwg.org/#elementdefinitionoptions
dictionary ElementDefinitionOptions { dictionary ElementDefinitionOptions {
DOMString? extends = null; DOMString? extends = null;
CSSStyleSheet style; sequence<CSSStyleSheet> styles;
}; };
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "third_party/blink/renderer/core/html/custom/custom_element_definition.h" #include "third_party/blink/renderer/core/html/custom/custom_element_definition.h"
#include "third_party/blink/renderer/core/css/css_import_rule.h" #include "third_party/blink/renderer/core/css/css_import_rule.h"
#include "third_party/blink/renderer/core/css/css_style_sheet.h"
#include "third_party/blink/renderer/core/css/style_change_reason.h" #include "third_party/blink/renderer/core/css/style_change_reason.h"
#include "third_party/blink/renderer/core/css/style_engine.h" #include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/css/style_sheet_contents.h" #include "third_party/blink/renderer/core/css/style_sheet_contents.h"
...@@ -39,7 +38,7 @@ CustomElementDefinition::~CustomElementDefinition() = default; ...@@ -39,7 +38,7 @@ CustomElementDefinition::~CustomElementDefinition() = default;
void CustomElementDefinition::Trace(blink::Visitor* visitor) { void CustomElementDefinition::Trace(blink::Visitor* visitor) {
visitor->Trace(construction_stack_); visitor->Trace(construction_stack_);
visitor->Trace(default_style_sheet_); visitor->Trace(default_style_sheets_);
} }
static String ErrorMessageForConstructorResult(Element* element, static String ErrorMessageForConstructorResult(Element* element,
...@@ -207,31 +206,33 @@ void CustomElementDefinition::Upgrade(Element* element) { ...@@ -207,31 +206,33 @@ void CustomElementDefinition::Upgrade(Element* element) {
} }
element->SetCustomElementDefinition(this); element->SetCustomElementDefinition(this);
AddDefaultStyle(element); AddDefaultStylesTo(*element);
} }
void CustomElementDefinition::AddDefaultStyle(Element* element) { void CustomElementDefinition::AddDefaultStylesTo(Element& element) {
if (!RuntimeEnabledFeatures::CustomElementDefaultStyleEnabled()) if (!RuntimeEnabledFeatures::CustomElementDefaultStyleEnabled() ||
return; !HasDefaultStyleSheets())
CSSStyleSheet* default_style = DefaultStyleSheet();
if (!default_style)
return;
Document* associated_document = default_style->AssociatedDocument();
if (associated_document && associated_document != &element->GetDocument()) {
// Throw exception here?
return; return;
const auto& default_styles = DefaultStyleSheets();
for (CSSStyleSheet* style : default_styles) {
Document* associated_document = style->AssociatedDocument();
if (associated_document && associated_document != &element.GetDocument()) {
// No spec yet, but for now we forbid usage of other document's
// constructed stylesheet.
return;
}
} }
if (!added_default_style_sheet_) { if (!added_default_style_sheet_) {
element->GetDocument().GetStyleEngine().AddedCustomElementDefaultStyle( element.GetDocument().GetStyleEngine().AddedCustomElementDefaultStyles(
default_style); default_styles);
added_default_style_sheet_ = true; added_default_style_sheet_ = true;
const AtomicString& local_tag_name = const AtomicString& local_tag_name = element.LocalNameForSelectorMatching();
element->LocalNameForSelectorMatching(); for (CSSStyleSheet* sheet : default_styles)
default_style->AddToCustomElementTagNames(local_tag_name); sheet->AddToCustomElementTagNames(local_tag_name);
} }
element->SetNeedsStyleRecalc( element.SetNeedsStyleRecalc(kLocalStyleChange,
kLocalStyleChange, StyleChangeReasonForTracing::Create( StyleChangeReasonForTracing::Create(
StyleChangeReason::kActiveStylesheetsUpdate)); StyleChangeReason::kActiveStylesheetsUpdate));
} }
bool CustomElementDefinition::HasAttributeChangedCallback( bool CustomElementDefinition::HasAttributeChangedCallback(
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h" #include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/css_style_sheet.h"
#include "third_party/blink/renderer/core/dom/create_element_flags.h" #include "third_party/blink/renderer/core/dom/create_element_flags.h"
#include "third_party/blink/renderer/core/html/custom/custom_element_descriptor.h" #include "third_party/blink/renderer/core/html/custom/custom_element_descriptor.h"
#include "third_party/blink/renderer/platform/bindings/name_client.h" #include "third_party/blink/renderer/platform/bindings/name_client.h"
...@@ -18,7 +19,6 @@ ...@@ -18,7 +19,6 @@
namespace blink { namespace blink {
class CSSStyleSheet;
class Document; class Document;
class Element; class Element;
class ExceptionState; class ExceptionState;
...@@ -91,11 +91,18 @@ class CORE_EXPORT CustomElementDefinition ...@@ -91,11 +91,18 @@ class CORE_EXPORT CustomElementDefinition
const AtomicString& old_value, const AtomicString& old_value,
const AtomicString& new_value); const AtomicString& new_value);
void SetDefaultStyleSheet(CSSStyleSheet& default_style_sheet) { void SetDefaultStyleSheets(
default_style_sheet_ = default_style_sheet; const HeapVector<Member<CSSStyleSheet>>& default_style_sheets) {
default_style_sheets_ = default_style_sheets;
} }
CSSStyleSheet* DefaultStyleSheet() const { return default_style_sheet_; } const HeapVector<Member<CSSStyleSheet>>& DefaultStyleSheets() const {
return default_style_sheets_;
}
bool HasDefaultStyleSheets() const {
return !default_style_sheets_.IsEmpty();
}
class CORE_EXPORT ConstructionStackScope final { class CORE_EXPORT ConstructionStackScope final {
STACK_ALLOCATED(); STACK_ALLOCATED();
...@@ -117,7 +124,7 @@ class CORE_EXPORT CustomElementDefinition ...@@ -117,7 +124,7 @@ class CORE_EXPORT CustomElementDefinition
CustomElementDefinition(const CustomElementDescriptor&, CustomElementDefinition(const CustomElementDescriptor&,
const HashSet<AtomicString>& observed_attributes); const HashSet<AtomicString>& observed_attributes);
void AddDefaultStyle(Element*); void AddDefaultStylesTo(Element&);
virtual bool RunConstructor(Element*) = 0; virtual bool RunConstructor(Element*) = 0;
...@@ -133,7 +140,7 @@ class CORE_EXPORT CustomElementDefinition ...@@ -133,7 +140,7 @@ class CORE_EXPORT CustomElementDefinition
bool has_style_attribute_changed_callback_; bool has_style_attribute_changed_callback_;
bool added_default_style_sheet_ = false; bool added_default_style_sheet_ = false;
Member<CSSStyleSheet> default_style_sheet_; HeapVector<Member<CSSStyleSheet>> default_style_sheets_;
void EnqueueAttributeChangedCallbackForAllAttributes(Element*); void EnqueueAttributeChangedCallbackForAllAttributes(Element*);
......
...@@ -212,8 +212,9 @@ CustomElementDefinition* CustomElementRegistry::DefineInternal( ...@@ -212,8 +212,9 @@ CustomElementDefinition* CustomElementRegistry::DefineInternal(
CHECK(!exception_state.HadException()); CHECK(!exception_state.HadException());
CHECK(definition->Descriptor() == descriptor); CHECK(definition->Descriptor() == descriptor);
if (RuntimeEnabledFeatures::CustomElementDefaultStyleEnabled() && if (RuntimeEnabledFeatures::CustomElementDefaultStyleEnabled() &&
options.hasStyle()) options.hasStyles())
definition->SetDefaultStyleSheet(*options.style()); definition->SetDefaultStyleSheets(options.styles());
definitions_.emplace_back(definition); definitions_.emplace_back(definition);
NameIdMap::AddResult result = name_id_map_.insert(descriptor.GetName(), id); NameIdMap::AddResult result = name_id_map_.insert(descriptor.GetName(), id);
CHECK(result.is_new_entry); CHECK(result.is_new_entry);
......
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