DOMTokenList.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/FlyString.h>
  9. #include <AK/Optional.h>
  10. #include <AK/String.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Vector.h>
  13. #include <LibWeb/Bindings/LegacyPlatformObject.h>
  14. #include <LibWeb/Forward.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. namespace Web::DOM {
  17. // https://dom.spec.whatwg.org/#domtokenlist
  18. class DOMTokenList final : public Bindings::LegacyPlatformObject {
  19. WEB_PLATFORM_OBJECT(DOMTokenList, Bindings::LegacyPlatformObject);
  20. public:
  21. static DOMTokenList* create(Element const& associated_element, FlyString associated_attribute);
  22. ~DOMTokenList() = default;
  23. void associated_attribute_changed(StringView value);
  24. virtual bool is_supported_property_index(u32 index) const override;
  25. virtual JS::Value item_value(size_t index) const override;
  26. size_t length() const { return m_token_set.size(); }
  27. String const& item(size_t index) const;
  28. bool contains(StringView token);
  29. WebIDL::ExceptionOr<void> add(Vector<String> const& tokens);
  30. WebIDL::ExceptionOr<void> remove(Vector<String> const& tokens);
  31. WebIDL::ExceptionOr<bool> toggle(String const& token, Optional<bool> force);
  32. WebIDL::ExceptionOr<bool> replace(String const& token, String const& new_token);
  33. WebIDL::ExceptionOr<bool> supports(StringView token);
  34. String value() const;
  35. void set_value(String value);
  36. private:
  37. DOMTokenList(Element const& associated_element, FlyString associated_attribute);
  38. WebIDL::ExceptionOr<void> validate_token(StringView token) const;
  39. void run_update_steps();
  40. WeakPtr<Element> m_associated_element;
  41. FlyString m_associated_attribute;
  42. Vector<String> m_token_set;
  43. };
  44. }