Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fonts): retain registrations shared by CSS declarations

ldm046705af6b76f
diff --git a/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/storage.rs b/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/storage.rs
index 755c4f9d8..bc30c63de 100644
--- a/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/storage.rs
+++ b/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/storage.rs
@@ -318,13 +318,31 @@ fn sync_font_face_set_owners<'s>(
previous: v8::Local<'s, v8::Array>,
current: v8::Local<'s, v8::Array>,
) {
+ // Distinct CSS-connected wrappers can share a resource slot. Removing one
+ // membership must not unregister the resource still used by another face.
+ let mut retained_slots = std::collections::HashSet::new();
+ for index in 0..current.length() {
+ let Some(face) = current
+ .get_index(scope, index)
+ .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
+ else {
+ continue;
+ };
+ if let Some(load) = super::state::get(scope, face) {
+ retained_slots.insert(load.borrow().slot().to_owned());
+ }
+ }
for index in 0..previous.length() {
let Some(face) = previous.get_index(scope, index) else {
continue;
};
if !array_contains_value(scope, current, face) {
if let Ok(face) = v8::Local::<v8::Object>::try_from(face) {
- super::loading::sync_registration(scope, owner, face, false);
+ let retained = super::state::get(scope, face)
+ .is_some_and(|load| retained_slots.contains(load.borrow().slot()));
+ if !retained {
+ super::loading::sync_registration(scope, owner, face, false);
+ }
}
remove_font_face_set_owner(scope, face, owner);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/fonts/selection_order.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/fonts/selection_order.rs
index fbd5cc674..fbda6cc45 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/fonts/selection_order.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/fonts/selection_order.rs
@@ -238,3 +238,47 @@ fn shared_font_faces_have_independent_selection_order_in_each_document() {
.unwrap();
assert_eq!(widths(&mut vm), "80,160");
}
+
+#[test]
+fn duplicate_css_font_declarations_keep_registration_until_last_member_leaves() {
+ let (_, wide) = order_font_data();
+ for removed in [0, 1] {
+ let mut vm = order_vm();
+ vm.eval(&format!(
+ r#"
+ const style=document.createElement('style');
+ const rule='@font-face{{font-family:OrderFace;src:url(data:font/ttf;base64,{wide})}}';
+ style.textContent=rule+rule;document.head.append(style);
+ globalThis.originalFaces=[...document.fonts];
+ document.fonts.load('40px OrderFace','AAAA');
+ "#
+ ))
+ .unwrap();
+ assert_eq!(vm.eval("originalFaces.length===2&&originalFaces[0]!==originalFaces[1]&&originalFaces.every(face=>face.status==='loaded')").unwrap(), "true");
+ assert_eq!(
+ vm.eval("String(probe.getBoundingClientRect().width)")
+ .unwrap(),
+ "160"
+ );
+ assert_eq!(
+ vm.eval(&format!(
+ "document.styleSheets[0].deleteRule({removed});document.fonts.size"
+ ))
+ .unwrap(),
+ "1"
+ );
+ assert_eq!(
+ vm.eval("String(probe.getBoundingClientRect().width)")
+ .unwrap(),
+ "160",
+ "deleting either duplicate must preserve the remaining registration"
+ );
+ assert_eq!(vm.document_web_font_counts_for_test().2, 1);
+ assert_eq!(
+ vm.eval("document.styleSheets[0].deleteRule(0);document.fonts.size")
+ .unwrap(),
+ "0"
+ );
+ assert_eq!(vm.document_web_font_counts_for_test().2, 0);
+ }
+}