MediaList.cpp 2.5 KB

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