MediaList.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/MediaList.h>
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. namespace Web::CSS {
  9. MediaList::MediaList(NonnullRefPtrVector<MediaQuery>&& media)
  10. : m_media(move(media))
  11. {
  12. }
  13. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  14. String MediaList::media_text() const
  15. {
  16. return serialize_a_media_query_list(m_media);
  17. }
  18. // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
  19. void MediaList::set_media_text(String const& text)
  20. {
  21. m_media.clear();
  22. if (text.is_empty())
  23. return;
  24. m_media = parse_media_query_list({}, text);
  25. }
  26. bool MediaList::is_supported_property_index(u32 index) const
  27. {
  28. return index < length();
  29. }
  30. // https://www.w3.org/TR/cssom-1/#dom-medialist-item
  31. String MediaList::item(u32 index) const
  32. {
  33. if (!is_supported_property_index(index))
  34. return {};
  35. return m_media[index].to_string();
  36. }
  37. // https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
  38. void MediaList::append_medium(String medium)
  39. {
  40. auto m = parse_media_query({}, medium);
  41. if (!m)
  42. return;
  43. if (m_media.contains_slow(*m))
  44. return;
  45. m_media.append(m.release_nonnull());
  46. }
  47. // https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
  48. void MediaList::delete_medium(String medium)
  49. {
  50. auto m = parse_media_query({}, medium);
  51. if (!m)
  52. return;
  53. m_media.remove_all_matching([&](auto& existing) -> bool {
  54. return m->to_string() == existing->to_string();
  55. });
  56. // FIXME: If nothing was removed, then throw a NotFoundError exception.
  57. }
  58. bool MediaList::evaluate(HTML::Window const& window)
  59. {
  60. for (auto& media : m_media)
  61. media.evaluate(window);
  62. return matches();
  63. }
  64. bool MediaList::matches() const
  65. {
  66. for (auto& media : m_media) {
  67. if (media.matches())
  68. return true;
  69. }
  70. return false;
  71. }
  72. }