Attr.cpp 4.5 KB

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