fix(fonts): derive loading event dictionary and validate sequence items

This commit is contained in:
ldm0
2026-10-23 12:50:27 +08:00
committed by Donough Liu
parent c3bc716f53
commit e033492157
6 changed files with 105 additions and 58 deletions
@@ -42,7 +42,7 @@ mod state;
mod storage;
pub(in crate::context_bootstrap) use events::{
initialize_font_face_set_load_event, install_font_face_set_event_handler_accessors,
FontFaceSetLoadEventInit, install_font_face_set_event_handler_accessors,
install_font_face_set_load_event_template_accessors,
};
pub(super) use font_face::{
@@ -1,4 +1,4 @@
use super::storage::{font_face_set_owner_snapshot, is_font_face_value, set_font_face_set_status};
use super::storage::{font_face_set_owner_snapshot, set_font_face_set_status};
use super::*;
use crate::context_bootstrap::events::initialize_event_object;
use crate::util::{
@@ -165,63 +165,38 @@ fn frozen_font_face_array<'s>(
scope: &mut v8::PinScope<'s, '_>,
values: impl IntoIterator<Item = v8::Local<'s, v8::Value>>,
) -> v8::Local<'s, v8::Array> {
let array = v8::Array::new(scope, 0);
for value in values {
let _ = array.set_index(scope, array.length(), value);
}
let values = values.into_iter().collect::<Vec<_>>();
let array = v8::Array::new_with_elements(scope, &values);
let _ = array.set_integrity_level(scope, v8::IntegrityLevel::Frozen);
array
}
fn font_face_values_from_init<'s>(
scope: &mut v8::PinScope<'s, '_>,
init: Option<v8::Local<'s, v8::Object>>,
) -> Option<Vec<v8::Local<'s, v8::Value>>> {
let Some(init) = init else {
return Some(Vec::new());
};
let values = match webidl::optional_member::<webidl::Sequence<v8::Local<'s, v8::Value>>>(
scope,
init,
"fontfaces",
webidl::Context::member("FontFaceSetLoadEventInit", "fontfaces"),
) {
Ok(values) => values.map(|values| values.0).unwrap_or_default(),
Err(error) => {
webidl::throw_error(scope, &error);
return None;
}
};
if values
.iter()
.copied()
.any(|value| !is_font_face_value(scope, value))
{
throw_type_error(
scope,
"Failed to construct 'FontFaceSetLoadEvent': member fontfaces is not a sequence of FontFace objects.",
);
return None;
}
Some(values)
#[derive(webidl::WebIdlDictionary)]
#[webidl(prefix = "FontFaceSetLoadEventInit")]
pub(in crate::context_bootstrap) struct FontFaceSetLoadEventInit<'s> {
#[webidl(inherit)]
base: crate::context_bootstrap::events::EventInit,
#[webidl(sequence, interface = web_api_interfaces::FontFace, default = Vec::new())]
fontfaces: Vec<v8::Local<'s, v8::Object>>,
}
pub(in crate::context_bootstrap) fn initialize_font_face_set_load_event<'s>(
scope: &mut v8::PinScope<'s, '_>,
event: v8::Local<'s, v8::Object>,
init: Option<v8::Local<'s, v8::Object>>,
) -> bool {
let Some(values) = font_face_values_from_init(scope, init) else {
return false;
};
let fontfaces = frozen_font_face_array(scope, values);
set_private_value(
scope,
event,
FONT_FACE_SET_LOAD_EVENT_FONTFACES_SLOT,
fontfaces.into(),
);
true
impl<'s> FontFaceSetLoadEventInit<'s> {
pub(in crate::context_bootstrap) fn event_flags(&self) -> (bool, bool, bool) {
(self.base.bubbles, self.base.cancelable, self.base.composed)
}
pub(in crate::context_bootstrap) fn initialize(
self,
scope: &mut v8::PinScope<'s, '_>,
event: v8::Local<'s, v8::Object>,
) {
let fontfaces = frozen_font_face_array(scope, self.fontfaces.into_iter().map(Into::into));
set_private_value(
scope,
event,
FONT_FACE_SET_LOAD_EVENT_FONTFACES_SLOT,
fontfaces.into(),
);
}
}
fn dispatched_font_face_set_load_event<'s>(
@@ -71,6 +71,16 @@ fn event_subclass_constructor_callback<'s>(
} else {
None
};
let font_face_init = if kind == EventSubclassKind::FontFaceSetLoadEvent {
let Some(init) = super::super::parse_event_dictionary::<
crate::context_bootstrap::css_fontface_runtime::FontFaceSetLoadEventInit,
>(scope, &args, "FontFaceSetLoadEvent") else {
return;
};
Some(init)
} else {
None
};
let storage_event_init = if kind == EventSubclassKind::StorageEvent {
let Some(init) = data::parse_storage_event_init(scope, &args) else {
return;
@@ -141,6 +151,7 @@ fn event_subclass_constructor_callback<'s>(
.as_ref()
.map(pointer::PointerEventInitPayload::event_flags)
})
.or_else(|| font_face_init.as_ref().map(crate::context_bootstrap::css_fontface_runtime::FontFaceSetLoadEventInit::event_flags))
.or_else(|| read_event_init(scope, &args))
else {
return;
@@ -257,11 +268,9 @@ fn event_subclass_constructor_callback<'s>(
}
EventSubclassKind::TrackEvent => data::initialize_track_event(scope, event, init),
EventSubclassKind::FontFaceSetLoadEvent => {
if !crate::context_bootstrap::css_fontface_runtime::initialize_font_face_set_load_event(
scope, event, init,
) {
return;
}
font_face_init
.expect("font event init is parsed")
.initialize(scope, event);
}
}
@@ -0,0 +1,15 @@
use super::*;
#[test]
fn font_face_event_converts_interfaces_during_iteration_before_freezing_the_snapshot() {
let mut vm = new_storage_html_test_vm("https://font-face-event-dictionary.test/");
assert_eq!(
vm.eval(include_str!(
"../../../../tests/fixtures/font-face-event-dictionary.js"
))
.unwrap(),
"true"
);
// Keep the existing native getter's array identity while migrating input.
assert_eq!(vm.eval("(() => {const event=new FontFaceSetLoadEvent('audit'); return event.fontfaces===event.fontfaces;})()").unwrap(), "true");
}
@@ -24,6 +24,7 @@ mod event_constructor_type;
mod event_handlers;
mod event_listener_options;
mod events_selection_storage;
mod font_face_event_init;
mod fontface_descriptors;
mod fonts;
mod gamepad;
@@ -0,0 +1,47 @@
(() => {
const assert = (ok,label) => {if (!ok) throw Error(label);};
const sentinel={};
const members=['bubbles','cancelable','composed','fontfaces'];
const reads=[];
new FontFaceSetLoadEvent('audit',new Proxy({}, {get(_target,key){reads.push(key);}}));
assert(reads.join()===members.join(),'FontFaceSetLoadEvent inherited member order');
for (const stop of members) {
reads.length=0;
let error;
try {new FontFaceSetLoadEvent('audit',new Proxy({}, {get(_target,key) {
reads.push(key);if (key===stop) throw sentinel;
}}));} catch(caught) {error=caught;}
assert(error===sentinel && reads.join()===members.slice(0,members.indexOf(stop)+1).join(),
stop+' original exception and short circuit');
}
let next=0,error;
try {new FontFaceSetLoadEvent('audit',{fontfaces:{[Symbol.iterator](){return {next(){
if (next++) throw sentinel;
return {done:false,value:{}};
}};}}});}catch(caught){error=caught;}
assert(error instanceof TypeError && next===1,'invalid interface rejected before the next iterator step');
const face=new FontFace('audit','url(/not-loaded.ttf)');
const source=new Set([face]);
const event=new FontFaceSetLoadEvent('audit',{bubbles:true,composed:true,fontfaces:source});
source.clear();
assert(event.bubbles && event.composed && event.fontfaces.length===1 && event.fontfaces[0]===face &&
Object.isFrozen(event.fontfaces),'frozen native identity snapshot');
for(const value of [1,'init',true]) {
let error;
try{new FontFaceSetLoadEvent('audit',value);}catch(caught){error=caught;}
assert(error instanceof TypeError,'primitive dictionary');
}
for(const value of [{},Object.create(FontFace.prototype),new Proxy(face,{})]) {
let error;
try{new FontFaceSetLoadEvent('audit',{fontfaces:[value]});}catch(caught){error=caught;}
assert(error instanceof TypeError,'native interface identity');
}
// Snapshot array construction must not run author indexed setters.
const previous=Object.getOwnPropertyDescriptor(Array.prototype,'0');
let writes=0,snapshot;
Object.defineProperty(Array.prototype,'0',{configurable:true,set(){writes++;throw sentinel;}});
try{snapshot=new FontFaceSetLoadEvent('audit',{fontfaces:new Set([face])}).fontfaces;}
finally {if(previous) Object.defineProperty(Array.prototype,'0',previous);else delete Array.prototype[0];}
assert(writes===0 && snapshot[0]===face && Object.isFrozen(snapshot),'snapshot bypasses array prototype setters');
return true;
})()