LibWeb: Add FileList from the FileAPI spec

This commit is contained in:
Andrew Kaster 2022-10-03 21:10:39 -06:00 committed by Andreas Kling
parent 162e4179fc
commit ffab9fb44e
Notes: sideshowbarker 2024-07-17 06:21:56 +09:00
6 changed files with 106 additions and 0 deletions

View file

@ -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) \

View file

@ -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

View file

@ -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);
}
}

View file

@ -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;
};
}

View file

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

View file

@ -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)