Attr.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, FlyString local_name, String value, Element* owner_element)
  16. {
  17. return document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), Optional<FlyString> {}, Optional<FlyString> {}), move(value), owner_element);
  18. }
  19. JS::NonnullGCPtr<Attr> Attr::create(Document& document, QualifiedName qualified_name, String 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, String 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(String 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(String 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.to_deprecated_string(), m_value.to_deprecated_string());
  77. }
  78. // https://dom.spec.whatwg.org/#handle-attribute-changes
  79. void Attr::handle_attribute_changes(Element& element, Optional<DeprecatedString> old_value, Optional<DeprecatedString> new_value)
  80. {
  81. DeprecatedString deprecated_namespace_uri;
  82. if (namespace_uri().has_value())
  83. deprecated_namespace_uri = namespace_uri().value().to_deprecated_fly_string();
  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().to_deprecated_fly_string(), deprecated_namespace_uri, old_value, {}, {}, nullptr, nullptr);
  86. // 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.
  87. if (element.is_custom()) {
  88. auto& vm = this->vm();
  89. JS::MarkedVector<JS::Value> arguments { vm.heap() };
  90. arguments.append(JS::PrimitiveString::create(vm, local_name()));
  91. arguments.append(!old_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, old_value.release_value()));
  92. arguments.append(!new_value.has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, new_value.release_value()));
  93. arguments.append(!namespace_uri().has_value() ? JS::js_null() : JS::PrimitiveString::create(vm, namespace_uri().value()));
  94. element.enqueue_a_custom_element_callback_reaction(HTML::CustomElementReactionNames::attributeChangedCallback, move(arguments));
  95. }
  96. // 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
  97. element.run_attribute_change_steps(local_name(), old_value, new_value, deprecated_namespace_uri);
  98. }
  99. }