DOMTokenList.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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 Optional<JS::Value> item_value(size_t index) const override;
  27. size_t length() const { return m_token_set.size(); }
  28. Optional<String> item(size_t index) const;
  29. bool contains(String const& token);
  30. WebIDL::ExceptionOr<void> add(Vector<String> const& tokens);
  31. WebIDL::ExceptionOr<void> remove(Vector<String> const& tokens);
  32. WebIDL::ExceptionOr<bool> toggle(String const& token, Optional<bool> force);
  33. WebIDL::ExceptionOr<bool> replace(String const& token, String const& new_token);
  34. WebIDL::ExceptionOr<bool> supports(StringView token);
  35. String value() const;
  36. void set_value(String const& value);
  37. private:
  38. DOMTokenList(Element& associated_element, FlyString associated_attribute);
  39. virtual void initialize(JS::Realm&) override;
  40. virtual void visit_edges(Cell::Visitor&) override;
  41. WebIDL::ExceptionOr<void> validate_token(StringView token) const;
  42. WebIDL::ExceptionOr<void> validate_token_not_empty(StringView token) const;
  43. WebIDL::ExceptionOr<void> validate_token_not_whitespace(StringView token) const;
  44. void run_update_steps();
  45. String serialize_ordered_set() const;
  46. JS::NonnullGCPtr<Element> m_associated_element;
  47. FlyString m_associated_attribute;
  48. Vector<String> m_token_set;
  49. };
  50. }