DOMTokenList.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/Optional.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/String.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Vector.h>
  13. #include <LibWeb/Bindings/Wrappable.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
  19. : public RefCounted<DOMTokenList>
  20. , public Bindings::Wrappable {
  21. public:
  22. using WrapperType = Bindings::DOMTokenListWrapper;
  23. static NonnullRefPtr<DOMTokenList> create(Element const& associated_element, FlyString associated_attribute);
  24. ~DOMTokenList() = default;
  25. void associated_attribute_changed(StringView value);
  26. bool is_supported_property_index(u32 index) const;
  27. size_t length() const { return m_token_set.size(); }
  28. String const& item(size_t index) const;
  29. bool contains(StringView token);
  30. ExceptionOr<void> add(Vector<String> const& tokens);
  31. ExceptionOr<void> remove(Vector<String> const& tokens);
  32. ExceptionOr<bool> toggle(String const& token, Optional<bool> force);
  33. ExceptionOr<bool> replace(String const& token, String const& new_token);
  34. ExceptionOr<bool> supports(StringView token);
  35. String value() const;
  36. void set_value(String value);
  37. private:
  38. DOMTokenList(Element const& associated_element, FlyString associated_attribute);
  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. DOMTokenListWrapper* wrap(JS::GlobalObject&, DOM::DOMTokenList&);
  48. }