Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(selection): respect document and modal inert boundaries

ldm031fbadca9bc8
diff --git a/moli-renderer-v8/src/context_bootstrap/range_algorithms/text.rs b/moli-renderer-v8/src/context_bootstrap/range_algorithms/text.rs
index 23741ad40..ceed15db5 100644
--- a/moli-renderer-v8/src/context_bootstrap/range_algorithms/text.rs
+++ b/moli-renderer-v8/src/context_bootstrap/range_algorithms/text.rs
@@ -1,5 +1,6 @@
use super::ordering::point_order_handles;
use super::*;
+use crate::native_bridge::document::XHTML_NS;
use crate::native_bridge::element::{
StyleMode, observable_sources_with_fragments, style_property_value,
};
@@ -78,7 +79,7 @@ pub(in crate::context_bootstrap) fn range_selection_string_contents<'s>(
}
};
let root_state = SelectionTextState {
- active_modal_dialog: selection_text_active_modal_dialog(runtime),
+ active_modal_dialog: selection_text_active_modal_dialog(runtime, document),
..Default::default()
};
let mut out = String::new();
@@ -227,7 +228,11 @@ fn append_selection_rendered_text(
if selection_text_element_is_hidden(runtime, handle) {
return Some(());
}
- if selection_text_element_is_inert(runtime, handle) {
+ if selection_text_element_is_inert(runtime, handle)
+ && !state.active_modal_dialog.is_some_and(|dialog| {
+ selection_text_descendant_or_self(runtime, dialog, handle)
+ })
+ {
return Some(());
}
if element.is_html_element("head")
@@ -361,25 +366,32 @@ fn selection_text_element_is_inert(
.dom_host()
.node(handle)
.and_then(|node| node.as_element())
- .is_some_and(|element| element.attribute("inert").is_some())
+ .is_some_and(|element| {
+ element.namespace() == XHTML_NS && element.attribute("inert").is_some()
+ })
}
fn selection_text_has_inert_ancestor(
runtime: &crate::native_bridge::JsContextHost,
handle: DomHandle,
+ modal_escape_root: Option<DomHandle>,
) -> bool {
let mut current = Some(handle);
while let Some(handle) = current {
if selection_text_element_is_inert(runtime, handle) {
return true;
}
- current = runtime.dom_host().parent_node(handle);
+ if Some(handle) == modal_escape_root {
+ return false;
+ }
+ current = selection_text_flat_tree_parent(runtime, handle);
}
false
}
fn selection_text_active_modal_dialog(
runtime: &crate::native_bridge::JsContextHost,
+ document: DomHandle,
) -> Option<DomHandle> {
runtime
.dom_host()
@@ -388,7 +400,9 @@ fn selection_text_active_modal_dialog(
.iter()
.rev()
.find_map(|node| {
- if !node.is_connected() {
+ if !node.is_connected()
+ || runtime.dom_host().owner_document_handle(node.id()) != Some(document)
+ {
return None;
}
let element = node.as_element()?;
@@ -404,10 +418,12 @@ fn selection_text_is_inert(
handle: DomHandle,
state: SelectionTextState,
) -> bool {
- selection_text_has_inert_ancestor(runtime, handle)
- || state
- .active_modal_dialog
- .is_some_and(|dialog| !selection_text_descendant_or_self(runtime, handle, dialog))
+ let modal_escape_root = match state.active_modal_dialog {
+ Some(dialog) if selection_text_descendant_or_self(runtime, handle, dialog) => Some(dialog),
+ Some(_) => return true,
+ None => None,
+ };
+ selection_text_has_inert_ancestor(runtime, handle, modal_escape_root)
}
fn selection_text_descendant_or_self(
@@ -420,11 +436,25 @@ fn selection_text_descendant_or_self(
if handle == ancestor {
return true;
}
- current = runtime.dom_host().parent_node(handle);
+ current = selection_text_flat_tree_parent(runtime, handle);
}
false
}
+fn selection_text_flat_tree_parent(
+ runtime: &crate::native_bridge::JsContextHost,
+ handle: DomHandle,
+) -> Option<DomHandle> {
+ if let Some(slot) = runtime.dom_host().assigned_slot_for_node(handle) {
+ return Some(slot);
+ }
+ let parent = runtime.dom_host().parent_node(handle)?;
+ if runtime.dom_host().is_shadow_root(parent) {
+ return runtime.dom_host().shadow_root_host(parent);
+ }
+ Some(parent)
+}
+
fn selection_text_user_select_value(
runtime: &crate::native_bridge::JsContextHost,
handle: DomHandle,
diff --git a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs b/moli-renderer-v8/src/native_bridge/document/lifecycle.rs
index c7a9c6f3d..70755cc2c 100644
--- a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs
+++ b/moli-renderer-v8/src/native_bridge/document/lifecycle.rs
@@ -624,7 +624,7 @@ fn exec_command_select_all_target(
runtime: &JsContextHost,
document_handle: DomHandle,
) -> Option<DomHandle> {
- let active_modal_dialog = current_modal_dialog(runtime);
+ let active_modal_dialog = current_modal_dialog(runtime, document_handle);
if let Some(active) = runtime.active_element_handle()
&& let Some(editing_host) = contenteditable_editing_host(runtime, active)
&& active_modal_dialog
@@ -644,7 +644,7 @@ fn exec_command_select_all_target(
.or(Some(document_handle))
}
-fn current_modal_dialog(runtime: &JsContextHost) -> Option<DomHandle> {
+fn current_modal_dialog(runtime: &JsContextHost, document: DomHandle) -> Option<DomHandle> {
runtime
.dom_host()
.dom()
@@ -652,7 +652,9 @@ fn current_modal_dialog(runtime: &JsContextHost) -> Option<DomHandle> {
.iter()
.rev()
.find_map(|node| {
- if !node.is_connected() {
+ if !node.is_connected()
+ || runtime.dom_host().owner_document_handle(node.id()) != Some(document)
+ {
return None;
}
let element = node.as_element()?;
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs
index fb750ed3e..6d0f27448 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage.rs
@@ -9094,3 +9094,78 @@ fn document_get_selection_uses_associated_window_selection() {
"true|true|true|false|true|[object Selection]||0|true|true|true|true|true|true|true|true|false|true|true|0"
);
}
+
+#[test]
+fn modal_dialog_selection_inertness_is_scoped_to_its_document() {
+ let mut vm = new_storage_test_vm("https://selection-modal-document-scope.test/");
+
+ let result = vm
+ .eval(
+ r#"
+ (() => {
+ const html = document.documentElement ||
+ document.appendChild(document.createElement("html"));
+ const body = document.body || html.appendChild(document.createElement("body"));
+ body.textContent = "";
+ const parentText = document.createElement("p");
+ parentText.textContent = "parent outside";
+ const parentDialog = document.createElement("dialog");
+ parentDialog.textContent = "parent dialog";
+ const frame = document.createElement("iframe");
+ body.append(parentText, parentDialog, frame);
+
+ const childDocument = frame.contentDocument;
+ const childHtml = childDocument.documentElement ||
+ childDocument.appendChild(childDocument.createElement("html"));
+ const childBody = childDocument.body ||
+ childHtml.appendChild(childDocument.createElement("body"));
+ childBody.textContent = "";
+ const childText = childDocument.createElement("p");
+ childText.textContent = "child outside";
+ const childDialog = childDocument.createElement("dialog");
+ childDialog.textContent = "child dialog";
+ childBody.append(childText, childDialog);
+
+ const selectedText = (selection, root) => {
+ selection.removeAllRanges();
+ selection.selectAllChildren(root);
+ return selection.toString();
+ };
+ const parentSelection = getSelection();
+ const childSelection = frame.contentWindow.getSelection();
+
+ parentDialog.showModal();
+ const parentWithParentModal = selectedText(parentSelection, body);
+ const childWithParentModal = selectedText(childSelection, childBody);
+ childSelection.removeAllRanges();
+ const childCommandWithParentModal = childDocument.execCommand("selectAll");
+ const childCommandTextWithParentModal = childSelection.toString();
+ parentDialog.close();
+
+ childDialog.showModal();
+ const parentWithChildModal = selectedText(parentSelection, body);
+ const childWithChildModal = selectedText(childSelection, childBody);
+ parentSelection.removeAllRanges();
+ const parentCommandWithChildModal = document.execCommand("selectAll");
+ const parentCommandTextWithChildModal = parentSelection.toString();
+
+ return JSON.stringify({
+ parentWithParentModal,
+ childWithParentModal,
+ childCommandWithParentModal,
+ childCommandTextWithParentModal,
+ parentWithChildModal,
+ childWithChildModal,
+ parentCommandWithChildModal,
+ parentCommandTextWithChildModal
+ });
+ })()
+ "#,
+ )
+ .expect("modal selection inertness should remain document-scoped");
+
+ assert_eq!(
+ result,
+ r#"{"parentWithParentModal":"parent dialog","childWithParentModal":"child outside","childCommandWithParentModal":true,"childCommandTextWithParentModal":"child outside","parentWithChildModal":"parent outside","childWithChildModal":"child dialog","parentCommandWithChildModal":true,"parentCommandTextWithChildModal":"parent outside"}"#
+ );
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index 2e3f7a9ed..0cc2ed0ea 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -3995,10 +3995,12 @@ fn exec_command_select_all_respects_modal_dialog_inertness() {
body.textContent = "";
body.append(
document.createTextNode("Here is a text node you can't select while the dialog is open."),
- document.createElement("dialog"),
+ document.createElement("div"),
document.createTextNode("Trailing text.")
);
- const dialog = body.querySelector("dialog");
+ const wrapper = body.querySelector("div");
+ const dialog = document.createElement("dialog");
+ wrapper.appendChild(dialog);
dialog.textContent = "I'm selectable.";
const selection = getSelection();
@@ -4016,6 +4018,11 @@ fn exec_command_select_all_respects_modal_dialog_inertness() {
commandRange.endContainer === dialog &&
commandRange.endOffset === dialog.childNodes.length;
+ wrapper.inert = true;
+ selection.selectAllChildren(body);
+ const inertAncestorText = selection.toString();
+ wrapper.inert = false;
+
dialog.close();
selection.selectAllChildren(body);
const afterCloseText = selection.toString();
@@ -4025,6 +4032,7 @@ fn exec_command_select_all_respects_modal_dialog_inertness() {
commandReturned,
commandText,
commandRangeSpansDialog,
+ inertAncestorText,
afterCloseHasOutside: afterCloseText.includes("text node you can't select"),
afterCloseHasDialog: afterCloseText.includes("I'm selectable."),
afterCloseHasTrailing: afterCloseText.includes("Trailing text.")
@@ -4036,7 +4044,7 @@ fn exec_command_select_all_respects_modal_dialog_inertness() {
assert_eq!(
result,
- r#"{"manualBodyText":"I'm selectable.","commandReturned":true,"commandText":"I'm selectable.","commandRangeSpansDialog":true,"afterCloseHasOutside":true,"afterCloseHasDialog":false,"afterCloseHasTrailing":true}"#
+ r#"{"manualBodyText":"I'm selectable.","commandReturned":true,"commandText":"I'm selectable.","commandRangeSpansDialog":true,"inertAncestorText":"I'm selectable.","afterCloseHasOutside":true,"afterCloseHasDialog":false,"afterCloseHasTrailing":true}"#
);
}
@@ -4283,6 +4291,69 @@ fn selection_to_string_uses_rendered_native_range_projection() {
);
}
+#[test]
+fn selection_only_applies_inert_attribute_to_html_elements() {
+ let mut vm = new_storage_test_vm("https://selection-html-inert-namespace.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 root = document.createElement('div');
+ body.appendChild(root);
+ const selection = getSelection();
+ const mathml = 'http://www.w3.org/1998/Math/MathML';
+
+ const selectedText = element => {
+ selection.removeAllRanges();
+ selection.selectAllChildren(element);
+ return selection.toString();
+ };
+ const mathWithText = text => {
+ const math = document.createElementNS(mathml, 'math');
+ const mi = document.createElementNS(mathml, 'mi');
+ mi.textContent = text;
+ math.appendChild(mi);
+ return { math, mi };
+ };
+
+ const own = mathWithText('math own');
+ own.math.setAttribute('inert', '');
+ own.mi.setAttribute('inert', '');
+ root.appendChild(own.math);
+
+ const nested = mathWithText('math ancestors');
+ nested.math.setAttribute('inert', '');
+ nested.mi.setAttribute('inert', '');
+ root.appendChild(nested.math);
+
+ const htmlChild = document.createElement('span');
+ htmlChild.textContent = 'html child';
+ htmlChild.inert = true;
+ root.appendChild(htmlChild);
+
+ const htmlAncestor = document.createElement('div');
+ htmlAncestor.inert = true;
+ const inherited = mathWithText('html ancestor');
+ htmlAncestor.appendChild(inherited.math);
+ root.appendChild(htmlAncestor);
+
+ return [
+ selectedText(own.math),
+ selectedText(nested.math),
+ selectedText(htmlChild),
+ selectedText(htmlAncestor)
+ ].join('|');
+})()
+"#,
+ )
+ .expect("Selection inert namespace probe should evaluate");
+
+ assert_eq!(result, "math own|math ancestors||");
+}
+
#[test]
fn month_and_week_inputs_do_not_support_variable_length_selection() {
let mut vm = new_storage_test_vm("https://forms-selection-temporal.test/");