Attr.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Attr.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/DOM/MutationType.h>
  11. #include <LibWeb/DOM/StaticNodeList.h>
  12. namespace Web::DOM {
  13. JS::NonnullGCPtr<Attr> Attr::create(Document& document, FlyString local_name, DeprecatedString value, Element const* owner_element)
  14. {
  15. return *document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), {}, {}), move(value), owner_element);
  16. }
  17. JS::NonnullGCPtr<Attr> Attr::clone(Document& document)
  18. {
  19. return *heap().allocate<Attr>(realm(), document, m_qualified_name, m_value, nullptr);
  20. }
  21. Attr::Attr(Document& document, QualifiedName qualified_name, DeprecatedString value, Element const* owner_element)
  22. : Node(document, NodeType::ATTRIBUTE_NODE)
  23. , m_qualified_name(move(qualified_name))
  24. , m_value(move(value))
  25. , m_owner_element(owner_element)
  26. {
  27. set_prototype(&Bindings::cached_web_prototype(document.realm(), "Attr"));
  28. }
  29. void Attr::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_owner_element.ptr());
  33. }
  34. Element* Attr::owner_element()
  35. {
  36. return m_owner_element.ptr();
  37. }
  38. Element const* Attr::owner_element() const
  39. {
  40. return m_owner_element.ptr();
  41. }
  42. void Attr::set_owner_element(Element const* owner_element)
  43. {
  44. m_owner_element = owner_element;
  45. }
  46. // https://dom.spec.whatwg.org/#set-an-existing-attribute-value
  47. void Attr::set_value(DeprecatedString value)
  48. {
  49. // 1. If attribute’s element is null, then set attribute’s value to value.
  50. if (!owner_element()) {
  51. m_value = move(value);
  52. return;
  53. }
  54. // 2. Otherwise, change attribute to value.
  55. // https://dom.spec.whatwg.org/#concept-element-attributes-change
  56. // 1. Handle attribute changes for attribute with attribute’s element, attribute’s value, and value.
  57. handle_attribute_changes(*owner_element(), m_value, value);
  58. // 2. Set attribute’s value to value.
  59. m_value = move(value);
  60. }
  61. // https://dom.spec.whatwg.org/#handle-attribute-changes
  62. void Attr::handle_attribute_changes(Element& element, DeprecatedString const& old_value, [[maybe_unused]] DeprecatedString const& new_value)
  63. {
  64. // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
  65. element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, StaticNodeList::create(realm(), {}), StaticNodeList::create(realm(), {}), nullptr, nullptr);
  66. // FIXME: 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.
  67. // FIXME: 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
  68. }
  69. }