MediaList.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.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. // https://www.w3.org/TR/cssom-1/#dom-medialist-item
  43. Optional<String> MediaList::item(u32 index) const
  44. {
  45. if (index >= m_media.size())
  46. return {};
  47. return m_media[index]->to_string();
  48. }
  49. // https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
  50. void MediaList::append_medium(StringView medium)
  51. {
  52. // 1. Let m be the result of parsing the given value.
  53. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  54. // 2. If m is null, then return.
  55. if (!m)
  56. return;
  57. // 3. If comparing m with any of the media queries in the collection of media queries returns true, then return.
  58. auto serialized = m->to_string();
  59. for (auto& existing_medium : m_media) {
  60. if (existing_medium->to_string() == serialized)
  61. return;
  62. }
  63. // 4. Append m to the collection of media queries.
  64. m_media.append(m.release_nonnull());
  65. }
  66. // https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
  67. void MediaList::delete_medium(StringView medium)
  68. {
  69. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  70. if (!m)
  71. return;
  72. m_media.remove_all_matching([&](auto& existing) -> bool {
  73. return m->to_string() == existing->to_string();
  74. });
  75. // FIXME: If nothing was removed, then throw a NotFoundError exception.
  76. }
  77. bool MediaList::evaluate(HTML::Window const& window)
  78. {
  79. for (auto& media : m_media)
  80. media->evaluate(window);
  81. return matches();
  82. }
  83. bool MediaList::matches() const
  84. {
  85. if (m_media.is_empty())
  86. return true;
  87. for (auto& media : m_media) {
  88. if (media->matches())
  89. return true;
  90. }
  91. return false;
  92. }
  93. Optional<JS::Value> MediaList::item_value(size_t index) const
  94. {
  95. if (index >= m_media.size())
  96. return {};
  97. return JS::PrimitiveString::create(vm(), m_media[index]->to_string());
  98. }
  99. }