Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(cssom): share native inline styles for windowless elements

ldm0c1f40cebca5a
diff --git a/moli-renderer-v8/src/detached_css_style.rs b/moli-renderer-v8/src/detached_css_style.rs
index fd529dfa1..82af12914 100644
--- a/moli-renderer-v8/src/detached_css_style.rs
+++ b/moli-renderer-v8/src/detached_css_style.rs
@@ -702,14 +702,6 @@ pub(crate) fn build_lightweight_css_style_declaration<'s>(
build_lightweight_css_style_declaration_with_interface(scope, "CSSStyleProperties", true)
}
-pub(crate) fn build_lightweight_detached_css_style_declaration<'s>(
- scope: &mut v8::PinScope<'s, '_>,
-) -> v8::Local<'s, v8::Object> {
- let style = build_lightweight_css_style_declaration(scope);
- set_style_uses_stylo_declaration_block(scope, style);
- style
-}
-
pub(crate) fn build_lightweight_css_rule_style_declaration<'s>(
scope: &mut v8::PinScope<'s, '_>,
) -> v8::Local<'s, v8::Object> {
diff --git a/moli-renderer-v8/src/native_bridge/element/styles.rs b/moli-renderer-v8/src/native_bridge/element/styles.rs
index 452a76270..f977d46f4 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles.rs
@@ -7,10 +7,7 @@ use crate::native_bridge::{
use crate::style_engine::StyleViewport;
use crate::webidl;
-use super::super::node::{
- node_is_element, node_runtime_and_handle_from_object,
- node_runtime_and_handle_from_object_or_detached,
-};
+use super::super::node::{node_is_element, node_runtime_and_handle_from_object_or_detached};
mod accessors;
mod computed_names;
mod declaration;
@@ -76,7 +73,6 @@ const STYLE_DECLARATION_COMPUTED_PROPERTY_COUNT_GENERATION_SLOT: &str =
"__moliStyleDeclarationComputedPropertyCountGeneration";
const STYLE_DECLARATION_COMPUTED_PROPERTY_COUNT_TARGET_EPOCH_SLOT: &str =
"__moliStyleDeclarationComputedPropertyCountTargetEpoch";
-const DETACHED_STYLE_SLOT: &str = "__lmDetachedStyle";
pub(crate) fn is_live_style_declaration_object(
scope: &mut v8::PinScope<'_, '_>,
@@ -988,65 +984,12 @@ fn style_for_element<'s>(
scope: &mut v8::PinScope<'s, '_>,
receiver: v8::Local<'s, v8::Object>,
) -> Option<v8::Local<'s, v8::Object>> {
- if let Ok((runtime_ptr, handle)) = node_runtime_and_handle_from_object(scope, receiver) {
- return style_for_element_handle(scope, runtime_ptr, handle);
- }
- let Ok((runtime_ptr, handle)) =
- node_runtime_and_handle_from_object_or_detached(scope, receiver)
- else {
- return None;
- };
- if !node_is_element(unsafe { &*runtime_ptr }, handle) {
- return None;
- }
- if unsafe { &*runtime_ptr }
- .dom_host()
- .node(handle)
- .is_some_and(crate::dom::native::Node::is_connected)
- {
- return style_for_element_handle(scope, runtime_ptr, handle);
- }
- if let Some(style) = get_private_value(scope, receiver, DETACHED_STYLE_SLOT)
- .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
- {
- return Some(style);
- }
- let style = crate::detached_css_style::build_lightweight_detached_css_style_declaration(scope);
- if let Some(callback) = v8::Function::builder(detached_style_changed_callback)
- .data(receiver.into())
- .build(scope)
- {
- crate::detached_css_style::set_lightweight_css_style_change_callback(
- scope, style, callback,
- );
- }
- set_private_value(scope, receiver, DETACHED_STYLE_SLOT, style.into());
- Some(style)
-}
-
-fn detached_style_changed_callback<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
- _rv: v8::ReturnValue<'_, v8::Value>,
-) {
- let Ok(element) = v8::Local::<v8::Object>::try_from(args.data()) else {
- return;
- };
- let Ok((runtime_ptr, handle)) = node_runtime_and_handle_from_object_or_detached(scope, element)
- else {
- return;
- };
- if !node_is_element(unsafe { &*runtime_ptr }, handle) {
- return;
- }
- let Ok(style) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
- return;
- };
- let Some(css_text) = crate::detached_css_style::lightweight_css_style_css_text(scope, style)
- else {
- return;
- };
- unsafe { &mut *runtime_ptr }.set_attribute(scope, runtime_ptr, handle, "style", &css_text);
+ let (runtime_ptr, handle) =
+ node_runtime_and_handle_from_object_or_detached(scope, receiver).ok()?;
+ // Windowless elements have native handles too. Sharing their declaration
+ // with the handle preserves typed values, held style objects, and attribute
+ // synchronization when the element is adopted or connected.
+ style_for_element_handle(scope, runtime_ptr, handle)
}
pub(crate) fn style_for_element_handle<'s>(
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/inline_state.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/inline_state.rs
index 05e3bf568..665618ee1 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/inline_state.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/inline_state.rs
@@ -135,7 +135,7 @@ pub(super) fn inline_css_text_pdb_storage_state(
if entries
.iter()
.any(style_entry_is_pdb_supplemental_side_entry)
- || inline_css_text_contains_animation_shorthand(css_text)
+ || inline_css_text_requires_entry_projection(css_text)
{
let mut state = inline_style_declaration_state_from_entries(&entries);
state.refresh_pdb_entries();
@@ -147,10 +147,15 @@ pub(super) fn inline_css_text_pdb_storage_state(
})
}
-pub(super) fn inline_css_text_contains_animation_shorthand(css_text: &str) -> bool {
+fn inline_css_text_requires_entry_projection(css_text: &str) -> bool {
parse_css_declaration_list(css_text)
.into_iter()
- .any(|declaration| canonical_style_property_name(&declaration.name) == "animation")
+ .any(|declaration| {
+ let name = canonical_style_property_name(&declaration.name.to_ascii_lowercase());
+ // CSSOM's compatibility aliases are normalized by the entry parser;
+ // feeding the original name directly to Stylo can drop declarations.
+ name == "animation" || !name.eq_ignore_ascii_case(declaration.name.trim())
+ })
}
pub(super) fn inline_css_text_can_seed_plain_pdb_block(css_text: &str) -> bool {
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs
index f92d5c521..3c338b061 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/pdb_compat.rs
@@ -396,6 +396,7 @@ pub(super) fn cssom_style_shorthand_query_uses_pdb(name: &str) -> bool {
| "margin-inline"
| "mask"
| "outline"
+ | "overflow"
| "overscroll-behavior"
| "page-break-after"
| "page-break-before"
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs
index 453679ae6..8c4b8bc5f 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse/tests.rs
@@ -1262,11 +1262,26 @@ fn inline_state_pdb_queries_use_stored_block_when_it_wins_order() {
);
let overflow_state = CssInlineStyleDeclarationState {
- block: moli_css_parse::parse_declaration_block("overflow-x: hidden; overflow-y: scroll;"),
+ block: moli_css_parse::parse_declaration_block(
+ "overflow-x: hidden !important; overflow-y: scroll !important;",
+ ),
+ ..Default::default()
+ };
+ assert_eq!(
+ inline_state_property_value_with_pdb(&overflow_state, "overflow").as_deref(),
+ Some("hidden scroll")
+ );
+ assert_eq!(
+ inline_state_property_priority_with_pdb(&overflow_state, "overflow"),
+ Some(true)
+ );
+
+ let animation_state = CssInlineStyleDeclarationState {
+ block: moli_css_parse::parse_declaration_block("animation: spin 1s;"),
..Default::default()
};
assert!(
- inline_state_property_value_with_pdb(&overflow_state, "overflow").is_none(),
+ inline_state_property_value_with_pdb(&animation_state, "animation").is_none(),
"non-whitelisted shorthand queries must keep their CSSOM adapter semantics"
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/declarations.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/declarations.rs
index c63cf4419..719a38a7b 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/declarations.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom/extracted/declarations.rs
@@ -3013,3 +3013,30 @@ fn css_style_declaration_exposes_iterator() {
"true|function|values|0|false|true|false|true|true|color,margin-top|display,opacity|display|opacity"
);
}
+
+#[test]
+fn windowless_inline_style_tracks_native_attributes_and_adoption() {
+ let mut vm = new_parsed_test_vm(
+ "https://windowless-inline-style.test/",
+ "<!doctype html><body>",
+ );
+ assert_eq!(vm.eval(r#"(() => {
+ const owner = document.implementation.createHTMLDocument('');
+ const element = owner.createElement('div');
+ element.setAttribute('style', 'color: red');
+ const style = element.style;
+ if (style.color !== 'red' || element.style !== style) throw Error('initial style');
+ element.setAttribute('style', 'color: blue; --token: before');
+ if (style.color !== 'blue' || style.getPropertyValue('--token') !== 'before') throw Error('attribute sync');
+ style.setProperty('width', '12px', 'important');
+ if (!element.getAttribute('style').includes('12px')) throw Error('style write');
+ document.adoptNode(element);
+ document.body.append(element);
+ if (element.style !== style || style.width !== '12px') throw Error('adoption identity');
+ element.setAttribute('style', 'height: 9px');
+ if (style.height !== '9px' || style.width !== '') throw Error('held style');
+ element.removeAttribute('style');
+ if (style.length !== 0 || style.cssText !== '') throw Error('attribute removal');
+ return 'pass';
+ })()"#).unwrap(), "pass");
+}