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): preserve collapsed inline whitespace

ldm03cdd95f6d51e
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 22facdd0c..0a38adeb9 100644
--- a/moli-renderer-v8/src/context_bootstrap/range_algorithms/text.rs
+++ b/moli-renderer-v8/src/context_bootstrap/range_algorithms/text.rs
@@ -495,15 +495,11 @@ fn append_rendered_text_node(out: &mut String, text: &str) {
let trailing = &text[last_non_ws..];
let body = collapse_whitespace(&text[first_non_ws..last_non_ws]);
- if leading.chars().any(char::is_whitespace)
- && !leading.contains('\n')
- && !out.is_empty()
- && !out.ends_with([' ', '\n'])
- {
+ if leading.chars().any(char::is_whitespace) && !out.is_empty() && !out.ends_with([' ', '\n']) {
out.push(' ');
}
out.push_str(&body);
- if trailing.chars().any(char::is_whitespace) && !trailing.contains('\n') {
+ if trailing.chars().any(char::is_whitespace) {
out.push(' ');
}
}
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 8ff2b9c1f..0e1670714 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
@@ -4248,7 +4248,11 @@ fn selection_to_string_uses_rendered_native_range_projection() {
const contentHidden = document.createElement("div");
contentHidden.setAttribute("style", "content-visibility: hidden");
contentHidden.textContent = "hidden content";
- root.append(basic, nested, container, contentHidden);
+ const inlineWhitespace = document.createElement("div");
+ inlineWhitespace.append("alpha\n ", Object.assign(document.createElement("span"), {
+ textContent: "\n beta\n"
+ }), "\n gamma");
+ root.append(basic, nested, container, contentHidden, inlineWhitespace);
const scriptStyleRange = document.createRange();
scriptStyleRange.selectNode(p);
const scriptStyle = selectedStringFor(scriptStyleRange).replace(/\r\n/g, "\n");
@@ -4265,7 +4269,8 @@ fn selection_to_string_uses_rendered_native_range_projection() {
selectContents(basic),
selectContents(nested),
selectContents(container),
- selectContents(contentHidden)
+ selectContents(contentHidden),
+ selectContents(inlineWhitespace)
].join("|");
})()
"##,
@@ -4274,7 +4279,7 @@ fn selection_to_string_uses_rendered_native_range_projection() {
assert_eq!(
result,
- "\nstyle text line\nfunction x() { return 1; }\n\nPASS|Hell|ac|start end|selectabletext|"
+ "\nstyle text line\nfunction x() { return 1; }\n\nPASS|Hell|ac|start end|selectabletext||alpha beta gamma"
);
}