Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(selectors): skip isolated HTML descendants in auto directionality

ldm03840f92a1cda
diff --git a/moli-selector/src/lib.rs b/moli-selector/src/lib.rs
index 65921821e..5f5c28840 100644
--- a/moli-selector/src/lib.rs
+++ b/moli-selector/src/lib.rs
@@ -1451,6 +1451,35 @@ mod tests {
);
}
+ #[test]
+ fn dir_auto_ignores_directionally_isolated_html_descendants() {
+ for local_name in ["script", "style", "textarea", "bdi", "span"] {
+ let url = url::Url::parse("https://example.test/").unwrap();
+ let mut host = DomHost::from_dom(NativeDom::new_html(url));
+ host.reset_html_document_shell();
+ let body = host.document_body_handle().unwrap();
+ let parent = host.create_element("div");
+ let descendant = host.create_element(local_name);
+ let latin = host.create_text_node("A");
+ let hebrew = host.create_text_node("\u{05ea}");
+ assert!(host.set_attribute(parent, "dir", "auto"));
+ assert!(host.append_child(descendant, latin));
+ assert!(host.append_child(parent, descendant));
+ assert!(host.append_child(parent, hebrew));
+ assert!(host.append_child(body, parent));
+ let engine = QueryEngine;
+ assert_eq!(
+ engine.matches_host(&host, parent, ":dir(rtl)").unwrap(),
+ local_name != "span",
+ "{local_name} must use its HTML auto-direction isolation rule"
+ );
+ assert_eq!(
+ engine.matches_host(&host, parent, ":dir(ltr)").unwrap(),
+ local_name == "span"
+ );
+ }
+ }
+
#[test]
fn dom_api_selectors_dir_auto_uses_first_strong_text() {
let url = url::Url::parse("https://example.test/").unwrap();
diff --git a/moli-selector/src/stylo/query/pseudo.rs b/moli-selector/src/stylo/query/pseudo.rs
index ce304c473..9dd9fe46c 100644
--- a/moli-selector/src/stylo/query/pseudo.rs
+++ b/moli-selector/src/stylo/query/pseudo.rs
@@ -664,7 +664,11 @@ fn auto_direction_for_element(host: &DomHost, root: NodeId) -> Option<CssDirecti
}
fn descendant_is_directionally_isolated_for_auto(element: &Element) -> bool {
- if element.is_html_element("bdi") {
+ if matches!(
+ element.local_name(),
+ "bdi" | "script" | "style" | "textarea"
+ ) && element.namespace() == "http://www.w3.org/1999/xhtml"
+ {
return true;
}
element.attribute("dir").is_some_and(|value| {