Attribute.cpp 2.8 KB

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