From cbf066f564094e4b7de73dc180acc9580b232bda Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 23:33:52 -0600 Subject: [PATCH] fix(id): collision-proof spaced-destination placeholders on save (#1348) Salvage of closed archive PR #7374 (task 14087, issue #7297). The save path mints a bare `idweb-spaced-destination-{n}-placeholder` token per whitespace-carrying link/image destination and restores it with a plain `str::replace`. A document whose own text contains that token therefore has its words rewritten on save, and the replacement is not confined to destination positions. - Mint placeholders against the document: `ast_contains_literal` scans the converted AST (text, code, html, code-block info, link/image url and title, plus a Debug fallback for every other node kind), and `fresh_spaced_destination_placeholder` skips taken or colliding candidates. - Restore positionally: `restore_spaced_destination` rewrites the token only where it is a rendered destination (after `](`, followed by `)`, whitespace, or a title opener). - Read the destination before minting: the collision scan borrows the AST immutably, so the old `&mut link.url` borrow would panic. - Regression test covers literal text, code span, fenced block, several colliding literals with a deduped destination, and a titled one. --- pkgs/id/src/web/markdown.rs | 177 +++++++++++++++++++++++++++++++++--- 1 file changed, 162 insertions(+), 15 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index babd380627..39551e7981 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -4323,8 +4323,8 @@ pub fn prosemirror_to_markdown(doc: &Value) -> Result { let substitutions = substitute_spaced_destinations(root); let mut output = String::new(); format_commonmark(root, &commonmark_options(), &mut output)?; - for (placeholder, raw) in substitutions { - output = output.replace(&placeholder, &format!("<{raw}>")); + for (placeholder, raw) in &substitutions { + output = restore_spaced_destination(&output, placeholder, raw); } for token in fence_tokens { output = strip_fence_placeholder(&output, &token); @@ -5157,6 +5157,88 @@ fn strip_fence_placeholder(output: &str, token: &str) -> String { result } +/// Whether any string content already in the converted AST contains +/// `needle` (#1348): text/code/html literals, code-block info strings, +/// link/image urls and titles, footnote names — plus a `Debug` fallback +/// covering any other string-carrying node kind. A generated +/// spaced-destination placeholder must avoid all of these, otherwise the +/// post-render restoration would rewrite the document's own words. +fn ast_contains_literal<'a>(root: &'a AstNode<'a>, needle: &str) -> bool { + root.descendants().any(|node| { + let value = node.data.borrow(); + match &value.value { + NodeValue::Text(literal) => literal.contains(needle), + NodeValue::Code(code) => code.literal.contains(needle), + NodeValue::CodeBlock(block) => { + block.literal.contains(needle) || block.info.contains(needle) + } + NodeValue::HtmlBlock(html) => html.literal.contains(needle), + NodeValue::HtmlInline(html) => html.contains(needle), + NodeValue::Link(link) | NodeValue::Image(link) => { + link.url.contains(needle) || link.title.contains(needle) + } + // Fallback for every other node kind (front matter, + // footnote names, math, …): its `Debug` rendering embeds the + // carried strings, and the placeholder token is plain + // alphanumerics-plus-dashes so debug escaping cannot hide it. + other => format!("{other:?}").contains(needle), + } + }) +} + +/// Mint a spaced-destination placeholder that occurs nowhere in the +/// document already (#1348). `counter` advances past every candidate so +/// skipped indices are never reused; the `-{n}-placeholder` shape keeps +/// tokens mutually prefix-free (as with the #1496 fence tokens). +fn fresh_spaced_destination_placeholder<'a>( + root: &'a AstNode<'a>, + taken: &[(String, String)], + mut counter: usize, +) -> (String, usize) { + loop { + let candidate = format!("idweb-spaced-destination-{counter}-placeholder"); + counter += 1; + if taken + .iter() + .any(|(placeholder, _)| placeholder == &candidate) + || ast_contains_literal(root, &candidate) + { + continue; + } + return (candidate, counter); + } +} + +/// Restore one spaced destination (#1348): ONLY where the placeholder +/// sits in a rendered link/image destination — immediately after `](` +/// and followed by `)`, whitespace (a title follows), or a title +/// opener (`"`, `'`, `(`). A plain `str::replace` would also rewrite +/// the token in user prose or code that happens to contain it. +fn restore_spaced_destination(output: &str, placeholder: &str, raw: &str) -> String { + let replacement = format!("<{raw}>"); + let mut result = String::with_capacity(output.len()); + let mut rest = output; + while let Some(pos) = rest.find(placeholder) { + let before = &rest[..pos]; + let after = &rest[pos + placeholder.len()..]; + let in_destination = before.ends_with("](") + && after + .chars() + .next() + .is_some_and(|c| c == ')' || c.is_whitespace() || matches!(c, '"' | '\'' | '(')); + if in_destination { + result.push_str(before); + result.push_str(&replacement); + rest = after; + } else { + result.push_str(&rest[..pos + placeholder.len()]); + rest = after; + } + } + result.push_str(rest); + result +} + /// Replace every link/image destination containing whitespace (space, /// tab, newline, …) with a unique placeholder token, returning /// (placeholder, raw destination) pairs for restoration after rendering @@ -5166,32 +5248,52 @@ fn strip_fence_placeholder(output: &str, token: &str) -> String { /// through here too: comrak resolves a reference definition into the /// referencing link's `url`, and the PM model stores that href directly, /// so no separate definition path exists in this codec. +/// +/// Placeholders are minted collision-proof (#1348): each candidate is +/// checked against the document's existing text, so a document that +/// literally contains `idweb-spaced-destination-0-placeholder` bumps +/// the real substitution to a higher index instead of letting the +/// restoration rewrite the document's own words. fn substitute_spaced_destinations<'a>(root: &'a AstNode<'a>) -> Vec<(String, String)> { let mut substitutions: Vec<(String, String)> = Vec::new(); + let mut counter: usize = 0; let mut stack: Vec<&AstNode<'a>> = vec![root]; while let Some(node) = stack.pop() { stack.extend(node.children()); - let mut data = node.data.borrow_mut(); - let url = match &mut data.value { - NodeValue::Link(link) | NodeValue::Image(link) => &mut link.url, + // Read the destination first: minting a placeholder scans the + // whole AST, which must not run while this node's data is + // mutably borrowed (RefCell would panic on the re-entrant + // immutable borrow of the same node). + let url = match &node.data.borrow().value { + NodeValue::Link(link) | NodeValue::Image(link) => link.url.clone(), _ => continue, }; if !url.contains(char::is_whitespace) { continue; } - if let Some(index) = substitutions + if let Some(existing) = substitutions .iter() - .position(|(_, raw)| raw.as_str() == url.as_str()) + .find(|(_, raw)| raw.as_str() == url.as_str()) { - *url = substitutions[index].0.clone(); + let placeholder = existing.0.clone(); + let mut data = node.data.borrow_mut(); + match &mut data.value { + NodeValue::Link(link) | NodeValue::Image(link) => { + link.url = placeholder; + } + _ => continue, + } } else { - let placeholder = format!( - "idweb-spaced-destination-{}-placeholder", - substitutions.len() - ); - let raw = std::mem::take(url); - *url = placeholder.clone(); - substitutions.push((placeholder, raw)); + let (placeholder, next) = + fresh_spaced_destination_placeholder(root, &substitutions, counter); + counter = next; + let mut data = node.data.borrow_mut(); + let slot = match &mut data.value { + NodeValue::Link(link) | NodeValue::Image(link) => &mut link.url, + _ => continue, + }; + *slot = placeholder.clone(); + substitutions.push((placeholder, url)); } } substitutions @@ -8096,6 +8198,51 @@ mod tests { ); } + #[test] + fn test_spaced_destination_placeholder_collision_preserves_literal_text() { + // #1348: a document that literally contains the placeholder + // token must round-trip with its own words intact. The + // substitution mints a non-colliding token and restoration only + // touches destination positions, so the literal paragraph, code + // span, and fenced block survive while the real spaced + // destination still saves in angle form. + let original = "idweb-spaced-destination-0-placeholder\n\n[notes]()\n"; + let (first, second) = two_saves(original); + assert_eq!(first, original, "literal token rewritten: {first:?}"); + assert_eq!(first, second, "fixpoint"); + assert!(!first.contains("%20"), "no percent-encoding: {first:?}"); + + let original = "`idweb-spaced-destination-0-placeholder`\n\n[notes]()\n"; + let (first, second) = two_saves(original); + assert_eq!( + first, original, + "literal token in code rewritten: {first:?}" + ); + assert_eq!(first, second, "fixpoint"); + + let original = + "```text\nidweb-spaced-destination-0-placeholder\n```\n\n[notes]()\n"; + let (first, second) = two_saves(original); + assert_eq!( + first, original, + "literal token in fence rewritten: {first:?}" + ); + assert_eq!(first, second, "fixpoint"); + + // Several colliding literals plus a repeated spaced destination + // (which reuses one placeholder via the dedupe path). + let original = "idweb-spaced-destination-0-placeholder and idweb-spaced-destination-1-placeholder\n\n[a]() and [b]()\n"; + let (first, second) = two_saves(original); + assert_eq!(first, original, "literals or dedupe broken: {first:?}"); + assert_eq!(first, second, "fixpoint"); + + // A colliding literal alongside a titled spaced destination. + let original = "idweb-spaced-destination-0-placeholder\n\n[t]( \"T\")\n"; + let (first, second) = two_saves(original); + assert_eq!(first, original, "titled save with collision: {first:?}"); + assert_eq!(first, second, "fixpoint"); + } + #[test] fn test_newline_destination_protection_skips_literal_contexts() { // #1707: protect_newline_destinations must leave