2024-08-20 11:12:55 -03:00
|
|
|
|
/*
|
2025-01-19 05:29:19 -03:00
|
|
|
|
* Copyright (c) 2024-2025, Tim Ledbetter <tim.ledbetter@ladybird.org>
|
2024-08-20 11:12:55 -03:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "SVGImageElement.h"
|
2024-11-14 12:01:23 -03:00
|
|
|
|
#include <LibGC/Heap.h>
|
2026-05-05 09:13:30 -03:00
|
|
|
|
#include <LibGfx/DecodedImageFrame.h>
|
2026-04-18 05:54:06 -03:00
|
|
|
|
#include <LibWeb/Bindings/SVGImageElement.h>
|
2026-02-08 13:25:06 -03:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2024-08-20 11:12:55 -03:00
|
|
|
|
#include <LibWeb/DOM/DocumentObserver.h>
|
2024-10-16 11:31:05 -03:00
|
|
|
|
#include <LibWeb/DOM/Event.h>
|
2024-08-20 11:12:55 -03:00
|
|
|
|
#include <LibWeb/HTML/PotentialCORSRequest.h>
|
|
|
|
|
|
#include <LibWeb/HTML/SharedResourceRequest.h>
|
|
|
|
|
|
#include <LibWeb/Layout/SVGImageBox.h>
|
2025-07-10 18:52:47 -03:00
|
|
|
|
#include <LibWeb/Namespace.h>
|
2024-08-29 17:41:22 -03:00
|
|
|
|
#include <LibWeb/Painting/Paintable.h>
|
2024-08-20 11:12:55 -03:00
|
|
|
|
#include <LibWeb/SVG/SVGDecodedImageData.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
|
|
2026-01-18 14:42:26 -03:00
|
|
|
|
GC_DEFINE_ALLOCATOR(SVGImageElement);
|
|
|
|
|
|
|
2024-08-20 11:12:55 -03:00
|
|
|
|
SVGImageElement::SVGImageElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
|
|
|
|
|
: SVGGraphicsElement(document, move(qualified_name))
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-06 00:10:39 -03:00
|
|
|
|
SVGImageElement::~SVGImageElement() = default;
|
|
|
|
|
|
|
2026-06-14 07:04:42 -03:00
|
|
|
|
void SVGImageElement::finalize()
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::finalize();
|
|
|
|
|
|
unregister_with_decoded_image_data_if_needed();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-20 11:12:55 -03:00
|
|
|
|
void SVGImageElement::initialize(JS::Realm& realm)
|
|
|
|
|
|
{
|
|
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGImageElement);
|
2025-04-20 11:22:57 -03:00
|
|
|
|
Base::initialize(realm);
|
2024-08-20 11:12:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SVGImageElement::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
|
SVGURIReferenceMixin::visit_edges(visitor);
|
|
|
|
|
|
visitor.visit(m_x);
|
|
|
|
|
|
visitor.visit(m_y);
|
|
|
|
|
|
visitor.visit(m_width);
|
|
|
|
|
|
visitor.visit(m_height);
|
|
|
|
|
|
visitor.visit(m_resource_request);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-10 16:58:06 -03:00
|
|
|
|
void SVGImageElement::adopted_from(DOM::Document& old_document)
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::adopted_from(old_document);
|
|
|
|
|
|
|
|
|
|
|
|
if (m_load_event_delayer.has_value())
|
|
|
|
|
|
m_load_event_delayer.emplace(document());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-14 10:14:16 -03:00
|
|
|
|
void SVGImageElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
2024-08-20 11:12:55 -03:00
|
|
|
|
{
|
2024-11-14 10:14:16 -03:00
|
|
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
|
|
|
|
|
|
2024-08-20 11:12:55 -03:00
|
|
|
|
if (name == SVG::AttributeNames::x) {
|
|
|
|
|
|
auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
|
|
|
|
|
|
MUST(x()->base_val()->set_value(parsed_value.value_or(0)));
|
|
|
|
|
|
} else if (name == SVG::AttributeNames::y) {
|
|
|
|
|
|
auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
|
|
|
|
|
|
MUST(y()->base_val()->set_value(parsed_value.value_or(0)));
|
|
|
|
|
|
} else if (name == SVG::AttributeNames::width) {
|
|
|
|
|
|
auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
|
|
|
|
|
|
MUST(width()->base_val()->set_value(parsed_value.value_or(0)));
|
|
|
|
|
|
} else if (name == SVG::AttributeNames::height) {
|
|
|
|
|
|
auto parsed_value = AttributeParser::parse_coordinate(value.value_or(String {}));
|
|
|
|
|
|
MUST(height()->base_val()->set_value(parsed_value.value_or(0)));
|
|
|
|
|
|
} else if (name == SVG::AttributeNames::href) {
|
2025-07-10 18:52:47 -03:00
|
|
|
|
// https://svgwg.org/svg2-draft/linking.html#XLinkRefAttrs
|
|
|
|
|
|
// For backwards compatibility, elements with an ‘href’ attribute also recognize an ‘href’ attribute in the
|
|
|
|
|
|
// XLink namespace. If the element is in the XLink namespace, it does not recognize an ‘href’ attribute in the
|
|
|
|
|
|
// SVG namespace. When the ‘href’ attribute is present in both the XLink namespace and without a namespace, the
|
|
|
|
|
|
// value of the attribute without a namespace shall be used. The attribute in the XLink namespace shall be ignored.
|
|
|
|
|
|
if (namespace_ == Namespace::XLink && has_attribute_ns({}, name))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
auto href = value;
|
|
|
|
|
|
if (!namespace_.has_value() && !href.has_value())
|
|
|
|
|
|
href = get_attribute_ns(SVG::AttributeNames::href, Namespace::XLink);
|
|
|
|
|
|
|
|
|
|
|
|
process_the_url(href);
|
2024-08-20 11:12:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__x
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<SVG::SVGAnimatedLength> SVGImageElement::x()
|
2024-08-20 11:12:55 -03:00
|
|
|
|
{
|
2025-08-26 11:51:46 -03:00
|
|
|
|
if (!m_x)
|
|
|
|
|
|
m_x = fake_animated_length_fixme();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
|
|
|
|
|
return *m_x;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__y
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<SVG::SVGAnimatedLength> SVGImageElement::y()
|
2024-08-20 11:12:55 -03:00
|
|
|
|
{
|
2025-08-26 11:51:46 -03:00
|
|
|
|
if (!m_y)
|
|
|
|
|
|
m_y = fake_animated_length_fixme();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
|
|
|
|
|
return *m_y;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__width
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<SVG::SVGAnimatedLength> SVGImageElement::width()
|
2024-08-20 11:12:55 -03:00
|
|
|
|
{
|
|
|
|
|
|
if (!m_width) {
|
|
|
|
|
|
auto& realm = this->realm();
|
2025-08-26 11:51:46 -03:00
|
|
|
|
m_width = SVGAnimatedLength::create(
|
|
|
|
|
|
realm,
|
|
|
|
|
|
SVGLength::create(realm, 0, intrinsic_width().value_or(0).to_double(), SVGLength::ReadOnly::No),
|
|
|
|
|
|
SVGLength::create(realm, 0, 0, SVGLength::ReadOnly::Yes));
|
2024-08-20 11:12:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return *m_width;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/embedded.html#__svg__SVGImageElement__height
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<SVG::SVGAnimatedLength> SVGImageElement::height()
|
2024-08-20 11:12:55 -03:00
|
|
|
|
{
|
|
|
|
|
|
if (!m_height) {
|
|
|
|
|
|
auto& realm = this->realm();
|
2025-08-26 11:51:46 -03:00
|
|
|
|
m_height = SVGAnimatedLength::create(
|
|
|
|
|
|
realm,
|
|
|
|
|
|
SVGLength::create(realm, 0, intrinsic_height().value_or(0).to_double(), SVGLength::ReadOnly::No),
|
|
|
|
|
|
SVGLength::create(realm, 0, 0, SVGLength::ReadOnly::Yes));
|
2024-08-20 11:12:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return *m_height;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-01 08:09:21 -03:00
|
|
|
|
Gfx::FloatRect SVGImageElement::bounding_box() const
|
2024-08-20 11:12:55 -03:00
|
|
|
|
{
|
2025-09-01 08:09:21 -03:00
|
|
|
|
Optional<float> width;
|
2024-08-20 11:12:55 -03:00
|
|
|
|
if (attribute(HTML::AttributeNames::width).has_value())
|
2025-09-01 08:09:21 -03:00
|
|
|
|
width = m_width->base_val()->value();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
2025-09-01 08:09:21 -03:00
|
|
|
|
Optional<float> height;
|
2024-08-20 11:12:55 -03:00
|
|
|
|
if (attribute(HTML::AttributeNames::height).has_value())
|
2025-09-01 08:09:21 -03:00
|
|
|
|
height = m_height->base_val()->value();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
|
|
|
|
|
if (!height.has_value() && width.has_value() && intrinsic_aspect_ratio().has_value())
|
2025-09-01 08:09:21 -03:00
|
|
|
|
height = width.value() / intrinsic_aspect_ratio().value().to_float();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
|
|
|
|
|
if (!width.has_value() && height.has_value() && intrinsic_aspect_ratio().has_value())
|
2025-09-01 08:09:21 -03:00
|
|
|
|
width = height.value() * intrinsic_aspect_ratio().value().to_float();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
|
|
|
|
|
if (!width.has_value() && intrinsic_width().has_value())
|
2025-09-01 08:09:21 -03:00
|
|
|
|
width = intrinsic_width()->to_float();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
|
|
|
|
|
if (!height.has_value() && intrinsic_height().has_value())
|
2025-09-01 08:09:21 -03:00
|
|
|
|
height = intrinsic_height()->to_float();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
|
|
|
|
|
|
return {
|
2025-09-01 08:09:21 -03:00
|
|
|
|
m_x ? m_x->base_val()->value() : 0.0f,
|
|
|
|
|
|
m_y ? m_y->base_val()->value() : 0.0f,
|
|
|
|
|
|
width.value_or(0.0f),
|
|
|
|
|
|
height.value_or(0.0f),
|
2024-08-20 11:12:55 -03:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG2/linking.html#processingURL
|
|
|
|
|
|
void SVGImageElement::process_the_url(Optional<String> const& href)
|
|
|
|
|
|
{
|
2025-01-19 05:29:19 -03:00
|
|
|
|
if (!href.has_value()) {
|
|
|
|
|
|
m_href = {};
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 11:35:11 -03:00
|
|
|
|
m_href = document().encoding_parse_url(*href);
|
2025-01-23 03:40:57 -03:00
|
|
|
|
if (!m_href.has_value())
|
2024-08-20 11:12:55 -03:00
|
|
|
|
return;
|
|
|
|
|
|
|
2025-01-23 03:40:57 -03:00
|
|
|
|
fetch_the_document(*m_href);
|
2024-08-20 11:12:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
|
|
|
|
|
|
void SVGImageElement::fetch_the_document(URL::URL const& url)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_load_event_delayer.emplace(document());
|
2026-06-14 07:04:42 -03:00
|
|
|
|
unregister_with_decoded_image_data_if_needed();
|
2024-08-20 11:12:55 -03:00
|
|
|
|
m_resource_request = HTML::SharedResourceRequest::get_or_create(realm(), document().page(), url);
|
|
|
|
|
|
m_resource_request->add_callbacks(
|
2025-07-10 18:48:12 -03:00
|
|
|
|
[this, resource_request = GC::Root { m_resource_request }] {
|
2024-08-20 11:12:55 -03:00
|
|
|
|
m_load_event_delayer.clear();
|
2026-06-14 07:04:42 -03:00
|
|
|
|
register_with_decoded_image_data_if_needed();
|
2024-09-02 13:10:13 -03:00
|
|
|
|
set_needs_style_update(true);
|
2026-02-26 07:57:29 -03:00
|
|
|
|
set_needs_layout_update(DOM::SetNeedsLayoutReason::SVGImageElementFetchTheDocument);
|
2024-10-16 11:31:05 -03:00
|
|
|
|
|
|
|
|
|
|
dispatch_event(DOM::Event::create(realm(), HTML::EventNames::load));
|
2024-08-20 11:12:55 -03:00
|
|
|
|
},
|
|
|
|
|
|
[this] {
|
|
|
|
|
|
m_load_event_delayer.clear();
|
2024-10-16 11:31:05 -03:00
|
|
|
|
|
|
|
|
|
|
dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error));
|
2024-08-20 11:12:55 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (m_resource_request->needs_fetching()) {
|
|
|
|
|
|
auto request = HTML::create_potential_CORS_request(vm(), url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS);
|
|
|
|
|
|
request->set_client(&document().relevant_settings_object());
|
|
|
|
|
|
m_resource_request->fetch_resource(realm(), request);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
LibWeb: Make layout nodes refcounted
Move the layout tree from GC allocation to refcounted ownership so
removed layout and paint subtrees are destroyed synchronously instead
of waiting for the next GC sweep. This dramatically reduces GC memory
usage peaks after layout tree churn and makes it easier for memory use
to fall back after large document updates.
Update layout factories, tree traversal, SVG layout node creation,
paintable back-pointers, and pseudo-element layout links to use RefPtr
ownership.
Make display: contents follow the same shape as Blink and WebKit: the
element itself does not create a layout node, and its children are
flattened into the nearest layout parent. Wrap direct non-whitespace
text in an anonymous inline node when the boxless element contributes
inherited style to that text.
Use an internal inline wrapper for display: contents pseudo-elements
so generated content can still participate in layout, painting, hit
testing, and pseudo-element queries. Keep CSSOM reporting the computed
display value from the pseudo style, not the internal wrapper.
Remove the retained out-of-tree layout node list and its testing hook,
since the flattened model does not need a side owner for boxless
elements. Add coverage for inherited text style, dynamic insertion
order, pseudo-element hit testing, and computed style queries.
2026-06-07 12:50:33 -03:00
|
|
|
|
RefPtr<Layout::Node> SVGImageElement::create_layout_node(CSS::ComputedProperties const& style)
|
2024-08-20 11:12:55 -03:00
|
|
|
|
{
|
LibWeb: Make layout nodes refcounted
Move the layout tree from GC allocation to refcounted ownership so
removed layout and paint subtrees are destroyed synchronously instead
of waiting for the next GC sweep. This dramatically reduces GC memory
usage peaks after layout tree churn and makes it easier for memory use
to fall back after large document updates.
Update layout factories, tree traversal, SVG layout node creation,
paintable back-pointers, and pseudo-element layout links to use RefPtr
ownership.
Make display: contents follow the same shape as Blink and WebKit: the
element itself does not create a layout node, and its children are
flattened into the nearest layout parent. Wrap direct non-whitespace
text in an anonymous inline node when the boxless element contributes
inherited style to that text.
Use an internal inline wrapper for display: contents pseudo-elements
so generated content can still participate in layout, painting, hit
testing, and pseudo-element queries. Keep CSSOM reporting the computed
display value from the pseudo style, not the internal wrapper.
Remove the retained out-of-tree layout node list and its testing hook,
since the flattened model does not need a side owner for boxless
elements. Add coverage for inherited text style, dynamic insertion
order, pseudo-element hit testing, and computed style queries.
2026-06-07 12:50:33 -03:00
|
|
|
|
return make_ref_counted<Layout::SVGImageBox>(document(), *this, style);
|
2024-08-20 11:12:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-04 16:51:46 -03:00
|
|
|
|
GC::Ptr<HTML::DecodedImageData> SVGImageElement::decoded_image_data() const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_resource_request)
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
return m_resource_request->image_data();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-20 11:12:55 -03:00
|
|
|
|
}
|