mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-30 16:28:48 +00:00
We have two known PlatformObjects that need to implement some of the behavior of LegacyPlatformObjects to date: Window, and HTMLFormElement. To make this not require double (or virtual) inheritance of PlatformObject, move the behavior of LegacyPlatformObject into PlatformObject. The selection of LegacyPlatformObject behavior is done with a new bitfield of feature flags instead of a dozen virtual functions that return bool. This change simplifies every class involved in the diff with the notable exception of Window, which now needs some ugly const casts to implement named property access.
62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
/*
|
|
* 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/PlatformObject.h>
|
|
#include <LibWeb/FileAPI/FileList.h>
|
|
|
|
namespace Web::FileAPI {
|
|
|
|
JS_DEFINE_ALLOCATOR(FileList);
|
|
|
|
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::PlatformObject(realm)
|
|
, m_files(move(files))
|
|
{
|
|
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
|
|
}
|
|
|
|
FileList::~FileList() = default;
|
|
|
|
void FileList::initialize(JS::Realm& realm)
|
|
{
|
|
Base::initialize(realm);
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::FileListPrototype>(realm, "FileList"_fly_string));
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
WebIDL::ExceptionOr<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);
|
|
}
|
|
|
|
}
|