Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Notes: never write an attachment twice; tables read-only until proven on a phone

Yashwanth Kamireddi93011cb03fac
diff --git a/.uiverify/notes-table.mjs b/.uiverify/notes-table.mjs
index d614c3c..a2d03f7 100644
--- a/.uiverify/notes-table.mjs
+++ b/.uiverify/notes-table.mjs
@@ -1,6 +1,5 @@
-// A table in a note, drawn as its grid and edited in place: a typed cell takes typing as its
-// own (the note's text is untouched), saves when left, and goes back with the reason if Apple
-// refuses; an empty cell takes typing like any other.
+// A table in a note, drawn as its grid, its text colour shown, and copied as its cells.
+// Editing is off until an edit made here is seen on an iPhone.
import { engine } from "./engine.mjs";
import { SERVICES, installStub, openScreen } from "./stub.mjs";
const b = await engine.launch();
@@ -18,27 +17,12 @@ await page.waitForSelector(".nf-table");
const grid = await page.locator(".nf-table tr").evaluateAll((rows) => rows.map((r) => [...r.cells].map((c) => c.textContent)));
ok(JSON.stringify(grid) === JSON.stringify([["Item", "Qty"], ["Milk", "2"], ["", ""]]), `the table is drawn as its grid ${JSON.stringify(grid)}`);
-const milk = page.locator(".nf-cell-edit", { hasText: "Milk" });
-await milk.click();
-await page.keyboard.press("End");
-await page.keyboard.type(" (oat)");
-await page.keyboard.press("Enter");
-await page.waitForTimeout(100);
-ok((await milk.textContent()) === "Milk (oat)", "a cell takes typing and shows it at once");
-await page.waitForTimeout(400);
-const writes = await page.evaluate(() => window.__cellWrites);
-ok(writes?.length === 1 && writes[0].row === 1 && writes[0].column === 0 && writes[0].text === "Milk (oat)", `the cell, and only it, goes to Apple ${JSON.stringify(writes)}`);
-const saves = await page.evaluate(() => window.__noteSaves?.length ?? 0);
-ok(saves === 0, "typing in a cell leaves the note's own text alone");
-
-await page.evaluate(() => { window.__cellFail = true; });
-const qty = page.locator(".nf-cell-edit", { hasText: "2" });
-await qty.click();
-await page.keyboard.press("End");
-await page.keyboard.type("0");
-await page.keyboard.press("Enter");
-await page.waitForTimeout(500);
-ok((await qty.textContent()) === "2" && /refused/.test((await qty.getAttribute("title")) ?? ""), "a change Apple refuses goes back, and says why");
+// Read-only for now: Quince's table edits were stored at Apple but did not show on the
+// iPhone, so no cell is offered for typing and no table menu opens.
+ok((await page.locator(".nf-cell-edit").count()) === 0, "no table cell is offered for typing while table writes are off");
+await page.locator(".nf-table tr").nth(1).locator("td").first().click({ button: "right" });
+ok((await page.getByRole("menuitem", { name: /Add Row|Delete Row/ }).count()) === 0, "and right-click offers no row or column changes");
+await page.keyboard.press("Escape");
const coloured = await page.locator(".note-rich .nf-em-4").evaluateAll((e) => e.map((x) => [x.textContent, getComputedStyle(x).color]));
ok(coloured.length === 1 && coloured[0][0] === "end" && !/rgb\(29, 29, 31\)|rgb\(0, 0, 0\)/.test(coloured[0][1]), `text coloured on a phone shows in its colour ${JSON.stringify(coloured)}`);
// Copying the note puts the table's cells out, never a placeholder word.
@@ -55,28 +39,46 @@ const copied = await page.evaluate(() => {
done({ text: data.getData("text/plain"), html: data.getData("text/html") });
}, 50));
});
-ok(!/Attachment/.test(copied.text + copied.html) && /Item\tQty/.test(copied.text) && /<td>Milk \(oat\)<\/td>/.test(copied.html), `copying a table copies its cells ${JSON.stringify(copied).slice(0, 300)}`);
-// An empty cell takes typing too: tables made on a phone hold an empty text for every cell.
-const empty = page.locator(".nf-table tr").nth(2).locator(".nf-cell-edit").first();
-ok((await page.locator(".nf-cell-empty").count()) === 0 && (await empty.count()) === 1, "an empty cell is offered for editing");
-await page.evaluate(() => { window.__cellFail = false; window.__cellWrites = []; });
-await empty.click();
-await page.keyboard.type("Bread");
-await page.keyboard.press("Enter");
-await page.waitForTimeout(400);
-const w2 = await page.evaluate(() => window.__cellWrites);
-ok((await empty.textContent()) === "Bread" && w2?.length === 1 && w2[0].row === 2 && w2[0].column === 0 && w2[0].text === "Bread", `typing in an empty cell saves it ${JSON.stringify(w2)}`);
-// Right-click a cell: a row added below it, as Notes adds one.
-await page.locator(".nf-table tr").nth(1).locator("td").first().click({ button: "right" });
-await page.getByRole("menuitem", { name: "Add Row Below" }).click();
-await page.waitForTimeout(300);
-const grown = await page.locator(".nf-table tr").evaluateAll((rows) => rows.map((r) => [...r.cells].map((c) => c.textContent)));
-const asked = await page.evaluate(() => window.__rowAdds?.at(-1));
-ok(grown.length === 4 && grown[2].every((c) => c === "") && asked?.at === 2, `Add Row Below adds an empty row under the one clicked ${JSON.stringify(grown)} ${JSON.stringify(asked)}`);
-await page.locator(".nf-table tr").first().locator("td").nth(1).click({ button: "right" });
-await page.getByRole("menuitem", { name: "Add Column Before" }).click();
-await page.waitForTimeout(300);
-const wider = await page.locator(".nf-table tr").first().evaluate((r) => [...r.cells].map((c) => c.textContent));
-ok(JSON.stringify(wider) === JSON.stringify(["Item", "", "Qty"]), `Add Column Before adds an empty column before the one clicked ${JSON.stringify(wider)}`);
+ok(!/Attachment/.test(copied.text + copied.html) && /Item\tQty/.test(copied.text) && /<td>Milk<\/td>/.test(copied.html), `copying a table copies its cells ${JSON.stringify(copied).slice(0, 300)}`);
+// Pasting a copied table never makes a second reference to it: the save holds it once.
+await page.evaluate(() => {
+ const ed = document.querySelector(".note-rich");
+ ed.focus();
+ const range = document.createRange();
+ range.selectNodeContents(ed);
+ getSelection().removeAllRanges();
+ getSelection().addRange(range);
+});
+await page.waitForTimeout(50);
+await page.evaluate(() => {
+ const ed = document.querySelector(".note-rich");
+ const data = new DataTransfer();
+ ed.dispatchEvent(new ClipboardEvent("copy", { clipboardData: data, bubbles: true, cancelable: true }));
+ const last = ed.lastElementChild;
+ const range = document.createRange();
+ range.selectNodeContents(last);
+ range.collapse(false);
+ getSelection().removeAllRanges();
+ getSelection().addRange(range);
+ window.__pasteData = data;
+});
+await page.waitForTimeout(80);
+await page.evaluate(() => {
+ const ed = document.querySelector(".note-rich");
+ ed.dispatchEvent(new ClipboardEvent("paste", { clipboardData: window.__pasteData, bubbles: true, cancelable: true }));
+});
+await page.waitForTimeout(1500);
+const pasted = await page.evaluate(() => window.__noteSaves?.at(-1));
+const refs = (pasted?.runs ?? []).filter((r) => r.attachment === "T1").length;
+ok(pasted && refs === 1 && (pasted.body.match(/\uFFFC/g) ?? []).length === 1 && /Item\tQty\nMilk\t2/.test(pasted.body), `a pasted table brings its cells as text, never a second reference refs=${refs} ${JSON.stringify(pasted?.body)}`);
+// Typed in on a phone while the note is open: the next sync redraws the table, without
+// leaving the note.
+await page.evaluate(() => {
+ window.__table[2] = ["Bread", "1"];
+ window.__emit("quince://synced", { services: [], asOf: Date.now() / 1000, tookMs: 5 });
+});
+await page.waitForTimeout(500);
+const after = await page.locator(".nf-table tr").nth(2).evaluate((r) => [...r.cells].map((c) => c.textContent));
+ok(JSON.stringify(after) === JSON.stringify(["Bread", "1"]), `a table changed elsewhere redraws after the next sync ${JSON.stringify(after)}`);
await b.close();
process.exitCode = failures ? 1 : 0;
diff --git a/.uiverify/stub.mjs b/.uiverify/stub.mjs
index 17b8452..451510a 100644
--- a/.uiverify/stub.mjs
+++ b/.uiverify/stub.mjs
@@ -307,6 +307,13 @@ export function installStub({ screen, services, twoFactor, calendarFixture, note
(window.__rowAdds ??= []).push(args);
return t.map((r) => r.slice());
}
+ case "note_table_remove": {
+ const t = (window.__table ??= [["Item", "Qty"], ["Milk", "2"], ["", ""]]);
+ if (args.column) t.forEach((r) => r.splice(args.at, 1));
+ else t.splice(args.at, 1);
+ (window.__removes ??= []).push(args);
+ return t.map((r) => r.slice());
+ }
case "note_table_set_cell": {
(window.__cellWrites ??= []).push(args);
await new Promise((r) => setTimeout(r, 300));
diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rs
index c3d0b69..d53f15a 100644
--- a/crates/quince-icloud/src/ckdocument.rs
+++ b/crates/quince-icloud/src/ckdocument.rs
@@ -607,8 +607,18 @@ fn set_varint_field(message: &[u8], tag: u64, value: u64) -> Option<Vec<u8>> {
/// identifiers Apple uses to merge edits. Only the lengths move.
fn rebuild_note(note: &[u8], old_text: &str, new_text: &str) -> Option<Vec<u8>> {
// Each edit on its own, the last first, so the offsets of those before it still hold.
- let hunks = utf16_hunks(old_text, new_text);
+ rebuild_note_with(note, old_text, new_text, &utf16_hunks(old_text, new_text))
+}
+/// [`rebuild_note`] with the edit's place given rather than worked out from the two texts.
+/// A text of identical characters, such as a table's ordered set, says nothing about where
+/// one went in or came out; the caller does.
+fn rebuild_note_with(
+ note: &[u8],
+ old_text: &str,
+ new_text: &str,
+ hunks: &[(u64, u64, u64)],
+) -> Option<Vec<u8>> {
// The merge records have to say what the text now says, or the phone shows what they
// say instead: edits made here arrived as text repeated, or lines that would not go.
let records = match crate::topotext::Topo::read(note) {
@@ -724,6 +734,33 @@ pub(crate) fn rewrite_text_object(
rebuild_note(object, old_text, new_text)
}
+/// [`rewrite_text_object`] for one edit at a known place: `removed` characters from character
+/// `at` replaced by `inserted`.
+pub(crate) fn rewrite_text_object_at(
+ object: &[u8],
+ old_text: &str,
+ at: usize,
+ removed: usize,
+ inserted: &str,
+) -> Option<Vec<u8>> {
+ let chars: Vec<char> = old_text.chars().collect();
+ if at + removed > chars.len() {
+ return None;
+ }
+ let units = |cs: &[char]| cs.iter().map(|c| c.len_utf16() as u64).sum::<u64>();
+ let from = units(&chars[..at]);
+ let gone = units(&chars[at..at + removed]);
+ let mut new_text: String = chars[..at].iter().collect();
+ new_text.push_str(inserted);
+ new_text.extend(&chars[at + removed..]);
+ rebuild_note_with(
+ object,
+ old_text,
+ &new_text,
+ &[(from, gone, utf16_len(inserted))],
+ )
+}
+
/// What to change about one paragraph's setting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParagraphChange {
@@ -1051,6 +1088,12 @@ pub fn set_runs(original: &[u8], runs: &[Run]) -> Option<Vec<u8>> {
pieces.push(piece);
}
+ // An attachment has one place in one note. Notes on a phone lagged and crashed over a note
+ // this app had saved with a table referenced twice and references to deleted tables, so
+ // nothing is written unless every reference is one the note already had, used once, on a
+ // single attachment character.
+ attachments_sound(&old, &pieces, &units)?;
+
let mut out = Vec::with_capacity(note.len() + 64);
let mut reader = Reader::new(note);
let mut wrote = false;
@@ -1078,6 +1121,31 @@ pub fn set_runs(original: &[u8], runs: &[Run]) -> Option<Vec<u8>> {
ensure_versions(&rewrite(&plain, &TEXT_PATH[..2], &out)?)
}
+/// `Some(())` when the attachment references in `new` runs (over `units`) are sound against
+/// `old`: each one already in `old`, none used twice, and each on a run of exactly one
+/// object-replacement character.
+fn attachments_sound(old: &[Vec<u8>], new: &[Vec<u8>], units: &[u16]) -> Option<()> {
+ let id_of =
+ |run: &[u8]| bytes_field(run, ATTACHMENT_INFO).and_then(|info| bytes_field(&info, 1));
+ let had: std::collections::HashSet<Vec<u8>> = old.iter().filter_map(|r| id_of(r)).collect();
+ let mut used = std::collections::HashSet::new();
+ let mut at = 0usize;
+ for run in new {
+ let length = usize::try_from(run_length(run)).ok()?;
+ if let Some(id) = id_of(run) {
+ if length != 1
+ || units.get(at) != Some(&0xFFFC)
+ || !had.contains(&id)
+ || !used.insert(id)
+ {
+ return None;
+ }
+ }
+ at += length;
+ }
+ Some(())
+}
+
/// The text and its formatting together: the text replaced as [`replace_text`] replaces it,
/// then the runs set as [`set_runs`] sets them. Over the base64 a CloudKit field carries.
#[must_use]
@@ -2811,6 +2879,39 @@ mod run_tests {
assert_eq!(cleared[0].start, None, "a plain line kept a list's start");
}
+ /// A save never writes an attachment twice, one the note did not have, or one off its own
+ /// object-replacement character: the shape that made Notes on a phone lag and crash.
+ #[test]
+ fn attachment_references_stay_sound() {
+ let run = |len: u64, id: Option<&[u8]>| {
+ let mut r = Vec::new();
+ varint(1 << 3, &mut r);
+ varint(len, &mut r);
+ if let Some(id) = id {
+ let mut info = Vec::new();
+ len_field(1, id, &mut info);
+ len_field(12, &info, &mut r);
+ }
+ r
+ };
+ let old = vec![run(1, Some(b"A")), run(1, None)];
+ let units: Vec<u16> = "\u{FFFC}\u{FFFC}".encode_utf16().collect();
+ assert!(attachments_sound(&old, &[run(1, Some(b"A")), run(1, None)], &units).is_some());
+ assert!(
+ attachments_sound(&old, &[run(1, Some(b"A")), run(1, Some(b"A"))], &units).is_none(),
+ "twice"
+ );
+ assert!(
+ attachments_sound(&old, &[run(1, Some(b"B")), run(1, None)], &units).is_none(),
+ "not the note's"
+ );
+ let text: Vec<u16> = "a\u{FFFC}".encode_utf16().collect();
+ assert!(
+ attachments_sound(&old, &[run(1, Some(b"A")), run(1, None)], &text).is_none(),
+ "off its character"
+ );
+ }
+
/// A style is the line's: one set on half a line covers all of it, from its newline.
#[test]
fn a_paragraph_style_covers_its_whole_line() {
diff --git a/crates/quince-icloud/src/notes.rs b/crates/quince-icloud/src/notes.rs
index 9862ead..26d0b40 100644
--- a/crates/quince-icloud/src/notes.rs
+++ b/crates/quince-icloud/src/notes.rs
@@ -2505,6 +2505,44 @@ pub async fn add_table_line_cloudkit(
.ok_or_else(|| Error::Protocol("Apple kept a table this cannot read back".into()))
}
+/// A table with the row (`column` false) or column at `at` deleted, as a phone deletes one,
+/// written against Apple's exact version and read back.
+pub async fn remove_table_line_cloudkit(
+ http: &reqwest::Client,
+ ckdatabasews_root: &str,
+ params: Vec<(String, String)>,
+ attachment: &str,
+ column: bool,
+ at: usize,
+) -> Result<Vec<Vec<String>>> {
+ let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);
+ client
+ .edit_record(ZONE, attachment, "Attachment", |record| {
+ let current = record
+ .pointer("/fields/MergeableDataEncrypted/value")?
+ .as_str()?;
+ let edited = crate::table::without_line_in_base64(current, column, at)?;
+ Some(serde_json::json!({
+ "MergeableDataEncrypted": { "type": "ENCRYPTED_BYTES", "value": edited }
+ }))
+ })
+ .await?
+ .ok_or_else(|| {
+ Error::Refused(format!(
+ "That {} can’t be deleted here: a table keeps at least one, or it holds something Quince can’t rewrite exactly.",
+ if column { "column" } else { "row" }
+ ))
+ })?;
+ client
+ .lookup_record(ZONE, attachment)
+ .await?
+ .ok_or(Error::NotFound)?
+ .pointer("/fields/MergeableDataEncrypted/value")
+ .and_then(Value::as_str)
+ .and_then(crate::table::cells_from_base64)
+ .ok_or_else(|| Error::Protocol("Apple kept a table this cannot read back".into()))
+}
+
/// A table cell set to `text`.
pub async fn set_table_cell_cloudkit(
http: &reqwest::Client,
diff --git a/crates/quince-icloud/src/table.rs b/crates/quince-icloud/src/table.rs
index f383648..f84bf00 100644
--- a/crates/quince-icloud/src/table.rs
+++ b/crates/quince-icloud/src/table.rs
@@ -323,10 +323,18 @@ pub fn with_cell(document: &[u8], row: usize, column: usize, text: &str) -> Opti
.map(|b| String::from_utf8_lossy(b).into_owned())
.unwrap_or_default();
+ let writers = bytes(&data, 7).next()?;
let rebuild = |new_text: &str| -> Option<Vec<u8>> {
- let new_string = crate::ckdocument::rewrite_text_object(string, &old, new_text)?;
+ let (new_string, new_writers) = edit_table_text(string, writers, |lent| {
+ crate::ckdocument::rewrite_text_object(lent, &old, new_text)
+ })?;
let new_object = splice(object, 10, 0, &new_string)?;
- let new_data = splice(data_bytes, 3, cell, &new_object)?;
+ let new_data = splice(
+ &splice(data_bytes, 3, cell, &new_object)?,
+ 7,
+ 0,
+ &new_writers,
+ )?;
let new_version = splice(version_bytes, 3, 0, &new_data)?;
splice(document, 2, 0, &new_version)
};
@@ -344,6 +352,34 @@ pub fn with_cell(document: &[u8], row: usize, column: usize, text: &str) -> Opti
(read.get(row)?.get(column)? == text).then_some(out)
}
+/// A table's text (a cell or an ordered set's) edited by `edit`, against the writer list
+/// the table keeps once for all its texts (document field 7, `ttTimestamp`): a phone writes
+/// none in the text itself, and ids in a table's texts count from the document's list. The
+/// list is lent to the text for the edit and taken back out, so this installation joins it
+/// as the next writer instead of claiming the phone's number. Answers the text and the
+/// document's list as they now are. A text that already carries a list of its own is
+/// refused: no device writes one, so its ids cannot be read against the document's.
+fn edit_table_text(
+ text: &[u8],
+ writers: &[u8],
+ edit: impl FnOnce(&[u8]) -> Option<Vec<u8>>,
+) -> Option<(Vec<u8>, Vec<u8>)> {
+ if bytes(&fields(text)?, 4).next().is_some() {
+ return None;
+ }
+ let mut lent = text.to_vec();
+ put_len(4, writers, &mut lent);
+ let edited = edit(&lent)?;
+ let new_writers = bytes(&fields(&edited)?, 4).next()?.to_vec();
+ let mut back = Vec::with_capacity(edited.len());
+ for (tag, raw) in raw_fields(&edited)? {
+ if tag != 4 {
+ back.extend_from_slice(raw);
+ }
+ }
+ Some((back, new_writers))
+}
+
/// Each field of a message with its whole encoding (key, length and payload), in order.
fn raw_fields(b: &[u8]) -> Option<Vec<(u64, &[u8])>> {
let mut out = Vec::new();
@@ -532,14 +568,11 @@ fn with_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
let saf = fields(string_array)?;
let text = bytes(&saf, 1).next()?;
let live = count;
+ // In at `at` itself: the text is all one character, so only the place says which.
let old_text: String = std::iter::repeat_n('\u{FFFC}', live).collect();
- let mut new_text = old_text.clone();
- let byte_at = old_text
- .char_indices()
- .nth(at)
- .map_or(old_text.len(), |(i, _)| i);
- new_text.insert(byte_at, '\u{FFFC}');
- let new_string = crate::ckdocument::rewrite_text_object(text, &old_text, &new_text)?;
+ let (new_string, new_writers) = edit_table_text(text, bytes(&data, 7).next()?, |lent| {
+ crate::ckdocument::rewrite_text_object_at(lent, &old_text, at, 0, "\u{FFFC}")
+ })?;
let mut attachments: Vec<(u64, Vec<u8>)> = Vec::new();
for a in bytes(&saf, 2) {
let f = fields(a)?;
@@ -709,6 +742,8 @@ fn with_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
for (i, (tag, field)) in raw.iter().enumerate() {
match *tag {
1 => put_len(1, &new_version_vector, &mut new_data),
+ // The table's writer list, with this installation in it.
+ 7 => put_len(7, &new_writers, &mut new_data),
3 => {
if object_n == rows_object {
put_len(3, &new_rows_object, &mut new_data);
@@ -747,6 +782,247 @@ fn with_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
(cells(&out)? == expected).then_some(out)
}
+/// A dictionary message without the one element keyed by object `key`. `None` unless exactly
+/// one element has that key.
+fn without_element(dictionary: &[u8], key: usize) -> Option<Vec<u8>> {
+ let mut out = Vec::with_capacity(dictionary.len());
+ let mut removed = 0;
+ for (tag, raw) in raw_fields(dictionary)? {
+ if tag == 1 {
+ let element = bytes(&fields(raw)?, 1).next()?;
+ if object_index(bytes(&fields(element)?, 1).next()?)? == key {
+ removed += 1;
+ continue;
+ }
+ }
+ out.extend_from_slice(raw);
+ }
+ (removed == 1).then_some(out)
+}
+
+/// The table's document with the row (or column) at `at` deleted, as a phone deletes one:
+/// its character tombstoned in the ordered set's text, its attachment gone and the later
+/// ones moved up, it out of the set, and its cells out of every column (or the column out of
+/// `cellColumns`). The order dictionary keeps it, as Apple's own deletes do. Proven as
+/// [`with_row`] is: the result must read back as the grid without that line.
+#[must_use]
+pub fn without_row(document: &[u8], at: usize) -> Option<Vec<u8>> {
+ without_member(document, false, at)
+}
+
+/// [`without_row`] for a column.
+#[must_use]
+pub fn without_column(document: &[u8], at: usize) -> Option<Vec<u8>> {
+ without_member(document, true, at)
+}
+
+fn without_member(document: &[u8], column: bool, at: usize) -> Option<Vec<u8>> {
+ let before = cells(document)?;
+ let width = before.first().map_or(0, Vec::len);
+ let count = if column { width } else { before.len() };
+ // A table keeps at least one row and one column, as Notes does.
+ if at >= count || count < 2 {
+ return None;
+ }
+ let top = fields(document)?;
+ let version_bytes = bytes(&top, 2).next()?;
+ let version = fields(version_bytes)?;
+ let data_bytes = bytes(&version, 3).next()?;
+ let data = fields(data_bytes)?;
+ let objects: Vec<&[u8]> = bytes(&data, 3).collect();
+ let uuids: Vec<&[u8]> = bytes(&data, 6).collect();
+ let doc = Doc {
+ objects: objects.clone(),
+ keys: bytes(&data, 4)
+ .map(|k| String::from_utf8_lossy(k).into_owned())
+ .collect(),
+ uuids: uuids.clone(),
+ };
+ let root = doc.custom(0)?;
+ let at_key = |name: &str| root.get(name).and_then(|id| object_index(id));
+ let set_object = at_key(if column { "crColumns" } else { "crRows" })?;
+ let cells_object = at_key("cellColumns")?;
+ let by_uuid = doc.uuid_objects();
+
+ // The line being deleted: its element (by attachment position) and its identity.
+ let (kind, set_body) = doc.object(set_object)?;
+ if kind != 16 {
+ return None;
+ }
+ let sf = fields(set_body)?;
+ let array_body = bytes(&sf, 1).next()?;
+ let af = fields(array_body)?;
+ let string_array = bytes(&af, 1).next()?;
+ let saf = fields(string_array)?;
+ let mut attachments: Vec<(u64, Vec<u8>)> = Vec::new();
+ for a in bytes(&saf, 2) {
+ let f = fields(a)?;
+ attachments.push((int(&f, 1).unwrap_or(0), bytes(&f, 2).next()?.to_vec()));
+ }
+ let gone_uuid = attachments.iter().find(|(i, _)| *i == at as u64)?.1.clone();
+ let element = *by_uuid.get(gone_uuid.as_slice())?;
+ let identity = *Doc::dictionary(bytes(&af, 2).next()?)?.get(&element)?;
+
+ let me = crate::topotext::writer_bytes();
+ let mut new_uuids: Vec<Vec<u8>> = Vec::new();
+ let replica = match uuids.iter().position(|u| *u == me) {
+ Some(i) => i,
+ None => {
+ new_uuids.push(me.to_vec());
+ uuids.len()
+ }
+ };
+ let versions: Vec<(u64, u64)> = bytes(&fields(bytes(&data, 1).next()?)?, 1)
+ .filter_map(|e| {
+ let f = fields(e)?;
+ Some((int(&f, 1).unwrap_or(0), int(&f, 2).unwrap_or(0)))
+ })
+ .collect();
+ let clock = versions
+ .iter()
+ .find(|(r, _)| *r == replica as u64)
+ .map_or(1, |(_, c)| c + 1);
+
+ // The ordered set: the character tombstoned, the attachments closed up, the set without it.
+ let text = bytes(&saf, 1).next()?;
+ // Out at `at` itself: the text is all one character, so only the place says which.
+ let old_text: String = std::iter::repeat_n('\u{FFFC}', count).collect();
+ let (new_string, new_writers) = edit_table_text(text, bytes(&data, 7).next()?, |lent| {
+ crate::ckdocument::rewrite_text_object_at(lent, &old_text, at, 1, "")
+ })?;
+ let mut new_string_array = Vec::new();
+ for (tag, raw) in raw_fields(string_array)? {
+ match tag {
+ 1 => put_len(1, &new_string, &mut new_string_array),
+ 2 => {}
+ _ => new_string_array.extend_from_slice(raw),
+ }
+ }
+ for (index, uuid) in &attachments {
+ if *index == at as u64 {
+ continue;
+ }
+ let mut a = Vec::new();
+ put_int(
+ 1,
+ if *index > at as u64 {
+ index - 1
+ } else {
+ *index
+ },
+ &mut a,
+ );
+ put_len(2, uuid, &mut a);
+ put_len(2, &a, &mut new_string_array);
+ }
+ let mut new_array = Vec::new();
+ for (tag, raw) in raw_fields(array_body)? {
+ match tag {
+ 1 => put_len(1, &new_string_array, &mut new_array),
+ _ => new_array.extend_from_slice(raw),
+ }
+ }
+ let mut new_set = Vec::new();
+ for (tag, raw) in raw_fields(set_body)? {
+ match tag {
+ 1 => put_len(1, &new_array, &mut new_set),
+ 2 => put_len(
+ 2,
+ &without_element(bytes(&fields(raw)?, 2).next()?, element)?,
+ &mut new_set,
+ ),
+ _ => new_set.extend_from_slice(raw),
+ }
+ }
+ let mut changed_objects: HashMap<usize, Vec<u8>> = HashMap::new();
+ let mut object = Vec::new();
+ put_len(16, &new_set, &mut object);
+ changed_objects.insert(set_object, object);
+
+ // The cells: a row out of every column's dictionary, or a column out of `cellColumns`.
+ let (cells_kind, cells_body) = doc.object(cells_object)?;
+ if column {
+ let mut object = Vec::new();
+ put_len(
+ cells_kind,
+ &without_element(cells_body, identity)?,
+ &mut object,
+ );
+ changed_objects.insert(cells_object, object);
+ } else {
+ for &column_dictionary in Doc::dictionary(cells_body)?.values() {
+ let (k, body) = doc.object(column_dictionary)?;
+ if k != 6 {
+ return None;
+ }
+ // A column with no cell for this row has nothing to drop.
+ if let Some(trimmed) = without_element(body, identity) {
+ let mut object = Vec::new();
+ put_len(6, &trimmed, &mut object);
+ changed_objects.insert(column_dictionary, object);
+ }
+ }
+ }
+
+ // This replica's clock raised; every other replica's entry copied as it is.
+ let mut new_version_vector = Vec::new();
+ let mut seen_me = false;
+ for (_, raw) in raw_fields(bytes(&data, 1).next()?)? {
+ let f = fields(bytes(&fields(raw)?, 1).next()?)?;
+ if int(&f, 1).unwrap_or(0) == replica as u64 {
+ let mut e = Vec::new();
+ put_int(1, replica as u64, &mut e);
+ put_int(2, clock, &mut e);
+ put_len(1, &e, &mut new_version_vector);
+ seen_me = true;
+ } else {
+ new_version_vector.extend_from_slice(raw);
+ }
+ }
+ if !seen_me {
+ let mut e = Vec::new();
+ put_int(1, replica as u64, &mut e);
+ put_int(2, clock, &mut e);
+ put_len(1, &e, &mut new_version_vector);
+ }
+ let raw = raw_fields(data_bytes)?;
+ let last_uuid = raw.iter().rposition(|(t, _)| *t == 6)?;
+ let mut new_data = Vec::with_capacity(data_bytes.len());
+ let mut object_n = 0usize;
+ for (i, (tag, field)) in raw.iter().enumerate() {
+ match *tag {
+ 1 => put_len(1, &new_version_vector, &mut new_data),
+ // The table's writer list, with this installation in it.
+ 7 => put_len(7, &new_writers, &mut new_data),
+ 3 => {
+ match changed_objects.get(&object_n) {
+ Some(o) => put_len(3, o, &mut new_data),
+ None => new_data.extend_from_slice(field),
+ }
+ object_n += 1;
+ }
+ _ => new_data.extend_from_slice(field),
+ }
+ if i == last_uuid {
+ for u in &new_uuids {
+ put_len(6, u, &mut new_data);
+ }
+ }
+ }
+ let new_version = splice(version_bytes, 3, 0, &new_data)?;
+ let out = splice(document, 2, 0, &new_version)?;
+
+ let mut expected = before;
+ if column {
+ for row in &mut expected {
+ row.remove(at);
+ }
+ } else {
+ expected.remove(at);
+ }
+ (cells(&out)? == expected).then_some(out)
+}
+
/// [`with_cell`] over the attachment's `MergeableDataEncrypted` as Apple sends it (base64,
/// compressed), answered in the same compression it came in.
#[must_use]
@@ -767,6 +1043,18 @@ pub fn with_column_in_base64(value: &str, at: usize) -> Option<String> {
edit_in_base64(value, |plain| with_column(plain, at))
}
+/// [`without_row`] or [`without_column`] over the attachment's `MergeableDataEncrypted`.
+#[must_use]
+pub fn without_line_in_base64(value: &str, column: bool, at: usize) -> Option<String> {
+ edit_in_base64(value, |plain| {
+ if column {
+ without_column(plain, at)
+ } else {
+ without_row(plain, at)
+ }
+ })
+}
+
/// A table edit made to the inflated document, packed back as it came: gzip, zlib or plain.
fn edit_in_base64(value: &str, edit: impl FnOnce(&[u8]) -> Option<Vec<u8>>) -> Option<String> {
use base64::Engine;
diff --git a/probe/examples/table_remove_live.rs b/probe/examples/table_remove_live.rs
new file mode 100644
index 0000000..3f28454
--- /dev/null
+++ b/probe/examples/table_remove_live.rs
@@ -0,0 +1,46 @@
+//! Live, on a throwaway table only: a row and a column deleted, then a row added with the
+//! corrected writer, each read back from Apple.
+use anyhow::{anyhow, Result};
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let id = std::env::args()
+ .nth(1)
+ .ok_or_else(|| anyhow!("attachment id"))?;
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let root = client
+ .session()
+ .service_url("ckdatabasews")
+ .ok_or_else(|| anyhow!("no ck"))?;
+ let (http, p) = (client.http().clone(), client.service_params());
+ for (column, at) in [(false, 2usize), (true, 2)] {
+ match quince_icloud::notes::remove_table_line_cloudkit(
+ &http,
+ &root,
+ p.clone(),
+ &id,
+ column,
+ at,
+ )
+ .await
+ {
+ Ok(cells) => println!(
+ "{} {at} deleted, Apple holds: {cells:?}",
+ if column { "column" } else { "row" }
+ ),
+ Err(e) => println!(
+ "{} {at} delete refused: {e}",
+ if column { "column" } else { "row" }
+ ),
+ }
+ }
+ match quince_icloud::notes::add_table_row_cloudkit(&http, &root, p.clone(), &id, 1).await {
+ Ok(cells) => println!("row added at 1, Apple holds: {cells:?}"),
+ Err(e) => println!("row add refused: {e}"),
+ }
+ Ok(())
+}
diff --git a/probe/examples/table_row_offline.rs b/probe/examples/table_row_offline.rs
index f85a26a..a1f51dd 100644
--- a/probe/examples/table_row_offline.rs
+++ b/probe/examples/table_row_offline.rs
@@ -31,6 +31,19 @@ fn main() {
}
);
}
+ // The new top row saved beside the table, for its merge records to be read.
+ if let (Some(out), Ok(dir)) = (
+ quince_icloud::table::with_row(&doc, 0),
+ std::env::var("QUINCE_DUMP"),
+ ) {
+ std::fs::write(format!("{dir}/{name}-row0.bin"), out).ok();
+ }
+ if let (Some(out), Ok(dir)) = (
+ quince_icloud::table::without_row(&doc, 0),
+ std::env::var("QUINCE_DUMP"),
+ ) {
+ std::fs::write(format!("{dir}/{name}-del0.bin"), out).ok();
+ }
for at in [0, grid.len() / 2, grid.len()] {
match quince_icloud::table::with_row(&doc, at) {
None => println!(
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index fa34471..6d41474 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -1575,6 +1575,12 @@ async fn note_runs(guid: String, data: State<'_, Data>) -> UiResult<serde_json::
}
/// A table's cells, row by row, as the last pass read them — `None` when none are known.
+/// Whether Quince writes to Notes tables. Off: Apple stored what Quince wrote, but his iPhone
+/// did not show it (2026-10-19), so no table is written until a write is seen on the phone.
+const TABLE_WRITES: bool = false;
+const TABLE_WRITES_OFF: &str =
+ "Tables are read-only in Quince for now: edits made here did not show on iPhone yet.";
+
/// Add an empty row to a Notes table at `at` (0 is the top), as Notes does, and keep the
/// table as Apple now holds it.
#[tauri::command]
@@ -1585,6 +1591,9 @@ async fn note_table_add_row(
auth: State<'_, Auth>,
data: State<'_, Data>,
) -> UiResult<Vec<Vec<String>>> {
+ if !TABLE_WRITES {
+ return Err(TABLE_WRITES_OFF.into());
+ }
let (http, root, params) = cloudkit_context(&auth).await?;
let cells = quince_icloud::notes::add_table_line_cloudkit(
&http,
@@ -1606,6 +1615,34 @@ async fn note_table_add_row(
Ok(cells)
}
+/// Delete a row (or, with `column`, a column) of a Notes table, and keep the table as Apple
+/// now holds it.
+#[tauri::command]
+async fn note_table_remove(
+ id: String,
+ at: usize,
+ column: bool,
+ auth: State<'_, Auth>,
+ data: State<'_, Data>,
+) -> UiResult<Vec<Vec<String>>> {
+ if !TABLE_WRITES {
+ return Err(TABLE_WRITES_OFF.into());
+ }
+ let (http, root, params) = cloudkit_context(&auth).await?;
+ let cells =
+ quince_icloud::notes::remove_table_line_cloudkit(&http, &root, params, &id, column, at)
+ .await
+ .map_err(|e| e.to_string())?;
+ if let Ok(json) = serde_json::to_string(&cells) {
+ data.0
+ .lock()
+ .await
+ .set_state(&quince_icloud::table::state_key(&id), &json)
+ .map_err(|e| e.to_string())?;
+ }
+ Ok(cells)
+}
+
/// Change one cell of a Notes table, and keep the table as Apple now holds it.
#[tauri::command]
async fn note_table_set_cell(
@@ -1616,6 +1653,9 @@ async fn note_table_set_cell(
auth: State<'_, Auth>,
data: State<'_, Data>,
) -> UiResult<Vec<Vec<String>>> {
+ if !TABLE_WRITES {
+ return Err(TABLE_WRITES_OFF.into());
+ }
let (http, root, params) = cloudkit_context(&auth).await?;
let cells = quince_icloud::notes::set_table_cell_cloudkit(
&http, &root, params, &id, row, column, &text,
@@ -3644,6 +3684,7 @@ fn main() {
note_table,
note_table_set_cell,
note_table_add_row,
+ note_table_remove,
note_attachment_info,
note_attachment_open,
diagnostics_save,
diff --git a/src/lib/ipc.ts b/src/lib/ipc.ts
index 810af37..fc78a08 100644
--- a/src/lib/ipc.ts
+++ b/src/lib/ipc.ts
@@ -145,6 +145,8 @@ export const api = {
noteTableAddRow: (id: string, at: number) => invoke<string[][]>("note_table_add_row", { id, at, column: false }),
/** An empty column added at `at` (0 the leading edge). */
noteTableAddColumn: (id: string, at: number) => invoke<string[][]>("note_table_add_row", { id, at, column: true }),
+ /** The row (or column) at `at` deleted; answers the table as Apple now holds it. */
+ noteTableRemove: (id: string, at: number, column: boolean) => invoke<string[][]>("note_table_remove", { id, at, column }),
noteRuns: (guid: string) => invoke<import("../screens/notes/Formatted").Run[]>("note_runs", { guid }),
noteFolderCreate: (title: string, parent: string | null) =>
invoke<string>("note_folder_create", { title, parent }),
diff --git a/src/screens/NotesView.tsx b/src/screens/NotesView.tsx
index 3438551..7d6746f 100644
--- a/src/screens/NotesView.tsx
+++ b/src/screens/NotesView.tsx
@@ -7,7 +7,7 @@ import { api, messageOf, nestFolders, type Note, type NoteFolder } from "../lib/
import { RowMenu } from "../components/RowMenu";
import { FormattedBody, type Run } from "./notes/Formatted";
import { carryRuns, runsFit } from "./notes/rich";
-import { NoteEditor, type EditorHandle, type EditorStatus } from "./notes/NoteEditor";
+import { NoteEditor, refreshTables, type EditorHandle, type EditorStatus } from "./notes/NoteEditor";
import { FormatPanel } from "./notes/FormatPanel";
import { useSync } from "../lib/sync";
import { Picker } from "../components/Picker";
@@ -56,6 +56,10 @@ export function NotesView() {
const [loaded, setLoaded] = useState(false);
const [error, setError] = useState<string | null>(null);
const { revision } = useSync();
+ // Tables on screen keep up with a phone's edits, as the note's text does.
+ useEffect(() => {
+ if (revision) refreshTables();
+ }, [revision]);
/** The body being typed, held apart from the note until it is saved. */
const [draft, setDraft] = useState<string | null>(null);
/** `draft`, for effects that read it without re-running on every keystroke. */
diff --git a/src/screens/notes/NoteEditor.tsx b/src/screens/notes/NoteEditor.tsx
index 536153b..817d929 100644
--- a/src/screens/notes/NoteEditor.tsx
+++ b/src/screens/notes/NoteEditor.tsx
@@ -5,12 +5,37 @@ import { EditorView } from "prosemirror-view";
import { history, undo, redo } from "prosemirror-history";
import { keymap } from "prosemirror-keymap";
import { baseKeymap, splitBlockAs, toggleMark, chainCommands } from "prosemirror-commands";
-import type { Node as PMNode } from "prosemirror-model";
+import { Fragment, Slice, type Node as PMNode } from "prosemirror-model";
import type { Run } from "./Formatted";
import { fromDoc, LIST_STYLES, schema, STYLE, tableCells, toDoc } from "./rich";
import { api, noteMediaSrc, type NoteAttachment } from "../../lib/ipc";
import { RowMenu, type MenuEntry } from "../../components/RowMenu";
+/** `content` with every attachment taken out, a table's cells kept as text in its place. */
+function withoutAttachments(content: Fragment): Fragment {
+ const kept: PMNode[] = [];
+ content.forEach((node) => {
+ if (node.type === schema.nodes.attachment) {
+ const text = node.textContent || (node.type.spec.leafText?.(node) ?? "");
+ if (text) kept.push(schema.text(text.replace(/\s*\n\s*/g, " ")));
+ return;
+ }
+ kept.push(node.isLeaf ? node : node.copy(withoutAttachments(node.content)));
+ });
+ return Fragment.fromArray(kept);
+}
+
+/** Every table on screen, by its element: how to read it again. */
+const liveTables = new Map<HTMLElement, () => void>();
+
+/** Every table on screen read again — after a sync, for a change made on another device. */
+export function refreshTables() {
+ liveTables.forEach((read) => read());
+}
+
+/** Whether tables can be edited here. Off until an edit made here is seen on an iPhone. */
+const TABLE_WRITES = false;
+
/** A table's right-click menu: drawn by the editor that holds the table, opened by a cell. */
type CellMenu = { at: { x: number; y: number; alignRight: boolean }; entries: MenuEntry[] };
let openCellMenu: ((menu: CellMenu) => void) | null = null;
@@ -122,9 +147,11 @@ function attachmentView(node: PMNode) {
row.forEach((cell, c) => {
const td = tr.insertCell();
const was = cell.trimEnd();
- if (!id) {
+ // Read-only for now: what Quince wrote to a table was stored at Apple but did not show
+ // on his iPhone, so no table is written until a write is seen there.
+ if (!id || !TABLE_WRITES) {
td.textContent = was;
- td.className = "nf-cell-empty";
+ td.className = was ? "nf-cell" : "nf-cell-empty";
return;
}
// Right-click, as in Notes: a row added above or below this one.
@@ -137,6 +164,13 @@ function attachmentView(node: PMNode) {
(err) => { dom.classList.remove("is-saving"); dom.title = String(err); },
);
};
+ const remove = (at: number, column: boolean) => () => {
+ dom.classList.add("is-saving");
+ api.noteTableRemove(id, at, column).then(
+ (next) => { dom.classList.remove("is-saving"); draw(next); },
+ (err) => { dom.classList.remove("is-saving"); dom.title = String(err); },
+ );
+ };
openCellMenu?.({
at: { x: e.clientX, y: e.clientY, alignRight: false },
entries: [
@@ -145,6 +179,10 @@ function attachmentView(node: PMNode) {
null,
{ label: "Add Column Before", run: add(c, true) },
{ label: "Add Column After", run: add(c + 1, true) },
+ null,
+ // A table keeps at least one row and one column, as Notes does.
+ ...(cells.length > 1 ? [{ label: "Delete Row", danger: true, run: remove(r, false) }] : []),
+ ...((cells[0]?.length ?? 0) > 1 ? [{ label: "Delete Column", danger: true, run: remove(c, true) }] : []),
],
});
});
@@ -202,9 +240,21 @@ function attachmentView(node: PMNode) {
},
() => {},
);
+ // Read again after every sync, so a table typed in on a phone shows its new cells here
+ // without leaving the note; drawn again only when they changed.
+ liveTables.set(dom, () => {
+ if (!dom.classList.contains("is-table")) return;
+ api.noteTable(id).then(
+ (cells) => {
+ if (cells && cells.length && JSON.stringify(cells) !== JSON.stringify(tableCells.get(id))) draw(cells);
+ },
+ () => {},
+ );
+ });
}
return {
dom,
+ destroy: () => void liveTables.delete(dom),
ignoreMutation: () => true,
// A cell's keys, clicks and typing are the cell's, not the note editor's.
stopEvent: (e: Event) => Boolean((e.target as HTMLElement | null)?.closest?.(".nf-cell-edit, .nf-image, .nf-doc, .nf-file")),
@@ -441,6 +491,20 @@ export const NoteEditor = forwardRef<EditorHandle, Props>(function NoteEditor(
if (tr.docChanged) reportSoon();
callbacks.current.onStatus?.(statusOf(next));
},
+ // A paste never brings an attachment's reference: Apple keeps each table or picture in
+ // one place in one note, and a pasted copy referred to the same one twice, which made
+ // Notes on a phone lag and crash. The copy's text (a table's cells) comes instead.
+ // A pasted table, as Notes pastes one into text: a line per row, its cells by tabs.
+ transformPastedHTML(html) {
+ return html
+ .replace(/<\/t[dh]>\s*<t[dh][^>]*>/gi, "\t")
+ .replace(/<tr[^>]*>/gi, "<p>")
+ .replace(/<\/tr>/gi, "</p>")
+ .replace(/<\/?(table|tbody|thead|tfoot|td|th)[^>]*>/gi, "");
+ },
+ transformPasted(slice) {
+ return new Slice(withoutAttachments(slice.content), slice.openStart, slice.openEnd);
+ },
// Lists typed as on the phone: "- " a dashed list, "* " a bulleted one, "1. " a numbered
// one; in a dashed line "[ ] " or "[x] " makes it a checklist item, so "- [ ] " works
// (icloud.com's notes3 userDidInsert). The typed marker goes; one undo brings it back.