Attr.cpp 4.4 KB

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