NamedNodeMap.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
  5. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <AK/DeprecatedString.h>
  11. #include <AK/StringView.h>
  12. #include <LibWeb/Bindings/LegacyPlatformObject.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. namespace Web::DOM {
  16. // https://dom.spec.whatwg.org/#interface-namednodemap
  17. class NamedNodeMap : public Bindings::LegacyPlatformObject {
  18. WEB_PLATFORM_OBJECT(NamedNodeMap, Bindings::LegacyPlatformObject);
  19. public:
  20. static WebIDL::ExceptionOr<JS::NonnullGCPtr<NamedNodeMap>> create(Element&);
  21. ~NamedNodeMap() = default;
  22. virtual bool is_supported_property_index(u32 index) const override;
  23. virtual Vector<DeprecatedString> supported_property_names() const override;
  24. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  25. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(DeprecatedFlyString const& name) const override;
  26. size_t length() const { return m_attributes.size(); }
  27. bool is_empty() const { return m_attributes.is_empty(); }
  28. // Methods defined by the spec for JavaScript:
  29. Attr const* item(u32 index) const;
  30. Attr const* get_named_item(StringView qualified_name) const;
  31. Attr const* get_named_item_ns(StringView namespace_, StringView local_name) const;
  32. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item(Attr& attribute);
  33. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item_ns(Attr& attribute);
  34. WebIDL::ExceptionOr<Attr const*> remove_named_item(StringView qualified_name);
  35. WebIDL::ExceptionOr<Attr const*> remove_named_item_ns(StringView namespace_, StringView local_name);
  36. // Methods defined by the spec for internal use:
  37. Attr* get_attribute(StringView qualified_name, size_t* item_index = nullptr);
  38. Attr* get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index = nullptr);
  39. Attr const* get_attribute(StringView qualified_name, size_t* item_index = nullptr) const;
  40. Attr const* get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index = nullptr) const;
  41. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute(Attr& attribute);
  42. void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index);
  43. void append_attribute(Attr& attribute);
  44. Attr const* remove_attribute(StringView qualified_name);
  45. Attr const* remove_attribute_ns(StringView namespace_, StringView local_name);
  46. private:
  47. explicit NamedNodeMap(Element&);
  48. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  49. virtual void visit_edges(Cell::Visitor&) override;
  50. // ^Bindings::LegacyPlatformObject
  51. virtual bool supports_indexed_properties() const override { return true; }
  52. virtual bool supports_named_properties() const override { return true; }
  53. virtual bool has_indexed_property_setter() const override { return false; }
  54. virtual bool has_named_property_setter() const override { return false; }
  55. virtual bool has_named_property_deleter() const override { return false; }
  56. virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
  57. virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return true; }
  58. virtual bool has_global_interface_extended_attribute() const override { return false; }
  59. virtual bool indexed_property_setter_has_identifier() const override { return false; }
  60. virtual bool named_property_setter_has_identifier() const override { return false; }
  61. virtual bool named_property_deleter_has_identifier() const override { return false; }
  62. Element& associated_element() { return *m_element; }
  63. Element const& associated_element() const { return *m_element; }
  64. void remove_attribute_at_index(size_t attribute_index);
  65. JS::NonnullGCPtr<DOM::Element> m_element;
  66. Vector<JS::NonnullGCPtr<Attr>> m_attributes;
  67. };
  68. }