Repository navigation
fix(id-web): keep marks spanning inline HTML atoms as one span on save (#1412) #1505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1709,9 +1709,70 @@ fn insert_marked_chain<'a>( | |
| /// segments. Pure mark chains go through the #968 tail insertion | ||
| /// (`insert_marked_chain`), which handles the #953 merge as its | ||
| /// shared-mark case; non-chain children keep the original merge. | ||
|
|
||
| /// #1412: adopt trailing `html_inline` atoms into the tail mark | ||
| /// wrapper when a marked chain that shares that wrapper's mark is | ||
| /// about to be inserted. The parse direction emits `html_inline` | ||
| /// atoms markless (see the `NodeValue::HtmlInline` arm of | ||
| /// `convert_node`), so a mark spanning an atom reaches the save | ||
| /// direction as marked run, bare atom, marked run. Appending the | ||
| /// atom as a sibling severs the tail wrapper: the next run opens a | ||
| /// NEW wrapper and the span fragments (`[a <b>x</b> c](/y)` saved as | ||
| /// `[a ](/y)<b>[x](/y)</b>[ c](/y)`, with ` ` leaks at emphasis | ||
| /// seams). When the incoming chain shares a mark with the wrapper | ||
| /// before the atoms, the atoms belong to that span: move them inside | ||
| /// the wrapper (recursing along the chain's shared marks, mirroring | ||
| /// `insert_marked_chain`'s descent, so nested spans adopt at the | ||
| /// deepest shared level), after which the chain insertion merges | ||
| /// into the same wrapper as usual. Atoms not followed by a chain | ||
| /// sharing the preceding wrapper's mark keep their legacy sibling | ||
| /// placement, so unmarked HTML and atoms after a finished span are | ||
| /// unchanged. Extending the atom predicate (e.g. to images, #1407) | ||
| /// generalizes this to the other atom kinds. | ||
| fn adopt_trailing_html_atoms<'a>(parent: &'a AstNode<'a>, wrappers: &[NodeValue]) { | ||
| let children: Vec<&'a AstNode<'a>> = parent.children().collect(); | ||
| let mut split = children.len(); | ||
| while split > 0 | ||
| && matches!( | ||
| children[split - 1].data.borrow().value, | ||
| NodeValue::HtmlInline(..) | ||
| ) | ||
| { | ||
| split -= 1; | ||
| } | ||
| if split == children.len() || split == 0 { | ||
| return; | ||
| } | ||
| let tail = children[split - 1]; | ||
| let tail_value = tail.data.borrow().value.clone(); | ||
| if !is_mark_wrapper(&tail_value) { | ||
| return; | ||
| } | ||
| let Some(shared) = wrappers | ||
| .iter() | ||
| .position(|wrapper| same_mark_wrapper(wrapper, &tail_value)) | ||
| else { | ||
| return; | ||
| }; | ||
| for atom in &children[split..] { | ||
| atom.detach(); | ||
| tail.append(atom); | ||
|
Comment on lines
+1757
to
+1759
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When two separate marked spans surround an opening HTML tag, adopting that tag changes their nesting and can change rendered formatting. For example, Useful? React with 👍 / 👎. |
||
| } | ||
| let rest: Vec<NodeValue> = wrappers | ||
| .iter() | ||
| .enumerate() | ||
| .filter(|(index, _)| *index != shared) | ||
| .map(|(_, wrapper)| wrapper.clone()) | ||
| .collect(); | ||
| adopt_trailing_html_atoms(tail, &rest); | ||
| } | ||
|
|
||
| fn append_inline_child<'a>(arena: &'a Arena<'a>, parent: &'a AstNode<'a>, child: &'a AstNode<'a>) { | ||
| if let Some((wrappers, leaf)) = peel_mark_chain(child) { | ||
| leaf.detach(); | ||
| // #1412: html_inline atoms sitting between this chain and the | ||
| // tail mark wrapper join the span before the chain merges in. | ||
| adopt_trailing_html_atoms(parent, &wrappers); | ||
| insert_marked_chain(arena, parent, &wrappers, leaf); | ||
| return; | ||
| } | ||
|
|
@@ -3609,4 +3670,68 @@ mod tests { | |
| assert!(!first.contains("\\<"), "no escaping: {first}"); | ||
| assert_eq!(first, second, "fixpoint"); | ||
| } | ||
| #[test] | ||
| fn test_marks_spanning_inline_html_roundtrip() { | ||
| // #1412: a mark spanning an html_inline atom must save as ONE | ||
| // span containing the tags, not fragmented per text run, and | ||
| // emphasis forms must not leak   entities at the seams. | ||
| for md in [ | ||
| "[a <b>x</b> c](/y)\n", | ||
| "[a <sup>2</sup> c](/y)\n", | ||
| "*a <b>x</b> c*\n", | ||
| "**a <b>x</b> c**\n", | ||
| "~~a <b>x</b> c~~\n", | ||
| "*a <b><i>x</i></b> c*\n", | ||
| ] { | ||
| let (first, second) = two_saves(md); | ||
| assert_eq!(first, md, "round-trip for {md:?}"); | ||
| assert_eq!(first, second, "fixpoint for {md:?}"); | ||
| assert!( | ||
| !first.contains(" "), | ||
| "no entity leak for {md:?}: {first}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_marks_spanning_inline_html_pm_to_md() { | ||
| // #1412, save direction alone: PM text runs carrying a link | ||
| // mark, separated by markless html_inline atoms, emit a single | ||
| // link span containing the tags. | ||
| let pm_json = json!({ | ||
| "type": "doc", | ||
| "content": [{ | ||
| "type": "paragraph", | ||
| "content": [ | ||
| {"type": "text", "text": "a ", "marks": [{"type": "link", "attrs": {"href": "/y"}}]}, | ||
| {"type": "html_inline", "attrs": {"html": "<b>"}}, | ||
| {"type": "text", "text": "x", "marks": [{"type": "link", "attrs": {"href": "/y"}}]}, | ||
| {"type": "html_inline", "attrs": {"html": "</b>"}}, | ||
| {"type": "text", "text": " c", "marks": [{"type": "link", "attrs": {"href": "/y"}}]} | ||
| ] | ||
| }] | ||
| }); | ||
| let md = prosemirror_to_markdown(&pm_json).unwrap(); | ||
| assert_eq!(md.trim(), "[a <b>x</b> c](/y)", "single link span: {md}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_inline_html_outside_marks_stays_outside() { | ||
| // #1412 controls: atoms that do NOT sit between same-mark runs | ||
| // keep their legacy placement outside any mark span. | ||
| for md in ["a <b>x</b> c\n", "*a* <b>x</b>\n"] { | ||
| let (first, second) = two_saves(md); | ||
| assert_eq!(first, md, "round-trip for {md:?}"); | ||
| assert_eq!(first, second, "fixpoint for {md:?}"); | ||
| } | ||
| // Single-run form ([<b>x</b>](/y)): the save form may differ | ||
| // textually but must re-parse to the identical PM structure. | ||
| let (first, second) = two_saves("[<b>x</b>](/y)\n"); | ||
| assert_eq!(first, second, "fixpoint for single-run form: {first}"); | ||
| assert_eq!( | ||
| markdown_to_prosemirror(&first), | ||
| markdown_to_prosemirror("[<b>x</b>](/y)\n"), | ||
| "same PM structure for single-run form" | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a long paragraph containing alternating marked and unmarked runs, such as repeated
*a* b, every marked run now collects all previously appended siblings even though there are no HTML atoms to adopt. Sinceappend_inline_childcalls this helper for every marked chain, a paragraph with n such runs incurs Θ(n²) sibling visits and repeated allocations, whereas the previous tail insertion only inspected the last child. This introduces quadratic work on ordinary Markdown saves throughconvert_doc_to_bytes. Walk backward fromlast_child()through only the trailing HTML atoms, returning immediately when the last child is not HTML.Useful? React with 👍 / 👎.