Serenity Operating System
at master 94 lines 3.8 kB view raw
1/* 2 * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibWeb/Bindings/Intrinsics.h> 8#include <LibWeb/DOM/Attr.h> 9#include <LibWeb/DOM/Document.h> 10#include <LibWeb/DOM/Element.h> 11#include <LibWeb/DOM/MutationType.h> 12#include <LibWeb/DOM/StaticNodeList.h> 13 14namespace Web::DOM { 15 16WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Attr::create(Document& document, DeprecatedFlyString local_name, DeprecatedString value, Element const* owner_element) 17{ 18 return MUST_OR_THROW_OOM(document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), {}, {}), move(value), owner_element)); 19} 20 21WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Attr::create(Document& document, QualifiedName qualified_name, DeprecatedString value, Element const* owner_element) 22{ 23 return MUST_OR_THROW_OOM(document.heap().allocate<Attr>(document.realm(), document, move(qualified_name), move(value), owner_element)); 24} 25 26JS::NonnullGCPtr<Attr> Attr::clone(Document& document) 27{ 28 return *heap().allocate<Attr>(realm(), document, m_qualified_name, m_value, nullptr).release_allocated_value_but_fixme_should_propagate_errors(); 29} 30 31Attr::Attr(Document& document, QualifiedName qualified_name, DeprecatedString value, Element const* owner_element) 32 : Node(document, NodeType::ATTRIBUTE_NODE) 33 , m_qualified_name(move(qualified_name)) 34 , m_value(move(value)) 35 , m_owner_element(owner_element) 36{ 37} 38 39JS::ThrowCompletionOr<void> Attr::initialize(JS::Realm& realm) 40{ 41 MUST_OR_THROW_OOM(Base::initialize(realm)); 42 set_prototype(&Bindings::ensure_web_prototype<Bindings::AttrPrototype>(realm, "Attr")); 43 44 return {}; 45} 46 47void Attr::visit_edges(Cell::Visitor& visitor) 48{ 49 Base::visit_edges(visitor); 50 visitor.visit(m_owner_element.ptr()); 51} 52 53Element const* Attr::owner_element() const 54{ 55 return m_owner_element.ptr(); 56} 57 58void Attr::set_owner_element(Element const* owner_element) 59{ 60 m_owner_element = owner_element; 61} 62 63// https://dom.spec.whatwg.org/#set-an-existing-attribute-value 64void Attr::set_value(DeprecatedString value) 65{ 66 // 1. If attribute’s element is null, then set attribute’s value to value. 67 if (!owner_element()) { 68 m_value = move(value); 69 return; 70 } 71 72 // 2. Otherwise, change attribute to value. 73 // https://dom.spec.whatwg.org/#concept-element-attributes-change 74 // 1. Handle attribute changes for attribute with attribute’s element, attribute’s value, and value. 75 handle_attribute_changes(*owner_element(), m_value, value); 76 77 // 2. Set attribute’s value to value. 78 m_value = move(value); 79} 80 81// https://dom.spec.whatwg.org/#handle-attribute-changes 82void Attr::handle_attribute_changes(Element const& element, DeprecatedString const& old_value, [[maybe_unused]] DeprecatedString const& new_value) 83{ 84 // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null. 85 auto added_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors(); 86 auto removed_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors(); 87 element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, added_node_list, removed_node_list, nullptr, nullptr); 88 89 // FIXME: 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. 90 91 // FIXME: 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace. 92} 93 94}