| 1 | //! Finding 2 of the native realtime audit: display positions are mapped by |
| 2 | //! walking the tree, not by building the projection. The walk must agree, |
| 3 | //! position by position, with the projection-based mapping it replaced |
| 4 | //! (support::oracle), on edited documents and on every kind of node. |
| 5 | mod support; |
| 6 | use docs_native_crdt::Editor; |
| 7 | use serde_json::{json, Value}; |
| 8 | use support::{generate, oracle, oracle_locate, Random}; |
| 9 | |
| 10 | fn open(bytes: &[u8], client: u64) -> Editor { |
| 11 | let editor = Editor::new("large-document", client); |
| 12 | editor.apply(bytes).unwrap(); |
| 13 | editor |
| 14 | } |
| 15 | /// The display text, rendered from the projection the way the apps do. |
| 16 | fn render(value: &Value, out: &mut Vec<u16>) { |
| 17 | match value["type"].as_str().unwrap_or("") { |
| 18 | "text" => for run in value["runs"].as_array().into_iter().flatten() { out.extend(run["insert"].as_str().unwrap().encode_utf16()); }, |
| 19 | "hard_break" => out.push(0x2028), |
| 20 | "image" => out.push(0xFFFC), |
| 21 | "horizontal_rule" => out.extend([0xFFFC, 10]), |
| 22 | tag => { |
| 23 | for child in value["children"].as_array().into_iter().flatten() { render(child, out); } |
| 24 | if ["paragraph", "heading", "code_block"].contains(&tag) { out.push(10); } |
| 25 | }, |
| 26 | } |
| 27 | } |
| 28 | fn display(editor: &Editor) -> Vec<u16> { |
| 29 | let mut out = Vec::new(); |
| 30 | for child in editor.projection()["children"].as_array().unwrap() { render(child, &mut out); } |
| 31 | out |
| 32 | } |
| 33 | fn check_every_position(editor: &Editor, step: u32) { |
| 34 | let projection = editor.projection(); |
| 35 | let (ranges, total) = oracle(&projection); |
| 36 | assert_eq!(editor.display_length(), total); |
| 37 | assert_eq!(display(editor).len() as u32, total); |
| 38 | let mut at = 0; |
| 39 | while at <= total + 1 { |
| 40 | for length in [0, 1, 3, 40] { |
| 41 | let expected = oracle_locate(&ranges, at, length); |
| 42 | let actual = editor.display_locate(at, length).ok(); |
| 43 | assert_eq!(actual, expected, "locate({at}, {length})"); |
| 44 | } |
| 45 | let anchor = editor.anchor(at).unwrap(); |
| 46 | let expected = match oracle_locate(&ranges, at, 0) { |
| 47 | Some((node, local)) => { |
| 48 | let span = ranges.iter().find(|r| r.node == node).unwrap(); |
| 49 | assert_eq!(anchor["node"], node); |
| 50 | span.start + local.min(span.length) |
| 51 | }, |
| 52 | None => at.min(total.saturating_sub(1)), |
| 53 | }; |
| 54 | assert_eq!(editor.resolve(&anchor).unwrap(), expected, "resolve(anchor({at}))"); |
| 55 | at += step; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn walk_matches_the_projection_mapping_on_edited_documents() { |
| 61 | for (pages, edits, step) in [(1, 0, 1), (2, 120, 1), (8, 900, 7)] { |
| 62 | let editor = open(&generate(pages, edits, 11), 101); |
| 63 | check_every_position(&editor, step); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn walk_matches_on_every_kind_of_node() { |
| 69 | let mut editor = Editor::new("kinds", 201); |
| 70 | editor.command(&json!({"op":"insert_element","at":0,"type":"paragraph"})).unwrap(); |
| 71 | editor.command(&json!({"op":"display_paste","at":0,"length":0,"blocks":[ |
| 72 | {"type":"heading","attrs":{"level":"1"},"children":[{"type":"text","runs":[{"insert":"Title 😀","marks":{"strong":{}}}]}]}, |
| 73 | {"type":"paragraph","children":[]}, |
| 74 | {"type":"paragraph","children":[{"type":"text","runs":[{"insert":"before","marks":{}}]},{"type":"hard_break"},{"type":"text","runs":[{"insert":"after","marks":{"em":{}}}]},{"type":"image","attrs":{"src":"https://example.com/a.png"}}]}, |
| 75 | {"type":"horizontal_rule"}, |
| 76 | {"type":"code_block","children":[{"type":"text","runs":[{"insert":"let x = 1;\nlet y = 2;","marks":{}}]}]}, |
| 77 | {"type":"blockquote","children":[{"type":"paragraph","children":[{"type":"text","runs":[{"insert":"quoted","marks":{}}]}]}]}, |
| 78 | {"type":"bullet_list","children":[ |
| 79 | {"type":"list_item","children":[{"type":"paragraph","children":[{"type":"text","runs":[{"insert":"one","marks":{}}]}]}, |
| 80 | {"type":"ordered_list","children":[{"type":"list_item","children":[{"type":"paragraph","children":[]}]}]}]}, |
| 81 | {"type":"list_item","children":[{"type":"paragraph","children":[{"type":"text","runs":[{"insert":"two","marks":{}}]}]}]} |
| 82 | ]}, |
| 83 | {"type":"image","attrs":{"src":"https://example.com/b.png"}}, |
| 84 | {"type":"paragraph","children":[{"type":"hard_break"},{"type":"hard_break"}]} |
| 85 | ]})).unwrap(); |
| 86 | check_every_position(&editor, 1); |
| 87 | // Empty text nodes, as an edit next to an inline atom leaves them. |
| 88 | let length = editor.display_length(); |
| 89 | for at in (0..length).rev().step_by(3) { let _ = editor.command(&json!({"op":"display_edit","at":at,"length":0,"text":"z"})); } |
| 90 | check_every_position(&editor, 1); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn typing_anywhere_edits_exactly_that_display_position() { |
| 95 | let mut editor = open(&generate(6, 600, 12), 301); |
| 96 | let mut random = Random::new(5); |
| 97 | for round in 0..200 { |
| 98 | let before = display(&editor); |
| 99 | let total = before.len() as u32; |
| 100 | let at = random.below(total as u64) as u32; |
| 101 | // Stay inside one text: never across a block end or an atom. |
| 102 | let length = if round % 3 == 0 { (1..=3).take_while(|n| at + n < total && !before[(at as usize)..(at + n) as usize].iter().any(|u| [10, 0x2028, 0xFFFC].contains(u))).last().unwrap_or(0) } else { 0 }; |
| 103 | if at > 0 && (0xDC00..0xE000).contains(&before[at as usize]) { continue; } |
| 104 | if (0xDC00..0xE000).contains(&before[(at + length) as usize]) { continue; } |
| 105 | let text = ["k", "é", "😀", "two words"][round % 4]; |
| 106 | editor.command(&json!({"op":"display_edit","at":at,"length":length,"text":text})).unwrap_or_else(|e| panic!("edit {at}+{length}: {e}")); |
| 107 | let mut expected = before[..at as usize].to_vec(); |
| 108 | expected.extend(text.encode_utf16()); |
| 109 | expected.extend(&before[(at + length) as usize..]); |
| 110 | assert_eq!(display(&editor), expected, "round {round}: {at}+{length} {text}"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn a_text_holding_an_embed_maps_one_position_per_unit() { |
| 116 | // Yjs editors never embed values in a text, but a peer can. The display |
| 117 | // shows the embed as one U+FFFC, so display offsets stay yrs offsets. |
| 118 | use yrs::{Any, Doc, ReadTxn, StateVector, Text, Transact, XmlElementPrelim, XmlFragment, XmlTextPrelim, XmlOut}; |
| 119 | let doc = Doc::with_options({ let mut o = yrs::Options::with_guid_and_client_id("embed".into(), 7); o.offset_kind = yrs::OffsetKind::Utf16; o }); |
| 120 | let root = doc.get_or_insert_xml_fragment("prosemirror"); |
| 121 | { |
| 122 | let mut txn = doc.transact_mut(); |
| 123 | let paragraph = root.insert(&mut txn, 0, XmlElementPrelim::empty("paragraph")); |
| 124 | let text = paragraph.insert(&mut txn, 0, XmlTextPrelim::new("abcd")); |
| 125 | text.insert_embed(&mut txn, 2, Any::from(std::collections::HashMap::from([("k".to_owned(), Any::from("v"))]))); |
| 126 | let _ = XmlOut::Text(text); |
| 127 | } |
| 128 | let bytes = doc.transact().encode_state_as_update_v1(&StateVector::default()); |
| 129 | let mut editor = Editor::new("embed", 8); |
| 130 | editor.apply(&bytes).unwrap(); |
| 131 | assert_eq!(display(&editor), "ab\u{FFFC}cd\n".encode_utf16().collect::<Vec<_>>()); |
| 132 | assert_eq!(editor.display_length(), 6); |
| 133 | check_every_position(&editor, 1); |
| 134 | editor.command(&json!({"op":"display_edit","at":4,"length":0,"text":"X"})).unwrap(); |
| 135 | assert_eq!(display(&editor), "ab\u{FFFC}cXd\n".encode_utf16().collect::<Vec<_>>()); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn projection_is_unchanged_for_documents_without_embeds() { |
| 140 | for (pages, edits) in [(1, 0), (3, 300), (10, 1200)] { |
| 141 | let bytes = generate(pages, edits, 13); |
| 142 | let editor = open(&bytes, 401); |
| 143 | assert_eq!(editor.projection()["children"], support::legacy_projection(&editor.snapshot())["children"], "{pages} pages, {edits} edits"); |
| 144 | } |
| 145 | } |
| 146 | |