Attr.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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::NonnullGCPtr<Attr> Attr::create(Document& document, DeprecatedFlyString local_name, DeprecatedString value, Element* owner_element)
  16. {
  17. return document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), {}, {}), move(value), owner_element);
  18. }
  19. JS::NonnullGCPtr<Attr> Attr::create(Document& document, QualifiedName qualified_name, DeprecatedString value, Element* owner_element)
  20. {
  21. return 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);
  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. void Attr::initialize(JS::Realm& realm)
  35. {
  36. Base::initialize(realm);
  37. set_prototype(&Bindings::ensure_web_prototype<Bindings::AttrPrototype>(realm, "Attr"));
  38. }
  39. void Attr::visit_edges(Cell::Visitor& visitor)
  40. {
  41. Base::visit_edges(visitor);
  42. visitor.visit(m_owner_element.ptr());
  43. }
  44. Element* Attr::owner_element()
  45. {
  46. return m_owner_element.ptr();
  47. }
  48. Element const* Attr::owner_element() const
  49. {
  50. return m_owner_element.ptr();
  51. }
  52. void Attr::set_owner_element(Element* owner_element)
  53. {
  54. m_owner_element = owner_element;
  55. }
  56. // https://dom.spec.whatwg.org/#set-an-existing-attribute-value
  57. void Attr::set_value(DeprecatedString value)
  58. {
  59. // 1. If attribute’s element is null, then set attribute’s value to value.
  60. if (!owner_element()) {
  61. m_value = move(value);
  62. }
  63. // 2. Otherwise, change attribute to value.
  64. else {
  65. change_attribute(move(value));
  66. }
  67. }
  68. // https://dom.spec.whatwg.org/#concept-element-attributes-change
  69. void Attr::change_attribute(DeprecatedString value)
  70. {
  71. // 1. Let oldValue be attribute’s value.
  72. auto old_value = move(m_value);
  73. // 2. Set attribute’s value to value.
  74. m_value = move(value);
  75. // 3. Handle attribute changes for attribute with attribute’s element, oldValue, and value.
  76. handle_attribute_changes(*owner_element(), old_value, m_value);
  77. }
  78. // https://dom.spec.whatwg.org/#handle-attribute-changes
  79. void Attr::handle_attribute_changes(Element& element, DeprecatedString const& old_value, DeprecatedString const& new_value)
  80. {
  81. // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
  82. element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, {}, {}, nullptr, nullptr);
  83. // 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.
  84. if (element.is_custom()) {
  85. auto& vm = this->vm();
  86. JS::MarkedVector<JS::Value> arguments { vm.heap() };
  87. arguments.append(JS::PrimitiveString::create(vm, local_name()));
  88. arguments.append(old_value.is_null() ? JS::js_null() : JS::PrimitiveString::create(vm, old_value));
  89. arguments.append(new_value.is_null() ? JS::js_null() : JS::PrimitiveString::create(vm, new_value));
  90. arguments.append(namespace_uri().is_null() ? JS::js_null() : JS::PrimitiveString::create(vm, namespace_uri()));
  91. element.enqueue_a_custom_element_callback_reaction(HTML::CustomElementReactionNames::attributeChangedCallback, move(arguments));
  92. }
  93. // 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
  94. element.run_attribute_change_steps(local_name(), old_value, new_value, namespace_uri());
  95. }
  96. }