From 439735fd35e06f707351ee6549d4d2d5c82bbb26 Mon Sep 17 00:00:00 2001 From: MacDue Date: Sun, 30 Jul 2023 13:50:16 +0100 Subject: [PATCH] LibWeb: Support legacy xlink:href attribute for linked gradients ...and rename the related functions to something more descriptive. --- .../LibWeb/SVG/SVGGradientElement.cpp | 14 ++++++----- .../Libraries/LibWeb/SVG/SVGGradientElement.h | 6 ++--- .../LibWeb/SVG/SVGLinearGradientElement.cpp | 16 ++++++------- .../LibWeb/SVG/SVGLinearGradientElement.h | 6 ++--- .../LibWeb/SVG/SVGRadialGradientElement.cpp | 24 +++++++++---------- .../LibWeb/SVG/SVGRadialGradientElement.h | 6 ++--- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/Userland/Libraries/LibWeb/SVG/SVGGradientElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGGradientElement.cpp index f0415385b7..e942f8f6e9 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGGradientElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGGradientElement.cpp @@ -35,8 +35,8 @@ GradientUnits SVGGradientElement::gradient_units() const { if (m_gradient_units.has_value()) return *m_gradient_units; - if (auto href = xlink_href()) - return href->gradient_units(); + if (auto gradient = linked_gradient()) + return gradient->gradient_units(); return GradientUnits::ObjectBoundingBox; } @@ -44,8 +44,8 @@ Optional SVGGradientElement::gradient_transform() const { if (m_gradient_transform.has_value()) return m_gradient_transform; - if (auto href = xlink_href()) - return href->gradient_transform(); + if (auto gradient = linked_gradient()) + return gradient->gradient_transform(); return {}; } @@ -78,11 +78,13 @@ void SVGGradientElement::add_color_stops(Gfx::SVGGradientPaintStyle& paint_style }); } -JS::GCPtr SVGGradientElement::xlink_href() const +JS::GCPtr SVGGradientElement::linked_gradient() const { // FIXME: This entire function is an ad-hoc hack! // It can only resolve # in the same document. - if (auto href = get_attribute("href"); !href.is_empty()) { + + auto link = has_attribute("href") ? get_attribute("href") : get_attribute("xlink:href"); + if (auto href = link; !href.is_empty()) { auto url = document().parse_url(href); auto id = url.fragment(); if (id.is_empty()) diff --git a/Userland/Libraries/LibWeb/SVG/SVGGradientElement.h b/Userland/Libraries/LibWeb/SVG/SVGGradientElement.h index 04ce1a697a..5472523a49 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGGradientElement.h +++ b/Userland/Libraries/LibWeb/SVG/SVGGradientElement.h @@ -39,7 +39,7 @@ protected: virtual JS::ThrowCompletionOr initialize(JS::Realm&) override; - JS::GCPtr xlink_href() const; + JS::GCPtr linked_gradient() const; Gfx::AffineTransform gradient_paint_transform(SVGPaintContext const&) const; @@ -52,8 +52,8 @@ protected: callback(stop); }); if (!color_stops_found) { - if (auto href = xlink_href()) - href->for_each_color_stop(callback); + if (auto gradient = linked_gradient()) + gradient->for_each_color_stop(callback); } } diff --git a/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.cpp index 8a70d0c5b9..a17ae73bb3 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.cpp @@ -51,8 +51,8 @@ NumberPercentage SVGLinearGradientElement::start_x() const { if (m_x1.has_value()) return *m_x1; - if (auto href = linear_gradient_xlink_href()) - return href->start_x(); + if (auto gradient = linked_linear_gradient()) + return gradient->start_x(); // If the attribute is not specified, the effect is as if a value of '0%' were specified. return NumberPercentage::create_percentage(0); } @@ -62,8 +62,8 @@ NumberPercentage SVGLinearGradientElement::start_y() const { if (m_y1.has_value()) return *m_y1; - if (auto href = linear_gradient_xlink_href()) - return href->start_x(); + if (auto gradient = linked_linear_gradient()) + return gradient->start_x(); // If the attribute is not specified, the effect is as if a value of '0%' were specified. return NumberPercentage::create_percentage(0); } @@ -73,8 +73,8 @@ NumberPercentage SVGLinearGradientElement::end_x() const { if (m_x2.has_value()) return *m_x2; - if (auto href = linear_gradient_xlink_href()) - return href->start_x(); + if (auto gradient = linked_linear_gradient()) + return gradient->start_x(); // If the attribute is not specified, the effect is as if a value of '100%' were specified. return NumberPercentage::create_percentage(100); } @@ -84,8 +84,8 @@ NumberPercentage SVGLinearGradientElement::end_y() const { if (m_y2.has_value()) return *m_y2; - if (auto href = linear_gradient_xlink_href()) - return href->start_x(); + if (auto gradient = linked_linear_gradient()) + return gradient->start_x(); // If the attribute is not specified, the effect is as if a value of '0%' were specified. return NumberPercentage::create_percentage(0); } diff --git a/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.h b/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.h index 405a819fcf..a4e2b0bfc2 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.h +++ b/Userland/Libraries/LibWeb/SVG/SVGLinearGradientElement.h @@ -33,10 +33,10 @@ protected: virtual JS::ThrowCompletionOr initialize(JS::Realm&) override; private: - JS::GCPtr linear_gradient_xlink_href() const + JS::GCPtr linked_linear_gradient() const { - if (auto href = xlink_href(); href && is(*href)) - return &verify_cast(*href); + if (auto gradient = linked_gradient(); gradient && is(*gradient)) + return &verify_cast(*gradient); return {}; } diff --git a/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.cpp index dbabe3390a..4ebd74492c 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.cpp @@ -57,8 +57,8 @@ NumberPercentage SVGRadialGradientElement::start_circle_x() const return *m_fx; // If the element references an element that specifies a value for 'fx', then the value of 'fx' is // inherited from the referenced element. - if (auto href = radial_gradient_xlink_href()) - return href->start_circle_x(); + if (auto gradient = linked_radial_gradient()) + return gradient->start_circle_x(); // If attribute ‘fx’ is not specified, ‘fx’ will coincide with the presentational value of ‘cx’ for // the element whether the value for 'cx' was inherited or not. return end_circle_x(); @@ -71,8 +71,8 @@ NumberPercentage SVGRadialGradientElement::start_circle_y() const return *m_fy; // If the element references an element that specifies a value for 'fy', then the value of 'fy' is // inherited from the referenced element. - if (auto href = radial_gradient_xlink_href()) - return href->start_circle_y(); + if (auto gradient = linked_radial_gradient()) + return gradient->start_circle_y(); // If attribute ‘fy’ is not specified, ‘fy’ will coincide with the presentational value of ‘cy’ for // the element whether the value for 'cy' was inherited or not. return end_circle_y(); @@ -86,8 +86,8 @@ NumberPercentage SVGRadialGradientElement::start_circle_radius() const return *m_fr; // if the element references an element that specifies a value for 'fr', then the value of // 'fr' is inherited from the referenced element. - if (auto href = radial_gradient_xlink_href()) - return href->start_circle_radius(); + if (auto gradient = linked_radial_gradient()) + return gradient->start_circle_radius(); // If the attribute is not specified, the effect is as if a value of '0%' were specified. return NumberPercentage::create_percentage(0); } @@ -97,8 +97,8 @@ NumberPercentage SVGRadialGradientElement::end_circle_x() const { if (m_cx.has_value()) return *m_cx; - if (auto href = radial_gradient_xlink_href()) - return href->end_circle_x(); + if (auto gradient = linked_radial_gradient()) + return gradient->end_circle_x(); return NumberPercentage::create_percentage(50); } @@ -107,8 +107,8 @@ NumberPercentage SVGRadialGradientElement::end_circle_y() const { if (m_cy.has_value()) return *m_cy; - if (auto href = radial_gradient_xlink_href()) - return href->end_circle_y(); + if (auto gradient = linked_radial_gradient()) + return gradient->end_circle_y(); return NumberPercentage::create_percentage(50); } @@ -118,8 +118,8 @@ NumberPercentage SVGRadialGradientElement::end_circle_radius() const // Note: A negative value is an error. if (m_r.has_value() && m_r->value() >= 0) return *m_r; - if (auto href = radial_gradient_xlink_href()) - return href->end_circle_radius(); + if (auto gradient = linked_radial_gradient()) + return gradient->end_circle_radius(); return NumberPercentage::create_percentage(50); } diff --git a/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.h b/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.h index 741d2c8493..cef93cddc3 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.h +++ b/Userland/Libraries/LibWeb/SVG/SVGRadialGradientElement.h @@ -35,10 +35,10 @@ protected: virtual JS::ThrowCompletionOr initialize(JS::Realm&) override; private: - JS::GCPtr radial_gradient_xlink_href() const + JS::GCPtr linked_radial_gradient() const { - if (auto href = xlink_href(); href && is(*href)) - return &verify_cast(*href); + if (auto gradient = linked_gradient(); gradient && is(*gradient)) + return &verify_cast(*gradient); return {}; }