summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2021-09-29 12:08:09 +0100
committerAndreas Kling <kling@serenityos.org>2021-10-01 20:03:03 +0200
commit8ac622f056f7ab00cb22dd4c00e53d36f8356b34 (patch)
tree2b9447df8c1118d03f3402090bb713d4d8555e99 /Userland/Libraries/LibWeb/CSS
parent0a4d9c6d313a8fd5e0799dda4c64d2edd8dd1252 (diff)
downloadserenity-8ac622f056f7ab00cb22dd4c00e53d36f8356b34.zip
LibWeb: Add MediaList
This is a list of MediaQuery objects. Not to be confused with `MediaQueryList`, which is concerned with firing events when a media query's match-state changes.
Diffstat (limited to 'Userland/Libraries/LibWeb/CSS')
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaList.cpp70
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaList.h38
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaList.idl7
3 files changed, 115 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/MediaList.cpp b/Userland/Libraries/LibWeb/CSS/MediaList.cpp
new file mode 100644
index 0000000000..aa8097f256
--- /dev/null
+++ b/Userland/Libraries/LibWeb/CSS/MediaList.cpp
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/CSS/MediaList.h>
+#include <LibWeb/CSS/Parser/Parser.h>
+
+namespace Web::CSS {
+
+MediaList::MediaList(NonnullRefPtrVector<MediaQuery>&& media)
+ : m_media(move(media))
+{
+}
+
+MediaList::~MediaList()
+{
+}
+
+// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
+String MediaList::media_text() const
+{
+ StringBuilder builder;
+ builder.join(", ", m_media);
+ return builder.to_string();
+}
+
+// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
+void MediaList::set_media_text(String const& text)
+{
+ m_media.clear();
+ if (text.is_empty())
+ return;
+ m_media = parse_media_query_list({}, text);
+}
+
+// https://www.w3.org/TR/cssom-1/#dom-medialist-item
+Optional<String> MediaList::item(size_t index) const
+{
+ if (index >= length())
+ return {};
+
+ return m_media[index].to_string();
+}
+
+// https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
+void MediaList::append_medium(String medium)
+{
+ auto m = parse_media_query({}, medium);
+ if (!m)
+ return;
+ if (m_media.contains_slow(*m))
+ return;
+ m_media.append(m.release_nonnull());
+}
+
+// https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
+void MediaList::delete_medium(String medium)
+{
+ auto m = parse_media_query({}, medium);
+ if (!m)
+ return;
+ m_media.remove_all_matching([&](auto& existing) -> bool {
+ return m->to_string() == existing->to_string();
+ });
+ // FIXME: If nothing was removed, then throw a NotFoundError exception.
+}
+
+}
diff --git a/Userland/Libraries/LibWeb/CSS/MediaList.h b/Userland/Libraries/LibWeb/CSS/MediaList.h
new file mode 100644
index 0000000000..4586f731bb
--- /dev/null
+++ b/Userland/Libraries/LibWeb/CSS/MediaList.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullRefPtrVector.h>
+#include <AK/Optional.h>
+#include <AK/RefCounted.h>
+#include <LibWeb/CSS/MediaQuery.h>
+
+namespace Web::CSS {
+
+// https://www.w3.org/TR/cssom-1/#the-medialist-interface
+class MediaList final : public RefCounted<MediaList> {
+public:
+ static NonnullRefPtr<MediaList> create(NonnullRefPtrVector<MediaQuery>&& media)
+ {
+ return adopt_ref(*new MediaList(move(media)));
+ }
+ ~MediaList();
+
+ String media_text() const;
+ void set_media_text(String const&);
+ size_t length() const { return m_media.size(); }
+ Optional<String> item(size_t index) const;
+ void append_medium(String);
+ void delete_medium(String);
+
+private:
+ explicit MediaList(NonnullRefPtrVector<MediaQuery>&&);
+
+ NonnullRefPtrVector<MediaQuery> m_media;
+};
+
+}
diff --git a/Userland/Libraries/LibWeb/CSS/MediaList.idl b/Userland/Libraries/LibWeb/CSS/MediaList.idl
new file mode 100644
index 0000000000..8ab4047b51
--- /dev/null
+++ b/Userland/Libraries/LibWeb/CSS/MediaList.idl
@@ -0,0 +1,7 @@
+interface MediaList {
+ stringifier attribute [LegacyNullToEmptyString] CSSOMString mediaText;
+ readonly attribute unsigned long length;
+ getter CSSOMString? item(unsigned long index);
+ undefined appendMedium(CSSOMString medium);
+ undefined deleteMedium(CSSOMString medium);
+};