2023-04-20 14:51:00 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
2025-07-10 12:13:52 -03:00
|
|
|
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
|
2023-04-20 14:51:00 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2026-04-18 05:54:06 -03:00
|
|
|
#include <LibWeb/Bindings/SVGStopElement.h>
|
2024-12-20 07:32:17 -03:00
|
|
|
#include <LibWeb/CSS/ComputedProperties.h>
|
2023-04-20 14:51:00 -03:00
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
|
|
|
|
#include <LibWeb/SVG/AttributeNames.h>
|
|
|
|
|
#include <LibWeb/SVG/SVGStopElement.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(SVGStopElement);
|
2023-11-19 15:47:52 -03:00
|
|
|
|
2023-04-20 14:51:00 -03:00
|
|
|
SVGStopElement::SVGStopElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
|
|
|
|
: SVGElement(document, qualified_name)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-23 13:51:10 -03:00
|
|
|
bool SVGStopElement::is_presentational_hint(FlyString const& name) const
|
|
|
|
|
{
|
|
|
|
|
if (Base::is_presentational_hint(name))
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-06-04 10:31:08 -03:00
|
|
|
return first_is_one_of(name, SVG::AttributeNames::stopColor, SVG::AttributeNames::stopOpacity);
|
2024-12-23 13:51:10 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 06:44:26 -03:00
|
|
|
void SVGStopElement::apply_presentational_hints(Vector<CSS::StyleProperty>& properties) const
|
2023-04-20 14:51:00 -03:00
|
|
|
{
|
2026-04-30 06:44:26 -03:00
|
|
|
Base::apply_presentational_hints(properties);
|
2025-08-21 10:53:37 -03:00
|
|
|
CSS::Parser::ParsingParams parsing_context { document(), CSS::Parser::ParsingMode::SVGPresentationAttribute };
|
2023-04-20 14:51:00 -03:00
|
|
|
for_each_attribute([&](auto& name, auto& value) {
|
2025-06-04 10:31:08 -03:00
|
|
|
if (name == SVG::AttributeNames::stopColor) {
|
2023-08-19 11:01:21 -03:00
|
|
|
if (auto stop_color = parse_css_value(parsing_context, value, CSS::PropertyID::StopColor)) {
|
2026-04-30 06:44:26 -03:00
|
|
|
properties.append({ .property_id = CSS::PropertyID::StopColor, .value = stop_color.release_nonnull() });
|
2023-04-20 14:51:00 -03:00
|
|
|
}
|
2025-06-04 10:31:08 -03:00
|
|
|
} else if (name == SVG::AttributeNames::stopOpacity) {
|
2023-08-19 11:01:21 -03:00
|
|
|
if (auto stop_opacity = parse_css_value(parsing_context, value, CSS::PropertyID::StopOpacity)) {
|
2026-04-30 06:44:26 -03:00
|
|
|
properties.append({ .property_id = CSS::PropertyID::StopOpacity, .value = stop_opacity.release_nonnull() });
|
2023-05-19 16:35:39 -03:00
|
|
|
}
|
2023-04-20 14:51:00 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 01:00:37 -03:00
|
|
|
Gfx::Color SVGStopElement::stop_color()
|
2023-04-20 14:51:00 -03:00
|
|
|
{
|
2024-12-20 12:35:12 -03:00
|
|
|
if (auto computed_properties = this->computed_properties())
|
2026-03-24 08:48:45 -03:00
|
|
|
return computed_properties->color(CSS::PropertyID::StopColor, CSS::ColorResolutionContext::for_element({ *this }));
|
2025-07-19 01:00:37 -03:00
|
|
|
return CSS::InitialValues::stop_color();
|
2023-04-20 14:51:00 -03:00
|
|
|
}
|
|
|
|
|
|
2023-05-19 16:35:39 -03:00
|
|
|
float SVGStopElement::stop_opacity() const
|
|
|
|
|
{
|
2024-12-20 12:35:12 -03:00
|
|
|
if (auto computed_properties = this->computed_properties())
|
|
|
|
|
return computed_properties->stop_opacity();
|
2023-05-19 16:35:39 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 12:13:52 -03:00
|
|
|
// https://svgwg.org/svg2-draft/pservers.html#StopElementOffsetAttribute
|
|
|
|
|
GC::Ref<SVGAnimatedNumber> SVGStopElement::offset()
|
2023-04-20 14:51:00 -03:00
|
|
|
{
|
2025-07-10 12:13:52 -03:00
|
|
|
if (!m_stop_offset)
|
2025-10-31 09:27:49 -03:00
|
|
|
m_stop_offset = SVGAnimatedNumber::create(realm(), *this, DOM::QualifiedName { AttributeNames::offset, OptionalNone {}, OptionalNone {} }, 0.f);
|
2025-07-10 12:13:52 -03:00
|
|
|
return *m_stop_offset;
|
2023-04-20 14:51:00 -03:00
|
|
|
}
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void SVGStopElement::initialize(JS::Realm& realm)
|
2023-04-20 14:51:00 -03:00
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGStopElement);
|
2025-04-20 11:22:57 -03:00
|
|
|
Base::initialize(realm);
|
2023-04-20 14:51:00 -03:00
|
|
|
}
|
|
|
|
|
|
2025-07-10 12:13:52 -03:00
|
|
|
void SVGStopElement::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_stop_offset);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 14:51:00 -03:00
|
|
|
}
|