Attr.cpp 2.8 KB

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