From 1ff8ad99523fc30fa5115fa0f44fbc174fefe939 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Mon, 19 Oct 2026 22:35:50 +0800 Subject: [PATCH] fix(cssom): serialize computed background layers Serialize computed background from the shared retained style read, using the image list to coordinate layers, repeating or truncating other component lists, and emitting color only in the final layer. Include computed initial components instead of using specified-value shorthand elision. When Stylo cannot serialize a computed shorthand, reify the shared CSSOM result as one property-associated opaque CSSStyleValue. Preserve native realm allocation, snapshot immutability and longhand Typed OM types. Cover multiple layers, currentcolor, list coordination, private native writes and cross-realm reads. --- .../native_bridge/element/styles/methods.rs | 6 ++++ .../computed_style/css_computed_background.js | 18 +++++++++-- .../computed_style/css_computed_background.rs | 12 +++++++ .../computed_style/css_computed_shorthands.js | 32 +++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_shorthands.js diff --git a/moli-renderer-v8/src/native_bridge/element/styles/methods.rs b/moli-renderer-v8/src/native_bridge/element/styles/methods.rs index 0caa8304f58..1282cadaf09 100644 --- a/moli-renderer-v8/src/native_bridge/element/styles/methods.rs +++ b/moli-renderer-v8/src/native_bridge/element/styles/methods.rs @@ -396,6 +396,12 @@ pub(crate) fn computed_typed_style_value_from_object<'s>( computed .computed_or_resolved_property_value(id.clone(), None, &mut css_text) .ok()?; + if css_text.is_empty() && id.as_shorthand().is_ok() { + // Stylo cannot serialize every computed shorthand. Use this read's + // shared CSSOM serializer, retaining one property-associated opaque + // value rather than splitting a shorthand into keywords or layers. + css_text = read.property(property); + } if css_text.is_empty() && !property.starts_with("--") { return None; } diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.js b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.js index e1ad052488d..fd566fc9327 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.js +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.js @@ -16,18 +16,30 @@ element.style.cssText = text; document.body.append(element); const style = getComputedStyle(element); + const map = element.computedStyleMap(); + const value = map.get('background'); check(style.background === expected, text + ': ' + style.background); + check(String(value) === expected, text + ': Typed OM ' + value); + check(Object.getPrototypeOf(value) === CSSStyleValue.prototype, 'opaque shorthand'); + const all = map.getAll('background'); + check(all.length === 1 && String(all[0]) === expected, 'whole shorthand is one value'); element.remove(); } const element = document.createElement('div'); element.style.background = 'blue'; document.body.append(element); const computed = getComputedStyle(element); - const previous = computed.background; + const previousComputed = computed.background; + const map = element.computedStyleMap(); + const previous = Object.freeze(map.get('background')); element.style.backgroundColor = 'red'; - check(String(previous).startsWith('rgb(0, 0, 255) '), 'retained value is immutable'); + check(String(previousComputed).startsWith('rgb(0, 0, 255) '), 'retained computed string is immutable'); check(computed.background.startsWith('rgb(255, 0, 0) '), 'retained declaration observes mutations'); + check(String(previous).startsWith('rgb(0, 0, 255) '), 'retained typed value is immutable'); + check(String(map.get('background')).startsWith('rgb(255, 0, 0) '), 'retained map observes mutations'); check(computed.backgroundImage === 'none', 'longhand value preserved'); + check(map.get('background-image') instanceof CSSKeywordValue, 'longhand type preserved'); element.remove(); - check(computed.background === '', 'inactive owner is empty'); + check(computed.background === '', 'inactive declaration owner is empty'); + check(map.get('background') === undefined && map.getAll('background').length === 0, 'inactive map owner is empty'); return true; })() diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.rs index 05b4c85aab1..34ecd743f66 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_background.rs @@ -8,3 +8,15 @@ fn css_computed_background_preserves_coordinated_layers() { "true" ); } + +#[test] +fn css_computed_shorthands_retain_property_identity_and_native_realms() { + let mut vm = new_parsed_test_vm( + "https://computed-shorthand-values.test/", + "", + ); + assert_eq!( + vm.eval(include_str!("css_computed_shorthands.js")).unwrap(), + "true" + ); +} diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_shorthands.js b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_shorthands.js new file mode 100644 index 00000000000..dd5e2628a30 --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/css_computed_shorthands.js @@ -0,0 +1,32 @@ +(() => { + const check = (ok, message) => {if (!ok) throw new Error(message);}; + const element = document.createElement('div'); + element.style.cssText = 'background:blue;mask:none;flex-flow:column wrap;transition:opacity 1s linear;'; + document.body.append(element); + const map = element.computedStyleMap(); + for (const property of ['background', 'mask', 'flex-flow', 'transition']) { + const value = map.get(property); + check(Object.getPrototypeOf(value) === CSSStyleValue.prototype, property + ': opaque type'); + check(map.getAll(property).length === 1, property + ': one value'); + let rejected = false; + try {element.attributeStyleMap.set('margin', value);} catch(e) {rejected = e instanceof TypeError;} + check(rejected, property + ': property association'); + const expected = String(value); + value.toString = () => {throw new Error('author stringifier');}; + element.attributeStyleMap.set(property, value); + check(String(map.get(property)) === expected, property + ': native write'); + } + const other = document.querySelector('iframe').contentWindow; + const childElement = other.document.createElement('div'); + childElement.style.background = 'blue'; other.document.body.append(childElement); + const childMap = childElement.computedStyleMap(); + const read = StylePropertyMapReadOnly.prototype.get; + const value = read.call(childMap, 'background'); + check(value instanceof other.CSSStyleValue && !(value instanceof CSSStyleValue), 'map owner realm'); + const native = other.CSSStyleValue; + other.CSSStyleValue = () => {throw new Error('author constructor');}; + check(read.call(childMap, 'background') instanceof native, 'intrinsic allocation'); + other.CSSStyleValue = native; + childElement.remove(); element.remove(); + return true; +})()