[css-pseudo] Unify the default styles for ::marker

This patch avoids triplicating the default styles for the ::marker
pseudo-element in StyleResolver, LayoutListMarker and LayoutNGListItem.
Now they will only be in StyleResolver.

When no style has been specified for ::marker, using StyleResolver has
some overhead that reduces the the flexbox_with_list_item.html perf test
by 2%-5%. But unifying the code for ::marker styles seems worth it,
since it will be necessary to avoid some inconsistences like the
propagation of text decorations.

BUG=457718

Change-Id: I9b30d43321f8076873256e20f97779bc279ec940
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1971929
Reviewed-by: Rune Lillesveen <futhark@chromium.org>
Commit-Queue: Oriol Brufau <obrufau@igalia.com>
Cr-Commit-Position: refs/heads/master@{#725913}
diff --git a/third_party/blink/renderer/core/css/resolver/style_resolver.cc b/third_party/blink/renderer/core/css/resolver/style_resolver.cc
index b2a4151..ce5c4dc7 100644
--- a/third_party/blink/renderer/core/css/resolver/style_resolver.cc
+++ b/third_party/blink/renderer/core/css/resolver/style_resolver.cc
@@ -981,9 +981,18 @@
     // but that would use a slow universal element selector. So instead we apply
     // the styles here as an optimization.
     if (pseudo_style_request.pseudo_id == kPseudoIdMarker) {
+      // Set 'unicode-bidi: isolate'
       state.Style()->SetUnicodeBidi(UnicodeBidi::kIsolate);
-      state.Style()->SetFontVariantNumericSpacing(
-          FontVariantNumeric::kTabularNums);
+
+      // Set 'font-variant-numeric: tabular-nums'
+      FontVariantNumeric variant_numeric;
+      variant_numeric.SetNumericSpacing(FontVariantNumeric::kTabularNums);
+      state.GetFontBuilder().SetVariantNumeric(variant_numeric);
+      UpdateFont(state);
+
+      // Don't bother matching rules if there is no style for ::marker
+      if (!state.ParentStyle()->HasPseudoElementStyle(kPseudoIdMarker))
+        return true;
     }
 
     MatchUARules(collector);
diff --git a/third_party/blink/renderer/core/layout/layout_list_marker.cc b/third_party/blink/renderer/core/layout/layout_list_marker.cc
index fd203ff..0da65289 100644
--- a/third_party/blink/renderer/core/layout/layout_list_marker.cc
+++ b/third_party/blink/renderer/core/layout/layout_list_marker.cc
@@ -519,24 +519,13 @@
 }
 
 void LayoutListMarker::ListItemStyleDidChange() {
-  Node* list_item = list_item_->GetNode();
+  Element* list_item = To<Element>(list_item_->GetNode());
   const ComputedStyle* cached_marker_style =
-      list_item->IsPseudoElement()
-          ? nullptr
-          : To<Element>(list_item)->CachedStyleForPseudoElement(
-                kPseudoIdMarker);
-  scoped_refptr<ComputedStyle> new_style;
-  if (cached_marker_style) {
-    new_style = ComputedStyle::Clone(*cached_marker_style);
-  } else {
-    // The marker always inherits from the list item, regardless of where it
-    // might end up (e.g., in some deeply nested line box). See CSS3 spec.
-    new_style = ComputedStyle::Create();
-    new_style->InheritFrom(list_item_->StyleRef());
-    new_style->SetStyleType(kPseudoIdMarker);
-    new_style->SetUnicodeBidi(UnicodeBidi::kIsolate);
-    new_style->SetFontVariantNumericSpacing(FontVariantNumeric::kTabularNums);
-  }
+      list_item->CachedStyleForPseudoElement(kPseudoIdMarker);
+  scoped_refptr<ComputedStyle> new_style =
+      cached_marker_style ? ComputedStyle::Clone(*cached_marker_style)
+                          : list_item->StyleForPseudoElement(kPseudoIdMarker);
+  DCHECK(new_style);
   if (Style()) {
     // Reuse the current margins. Otherwise resetting the margins to initial
     // values would trigger unnecessary layout.
diff --git a/third_party/blink/renderer/core/layout/ng/list/layout_ng_list_item.cc b/third_party/blink/renderer/core/layout/ng/list/layout_ng_list_item.cc
index 56d8a6a..e543dff 100644
--- a/third_party/blink/renderer/core/layout/ng/list/layout_ng_list_item.cc
+++ b/third_party/blink/renderer/core/layout/ng/list/layout_ng_list_item.cc
@@ -147,12 +147,9 @@
   }
 
   // Create a marker box if it does not exist yet.
-  Node* list_item = GetNode();
+  Element* list_item = To<Element>(GetNode());
   const ComputedStyle* cached_marker_style =
-      list_item->IsPseudoElement()
-          ? nullptr
-          : To<Element>(list_item)->CachedStyleForPseudoElement(
-                kPseudoIdMarker);
+      list_item->CachedStyleForPseudoElement(kPseudoIdMarker);
   if (cached_marker_style && cached_marker_style->GetContentData()) {
     // Don't create an anonymous layout for the marker, it will be generated
     // by the ::marker pseudo-element.
@@ -161,17 +158,10 @@
     is_marker_text_updated_ = true;
     return;
   }
-  scoped_refptr<ComputedStyle> marker_style;
-  if (cached_marker_style) {
-    marker_style = ComputedStyle::Clone(*cached_marker_style);
-  } else {
-    marker_style = ComputedStyle::Create();
-    marker_style->InheritFrom(style);
-    marker_style->SetStyleType(kPseudoIdMarker);
-    marker_style->SetUnicodeBidi(UnicodeBidi::kIsolate);
-    marker_style->SetFontVariantNumericSpacing(
-        FontVariantNumeric::kTabularNums);
-  }
+  scoped_refptr<ComputedStyle> marker_style =
+      cached_marker_style ? ComputedStyle::Clone(*cached_marker_style)
+                          : list_item->StyleForPseudoElement(kPseudoIdMarker);
+  DCHECK(marker_style);
   if (IsInside()) {
     if (marker_ && !marker_->IsLayoutInline())
       DestroyMarker();
diff --git a/third_party/blink/renderer/core/style/computed_style.cc b/third_party/blink/renderer/core/style/computed_style.cc
index 7f502c6..5552ab05 100644
--- a/third_party/blink/renderer/core/style/computed_style.cc
+++ b/third_party/blink/renderer/core/style/computed_style.cc
@@ -1967,17 +1967,6 @@
   GetFont().Update(current_font_selector);
 }
 
-void ComputedStyle::SetFontVariantNumericSpacing(
-    FontVariantNumeric::NumericSpacing numeric_spacing) {
-  FontSelector* current_font_selector = GetFont().GetFontSelector();
-  FontDescription desc(GetFontDescription());
-  FontVariantNumeric variant_numeric = desc.VariantNumeric();
-  variant_numeric.SetNumericSpacing(numeric_spacing);
-  desc.SetVariantNumeric(variant_numeric);
-  SetFontDescription(desc);
-  GetFont().Update(current_font_selector);
-}
-
 void ComputedStyle::SetTextAutosizingMultiplier(float multiplier) {
   SetTextAutosizingMultiplierInternal(multiplier);
 
diff --git a/third_party/blink/renderer/core/style/computed_style.h b/third_party/blink/renderer/core/style/computed_style.h
index e747fb55..5156dec 100644
--- a/third_party/blink/renderer/core/style/computed_style.h
+++ b/third_party/blink/renderer/core/style/computed_style.h
@@ -1087,9 +1087,6 @@
   float WordSpacing() const { return GetFontDescription().WordSpacing(); }
   void SetWordSpacing(float);
 
-  // font-variant-numeric spacing
-  void SetFontVariantNumericSpacing(FontVariantNumeric::NumericSpacing);
-
   // orphans
   void SetOrphans(int16_t o) { SetOrphansInternal(clampTo<int16_t>(o, 1)); }