LibWeb: Mark FontFaceSet as a setlike IDL interface

And implement more of the constructor logic.
This commit is contained in:
Andrew Kaster 2024-05-21 21:05:36 -06:00 committed by Andreas Kling
parent 7077e4d002
commit bab546472e
Notes: sideshowbarker 2024-07-17 18:46:57 +09:00
5 changed files with 124 additions and 16 deletions

View file

@ -0,0 +1,23 @@
-- Empty FontFaceSet --
fontFaceSet.size: 0
fontFaceSet.has(fontFace): false
fontFaceSet.status: loaded
deleteBeforeAdd: false
-- Add Font --
fontFaceSet.size: 1
fontFaceSet.has(fontFace): true
fontFaceKey.name: Hash Sans
FIXME: fontFaceSet.status: loaded
-- Delete Font --
fontFaceSet.size: 0
fontFaceSet.has(fontFace): false
didDelete: true
fontFaceSet.status: loaded
-- Add Font again --
fontFaceSet.size: 1
fontFaceSet.has(fontFace): true
FIXME: fontFaceSet.status: loaded
-- Clear FontFaceSet --
fontFaceSet.size: 0
fontFaceSet.has(fontFace): false
fontFaceSet.status: loaded

View file

@ -0,0 +1,50 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
const fontFaceSet = new FontFaceSet([]);
const fontFace = new FontFace("Hash Sans", 'url(../../../../Ref/assets/HashSans.woff)');
println("-- Empty FontFaceSet --");
println(`fontFaceSet.size: ${fontFaceSet.size}`);
println(`fontFaceSet.has(fontFace): ${fontFaceSet.has(fontFace)}`);
for (const fontFaceKey of fontFaceSet) {
println("FAIL: FontFace is supposed to be empty");
}
println(`fontFaceSet.status: ${fontFaceSet.status}`);
const deleteBeforeAdd = fontFaceSet.delete(fontFace);
println(`deleteBeforeAdd: ${deleteBeforeAdd}`);
println("-- Add Font --");
fontFaceSet.add(fontFace);
println(`fontFaceSet.size: ${fontFaceSet.size}`);
println(`fontFaceSet.has(fontFace): ${fontFaceSet.has(fontFace)}`);
for (const fontFaceKey of fontFaceSet) {
println(`fontFaceKey.name: ${fontFaceKey.family}`);
}
// Should be changed 'loading' until the font is loaded; should fire loading event
println(`FIXME: fontFaceSet.status: ${fontFaceSet.status}`);
println("-- Delete Font --");
const didDelete = fontFaceSet.delete(fontFace);
println(`fontFaceSet.size: ${fontFaceSet.size}`);
println(`fontFaceSet.has(fontFace): ${fontFaceSet.has(fontFace)}`);
println(`didDelete: ${didDelete}`);
println(`fontFaceSet.status: ${fontFaceSet.status}`);
println("-- Add Font again --");
fontFaceSet.add(fontFace);
println(`fontFaceSet.size: ${fontFaceSet.size}`);
println(`fontFaceSet.has(fontFace): ${fontFaceSet.has(fontFace)}`);
// Should be changed 'loading' until the font is loaded; should fire loading event
println(`FIXME: fontFaceSet.status: ${fontFaceSet.status}`);
println("-- Clear FontFaceSet --");
fontFaceSet.clear();
println(`fontFaceSet.size: ${fontFaceSet.size}`);
println(`fontFaceSet.has(fontFace): ${fontFaceSet.has(fontFace)}`);
println(`fontFaceSet.status: ${fontFaceSet.status}`);
});
</script>

View file

@ -6,8 +6,10 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/Set.h>
#include <LibWeb/Bindings/FontFaceSetPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/FontFace.h>
#include <LibWeb/CSS/FontFaceSet.h>
#include <LibWeb/WebIDL/Promise.h>
@ -15,10 +17,20 @@ namespace Web::CSS {
JS_DEFINE_ALLOCATOR(FontFaceSet);
JS::NonnullGCPtr<FontFaceSet> FontFaceSet::construct_impl(JS::Realm& realm, Vector<JS::Handle<FontFace>> initial_faces)
// https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-fontfaceset
JS::NonnullGCPtr<FontFaceSet> FontFaceSet::construct_impl(JS::Realm& realm, Vector<JS::Handle<FontFace>> const& initial_faces)
{
auto promise = WebIDL::create_promise(realm);
return realm.heap().allocate<FontFaceSet>(realm, realm, promise, move(initial_faces));
auto ready_promise = WebIDL::create_promise(realm);
auto set_entries = JS::Set::create(realm);
// The FontFaceSet constructor, when called, must iterate its initialFaces argument and add each value to its set entries.
for (auto const& face : initial_faces)
set_entries->set_add(face);
if (set_entries->set_size() == 0)
WebIDL::resolve_promise(realm, *ready_promise);
return realm.heap().allocate<FontFaceSet>(realm, realm, ready_promise, set_entries);
}
JS::NonnullGCPtr<FontFaceSet> FontFaceSet::create(JS::Realm& realm)
@ -26,14 +38,13 @@ JS::NonnullGCPtr<FontFaceSet> FontFaceSet::create(JS::Realm& realm)
return construct_impl(realm, {});
}
FontFaceSet::FontFaceSet(JS::Realm& realm, JS::NonnullGCPtr<WebIDL::Promise> ready_promise, Vector<JS::Handle<FontFace>>)
FontFaceSet::FontFaceSet(JS::Realm& realm, JS::NonnullGCPtr<WebIDL::Promise> ready_promise, JS::NonnullGCPtr<JS::Set> set_entries)
: Bindings::PlatformObject(realm)
, m_set_entries(set_entries)
, m_ready_promise(ready_promise)
{
// FIXME: Only set this after all the initial faces have been loaded
m_status = Bindings::FontFaceSetLoadStatus::Loaded;
// FIXME: Only resolve the promise after all the initial faces have been loaded
WebIDL::resolve_promise(realm, *m_ready_promise);
bool const is_ready = ready()->state() == JS::Promise::State::Fulfilled;
m_status = is_ready ? Bindings::FontFaceSetLoadStatus::Loaded : Bindings::FontFaceSetLoadStatus::Loading;
}
void FontFaceSet::initialize(JS::Realm& realm)
@ -46,16 +57,32 @@ void FontFaceSet::initialize(JS::Realm& realm)
void FontFaceSet::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_set_entries);
visitor.visit(m_ready_promise);
}
// https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add
JS::NonnullGCPtr<FontFaceSet> FontFaceSet::add(JS::Handle<FontFace>)
JS::NonnullGCPtr<FontFaceSet> FontFaceSet::add(JS::Handle<FontFace> face)
{
// FIXME: Do the steps
// FIXME: Do the actual spec steps
m_set_entries->set_add(face);
return *this;
}
// https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-delete
bool FontFaceSet::delete_(JS::Handle<FontFace> face)
{
// FIXME: Do the actual spec steps
return m_set_entries->set_remove(face);
}
// https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear
void FontFaceSet::clear()
{
// FIXME: Do the actual spec steps
m_set_entries->set_clear();
}
// https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load
JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> FontFaceSet::load(String const&, String const&)
{

View file

@ -6,6 +6,8 @@
#pragma once
#include <LibJS/Runtime/Set.h>
#include <LibJS/Runtime/SetIterator.h>
#include <LibWeb/Bindings/FontFaceSetPrototype.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/CSS/FontFace.h>
@ -17,23 +19,29 @@ class FontFaceSet final : public Bindings::PlatformObject {
JS_DECLARE_ALLOCATOR(FontFaceSet);
public:
[[nodiscard]] static JS::NonnullGCPtr<FontFaceSet> construct_impl(JS::Realm&, Vector<JS::Handle<FontFace>> initial_faces);
[[nodiscard]] static JS::NonnullGCPtr<FontFaceSet> construct_impl(JS::Realm&, Vector<JS::Handle<FontFace>> const& initial_faces);
[[nodiscard]] static JS::NonnullGCPtr<FontFaceSet> create(JS::Realm&);
virtual ~FontFaceSet() override = default;
JS::NonnullGCPtr<FontFaceSet> add(JS::Handle<FontFace> face);
JS::NonnullGCPtr<JS::Set> set_entries() const { return m_set_entries; }
JS::NonnullGCPtr<FontFaceSet> add(JS::Handle<FontFace>);
bool delete_(JS::Handle<FontFace>);
void clear();
JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> load(String const& font, String const& text);
JS::NonnullGCPtr<JS::Promise> ready() const;
Bindings::FontFaceSetLoadStatus status() const { return m_status; }
private:
FontFaceSet(JS::Realm&, JS::NonnullGCPtr<WebIDL::Promise> ready_promise, Vector<JS::Handle<FontFace>> initial_faces);
FontFaceSet(JS::Realm&, JS::NonnullGCPtr<WebIDL::Promise> ready_promise, JS::NonnullGCPtr<JS::Set> set_entries);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
JS::NonnullGCPtr<JS::Set> m_set_entries;
JS::GCPtr<WebIDL::Promise> m_ready_promise; // [[ReadyPromise]]
Bindings::FontFaceSetLoadStatus m_status { Bindings::FontFaceSetLoadStatus::Loading };
};

View file

@ -18,10 +18,10 @@ enum FontFaceSetLoadStatus { "loading", "loaded" };
interface FontFaceSet : EventTarget {
constructor(sequence<FontFace> initialFaces);
// FIXME: setlike<FontFace>;
setlike<FontFace>;
FontFaceSet add(FontFace font);
[FIXME] boolean delete(FontFace font);
[FIXME] undefined clear();
boolean delete(FontFace font);
undefined clear();
// events for when loading state changes
[FIXME] attribute EventHandler onloading;