From 13733a789c05f7e55bd0edac9983f0844c6e9200 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 5 Dec 2024 11:52:43 +0000 Subject: [PATCH] LibWeb: Stop passing Realm unnecessarily to parse CSS properties Also use the parse_css_value() helper in cases where we previously constructed a Parser manually. --- Libraries/LibWeb/Animations/AnimationEffect.cpp | 8 +++----- Libraries/LibWeb/Animations/AnimationEffect.h | 2 +- Libraries/LibWeb/Animations/KeyframeEffect.cpp | 8 +++----- Libraries/LibWeb/CSS/CSS.cpp | 6 ++---- Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp | 2 +- Libraries/LibWeb/CSS/FontFace.cpp | 15 ++++----------- Libraries/LibWeb/CSS/FontFaceSet.cpp | 3 +-- Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp | 3 +-- .../LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h | 10 ++-------- .../LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h | 3 +-- .../LibWeb/HTML/CanvasRenderingContext2D.cpp | 6 +----- 11 files changed, 20 insertions(+), 46 deletions(-) diff --git a/Libraries/LibWeb/Animations/AnimationEffect.cpp b/Libraries/LibWeb/Animations/AnimationEffect.cpp index a5bd22a919e1..5af94e359854 100644 --- a/Libraries/LibWeb/Animations/AnimationEffect.cpp +++ b/Libraries/LibWeb/Animations/AnimationEffect.cpp @@ -155,7 +155,7 @@ WebIDL::ExceptionOr AnimationEffect::update_timing(OptionalEffectTiming ti // [CSS-EASING-1], throw a TypeError and abort this procedure. RefPtr easing_value; if (timing.easing.has_value()) { - easing_value = parse_easing_string(realm(), timing.easing.value()); + easing_value = parse_easing_string(timing.easing.value()); if (!easing_value) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid easing function"sv }; VERIFY(easing_value->is_easing()); @@ -589,11 +589,9 @@ Optional AnimationEffect::transformed_progress() const return m_timing_function.evaluate_at(directed_progress.value(), before_flag); } -RefPtr AnimationEffect::parse_easing_string(JS::Realm& realm, StringView value) +RefPtr AnimationEffect::parse_easing_string(StringView value) { - auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), value); - - if (auto style_value = parser.parse_as_css_value(CSS::PropertyID::AnimationTimingFunction)) { + if (auto style_value = parse_css_value(CSS::Parser::ParsingContext(), value, CSS::PropertyID::AnimationTimingFunction)) { if (style_value->is_easing()) return style_value; } diff --git a/Libraries/LibWeb/Animations/AnimationEffect.h b/Libraries/LibWeb/Animations/AnimationEffect.h index 9b6a2de42eff..11b6d2676aae 100644 --- a/Libraries/LibWeb/Animations/AnimationEffect.h +++ b/Libraries/LibWeb/Animations/AnimationEffect.h @@ -65,7 +65,7 @@ class AnimationEffect : public Bindings::PlatformObject { GC_DECLARE_ALLOCATOR(AnimationEffect); public: - static RefPtr parse_easing_string(JS::Realm& realm, StringView value); + static RefPtr parse_easing_string(StringView value); EffectTiming get_timing() const; ComputedEffectTiming get_computed_timing() const; diff --git a/Libraries/LibWeb/Animations/KeyframeEffect.cpp b/Libraries/LibWeb/Animations/KeyframeEffect.cpp index 4c166406506c..ad402b3771a3 100644 --- a/Libraries/LibWeb/Animations/KeyframeEffect.cpp +++ b/Libraries/LibWeb/Animations/KeyframeEffect.cpp @@ -542,9 +542,7 @@ static WebIDL::ExceptionOr> process_a_keyframes_argument(JS if (!property_id.has_value()) continue; - auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), value_string); - - if (auto style_value = parser.parse_as_css_value(*property_id)) { + if (auto style_value = parse_css_value(CSS::Parser::ParsingContext(), value_string, *property_id)) { // 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()) style_value = CSS::property_initial_value(*property_id); @@ -558,7 +556,7 @@ static WebIDL::ExceptionOr> process_a_keyframes_argument(JS // // If parsing the "easing" property fails, throw a TypeError and abort this procedure. auto easing_string = keyframe.easing.get(); - auto easing_value = AnimationEffect::parse_easing_string(realm, easing_string); + auto easing_value = AnimationEffect::parse_easing_string(easing_string); if (!easing_value) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Invalid animation easing value: \"{}\"", easing_string)) }; @@ -570,7 +568,7 @@ static WebIDL::ExceptionOr> process_a_keyframes_argument(JS // interface, and if any of the values fail to parse, throw a TypeError and abort this procedure. for (auto& unused_easing : unused_easings) { auto easing_string = unused_easing.get(); - auto easing_value = AnimationEffect::parse_easing_string(realm, easing_string); + auto easing_value = AnimationEffect::parse_easing_string(easing_string); if (!easing_value) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Invalid animation easing value: \"{}\"", easing_string)) }; } diff --git a/Libraries/LibWeb/CSS/CSS.cpp b/Libraries/LibWeb/CSS/CSS.cpp index 5acace9e7862..ef10d4353b57 100644 --- a/Libraries/LibWeb/CSS/CSS.cpp +++ b/Libraries/LibWeb/CSS/CSS.cpp @@ -22,14 +22,12 @@ WebIDL::ExceptionOr escape(JS::VM&, StringView identifier) } // https://www.w3.org/TR/css-conditional-3/#dom-css-supports -bool supports(JS::VM& vm, StringView property, StringView value) +bool supports(JS::VM&, StringView property, StringView value) { - auto& realm = *vm.current_realm(); - // 1. If property is an ASCII case-insensitive match for any defined CSS property that the UA supports, // and value successfully parses according to that property’s grammar, return true. if (auto property_id = property_id_from_string(property); property_id.has_value()) { - if (parse_css_value(Parser::ParsingContext { realm }, value, property_id.value())) + if (parse_css_value(Parser::ParsingContext {}, value, property_id.value())) return true; } diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index b7db2a6fe4be..544195d9445d 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -132,7 +132,7 @@ WebIDL::ExceptionOr PropertyOwningCSSStyleDeclaration::set_property(String // 5. Let component value list be the result of parsing value for property property. auto component_value_list = is(this) ? parse_css_value(CSS::Parser::ParsingContext { static_cast(*this).element()->document() }, value, property_id) - : parse_css_value(CSS::Parser::ParsingContext { realm() }, value, property_id); + : parse_css_value(CSS::Parser::ParsingContext {}, value, property_id); // 6. If component value list is null, then return. if (!component_value_list) diff --git a/Libraries/LibWeb/CSS/FontFace.cpp b/Libraries/LibWeb/CSS/FontFace.cpp index 67bddedd1f3f..891e336a6341 100644 --- a/Libraries/LibWeb/CSS/FontFace.cpp +++ b/Libraries/LibWeb/CSS/FontFace.cpp @@ -57,13 +57,6 @@ static NonnullRefPtr>> load_vector_fo GC_DEFINE_ALLOCATOR(FontFace); -template -RefPtr parse_property_string(JS::Realm& realm, StringView value) -{ - auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), value); - return parser.parse_as_css_value(PropertyID); -} - // https://drafts.csswg.org/css-font-loading/#font-face-constructor GC::Ref FontFace::construct_impl(JS::Realm& realm, String family, FontFaceSource source, FontFaceDescriptors const& descriptors) { @@ -213,7 +206,7 @@ GC::Ref FontFace::loaded() const // https://drafts.csswg.org/css-font-loading/#dom-fontface-family WebIDL::ExceptionOr FontFace::set_family(String const& string) { - auto property = parse_property_string(realm(), string); + auto property = parse_css_value(Parser::ParsingContext(), string, CSS::PropertyID::FontFamily); if (!property) return WebIDL::SyntaxError::create(realm(), "FontFace.family setter: Invalid font descriptor"_string); @@ -229,7 +222,7 @@ WebIDL::ExceptionOr FontFace::set_family(String const& string) // https://drafts.csswg.org/css-font-loading/#dom-fontface-style WebIDL::ExceptionOr FontFace::set_style(String const& string) { - auto property = parse_property_string(realm(), string); + auto property = parse_css_value(Parser::ParsingContext(), string, CSS::PropertyID::FontStyle); if (!property) return WebIDL::SyntaxError::create(realm(), "FontFace.style setter: Invalid font descriptor"_string); @@ -245,7 +238,7 @@ WebIDL::ExceptionOr FontFace::set_style(String const& string) // https://drafts.csswg.org/css-font-loading/#dom-fontface-weight WebIDL::ExceptionOr FontFace::set_weight(String const& string) { - auto property = parse_property_string(realm(), string); + auto property = parse_css_value(Parser::ParsingContext(), string, CSS::PropertyID::FontWeight); if (!property) return WebIDL::SyntaxError::create(realm(), "FontFace.weight setter: Invalid font descriptor"_string); @@ -262,7 +255,7 @@ WebIDL::ExceptionOr FontFace::set_weight(String const& string) WebIDL::ExceptionOr FontFace::set_stretch(String const& string) { // NOTE: font-stretch is now an alias for font-width - auto property = parse_property_string(realm(), string); + auto property = parse_css_value(Parser::ParsingContext(), string, CSS::PropertyID::FontWidth); if (!property) return WebIDL::SyntaxError::create(realm(), "FontFace.stretch setter: Invalid font descriptor"_string); diff --git a/Libraries/LibWeb/CSS/FontFaceSet.cpp b/Libraries/LibWeb/CSS/FontFaceSet.cpp index 254cb3dfa3d5..c1a7c1796b68 100644 --- a/Libraries/LibWeb/CSS/FontFaceSet.cpp +++ b/Libraries/LibWeb/CSS/FontFaceSet.cpp @@ -174,8 +174,7 @@ WebIDL::CallbackType* FontFaceSet::onloadingerror() static WebIDL::ExceptionOr> find_matching_font_faces(JS::Realm& realm, FontFaceSet& font_face_set, String const& font, String const&) { // 1. Parse font using the CSS value syntax of the font property. If a syntax error occurs, return a syntax error. - auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), font); - auto property = parser.parse_as_css_value(PropertyID::Font); + auto property = parse_css_value(CSS::Parser::ParsingContext(), font, PropertyID::Font); if (!property) return WebIDL::SyntaxError::create(realm, "Unable to parse font"_string); diff --git a/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp b/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp index f8cd279434c7..ac944796b664 100644 --- a/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp +++ b/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp @@ -946,8 +946,7 @@ WebIDL::ExceptionOr parse_dom_matrix_init_string(JS::Realm& realm, // 2. Parse transformList into parsedValue given the grammar for the CSS transform property. // The result will be a , the keyword none, or failure. // If parsedValue is failure, or any has values without absolute length units, or any keyword other than none is used, then return failure. [CSS3-SYNTAX] [CSS3-TRANSFORMS] - auto parsing_context = CSS::Parser::ParsingContext { realm }; - auto transform_style_value = parse_css_value(parsing_context, transform_list, CSS::PropertyID::Transform); + auto transform_style_value = parse_css_value(CSS::Parser::ParsingContext {}, transform_list, CSS::PropertyID::Transform); if (!transform_style_value) return WebIDL::SyntaxError::create(realm, "Failed to parse CSS transform string."_string); auto parsed_value = CSS::StyleProperties::transformations_for_style_value(*transform_style_value); diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h b/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h index b25788a7b56f..bfd426585efd 100644 --- a/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h +++ b/Libraries/LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h @@ -26,19 +26,16 @@ class CanvasFillStrokeStyles { void set_fill_style(FillOrStrokeStyleVariant style) { - auto& realm = static_cast(*this).realm(); - // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-fillstyle style.visit( // 1. If the given value is a string, then: [&](String const& string) { // 1. Let context be this's canvas attribute's value, if that is an element; otherwise null. - auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), string); // 2. Let parsedValue be the result of parsing the given value with context if non-null. // FIXME: Parse a color value // https://drafts.csswg.org/css-color/#parse-a-css-color-value - auto style_value = parser.parse_as_css_value(CSS::PropertyID::Color); + auto style_value = parse_css_value(CSS::Parser::ParsingContext(), string, CSS::PropertyID::Color); if (style_value && style_value->has_color()) { auto parsedValue = style_value->to_color(OptionalNone()); @@ -67,20 +64,17 @@ class CanvasFillStrokeStyles { void set_stroke_style(FillOrStrokeStyleVariant style) { - auto& realm = static_cast(*this).realm(); - // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-strokestyle style.visit( // 1. If the given value is a string, then: [&](String const& string) { // 1. Let context be this's canvas attribute's value, if that is an element; otherwise null. - auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), string); // 2. Let parsedValue be the result of parsing the given value with context if non-null. // FIXME: Parse a color value // https://drafts.csswg.org/css-color/#parse-a-css-color-value - auto style_value = parser.parse_as_css_value(CSS::PropertyID::Color); + auto style_value = parse_css_value(CSS::Parser::ParsingContext(), string, CSS::PropertyID::Color); if (style_value && style_value->has_color()) { auto parsedValue = style_value->to_color(OptionalNone()); diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h b/Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h index d3909d8c4b85..b9e7d039cac3 100644 --- a/Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h +++ b/Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h @@ -43,8 +43,7 @@ class CanvasTextDrawingStyles { // and with system fonts being computed to explicit values. // FIXME: with the 'line-height' component forced to 'normal' // FIXME: with the 'font-size' component converted to CSS pixels - auto parsing_context = CSS::Parser::ParsingContext { reinterpret_cast(*this).realm() }; - auto font_style_value_result = parse_css_value(parsing_context, font, CSS::PropertyID::Font); + auto font_style_value_result = parse_css_value(CSS::Parser::ParsingContext {}, font, CSS::PropertyID::Font); // If the new value is syntactically incorrect (including using property-independent style sheet syntax like 'inherit' or 'initial'), then it must be ignored, without assigning a new font value. // NOTE: ShorthandStyleValue should be the only valid option here. We implicitly VERIFY this below. diff --git a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 2650f675bf16..85753d699329 100644 --- a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -790,14 +790,10 @@ String CanvasRenderingContext2D::shadow_color() const void CanvasRenderingContext2D::set_shadow_color(String color) { - auto& realm = static_cast(*this).realm(); - // 1. Let context be this's canvas attribute's value, if that is an element; otherwise null. - auto parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), color); - - auto style_value = parser.parse_as_css_value(CSS::PropertyID::Color); // 2. Let parsedValue be the result of parsing the given value with context if non-null. + auto style_value = parse_css_value(CSS::Parser::ParsingContext(), color, CSS::PropertyID::Color); if (style_value && style_value->has_color()) { auto parsedValue = style_value->to_color(OptionalNone());