2020-07-19 23:01:53 -07:00
|
|
|
|
/*
|
2021-04-22 16:53:07 -07:00
|
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2023-05-30 21:23:52 +01:00
|
|
|
|
* Copyright (c) 2023, Preston Taylor <95388976+PrestonLTaylor@users.noreply.github.com>
|
2020-07-19 23:01:53 -07:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-19 23:01:53 -07:00
|
|
|
|
*/
|
|
|
|
|
|
2023-05-21 13:25:03 +02:00
|
|
|
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
2022-09-30 17:16:16 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/SVGElementPrototype.h>
|
2024-12-20 11:32:17 +01:00
|
|
|
|
#include <LibWeb/CSS/ComputedProperties.h>
|
2023-05-30 21:23:52 +01:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2023-07-12 11:23:33 +02:00
|
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
2024-12-24 17:16:10 +09:00
|
|
|
|
#include <LibWeb/SVG/SVGDescElement.h>
|
2020-07-23 09:44:42 -07:00
|
|
|
|
#include <LibWeb/SVG/SVGElement.h>
|
2024-07-16 11:26:25 +01:00
|
|
|
|
#include <LibWeb/SVG/SVGSVGElement.h>
|
2024-12-24 17:16:10 +09:00
|
|
|
|
#include <LibWeb/SVG/SVGTitleElement.h>
|
2023-05-30 21:23:52 +01:00
|
|
|
|
#include <LibWeb/SVG/SVGUseElement.h>
|
2020-07-19 23:01:53 -07:00
|
|
|
|
|
2020-07-23 09:44:42 -07:00
|
|
|
|
namespace Web::SVG {
|
2020-07-19 23:01:53 -07:00
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
|
SVGElement::SVGElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
|
: Element(document, move(qualified_name))
|
2020-07-23 09:44:42 -07:00
|
|
|
|
{
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void SVGElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGElement);
|
2022-08-28 13:42:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 17:16:10 +09:00
|
|
|
|
bool SVGElement::should_include_in_accessibility_tree() const
|
|
|
|
|
{
|
|
|
|
|
bool has_title_or_desc = false;
|
|
|
|
|
auto role = role_from_role_attribute_value();
|
|
|
|
|
for_each_child_of_type<SVGElement>([&has_title_or_desc](auto& child) {
|
|
|
|
|
if ((is<SVGTitleElement>(child) || is<SVGDescElement>(child)) && !child.text_content()->trim_ascii_whitespace().value().is_empty()) {
|
|
|
|
|
has_title_or_desc = true;
|
|
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
// https://w3c.github.io/svg-aam/#include_elements
|
|
|
|
|
// TODO: Add support for the SVG tabindex attribute, and include a check for it here.
|
|
|
|
|
return has_title_or_desc
|
|
|
|
|
|| (aria_label().has_value() && !aria_label().value().trim_ascii_whitespace().value().is_empty())
|
|
|
|
|
|| (aria_labelled_by().has_value() && !aria_labelled_by().value().trim_ascii_whitespace().value().is_empty())
|
|
|
|
|
|| (aria_described_by().has_value() && !aria_described_by().value().trim_ascii_whitespace().value().is_empty())
|
|
|
|
|
|| (role.has_value() && ARIA::is_abstract_role(role.value()) && role != ARIA::Role::none && role != ARIA::Role::presentation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<ARIA::Role> SVGElement::default_role() const
|
|
|
|
|
{
|
|
|
|
|
// https://w3c.github.io/svg-aam/#mapping_role_table
|
|
|
|
|
if (local_name() == TagNames::a && (has_attribute(SVG::AttributeNames::href) || has_attribute(AttributeNames::xlink_href)))
|
|
|
|
|
return ARIA::Role::link;
|
2024-12-26 11:32:13 +09:00
|
|
|
|
if (local_name().is_one_of(TagNames::foreignObject, TagNames::g)
|
|
|
|
|
&& should_include_in_accessibility_tree())
|
2024-12-24 17:16:10 +09:00
|
|
|
|
return ARIA::Role::group;
|
|
|
|
|
if (local_name() == TagNames::image && should_include_in_accessibility_tree())
|
|
|
|
|
return ARIA::Role::image;
|
2024-12-26 11:32:13 +09:00
|
|
|
|
if (local_name() == TagNames::circle && should_include_in_accessibility_tree())
|
|
|
|
|
return ARIA::Role::graphicssymbol;
|
|
|
|
|
if (local_name().is_one_of(TagNames::ellipse, TagNames::path, TagNames::polygon, TagNames::polyline)
|
|
|
|
|
&& should_include_in_accessibility_tree())
|
|
|
|
|
return ARIA::Role::graphicssymbol;
|
|
|
|
|
return ARIA::Role::generic;
|
2024-12-24 17:16:10 +09:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
|
void SVGElement::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2024-10-29 11:07:02 +01:00
|
|
|
|
HTMLOrSVGElement::visit_edges(visitor);
|
2024-07-09 20:59:00 +01:00
|
|
|
|
visitor.visit(m_class_name_animated_string);
|
2020-07-23 09:44:42 -07:00
|
|
|
|
}
|
2020-07-19 23:01:53 -07:00
|
|
|
|
|
2024-11-14 08:14:16 -05:00
|
|
|
|
void SVGElement::attribute_changed(FlyString const& local_name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
2023-05-30 21:23:52 +01:00
|
|
|
|
{
|
2024-11-14 08:14:16 -05:00
|
|
|
|
Base::attribute_changed(local_name, old_value, value, namespace_);
|
|
|
|
|
HTMLOrSVGElement::attribute_changed(local_name, old_value, value, namespace_);
|
2023-05-30 21:23:52 +01:00
|
|
|
|
|
|
|
|
|
update_use_elements_that_reference_this();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-11 17:37:08 +00:00
|
|
|
|
WebIDL::ExceptionOr<void> SVGElement::cloned(DOM::Node& copy, bool clone_children) const
|
2024-10-29 13:27:01 +01:00
|
|
|
|
{
|
|
|
|
|
TRY(Base::cloned(copy, clone_children));
|
|
|
|
|
TRY(HTMLOrSVGElement::cloned(copy, clone_children));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 21:23:52 +01:00
|
|
|
|
void SVGElement::inserted()
|
|
|
|
|
{
|
|
|
|
|
Base::inserted();
|
2024-10-29 13:27:01 +01:00
|
|
|
|
HTMLOrSVGElement::inserted();
|
2023-05-30 21:23:52 +01:00
|
|
|
|
|
|
|
|
|
update_use_elements_that_reference_this();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 01:16:33 +13:00
|
|
|
|
void SVGElement::children_changed(ChildrenChangedMetadata const* metadata)
|
2023-05-30 21:23:52 +01:00
|
|
|
|
{
|
2025-01-27 01:16:33 +13:00
|
|
|
|
Base::children_changed(metadata);
|
2023-05-30 21:23:52 +01:00
|
|
|
|
|
|
|
|
|
update_use_elements_that_reference_this();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SVGElement::update_use_elements_that_reference_this()
|
|
|
|
|
{
|
2023-07-12 11:23:33 +02:00
|
|
|
|
if (is<SVGUseElement>(this)
|
|
|
|
|
// If this element is in a shadow root, it already represents a clone and is not itself referenced.
|
|
|
|
|
|| is<DOM::ShadowRoot>(this->root())
|
|
|
|
|
// If this does not have an id it cannot be referenced, no point in searching the entire DOM tree.
|
2023-11-02 15:01:06 +01:00
|
|
|
|
|| !id().has_value()
|
2023-07-12 11:23:33 +02:00
|
|
|
|
// An unconnected node cannot have valid references.
|
|
|
|
|
// This also prevents searches for elements that are in the process of being constructed - as clones.
|
|
|
|
|
|| !this->is_connected()
|
2025-04-07 10:46:22 +00:00
|
|
|
|
// Each use element already listens for the completely_loaded event and then clones its reference,
|
2023-07-12 11:23:33 +02:00
|
|
|
|
// we do not have to also clone it in the process of initial DOM building.
|
|
|
|
|
|| !document().is_completely_loaded()) {
|
|
|
|
|
|
2023-05-30 21:23:52 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document().for_each_in_subtree_of_type<SVGUseElement>([this](SVGUseElement& use_element) {
|
|
|
|
|
use_element.svg_element_changed(*this);
|
2024-05-04 14:47:04 +01:00
|
|
|
|
return TraversalDecision::Continue;
|
2023-05-30 21:23:52 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:37:18 +01:00
|
|
|
|
void SVGElement::removed_from(Node* old_parent, Node& old_root)
|
2023-05-30 21:23:52 +01:00
|
|
|
|
{
|
2025-01-23 17:37:18 +01:00
|
|
|
|
Base::removed_from(old_parent, old_root);
|
2023-05-30 21:23:52 +01:00
|
|
|
|
|
|
|
|
|
remove_from_use_element_that_reference_this();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SVGElement::remove_from_use_element_that_reference_this()
|
|
|
|
|
{
|
2023-11-02 15:01:06 +01:00
|
|
|
|
if (is<SVGUseElement>(this) || !id().has_value()) {
|
2023-05-30 21:23:52 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document().for_each_in_subtree_of_type<SVGUseElement>([this](SVGUseElement& use_element) {
|
|
|
|
|
use_element.svg_element_removed(*this);
|
2024-05-04 14:47:04 +01:00
|
|
|
|
return TraversalDecision::Continue;
|
2023-05-30 21:23:52 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 20:59:00 +01:00
|
|
|
|
// https://svgwg.org/svg2-draft/types.html#__svg__SVGElement__classNames
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<SVGAnimatedString> SVGElement::class_name()
|
2024-07-09 20:59:00 +01:00
|
|
|
|
{
|
|
|
|
|
// The className IDL attribute reflects the ‘class’ attribute.
|
|
|
|
|
if (!m_class_name_animated_string)
|
|
|
|
|
m_class_name_animated_string = SVGAnimatedString::create(realm(), *this, AttributeNames::class_);
|
|
|
|
|
|
|
|
|
|
return *m_class_name_animated_string;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 11:26:25 +01:00
|
|
|
|
// https://svgwg.org/svg2-draft/types.html#__svg__SVGElement__ownerSVGElement
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<SVGSVGElement> SVGElement::owner_svg_element()
|
2024-07-16 11:26:25 +01:00
|
|
|
|
{
|
|
|
|
|
// The ownerSVGElement IDL attribute represents the nearest ancestor ‘svg’ element.
|
|
|
|
|
// On getting ownerSVGElement, the nearest ancestor ‘svg’ element is returned;
|
|
|
|
|
// if the current element is the outermost svg element, then null is returned.
|
2024-09-06 23:44:34 +01:00
|
|
|
|
return shadow_including_first_ancestor_of_type<SVGSVGElement>();
|
2024-07-16 11:26:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<SVGAnimatedLength> SVGElement::svg_animated_length_for_property(CSS::PropertyID property) const
|
2024-04-01 00:40:26 +01:00
|
|
|
|
{
|
|
|
|
|
// FIXME: Create a proper animated value when animations are supported.
|
|
|
|
|
auto make_length = [&] {
|
2024-12-20 16:35:12 +01:00
|
|
|
|
if (auto const computed_properties = this->computed_properties()) {
|
|
|
|
|
if (auto length = computed_properties->length_percentage(property); length.has_value())
|
2024-04-01 00:40:26 +01:00
|
|
|
|
return SVGLength::from_length_percentage(realm(), *length);
|
|
|
|
|
}
|
|
|
|
|
return SVGLength::create(realm(), 0, 0.0f);
|
|
|
|
|
};
|
|
|
|
|
return SVGAnimatedLength::create(realm(), make_length(), make_length());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-19 23:01:53 -07:00
|
|
|
|
}
|