Attribute.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. NonnullRefPtr<Attribute> Attribute::create(Document& document, FlyString local_name, String value, Element const* owner_element)
  13. {
  14. return adopt_ref(*new Attribute(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. }
  23. Element* Attribute::owner_element()
  24. {
  25. return m_owner_element;
  26. }
  27. Element const* Attribute::owner_element() const
  28. {
  29. return m_owner_element;
  30. }
  31. void Attribute::set_owner_element(Element const* owner_element)
  32. {
  33. m_owner_element = owner_element;
  34. }
  35. // https://dom.spec.whatwg.org/#set-an-existing-attribute-value
  36. void Attribute::set_value(String value)
  37. {
  38. // 1. If attribute’s element is null, then set attribute’s value to value.
  39. if (!owner_element()) {
  40. m_value = move(value);
  41. return;
  42. }
  43. // 2. Otherwise, change attribute to value.
  44. // https://dom.spec.whatwg.org/#concept-element-attributes-change
  45. // 1. Handle attribute changes for attribute with attribute’s element, attribute’s value, and value.
  46. handle_attribute_changes(*owner_element(), m_value, value);
  47. // 2. Set attribute’s value to value.
  48. m_value = move(value);
  49. }
  50. // https://dom.spec.whatwg.org/#handle-attribute-changes
  51. void Attribute::handle_attribute_changes(Element& element, String const& old_value, [[maybe_unused]] String const& new_value)
  52. {
  53. // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null.
  54. element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, StaticNodeList::create({}), StaticNodeList::create({}), nullptr, nullptr);
  55. // 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.
  56. // FIXME: 3. Run the attribute change steps with element, attribute’s local name, oldValue, newValue, and attribute’s namespace.
  57. }
  58. }