123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /*
- * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
- * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <LibWeb/Bindings/Intrinsics.h>
- #include <LibWeb/DOM/Attr.h>
- #include <LibWeb/DOM/Document.h>
- #include <LibWeb/DOM/Element.h>
- #include <LibWeb/DOM/MutationType.h>
- #include <LibWeb/DOM/StaticNodeList.h>
- #include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
- namespace Web::DOM {
- JS::NonnullGCPtr<Attr> Attr::create(Document& document, FlyString local_name, String value, Element* owner_element)
- {
- return document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), Optional<FlyString> {}, Optional<FlyString> {}), move(value), owner_element);
- }
- JS::NonnullGCPtr<Attr> Attr::create(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
- {
- return document.heap().allocate<Attr>(document.realm(), document, move(qualified_name), move(value), owner_element);
- }
- JS::NonnullGCPtr<Attr> Attr::clone(Document& document)
- {
- return *heap().allocate<Attr>(realm(), document, m_qualified_name, m_value, nullptr);
- }
- Attr::Attr(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
- : Node(document, NodeType::ATTRIBUTE_NODE)
- , m_qualified_name(move(qualified_name))
- , m_value(move(value))
- , m_owner_element(owner_element)
- {
- }
- void Attr::initialize(JS::Realm& realm)
- {
- Base::initialize(realm);
- set_prototype(&Bindings::ensure_web_prototype<Bindings::AttrPrototype>(realm, "Attr"));
- }
- void Attr::visit_edges(Cell::Visitor& visitor)
- {
- Base::visit_edges(visitor);
- visitor.visit(m_owner_element.ptr());
- }
- Element* Attr::owner_element()
- {
- return m_owner_element.ptr();
- }
- Element const* Attr::owner_element() const
- {
- return m_owner_element.ptr();
- }
- void Attr::set_owner_element(Element* owner_element)
- {
- m_owner_element = owner_element;
- }
- // https://dom.spec.whatwg.org/#set-an-existing-attribute-value
- void Attr::set_value(String value)
- {
- // 1. If attribute’s element is null, then set attribute’s value to value.
- if (!owner_element()) {
- m_value = move(value);
- }
- // 2. Otherwise, change attribute to value.
- else {
- change_attribute(move(value));
- }
- }
- // https://dom.spec.whatwg.org/#concept-element-attributes-change
- void Attr::change_attribute(String value)
- {
- // 1. Let oldValue be attribute’s value.
- auto old_value = move(m_value);
- // 2. Set attribute’s value to value.
- m_value = move(value);
- // 3. Handle attribute changes for attribute with attribute’s element, oldValue, and value.
- handle_attribute_changes(*owner_element(), old_value.to_deprecated_string(), m_value.to_deprecated_string());
- }
- // https://dom.spec.whatwg.org/#handle-attribute-changes
- void Attr::handle_attribute_changes(Element& element, Optional<DeprecatedString> old_value, Optional<DeprecatedString> new_value)
- {
- DeprecatedString deprecated_namespace_uri;
- if (namespace_uri().has_value())
- deprecated_namespace_uri = namespace_uri().value().to_deprecated_fly_string();
- // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
- element.queue_mutation_record(MutationType::attributes, local_name().to_deprecated_fly_string(), deprecated_namespace_uri, old_value, {}, {}, nullptr, nullptr);
- // 2. If element is custom, then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback", and an argument list containing attribute’s local name, oldValue, newValue, and attribute’s namespace.
- if (element.is_custom()) {
- auto& vm = this->vm();
- JS::MarkedVector<JS::Value> arguments { vm.heap() };
- arguments.append(JS::PrimitiveString::create(vm, local_name()));
- arguments.append(!old_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, old_value.release_value()));
- arguments.append(!new_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, new_value.release_value()));
- arguments.append(!namespace_uri().has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, namespace_uri().value()));
- element.enqueue_a_custom_element_callback_reaction(HTML::CustomElementReactionNames::attributeChangedCallback, move(arguments));
- }
- // 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
- element.run_attribute_change_steps(local_name(), old_value, new_value, deprecated_namespace_uri);
- }
- }
|