DOMTokenList.h 1.9 KB

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