MediaList.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2021, 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. WebIDL::ExceptionOr<JS::NonnullGCPtr<MediaList>> MediaList::create(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media)
  14. {
  15. return MUST_OR_THROW_OOM(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. 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(Parser::ParsingContext { realm() }, 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. // 1. Let m be the result of parsing the given value.
  56. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  57. // 2. If m is null, then return.
  58. if (!m)
  59. return;
  60. // 3. If comparing m with any of the media queries in the collection of media queries returns true, then return.
  61. auto serialized = m->to_string().release_value_but_fixme_should_propagate_errors();
  62. for (auto& existing_medium : m_media) {
  63. if (existing_medium->to_string().release_value_but_fixme_should_propagate_errors() == serialized)
  64. return;
  65. }
  66. // 4. Append m to the collection of media queries.
  67. m_media.append(m.release_nonnull());
  68. }
  69. // https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
  70. void MediaList::delete_medium(DeprecatedString medium)
  71. {
  72. auto m = parse_media_query(Parser::ParsingContext { realm() }, medium);
  73. if (!m)
  74. return;
  75. m_media.remove_all_matching([&](auto& existing) -> bool {
  76. return m->to_string().release_value_but_fixme_should_propagate_errors() == existing->to_string().release_value_but_fixme_should_propagate_errors();
  77. });
  78. // FIXME: If nothing was removed, then throw a NotFoundError exception.
  79. }
  80. bool MediaList::evaluate(HTML::Window const& window)
  81. {
  82. for (auto& media : m_media)
  83. media->evaluate(window);
  84. return matches();
  85. }
  86. bool MediaList::matches() const
  87. {
  88. if (m_media.is_empty()) {
  89. return true;
  90. }
  91. for (auto& media : m_media) {
  92. if (media->matches())
  93. return true;
  94. }
  95. return false;
  96. }
  97. WebIDL::ExceptionOr<JS::Value> MediaList::item_value(size_t index) const
  98. {
  99. if (index >= m_media.size())
  100. return JS::js_undefined();
  101. return JS::PrimitiveString::create(vm(), m_media[index]->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string());
  102. }
  103. }