NamedNodeMap.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/NonnullRefPtrVector.h>
  9. #include <AK/String.h>
  10. #include <AK/StringView.h>
  11. #include <LibWeb/Bindings/LegacyPlatformObject.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::DOM {
  15. // https://dom.spec.whatwg.org/#interface-namednodemap
  16. class NamedNodeMap : public Bindings::LegacyPlatformObject {
  17. WEB_PLATFORM_OBJECT(NamedNodeMap, Bindings::LegacyPlatformObject);
  18. public:
  19. static JS::NonnullGCPtr<NamedNodeMap> create(Element&);
  20. ~NamedNodeMap() = default;
  21. virtual bool is_supported_property_index(u32 index) const override;
  22. virtual Vector<String> supported_property_names() const override;
  23. virtual JS::Value item_value(size_t index) const override;
  24. virtual 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(StringView qualified_name) const;
  30. WebIDL::ExceptionOr<Attr const*> set_named_item(Attr& attribute);
  31. WebIDL::ExceptionOr<Attr const*> remove_named_item(StringView qualified_name);
  32. // Methods defined by the spec for internal use:
  33. Attr* get_attribute(StringView qualified_name, size_t* item_index = nullptr);
  34. Attr const* get_attribute(StringView qualified_name, size_t* item_index = nullptr) const;
  35. WebIDL::ExceptionOr<Attr const*> set_attribute(Attr& attribute);
  36. void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index);
  37. void append_attribute(Attr& attribute);
  38. Attr const* remove_attribute(StringView qualified_name);
  39. private:
  40. explicit NamedNodeMap(Element&);
  41. virtual void visit_edges(Cell::Visitor&) override;
  42. Element& associated_element() { return *m_element; }
  43. Element const& associated_element() const { return *m_element; }
  44. void remove_attribute_at_index(size_t attribute_index);
  45. JS::NonnullGCPtr<DOM::Element> m_element;
  46. Vector<JS::NonnullGCPtr<Attr>> m_attributes;
  47. };
  48. }