瀏覽代碼

LibWeb: Remove Realm parameter from property_initial_value()

We don't need the Realm to parse a style value.

Fixes #2720
Sam Atkins 8 月之前
父節點
當前提交
2c3c821305

+ 15 - 15
Documentation/CSSGeneratedFiles.md

@@ -19,21 +19,21 @@ Each property will have some set of these fields on it:
 
 
 (Note that required fields are not required on properties with `legacy-alias-for` or `logical-alias-for` set.)
 (Note that required fields are not required on properties with `legacy-alias-for` or `logical-alias-for` set.)
 
 
-| Field                      | Required | Default | Description                                                                                                                               | Generated functions                                                           |
-|----------------------------|----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|
-| `affects-layout`           | No       | `true`  | Boolean. Whether changing this property will invalidate the element's layout.                                                             | `bool property_affects_layout(PropertyID)`                                    |
-| `affects-stacking-context` | No       | `false` | Boolean. Whether this property can cause a new stacking context for the element.                                                          | `bool property_affects_stacking_context(PropertyID)`                          |
-| `animation-type`           | Yes      |         | String. How the property should be animated. Defined by the spec. See below.                                                              | `AnimationType animation_type_from_longhand_property(PropertyID)`             |
-| `inherited`                | Yes      |         | Boolean. Whether the property is inherited by its child elements.                                                                         | `bool is_inherited_property(PropertyID)`                                      |
-| `initial`                  | Yes      |         | String. The property's initial value if it is not specified.                                                                              | `NonnullRefPtr<CSSStyleValue> property_initial_value(JS::Realm&, PropertyID)` |
-| `legacy-alias-for`         | No       | Nothing | String. The name of a property this is an alias for. See below.                                                                           |                                                                               |
-| `logical-alias-for`        | No       | Nothing | Array of strings. The name of a property this is an alias for. See below.                                                                 |                                                                               |
-| `longhands`                | No       | `[]`    | Array of strings. If this is a shorthand, these are the property names that it expands out into.                                          | `Vector<PropertyID> longhands_for_shorthand(PropertyID)`                      |
-| `max-values`               | No       | `1`     | Integer. How many values can be parsed for this property. eg, `margin` can have up to 4 values.                                           | `size_t property_maximum_value_count(PropertyID)`                             |
-| `percentages-resolve-to`   | No       | Nothing | String. What type percentages get resolved to. eg, for `width` percentages are resolved to `length` values.                               | `Optional<ValueType> property_resolves_percentages_relative_to(PropertyID)`   |
-| `quirks`                   | No       | `[]`    | Array of strings. Some properties have special behavior in "quirks mode", which are listed here. See below.                               | `bool property_has_quirk(PropertyID, Quirk)`                                  |
-| `valid-identifiers`        | No       | `[]`    | Array of strings. Which keywords the property accepts. Consider defining an enum instead and putting its name in the `valid-types` array. | `bool property_accepts_keyword(PropertyID, Keyword)`                          |
-| `valid-types`              | No       | `[]`    | Array of strings. Which value types the property accepts. See below.                                                                      | `bool property_accepts_type(PropertyID, ValueType)`                           |
+| Field                      | Required | Default | Description                                                                                                                               | Generated functions                                                         |
+|----------------------------|----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
+| `affects-layout`           | No       | `true`  | Boolean. Whether changing this property will invalidate the element's layout.                                                             | `bool property_affects_layout(PropertyID)`                                  |
+| `affects-stacking-context` | No       | `false` | Boolean. Whether this property can cause a new stacking context for the element.                                                          | `bool property_affects_stacking_context(PropertyID)`                        |
+| `animation-type`           | Yes      |         | String. How the property should be animated. Defined by the spec. See below.                                                              | `AnimationType animation_type_from_longhand_property(PropertyID)`           |
+| `inherited`                | Yes      |         | Boolean. Whether the property is inherited by its child elements.                                                                         | `bool is_inherited_property(PropertyID)`                                    |
+| `initial`                  | Yes      |         | String. The property's initial value if it is not specified.                                                                              | `NonnullRefPtr<CSSStyleValue> property_initial_value(PropertyID)`           |
+| `legacy-alias-for`         | No       | Nothing | String. The name of a property this is an alias for. See below.                                                                           |                                                                             |
+| `logical-alias-for`        | No       | Nothing | Array of strings. The name of a property this is an alias for. See below.                                                                 |                                                                             |
+| `longhands`                | No       | `[]`    | Array of strings. If this is a shorthand, these are the property names that it expands out into.                                          | `Vector<PropertyID> longhands_for_shorthand(PropertyID)`                    |
+| `max-values`               | No       | `1`     | Integer. How many values can be parsed for this property. eg, `margin` can have up to 4 values.                                           | `size_t property_maximum_value_count(PropertyID)`                           |
+| `percentages-resolve-to`   | No       | Nothing | String. What type percentages get resolved to. eg, for `width` percentages are resolved to `length` values.                               | `Optional<ValueType> property_resolves_percentages_relative_to(PropertyID)` |
+| `quirks`                   | No       | `[]`    | Array of strings. Some properties have special behavior in "quirks mode", which are listed here. See below.                               | `bool property_has_quirk(PropertyID, Quirk)`                                |
+| `valid-identifiers`        | No       | `[]`    | Array of strings. Which keywords the property accepts. Consider defining an enum instead and putting its name in the `valid-types` array. | `bool property_accepts_keyword(PropertyID, Keyword)`                        |
+| `valid-types`              | No       | `[]`    | Array of strings. Which value types the property accepts. See below.                                                                      | `bool property_accepts_type(PropertyID, ValueType)`                         |
 
 
 ### `animation-type`
 ### `animation-type`
 
 

+ 1 - 1
Libraries/LibWeb/Animations/KeyframeEffect.cpp

@@ -547,7 +547,7 @@ static WebIDL::ExceptionOr<Vector<BaseKeyframe>> process_a_keyframes_argument(JS
             if (auto style_value = parser.parse_as_css_value(*property_id)) {
             if (auto style_value = parser.parse_as_css_value(*property_id)) {
                 // Handle 'initial' here so we don't have to get the default value of the property every frame in StyleComputer
                 // Handle 'initial' here so we don't have to get the default value of the property every frame in StyleComputer
                 if (style_value->is_initial())
                 if (style_value->is_initial())
-                    style_value = CSS::property_initial_value(realm, *property_id);
+                    style_value = CSS::property_initial_value(*property_id);
                 parsed_properties.set(*property_id, *style_value);
                 parsed_properties.set(*property_id, *style_value);
             }
             }
         }
         }

+ 1 - 1
Libraries/LibWeb/CSS/Interpolation.cpp

@@ -46,7 +46,7 @@ static NonnullRefPtr<CSSStyleValue const> with_keyword_values_resolved(DOM::Elem
     switch (value.as_keyword().keyword()) {
     switch (value.as_keyword().keyword()) {
     case CSS::Keyword::Initial:
     case CSS::Keyword::Initial:
     case CSS::Keyword::Unset:
     case CSS::Keyword::Unset:
-        return property_initial_value(element.realm(), property_id);
+        return property_initial_value(property_id);
     case CSS::Keyword::Inherit:
     case CSS::Keyword::Inherit:
         return CSS::StyleComputer::get_inherit_value(element.realm(), property_id, &element);
         return CSS::StyleComputer::get_inherit_value(element.realm(), property_id, &element);
     default:
     default:

+ 30 - 30
Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -4306,14 +4306,14 @@ RefPtr<CSSStyleValue> Parser::parse_background_value(TokenStream<ComponentValue>
     StyleValueVector background_origins;
     StyleValueVector background_origins;
     RefPtr<CSSStyleValue> background_color;
     RefPtr<CSSStyleValue> background_color;
 
 
-    auto initial_background_image = property_initial_value(m_context.realm(), PropertyID::BackgroundImage);
-    auto initial_background_position = property_initial_value(m_context.realm(), PropertyID::BackgroundPosition);
-    auto initial_background_size = property_initial_value(m_context.realm(), PropertyID::BackgroundSize);
-    auto initial_background_repeat = property_initial_value(m_context.realm(), PropertyID::BackgroundRepeat);
-    auto initial_background_attachment = property_initial_value(m_context.realm(), PropertyID::BackgroundAttachment);
-    auto initial_background_clip = property_initial_value(m_context.realm(), PropertyID::BackgroundClip);
-    auto initial_background_origin = property_initial_value(m_context.realm(), PropertyID::BackgroundOrigin);
-    auto initial_background_color = property_initial_value(m_context.realm(), PropertyID::BackgroundColor);
+    auto initial_background_image = property_initial_value(PropertyID::BackgroundImage);
+    auto initial_background_position = property_initial_value(PropertyID::BackgroundPosition);
+    auto initial_background_size = property_initial_value(PropertyID::BackgroundSize);
+    auto initial_background_repeat = property_initial_value(PropertyID::BackgroundRepeat);
+    auto initial_background_attachment = property_initial_value(PropertyID::BackgroundAttachment);
+    auto initial_background_clip = property_initial_value(PropertyID::BackgroundClip);
+    auto initial_background_origin = property_initial_value(PropertyID::BackgroundOrigin);
+    auto initial_background_color = property_initial_value(PropertyID::BackgroundColor);
 
 
     // Per-layer values
     // Per-layer values
     RefPtr<CSSStyleValue> background_image;
     RefPtr<CSSStyleValue> background_image;
@@ -4750,11 +4750,11 @@ RefPtr<CSSStyleValue> Parser::parse_border_value(PropertyID property_id, TokenSt
     }
     }
 
 
     if (!border_width)
     if (!border_width)
-        border_width = property_initial_value(m_context.realm(), width_property);
+        border_width = property_initial_value(width_property);
     if (!border_style)
     if (!border_style)
-        border_style = property_initial_value(m_context.realm(), style_property);
+        border_style = property_initial_value(style_property);
     if (!border_color)
     if (!border_color)
-        border_color = property_initial_value(m_context.realm(), color_property);
+        border_color = property_initial_value(color_property);
 
 
     transaction.commit();
     transaction.commit();
     return ShorthandStyleValue::create(property_id,
     return ShorthandStyleValue::create(property_id,
@@ -4930,9 +4930,9 @@ RefPtr<CSSStyleValue> Parser::parse_columns_value(TokenStream<ComponentValue>& t
     }
     }
 
 
     if (!column_count)
     if (!column_count)
-        column_count = property_initial_value(m_context.realm(), PropertyID::ColumnCount);
+        column_count = property_initial_value(PropertyID::ColumnCount);
     if (!column_width)
     if (!column_width)
-        column_width = property_initial_value(m_context.realm(), PropertyID::ColumnWidth);
+        column_width = property_initial_value(PropertyID::ColumnWidth);
 
 
     transaction.commit();
     transaction.commit();
     return ShorthandStyleValue::create(PropertyID::Columns,
     return ShorthandStyleValue::create(PropertyID::Columns,
@@ -5650,9 +5650,9 @@ RefPtr<CSSStyleValue> Parser::parse_flex_shorthand_value(TokenStream<ComponentVa
     }
     }
 
 
     if (!flex_grow)
     if (!flex_grow)
-        flex_grow = property_initial_value(m_context.realm(), PropertyID::FlexGrow);
+        flex_grow = property_initial_value(PropertyID::FlexGrow);
     if (!flex_shrink)
     if (!flex_shrink)
-        flex_shrink = property_initial_value(m_context.realm(), PropertyID::FlexShrink);
+        flex_shrink = property_initial_value(PropertyID::FlexShrink);
     if (!flex_basis) {
     if (!flex_basis) {
         // NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%.
         // NOTE: The spec says that flex-basis should be 0 here, but other engines currently use 0%.
         // https://github.com/w3c/csswg-drafts/issues/5742
         // https://github.com/w3c/csswg-drafts/issues/5742
@@ -5692,9 +5692,9 @@ RefPtr<CSSStyleValue> Parser::parse_flex_flow_value(TokenStream<ComponentValue>&
     }
     }
 
 
     if (!flex_direction)
     if (!flex_direction)
-        flex_direction = property_initial_value(m_context.realm(), PropertyID::FlexDirection);
+        flex_direction = property_initial_value(PropertyID::FlexDirection);
     if (!flex_wrap)
     if (!flex_wrap)
-        flex_wrap = property_initial_value(m_context.realm(), PropertyID::FlexWrap);
+        flex_wrap = property_initial_value(PropertyID::FlexWrap);
 
 
     transaction.commit();
     transaction.commit();
     return ShorthandStyleValue::create(PropertyID::FlexFlow,
     return ShorthandStyleValue::create(PropertyID::FlexFlow,
@@ -5815,15 +5815,15 @@ RefPtr<CSSStyleValue> Parser::parse_font_value(TokenStream<ComponentValue>& toke
         return nullptr;
         return nullptr;
 
 
     if (!font_style)
     if (!font_style)
-        font_style = property_initial_value(m_context.realm(), PropertyID::FontStyle);
+        font_style = property_initial_value(PropertyID::FontStyle);
     if (!font_variant)
     if (!font_variant)
-        font_variant = property_initial_value(m_context.realm(), PropertyID::FontVariant);
+        font_variant = property_initial_value(PropertyID::FontVariant);
     if (!font_weight)
     if (!font_weight)
-        font_weight = property_initial_value(m_context.realm(), PropertyID::FontWeight);
+        font_weight = property_initial_value(PropertyID::FontWeight);
     if (!font_width)
     if (!font_width)
-        font_width = property_initial_value(m_context.realm(), PropertyID::FontWidth);
+        font_width = property_initial_value(PropertyID::FontWidth);
     if (!line_height)
     if (!line_height)
-        line_height = property_initial_value(m_context.realm(), PropertyID::LineHeight);
+        line_height = property_initial_value(PropertyID::LineHeight);
 
 
     transaction.commit();
     transaction.commit();
     return ShorthandStyleValue::create(PropertyID::Font,
     return ShorthandStyleValue::create(PropertyID::Font,
@@ -6229,11 +6229,11 @@ RefPtr<CSSStyleValue> Parser::parse_list_style_value(TokenStream<ComponentValue>
     }
     }
 
 
     if (!list_position)
     if (!list_position)
-        list_position = property_initial_value(m_context.realm(), PropertyID::ListStylePosition);
+        list_position = property_initial_value(PropertyID::ListStylePosition);
     if (!list_image)
     if (!list_image)
-        list_image = property_initial_value(m_context.realm(), PropertyID::ListStyleImage);
+        list_image = property_initial_value(PropertyID::ListStyleImage);
     if (!list_type)
     if (!list_type)
-        list_type = property_initial_value(m_context.realm(), PropertyID::ListStyleType);
+        list_type = property_initial_value(PropertyID::ListStyleType);
 
 
     transaction.commit();
     transaction.commit();
     return ShorthandStyleValue::create(PropertyID::ListStyle,
     return ShorthandStyleValue::create(PropertyID::ListStyle,
@@ -6463,13 +6463,13 @@ RefPtr<CSSStyleValue> Parser::parse_text_decoration_value(TokenStream<ComponentV
     }
     }
 
 
     if (!decoration_line)
     if (!decoration_line)
-        decoration_line = property_initial_value(m_context.realm(), PropertyID::TextDecorationLine);
+        decoration_line = property_initial_value(PropertyID::TextDecorationLine);
     if (!decoration_thickness)
     if (!decoration_thickness)
-        decoration_thickness = property_initial_value(m_context.realm(), PropertyID::TextDecorationThickness);
+        decoration_thickness = property_initial_value(PropertyID::TextDecorationThickness);
     if (!decoration_style)
     if (!decoration_style)
-        decoration_style = property_initial_value(m_context.realm(), PropertyID::TextDecorationStyle);
+        decoration_style = property_initial_value(PropertyID::TextDecorationStyle);
     if (!decoration_color)
     if (!decoration_color)
-        decoration_color = property_initial_value(m_context.realm(), PropertyID::TextDecorationColor);
+        decoration_color = property_initial_value(PropertyID::TextDecorationColor);
 
 
     transaction.commit();
     transaction.commit();
     return ShorthandStyleValue::create(PropertyID::TextDecoration,
     return ShorthandStyleValue::create(PropertyID::TextDecoration,
@@ -8239,7 +8239,7 @@ Parser::ParseErrorOr<NonnullRefPtr<CSSStyleValue>> Parser::parse_css_value(Prope
     }
     }
 
 
     for (auto& property : unassigned_properties)
     for (auto& property : unassigned_properties)
-        assigned_values.ensure(to_underlying(property)).append(property_initial_value(m_context.realm(), property));
+        assigned_values.ensure(to_underlying(property)).append(property_initial_value(property));
 
 
     stream.discard_whitespace();
     stream.discard_whitespace();
     if (stream.has_next_token())
     if (stream.has_next_token())

+ 7 - 7
Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -934,7 +934,7 @@ void StyleComputer::set_all_properties(DOM::Element& element, Optional<CSS::Sele
             } else {
             } else {
                 style.set_property(
                 style.set_property(
                     property_id,
                     property_id,
-                    property_initial_value(document.realm(), property_id),
+                    property_initial_value(property_id),
                     StyleProperties::Inherited::No,
                     StyleProperties::Inherited::No,
                     important);
                     important);
             }
             }
@@ -1103,7 +1103,7 @@ void StyleComputer::collect_animation_into(DOM::Element& element, Optional<CSS::
         auto resolved_end_property = resolve_property(end_property.value());
         auto resolved_end_property = resolve_property(end_property.value());
 
 
         if (resolved_end_property && !resolved_start_property)
         if (resolved_end_property && !resolved_start_property)
-            resolved_start_property = CSS::property_initial_value(document().realm(), it.key);
+            resolved_start_property = property_initial_value(it.key);
 
 
         if (!resolved_start_property || !resolved_end_property)
         if (!resolved_start_property || !resolved_end_property)
             continue;
             continue;
@@ -1670,12 +1670,12 @@ DOM::Element const* element_to_inherit_style_from(DOM::Element const* element, O
     return parent_element;
     return parent_element;
 }
 }
 
 
-NonnullRefPtr<CSSStyleValue const> StyleComputer::get_inherit_value(JS::Realm& initial_value_context_realm, CSS::PropertyID property_id, DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element)
+NonnullRefPtr<CSSStyleValue const> StyleComputer::get_inherit_value([[maybe_unused]] JS::Realm& initial_value_context_realm, CSS::PropertyID property_id, DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element)
 {
 {
     auto* parent_element = element_to_inherit_style_from(element, pseudo_element);
     auto* parent_element = element_to_inherit_style_from(element, pseudo_element);
 
 
     if (!parent_element || !parent_element->computed_css_values().has_value())
     if (!parent_element || !parent_element->computed_css_values().has_value())
-        return property_initial_value(initial_value_context_realm, property_id);
+        return property_initial_value(property_id);
     return parent_element->computed_css_values()->property(property_id);
     return parent_element->computed_css_values()->property(property_id);
 }
 }
 
 
@@ -1692,13 +1692,13 @@ void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM
                 StyleProperties::Inherited::Yes,
                 StyleProperties::Inherited::Yes,
                 Important::No);
                 Important::No);
         } else {
         } else {
-            style.set_property(property_id, property_initial_value(document().realm(), property_id));
+            style.set_property(property_id, property_initial_value(property_id));
         }
         }
         return;
         return;
     }
     }
 
 
     if (value_slot->is_initial()) {
     if (value_slot->is_initial()) {
-        value_slot = property_initial_value(document().realm(), property_id);
+        value_slot = property_initial_value(property_id);
         return;
         return;
     }
     }
 
 
@@ -1717,7 +1717,7 @@ void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM
             style.set_property_inherited(property_id, StyleProperties::Inherited::Yes);
             style.set_property_inherited(property_id, StyleProperties::Inherited::Yes);
         } else {
         } else {
             // and if it is not, this is treated as initial.
             // and if it is not, this is treated as initial.
-            value_slot = property_initial_value(document().realm(), property_id);
+            value_slot = property_initial_value(property_id);
         }
         }
     }
     }
 }
 }

+ 1 - 1
Libraries/LibWeb/CSS/StyleValues/ShorthandStyleValue.cpp

@@ -239,7 +239,7 @@ String ShorthandStyleValue::to_string() const
         StringBuilder builder;
         StringBuilder builder;
         auto append_if_non_default = [&](PropertyID property_id) {
         auto append_if_non_default = [&](PropertyID property_id) {
             auto value = longhand(property_id);
             auto value = longhand(property_id);
-            if (!value->equals(property_initial_value({}, property_id))) {
+            if (!value->equals(property_initial_value(property_id))) {
                 if (!builder.is_empty())
                 if (!builder.is_empty())
                     builder.append(' ');
                     builder.append(' ');
                 builder.append(value->to_string());
                 builder.append(value->to_string());

+ 3 - 6
Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp

@@ -219,7 +219,7 @@ Optional<PropertyID> property_id_from_string(StringView);
 [[nodiscard]] FlyString const& string_from_property_id(PropertyID);
 [[nodiscard]] FlyString const& string_from_property_id(PropertyID);
 [[nodiscard]] FlyString const& camel_case_string_from_property_id(PropertyID);
 [[nodiscard]] FlyString const& camel_case_string_from_property_id(PropertyID);
 bool is_inherited_property(PropertyID);
 bool is_inherited_property(PropertyID);
-NonnullRefPtr<CSSStyleValue> property_initial_value(Optional<JS::Realm&>, PropertyID);
+NonnullRefPtr<CSSStyleValue> property_initial_value(PropertyID);
 
 
 enum class ValueType {
 enum class ValueType {
     Angle,
     Angle,
@@ -662,20 +662,17 @@ bool property_affects_stacking_context(PropertyID property_id)
     }
     }
 }
 }
 
 
-NonnullRefPtr<CSSStyleValue> property_initial_value(Optional<JS::Realm&> context_realm, PropertyID property_id)
+NonnullRefPtr<CSSStyleValue> property_initial_value(PropertyID property_id)
 {
 {
     static Array<RefPtr<CSSStyleValue>, to_underlying(last_property_id) + 1> initial_values;
     static Array<RefPtr<CSSStyleValue>, to_underlying(last_property_id) + 1> initial_values;
     if (auto initial_value = initial_values[to_underlying(property_id)])
     if (auto initial_value = initial_values[to_underlying(property_id)])
         return initial_value.release_nonnull();
         return initial_value.release_nonnull();
 
 
-    // We need a Realm to parse any new values.
-    VERIFY(context_realm.has_value());
-
     // Lazily parse initial values as needed.
     // Lazily parse initial values as needed.
     // This ensures the shorthands will always be able to get the initial values of their longhands.
     // This ensures the shorthands will always be able to get the initial values of their longhands.
     // This also now allows a longhand have its own longhand (like background-position-x).
     // This also now allows a longhand have its own longhand (like background-position-x).
 
 
-    Parser::ParsingContext parsing_context(context_realm.value());
+    Parser::ParsingContext parsing_context;
     switch (property_id) {
     switch (property_id) {
 )~~~");
 )~~~");