ladybird/Libraries/LibWeb/SVG/SVGAElement.cpp
Andreas Kling 9340d2d1a3 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 20:52:49 +02:00

118 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/SVGAElement.h>
#include <LibWeb/CSS/Invalidation/LinkInvalidator.h>
#include <LibWeb/DOM/DOMTokenList.h>
#include <LibWeb/HTML/UserNavigationInvolvement.h>
#include <LibWeb/Layout/SVGGraphicsBox.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/SVGAElement.h>
#include <LibWeb/UIEvents/MouseEvent.h>
namespace Web::SVG {
GC_DEFINE_ALLOCATOR(SVGAElement);
SVGAElement::SVGAElement(DOM::Document& document, DOM::QualifiedName qualified_name)
: SVGGraphicsElement(document, move(qualified_name))
{
}
SVGAElement::~SVGAElement() = default;
void SVGAElement::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGAElement);
Base::initialize(realm);
}
void SVGAElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
SVGURIReferenceMixin::visit_edges(visitor);
visitor.visit(m_rel_list);
visitor.visit(m_target);
}
void SVGAElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
{
Base::attribute_changed(name, old_value, value, namespace_);
if (name == SVG::AttributeNames::href)
CSS::Invalidation::invalidate_style_after_hyperlink_state_change(*this);
if (name == HTML::AttributeNames::rel) {
if (m_rel_list)
m_rel_list->associated_attribute_changed(value.value_or(String {}));
}
}
// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
i32 SVGAElement::default_tab_index_value() const
{
// See the base function for the spec comments.
return 0;
}
// https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__target
GC::Ref<SVGAnimatedString> SVGAElement::target()
{
if (!m_target)
m_target = SVGAnimatedString::create(realm(), *this, DOM::QualifiedName { HTML::AttributeNames::target, OptionalNone {}, OptionalNone {} });
return *m_target;
}
// https://svgwg.org/svg2-draft/linking.html#__svg__SVGAElement__relList
GC::Ref<DOM::DOMTokenList> SVGAElement::rel_list()
{
// The relList IDL attribute reflects the rel content attribute.
if (!m_rel_list)
m_rel_list = DOM::DOMTokenList::create(*this, HTML::AttributeNames::rel);
return *m_rel_list;
}
RefPtr<Layout::Node> SVGAElement::create_layout_node(CSS::ComputedProperties const& style)
{
return make_ref_counted<Layout::SVGGraphicsBox>(document(), *this, style);
}
// https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements
void SVGAElement::activation_behavior(DOM::Event const& event)
{
// The activation behavior of an a or area element element given an event event is:
// 1. If element has no href attribute, then return.
if (href()->base_val().is_empty())
return;
// AD-HOC: Do not activate the element for clicks with the ctrl/cmd modifier present. This lets
// the browser process open the link in a new tab.
if (is<UIEvents::MouseEvent>(event)) {
auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
if (mouse_event.platform_ctrl_key())
return;
}
// 2. Let hyperlinkSuffix be null.
Optional<String> hyperlink_suffix {};
// FIXME: 3. If element is an a element, and event's target is an img with an ismap attribute specified, then:
// 4. Let userInvolvement be event's user navigation involvement.
auto user_involvement = HTML::user_navigation_involvement(event);
// FIXME: 5. If the user has expressed a preference to download the hyperlink, then set userInvolvement to "browser UI".
// FIXME: 6. If element has a download attribute, or if the user has expressed a preference to download the
// hyperlink, then download the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and
// userInvolvement set to userInvolvement.
// 7. Otherwise, follow the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and userInvolvement set to userInvolvement.
follow_the_hyperlink(hyperlink_suffix, user_involvement);
}
}