MediaList.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/MediaListPrototype.h>
  9. #include <LibWeb/CSS/MediaList.h>
  10. #include <LibWeb/CSS/Parser/Parser.h>
  11. #include <LibWeb/WebIDL/ExceptionOr.h>
  12. namespace Web::CSS {
  13. JS_DEFINE_ALLOCATOR(MediaList);
  14. JS::NonnullGCPtr<MediaList> MediaList::create(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media)
  15. {
  16. return realm.heap().allocate<MediaList>(realm, realm, move(media));
  17. }
  18. MediaList::MediaList(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media)
  19. : Bindings::PlatformObject(realm)
  20. , m_media(move(media))
  21. {
  22. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = true };
  23. }
  24. void MediaList::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(MediaList);
  28. }
  29. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  30. String MediaList::media_text() const
  31. {
  32. return serialize_a_media_query_list(m_media);
  33. }
  34. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  35. void MediaList::set_media_text(StringView text)
  36. {
  37. m_media.clear();
  38. if (text.is_empty())
  39. return;
  40. m_media = parse_media_query_list(Parser::ParsingContext { realm() }, text);
  41. }
  42. bool MediaList::is_supported_property_index(u32 index) const
  43. {
  44. return index < length();
  45. }
  46. // https://www.w3.org/TR/cssom-1/#dom-medialist-item
  47. Optional<String> MediaList::item(u32 index) const
  48. {
  49. if (!is_supported_property_index(index))
  50. return {};
  51. return m_media[index]->to_string();
  52. }
  53. // https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
  54. void MediaList::append_medium(StringView medium)
  55. {
  56. // 1. Let m be the result of parsing the given value.
  57. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  58. // 2. If m is null, then return.
  59. if (!m)
  60. return;
  61. // 3. If comparing m with any of the media queries in the collection of media queries returns true, then return.
  62. auto serialized = m->to_string();
  63. for (auto& existing_medium : m_media) {
  64. if (existing_medium->to_string() == serialized)
  65. return;
  66. }
  67. // 4. Append m to the collection of media queries.
  68. m_media.append(m.release_nonnull());
  69. }
  70. // https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
  71. void MediaList::delete_medium(StringView medium)
  72. {
  73. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  74. if (!m)
  75. return;
  76. m_media.remove_all_matching([&](auto& existing) -> bool {
  77. return m->to_string() == existing->to_string();
  78. });
  79. // FIXME: If nothing was removed, then throw a NotFoundError exception.
  80. }
  81. bool MediaList::evaluate(HTML::Window const& window)
  82. {
  83. for (auto& media : m_media)
  84. media->evaluate(window);
  85. return matches();
  86. }
  87. bool MediaList::matches() const
  88. {
  89. if (m_media.is_empty()) {
  90. return true;
  91. }
  92. for (auto& media : m_media) {
  93. if (media->matches())
  94. return true;
  95. }
  96. return false;
  97. }
  98. WebIDL::ExceptionOr<JS::Value> MediaList::item_value(size_t index) const
  99. {
  100. if (index >= m_media.size())
  101. return JS::js_undefined();
  102. return JS::PrimitiveString::create(vm(), m_media[index]->to_string());
  103. }
  104. }