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; +})()