NamedNodeMap.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::DOM {
  14. // https://dom.spec.whatwg.org/#interface-namednodemap
  15. class NamedNodeMap : public Bindings::PlatformObject {
  16. WEB_PLATFORM_OBJECT(NamedNodeMap, Bindings::PlatformObject);
  17. JS_DECLARE_ALLOCATOR(NamedNodeMap);
  18. public:
  19. [[nodiscard]] static JS::NonnullGCPtr<NamedNodeMap> create(Element&);
  20. ~NamedNodeMap() = default;
  21. virtual bool is_supported_property_index(u32 index) const override;
  22. virtual Vector<FlyString> supported_property_names() const override;
  23. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  24. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
  25. size_t length() const { return m_attributes.size(); }
  26. bool is_empty() const { return m_attributes.is_empty(); }
  27. // Methods defined by the spec for JavaScript:
  28. Attr const* item(u32 index) const;
  29. Attr const* get_named_item(FlyString const& qualified_name) const;
  30. Attr const* get_named_item_ns(Optional<FlyString> const& namespace_, FlyString const& local_name) const;
  31. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item(Attr& attribute);
  32. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item_ns(Attr& attribute);
  33. WebIDL::ExceptionOr<Attr const*> remove_named_item(FlyString const& qualified_name);
  34. WebIDL::ExceptionOr<Attr const*> remove_named_item_ns(Optional<FlyString> const& namespace_, FlyString const& local_name);
  35. // Methods defined by the spec for internal use:
  36. Attr* get_attribute(FlyString const& qualified_name, size_t* item_index = nullptr);
  37. Attr const* get_attribute(FlyString const& qualified_name, size_t* item_index = nullptr) const;
  38. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute(Attr& attribute);
  39. void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index);
  40. void append_attribute(Attr& attribute);
  41. Attr* get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index = nullptr);
  42. Attr const* get_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name, size_t* item_index = nullptr) const;
  43. Attr const* remove_attribute(FlyString const& qualified_name);
  44. Attr const* remove_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& local_name);
  45. Attr const* get_attribute_with_lowercase_qualified_name(FlyString const&) const;
  46. private:
  47. explicit NamedNodeMap(Element&);
  48. virtual void initialize(JS::Realm&) override;
  49. virtual void visit_edges(Cell::Visitor&) override;
  50. Element& associated_element() { return *m_element; }
  51. Element const& associated_element() const { return *m_element; }
  52. void remove_attribute_at_index(size_t attribute_index);
  53. JS::NonnullGCPtr<DOM::Element> m_element;
  54. Vector<JS::NonnullGCPtr<Attr>> m_attributes;
  55. };
  56. }