2021-10-15 10:57:07 -03:00
|
|
|
|
/*
|
2022-01-31 15:07:22 -03:00
|
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
2023-03-29 19:46:18 -03:00
|
|
|
|
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
2025-08-07 21:12:15 -03:00
|
|
|
|
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
|
2021-10-15 10:57:07 -03:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-04-18 05:54:06 -03:00
|
|
|
|
#include <LibWeb/Bindings/Attr.h>
|
2022-09-25 19:15:49 -03:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2022-09-17 20:03:58 -03:00
|
|
|
|
#include <LibWeb/DOM/Attr.h>
|
2021-10-15 10:57:07 -03:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2021-10-17 16:09:47 -03:00
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
2022-07-11 12:40:01 -03:00
|
|
|
|
#include <LibWeb/DOM/MutationType.h>
|
|
|
|
|
|
#include <LibWeb/DOM/StaticNodeList.h>
|
2023-03-29 19:46:18 -03:00
|
|
|
|
#include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
|
2025-08-07 21:12:15 -03:00
|
|
|
|
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
|
2021-10-15 10:57:07 -03:00
|
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC_DEFINE_ALLOCATOR(Attr);
|
2023-11-19 15:47:52 -03:00
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<Attr> Attr::create(Document& document, FlyString local_name, String value, Element* owner_element)
|
2021-10-15 10:57:07 -03:00
|
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
|
return document.realm().create<Attr>(document, QualifiedName(move(local_name), Optional<FlyString> {}, Optional<FlyString> {}), move(value), owner_element);
|
2021-10-15 10:57:07 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC::Ref<Attr> Attr::create(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
|
2023-03-10 10:56:29 -03:00
|
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
|
return document.realm().create<Attr>(document, move(qualified_name), move(value), owner_element);
|
2023-03-10 10:56:29 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-11 14:37:08 -03:00
|
|
|
|
GC::Ref<Attr> Attr::clone(Document& document) const
|
2022-12-13 09:08:46 -03:00
|
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
|
return realm().create<Attr>(document, m_qualified_name, m_value, nullptr);
|
2022-12-13 09:08:46 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-10 01:06:58 -03:00
|
|
|
|
Attr::Attr(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
|
2021-10-15 10:57:07 -03:00
|
|
|
|
: Node(document, NodeType::ATTRIBUTE_NODE)
|
2022-12-13 09:08:46 -03:00
|
|
|
|
, m_qualified_name(move(qualified_name))
|
2021-10-15 10:57:07 -03:00
|
|
|
|
, m_value(move(value))
|
2021-10-15 13:03:08 -03:00
|
|
|
|
, m_owner_element(owner_element)
|
2021-10-15 10:57:07 -03:00
|
|
|
|
{
|
2023-01-10 08:28:20 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
|
void Attr::initialize(JS::Realm& realm)
|
2023-01-10 08:28:20 -03:00
|
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(Attr);
|
2025-04-20 11:22:57 -03:00
|
|
|
|
Base::initialize(realm);
|
2022-08-28 08:42:07 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-17 20:03:58 -03:00
|
|
|
|
void Attr::visit_edges(Cell::Visitor& visitor)
|
2022-08-28 08:42:07 -03:00
|
|
|
|
{
|
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-11-19 00:18:00 -03:00
|
|
|
|
visitor.visit(m_owner_element);
|
2021-10-15 10:57:07 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-29 19:46:18 -03:00
|
|
|
|
Element* Attr::owner_element()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_owner_element.ptr();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-17 20:03:58 -03:00
|
|
|
|
Element const* Attr::owner_element() const
|
2021-10-17 16:09:47 -03:00
|
|
|
|
{
|
2022-08-28 08:42:07 -03:00
|
|
|
|
return m_owner_element.ptr();
|
2021-10-17 16:09:47 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-29 19:46:18 -03:00
|
|
|
|
void Attr::set_owner_element(Element* owner_element)
|
2021-10-17 16:09:47 -03:00
|
|
|
|
{
|
|
|
|
|
|
m_owner_element = owner_element;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-31 17:08:58 -03:00
|
|
|
|
// https://dom.spec.whatwg.org/#set-an-existing-attribute-value
|
2025-08-07 21:12:15 -03:00
|
|
|
|
WebIDL::ExceptionOr<void> Attr::set_value(String value)
|
2022-07-11 12:40:01 -03:00
|
|
|
|
{
|
2025-10-31 17:08:58 -03:00
|
|
|
|
// 1. If attribute’s element is null, then set attribute’s value to value and return.
|
2022-07-11 12:40:01 -03:00
|
|
|
|
if (!owner_element()) {
|
|
|
|
|
|
m_value = move(value);
|
2025-10-31 17:08:58 -03:00
|
|
|
|
return {};
|
2022-07-11 12:40:01 -03:00
|
|
|
|
}
|
2025-10-31 17:08:58 -03:00
|
|
|
|
|
|
|
|
|
|
// 2. Let element be attribute’s element.
|
|
|
|
|
|
auto const& element = *owner_element();
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value with
|
|
|
|
|
|
// attribute’s local name, attribute’s namespace, element, and value.
|
|
|
|
|
|
auto const verified_value = TRY(TrustedTypes::get_trusted_types_compliant_attribute_value(
|
|
|
|
|
|
local_name(),
|
|
|
|
|
|
namespace_uri().has_value() ? Utf16String::from_utf8(namespace_uri().value()) : Optional<Utf16String>(),
|
|
|
|
|
|
element,
|
|
|
|
|
|
Utf16String::from_utf8(value)));
|
|
|
|
|
|
|
|
|
|
|
|
// 4. If attribute’s element is null, then set attribute’s value to verifiedValue, and return.
|
|
|
|
|
|
if (!owner_element()) {
|
|
|
|
|
|
m_value = verified_value.to_utf8_but_should_be_ported_to_utf16();
|
|
|
|
|
|
return {};
|
2023-09-01 11:40:37 -03:00
|
|
|
|
}
|
2025-08-07 21:12:15 -03:00
|
|
|
|
|
2025-10-31 17:08:58 -03:00
|
|
|
|
// 5. Change attribute to verifiedValue.
|
|
|
|
|
|
change_attribute(verified_value.to_utf8_but_should_be_ported_to_utf16());
|
|
|
|
|
|
|
2025-08-07 21:12:15 -03:00
|
|
|
|
return {};
|
2023-09-01 11:40:37 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-element-attributes-change
|
2023-09-10 01:06:58 -03:00
|
|
|
|
void Attr::change_attribute(String value)
|
2023-09-01 11:40:37 -03:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Let oldValue be attribute’s value.
|
|
|
|
|
|
auto old_value = move(m_value);
|
2022-07-11 12:40:01 -03:00
|
|
|
|
|
|
|
|
|
|
// 2. Set attribute’s value to value.
|
|
|
|
|
|
m_value = move(value);
|
2023-09-01 11:40:37 -03:00
|
|
|
|
|
|
|
|
|
|
// 3. Handle attribute changes for attribute with attribute’s element, oldValue, and value.
|
2023-11-19 02:10:36 -03:00
|
|
|
|
handle_attribute_changes(*owner_element(), old_value, m_value);
|
2022-07-11 12:40:01 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#handle-attribute-changes
|
2023-11-19 02:10:36 -03:00
|
|
|
|
void Attr::handle_attribute_changes(Element& element, Optional<String> const& old_value, Optional<String> const& new_value)
|
2022-07-11 12:40:01 -03:00
|
|
|
|
{
|
|
|
|
|
|
// 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
|
2023-11-19 02:10:36 -03:00
|
|
|
|
element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, {}, {}, nullptr, nullptr);
|
2022-07-11 12:40:01 -03:00
|
|
|
|
|
2024-12-18 14:01:03 -03:00
|
|
|
|
// 2. If element is custom, then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback",
|
|
|
|
|
|
// and « attribute’s local name, oldValue, newValue, attribute’s namespace ».
|
2023-03-29 19:46:18 -03:00
|
|
|
|
if (element.is_custom()) {
|
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
2026-05-19 15:40:15 -03:00
|
|
|
|
GC::RootVector<JS::Value> arguments;
|
2026-06-21 14:03:10 -03:00
|
|
|
|
arguments.append(JS::PrimitiveString::create(vm, Utf16FlyString::from_utf8(local_name())));
|
|
|
|
|
|
arguments.append(!old_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, Utf16String::from_utf8(old_value.value())));
|
|
|
|
|
|
arguments.append(!new_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, Utf16String::from_utf8(new_value.value())));
|
|
|
|
|
|
arguments.append(!namespace_uri().has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, Utf16FlyString::from_utf8(namespace_uri().value())));
|
2023-03-29 19:46:18 -03:00
|
|
|
|
|
|
|
|
|
|
element.enqueue_a_custom_element_callback_reaction(HTML::CustomElementReactionNames::attributeChangedCallback, move(arguments));
|
|
|
|
|
|
}
|
2022-07-11 12:40:01 -03:00
|
|
|
|
|
2023-09-02 11:05:15 -03:00
|
|
|
|
// 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
|
2023-11-06 16:07:02 -03:00
|
|
|
|
element.run_attribute_change_steps(local_name(), old_value, new_value, namespace_uri());
|
2022-07-11 12:40:01 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-15 10:57:07 -03:00
|
|
|
|
}
|