DOMTokenList.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/FlyString.h>
  10. #include <AK/Optional.h>
  11. #include <AK/String.h>
  12. #include <AK/StringView.h>
  13. #include <AK/Vector.h>
  14. #include <LibWeb/Bindings/PlatformObject.h>
  15. #include <LibWeb/Forward.h>
  16. #include <LibWeb/WebIDL/ExceptionOr.h>
  17. namespace Web::DOM {
  18. // https://dom.spec.whatwg.org/#domtokenlist
  19. class DOMTokenList final : public Bindings::PlatformObject {
  20. WEB_PLATFORM_OBJECT(DOMTokenList, Bindings::PlatformObject);
  21. JS_DECLARE_ALLOCATOR(DOMTokenList);
  22. public:
  23. [[nodiscard]] static JS::NonnullGCPtr<DOMTokenList> create(Element& associated_element, FlyString associated_attribute);
  24. ~DOMTokenList() = default;
  25. void associated_attribute_changed(StringView value);
  26. virtual bool is_supported_property_index(u32 index) const override;
  27. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  28. size_t length() const { return m_token_set.size(); }
  29. Optional<String> item(size_t index) const;
  30. bool contains(String const& token);
  31. WebIDL::ExceptionOr<void> add(Vector<String> const& tokens);
  32. WebIDL::ExceptionOr<void> remove(Vector<String> const& tokens);
  33. WebIDL::ExceptionOr<bool> toggle(String const& token, Optional<bool> force);
  34. WebIDL::ExceptionOr<bool> replace(String const& token, String const& new_token);
  35. WebIDL::ExceptionOr<bool> supports(StringView token);
  36. String value() const;
  37. void set_value(String const& value);
  38. private:
  39. DOMTokenList(Element& associated_element, FlyString associated_attribute);
  40. virtual void initialize(JS::Realm&) override;
  41. virtual void visit_edges(Cell::Visitor&) override;
  42. WebIDL::ExceptionOr<void> validate_token(StringView token) const;
  43. void run_update_steps();
  44. JS::NonnullGCPtr<Element> m_associated_element;
  45. FlyString m_associated_attribute;
  46. Vector<String> m_token_set;
  47. };
  48. }