MediaList.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, 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. namespace Web::CSS {
  12. MediaList* MediaList::create(JS::Realm& realm, NonnullRefPtrVector<MediaQuery>&& media)
  13. {
  14. return realm.heap().allocate<MediaList>(realm, realm, move(media));
  15. }
  16. MediaList::MediaList(JS::Realm& realm, NonnullRefPtrVector<MediaQuery>&& media)
  17. : Bindings::LegacyPlatformObject(realm)
  18. , m_media(move(media))
  19. {
  20. }
  21. void MediaList::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::MediaListPrototype>(realm, "MediaList"));
  25. }
  26. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  27. DeprecatedString MediaList::media_text() const
  28. {
  29. return serialize_a_media_query_list(m_media);
  30. }
  31. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  32. void MediaList::set_media_text(DeprecatedString const& text)
  33. {
  34. m_media.clear();
  35. if (text.is_empty())
  36. return;
  37. m_media = parse_media_query_list({}, text);
  38. }
  39. bool MediaList::is_supported_property_index(u32 index) const
  40. {
  41. return index < length();
  42. }
  43. // https://www.w3.org/TR/cssom-1/#dom-medialist-item
  44. DeprecatedString MediaList::item(u32 index) const
  45. {
  46. if (!is_supported_property_index(index))
  47. return {};
  48. return m_media[index].to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
  49. }
  50. // https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
  51. void MediaList::append_medium(DeprecatedString medium)
  52. {
  53. auto m = parse_media_query({}, medium);
  54. if (!m)
  55. return;
  56. if (m_media.contains_slow(*m))
  57. return;
  58. m_media.append(m.release_nonnull());
  59. }
  60. // https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
  61. void MediaList::delete_medium(DeprecatedString medium)
  62. {
  63. auto m = parse_media_query({}, medium);
  64. if (!m)
  65. return;
  66. m_media.remove_all_matching([&](auto& existing) -> bool {
  67. return m->to_string().release_value_but_fixme_should_propagate_errors() == existing->to_string().release_value_but_fixme_should_propagate_errors();
  68. });
  69. // FIXME: If nothing was removed, then throw a NotFoundError exception.
  70. }
  71. bool MediaList::evaluate(HTML::Window const& window)
  72. {
  73. for (auto& media : m_media)
  74. media.evaluate(window);
  75. return matches();
  76. }
  77. bool MediaList::matches() const
  78. {
  79. if (m_media.is_empty()) {
  80. return true;
  81. }
  82. for (auto& media : m_media) {
  83. if (media.matches())
  84. return true;
  85. }
  86. return false;
  87. }
  88. JS::Value MediaList::item_value(size_t index) const
  89. {
  90. if (index >= m_media.size())
  91. return JS::js_undefined();
  92. return JS::PrimitiveString::create(vm(), m_media[index].to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string());
  93. }
  94. }