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>
|
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>
|
2022-10-08 12:25:01 +02:00
|
|
|
#include <LibWeb/HTML/DOMStringMap.h>
|
2020-07-23 09:44:42 -07:00
|
|
|
#include <LibWeb/SVG/SVGElement.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-01-28 12:33:35 -05:00
|
|
|
JS::ThrowCompletionOr<void> SVGElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 06:28:20 -05:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGElementPrototype>(realm, "SVGElement"));
|
2023-01-28 12:33:35 -05:00
|
|
|
|
2023-05-21 13:25:03 +02:00
|
|
|
m_dataset = TRY(Bindings::throw_dom_exception_if_needed(realm.vm(), [&]() {
|
|
|
|
return HTML::DOMStringMap::create(*this);
|
|
|
|
}));
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
return {};
|
2022-08-28 13:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SVGElement::visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
2023-05-21 13:25:03 +02:00
|
|
|
visitor.visit(m_dataset);
|
2020-07-23 09:44:42 -07:00
|
|
|
}
|
2020-07-19 23:01:53 -07:00
|
|
|
|
2023-07-03 17:08:37 +02:00
|
|
|
void SVGElement::attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value)
|
2023-05-30 21:23:52 +01:00
|
|
|
{
|
2023-07-03 17:08:37 +02:00
|
|
|
Base::attribute_changed(name, value);
|
2023-05-30 21:23:52 +01:00
|
|
|
|
|
|
|
update_use_elements_that_reference_this();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVGElement::inserted()
|
|
|
|
{
|
|
|
|
Base::inserted();
|
|
|
|
|
|
|
|
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.
|
|
|
|
|| !this->has_attribute(HTML::AttributeNames::id)
|
|
|
|
// 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);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-12 11:23:33 +02:00
|
|
|
if (is<SVGUseElement>(this) || !this->has_attribute(HTML::AttributeNames::id)) {
|
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);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:01:53 -07:00
|
|
|
}
|