MediaList.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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::NonnullGCPtr<MediaList> MediaList::create(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media)
  14. {
  15. return realm.heap().allocate<MediaList>(realm, realm, move(media));
  16. }
  17. MediaList::MediaList(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media)
  18. : Bindings::LegacyPlatformObject(realm)
  19. , m_media(move(media))
  20. {
  21. }
  22. void MediaList::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::MediaListPrototype>(realm, "MediaList"));
  26. }
  27. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  28. DeprecatedString MediaList::media_text() const
  29. {
  30. return serialize_a_media_query_list(m_media).to_deprecated_string();
  31. }
  32. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  33. void MediaList::set_media_text(DeprecatedString const& text)
  34. {
  35. m_media.clear();
  36. if (text.is_empty())
  37. return;
  38. m_media = parse_media_query_list(Parser::ParsingContext { realm() }, text);
  39. }
  40. bool MediaList::is_supported_property_index(u32 index) const
  41. {
  42. return index < length();
  43. }
  44. // https://www.w3.org/TR/cssom-1/#dom-medialist-item
  45. DeprecatedString MediaList::item(u32 index) const
  46. {
  47. if (!is_supported_property_index(index))
  48. return {};
  49. return m_media[index]->to_string().to_deprecated_string();
  50. }
  51. // https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
  52. void MediaList::append_medium(DeprecatedString medium)
  53. {
  54. // 1. Let m be the result of parsing the given value.
  55. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  56. // 2. If m is null, then return.
  57. if (!m)
  58. return;
  59. // 3. If comparing m with any of the media queries in the collection of media queries returns true, then return.
  60. auto serialized = m->to_string();
  61. for (auto& existing_medium : m_media) {
  62. if (existing_medium->to_string() == serialized)
  63. return;
  64. }
  65. // 4. Append m to the collection of media queries.
  66. m_media.append(m.release_nonnull());
  67. }
  68. // https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
  69. void MediaList::delete_medium(DeprecatedString medium)
  70. {
  71. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  72. if (!m)
  73. return;
  74. m_media.remove_all_matching([&](auto& existing) -> bool {
  75. return m->to_string() == existing->to_string();
  76. });
  77. // FIXME: If nothing was removed, then throw a NotFoundError exception.
  78. }
  79. bool MediaList::evaluate(HTML::Window const& window)
  80. {
  81. for (auto& media : m_media)
  82. media->evaluate(window);
  83. return matches();
  84. }
  85. bool MediaList::matches() const
  86. {
  87. if (m_media.is_empty()) {
  88. return true;
  89. }
  90. for (auto& media : m_media) {
  91. if (media->matches())
  92. return true;
  93. }
  94. return false;
  95. }
  96. WebIDL::ExceptionOr<JS::Value> MediaList::item_value(size_t index) const
  97. {
  98. if (index >= m_media.size())
  99. return JS::js_undefined();
  100. return JS::PrimitiveString::create(vm(), m_media[index]->to_string().to_deprecated_string());
  101. }
  102. }