Attr.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. JS_DEFINE_ALLOCATOR(Attr);
  16. JS::NonnullGCPtr<Attr> Attr::create(Document& document, FlyString local_name, String value, Element* owner_element)
  17. {
  18. return document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), Optional<FlyString> {}, Optional<FlyString> {}), move(value), owner_element);
  19. }
  20. JS::NonnullGCPtr<Attr> Attr::create(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
  21. {
  22. return document.heap().allocate<Attr>(document.realm(), document, move(qualified_name), move(value), owner_element);
  23. }
  24. JS::NonnullGCPtr<Attr> Attr::clone(Document& document)
  25. {
  26. return *heap().allocate<Attr>(realm(), document, m_qualified_name, m_value, nullptr);
  27. }
  28. Attr::Attr(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
  29. : Node(document, NodeType::ATTRIBUTE_NODE)
  30. , m_qualified_name(move(qualified_name))
  31. , m_value(move(value))
  32. , m_owner_element(owner_element)
  33. {
  34. }
  35. void Attr::initialize(JS::Realm& realm)
  36. {
  37. Base::initialize(realm);
  38. set_prototype(&Bindings::ensure_web_prototype<Bindings::AttrPrototype>(realm, "Attr"_fly_string));
  39. }
  40. void Attr::visit_edges(Cell::Visitor& visitor)
  41. {
  42. Base::visit_edges(visitor);
  43. visitor.visit(m_owner_element);
  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(String 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. }
  64. // 2. Otherwise, change attribute to value.
  65. else {
  66. change_attribute(move(value));
  67. }
  68. }
  69. // https://dom.spec.whatwg.org/#concept-element-attributes-change
  70. void Attr::change_attribute(String value)
  71. {
  72. // 1. Let oldValue be attribute’s value.
  73. auto old_value = move(m_value);
  74. // 2. Set attribute’s value to value.
  75. m_value = move(value);
  76. // 3. Handle attribute changes for attribute with attribute’s element, oldValue, and value.
  77. handle_attribute_changes(*owner_element(), old_value, m_value);
  78. }
  79. // https://dom.spec.whatwg.org/#handle-attribute-changes
  80. void Attr::handle_attribute_changes(Element& element, Optional<String> const& old_value, Optional<String> const& new_value)
  81. {
  82. // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
  83. element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, {}, {}, nullptr, nullptr);
  84. // 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.
  85. if (element.is_custom()) {
  86. auto& vm = this->vm();
  87. JS::MarkedVector<JS::Value> arguments { vm.heap() };
  88. arguments.append(JS::PrimitiveString::create(vm, local_name()));
  89. arguments.append(!old_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, old_value.value()));
  90. arguments.append(!new_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, new_value.value()));
  91. arguments.append(!namespace_uri().has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, namespace_uri().value()));
  92. element.enqueue_a_custom_element_callback_reaction(HTML::CustomElementReactionNames::attributeChangedCallback, move(arguments));
  93. }
  94. // 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
  95. element.run_attribute_change_steps(local_name(), old_value, new_value, namespace_uri());
  96. }
  97. }