fix(selection): preserve collapsed inline whitespace

This commit is contained in:
ldm0
2026-09-09 06:52:21 +08:00
parent d4cc13fbcf
commit e438ce489b
2 changed files with 10 additions and 9 deletions
@@ -498,15 +498,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(' ');
}
}
@@ -4475,7 +4475,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");
@@ -4492,7 +4496,8 @@ fn selection_to_string_uses_rendered_native_range_projection() {
selectContents(basic),
selectContents(nested),
selectContents(container),
selectContents(contentHidden)
selectContents(contentHidden),
selectContents(inlineWhitespace)
].join("|");
})()
"##,
@@ -4501,7 +4506,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"
);
}