DOMTokenList.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/DeprecatedFlyString.h>
  10. #include <AK/DeprecatedString.h>
  11. #include <AK/Optional.h>
  12. #include <AK/StringView.h>
  13. #include <AK/Vector.h>
  14. #include <LibWeb/Bindings/LegacyPlatformObject.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::LegacyPlatformObject {
  20. WEB_PLATFORM_OBJECT(DOMTokenList, Bindings::LegacyPlatformObject);
  21. public:
  22. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMTokenList>> create(Element& associated_element, DeprecatedFlyString associated_attribute);
  23. ~DOMTokenList() = default;
  24. void associated_attribute_changed(StringView value);
  25. virtual bool is_supported_property_index(u32 index) const override;
  26. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  27. size_t length() const { return m_token_set.size(); }
  28. DeprecatedString const& item(size_t index) const;
  29. bool contains(StringView token);
  30. WebIDL::ExceptionOr<void> add(Vector<DeprecatedString> const& tokens);
  31. WebIDL::ExceptionOr<void> remove(Vector<DeprecatedString> const& tokens);
  32. WebIDL::ExceptionOr<bool> toggle(DeprecatedString const& token, Optional<bool> force);
  33. WebIDL::ExceptionOr<bool> replace(DeprecatedString const& token, DeprecatedString const& new_token);
  34. WebIDL::ExceptionOr<bool> supports(StringView token);
  35. DeprecatedString value() const;
  36. void set_value(DeprecatedString value);
  37. private:
  38. DOMTokenList(Element& associated_element, DeprecatedFlyString associated_attribute);
  39. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  40. virtual void visit_edges(Cell::Visitor&) override;
  41. // ^Bindings::LegacyPlatformObject
  42. virtual bool supports_indexed_properties() const override { return true; }
  43. virtual bool supports_named_properties() const override { return false; }
  44. virtual bool has_indexed_property_setter() const override { return false; }
  45. virtual bool has_named_property_setter() const override { return false; }
  46. virtual bool has_named_property_deleter() const override { return false; }
  47. virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
  48. virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return false; }
  49. virtual bool has_global_interface_extended_attribute() const override { return false; }
  50. virtual bool indexed_property_setter_has_identifier() const override { return false; }
  51. virtual bool named_property_setter_has_identifier() const override { return false; }
  52. virtual bool named_property_deleter_has_identifier() const override { return false; }
  53. WebIDL::ExceptionOr<void> validate_token(StringView token) const;
  54. void run_update_steps();
  55. JS::NonnullGCPtr<Element> m_associated_element;
  56. DeprecatedFlyString m_associated_attribute;
  57. Vector<DeprecatedString> m_token_set;
  58. };
  59. }