Skip to content

The headless browser for AI agents and web scraping

fix(render): hide closed dialogs through the UA cascade (#1568)

SGavrl2c81268d8579
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index d92a8446..91b6200e 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -25611,6 +25611,27 @@ mod tests {
);
}
+ #[cfg(feature = "render")]
+ #[test]
+ fn closed_dialog_ua_visibility_tracks_open_mutations_in_retained_styles() {
+ let mut rt = setup_runtime(r#"<!doctype html><style>dialog{width:80px;height:30px}</style>
+ <dialog id=target><div>hidden content</div></dialog>"#);
+ assert_eq!(rt.evaluate(r#"(() => {
+ const dialog = document.getElementById('target');
+ const style = getComputedStyle(dialog);
+ const read = () => [style.display, dialog.getBoundingClientRect().height];
+ const result = [read()];
+ dialog.show(); result.push(read());
+ dialog.close(); result.push(read());
+ dialog.open = true; result.push(read());
+ dialog.removeAttribute('open'); result.push(read());
+ dialog.style.display = 'block'; result.push(read());
+ return result;
+ })()"#).unwrap(), serde_json::json!([
+ ["none",0],["block",30],["none",0],["block",30],["none",0],["block",30]
+ ]));
+ }
+
#[test]
fn test_promise_rejection_event_requires_promise() {
let mut rt = setup_runtime("<html><body></body></html>");
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rs
index 04941129..8acdd0c8 100644
--- a/crates/obscura-render/src/dom.rs
+++ b/crates/obscura-render/src/dom.rs
@@ -511,7 +511,8 @@ pub fn retained_attribute_mutation_kind(
| "valign"
| "viewbox"
| "width"
- ) || (local == "input" && matches!(name.as_str(), "size" | "type" | "value"))
+ ) || (local == "dialog" && name == "open")
+ || (local == "input" && matches!(name.as_str(), "size" | "type" | "value"))
|| (matches!(local.as_str(), "select" | "font") && name == "size")
|| (local == "textarea" && matches!(name.as_str(), "cols" | "rows" | "wrap"))
|| matches!(name.as_str(), "dir" | "lang" | "xml:lang")
@@ -2782,6 +2783,11 @@ fn cascade_node_style(
_ => {}
}
}
+ if elem.local.as_ref() == "dialog" && elem.ns.as_ref() == "http://www.w3.org/1999/xhtml"
+ && node.get_attribute("open").is_none()
+ {
+ style.display = crate::Display::None;
+ }
// UA rule `[hidden]:not([hidden=until-found]) { display: none }`.
// Applied before the author cascade so a matching author `display`
// still wins (UA origin, per the HTML rendering spec). Sites ship
diff --git a/crates/obscura-render/tests/layout_test.rs b/crates/obscura-render/tests/layout_test.rs
index 4ad80996..efcb5af7 100644
--- a/crates/obscura-render/tests/layout_test.rs
+++ b/crates/obscura-render/tests/layout_test.rs
@@ -487,6 +487,28 @@ fn css_table_mixed_children_are_not_discarded() {
}
}
+#[test]
+fn closed_html_dialogs_use_a_ua_display_rule_below_author_styles() {
+ let tree = parse_html(r#"<!doctype html><style>
+ html,body{margin:0} dialog{width:80px;height:30px}
+ #authored{display:block} #contents{display:contents}
+ #contents > div{width:20px;height:10px}
+ </style><dialog id=closed><div id=closed_child>hidden</div></dialog>
+ <dialog id=opened open></dialog><dialog id=authored></dialog>
+ <dialog id=contents><div id=visible_child></div></dialog>
+ <svg><dialog id=svg_dialog></dialog></svg>"#);
+ let layout = layout_dom(&tree, (400.0,300.0));
+ for name in ["closed", "closed_child"] {
+ assert!(!layout.rects.contains_key(&tree.get_element_by_id(name).unwrap()), "{name} generated a box");
+ }
+ for name in ["opened", "authored"] {
+ let rect = layout.rects[&tree.get_element_by_id(name).unwrap()];
+ assert_eq!((rect.width,rect.height),(80.0,30.0), "{name}");
+ }
+ assert!(layout.rects.contains_key(&tree.get_element_by_id("visible_child").unwrap()));
+ assert_ne!(layout.styles[&tree.get_element_by_id("svg_dialog").unwrap()].display, obscura_render::Display::None);
+}
+
#[test]
fn table_rowspans_are_clipped_to_the_originating_row_group() {
let tree = parse_html(