Attr.cpp 3.4 KB

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