Attr.cpp 3.7 KB

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