Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(css): enable native variable font properties

ldm0700236f0299c
diff --git a/moli-renderer-v8/src/detached_css_style.rs b/moli-renderer-v8/src/detached_css_style.rs
index c39dfd098..997f6cf64 100644
--- a/moli-renderer-v8/src/detached_css_style.rs
+++ b/moli-renderer-v8/src/detached_css_style.rs
@@ -584,7 +584,6 @@ const CSS_STYLE_DECLARATION_SHELL_PROPERTIES: &[&str] = &[
"column-rule-inset-start",
"column-wrap",
"dynamic-range-limit",
- "font-optical-sizing",
"interactivity",
"interest-delay-end",
"interest-delay-start",
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index 4d936e557..264dcd161 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -60,6 +60,7 @@ mod content_and_invalidation;
mod cross_document_and_animations;
mod css_computed_background;
mod css_computed_map_enumeration;
+mod css_variable_fonts;
mod nested_document_invalidation;
mod properties_and_selectors;
mod stylesheet_and_document_lifecycle;
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_font_computation.js b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_font_computation.js
new file mode 100644
index 000000000..77b1bc30e
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_font_computation.js
@@ -0,0 +1,39 @@
+(() => {
+ const check = (ok, message) => { if (!ok) throw new Error(message); };
+ const parent = document.createElement('div');
+ const child = document.createElement('div');
+ parent.style.cssText = 'font-optical-sizing:none;font-variation-settings:"wght" 700, "wdth" 90, "wght" 600;--weight:650';
+ parent.append(child); document.body.append(parent);
+ function computed(optical, variation) {
+ const style = getComputedStyle(child);
+ for (const [property, expected] of [['font-optical-sizing', optical], ['font-variation-settings', variation]]) {
+ check(style.getPropertyValue(property) === expected, property + ': computed value');
+ }
+ }
+ for (const keyword of ['', 'inherit', 'unset', 'revert']) {
+ child.style.cssText = keyword ? `font-optical-sizing:${keyword};font-variation-settings:${keyword}` : '';
+ computed('none', '"wdth" 90, "wght" 600');
+ }
+ child.style.cssText = 'font-optical-sizing:initial;font-variation-settings:initial';
+ computed('auto', 'normal');
+ child.style.cssText = 'font-variation-settings:"wght" var(--weight), "wdth" calc(100 / 2), "wght" calc(600 + 50)';
+ computed('none', '"wdth" 50, "wght" 650');
+ child.style.fontVariationSettings = 'var(--missing)';
+ computed('none', '"wdth" 90, "wght" 600');
+ child.style.cssText = 'font:16px serif;font-optical-sizing:none;font-variation-settings:"wght" 600';
+ computed('none', '"wght" 600');
+ check(child.style.font === '', 'font cannot express non-initial reset-only values');
+ const optical = getComputedStyle(child).getPropertyValue('font-optical-sizing');
+ const variation = getComputedStyle(child).getPropertyValue('font-variation-settings');
+ child.style.font = '16px serif';
+ computed('auto', 'normal');
+ check(child.style.font === '16px serif', 'font resets native values');
+ check(String(optical) === 'none' && String(variation) === '"wght" 600', 'retained CSSOM strings');
+ const sheet = document.createElement('style');
+ sheet.textContent = '@layer base { #font-target { font-optical-sizing:none;font-variation-settings:"wght" 400 } } #font-target { font-optical-sizing:auto;font-variation-settings:"wght" 550 }';
+ document.head.append(sheet); child.id = 'font-target';
+ child.style.cssText = 'font-optical-sizing:revert-layer;font-variation-settings:revert-layer';
+ computed('auto', '"wght" 550');
+ sheet.remove(); parent.remove();
+ return true;
+})()
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_font_declarations.js b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_font_declarations.js
new file mode 100644
index 000000000..494412484
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_font_declarations.js
@@ -0,0 +1,42 @@
+(() => {
+ const check = (ok, message) => { if (!ok) throw new Error(message); };
+ const properties = [
+ ['font-optical-sizing', 'fontOpticalSizing', 'none'],
+ ['font-variation-settings', 'fontVariationSettings', '"wght" 700, "wdth" 90, "wght" 600'],
+ ];
+ const documents = [document, document.implementation.createHTMLDocument('')];
+ for (const [property, alias, value] of properties) {
+ check(CSS.supports(property, value), property + ': native grammar');
+ check(CSS.supports(property, 'var(--font-value)'), property + ': pending substitution');
+ for (const doc of documents) {
+ const style = doc.createElement('div').style;
+ check(property in style && alias in style, property + ': accessors');
+ for (const keyword of ['initial', 'inherit', 'unset', 'revert', 'revert-layer']) {
+ check(CSS.supports(property, keyword), property + ': CSS-wide support');
+ check(CSS.supports(`(${property}: ${keyword} !important)`), property + ': condition priority');
+ style.setProperty(property, keyword, 'important');
+ check(style.getPropertyValue(property) === keyword, property + ': stored keyword');
+ check(style[alias] === keyword, property + ': keyword alias');
+ check(style.getPropertyPriority(property) === 'important', property + ': keyword priority');
+ style[alias] = keyword;
+ check(style.getPropertyPriority(property) === '', property + ': alias clears priority');
+ }
+ style.setProperty(property, value, 'important');
+ for (const invalid of ['unsupported-value', 'initial inherit', 'initial; color: red']) {
+ check(!CSS.supports(property, invalid), property + ': invalid grammar');
+ check(!CSS.supports(`(${property}: ${invalid})`), property + ': invalid condition');
+ style.setProperty(property, invalid);
+ check(style[alias] === value, property + ': rejected write preserves value');
+ check(style.getPropertyPriority(property) === 'important', property + ': rejected write preserves priority');
+ }
+ check(style.removeProperty(property) === value, property + ': remove returns value');
+ check(style[alias] === '', property + ': removal');
+ style[alias] = '/**/IN\\69 TIAL/**/';
+ check(style[property] === 'initial', property + ': tokenized CSS-wide value');
+ check(CSS.supports(property, '/**/IN\\69 TIAL/**/'), property + ': tokenized support');
+ check(CSS.supports(`(not (${property}: unsupported-value)) and (${property}: inherit)`), property + ': condition evaluation');
+ }
+
+ }
+ return true;
+})()
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_fonts.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_fonts.rs
new file mode 100644
index 000000000..04dedfba7
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_variable_fonts.rs
@@ -0,0 +1,21 @@
+use super::*;
+
+#[test]
+fn css_variable_fonts_parse_native_declarations_in_live_and_windowless_documents() {
+ let mut vm = new_parsed_test_vm("https://variable-fonts.test/", "<!doctype html><body>");
+ assert_eq!(
+ vm.eval(include_str!("css_variable_font_declarations.js"))
+ .unwrap(),
+ "true"
+ );
+}
+
+#[test]
+fn css_variable_fonts_resolve_cascade_and_reset_with_font_shorthand() {
+ let mut vm = new_parsed_test_vm("https://variable-fonts.test/", "<!doctype html><body>");
+ assert_eq!(
+ vm.eval(include_str!("css_variable_font_computation.js"))
+ .unwrap(),
+ "true"
+ );
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/css_property_shells.js b/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/css_property_shells.js
index 4ed2a48b4..6a446291b 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/css_property_shells.js
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/css_property_shells.js
@@ -6,7 +6,6 @@
];
for (const style of styles) for (const [property, alias] of [
['timeline-trigger', 'timelineTrigger'],
- ['font-optical-sizing', 'fontOpticalSizing'],
['-webkit-locale', 'webkitLocale']
]) {
assert(property in style && alias in style, 'property accessors');
diff --git a/moli-renderer-v8/src/style_engine/mod.rs b/moli-renderer-v8/src/style_engine/mod.rs
index 112648105..6829311d8 100644
--- a/moli-renderer-v8/src/style_engine/mod.rs
+++ b/moli-renderer-v8/src/style_engine/mod.rs
@@ -1152,6 +1152,7 @@ pub(crate) fn ensure_stylo_browser_compat_prefs() {
stylo_static_prefs::set_pref!("layout.css.tree-counting-functions.enabled", true);
stylo_static_prefs::set_pref!("layout.css.zoom.enabled", true);
stylo_static_prefs::set_pref!("layout.grid.enabled", true);
+ stylo_static_prefs::set_pref!("layout.variable_fonts.enabled", true);
// Stylo exposes the experimental CSS Sizing `fit-content(<length>)`
// form behind a Servo pref. Chromium 147 rejects that function for
// width/height while still accepting the bare `fit-content` keyword