Attr.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/AttrPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Attr.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Element.h>
  12. #include <LibWeb/DOM/MutationType.h>
  13. #include <LibWeb/DOM/StaticNodeList.h>
  14. #include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
  15. namespace Web::DOM {
  16. GC_DEFINE_ALLOCATOR(Attr);
  17. GC::Ref<Attr> Attr::create(Document& document, FlyString local_name, String value, Element* owner_element)
  18. {
  19. return document.realm().create<Attr>(document, QualifiedName(move(local_name), Optional<FlyString> {}, Optional<FlyString> {}), move(value), owner_element);
  20. }
  21. GC::Ref<Attr> Attr::create(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
  22. {
  23. return document.realm().create<Attr>(document, move(qualified_name), move(value), owner_element);
  24. }
  25. GC::Ref<Attr> Attr::clone(Document& document)
  26. {
  27. return realm().create<Attr>(document, m_qualified_name, m_value, nullptr);
  28. }
  29. Attr::Attr(Document& document, QualifiedName qualified_name, String value, Element* owner_element)
  30. : Node(document, NodeType::ATTRIBUTE_NODE)
  31. , m_qualified_name(move(qualified_name))
  32. , m_lowercase_name(MUST(String(m_qualified_name.as_string()).to_lowercase()))
  33. , m_value(move(value))
  34. , m_owner_element(owner_element)
  35. {
  36. }
  37. void Attr::initialize(JS::Realm& realm)
  38. {
  39. Base::initialize(realm);
  40. WEB_SET_PROTOTYPE_FOR_INTERFACE(Attr);
  41. }
  42. void Attr::visit_edges(Cell::Visitor& visitor)
  43. {
  44. Base::visit_edges(visitor);
  45. visitor.visit(m_owner_element);
  46. }
  47. Element* Attr::owner_element()
  48. {
  49. return m_owner_element.ptr();
  50. }
  51. Element const* Attr::owner_element() const
  52. {
  53. return m_owner_element.ptr();
  54. }
  55. void Attr::set_owner_element(Element* owner_element)
  56. {
  57. m_owner_element = owner_element;
  58. }
  59. // https://dom.spec.whatwg.org/#set-an-existing-attribute-value
  60. void Attr::set_value(String value)
  61. {
  62. // 1. If attribute’s element is null, then set attribute’s value to value.
  63. if (!owner_element()) {
  64. m_value = move(value);
  65. }
  66. // 2. Otherwise, change attribute to value.
  67. else {
  68. change_attribute(move(value));
  69. }
  70. }
  71. // https://dom.spec.whatwg.org/#concept-element-attributes-change
  72. void Attr::change_attribute(String value)
  73. {
  74. // 1. Let oldValue be attribute’s value.
  75. auto old_value = move(m_value);
  76. // 2. Set attribute’s value to value.
  77. m_value = move(value);
  78. // 3. Handle attribute changes for attribute with attribute’s element, oldValue, and value.
  79. handle_attribute_changes(*owner_element(), old_value, m_value);
  80. }
  81. // https://dom.spec.whatwg.org/#handle-attribute-changes
  82. void Attr::handle_attribute_changes(Element& element, Optional<String> const& old_value, Optional<String> const& new_value)
  83. {
  84. // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
  85. element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, {}, {}, nullptr, nullptr);
  86. // 2. If element is custom, then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback",
  87. // and « attribute’s local name, oldValue, newValue, attribute’s namespace ».
  88. if (element.is_custom()) {
  89. auto& vm = this->vm();
  90. GC::RootVector<JS::Value> arguments { vm.heap() };
  91. arguments.append(JS::PrimitiveString::create(vm, local_name()));
  92. arguments.append(!old_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, old_value.value()));
  93. arguments.append(!new_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, new_value.value()));
  94. arguments.append(!namespace_uri().has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, namespace_uri().value()));
  95. element.enqueue_a_custom_element_callback_reaction(HTML::CustomElementReactionNames::attributeChangedCallback, move(arguments));
  96. }
  97. // 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
  98. element.run_attribute_change_steps(local_name(), old_value, new_value, namespace_uri());
  99. }
  100. }