Browse Source

LibWeb: Add FileList from the FileAPI spec

Andrew Kaster 2 years ago
parent
commit
ffab9fb44e

+ 3 - 0
Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h

@@ -92,6 +92,8 @@
 #include <LibWeb/Bindings/EventTargetConstructor.h>
 #include <LibWeb/Bindings/EventTargetPrototype.h>
 #include <LibWeb/Bindings/FileConstructor.h>
+#include <LibWeb/Bindings/FileListConstructor.h>
+#include <LibWeb/Bindings/FileListPrototype.h>
 #include <LibWeb/Bindings/FilePrototype.h>
 #include <LibWeb/Bindings/FocusEventConstructor.h>
 #include <LibWeb/Bindings/FocusEventPrototype.h>
@@ -449,6 +451,7 @@
     ADD_WINDOW_OBJECT_INTERFACE(Event)                                                              \
     ADD_WINDOW_OBJECT_INTERFACE(EventTarget)                                                        \
     ADD_WINDOW_OBJECT_INTERFACE(File)                                                               \
+    ADD_WINDOW_OBJECT_INTERFACE(FileList)                                                           \
     ADD_WINDOW_OBJECT_INTERFACE(Headers)                                                            \
     ADD_WINDOW_OBJECT_INTERFACE(History)                                                            \
     ADD_WINDOW_OBJECT_INTERFACE(HTMLAnchorElement)                                                  \

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -137,6 +137,7 @@ set(SOURCES
     Fetch/Response.cpp
     FileAPI/Blob.cpp
     FileAPI/File.cpp
+    FileAPI/FileList.cpp
     FontCache.cpp
     Geometry/DOMPoint.cpp
     Geometry/DOMPointReadOnly.cpp

+ 53 - 0
Userland/Libraries/LibWeb/FileAPI/FileList.cpp

@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/Realm.h>
+#include <LibWeb/Bindings/Intrinsics.h>
+#include <LibWeb/Bindings/LegacyPlatformObject.h>
+#include <LibWeb/FileAPI/FileList.h>
+
+namespace Web::FileAPI {
+
+JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
+{
+    return *realm.heap().allocate<FileList>(realm, realm, move(files));
+}
+
+FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
+    : Bindings::LegacyPlatformObject(Bindings::cached_web_prototype(realm, "FileList"))
+    , m_files(move(files))
+{
+}
+
+FileList::~FileList() = default;
+
+// https://w3c.github.io/FileAPI/#dfn-item
+bool FileList::is_supported_property_index(u32 index) const
+{
+    // Supported property indices are the numbers in the range zero to one less than the number of File objects represented by the FileList object.
+    // If there are no such File objects, then there are no supported property indices.
+    if (m_files.is_empty())
+        return false;
+
+    return m_files.size() < index;
+}
+
+JS::Value FileList::item_value(size_t index) const
+{
+    if (index >= m_files.size())
+        return JS::js_undefined();
+
+    return m_files[index].ptr();
+}
+
+void FileList::visit_edges(Cell::Visitor& visitor)
+{
+    Base::visit_edges(visitor);
+    for (auto file : m_files)
+        visitor.visit(file);
+}
+
+}

+ 43 - 0
Userland/Libraries/LibWeb/FileAPI/FileList.h

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Vector.h>
+#include <LibJS/Heap/GCPtr.h>
+#include <LibWeb/Bindings/LegacyPlatformObject.h>
+#include <LibWeb/FileAPI/File.h>
+
+namespace Web::FileAPI {
+
+class FileList : public Bindings::LegacyPlatformObject {
+    WEB_PLATFORM_OBJECT(FileList, Bindings::LegacyPlatformObject);
+
+public:
+    static JS::NonnullGCPtr<FileList> create(JS::Realm&, Vector<JS::NonnullGCPtr<File>>&&);
+    virtual ~FileList() override;
+
+    // https://w3c.github.io/FileAPI/#dfn-length
+    unsigned long length() const { return m_files.size(); }
+
+    // https://w3c.github.io/FileAPI/#dfn-item
+    File const* item(size_t index) const
+    {
+        return index < m_files.size() ? m_files[index].ptr() : nullptr;
+    }
+
+    virtual bool is_supported_property_index(u32 index) const override;
+    virtual JS::Value item_value(size_t index) const override;
+
+private:
+    FileList(JS::Realm&, Vector<JS::NonnullGCPtr<File>>&&);
+
+    virtual void visit_edges(Cell::Visitor&) override;
+
+    Vector<JS::NonnullGCPtr<File>> m_files;
+};
+
+}

+ 5 - 0
Userland/Libraries/LibWeb/FileAPI/FileList.idl

@@ -0,0 +1,5 @@
+[Exposed=(Window,Worker), Serializable]
+interface FileList {
+  getter File? item(unsigned long index);
+  readonly attribute unsigned long length;
+};

+ 1 - 0
Userland/Libraries/LibWeb/idl_files.cmake

@@ -57,6 +57,7 @@ libweb_js_bindings(Fetch/Request)
 libweb_js_bindings(Fetch/Response)
 libweb_js_bindings(FileAPI/Blob)
 libweb_js_bindings(FileAPI/File)
+libweb_js_bindings(FileAPI/FileList)
 libweb_js_bindings(Geometry/DOMPoint)
 libweb_js_bindings(Geometry/DOMPointReadOnly)
 libweb_js_bindings(Geometry/DOMRect)