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-04-01 00:40:26 +01:00
|
|
|
|
#include <LibWeb/CSS/StyleProperties.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>
|
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>
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07-09 20:18:41 +01:00
|
|
|
|
void SVGElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
|
2023-05-30 21:23:52 +01:00
|
|
|
|
{
|
2024-07-09 20:18:41 +01:00
|
|
|
|
Base::attribute_changed(name, old_value, value);
|
2023-05-30 21:23:52 +01:00
|
|
|
|
|
|
|
|
|
update_use_elements_that_reference_this();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 13:27:01 +01:00
|
|
|
|
void SVGElement::attribute_change_steps(FlyString const& local_name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
|
|
|
|
{
|
|
|
|
|
Base::attribute_change_steps(local_name, old_value, value, namespace_);
|
|
|
|
|
HTMLOrSVGElement::attribute_change_steps(local_name, old_value, value, namespace_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebIDL::ExceptionOr<void> SVGElement::cloned(DOM::Node& copy, bool clone_children)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SVGElement::children_changed()
|
|
|
|
|
{
|
|
|
|
|
Base::children_changed();
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
// Each use element already listens for the completely_loaded event and then clones its referece,
|
|
|
|
|
// 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
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SVGElement::removed_from(Node* parent)
|
|
|
|
|
{
|
|
|
|
|
Base::removed_from(parent);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
JS::NonnullGCPtr<SVGAnimatedString> SVGElement::class_name()
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
JS::GCPtr<SVGSVGElement> SVGElement::owner_svg_element()
|
|
|
|
|
{
|
|
|
|
|
// 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-04-01 00:40:26 +01:00
|
|
|
|
JS::NonnullGCPtr<SVGAnimatedLength> SVGElement::svg_animated_length_for_property(CSS::PropertyID property) const
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Create a proper animated value when animations are supported.
|
|
|
|
|
auto make_length = [&] {
|
2024-10-26 17:42:27 +02:00
|
|
|
|
if (auto const style = computed_css_values(); style.has_value()) {
|
2024-04-01 00:40:26 +01:00
|
|
|
|
if (auto length = style->length_percentage(property); length.has_value())
|
|
|
|
|
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
|
|
|
|
}
|