Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(style): reconcile duplicate adopted stylesheets
ldm0fdba8d5ae65e
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/style_invalidation.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/style_invalidation.rsindex 48169de7d..3fc11a792 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/style_invalidation.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/style_invalidation.rs@@ -1587,6 +1587,88 @@ fn adopted_stylesheets_array_mutations_validate_and_resync() {));}++#[test]+fn duplicate_constructed_adopted_stylesheets_reconcile_document_and_shadow_cascade_order() {+ let mut vm = new_storage_test_vm("https://duplicate-adopted-cascade.test/");++ let result = vm+ .eval(+ r#"+(() => {+ const html = document.documentElement || document.appendChild(document.createElement('html'));+ const body = document.body || html.appendChild(document.createElement('body'));+ const target = body.appendChild(document.createElement('div'));+ const host = body.appendChild(document.createElement('section'));+ const shadow = host.attachShadow({ mode: 'open' });+ const shadowTarget = shadow.appendChild(document.createElement('div'));++ const makeSheets = () => {+ const zero = new CSSStyleSheet();+ zero.replaceSync('div { z-index: 0; color: blue; }');+ const one = new CSSStyleSheet();+ one.replaceSync('div { z-index: 1; color: blue; }');+ const red = new CSSStyleSheet();+ red.replaceSync('div { color: red; }');+ return { zero, one, red };+ };+ const read = element => {+ const style = getComputedStyle(element);+ return `${style.zIndex}:${style.color}`;+ };++ const doc = makeSheets();+ document.adoptedStyleSheets = [doc.one, doc.zero, doc.one];+ const documentDuplicate = read(target);+ document.adoptedStyleSheets = [];+ const documentEmpty = read(target);+ document.adoptedStyleSheets = [doc.one, doc.zero];+ const documentUnique = read(target);+ document.adoptedStyleSheets = [doc.one, doc.zero, doc.one, doc.red];+ const documentExpanded = read(target);+ document.adoptedStyleSheets = [];+ const documentRemoved = read(target);++ const scoped = makeSheets();+ shadow.adoptedStyleSheets = [scoped.one, scoped.zero, scoped.one];+ const shadowDuplicate = read(shadowTarget);+ shadow.adoptedStyleSheets = [];+ const shadowEmpty = read(shadowTarget);+ shadow.adoptedStyleSheets = [scoped.one, scoped.zero];+ const shadowUnique = read(shadowTarget);+ shadow.adoptedStyleSheets = [scoped.one, scoped.zero, scoped.one, scoped.red];+ const shadowExpanded = read(shadowTarget);+ shadow.adoptedStyleSheets = [];+ const shadowRemoved = read(shadowTarget);++ return [+ documentDuplicate,+ documentEmpty,+ documentUnique,+ documentExpanded,+ documentRemoved,+ shadowDuplicate,+ shadowEmpty,+ shadowUnique,+ shadowExpanded,+ shadowRemoved,+ ].join('|');+})()+"#,+ )+ .expect("duplicate adopted stylesheet cascades should reconcile without panicking");++ assert_eq!(+ result,+ concat!(+ "1:rgb(0, 0, 255)|auto:rgb(0, 0, 0)|",+ "0:rgb(0, 0, 255)|1:rgb(255, 0, 0)|auto:rgb(0, 0, 0)|",+ "1:rgb(0, 0, 255)|auto:rgb(0, 0, 0)|",+ "0:rgb(0, 0, 255)|1:rgb(255, 0, 0)|auto:rgb(0, 0, 0)"+ )+ );+}+#[test]fn fragment_change_without_target_selectors_preserves_style_generation() {let mut vm = new_parsed_test_vm(diff --git a/moli-renderer-v8/src/style_engine/active_stylesheets.rs b/moli-renderer-v8/src/style_engine/active_stylesheets.rsindex 54bbddcf9..70321b10b 100644--- a/moli-renderer-v8/src/style_engine/active_stylesheets.rs+++ b/moli-renderer-v8/src/style_engine/active_stylesheets.rs@@ -188,6 +188,17 @@ impl ActiveStylesheetCollection {&self.entries}+ /// Returns the Stylo-facing cascade list.+ ///+ /// The adoptedStyleSheets observable array may contain the same sheet more+ /// than once, but Stylo's stylesheet set requires unique sheet objects.+ /// Earlier occurrences are cascade-inert because the last occurrence of+ /// an identical sheet has the highest position, so retain only that last+ /// occurrence while preserving its relative order with other sheets.+ pub(super) fn cascade_stylesheets(&self) -> Vec<DocumentStyleSheet> {+ cascade_stylesheets(&self.entries)+ }+pub(super) fn matches_sources(&self, sources: &[StyloStylesheetSource]) -> bool {self.entries.len() == sources.len()&& self@@ -209,10 +220,7 @@ impl ActiveStylesheetCollection {}let mut previous = std::mem::take(&mut self.entries);- let previous_stylesheets = previous- .iter()- .map(|entry| entry.stylesheet().clone())- .collect::<Vec<_>>();+ let previous_stylesheets = cascade_stylesheets(&previous);let mut in_place_updates = Vec::new();self.entries = sources.iter()@@ -245,14 +253,12 @@ impl ActiveStylesheetCollection {next_entry}).collect();- let stylesheet_set_changed = previous_stylesheets- .iter()- .ne(self.entries.iter().map(ActiveStylesheet::stylesheet));+ let next_stylesheets = self.cascade_stylesheets();+ let stylesheet_set_changed = previous_stylesheets != next_stylesheets;let stylesheet_removed = previous_stylesheets.iter().any(|stylesheet| {- !self- .entries+ !next_stylesheets.iter()- .any(|entry| entry.stylesheet() == stylesheet)+ .any(|next_stylesheet| next_stylesheet == stylesheet)});Some(ActiveStylesheetReconciliation {previous_stylesheets,@@ -263,6 +269,19 @@ impl ActiveStylesheetCollection {}}+fn cascade_stylesheets(entries: &[ActiveStylesheet]) -> Vec<DocumentStyleSheet> {+ entries+ .iter()+ .enumerate()+ .filter(|(index, entry)| {+ !entries[index + 1..]+ .iter()+ .any(|later| later.stylesheet() == entry.stylesheet())+ })+ .map(|(_, entry)| entry.stylesheet().clone())+ .collect()+}+fn has_same_installation(previous: &StyloStylesheetSource, next: &StyloStylesheetSource) -> bool {match (previous.adopted_client_id(), next.adopted_client_id()) {(Some(previous), Some(next)) => previous == next,diff --git a/moli-renderer-v8/src/style_engine/retained.rs b/moli-renderer-v8/src/style_engine/retained.rsindex 2887ef505..d24eff2d1 100644--- a/moli-renderer-v8/src/style_engine/retained.rs+++ b/moli-renderer-v8/src/style_engine/retained.rs@@ -112,8 +112,8 @@ pub(super) fn build_retained_style_system();{let guard = shared_lock.read();- for entry in document_stylesheets.entries() {- stylist.append_stylesheet(entry.stylesheet().clone(), &guard);+ for stylesheet in document_stylesheets.cascade_stylesheets() {+ stylist.append_stylesheet(stylesheet, &guard);}}let mut shadow_scopes = Vec::new();@@ -124,13 +124,10 @@ pub(super) fn build_retained_style_system(let mut author_styles = AuthorStyles::<DocumentStyleSheet>::new();let custom_media = CustomMediaMap::default();let guard = shared_lock.read();- for entry in active_stylesheets.entries() {- author_styles.stylesheets.append_stylesheet(- None,- &custom_media,- entry.stylesheet().clone(),- &guard,- );+ for stylesheet in active_stylesheets.cascade_stylesheets() {+ author_styles+ .stylesheets+ .append_stylesheet(None, &custom_media, stylesheet, &guard);}author_styles.flush(&mut stylist, &guard);shadow_cascade_data.push((*root, author_styles.data.clone()));@@ -401,12 +398,7 @@ fn update_document_scope(let guard = shared_lock.read();scope_fallback |= reconciliation.stylesheet_removed();if reconciliation.stylesheet_set_changed() {- let next_stylesheets = retained- .document_stylesheets- .entries()- .iter()- .map(|entry| entry.stylesheet().clone())- .collect::<Vec<_>>();+ let next_stylesheets = retained.document_stylesheets.cascade_stylesheets();update_document_stylesheet_set(&mut retained.stylist,reconciliation.previous_stylesheets(),diff --git a/moli-renderer-v8/src/style_engine/shadow_scopes.rs b/moli-renderer-v8/src/style_engine/shadow_scopes.rsindex 8bd76957d..1047d8d5e 100644--- a/moli-renderer-v8/src/style_engine/shadow_scopes.rs+++ b/moli-renderer-v8/src/style_engine/shadow_scopes.rs@@ -313,11 +313,11 @@ fn build_shadow_scope(let mut author_styles = AuthorStyles::<DocumentStyleSheet>::new();let custom_media = CustomMediaMap::default();let guard = shared_lock.read();- for entry in active_stylesheets.entries() {+ for stylesheet in active_stylesheets.cascade_stylesheets() {author_styles.stylesheets.append_stylesheet(Some(stylist.device()),&custom_media,- entry.stylesheet().clone(),+ stylesheet,&guard,);}@@ -337,12 +337,7 @@ fn apply_stylesheet_set_reconciliation(let custom_media = CustomMediaMap::default();let guard = shared_lock.read();if reconciliation.stylesheet_set_changed() {- let next_stylesheets = scope- .active_stylesheets()- .entries()- .iter()- .map(|entry| entry.stylesheet().clone())- .collect::<Vec<_>>();+ let next_stylesheets = scope.active_stylesheets().cascade_stylesheets();update_shadow_stylesheet_set(scope.author_styles_mut(),stylist.device(),diff --git a/moli-renderer-v8/src/style_engine/source_cascade.rs b/moli-renderer-v8/src/style_engine/source_cascade.rsindex 9eaf6a133..5bc9c517e 100644--- a/moli-renderer-v8/src/style_engine/source_cascade.rs+++ b/moli-renderer-v8/src/style_engine/source_cascade.rs@@ -187,6 +187,13 @@ fn installed_sources_by_id(continue;}let (sources, stylesheets) = sources_by_id.entry(source_id).or_default();+ if let Some(index) = stylesheets+ .iter()+ .position(|stylesheet| stylesheet == entry.stylesheet())+ {+ sources.remove(index);+ stylesheets.remove(index);+ }sources.push(entry.source().clone());stylesheets.push(entry.stylesheet().clone());}