MediaList.cpp 3.2 KB

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