diff --git a/.agents/skills/reverse-engineer-anything/references/desktop-build.md b/.agents/skills/reverse-engineer-anything/references/desktop-build.md
index 5618a8ad9a..8ab1876a5a 100644
--- a/.agents/skills/reverse-engineer-anything/references/desktop-build.md
+++ b/.agents/skills/reverse-engineer-anything/references/desktop-build.md
@@ -183,6 +183,19 @@ the first source that has the app:
only at a clean exit. It writes ``;
+ const file = drafted(before, after);
+ const results = evaluateTaskChecks(
+ file.suggested.map((check, index) => ({ ...check, id: `c${index}` })),
+ new Map([
+ [
+ FILE,
+ {
+ format: "text" as const,
+ view: textView(`${opening}${node("1793955259999")}\n`),
+ },
+ ],
+ ]),
+ );
+ expect(file.suggested).toEqual([
+ expect.objectContaining({
+ kind: "line_count",
+ contains: ' {
+ const opening = ']>\n\n';
+ const line =
+ '';
+ const file = drafted(`${opening}`, `${opening}${line}\n`);
+ expect(file.suggested).toEqual([
+ { kind: "lines_present", file: FILE, lines: [line] },
+ ]);
+ const altered = line.replace("2025550123", "WRONG");
+ const results = evaluateTaskChecks(
+ file.suggested.map((check, index) => ({ ...check, id: `c${index}` })),
+ new Map([
+ [
+ FILE,
+ {
+ format: "text" as const,
+ view: textView(`${opening}${altered}\n`),
+ },
+ ],
+ ]),
+ );
+ expect(results.map(({ passed }) => passed)).toEqual([false]);
+ });
+
+ it("keeps requested text inside a multiline XML attribute exact", () => {
+ const opening = ']>\n\n';
+ const text = 'created=2025550123 and save"/>';
+ const file = drafted(
+ `${opening}`,
+ `${opening} ({ ...check, id: `c${index}` })),
+ new Map([
+ [
+ FILE,
+ {
+ format: "text" as const,
+ view: textView(
+ `${opening}`,
+ ),
+ },
+ ],
+ ]),
+ );
+ expect(result.some(({ passed }) => !passed)).toBe(true);
+ });
+
+ it("recognizes an attribute on a later line of an XML tag", () => {
+ const opening = ']>\n\n`, `${opening}${attribute}\n`);
+ expect(file.suggested).toEqual([
+ expect.objectContaining({
+ kind: "line_count",
+ contains: 'TEXT="C:\\work\\" CREATED="',
+ }),
+ ]);
+ });
+});
+
+describe("draftTaskFiles for markup boundaries and incomplete XML", () => {
+ it("keeps quoted text after a Markdown autolink", () => {
+ const line =
+ ' msgstr "Set \\\"created=2025550123 and save"';
+ const file = drafted("# Note", `# Note\n${line}`);
+ expect(file.suggested).toEqual([
+ { kind: "lines_present", file: FILE, lines: [line] },
+ ]);
+ });
+
+ it("recognizes attributes after a comment inside the DTD", () => {
+ const opening = "]>\n\n";
+ const node = '';
+ const file = drafted(`${opening}`, `${opening}${node}\n`);
+ expect(file.suggested).toEqual([
+ expect.objectContaining({
+ kind: "line_count",
+ contains: ' {
+ const opening = ']>\n\n';
+ const node = '';
+ const file = drafted(
+ `${opening}`,
+ `${opening}\n${node}\n`,
+ );
+ expect(file.suggested).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ kind: "line_count",
+ contains: '\n`),
+ },
+ ],
+ ]),
+ );
+ expect(results.some(({ passed }) => !passed)).toBe(true);
+ });
+
+ it("does not guess attribute boundaries when the XML syntax reader cannot finish", () => {
+ const opening = ']>\n&title;\n';
+ const node = '';
+ const file = drafted(`${opening}`, `${opening}${node}\n`);
+ expect(file.suggested).toEqual([]);
+ expect(file.notes).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ code: "read_as_text",
+ message: expect.stringContaining("could not locate XML attributes"),
+ }),
+ ]),
+ );
+ });
+});
+
+describe("draftTaskFiles for added lines", () => {
+ it("recognizes Unicode XML names before a malformed closing tag", () => {
+ const opening = "\n";
+ const node = '';
+ const file = drafted(`${opening}`, `${opening}${node}\n`);
+ expect(file.suggested).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ kind: "line_count",
+ contains: ' {
+ const line = "Tracking id: 123e4567-e89b-12d3-a456-426614174000";
+ const file = drafted("# Notes", `# Notes\n${line}`);
+ expect(file.suggested).toEqual([
+ { kind: "lines_present", file: FILE, lines: [line] },
+ ]);
+ const results = evaluateTaskChecks(
+ file.suggested.map((check, index) => ({ ...check, id: `c${index}` })),
+ new Map([
+ [
+ FILE,
+ {
+ format: "text" as const,
+ view: textView("# Notes\nTracking id: WRONG"),
+ },
+ ],
+ ]),
+ );
+ expect(results.map(({ passed }) => passed)).toEqual([false]);
+ });
+
+ it("retains parsed attribute positions before a malformed closing tag", () => {
+ const opening = "\n";
+ const node = '';
+ const file = drafted(`${opening}`, `${opening}${node}\n`);
+ expect(file.suggested).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ kind: "line_count",
+ contains: ' {
+ const opening = `<${link}>\n`;
+ const line = 'msgstr "Set \\\"created=2025550123 and save"';
+ const file = drafted(opening, `${opening}${line}`);
+ expect(file.suggested).toEqual([
+ { kind: "lines_present", file: FILE, lines: [line] },
+ ]);
+ });
+
+ it("keeps long ordinary text as a whole line", () => {
+ const text = `msgstr "${"ordinary words ".repeat(3000)}"`;
+ const file = drafted(lines(), lines(text));
+ expect(file.suggested).toEqual([
+ { kind: "lines_present", file: FILE, lines: [text] },
+ ]);
+ });
+ it("keeps phone numbers but counts a stable prefix before a named creation time", () => {
+ const phone = 'topic TEXT="Call 2025550123"';
+ const timed = 'topic TEXT="Water filter" CREATED="1793955250671"';
+ const file = drafted(lines(), lines(phone, timed));
+ expect(file.suggested).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ kind: "lines_present", lines: [phone] }),
+ expect.objectContaining({
+ kind: "line_count",
+ contains: 'topic TEXT="Water filter" CREATED="',
+ }),
+ ]),
+ );
+ });
+ it("counts the lines holding a new line's text up to its first generated value, rather than the line whole, which another save would not repeat", () => {
+ const file = drafted(
+ lines(),
+ lines(
+ 'topic TEXT="Water filter" ID="ID_329023692" CREATED="1793955250671" MODIFIED="1793955252979"',
+ ),
+ );
+
+ expect(file.suggested).toEqual([
+ {
+ kind: "line_count",
+ file: FILE,
+ contains: 'topic TEXT="Water filter" ID="',
+ equals: 1,
+ },
+ ]);
+ expect(file.notes).toEqual([]);
+ });
+
+ it("keeps an ID-like token without a generated ID field", () => {
+ const line = "ID_329023692 1793955250671";
+ const file = drafted(lines(), lines(line));
+ expect(file.suggested).toEqual([
+ { kind: "lines_present", file: FILE, lines: [line] },
+ ]);
+ });
+
+ it("guards a kept line beside a new one, never a lone closing tag any such file holds", () => {
+ const file = drafted(lines(), lines("Water filter"));
+
+ expect(file.guards).toEqual([
+ {
+ kind: "lines_present",
+ file: FILE,
+ lines: [
+ 'topic TEXT="Packing" ID="ID_1965352635"',
+ 'topic TEXT="Sleeping bags" ID="ID_1227863932" CREATED="1793955122470"',
+ ],
+ },
+ ]);
+ });
+});
+
+describe("draftTaskFiles for lines added to a text file longer than its view", () => {
+ it("counts no lines a view cut short, since a count there is undecided", () => {
+ // Both views keep the first 5,000 lines of 5,001 or more, the new line among them.
+ const long = (...added: string[]) =>
+ [
+ ...added,
+ ...Array.from({ length: 5001 }, (_, index) => `line ${String(index)}`),
+ ].join("\n");
+
+ const file = drafted(
+ long(),
+ long(
+ 'topic TEXT="Water filter" ID="ID_329023692" CREATED="1793955250671"',
+ ),
+ );
+
+ expect(file.change).toMatchObject({ complete: false });
+ expect(file.suggested.map(({ kind }) => kind)).not.toContain("line_count");
+ });
+});
+
+describe("draftTaskFiles guards of a document of commands and text, as LyX saves one", () => {
+ /** A LyX body: each heading and paragraph opened by a command and closed by `\end_layout`. */
+ const report = (heading: string) =>
+ [
+ "\\begin_body",
+ "",
+ "\\begin_layout Section",
+ "Introduction",
+ "\\end_layout",
+ "",
+ "\\begin_layout Standard",
+ "This report summarises the spring season.",
+ "\\end_layout",
+ "",
+ "\\begin_layout Section",
+ heading,
+ "\\end_layout",
+ "",
+ "\\begin_layout Standard",
+ "Volunteers spread compost in early March.",
+ "\\end_layout",
+ "",
+ "\\begin_layout Section",
+ "Harvest Results",
+ "\\end_layout",
+ "",
+ "\\end_body",
+ ].join("\n");
+ const doc = (heading: string) => {
+ const view = textView(report(heading));
+ return { view, file: "Documents/garden-report.lyx" };
+ };
+
+ it("guards the other headings the same command opens, never a command line or one every section repeats", () => {
+ const [seed, end] = [doc("Soil Preperation"), doc("Soil Preparation")];
+ const [file] = draftOf({
+ [seed.file]: statePath("document", "text", seed.view, end.view),
+ });
+
+ expect(file?.suggested).toEqual([
+ { kind: "lines_present", file: seed.file, lines: ["Soil Preparation"] },
+ { kind: "lines_absent", file: seed.file, lines: ["Soil Preperation"] },
+ ]);
+ expect(file?.guards).toEqual([
+ {
+ kind: "lines_present",
+ file: seed.file,
+ lines: ["Introduction", "Harvest Results"],
+ },
+ ]);
+ });
+});
+
+describe("draftTaskFiles guards of lines with no text of their own", () => {
+ it("guards a text file's nearest lines with words, never a key alone, as a RedNotebook day's `24:`, nor a tag alone, as ``", () => {
+ const journal = (twentieth: string) =>
+ [
+ "20:",
+ " text: Repotted the basil and the rosemary on the balcony. The rosemary roots had",
+ ` circled the whole pot.${twentieth}`,
+ "24:",
+ " text: Cycled to the farmers market with Priya.",
+ "",
+ "",
+ ].join("\n");
+ const file = ".rednotebook/data/2026-10.txt";
+ const [drafted] = draftOf({
+ [file]: statePath(
+ "application_state",
+ "text",
+ textView(journal("")),
+ textView(journal(" Moved the rosemary to the sunny corner.")),
+ ),
+ });
+
+ expect(drafted?.guards).toEqual([
+ {
+ kind: "lines_present",
+ file,
+ lines: [
+ " text: Repotted the basil and the rosemary on the balcony. The rosemary roots had",
+ " text: Cycled to the farmers market with Priya.",
+ ],
+ },
+ ]);
+ });
+});
+
+describe("draftTaskFiles for the app's log beside its journal", () => {
+ it("drafts a changed unselected conversation history in app state", () => {
+ const [file] = draftOf({
+ ".config/app/history.log": statePath(
+ "application_state",
+ "text",
+ textView("Ada: Hello"),
+ textView("Ada: Hello\nLin: Welcome"),
+ ),
+ });
+ expect(file?.suggested).toContainEqual({
+ kind: "lines_present",
+ file: ".config/app/history.log",
+ lines: ["Lin: Welcome"],
+ });
+ });
+
+ it("drafts an app log and notes a text file that opens like XML but is not", () => {
+ const lines = (...text: string[]) => textView(text.join("\n"));
+ const files = draftOf({
+ ".rednotebook/data/2026-10.txt": statePath(
+ "application_state",
+ "text",
+ lines("20:", " text: Repotted the basil."),
+ lines("20:", " text: Repotted the basil. Moved the rosemary."),
+ ),
+ ".rednotebook/rednotebook.log": statePath(
+ "application_state",
+ "text",
+ lines("2026-11-06 15:11:21,847 DEBUG Cloud updated"),
+ lines(
+ "2026-11-06 15:11:21,847 DEBUG Cloud updated",
+ "2026-11-06 15:11:50,559 INFO Wrote file /home/user/.rednotebook/data/2026-10.txt",
+ ),
+ ),
+ ".config/app/state.xml": statePath(
+ "application_state",
+ "text",
+ lines("]>", "&e;"),
+ lines("]>", "&e;!"),
+ ),
+ });
+
+ const byFile = new Map(files.map((file) => [file.file, file]));
+ expect(byFile.get(".rednotebook/rednotebook.log")?.suggested).not.toEqual(
+ [],
+ );
+ expect(byFile.get(".rednotebook/data/2026-10.txt")?.suggested).not.toEqual(
+ [],
+ );
+ expect(
+ byFile.get(".config/app/state.xml")?.notes.map(({ code }) => code),
+ ).toEqual(["read_as_text"]);
+ });
+
+ it("drafts a log the caller selected, and does not call a quoted bracket an entity subset", () => {
+ const lines = (...text: string[]) => textView(text.join("\n"));
+ const selected = {
+ ...statePath(
+ "application_state",
+ "text",
+ lines("opened"),
+ lines("opened", "saved"),
+ ),
+ reasons: ["selected_path" as const],
+ };
+ const logged = draftOf({ ".config/app/notes.log": selected }, [
+ ".config/app/notes.log",
+ ]);
+ const noted = draftOf({
+ "Documents/state.txt": statePath(
+ "document",
+ "text",
+ lines('', ""),
+ lines('', "saved"),
+ ),
+ });
+
+ expect(logged[0]?.suggested).not.toEqual([]);
+ expect(
+ noted[0]?.notes.some((note) => note.message.includes("internal subset")),
+ ).toBe(false);
+ });
+});
+
+describe("draftTaskFiles guards of a line-keyed catalogue, as Poedit saves one", () => {
+ it("guards the other records' lines of the changed line's keyword, never its own record's source reference or msgid", () => {
+ const entry = (line: number, msgid: string, msgstr: string) => [
+ `#: argparse.py:${String(line)}`,
+ `msgid "${msgid}"`,
+ `msgstr "${msgstr}"`,
+ "",
+ ];
+ const catalogue = (fixed: string) =>
+ [
+ ...entry(1811, "positional arguments", "arguments positionnels"),
+ ...entry(1858, "cannot have multiple subparser arguments", ""),
+ ...entry(2249, "expected one argument", fixed),
+ ...entry(
+ 2250,
+ "expected at most one argument",
+ "au plus un argument attendu",
+ ),
+ ...entry(2312, "ambiguous option", ""),
+ ].join("\n");
+ const file = "Documents/fr.po";
+ const [drafted] = draftOf({
+ [file]: statePath(
+ "document",
+ "text",
+ textView(catalogue("un argument inattendu")),
+ textView(catalogue("un argument attendu")),
+ ),
+ });
+
+ expect(drafted?.guards).toEqual([
+ {
+ kind: "lines_present",
+ file,
+ lines: [
+ 'msgstr "arguments positionnels"',
+ 'msgstr "au plus un argument attendu"',
+ ],
+ },
+ ]);
+ });
+});
diff --git a/src/domain/desktop/appTaskDraftLineGuards.ts b/src/domain/desktop/appTaskDraftLineGuards.ts
index 8d34bfcf23..bf354f22d2 100644
--- a/src/domain/desktop/appTaskDraftLineGuards.ts
+++ b/src/domain/desktop/appTaskDraftLineGuards.ts
@@ -1,6 +1,13 @@
+import { xmlAttributeQuoteRanges } from "../xmlDocument.js";
import type { DesktopTaskCheckInput } from "./appTask.js";
+import {
+ isBookkeepingName,
+ isUuid,
+ isVolatileValue,
+} from "./appTaskDraftNoise.js";
+import { textOpensXml, type FileNote } from "./appTaskDraftFileNotes.js";
-/** Lines a text file's guard names, and how far from the added line it looks for them. */
+/** Lines a text file's guard names, and how far from the added line it looks for them when it has no like sibling. */
const MAX_GUARD_LINES = 2;
const NEIGHBOURHOOD = 6;
/** A guard line longer than this is not copied into the task. */
@@ -10,49 +17,258 @@ const MAX_GUARD_TEXT = 2000;
const markerOf = (line: string): string | undefined =>
/^\s*(?\[[ xX*>]?\]|[-*+•]|\d+[.)])\s/u.exec(line)?.groups?.marker;
+/** Whether a line is a command, as LyX's `\begin_layout Section` or `\end_layout`. */
+const isCommand = (line: string) => /^\s*\\[\p{L}_]+\b/u.test(line);
+
+/** Whether a line is only markup: a command, tags with no text between them, as KmPlot's `` or ``, or a key alone, as a RedNotebook day's `24:`. */
+const isMarkup = (line: string) =>
+ isCommand(line) ||
+ !/[\p{L}\d]/u.test(
+ line.replaceAll(/<[^>]*>/gu, "").replace(/^\s*[^\s:]+:\s*$/u, ""),
+ );
+
+/** The keyword a line of a line-keyed format opens with, as a gettext catalogue's `msgstr "…"`, or its `key:` or `key =`. */
+const keywordOf = (line: string): string | undefined =>
+ /^\s*(?[A-Za-z_][\w.-]*)(?:\s*[:=]|\s+")/u.exec(line)?.groups
+ ?.keyword;
+
+/** What kind of line a line is, for its like siblings: a list item's marker, the command opening it, as `\begin_layout Section` opens a LyX heading, or its keyword (`keywordOf`), as Poedit's other `msgstr` lines. */
+const kindOf = (lines: readonly string[], index: number) => {
+ const line = lines[index] ?? "";
+ const marker = markerOf(line);
+ const opener = lines[index - 1];
+ const keyword = keywordOf(line);
+ return marker !== undefined
+ ? `marker\u0000${marker}`
+ : opener !== undefined && isCommand(opener)
+ ? `opens\u0000${opener.trim()}`
+ : keyword === undefined
+ ? undefined
+ : `keyword\u0000${keyword}`;
+};
+
+/** How many times each line occurs. */
+const tally = (lines: readonly string[]) => {
+ const counts = new Map();
+ for (const line of lines) counts.set(line, (counts.get(line) ?? 0) + 1);
+ return counts;
+};
+
/**
* A `lines_present` guard of a text file's unchanged lines beside the first
- * line an edit added, for an author to add by choice: the list items next to
- * it with the same marker, as the other `[ ]` action items around a new one,
- * nearest first and kept in file order; or, when it is no list item or has no
- * such sibling, the nearest lines with words in them. Each is a line the
- * seed and the end both hold. None when nothing was added.
+ * line an edit added, for an author to add by choice: the nearest lines of
+ * its kind (`kindOf`), as the other `[ ]` action items around a new one,
+ * the other headings a LyX command opens or a gettext catalogue's other
+ * `msgstr` lines, never its own record's `msgid`, kept in file order; or,
+ * when it has no such sibling, the nearest lines with words in them. Each
+ * is text
+ * the seed and the end both hold once: never a line of markup alone, as
+ * LyX's `\end_layout`, a tag or a key with no text, nor one that repeats.
+ * None when nothing was added.
*/
export const lineGuards = (
file: string,
before: readonly string[],
after: readonly string[],
): DesktopTaskCheckInput[] => {
- const seed = new Set(before);
+ const [seed, end] = [tally(before), tally(after)];
const at = after.findIndex((line) => !seed.has(line));
- const added = after[at];
- if (added === undefined) return [];
- const near = after
+ if (at < 0) return [];
+ const kind = kindOf(after, at);
+ const kept = after
.map((line, index) => ({ line, index }))
.filter(
({ line, index }) =>
index !== at &&
- Math.abs(index - at) <= NEIGHBOURHOOD &&
- seed.has(line) &&
+ seed.get(line) === 1 &&
+ end.get(line) === 1 &&
line.length <= MAX_GUARD_TEXT &&
- /[\p{L}\d]/u.test(line),
+ /[\p{L}\d]/u.test(line) &&
+ !isMarkup(line),
)
.toSorted(
(left, right) =>
Math.abs(left.index - at) - Math.abs(right.index - at) ||
left.index - right.index,
);
- const marker = markerOf(added);
- const siblings = near.filter(
- ({ line }) => marker !== undefined && markerOf(line) === marker,
+ const siblings = kept.filter(
+ ({ index }) => kind !== undefined && kindOf(after, index) === kind,
);
- const lines = [
- ...new Set(
- (siblings.length > 0 ? siblings : near)
- .slice(0, MAX_GUARD_LINES)
- .toSorted((left, right) => left.index - right.index)
- .map(({ line }) => line),
- ),
- ];
+ const near = kept.filter(
+ ({ index }) => Math.abs(index - at) <= NEIGHBOURHOOD,
+ );
+ const lines = (siblings.length > 0 ? siblings : near)
+ .slice(0, MAX_GUARD_LINES)
+ .toSorted((left, right) => left.index - right.index)
+ .map(({ line }) => line);
return lines.length === 0 ? [] : [{ kind: "lines_present", file, lines }];
};
+
+/** A run of letters, digits, `_` and `-`, a token a line may hold. */
+const TOKEN = /[\p{L}\d_-]+/gu;
+
+/** Where a line's first identifiable generated time or id starts; -1 when it holds none. */
+const volatileAt = (
+ line: string,
+ xmlQuotes: ReadonlySet,
+ enclosingQuote: string | undefined,
+): number => {
+ let end = 0;
+ let quote = enclosingQuote;
+ let xmlAttribute = enclosingQuote !== undefined;
+ let previous: string | undefined;
+ let previousOutside = false;
+ for (const { 0: token, index } of line.matchAll(TOKEN)) {
+ const gap = line.slice(end, index);
+ const key =
+ quote === undefined && previousOutside && /^\s*=\s*["']?$/u.test(gap)
+ ? previous
+ : undefined;
+ for (let at = 0; at < gap.length; at += 1) {
+ const character = gap[at];
+ if (character === "\\" && quote !== undefined && !xmlAttribute) {
+ at += 1;
+ } else if (character === quote) {
+ quote = undefined;
+ xmlAttribute = false;
+ } else if (
+ quote === undefined &&
+ (character === '"' ||
+ (character === "'" && /[\s=]/u.test(line[end + at - 1] ?? " ")))
+ ) {
+ quote = character;
+ xmlAttribute = xmlQuotes.has(end + at);
+ }
+ }
+ if (
+ (isVolatileValue(token, key !== undefined && isBookkeepingName(key)) &&
+ !isUuid(token)) ||
+ (key !== undefined &&
+ /^(?:[^:]+:)?id$/iu.test(key) &&
+ /^\p{L}+_\d{6,}$/u.test(token))
+ )
+ return index;
+ previous = token;
+ previousOutside = quote === undefined;
+ end = index + token.length;
+ }
+ return -1;
+};
+
+/**
+ * Checks of the lines a text file gained: a `lines_present` of those whole,
+ * and, for a line holding a value the app generates, as a Freeplane map's
+ * `ID="ID_329023692" CREATED="1793955250671"`, which a later save of the same
+ * edit would not repeat, a `line_count` of the lines holding its text up to
+ * that value, as `();
+ const unexamined = new Set();
+ let lineStart = 0;
+ let nextRange = 0;
+ for (const line of lines.after) {
+ const quoteStarts = new Set();
+ const lineEnd = lineStart + line.length;
+ while (nextRange < ranges.length && ranges[nextRange]!.end < lineStart)
+ nextRange += 1;
+ let enclosingQuote: string | undefined;
+ for (let index = nextRange; index < ranges.length; index += 1) {
+ const range = ranges[index]!;
+ if (range.start >= lineEnd) break;
+ if (range.start < lineStart) enclosingQuote = source[range.start];
+ else quoteStarts.add(range.start - lineStart);
+ }
+ if (addedSet.has(line)) {
+ if (parsed?.ok === false && lineEnd > examined) {
+ unexamined.add(line);
+ lineStart = lineEnd + 1;
+ continue;
+ }
+ const at = volatileAt(line, quoteStarts, enclosingQuote);
+ const previous = classifications.get(line);
+ if (
+ previous === undefined ||
+ (at >= 0 && (previous < 0 || at < previous))
+ )
+ classifications.set(line, at);
+ }
+ lineStart = lineEnd + 1;
+ }
+ const classified = added
+ .filter((line) => !unexamined.has(line))
+ .map((line) => ({
+ line,
+ at: classifications.get(line) ?? -1,
+ }));
+ const whole = classified.filter(({ at }) => at < 0).map(({ line }) => line);
+ const prefixes = new Set();
+ let unnamed = 0;
+ for (const { line, at } of classified.filter(
+ ({ at }) => counted && at >= 0,
+ )) {
+ const prefix = line.slice(0, at).trimStart();
+ const equals = holding(lines.after, prefix);
+ if (
+ prefix.trim().length >= 3 &&
+ /\p{L}/u.test(prefix) &&
+ equals !== holding(lines.before, prefix)
+ )
+ prefixes.add(prefix);
+ else unnamed += 1;
+ }
+ return {
+ checks: [
+ ...(whole.length > 0
+ ? [{ kind: "lines_present" as const, file, lines: whole }]
+ : []),
+ ...[...prefixes].map((contains) => ({
+ kind: "line_count" as const,
+ file,
+ contains,
+ equals: holding(lines.after, contains),
+ })),
+ ],
+ notes: [
+ ...(unexamined.size === 0
+ ? []
+ : [
+ {
+ code: "read_as_text" as const,
+ file,
+ message: `REA could not locate XML attributes in ${String(unexamined.size)} added line(s) of ~/${file}, so no checks are suggested for them; inspect the changed text and add checks manually.`,
+ },
+ ]),
+ ...(unnamed === 0
+ ? []
+ : [
+ {
+ code: "volatile_lines" as const,
+ file,
+ message: `${String(unnamed)} line(s) ~/${file} gained hold a value the app generates, an id or a time, and no text before it tells them apart, so no check of them is suggested: check what the line says with line_count and contains.`,
+ },
+ ]),
+ ],
+ };
+};
diff --git a/src/domain/desktop/appTaskDraftMemberCount.ts b/src/domain/desktop/appTaskDraftMemberCount.ts
new file mode 100644
index 0000000000..5a67133ab0
--- /dev/null
+++ b/src/domain/desktop/appTaskDraftMemberCount.ts
@@ -0,0 +1,76 @@
+import type { JsonValue } from "../jsonValue.js";
+import { isXmlElement, resolvePointer } from "./viewPointer.js";
+
+/** A file's views at its two ends. */
+interface Ends {
+ readonly baseline: JsonValue | null;
+ readonly final: JsonValue | null;
+}
+
+/**
+ * Whether a value is a count an app keeps of an element's members, as VYM's
+ * root `branchCount`, KMyMoney's `` or GnuCash's
+ * `2` in its book:
+ * an attribute whose key ends in `count`, or the text of an element whose
+ * tag names a count, an integer that changed, at each end equal to how many
+ * elements of one tag the element, or for text the element holding it,
+ * holds at any depth there, the counter itself left out; or, for a counter
+ * the change added, to how many of one tag, more than one, it holds as
+ * children, as FreeCAD's `` its `Dep`s. A count a
+ * user enters, as an item's ``, tallies no tag so.
+ */
+export const isMemberCount = (
+ ends: Ends,
+ pointer: string,
+ key: string | undefined,
+) => {
+ const counter = pointer.replace(/\/children\/\d+$/u, "");
+ const holder = resolvePointer(ends.final, counter);
+ const scope =
+ key === undefined
+ ? counter !== pointer &&
+ isXmlElement(holder) &&
+ /count/iu.test(holder.name.split(":").at(-1) ?? holder.name)
+ ? counter.replace(/\/children\/[^/]+$/u, "")
+ : undefined
+ : /count$/iu.test(key.split(":").at(-1) ?? key)
+ ? pointer.split("/").slice(0, -2).join("/")
+ : undefined;
+ const [before, after] = [ends.baseline, ends.final].map((view) =>
+ resolvePointer(view, pointer),
+ );
+ const number = (value: JsonValue | undefined) =>
+ typeof value === "string" && /^\d+$/u.test(value)
+ ? Number(value)
+ : undefined;
+ const [seedCount, endCount] = [number(before), number(after)];
+ if (
+ scope === undefined ||
+ endCount === undefined ||
+ seedCount === endCount ||
+ (seedCount === undefined && before !== undefined)
+ )
+ return false;
+ const tallyAt = (view: JsonValue | null, deep: boolean) => {
+ const tally = new Map();
+ const own = key === undefined ? resolvePointer(view, counter) : undefined;
+ const owner = resolvePointer(view, scope);
+ const pending = isXmlElement(owner) ? [...owner.children] : [];
+ for (let node = pending.pop(); node !== undefined; node = pending.pop())
+ if (isXmlElement(node) && node !== own) {
+ tally.set(node.name, (tally.get(node.name) ?? 0) + 1);
+ if (deep) pending.push(...node.children);
+ }
+ return tally;
+ };
+ if (seedCount === undefined)
+ return [...tallyAt(ends.final, false).values()].some(
+ (count) => count > 1 && count === endCount,
+ );
+ const seed = tallyAt(ends.baseline, true);
+ const end = tallyAt(ends.final, true);
+ return [...new Set([...seed.keys(), ...end.keys()])].some(
+ (tag) =>
+ (seed.get(tag) ?? 0) === seedCount && (end.get(tag) ?? 0) === endCount,
+ );
+};
diff --git a/src/domain/desktop/appTaskDraftMirrors.ts b/src/domain/desktop/appTaskDraftMirrors.ts
new file mode 100644
index 0000000000..503b135e22
--- /dev/null
+++ b/src/domain/desktop/appTaskDraftMirrors.ts
@@ -0,0 +1,156 @@
+import type { JsonValue } from "../jsonValue.js";
+import { isNameLikeKey } from "./attributeRank.js";
+import type { DesktopStateDifference } from "./stateChanges.js";
+import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js";
+import { selectorOf } from "./viewSelector.js";
+
+/** A file's views at its two ends. */
+interface Ends {
+ readonly baseline: JsonValue | null;
+ readonly final: JsonValue | null;
+}
+
+/** The value of the name-like attribute a pointer's last step names its element by, as `label` for `[name=elementInformation][@name=label]`. */
+const fieldKeyOf = (element: string) =>
+ selectorOf(
+ (element.split("/").at(-1) ?? "")
+ .replaceAll("~1", "/")
+ .replaceAll("~0", "~"),
+ )?.find(({ prefix, key }) => prefix === "@" && isNameLikeKey(key))?.value;
+
+/**
+ * Text an app shows of another field it keeps, which changed alike: a text
+ * node whose element sits beside a field naming the key of another changed
+ * text with the same value at both ends, as QElectroTech's
+ * `` shows a component's label in its `` beside
+ * `label` while `elementInformation[@name=label]`
+ * holds it.
+ */
+export const mirrorsOf = (
+ change: Pick,
+ ends: Ends,
+) => {
+ const texts = change.changed.flatMap((pointer) => {
+ const [before, after] = [ends.baseline, ends.final].map((view) =>
+ resolvePointer(view, pointer),
+ );
+ const element = pointer.replace(/\/children\/\d+$/u, "");
+ return element !== pointer &&
+ typeof before === "string" &&
+ typeof after === "string"
+ ? [{ pointer, element, before, after }]
+ : [];
+ });
+ // The keys of the changed texts' elements, by what each was and became.
+ const keys = new Map();
+ for (const { pointer, element, before, after } of texts) {
+ const key = fieldKeyOf(element);
+ const alike = JSON.stringify([before, after]);
+ if (key === undefined) continue;
+ const group = keys.get(alike) ?? [];
+ group.push({ pointer, key });
+ keys.set(alike, group);
+ }
+ const mirrored = new Set(
+ texts.flatMap((mirror) => {
+ const sources = keys.get(JSON.stringify([mirror.before, mirror.after]));
+ const record = sources?.some(({ pointer }) => pointer !== mirror.pointer)
+ ? resolvePointer(
+ ends.final,
+ mirror.element.replace(/\/children\/[^/]+$/u, ""),
+ )
+ : undefined;
+ const shown = new Set(
+ (isXmlElement(record) ? record.children : []).map((field) =>
+ isXmlElement(field) ? elementText(field).trim() : "",
+ ),
+ );
+ return (sources ?? []).some(
+ ({ pointer, key }) =>
+ pointer !== mirror.pointer &&
+ shown.has(key) &&
+ sameRecord(mirror.element, pointer),
+ )
+ ? [mirror.pointer]
+ : [];
+ }),
+ );
+ return (pointer: string) => mirrored.has(pointer);
+};
+
+/** An attribute that identifies a record, not a `name` two independent lists can share. */
+const isRecordId = (key: string) =>
+ /^(?:[^:]*:)?(?:id|uid|uuid|guid|xid)$/iu.test(key);
+
+/** The tag a selector step names, as `element` for `[name=element][@uuid={1}]`. */
+const stepTag = (token: string) => /\[name=([^\][]+)/u.exec(token)?.[1];
+
+/**
+ * Whether two pointers stay in one record. They may diverge into different
+ * parts of that record. Two different members of the same list, such as two
+ * components, are not one record.
+ */
+const sameRecord = (left: string, right: string) => {
+ const first = left.split("/");
+ const second = right.split("/");
+ let index = 0;
+ while (
+ index < first.length &&
+ index < second.length &&
+ first[index] === second[index]
+ )
+ index += 1;
+ if (index >= first.length || index >= second.length) return true;
+ if (first[index - 1] !== "children") return true;
+ const leftTag = stepTag(first[index] ?? "");
+ const rightTag = stepTag(second[index] ?? "");
+ return (
+ leftTag === undefined || rightTag === undefined || leftTag !== rightTag
+ );
+};
+
+/** Whether an element holds nothing but one identifier, as Parley's lesson membership ``. */
+export const isIdReference = (element: JsonValue | undefined): boolean => {
+ if (!isXmlElement(element)) return false;
+ const [key, ...more] = Object.keys(element.attributes);
+ return (
+ key !== undefined &&
+ more.length === 0 &&
+ isRecordId(key) &&
+ element.children.every(
+ (child) => typeof child === "string" && child.trim() === "",
+ )
+ );
+};
+
+/**
+ * Elements a change added that hold nothing but an identifier of a record it
+ * added elsewhere in the file (`isIdReference`), as Parley lists a new entry
+ * in its lesson by ``: a mirror of that record, never a check
+ * or a count of its own.
+ */
+export const idReferencesOf = (
+ change: Pick,
+ ends: Ends,
+) => {
+ const added = change.added.flatMap((pointer) => {
+ const element = resolvePointer(ends.final, pointer);
+ return isXmlElement(element) ? [{ pointer, element }] : [];
+ });
+ const mirrors = added.flatMap(({ pointer, element }) => {
+ const [key = ""] = Object.keys(element.attributes);
+ return isIdReference(element) &&
+ added.some(
+ (record) =>
+ record.element !== element &&
+ !isIdReference(record.element) &&
+ record.element.attributes[key] === element.attributes[key],
+ )
+ ? [pointer]
+ : [];
+ });
+ return (pointer: string) =>
+ mirrors.some(
+ (mirror) => pointer === mirror || pointer.startsWith(`${mirror}/`),
+ );
+};
diff --git a/src/domain/desktop/appTaskDraftModel.test.ts b/src/domain/desktop/appTaskDraftModel.test.ts
new file mode 100644
index 0000000000..40efc3b3d9
--- /dev/null
+++ b/src/domain/desktop/appTaskDraftModel.test.ts
@@ -0,0 +1,186 @@
+import { describe, expect, it } from "vitest";
+
+import type { JsonValue } from "../jsonValue.js";
+import { evaluateTaskChecks } from "./appTaskChecks.js";
+import {
+ draftOf,
+ passing,
+ pointerOf,
+ statePath,
+ xml,
+ xmlDraft,
+} from "./appTaskDraft.fixture.js";
+
+const FILE = "Documents/doc.xml";
+
+const STRING = "uMUM2QvoIZjRj";
+const INT = "uC0y4gxFPBxQT";
+const attribute = (id: string, name: string, type: string) =>
+ ``;
+/** A model: data types, two classes of attributes, the class diagram's boxes, the id counter, and the tree view mirroring the model by id. */
+const model = (added: string, height: string, counter: string) =>
+ `${attribute("unJOJYqtTrFmw", "title", STRING)}${attribute("ufuOeMM0RJQRU", "isbn", STRING)}${attribute("u0xTeAOXDVGS9", "name", STRING)}${attribute("uJiGZsdCWAH42", "memberId", INT)}${added}${added === "" ? "" : ''}`;
+const SEED = model("", "52", "uAmSta7YLvI1L");
+const email = (type: string) =>
+ model(attribute("ubcgF2AWfBmHh", "email", type), "67", "ubcgF2AWfBmHh");
+const MEMBER =
+ "/root/children/[name=uml:Model]/children/[name=packagedElement]/children/[name=packagedElement][@name=Member]";
+
+describe("draftTaskFiles for a UML model, as Umbrello saves one", () => {
+ it("checks the new attribute's type, never its class's visibility values as runs of text, so an attribute of another type fails", () => {
+ const file = xmlDraft(SEED, email(STRING));
+
+ expect(file.suggested).toEqual([
+ {
+ kind: "value_equals",
+ file: FILE,
+ pointer: `${MEMBER}/children/[name=ownedAttribute][@name=email]/attributes/type`,
+ value: STRING,
+ },
+ {
+ kind: "member_count",
+ file: FILE,
+ pointer: `${MEMBER}/children`,
+ matching: "[name=ownedAttribute][@xmi:type=uml:Property]",
+ equals: 3,
+ },
+ ]);
+ expect(passing(FILE, xml(SEED), file.suggested)).toEqual([false, false]);
+ expect(passing(FILE, xml(email(STRING)), file.suggested)).toEqual([
+ true,
+ true,
+ ]);
+ expect(passing(FILE, xml(email(INT)), file.suggested)).toEqual([
+ false,
+ true,
+ ]);
+ });
+
+ it("notes the id counter and a value equal to the new attribute's id as volatile, the tree view as view state, and the class box's growth as layout", () => {
+ const file = xmlDraft(SEED, email(STRING));
+
+ const notes = Object.fromEntries(
+ file.values.map(({ pointer, note }) => [
+ pointer.replace(/^.*\/children\//u, ""),
+ note ?? null,
+ ]),
+ );
+ expect(notes).toEqual({
+ "[name=ownedAttribute][@name=email]": null,
+ "[name=docsettings]/attributes/uniqueid": "volatile",
+ "[name=codegeneration]/attributes/lastobject": "volatile",
+ "[name=listitem][@id=ubcgF2AWfBmHh]": "view_state",
+ "[name=classwidget][@x=574]/attributes/height": "layout",
+ });
+ // No selector names an element by an id of random case.
+ expect(file.values.map(({ pointer }) => pointer).join()).not.toMatch(
+ /xmi\.id=/u,
+ );
+ });
+
+ it("names the data type the new attribute's type refers to by its xmi:id, though the tree view mirrors that id", () => {
+ const file = xmlDraft(SEED, email(STRING));
+
+ expect(file.values[0]?.refers_to).toEqual([
+ {
+ attribute: "type",
+ value: STRING,
+ element: ``,
+ },
+ ]);
+ expect(file.guards.map(pointerOf)).toEqual([
+ `${MEMBER}/children/[name=ownedAttribute][@name=name]/attributes/type`,
+ `${MEMBER}/children/[name=ownedAttribute][@name=memberId]/attributes/type`,
+ ]);
+ });
+});
+
+describe("draftTaskFiles for a zipped UML model", () => {
+ it("checks the new attribute's type in a zipped model, whose data types only the archive's entries hold", () => {
+ const ZIP = "Documents/library.zip";
+ const zipped = (text: string): JsonValue => ({
+ entries: [
+ {
+ name: "library.xmi",
+ size: text.length,
+ sha256: "a".repeat(64),
+ format: "xml",
+ view: xml(text),
+ view_error: null,
+ },
+ ],
+ entry_count: 1,
+ truncated: false,
+ });
+ const [file] = draftOf({
+ [ZIP]: statePath("document", "zip", zipped(SEED), zipped(email(STRING))),
+ });
+ const passes = (text: string) =>
+ evaluateTaskChecks(
+ (file?.suggested ?? []).map((check, index) => ({
+ ...check,
+ id: `c${String(index)}`,
+ })),
+ new Map([[ZIP, { format: "zip", view: zipped(text) }]]),
+ ).map(({ passed }) => passed);
+
+ const member = `/entries/[name=library.xmi]/view${MEMBER}`;
+ expect(file?.suggested).toEqual([
+ {
+ kind: "value_equals",
+ file: ZIP,
+ pointer: `${member}/children/[name=ownedAttribute][@name=email]/attributes/type`,
+ value: STRING,
+ },
+ {
+ kind: "member_count",
+ file: ZIP,
+ pointer: `${member}/children`,
+ matching: "[name=ownedAttribute][@xmi:type=uml:Property]",
+ equals: 3,
+ },
+ ]);
+ expect(passes(SEED)).toEqual([false, false]);
+ expect(passes(email(STRING))).toEqual([true, true]);
+ expect(passes(email(INT))).toEqual([false, true]);
+ });
+ it("does not treat XML-shaped JSON in another entry as an XML type reference", () => {
+ const declaration = ``;
+ const zipped = (text: string): JsonValue => ({
+ entries: [
+ {
+ name: "library.xmi",
+ size: text.length,
+ sha256: "a".repeat(64),
+ format: "xml",
+ view: xml(text.replace(declaration, "")),
+ view_error: null,
+ },
+ {
+ name: "other.json",
+ size: 1,
+ sha256: "b".repeat(64),
+ format: "json",
+ view: { name: "type", attributes: { id: STRING }, children: [] },
+ view_error: null,
+ },
+ ],
+ entry_count: 2,
+ truncated: false,
+ });
+ const [file] = draftOf({
+ "Documents/library.zip": statePath(
+ "document",
+ "zip",
+ zipped(SEED),
+ zipped(email(STRING)),
+ ),
+ });
+ expect(
+ file?.suggested.some(
+ (check) =>
+ "pointer" in check && check.pointer.endsWith("/attributes/type"),
+ ),
+ ).toBe(false);
+ });
+});
diff --git a/src/domain/desktop/appTaskDraftNamed.ts b/src/domain/desktop/appTaskDraftNamed.ts
index 196bf34ed2..ed2b334d9e 100644
--- a/src/domain/desktop/appTaskDraftNamed.ts
+++ b/src/domain/desktop/appTaskDraftNamed.ts
@@ -1,11 +1,18 @@
import type { JsonValue } from "../jsonValue.js";
-import type { DesktopTaskCheckInput } from "./appTask.js";
+import { collapseWhitespace, type DesktopTaskCheckInput } from "./appTask.js";
import {
attributeChecks,
fieldChecks,
recordChecks,
type AddedContext,
} from "./appTaskDraftAdded.js";
+import {
+ attributeTextCheck,
+ readsText,
+ textAttributes,
+} from "./appTaskDraftAttributeText.js";
+import { draftCounts } from "./appTaskDraftCounts.js";
+import { nestedRecordChecks } from "./appTaskDraftNested.js";
import { draftGuards } from "./appTaskDraftGuards.js";
import {
attributeKeyOf,
@@ -14,31 +21,29 @@ import {
isStamp,
type NoiseReason,
} from "./appTaskDraftNoise.js";
-import { valueEqualsCheck } from "./appTaskDraftScalars.js";
+import { passesOn, valueEqualsCheck } from "./appTaskDraftScalars.js";
import { isIdentifyingAttribute } from "./attributeRank.js";
import {
wholeTextSuggestions,
type WholeTextSuggestions,
} from "./appTaskDraftText.js";
-import { draftValues, oneEnd, type DraftValue } from "./appTaskDraftValues.js";
+import { draftValues, type DraftValue } from "./appTaskDraftValues.js";
import {
diffNamedMembers,
type DesktopStateDifference,
} from "./stateChanges.js";
+import { pairMembers, pairedAtSeed } from "./memberPairing.js";
import {
escapeToken,
isXmlElement,
locatePointer,
- pairMembers,
resolvePointer,
} from "./viewPointer.js";
import {
- entryWithoutView,
- zipArrayLeavesOut,
- zipEntryAt,
- zipLeavesOutAt,
- zipView,
-} from "./zipView.js";
+ referenceRoots,
+ valueFormat,
+ type CheckTree,
+} from "./appTaskDraftTree.js";
type Suggestion = DesktopTaskCheckInput;
@@ -53,120 +58,6 @@ export const innermost = (pointers: readonly string[]): readonly string[] =>
(pointer) => !pointers.some((other) => other.startsWith(`${pointer}/`)),
);
-/**
- * The entry a pointer into a zip view goes into, when it has a view and both
- * evaluators read it alike: JSON, a key file or XML. A binary or unread
- * entry also holds a `null` view, which is no JSON `null`.
- */
-const readableEntry = (view: JsonValue | null, pointer: string) => {
- const entry = zipEntryAt(view, pointer);
- return typeof entry === "object" &&
- entryWithoutView(entry) === undefined &&
- (entry.format === "json" ||
- entry.format === "keyfile" ||
- entry.format === "xml")
- ? entry
- : undefined;
-};
-
-/**
- * Where checks for a pointer the diff lists start: the view of an entry it
- * names whole, or its root element for XML; the pointer itself inside that;
- * undefined for an entry neither evaluator reads alike, or the entry's fields.
- */
-const checkRoot = (
- view: JsonValue | null,
- pointer: string,
-): string | undefined => {
- const entry = readableEntry(view, pointer);
- const [entryPointer] = /^\/entries\/[^/]+/u.exec(pointer) ?? [];
- if (entry === undefined || entryPointer === undefined) return undefined;
- const inner = pointer.slice(entryPointer.length).split("/").slice(1);
- const root = entry.format === "xml" ? ["view", "root"] : ["view"];
- const prefix = (short: readonly string[], long: readonly string[]) =>
- short.length <= long.length &&
- short.every((part, index) => long[index] === part);
- if (prefix(inner, root)) return `${entryPointer}/${root.join("/")}`;
- return prefix(root, inner) ? pointer : undefined;
-};
-
-/**
- * Where checks into a view can go: an archive's entries, or an XML file's root
- * element. `root` is where checks for a pointer the diff lists start, undefined
- * where the evaluators do not read alike; `xml` whether a pointer goes into an
- * XML view; `leavesOut` whether the view may lack part of what a pointer
- * reaches, and `arrayLeavesOut` members of the array a pointer names;
- * `listed` whether a pointer is a value of the document a values list shows;
- * `empty` the view of a file that holds nothing.
- */
-interface CheckTree {
- readonly root: (
- view: JsonValue | null,
- pointer: string,
- ) => string | undefined;
- readonly xml: (view: JsonValue | null, pointer: string) => boolean;
- readonly leavesOut: (view: JsonValue | null, pointer: string) => boolean;
- readonly arrayLeavesOut: (view: JsonValue | null, array: string) => boolean;
- readonly listed: (view: JsonValue | null, pointer: string) => boolean;
- readonly empty: JsonValue | null;
-}
-
-const valueFormat = (
- tree: CheckTree,
- view: JsonValue | null,
- pointer: string,
-) =>
- tree === ARCHIVE && /^\/entries\/[^/]+$/u.test(pointer)
- ? ("archive" as const)
- : tree.xml(view, pointer)
- ? ("xml" as const)
- : ("json" as const);
-
-const referenceRoots = (
- tree: CheckTree,
- view: JsonValue | null,
-): JsonValue[] => {
- if (tree !== ARCHIVE) return view === null ? [] : [view];
- if (!isRecord(view) || !Array.isArray(view.entries)) return [];
- return view.entries.flatMap((entry) =>
- isRecord(entry) &&
- entry.format === "xml" &&
- entry.view !== undefined &&
- entry.view !== null
- ? [entry.view]
- : [],
- );
-};
-
-export const ARCHIVE: CheckTree = {
- root: checkRoot,
- xml: (view, pointer) => readableEntry(view, pointer)?.format === "xml",
- leavesOut: zipLeavesOutAt,
- arrayLeavesOut: zipArrayLeavesOut,
- // An entry added or removed whole, or a value inside a readable one; not an entry's size or digest.
- listed: (view, pointer) =>
- /^\/entries\/[^/]+$/u.test(pointer) ||
- checkRoot(view, pointer) !== undefined,
- // An archive with no entries, so each entry of one created or deleted is listed by its name.
- empty: zipView([], 0),
-};
-
-/** An XML file's view leaves out what its `truncated` says, anywhere in the tree. */
-const truncatedXml = (view: JsonValue | null) =>
- isRecord(view) && view.truncated === true;
-
-const inXmlRoot = (pointer: string) =>
- pointer === "/root" || pointer.startsWith("/root/");
-
-export const XML_FILE: CheckTree = {
- root: (_view, pointer) => (inXmlRoot(pointer) ? pointer : undefined),
- xml: () => true,
- leavesOut: truncatedXml,
- arrayLeavesOut: truncatedXml,
- listed: (_view, pointer) => inXmlRoot(pointer),
- empty: null,
-};
-
/** Whether a check at a pointer is decided by name: no object or array reached by position, no array searched that the view may cut. */
const byName = (view: JsonValue | null, pointer: string, tree: CheckTree) => {
const located = locatePointer(view, pointer);
@@ -237,7 +128,7 @@ export interface NamedDraft {
/** Noise checks a document keeps when its change is nothing else, so an author still sees one. */
const MAX_FLAGGED = 3;
-/** Guards a file's draft offers. */
+/** Guards a file's draft offers besides its count guard. */
const MAX_GUARDS = 4;
/** The draft's options: its limits, and whether the file is a document, which keeps fewer noise checks than the app's own state. */
@@ -332,6 +223,14 @@ const equals = (
const { file, ends, tree, texts, noise } = collected.context;
const list =
noise(pointer) === undefined ? collected.values : collected.noise.values;
+ const before = resolvePointer(ends.baseline, pointer);
+ // Text whose whitespace alone changed, as a formula moved to a line of its own, holds what the seed did.
+ const relaid =
+ typeof value === "string" &&
+ typeof before === "string" &&
+ collapseWhitespace(value) === collapseWhitespace(before) &&
+ /\/children\/[^/]+$/u.test(pointer) &&
+ tree.xml(ends.final, pointer);
if (
!room(collected, list) ||
texts.covers(pointer) ||
@@ -339,24 +238,21 @@ const equals = (
(isRecord(value) && Object.keys(value).length > 0) ||
isStamp(value) ||
(tree.xml(ends.final, pointer) && inSplitDate(ends.final, pointer)) ||
- isLayoutText(tree, ends.final, pointer, value)
+ isLayoutText(tree, ends.final, pointer, value) ||
+ relaid
)
return;
- if (named(collected, ends.final, pointer))
- take(collected, "values", [
- valueEqualsCheck(
- file,
- pointer,
- value,
- resolvePointer(ends.baseline, pointer),
- ),
- ]);
- else unnamed(collected, ends.final, pointer);
+ const check = valueEqualsCheck(file, pointer, value, before);
+ // A check that already passes at the seed tells nothing.
+ if (!named(collected, ends.final, pointer))
+ unnamed(collected, ends.final, pointer);
+ else if (!passesOn(check, before)) take(collected, "values", [check]);
};
/**
* Checks of an element added whole: a record only position names by its new
- * fields, beside any whole text; a list member by its own attributes; any
+ * fields, beside any whole text; a list member by its own attributes and
+ * those of the records of attributes it nests (`nestedRecordChecks`); any
* other by its presence.
*/
const elementChecks = (
@@ -374,6 +270,8 @@ const elementChecks = (
named: (view: JsonValue | null, at: string) =>
named(collected, view, at) && !tree.leavesOut(view, at),
chars: collected.context.limit.chars,
+ referenceRoots: () => referenceRoots(tree, ends.baseline),
+ noisy: (at: string) => collected.context.noise(at) !== undefined,
long: collected.fieldLong,
};
if (!named(collected, ends.final, `${pointer}/name`)) {
@@ -390,6 +288,17 @@ const elementChecks = (
const own = [
...attributeChecks(added, pointer, value),
...(records.length > 0 ? records : fieldChecks(added, pointer, value)),
+ ...nestedRecordChecks(
+ {
+ file,
+ final: ends.final,
+ named: (at) => added.named(ends.final, at),
+ chars: added.chars,
+ long: added.long,
+ },
+ pointer,
+ value,
+ ),
];
take(
collected,
@@ -461,22 +370,25 @@ const removedCheck = (collected: Collected, pointer: string) => {
else unnamed(collected, ends.baseline, pointer);
};
+/** The element a member pointer names at the seed: there, or, for one keyed by its new name as `renameKeys` keys it, the member it pairs with. */
+const atSeed = (ends: AddedContext["ends"], owner: string) =>
+ resolvePointer(ends.baseline, owner) ?? pairedAtSeed(ends, owner);
+
/**
* A check that the identifier an element was renamed from names nothing at
- * the end, as a layer renamed from `Untitled` that its `src` names: its tag
+ * the end, as a layer renamed from `Layer` that its `src` names: its tag
* and old value, where that named the element alone at the seed. An element
* its tag alone names gets none; the check of its identifier says as much.
*/
const renamedFrom = (collected: Collected, pointer: string) => {
const { file, ends, tree } = collected.context;
- const key = attributeKeyOf(pointer);
+ const key = attributeKeyOf(pointer) ?? "";
const owner = pointer.split("/").slice(0, -2).join("/");
const list = /^(?.+\/children)\/[^/]+$/u.exec(owner)?.groups?.list;
- const before = resolvePointer(ends.baseline, owner);
- const old = resolvePointer(ends.baseline, pointer);
+ const before = atSeed(ends, owner);
+ const old = isXmlElement(before) ? before.attributes[key] : undefined;
if (
!tree.xml(ends.baseline, owner) ||
- key === undefined ||
list === undefined ||
!(owner.split("/").at(-1) ?? "").includes("][@") ||
!isIdentifyingAttribute(key) ||
@@ -503,7 +415,7 @@ const renamedFrom = (collected: Collected, pointer: string) => {
const collectChecks = (
context: CollectContext,
change: DesktopStateDifference,
-): Collected => {
+): Collected & { readonly runs: ReturnType } => {
const collected: Collected = {
context,
elements: [],
@@ -513,35 +425,84 @@ const collectChecks = (
fieldLong: [],
};
const { ends, tree } = context;
- for (const pointer of change.added) {
+ const runs = attributeTexts(collected, change);
+ for (const pointer of change.added.filter((at) => !runs(at, ends.final))) {
const root = tree.root(ends.final, pointer);
if (root !== undefined)
addedChecks(collected, root, resolvePointer(ends.final, root));
}
for (const pointer of innermost(change.changed))
- if (tree.root(ends.final, pointer) === pointer) {
+ if (
+ tree.root(ends.final, pointer) === pointer &&
+ !runs(pointer, ends.final)
+ ) {
equals(collected, pointer, resolvePointer(ends.final, pointer));
// An id the app renumbered, or other noise, renamed nothing.
if (context.noise(pointer) === undefined) renamedFrom(collected, pointer);
}
- for (const pointer of change.removed) removedCheck(collected, pointer);
- return collected;
+ for (const pointer of change.removed.filter((at) => !runs(at, ends.baseline)))
+ removedCheck(collected, pointer);
+ return { ...collected, runs };
+};
+
+/**
+ * Take the `attributeTextCheck` of each attribute that may hold runs of
+ * text; whether a change at a pointer is one a check took reads: the text
+ * attribute of an element in it, or a run holding that attribute added or
+ * removed in it, not another of its attributes or elements.
+ */
+const attributeTexts = (
+ collected: Collected,
+ change: DesktopStateDifference,
+) => {
+ const { file, ends, tree, limit } = collected.context;
+ const covered: { element: string; key: string }[] = [];
+ for (const { owner, key } of textAttributes(
+ change,
+ ends,
+ collected.context.noise,
+ )) {
+ const found =
+ tree.root(ends.final, owner) === owner ||
+ tree.root(ends.baseline, owner) === owner
+ ? attributeTextCheck(
+ {
+ file,
+ ends,
+ named: (at) => named(collected, ends.final, at),
+ chars: limit.chars,
+ },
+ owner,
+ key,
+ )
+ : undefined;
+ if (
+ found !== undefined &&
+ !covered.some(({ element }) => element === found.element)
+ ) {
+ covered.push({ element: found.element, key });
+ take(collected, "elements", [found.check]);
+ }
+ }
+ return readsText(covered);
};
/**
* Checks by the selectors a keyed diff names members with, in an archive's
* entries or an XML file: the whole text of each element whose text changed
- * first, then elements added or removed whole, then values changed, added and
- * removed. An element added to a list is checked by its own attributes, a
- * record only position names by its new fields, a decimal by its number. A
- * JSON value or an entry added whole is checked through its members. Nothing
- * goes through an object or array by position. Noise (view state, renumbered
- * ids, volatile values, times, upkeep) leaves the suggestions when anything
+ * first, then text kept in an attribute's runs (`attributeTextCheck`), then
+ * elements added or removed whole, then values changed, added and removed,
+ * then counts of lists whose members were added or removed (`draftCounts`).
+ * An element added to a list is checked by its own attributes, a record only
+ * position names by its new fields, a decimal by its number. A JSON value or
+ * an entry added whole is checked through its members. Nothing goes through
+ * an object or array by position. Noise (view state, renumbered ids,
+ * volatile values, times, upkeep, layout) leaves the suggestions when anything
* else changed, a change only position reaches included; when noise is all
* there is, it stays, flagged: up to three in a document, as many as fit in
* the app's own state, where a view setting may be the task. Beside them: the
* values the change added, changed and removed, and guards that the rest
- * stays as it was.
+ * stays as it was, a count of the list an edit went through among them.
*/
export const namedDraft = (
file: string,
@@ -566,10 +527,23 @@ export const namedDraft = (
change,
);
const noisy = (check: Suggestion) => isNoisy(noise, check);
+ const counts = draftCounts(
+ {
+ file,
+ ends,
+ whole: (view, list) =>
+ named(view, list) && !tree.arrayLeavesOut(view, list),
+ noisy: (pointer) => noise(pointer) !== undefined,
+ covered: texts.covers,
+ coveredRun: checks.runs,
+ },
+ change,
+ );
const clean = [
...texts.suggestions.filter((check) => !noisy(check)),
...checks.elements,
...checks.values,
+ ...counts.checks,
];
const flaggable = [
...texts.suggestions.filter(noisy),
@@ -592,57 +566,54 @@ export const namedDraft = (
format: (view, pointer) => valueFormat(tree, view, pointer),
referenceRoots: (view) => referenceRoots(tree, view),
});
+ // The count guard sits outside the cap, so line and sibling guards never squeeze it out.
const guards = edited
- ? draftGuards(
- {
- file,
- ends,
- named,
- noisy: (pointer) => noise(pointer) !== undefined,
- chars: options.limit.chars,
- },
- suggested,
- ).slice(0, MAX_GUARDS)
+ ? [
+ ...texts.guards,
+ ...draftGuards(
+ {
+ file,
+ ends,
+ named,
+ noisy: (pointer) => noise(pointer) !== undefined,
+ chars: options.limit.chars,
+ },
+ suggested,
+ ),
+ ]
+ .slice(0, MAX_GUARDS)
+ .concat(counts.guards)
: [];
return {
suggested,
values,
valuesCut: cut,
guards,
- facts: {
- positional: checks.positional.count,
- positionalTags: [...checks.positional.tags],
- long: [...new Set([...texts.long, ...checks.fieldLong])],
- changes: listed.length,
- noise: listed.filter((pointer) => noise(pointer) !== undefined).length,
- flagged: edited
- ? []
- : suggested.flatMap((check) =>
- "pointer" in check ? [check.pointer] : [],
- ),
- },
+ facts: factsOf(checks, texts, {
+ listed,
+ noise,
+ flagged: edited ? [] : suggested,
+ }),
};
};
-/**
- * The values of an archive or XML file created or deleted: what its one end
- * holds against `tree.empty`, each entry, or the root element, whole once, as
- * one added to a file that changed is listed, within the same limits.
- */
-export const presenceValues = (
- ends: AddedContext["ends"],
- tree: CheckTree,
- created: boolean,
-): { readonly values: DraftValue[]; readonly cut: boolean } => {
- const from = created
- ? { baseline: tree.empty, final: ends.final }
- : { baseline: ends.baseline, final: tree.empty };
- const change = oneEnd(diffNamedMembers(from.baseline, from.final), created);
- return draftValues(change, {
- ends: from,
- listed: tree.listed,
- noise: changeNoise(change, from, tree.xml),
- format: (view, pointer) => valueFormat(tree, view, pointer),
- referenceRoots: (view) => referenceRoots(tree, view),
- });
-};
+/** The facts a file's notes rest on: what position alone reached, text past the limit, how many changes are noise, and the noise checks kept. */
+const factsOf = (
+ checks: Collected,
+ texts: WholeTextSuggestions,
+ change: {
+ readonly listed: readonly string[];
+ readonly noise: (pointer: string) => NoiseReason | undefined;
+ readonly flagged: readonly Suggestion[];
+ },
+): NamedFacts => ({
+ positional: checks.positional.count,
+ positionalTags: [...checks.positional.tags],
+ long: [...new Set([...texts.long, ...checks.fieldLong])],
+ changes: change.listed.length,
+ noise: change.listed.filter((pointer) => change.noise(pointer) !== undefined)
+ .length,
+ flagged: change.flagged.flatMap((check) =>
+ "pointer" in check ? [check.pointer] : [],
+ ),
+});
diff --git a/src/domain/desktop/appTaskDraftNested.ts b/src/domain/desktop/appTaskDraftNested.ts
new file mode 100644
index 0000000000..c4ef83499c
--- /dev/null
+++ b/src/domain/desktop/appTaskDraftNested.ts
@@ -0,0 +1,236 @@
+import type { JsonValue } from "../jsonValue.js";
+import type { DesktopTaskCheckInput } from "./appTask.js";
+import {
+ isBookkeepingName,
+ isStamp,
+ isVolatileValue,
+} from "./appTaskDraftNoise.js";
+import { valueEqualsCheck } from "./appTaskDraftScalars.js";
+import {
+ isGeneratedId,
+ isIdentifyingAttribute,
+ isPlaceholder,
+ isViewStateName,
+ rankAttributes,
+} from "./attributeRank.js";
+import { escapeToken, isXmlElement, resolvePointer } from "./viewPointer.js";
+import { spellCondition } from "./viewSelector.js";
+import { checkFits, type AddedContext } from "./appTaskDraftAdded.js";
+
+type Element = Record & {
+ name: string;
+ attributes: Record;
+ children: JsonValue[];
+};
+
+/** Checks of the records nested in one element added whole, and of one record's attributes. */
+const MAX_NESTED_CHECKS = 6;
+const MAX_PER_RECORD = 3;
+
+/** Whether an element is a record of attributes alone, as KMyMoney's ``: no element or text of its own. */
+const attributesOnly = (item: JsonValue): item is Element =>
+ isXmlElement(item) &&
+ Object.keys(item.attributes).length > 0 &&
+ item.children.every(
+ (child) => typeof child === "string" && child.trim() === "",
+ );
+
+/** The records of attributes alone among an element's children, when they are all one tag; empty otherwise. */
+const recordsIn = (element: JsonValue | undefined): Element[] => {
+ const items = isXmlElement(element)
+ ? element.children.filter(isXmlElement)
+ : [];
+ const records = items.filter(attributesOnly);
+ return records.length === items.length &&
+ records.every((record) => record.name === records[0]?.name)
+ ? records
+ : [];
+};
+
+/** A list of records of attributes alone in an element: the steps from it to the element holding them, none for its own or a child's tag for a child's, as a transaction's ``, and the records. */
+interface RecordList {
+ readonly steps: readonly string[];
+ readonly records: readonly Element[];
+}
+
+/**
+ * The lists of records of attributes alone an element holds: its own
+ * children, or those of a child holding nothing else that its tag names
+ * alone, as a KMyMoney `` its `` of ``.
+ */
+export const recordListsOf = (element: JsonValue): RecordList[] => {
+ const own = recordsIn(element);
+ if (own.length > 0) return [{ steps: [], records: own }];
+ const children = isXmlElement(element)
+ ? element.children.filter(isXmlElement)
+ : [];
+ const tags = new Map();
+ for (const { name } of children) tags.set(name, (tags.get(name) ?? 0) + 1);
+ return children.flatMap((child) => {
+ const records = recordsIn(child);
+ return records.length > 0 &&
+ tags.get(child.name) === 1 &&
+ !/[=[\]]/u.test(child.name)
+ ? [{ steps: ["children", escapeToken(`[name=${child.name}]`)], records }]
+ : [];
+ });
+};
+
+/** The records at a list's steps in each like sibling of an element, as the splits of the transactions already in the book. */
+const cousinsOf = (
+ likes: readonly JsonValue[],
+ steps: readonly string[],
+ tag: string,
+): Element[] =>
+ likes.flatMap((like) =>
+ recordsIn(
+ steps.length === 0 ? like : resolvePointer(like, `/${steps.join("/")}`),
+ ).filter((record) => record.name === tag),
+ );
+
+/** Whether a value a selector can carry and that names something across saves. */
+const selectable = (key: string, value: JsonValue | undefined) =>
+ typeof value === "string" &&
+ value !== "" &&
+ !/[=[\]]/u.test(key) &&
+ !value.includes("][") &&
+ !isStamp(value) &&
+ !isPlaceholder(value) &&
+ !isVolatileValue(value, isBookkeepingName(key));
+
+/**
+ * The selector naming a nested record alone among its list's: an
+ * identifier first, unless it is one the app generates or numbers anew in
+ * each record holding such a list, as KMyMoney's `S0001` in every
+ * transaction; then the best ranked attribute short of a coordinate or an
+ * amount, as a split's `[@account=A000001]`.
+ */
+const recordKey = (
+ record: Element,
+ records: readonly Element[],
+ cousins: readonly Element[],
+ chars: number,
+) => {
+ const values = Object.entries(record.attributes).flatMap(([key, value]) =>
+ selectable(key, value) && typeof value === "string" && value.length <= chars
+ ? [{ key, value }]
+ : [],
+ );
+ const repeated = (key: string, value: string) =>
+ cousins.some((cousin) => cousin.attributes[key] === value);
+ const [best] = [
+ ...values.filter(
+ ({ key, value }) =>
+ isIdentifyingAttribute(key) &&
+ !isGeneratedId(value) &&
+ !repeated(key, value),
+ ),
+ ...rankAttributes(
+ values.filter(({ key }) => !isIdentifyingAttribute(key)),
+ ).filter(({ tier }) => tier < 3),
+ ].filter(
+ ({ key, value }) =>
+ records.filter((other) => other.attributes[key] === value).length === 1,
+ );
+ return best === undefined
+ ? undefined
+ : {
+ key: best.key,
+ segment: escapeToken(
+ `[name=${record.name}]${spellCondition({ prefix: "@", ...best })}`,
+ ),
+ };
+};
+
+/**
+ * The keys of a record checked one by one: those whose values vary among
+ * the like records, its list's and those under the element's like siblings,
+ * as a split's `value` and `payee`, but not its `memo` or `price` that every
+ * split holds alike, nor one each like record holds equal to an earlier one,
+ * as `shares` repeats `value`; never its selector's, an identifier, a time,
+ * a volatile value, view state or an id the app renumbers.
+ */
+const checkedKeys = (
+ record: Element,
+ likes: readonly Element[],
+ skip: string,
+) => {
+ const keys: string[] = [];
+ for (const [key, value] of Object.entries(record.attributes)) {
+ const all = likes.map((like) => like.attributes[key]);
+ if (
+ key === skip ||
+ typeof value !== "string" ||
+ isIdentifyingAttribute(key) ||
+ isPlaceholder(value) ||
+ isStamp(value) ||
+ isVolatileValue(value, isBookkeepingName(key)) ||
+ isViewStateName(key) ||
+ new Set(all).size < 2 ||
+ keys.some((earlier) =>
+ likes.every(
+ (like) => like.attributes[earlier] === like.attributes[key],
+ ),
+ )
+ )
+ continue;
+ keys.push(key);
+ }
+ return keys;
+};
+
+/** What checks of nested records read: the file, the end, and whether a pointer names its value without a step by position. */
+export interface NestedContext extends Pick {
+ readonly file: string;
+ readonly final: JsonValue | null;
+ readonly named: (pointer: string) => boolean;
+}
+
+/**
+ * A `value_equals` per attribute of each record of attributes alone nested
+ * in an element added whole to a list, as each split of a KMyMoney
+ * transaction holds its amount, account and payee, which the element's own
+ * attributes and text leave out: the record named by `recordKey`, its
+ * attributes by `checkedKeys`, at most six in all. Each fails at the seed,
+ * which lacks the element. Empty for an element with no like sibling, as no
+ * record then tells what varies.
+ */
+export const nestedRecordChecks = (
+ context: NestedContext,
+ pointer: string,
+ element: JsonValue,
+): DesktopTaskCheckInput[] => {
+ if (!isXmlElement(element)) return [];
+ const list = resolvePointer(context.final, pointer.replace(/\/[^/]+$/u, ""));
+ const likes = (Array.isArray(list) ? list : []).filter(
+ (item) =>
+ item !== element && isXmlElement(item) && item.name === element.name,
+ );
+ if (likes.length === 0) return [];
+ return recordListsOf(element)
+ .flatMap(({ steps, records }) => {
+ const cousins = cousinsOf(likes, steps, records[0]?.name ?? "");
+ return records.flatMap((record) => {
+ const key = recordKey(record, records, cousins, context.chars);
+ if (key === undefined) return [];
+ const at = [pointer, ...steps, "children", key.segment].join("/");
+ return checkedKeys(record, [...records, ...cousins], key.key)
+ .filter((name) => {
+ const value = record.attributes[name];
+ return (
+ typeof value === "string" &&
+ checkFits(context, `${at}/attributes/${escapeToken(name)}`, value)
+ );
+ })
+ .slice(0, MAX_PER_RECORD)
+ .flatMap((name) => {
+ const field = `${at}/attributes/${escapeToken(name)}`;
+ const value = record.attributes[name];
+ return context.named(field) && typeof value === "string"
+ ? [valueEqualsCheck(context.file, field, value)]
+ : [];
+ });
+ });
+ })
+ .slice(0, MAX_NESTED_CHECKS);
+};
diff --git a/src/domain/desktop/appTaskDraftNoise.test.ts b/src/domain/desktop/appTaskDraftNoise.test.ts
index 78f34fc695..7b61f252ae 100644
--- a/src/domain/desktop/appTaskDraftNoise.test.ts
+++ b/src/domain/desktop/appTaskDraftNoise.test.ts
@@ -3,12 +3,14 @@ import { describe, expect, it } from "vitest";
import type { JsonValue } from "../jsonValue.js";
import {
draftOf,
+ passing,
pointerOf,
statePath,
xml,
xmlDraft,
} from "./appTaskDraft.fixture.js";
+const FILE = "Documents/doc.xml";
const CELLS =
"/root/children/[name=Sheet]/children/[name=Cells]/children/[name=Cell]";
const SELECTIONS = "/root/children/[name=Sheet]/children/[name=Selections]";
@@ -43,10 +45,15 @@ const EDITED = {
} as const;
describe("draftTaskFiles noise", () => {
- it("leaves view state, renumbered ids, volatile values and times out of a document's suggestions when an edit is there, noting each in values", () => {
+ it("leaves view state, volatile values and times out while retaining unproven ids", () => {
const file = xmlDraft(workbook(SEED), workbook(EDITED));
- expect(file.suggested.map(pointerOf)).toEqual([`${CELLS}[@Col=3][@Row=2]`]);
+ expect(file.suggested.map(pointerOf)).toEqual(
+ expect.arrayContaining([
+ `${CELLS}[@Col=3][@Row=2]`,
+ `${CELLS}[@Col=3][@Row=5]/attributes/ExprID`,
+ ]),
+ );
const noted = (note: string) =>
file.values
.filter((value) => value.note === note)
@@ -60,15 +67,8 @@ describe("draftTaskFiles noise", () => {
`${SELECTIONS}/children/[name=Selection]/attributes/startCol`,
].toSorted(),
);
- // The emptied formula text goes with the renumbered id of its cell.
- expect(noted("renumbered")).toEqual(
- [
- `${CELLS}[@Col=3][@Row=5]/attributes/ExprID`,
- `${CELLS}[@Col=3][@Row=5]/children/0`,
- `${CELLS}[@Col=4][@Row=5]/attributes/ExprID`,
- `${CELLS}[@Col=4][@Row=5]/children/0`,
- ].toSorted(),
- );
+ // The attribute name alone does not prove its value is generated.
+ expect(noted("renumbered")).toEqual([]);
expect(noted("volatile")).toEqual([
"/root/children/[name=meta]/attributes/saved",
"/root/children/[name=meta]/attributes/session",
@@ -81,6 +81,28 @@ describe("draftTaskFiles noise", () => {
expect(file.notes).toEqual([]);
});
+ it("keeps a changed relationship id alongside another edit", () => {
+ const expense = (account: string, description: string) =>
+ ``;
+ const file = xmlDraft(expense("A1", "old"), expense("A2", "new"));
+ const account = file.values.find(({ pointer }) =>
+ pointer.endsWith("/attributes/accountId"),
+ );
+ expect(account?.note).toBeUndefined();
+ expect(file.suggested).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ kind: "value_equals",
+ pointer: expect.stringContaining("/attributes/accountId"),
+ value: "A2",
+ }),
+ ]),
+ );
+ expect(passing(FILE, xml(expense("A1", "new")), file.suggested)).toContain(
+ false,
+ );
+ });
+
it("keeps a formula edit when the cell's ExprID changes too", () => {
const sheet = (march: string, formula: string, id: string) =>
`${march}${formula}`;
@@ -94,12 +116,9 @@ describe("draftTaskFiles noise", () => {
expect(shown).toContain("=sum(C2:C5)");
expect(
- file.values
- .filter(({ note }) => note === "renumbered")
- .map(({ pointer }) => pointer),
- ).toEqual([
- "/root/children/[name=Sheet]/children/[name=Cells]/children/[name=Cell][@Row=5]/attributes/ExprID",
- ]);
+ file.values.find(({ pointer }) => pointer.endsWith("/attributes/ExprID"))
+ ?.note,
+ ).toBeUndefined();
});
it("keeps an edit that follows more noise than a file has room for checks", () => {
@@ -356,6 +375,312 @@ describe("draftTaskFiles upkeep in an archive", () => {
});
});
+describe("draftTaskFiles layout beside a text edit", () => {
+ it("keeps a number an edit changed beside text as content, unless a name on its way says it is layout", () => {
+ const shop = (description: string, amount: string, width: string) =>
+ `${description}Tape`;
+ const order = "/root/children/[name=orders]/children/[name=order][@id=1]";
+
+ const file = xmlDraft(
+ shop("Two boxes", "10", "4"),
+ shop("Twelve boxes", "12", "6"),
+ );
+
+ expect(
+ file.values.map(({ pointer, note }) => [
+ pointer.replace(order, "~"),
+ note ?? null,
+ ]),
+ ).toEqual([
+ ["~/children/[name=description]/children/0", null],
+ ["~/children/[name=amount]/attributes/value", null],
+ ["~/children/[name=box]/attributes/width", "layout"],
+ ]);
+ expect(file.suggested.map(pointerOf)).toContain(
+ `${order}/children/[name=amount]/attributes/value`,
+ );
+ });
+});
+
+describe("draftTaskFiles for a presentation whose save after a reload rewrote its styles", () => {
+ const entry = (name: string, view: string) => ({
+ name,
+ size: view.length,
+ sha256: view,
+ format: "xml",
+ view: xml(view),
+ view_error: null,
+ });
+ const NS =
+ 'xmlns:office="o" xmlns:draw="d" xmlns:presentation="p" xmlns:text="t" xmlns:style="s"';
+ /** Slides as Impress keeps them: frames by class, each with an automatic style it shares and renumbers. */
+ const slides = (title: string) =>
+ `${[
+ "Welcome",
+ "Schedule",
+ ]
+ .map(
+ (page) =>
+ `${page === "Schedule" ? title : "Welcome"}Tomatoes`,
+ )
+ .join("")}`;
+ /** Master pages' fields, wrapped or not in an automatic text style, beside many such pages. */
+ const masters = (wrapped: boolean, spacing: string) =>
+ `${Array.from(
+ { length: 12 },
+ (_, index) =>
+ `${wrapped ? '' : ""}`,
+ ).join("")}`;
+ const deck = (title: string, wrapped: boolean) => ({
+ entries: [
+ entry("content.xml", slides(title)),
+ entry("styles.xml", masters(wrapped, wrapped ? "0.9cm" : "0.6cm")),
+ ],
+ entry_count: 2,
+ truncated: false,
+ });
+ const SCHEDULE =
+ "/entries/[name=content.xml]/view/root/children/[name=office:body]/children/[name=office:presentation]/children/[name=draw:page][@draw:name=Schedule]/children/[name=draw:frame]";
+
+ it("names a slide's frames by their class, not the automatic style the app renumbers, suggests the title alone, and lists it before the rewrite", () => {
+ const [file] = draftOf({
+ "Documents/garden.odp": statePath(
+ "document",
+ "zip",
+ deck("Planting Schedule", false),
+ deck("Spring Planting Calendar", true),
+ ),
+ });
+
+ expect(file?.suggested).toEqual([
+ {
+ kind: "text_equals",
+ file: "Documents/garden.odp",
+ pointer: `${SCHEDULE}[@presentation:class=title]`,
+ value: "Spring Planting Calendar",
+ },
+ ]);
+ const [first, ...rest] = file?.values ?? [];
+ expect(first).toMatchObject({
+ pointer: `${SCHEDULE}[@presentation:class=title]/children/[name=draw:text-box]/children/[name=text:p]/children/0`,
+ after: "Spring Planting Calendar",
+ });
+ expect(rest.length).toBeGreaterThan(10);
+ expect(rest.every(({ note }) => note === "upkeep")).toBe(true);
+ });
+});
+
+describe("draftTaskFiles for an animation whose app keeps what is current", () => {
+ /** A Pencil2D project: the frame, colour, layer and view the app has current, and its layers. */
+ const project = (current: string, bitmap: string) =>
+ ``;
+
+ it("notes the layer the app has current as view state, never a check, beside the layer the user renamed", () => {
+ const file = xmlDraft(project("3", "Bitmap Layer"), project("2", "Ball"));
+
+ expect(file.suggested.map(pointerOf)).toEqual([
+ "/root/children/[name=object]/children/[name=layer][@id=3]/attributes/name",
+ "/root/children/[name=object]/children/[name=layer][@name=Bitmap Layer]",
+ ]);
+ expect(
+ file.values.map(({ pointer, note }) => [pointer, note ?? null]),
+ ).toEqual([
+ [
+ "/root/children/[name=object]/children/[name=layer][@id=3]/attributes/name",
+ null,
+ ],
+ [
+ "/root/children/[name=projectdata]/children/[name=currentLayer]/attributes/value",
+ "view_state",
+ ],
+ ]);
+ });
+});
+
+describe("draftTaskFiles for a mind map, as VYM saves one", () => {
+ const branch = (uuid: string, heading: string) =>
+ `${heading}`;
+ /** A map: its branch count, branches each with a GUID VYM makes, a view setting, and the saved selection. */
+ const map = (count: string, added: string, selected: string) =>
+ `Team Offsite${branch("d1e07ab2-1ecd-4c39-aa93-e7b70576a029", "Venue")}Agenda${branch("d47c1733-7117-42b7-b45b-9ac244bc3d67", "Kickoff talk")}${branch("8e689d15-a04a-4c60-81ad-6317a3b5874f", "Strategy workshop")}${added}${added === "" ? "" : 'false'}`;
+ const SEED = map("4", "", "mc:0,bo:2");
+ const END = map(
+ "5",
+ branch("18eadb5d-04fc-4448-a0f9-1f9a3dedaff7", "Sunset boat tour"),
+ "mc:0,bo:1",
+ );
+ const AGENDA =
+ "/root/children/[name=mapcenter]/children/[name=branch][@uuid={4cdcd61f-90fb-4965-91a1-849509829d26}]/children";
+
+ it("names the new branch by its heading, not the GUID VYM made for it, and checks that heading and the branches' count", () => {
+ const file = xmlDraft(SEED, END);
+
+ expect(file.suggested).toEqual([
+ {
+ kind: "text_equals",
+ file: "Documents/doc.xml",
+ pointer: `${AGENDA}/[name=branch][>heading=Sunset boat tour]/children/[name=heading]`,
+ value: "Sunset boat tour",
+ },
+ {
+ kind: "member_count",
+ file: "Documents/doc.xml",
+ pointer: AGENDA,
+ matching: "[name=branch]",
+ equals: 3,
+ },
+ ]);
+ });
+
+ it("notes the saved selection and a tree editor setting as view state and the branch count as upkeep, the new heading first", () => {
+ const file = xmlDraft(SEED, END);
+
+ expect(
+ file.values.map(({ pointer, note }) => [
+ pointer.replace(AGENDA, "~"),
+ note ?? null,
+ ]),
+ ).toEqual([
+ [
+ "~/[name=branch][>heading=Sunset boat tour]/children/[name=heading]",
+ null,
+ ],
+ ["/root/children/[name=setting]", "view_state"],
+ ["/root/attributes/branchCount", "upkeep"],
+ ["/root/children/[name=select]/children/0", "view_state"],
+ ]);
+ });
+});
+
+describe("draftTaskFiles counts an app keeps of its records", () => {
+ it("notes a count in an element's text that equals its records, as GnuCash's count-data, as upkeep, never a check or a guard", () => {
+ const book = (count: string, added: string) =>
+ `2${count}${added}`;
+
+ const file = xmlDraft(
+ book("1", ""),
+ book("2", ''),
+ );
+
+ const count =
+ "/root/children/[name=gnc:count-data][@cd:type=transaction]/children/0";
+ expect(file.values.find(({ pointer }) => pointer === count)).toMatchObject({
+ note: "upkeep",
+ });
+ expect(file.suggested.map(pointerOf)).not.toContain(count);
+ expect(
+ file.guards.map(pointerOf).filter((at) => at?.includes("count-data")),
+ ).toEqual([]);
+ });
+
+ it("checks a count the user entered, which tallies no tag at both ends, though it equals how many of a tag its record holds", () => {
+ const stock = (widgets: string) =>
+ `Widget${widgets}Gadget5`;
+
+ const file = xmlDraft(stock("3"), stock("1"));
+
+ const count = file.values.find(({ after }) => after === "1");
+ expect(count).toBeDefined();
+ expect(count?.note).toBeUndefined();
+ expect(passing(FILE, xml(stock("3")), file.suggested)).toEqual([false]);
+ expect(passing(FILE, xml(stock("1")), file.suggested)).toEqual([true]);
+ });
+
+ it("suggests no check of the count a new element keeps of its members, as FreeCAD's ObjectDeps of its Deps", () => {
+ /** Objects as FreeCAD saves them: each one's dependencies, by name, then the objects. */
+ const model = (count: string, deps: string, objects: string) =>
+ `${deps}${objects}`;
+
+ const file = xmlDraft(
+ model("1", "", ""),
+ model(
+ "3",
+ '',
+ '',
+ ),
+ );
+
+ // Count="2" counts the two Deps it holds; Count="0" another object's none, which no tag tallies.
+ expect(file.suggested.map(pointerOf)).not.toContain(
+ "/root/children/[name=Objects]/children/[name=ObjectDeps][@Name=PlateWithHole]/attributes/Count",
+ );
+ });
+});
+
+describe("draftTaskFiles styles a user applies", () => {
+ const NS = 'xmlns:office="o" xmlns:text="t" xmlns:style="s"';
+ /** A Writer document: its automatic styles, and named sections each holding a paragraph in a named style. */
+ const minutes = (quote: string, next: string) =>
+ `MinutesShip it, said Ana.${next}`;
+
+ it("checks a named style the user gave a paragraph beside another edit, never upkeep, as it would an automatic one the app renumbers", () => {
+ const seed = minutes("Standard", "Next meeting Friday.");
+ const end = minutes("Quotations", "Next meeting Monday.");
+ const file = xmlDraft(seed, end);
+
+ const style = file.values.find(({ after }) => after === "Quotations");
+ expect(style).toBeDefined();
+ expect(style?.note).toBeUndefined();
+ expect(
+ file.suggested.some(
+ (check) => "value" in check && check.value === "Quotations",
+ ),
+ ).toBe(true);
+ // A solve that edits the date alone fails.
+ expect(
+ passing(
+ FILE,
+ xml(minutes("Standard", "Next meeting Monday.")),
+ file.suggested,
+ ),
+ ).toContain(false);
+ });
+});
+
+describe("draftTaskFiles for a name shared by two lists", () => {
+ it("checks a category added beside a template of the same name", () => {
+ const file = xmlDraft(
+ 'bag',
+ 'bag',
+ );
+
+ expect(
+ file.values.some(
+ (value) =>
+ value.pointer.includes("category") && value.note === "upkeep",
+ ),
+ ).toBe(false);
+ expect(file.suggested.map(pointerOf).join(" ")).toContain("category");
+ });
+});
+
+describe("draftTaskFiles for a drawing resized beside a rename", () => {
+ const model = (name: string, width: string) =>
+ `${name}`;
+
+ it("checks the width the user changed when the only other edit is a name outside the drawing", () => {
+ const file = xmlDraft(model("Shop", "10"), model("Store", "20"));
+ const width = file.suggested.find(
+ (check) => "pointer" in check && check.pointer.endsWith("width"),
+ );
+
+ expect(file.suggested.map(pointerOf)).toContain(
+ "/root/children/[name=diagram]/children/[name=widget][@width=20]/name",
+ );
+ expect(
+ file.values.some(
+ (value) => value.pointer.includes("width") && value.note === "layout",
+ ),
+ ).toBe(false);
+ expect(passing(FILE, xml(model("Store", "10")), file.suggested)).toContain(
+ false,
+ );
+ expect(
+ passing(FILE, xml(model("Store", "20")), file.suggested).every(Boolean),
+ ).toBe(true);
+ });
+});
+
describe("draftTaskFiles when a sibling lock changed", () => {
const text = (line: string) => ({
lines: [line],
diff --git a/src/domain/desktop/appTaskDraftNoise.ts b/src/domain/desktop/appTaskDraftNoise.ts
index cc1dbd2f91..fa9842f9fb 100644
--- a/src/domain/desktop/appTaskDraftNoise.ts
+++ b/src/domain/desktop/appTaskDraftNoise.ts
@@ -1,11 +1,14 @@
import type { JsonValue } from "../jsonValue.js";
import { isTimestamp } from "./appTask.js";
import {
+ isAutomaticStyle,
isIdentifyingAttribute,
isNameLikeKey,
isViewStateName,
VIEW_STATE,
} from "./attributeRank.js";
+import { isMemberCount } from "./appTaskDraftMemberCount.js";
+import { idReferencesOf, mirrorsOf } from "./appTaskDraftMirrors.js";
import { isPositionalKey, positionalKeys } from "./positionalKeys.js";
import type { DesktopStateDifference } from "./stateChanges.js";
import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js";
@@ -17,6 +20,7 @@ export const NOISE_REASONS = [
"volatile",
"timestamp",
"upkeep",
+ "layout",
] as const;
export type NoiseReason = (typeof NOISE_REASONS)[number];
@@ -34,25 +38,24 @@ const isViewStateValue = (value: string): boolean =>
const namesElement = (key: string) =>
isNameLikeKey(key) || /^(?:[^:]*:)?key$/iu.test(key);
-/** An attribute an app renumbers as it saves: a generated id such as Gnumeric's `ExprID` or Scribus's `ItemID`, or LibreOffice's `rsid`. */
-export const isRenumberedKey = (key: string): boolean => {
- const local = key.split(":").at(-1) ?? key;
- return (
- !isIdentifyingAttribute(key) &&
- /(?:[a-z]I[dD]|(?:^|[-_])[rR][sS][iI][dD])$/u.test(local)
- );
-};
-
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;
+export const isUuid = (value: string): boolean => UUID.test(value);
/** Seconds since 1970 of 2000-01-01 and 2100-01-01: an epoch time a save stamps falls between. */
const EPOCH_RANGE = [946_684_800, 4_102_444_800] as const;
-/** UUIDs and epoch-shaped values in named save metadata are likely generated. */
+/** Base64 of binary data an app encodes as it saves, as Xournal++'s page preview, a PNG (`iVBORw0KGgo`), or any run of 256 or more base64 characters mixing cases and digits, line breaks aside: no prose runs that long without a space. */
+const ENCODED =
+ /^(?:iVBORw0KGgo[A-Za-z0-9+/=\r\n]*|(?=[^]*[a-z])(?=[^]*[A-Z])(?=[^]*\d)[A-Za-z0-9+/\r\n]{256,}={0,2})$/u;
+
+/**
+ * UUIDs, encoded binary, and epoch-shaped values in named save metadata are
+ * likely generated. Numeric shape alone does not identify a timestamp.
+ */
export const isVolatileValue = (
value: string,
bookkeeping = false,
): boolean => {
- if (UUID.test(value)) return true;
+ if (isUuid(value) || ENCODED.test(value.trim())) return true;
if (!bookkeeping) return false;
if (!/^\d{10}(?:\d{3})?$/u.test(value)) return false;
const seconds = Number(value.slice(0, 10));
@@ -81,7 +84,7 @@ const DATE_PART = /^(?:year|month|day|hour|minute|second)$/u;
* `cdate` and `mdate`, or `created`, `modified`, `saved` or `updated`.
*/
const BOOKKEEPING =
- /^[cm]date$|creat(?:ed|ion)|modif(?:ied|ication)|updated|saved|changed|accessed|timestamp/u;
+ /^[cm]date$|creat(?:ed|ion)|modif(?:ied|ication)|updated|saved|changed|accessed|entered|timestamp/u;
export const isBookkeepingName = (name: string) =>
BOOKKEEPING.test((name.split(":").at(-1) ?? name).toLowerCase());
@@ -167,6 +170,97 @@ const stampedAlong = (elements: readonly Element[]) => {
});
};
+/** A date written whole, as GnuCash's `2026-11-06 08:50:12 +0000`, which `isTimestamp` reads only in ISO 8601's own spelling. */
+const DATE_TEXT = /^\s*\d{4}-\d{2}-\d{2}\b/u;
+
+/** Whether a value is a date under an element bookkeeping names, as GnuCash's `…`, or such an element holding one. */
+const stampedWhole = (
+ elements: readonly Element[],
+ value: JsonValue | undefined,
+) =>
+ elements.some(isBookkeeping) &&
+ DATE_TEXT.test(
+ typeof value === "string"
+ ? value
+ : isXmlElement(value)
+ ? elementText(value)
+ : "",
+ );
+
+/** Whether an element holds an id the app assigns, as GnuCash's ``. */
+const assignedId = (element: Element) =>
+ /^(?:[^:]*:)?id$/iu.test(element.name) &&
+ isRecord(element.attributes) &&
+ element.attributes.type === "guid";
+
+/** Whether text holds a control character other than a tab or line break, or the replacement character, which no user types, as GnuCash's options hold before it fills them. */
+const isGarbled = (value: string) =>
+ [...value].some((character) => {
+ const code = character.codePointAt(0) ?? 0;
+ return (
+ (code < 0x20 && code !== 0x09 && code !== 0x0a && code !== 0x0d) ||
+ code === 0x7f ||
+ code === 0xfffd
+ );
+ });
+
+/** An attribute counting the ids an app hands out, as Umbrello's `uniqueid` or Tiled's `nextobjectid`. */
+const ID_COUNTER = /^(?:next|unique|last)[-_]?(?:[\p{L}_-]*)id$/iu;
+
+/** A key keeping the last or next id an app hands out, as Umbrello's `lastobject`, which holds the id its new element took. */
+const COUNTER_KEY = /^(?:next|unique|last)[-_]?[\p{L}_-]*$/iu;
+
+/** Whether a value is one a program makes: a UUID, an epoch time or garbled text, a count under the metadata, an id counter, an id the change gave a new element where a counter or that element holds it (`fresh`), or in or at an id the app assigns. */
+const volatileAt = (
+ elements: readonly Element[],
+ key: string | undefined,
+ value: JsonValue | undefined,
+ fresh: boolean,
+ xml: boolean,
+) =>
+ (typeof value === "string" &&
+ (isVolatileValue(
+ value,
+ elements.some(isBookkeeping) ||
+ (key !== undefined && isBookkeepingName(key)),
+ ) ||
+ isGarbled(value) ||
+ fresh)) ||
+ (key !== undefined && ID_COUNTER.test(key.split(":").at(-1) ?? key)) ||
+ metadataCount(elements, key, value) ||
+ elements.some(assignedId) ||
+ (xml && isXmlElement(value) && assignedId(value));
+
+/**
+ * The ids of the elements a change added (`id`, `xid`, `uuid`, `guid`, as
+ * Umbrello's new attribute's `xmi:id="ubcgF2AWfBmHh"`), which another save
+ * of the same edit would not repeat, so a value equal to one in an id
+ * counter or in that element is the app's: those of four or more
+ * characters, not a plain number.
+ */
+const freshIds = (
+ change: Pick,
+ ends: Ends,
+): ReadonlySet =>
+ new Set(
+ change.added.flatMap((pointer) => {
+ const element = resolvePointer(ends.final, pointer);
+ return isXmlElement(element)
+ ? Object.entries(element.attributes).flatMap(([key, value]) =>
+ /^(?:[^:]*:)?(?:x|uu|g)?id$/iu.test(key) &&
+ typeof value === "string" &&
+ value.length >= 4 &&
+ !/^\d+$/u.test(value)
+ ? [value]
+ : [],
+ )
+ : [];
+ }),
+ );
+
+/** A container of an app's own bookkeeping slots, as GnuCash's `book:slots`, which it adds to and fills on reopening a book. */
+const isSlots = (element: Element) => /^(?:[^:]*:)?slots$/u.test(element.name);
+
/** Whether a pointer is a date the app stamps in parts, or inside one. */
export const inSplitDate = (view: JsonValue | null, pointer: string): boolean =>
stampedAlong(elementsAlong(view, pointer));
@@ -198,9 +292,15 @@ const showsView = (element: Element) =>
isViewStateName(element.name) ||
Object.entries(isRecord(element.attributes) ? element.attributes : {}).some(
([key, value]) =>
- typeof value === "string" && namesElement(key) && isViewStateValue(value),
+ typeof value === "string" &&
+ namesElement(key) &&
+ (isViewStateValue(value) || VIEW_SETTING.test(value)),
);
+/** A settings path whose last part says how a panel shows, as VYM's ``. */
+const VIEW_SETTING =
+ /^\/(?:[\w-]+\/)+(?:visible|hidden|shown?|expanded|collapsed|geometry|state|size|zoom)$/iu;
+
/** A file's views at its two ends. */
interface Ends {
readonly baseline: JsonValue | null;
@@ -246,13 +346,21 @@ const positionalRenumbering = (ends: Ends) => {
};
};
+/** A pointer to an archive entry's own size or digest, which change with any edit to it. */
+const ENTRY_META = /^\/entries\/[^/]+\/(?!view(?:\/|$))/u;
+
/** A pointer into an archive entry's view: the entry's part of it, as `/entries/[name=styles.xml]/view/`. */
const ENTRY_VIEW = /^\/entries\/[^/]+\/view\//u;
/**
- * Attributes an archive entry's elements only gained, as LibreOffice fills in
- * `loext:tab-stop-distance` across `styles.xml` on save, while another entry
- * holds the edit: whether each pointer is one.
+ * What an app keeps up as it saves, while the edit is elsewhere: attributes an
+ * known style defaults an archive entry's elements gained, as LibreOffice fills in
+ * `loext:tab-stop-distance` across `styles.xml`, while another entry holds
+ * the edit; and, while a change outside them is the edit, any change to a
+ * document's styles, `styles.xml` or its style containers and master,
+ * handout and notes pages, or to an attribute naming an automatic style
+ * at both ends (`isAutomaticStyle`), as Impress wraps its master pages'
+ * fields in a new `MT1` style after a reload.
*/
const upkeepOf = (
change: Pick,
@@ -288,6 +396,41 @@ const upkeepOf = (
filled.set(entry, counts);
counts.set(key, (counts.get(key) ?? 0) + 1);
}
+ // A style a user names, as "Quotations" given to a paragraph, is an edit; one the app numbers, as `P1`, its upkeep.
+ const automatic = (pointer: string, key: string) =>
+ [ends.baseline, ends.final].every((view) => {
+ const value = resolvePointer(view, pointer);
+ return (
+ value === undefined ||
+ (typeof value === "string" && isAutomaticStyle(key, value))
+ );
+ });
+ const styled = (pointer: string) => {
+ const key = attributeKeyOf(pointer);
+ const view =
+ resolvePointer(ends.final, pointer) === undefined
+ ? ends.baseline
+ : ends.final;
+ return (
+ xml(view, pointer) &&
+ (ENTRY_VIEW.exec(pointer)?.[0] === "/entries/[name=styles.xml]/view/" ||
+ (key !== undefined &&
+ /style-name$/iu.test(key) &&
+ automatic(pointer, key)) ||
+ elementsAlong(view, pointer).some(({ name }) => STYLE_AREA.test(name)))
+ );
+ };
+ // The user's edit is elsewhere when a change no other rule calls noise lies outside the styles.
+ const elsewhere = [
+ ...change.added,
+ ...change.changed,
+ ...change.removed,
+ ].some(
+ (pointer) =>
+ noise(pointer) === undefined &&
+ !styled(pointer) &&
+ !ENTRY_META.test(pointer),
+ );
return (pointer: string) => {
const [entry] = ENTRY_VIEW.exec(pointer) ?? [];
const key = attributeKeyOf(pointer);
@@ -295,39 +438,235 @@ const upkeepOf = (
entry === undefined || key === undefined
? 0
: (filled.get(entry)?.get(key) ?? 0);
+ const view =
+ resolvePointer(ends.final, pointer) === undefined
+ ? ends.baseline
+ : ends.final;
+ const inStyleArea = elementsAlong(view, pointer).some(({ name }) =>
+ STYLE_AREA.test(name),
+ );
+ const automaticName =
+ key !== undefined && /style-name$/iu.test(key) && automatic(pointer, key);
+ // One attribute gained in styles.xml is the edit. A repeated fill-in, an automatic style name, or a style area the app rewrites is upkeep.
return (
- edited.size > 0 &&
- entry !== undefined &&
- !edited.has(entry) &&
- gained(pointer) &&
- count >= 2
+ (elsewhere &&
+ (automaticName || inStyleArea || (styled(pointer) && count >= 2))) ||
+ (edited.size > 0 &&
+ entry !== undefined &&
+ !edited.has(entry) &&
+ gained(pointer) &&
+ count >= 2)
);
};
};
+/** An element holding an ODF document's styles, or its master, handout and notes pages, which an app rewrites as it saves after a reload. */
+const STYLE_AREA =
+ /^(?:office:(?:styles|automatic-styles|master-styles|font-face-decls)|style:(?:master-page|handout-master))$/u;
+
+/** A number, or a list of numbers split by commas or semicolons, as Dia's `6.3225,6.15;12.5775,8.15`. */
+const COORDINATES =
+ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?(?:[,;] ?-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)*$/u;
+
+/** Whether a value is a coordinate or a list of them, which an app may derive rather than its user enter. */
+export const isCoordinates = (value: JsonValue | undefined): boolean =>
+ typeof value === "string" && COORDINATES.test(value.trim());
+
+/** How far above an attribute's element the element whose text changed may sit for the attribute to be its layout. */
+const MAX_LAYOUT_CLIMB = 3;
+
+/**
+ * Attributes holding coordinates that changed inside an element whose text
+ * changed while their own element's did not, as Dia moves and resizes a box
+ * (`obj_pos`, `obj_bb`, `elem_width`) around its new text: layout the app
+ * derives from the edit. A list of numbers is layout; a lone number only
+ * where a name on its way says it is (`namesLayout`). The element whose text changed is at most three
+ * levels up and below the root's own children; an attribute of an element
+ * whose own text changed, as a spreadsheet cell's `office:value` beside its
+ * text, is none.
+ */
+const derivedLayout = (
+ change: Pick,
+ ends: Ends,
+) => {
+ const textAt = (view: JsonValue | null, pointer: string) => {
+ const element = resolvePointer(view, pointer);
+ return isXmlElement(element) ? elementText(element) : undefined;
+ };
+ const known = new Map();
+ const retexted = (pointer: string) => {
+ const before = textAt(ends.baseline, pointer);
+ const found =
+ known.get(pointer) ??
+ (before !== undefined && before !== textAt(ends.final, pointer));
+ known.set(pointer, found);
+ return found;
+ };
+ const layout = new Set(
+ change.changed.filter((pointer) => {
+ const value = resolvePointer(ends.final, pointer);
+ const owner = ownerOfAttribute(pointer);
+ if (
+ attributeKeyOf(pointer) === undefined ||
+ !isCoordinates(value) ||
+ retexted(owner)
+ )
+ return false;
+ const segments = owner.split("/");
+ const above = Array.from({ length: MAX_LAYOUT_CLIMB }, (_, index) =>
+ segments.slice(0, -2 * (index + 1)).join("/"),
+ );
+ const at = above.findIndex(
+ (element) =>
+ /\/children\/[^/]+$/u.test(element) &&
+ element.split("/").at(-3) !== "root" &&
+ retexted(element),
+ );
+ // A lone number is layout only where a name on its way says so; a list of them always is.
+ return (
+ at >= 0 &&
+ (/[,;]/u.test(String(value)) ||
+ namesLayout(ends.final, pointer, [owner, ...above.slice(0, at)]))
+ );
+ }),
+ );
+ return (pointer: string) => layout.has(pointer);
+};
+
+/** A tag naming a drawing of a model rather than the model, as Umbrello's `diagrams`, `diagram`, `widgets` and `classwidget`. */
+const DRAWING = /(?:diagram|widget|canvas|scene)s?$/iu;
+
+/**
+ * Coordinates in a drawing of the model, while the model change is
+ * elsewhere, as Umbrello grows a class's box (`classwidget` `height` 52 to
+ * 67) in its diagram when an attribute is added to the class: an attribute
+ * holding a list of numbers, or a number a name on its way calls layout,
+ * below an element whose tag names a diagram, widget, canvas or scene, while
+ * a change no other rule calls noise lies outside every such element.
+ */
+const drawingLayout = (
+ change: Pick,
+ ends: Ends,
+ noise: (pointer: string) => NoiseReason | undefined,
+) => {
+ const viewOf = (pointer: string) =>
+ resolvePointer(ends.final, pointer) === undefined
+ ? ends.baseline
+ : ends.final;
+ const drawn = (pointer: string) =>
+ elementsAlong(viewOf(pointer), pointer).some(({ name }) =>
+ DRAWING.test(name.split(":").at(-1) ?? name),
+ );
+ // A model element added or removed can resize the drawing. A rename or
+ // other edit outside it does not make a deliberate resize into layout.
+ const elsewhere = [...change.added, ...change.removed].some(
+ (pointer) =>
+ noise(pointer) === undefined &&
+ !ENTRY_META.test(pointer) &&
+ !drawn(pointer),
+ );
+ return (pointer: string) => {
+ const value = resolvePointer(viewOf(pointer), pointer);
+ return (
+ elsewhere &&
+ attributeKeyOf(pointer) !== undefined &&
+ isCoordinates(value) &&
+ drawn(pointer) &&
+ (/[,;]/u.test(String(value)) ||
+ namesLayout(viewOf(pointer), pointer, [ownerOfAttribute(pointer)]))
+ );
+ };
+};
+
+/** A word in a name for where or how large something is drawn: a position, a point, a box, a corner, a width or height, an offset or a side. */
+const LAYOUT_WORD =
+ /^(?:pos|position|x|y|z|point|bb|bbox|bounds|rect|rectangle|corner|width|height|w|h|left|top|right|bottom|offset|origin|coords?)$/u;
+
+/** Whether a name holds a layout word, its parts split at punctuation and case, as Dia's `obj_pos`, `elem_width` or `dia:point`. */
+const isLayoutName = (name: string) =>
+ name
+ .replaceAll(/(?\p{Ll})(?\p{Lu})/gu, "$ $")
+ .toLowerCase()
+ .split(/[^\p{L}\d]+/u)
+ .some((word) => LAYOUT_WORD.test(word));
+
+/**
+ * Whether the attribute at a pointer, its element's tag, or the tag or a
+ * name-like attribute's value of an element between it and the element
+ * whose text changed, names layout, as Dia keeps `elem_width` in
+ * ``; an order's
+ * `` is no layout.
+ */
+const namesLayout = (
+ view: JsonValue | null,
+ pointer: string,
+ elements: readonly string[],
+) =>
+ [
+ attributeKeyOf(pointer) ?? "",
+ ...elements.flatMap((at) => {
+ const element = resolvePointer(view, at);
+ return isXmlElement(element)
+ ? [
+ element.name,
+ ...Object.entries(element.attributes).flatMap(([key, value]) =>
+ isNameLikeKey(key) && typeof value === "string" ? [value] : [],
+ ),
+ ]
+ : [];
+ }),
+ ].some(isLayoutName);
+
/**
* Why each pointer into a file is noise, or undefined when it is not: a tag,
* attribute or name-like attribute value on its way naming view state, as
- * `[@config:name=CursorPositionX]`; an attribute an app renumbers, an
- * identifier it numbers by position, or anything inside an element both ends
- * hold whose renumbered attribute changed, as Gnumeric empties a formula cell
- * whose shared expression it renumbers; a UUID or epoch time, or a duration or
+ * `[@config:name=CursorPositionX]`; an identifier numbered by position;
+ * a UUID or epoch time, an id
+ * counter, an id the change gave a new element (`freshIds`), or a duration or
* count under the document's metadata; a time the app stamps, whole or in
- * parts (`inSplitDate`); the known style default an archive entry gained
- * while another entry holds the edit.
+ * parts (`inSplitDate`), or a date under a tag naming bookkeeping; an id
+ * the app assigns (`type="guid"`), or text of control characters; upkeep, a
+ * change inside an app's `slots`, a known style default an archive entry
+ * gained while another entry holds the edit, a text showing another field
+ * that changed alike (`mirrorsOf`), or an element holding nothing but the id
+ * of a record the change added (`idReferencesOf`); layout an edit's text
+ * derived (`derivedLayout`), or a drawing of the model holds while the
+ * model change is elsewhere (`drawingLayout`).
*/
export const changeNoise = (
change: Pick,
ends: Ends,
xml: (view: JsonValue | null, pointer: string) => boolean,
): ((pointer: string) => NoiseReason | undefined) => {
- const renumbered = change.changed.flatMap((pointer) => {
- const key = attributeKeyOf(pointer);
- return xml(ends.final, pointer) && key !== undefined && isRenumberedKey(key)
- ? [ownerOfAttribute(pointer)]
- : [];
- });
+ const isXmlPointer = (pointer: string) =>
+ xml(
+ resolvePointer(ends.final, pointer) === undefined
+ ? ends.baseline
+ : ends.final,
+ pointer,
+ );
+ const xmlChange = {
+ added: change.added.filter((pointer) => xml(ends.final, pointer)),
+ changed: change.changed.filter(isXmlPointer),
+ removed: change.removed.filter((pointer) => xml(ends.baseline, pointer)),
+ };
const byPosition = positionalRenumbering(ends);
+ const fresh = freshIds(xmlChange, ends);
+ const added = new Set(change.added);
+ // A reference on an element both ends hold, as a split moved to the new account, is the user's.
+ const freshAt = (
+ pointer: string,
+ key: string | undefined,
+ value: JsonValue | undefined,
+ ) =>
+ typeof value === "string" &&
+ fresh.has(value) &&
+ ((key !== undefined && COUNTER_KEY.test(key.split(":").at(-1) ?? key)) ||
+ pointer
+ .split("/")
+ .some((_, index, segments) =>
+ added.has(segments.slice(0, index + 1).join("/")),
+ ));
const known = new Map();
const base = (pointer: string): NoiseReason | undefined => {
if (known.has(pointer)) return known.get(pointer);
@@ -342,38 +681,40 @@ export const changeNoise = (
const reason: NoiseReason | undefined =
elements.some(showsView) || (key !== undefined && isViewStateName(key))
? "view_state"
- : (key !== undefined &&
- (isRenumberedKey(key) ||
- (isIdentifyingAttribute(key) && byPosition(pointer, key)))) ||
- renumbered.some((element) => {
- if (pointer !== element && !pointer.startsWith(`${element}/`))
- return false;
- if (pointer === element) return false;
- const next = resolvePointer(ends.final, pointer);
- const text =
- typeof next === "string"
- ? next.trim()
- : isXmlElement(next)
- ? elementText(next).trim()
- : undefined;
- // Gnumeric clears a shared formula when it renumbers the cell. A formula that still has text was edited.
- return text === undefined || text === "";
- })
+ : key !== undefined &&
+ isIdentifyingAttribute(key) &&
+ byPosition(pointer, key)
? "renumbered"
- : (typeof value === "string" &&
- isVolatileValue(
- value,
- elements.some(isBookkeeping) ||
- (key !== undefined && isBookkeepingName(key)),
- )) ||
- metadataCount(elements, key, value)
+ : volatileAt(
+ elements,
+ key,
+ value,
+ freshAt(pointer, key, value),
+ isXml,
+ )
? "volatile"
- : isStamp(value ?? null) || stampedAlong(elements)
+ : isStamp(value ?? null) ||
+ stampedAlong(elements) ||
+ stampedWhole(elements, value)
? "timestamp"
- : undefined;
+ : elements.some(isSlots) ||
+ (isXml && isMemberCount(ends, pointer, key))
+ ? "upkeep"
+ : undefined;
known.set(pointer, reason);
return reason;
};
const upkeep = upkeepOf(change, ends, base, xml);
- return (pointer) => base(pointer) ?? (upkeep(pointer) ? "upkeep" : undefined);
+ const layout = derivedLayout(change, ends);
+ const drawing = drawingLayout(change, ends, base);
+ const mirror = mirrorsOf(xmlChange, ends);
+ const reference = idReferencesOf(xmlChange, ends);
+ return (pointer) =>
+ base(pointer) ??
+ (isXmlPointer(pointer) &&
+ (upkeep(pointer) || mirror(pointer) || reference(pointer))
+ ? "upkeep"
+ : isXmlPointer(pointer) && (layout(pointer) || drawing(pointer))
+ ? "layout"
+ : undefined);
};
diff --git a/src/domain/desktop/appTaskDraftNotes.ts b/src/domain/desktop/appTaskDraftNotes.ts
index e8cfc44004..368853be4f 100644
--- a/src/domain/desktop/appTaskDraftNotes.ts
+++ b/src/domain/desktop/appTaskDraftNotes.ts
@@ -34,7 +34,7 @@ export const draftNoteSchema = z.strictObject({
.string()
.optional()
.describe(
- "by_position, view_state_only, long_text, cut_view, unreadable_view: the drafted file the note is about.",
+ "by_position, view_state_only, long_text, cut_view, unreadable_view, volatile_lines: the drafted file the note is about.",
),
files: z
.array(z.strictObject({ file: z.string(), role: z.string() }))
diff --git a/src/domain/desktop/appTaskDraftPositional.test.ts b/src/domain/desktop/appTaskDraftPositional.test.ts
index 20bf6c0043..4703489e9e 100644
--- a/src/domain/desktop/appTaskDraftPositional.test.ts
+++ b/src/domain/desktop/appTaskDraftPositional.test.ts
@@ -1,8 +1,9 @@
import { describe, expect, it } from "vitest";
import type { JsonValue } from "../jsonValue.js";
-import { passing, xml, xmlDraft } from "./appTaskDraft.fixture.js";
-import { pairMembers, resolvePointer } from "./viewPointer.js";
+import { passing, pointerOf, xml, xmlDraft } from "./appTaskDraft.fixture.js";
+import { pairMembers } from "./memberPairing.js";
+import { resolvePointer } from "./viewPointer.js";
const FILE = "Documents/doc.xml";
@@ -14,9 +15,17 @@ const membersAt = (text: string, pointer: string): JsonValue[] => {
};
describe("draftTaskFiles for a change only position reaches", () => {
- /** A sheet as an ODS spreadsheet keeps it: rows no attribute names, each cell's text in a paragraph, and a save counter. */
- const sheet = (february: string, total: string, cycles: string) =>
- `${cycles}
Month
Sales
February
${february}
Total
${total}
`;
+ /** A sheet as an ODS spreadsheet keeps it: rows no attribute names, each cell's text in a paragraph, labelled apart or alike, and a save counter. */
+ const sheet = (
+ february: string,
+ total: string,
+ cycles: string,
+ labelled = false,
+ ) => {
+ const label = (text: string) =>
+ `
${labelled ? text : "Q1"}
`;
+ return `${cycles}
Month
Sales
${label("February")}
${february}
${label("Total")}
${total}
`;
+ };
it("checks the whole text of the ancestor above the step by position, by its name, and demotes the noise beside it rather than suggesting it", () => {
const before = sheet("21300", "59625", "1");
@@ -29,7 +38,7 @@ describe("draftTaskFiles for a change only position reaches", () => {
kind: "text_equals",
file: FILE,
pointer: "/root/children/[name=table][@name=Q1 Sales]",
- value: "MonthSalesFebruary23100Total61425",
+ value: "MonthSalesQ123100Q161425",
},
]);
expect(passing(FILE, xml(before), file.suggested)).toEqual([false]);
@@ -37,6 +46,34 @@ describe("draftTaskFiles for a change only position reaches", () => {
expect(file.notes.map(({ code }) => code)).not.toContain("view_state_only");
});
+ it("names a row by its label cell, so the edited cell is checked by name, not the table's whole text", () => {
+ const before = sheet("21300", "59625", "1", true);
+ const after = sheet("23100", "61425", "2", true);
+
+ const file = xmlDraft(before, after);
+
+ const row = (label: string) =>
+ `/root/children/[name=table][>row=MonthSales]/children/[name=row][>cell=${label}]/children/[name=cell][@type=float]`;
+ expect(file.suggested.slice(0, 2)).toEqual([
+ {
+ kind: "text_equals",
+ file: FILE,
+ pointer: row("February"),
+ value: "23100",
+ },
+ {
+ kind: "text_equals",
+ file: FILE,
+ pointer: row("Total"),
+ value: "61425",
+ },
+ ]);
+ expect(passing(FILE, xml(before), file.suggested).some(Boolean)).toBe(
+ false,
+ );
+ expect(passing(FILE, xml(after), file.suggested).every(Boolean)).toBe(true);
+ });
+
it("suggests no noise when position alone reaches the edit and no ancestor's text can take a check", () => {
// The table's text is past the limit a text check compares.
const long = "x".repeat(2100);
@@ -100,6 +137,13 @@ describe("identifiers an app numbers by position", () => {
pointer: `${tasks}[@name=User testing]/name`,
value: "task",
},
+ {
+ kind: "member_count",
+ file: FILE,
+ pointer: "/root/children/[name=tasks]/children",
+ matching: "[name=task][@type=normal]",
+ equals: 4,
+ },
]);
expect(file.values.map(({ pointer, note }) => [pointer, note])).toEqual([
[`${tasks}[@name=User testing]`, undefined],
@@ -116,6 +160,13 @@ describe("identifiers an app numbers by position", () => {
const tasks = "/root/children/[name=tasks]/children/[name=task]";
expect(file.suggested).toEqual([
{ kind: "value_absent", file: FILE, pointer: `${tasks}[@name=Design]` },
+ {
+ kind: "member_count",
+ file: FILE,
+ pointer: "/root/children/[name=tasks]/children",
+ matching: "[name=task][@type=normal]",
+ equals: 2,
+ },
]);
});
@@ -166,28 +217,41 @@ describe("identifiers an app numbers by position", () => {
});
describe("an element renamed", () => {
- /** A painting's layer stack as MyPaint saves it: each layer by name, its pixels in `src`, the one selected flagged. */
- const stack = (top: string) =>
- `${[
- [top, "00", "576", "true"],
- ["Sky", "01", "0", "false"],
- ["Background", "02", "0", "false"],
- ]
+ /**
+ * A painting's layer stack as MyPaint saves it: each layer by name, its
+ * pixels in `src`, numbered along the stack as MyPaint 2 writes it or named
+ * by the layer, the one selected flagged; the top layer first, or second.
+ */
+ const stack = (
+ top: string,
+ {
+ numbered = true,
+ moved = false,
+ }: { numbered?: boolean; moved?: boolean } = {},
+ ) => {
+ const layers = [
+ [top, "sun", "576", "true"],
+ ["Sky", "sky", "0", "false"],
+ ["Background", "ground", "0", "false"],
+ ];
+ const order = moved ? [1, 0, 2].map((at) => layers[at] ?? []) : layers;
+ return `${order
.map(
- ([name, layer, x, selected]) =>
- ``,
+ ([name, own, x, selected], index) =>
+ ``,
)
.join("")}`;
+ };
const LAYER = "/root/children/[name=stack]/children/[name=layer]";
it("pairs a layer whose name alone changed, at its place among the layers, by another attribute naming it alone rather than the one selected", () => {
const layers = (top: string) =>
- membersAt(stack(top), "/root/children/0/children");
+ membersAt(stack(top, { numbered: false }), "/root/children/0/children");
const pairing = pairMembers(layers("Sun"), layers("Untitled"));
const keys = [
- "[name=layer][@src=data/layer-00.png]",
+ "[name=layer][@src=data/sun.png]",
"[name=layer][@name=Sky]",
"[name=layer][@name=Background]",
];
@@ -197,14 +261,25 @@ describe("an element renamed", () => {
});
});
- it("checks the renamed layer's new name by the attribute that names it, and that the old name names nothing", () => {
+ it("keys a renamed layer by each version's own name where the attribute pairing it numbers the layers in order, as MyPaint's src", () => {
+ const layers = (top: string) =>
+ membersAt(stack(top), "/root/children/0/children");
+
+ const pairing = pairMembers(layers("Sun"), layers("Untitled"));
+
+ expect(pairing.newer.keys[0]).toBe("[name=layer][@name=Sun]");
+ expect(pairing.older.keys[0]).toBe("[name=layer][@name=Untitled]");
+ expect(pairing.newer.partners).toEqual([0, 1, 2]);
+ });
+
+ it("checks the renamed layer's new name by the attribute that names it, and that the old name names nothing, though a later save moves it", () => {
const file = xmlDraft(stack("Untitled"), stack("Sun"));
expect(file.suggested).toEqual([
{
kind: "value_equals",
file: FILE,
- pointer: `${LAYER}[@src=data~1layer-00.png]/attributes/name`,
+ pointer: `${LAYER}[@name=Sun]/attributes/name`,
value: "Sun",
},
{
@@ -213,14 +288,247 @@ describe("an element renamed", () => {
pointer: `${LAYER}[@name=Untitled]`,
},
]);
+ expect(file.values).toMatchObject([
+ { kind: "changed", before: "Untitled", after: "Sun" },
+ ]);
+ // The move renumbers every src, which a check by src would have failed.
expect(
- [stack("Untitled"), stack("Sun"), stack("Moon")].map((text) =>
- passing(FILE, xml(text), file.suggested),
- ),
+ [
+ stack("Untitled"),
+ stack("Sun"),
+ stack("Moon"),
+ stack("Sun", { moved: true }),
+ ].map((text) => passing(FILE, xml(text), file.suggested)),
).toEqual([
[false, false],
[true, true],
[false, true],
+ [true, true],
+ ]);
+ // No layer added or removed is a guard at the unchanged count.
+ expect(file.guards.at(-1)).toEqual({
+ kind: "member_count",
+ file: FILE,
+ pointer: "/root/children/[name=stack]/children",
+ matching: "[name=layer]",
+ equals: 3,
+ });
+ });
+});
+
+describe("members named by a child's text or a generated id", () => {
+ it("counts a child's text on every sibling, one an identifier names included, so a selector both match names neither", () => {
+ const records = (note: string) =>
+ `SharedkeptShared${note}`;
+
+ const file = xmlDraft(records("before"), records("after"));
+
+ const pointers = [
+ ...file.suggested.flatMap((check) =>
+ "pointer" in check ? [check.pointer] : [],
+ ),
+ ...file.values.map(({ pointer }) => pointer),
+ ];
+ expect(
+ pointers.some((pointer) => pointer.includes("[>title=Shared]")),
+ ).toBe(false);
+ expect(file.values.map(({ after }) => after)).toEqual(["after"]);
+ expect(
+ passing(FILE, xml(records("after")), file.suggested).every(Boolean),
+ ).toBe(true);
+ });
+
+ it("names a new node by its text rather than the id the app generated, wherever the id sits among its attributes, and pairs a kept one by that id", () => {
+ const map = (...nodes: string[]) =>
+ `${nodes.join("")}`;
+ const node = (id: string, text: string) =>
+ ``;
+
+ const pairing = pairMembers(
+ membersAt(
+ map(
+ node("ID_1749680464", "Tent"),
+ node("ID_329023692", "Water filter"),
+ ),
+ "/root/children/0/children",
+ ),
+ membersAt(
+ map(node("ID_1749680464", "Tent")),
+ "/root/children/0/children",
+ ),
+ );
+
+ expect(pairing.newer.keys).toEqual([
+ "[name=node][@ID=ID_1749680464]",
+ "[name=node][@TEXT=Water filter]",
+ ]);
+ });
+});
+
+describe("a container in a list named for its tag", () => {
+ /** A workbook as Gnumeric saves one: sheets in `Sheets`, each named by its `Name` child. */
+ const book = (march: string, more = "") =>
+ `Budget402.15${march}${more}`;
+
+ it("names a sheet its tag alone names by its name child, so a sheet added later leaves the check its own", () => {
+ const file = xmlDraft(book("376.9"), book("418.65"));
+
+ expect(file.suggested.map(pointerOf)).toEqual([
+ "/root/children/[name=Sheets]/children/[name=Sheet][>Name=Budget]/children/[name=Cells]/children/[name=Cell][@Col=3]",
+ ]);
+ const another = book("418.65", "Notes");
+ expect(passing(FILE, xml(another), file.suggested)).toEqual([true]);
+ });
+
+ it("holds no step of a list not named for its member's tag, nor a member with no name", () => {
+ const plain = (text: string) =>
+ `Plan
${text}
`;
+
+ const file = xmlDraft(plain("Monday"), plain("Tuesday"));
+
+ expect(file.suggested.map(pointerOf)).toEqual([
+ "/root/children/[name=body]/children/[name=section]/children/[name=p]",
+ ]);
+ });
+});
+
+describe("records an app gave generated ids", () => {
+ const records = (first: string, second: string) =>
+ `${first}${second}`;
+
+ it("pairs kept records by their ids, so titles they swapped read as two title edits, not two id changes", () => {
+ const file = xmlDraft(records("Alpha", "Beta"), records("Beta", "Alpha"));
+
+ expect(
+ file.values.map(({ pointer, kind, note }) => [
+ pointer,
+ kind,
+ note ?? null,
+ ]),
+ ).toEqual(
+ [
+ "0b5c1f0e-3c1d-4f7a-9a3e-2d7f5b8c9e10",
+ "7d1e2a90-5b4c-4e8f-8a21-6c3b9f0d4e57",
+ ].map((id) => [
+ `/root/children/[name=record][@id=${id}]/children/[name=title]/children/0`,
+ "changed",
+ null,
+ ]),
+ );
+ expect(
+ passing(FILE, xml(records("Alpha", "Beta")), file.suggested),
+ ).not.toContain(true);
+ expect(
+ passing(FILE, xml(records("Beta", "Alpha")), file.suggested).every(
+ Boolean,
+ ),
+ ).toBe(true);
+ });
+});
+
+describe("shapes an ODF app numbers on every save", () => {
+ /** A Draw page's boxes, each named by the user, and numbered `id1`… by LibreOffice from `first` as it saves. */
+ const page = (second: string, first = 1) =>
+ `${[
+ "Order Received",
+ second,
+ "Pack Items",
+ "Ship Order",
+ ]
+ .map(
+ (text, index) =>
+ `${text}`,
+ )
+ .join("")}`;
+ const shape = (step: number) =>
+ `/root/children/[name=draw:custom-shape][@draw:name=Step ${String(step)}]`;
+
+ it("names each box by the name the user gave it, not the id the save numbered, so a save numbering them anew keeps the checks and guards", () => {
+ const file = xmlDraft(page("Pick Items"), page("Pick and Scan"));
+
+ expect(file.suggested).toEqual([
+ {
+ kind: "text_equals",
+ file: FILE,
+ pointer: shape(2),
+ value: "Pick and Scan",
+ },
+ ]);
+ expect(file.guards.map(pointerOf)).toEqual([
+ shape(1),
+ shape(3),
+ "/root/children",
+ ]);
+ // Another save numbers the shapes from 5.
+ const renumbered = xml(page("Pick and Scan", 5));
+ expect(passing(FILE, renumbered, file.suggested)).toEqual([true]);
+ expect(passing(FILE, renumbered, file.guards)).toEqual([true, true, true]);
+ // The value lists no other pointer by an id the save numbered.
+ expect(file.values[0]).toMatchObject({
+ pointer: `${shape(2)}/children/[name=text:p]/children/0`,
+ });
+ expect(file.values[0]?.alternatives).toBeUndefined();
+ });
+});
+
+describe("entries an app numbers by where it saves them", () => {
+ /** A Parley collection: entries numbered 0.. by save position, each two translations by language, and a lesson listing them by id. */
+ const entry = (id: number, english: string, spanish: string) =>
+ `${english}${spanish}`;
+ const collection = (pairs: readonly (readonly [string, string])[]) =>
+ `${pairs.map(([english, spanish], id) => entry(id, english, spanish)).join("")}Lesson 1${pairs.map((_, id) => ``).join("")}`;
+ const DOG = ["dog", "perro"] as const;
+ const BOOK = ["book", "libro"] as const;
+ const APPLE = ["apple", "manzana"] as const;
+ const LIBRARY = ["library", "biblioteca"] as const;
+ const SEED = [DOG, BOOK, APPLE];
+ const ENTRIES = "/root/children/[name=entries]/children";
+
+ it("names the new entry and the kept ones by their translations, not the id that numbers them by save position, so a save inserting the entry elsewhere keeps the checks", () => {
+ const file = xmlDraft(collection(SEED), collection([...SEED, LIBRARY]));
+
+ expect(file.suggested.map(pointerOf)).toEqual([
+ `${ENTRIES}/[name=entry][>translation=library]/children/[name=translation][@id=1]`,
+ ENTRIES,
+ ]);
+ expect(file.guards.map(pointerOf)).toEqual([
+ `${ENTRIES}/[name=entry][>translation=apple]/children/[name=translation][@id=1]`,
+ `${ENTRIES}/[name=entry][>translation=book]/children/[name=translation][@id=1]`,
+ ]);
+ const inserted = xml(collection([DOG, BOOK, LIBRARY, APPLE]));
+ expect(passing(FILE, inserted, file.suggested)).toEqual([true, true]);
+ expect(passing(FILE, inserted, file.guards)).toEqual([true, true]);
+ });
+
+ it("notes the lesson's entry holding nothing but the new entry's id as upkeep naming that entry, never a check or a count", () => {
+ const file = xmlDraft(collection(SEED), collection([...SEED, LIBRARY]));
+
+ const lesson =
+ "/root/children/[name=lessons]/children/[name=container]/children";
+ expect(
+ file.values.find(({ pointer }) => pointer.startsWith(lesson)),
+ ).toMatchObject({
+ pointer: `${lesson}/[name=entry][@id=3]`,
+ note: "upkeep",
+ refers_to: [{ attribute: "id", value: "3" }],
+ });
+ expect(
+ [...file.suggested, ...file.guards].some((check) =>
+ pointerOf(check)?.startsWith(lesson),
+ ),
+ ).toBe(false);
+ });
+});
+
+describe("elements keyed only by an id a save numbered with six digits", () => {
+ const notes = (hello: string) =>
+ `
${hello}
There
`;
+
+ it("checks the edited paragraph by that id, counted once", () => {
+ const file = xmlDraft(notes("Hello"), notes("Hi"));
+
+ expect(file.suggested.map(pointerOf)).toEqual([
+ "/root/children/[name=p][@xml:id=id123456]",
]);
});
});
diff --git a/src/domain/desktop/appTaskDraftScalars.ts b/src/domain/desktop/appTaskDraftScalars.ts
index 2707589b03..550633362a 100644
--- a/src/domain/desktop/appTaskDraftScalars.ts
+++ b/src/domain/desktop/appTaskDraftScalars.ts
@@ -1,6 +1,6 @@
import type { JsonValue } from "../jsonValue.js";
import type { DesktopTaskCheckInput } from "./appTask.js";
-import { numberOf } from "./appTaskChecks.js";
+import { numberOf, sameJson } from "./appTaskChecks.js";
/** How far a decimal the app writes as text may drift, such as `437.80000000000001` for 437.8, and still match. */
export const DECIMAL_TOLERANCE = 0.001;
@@ -13,12 +13,24 @@ export const decimalOf = (value: JsonValue | undefined): number | undefined => {
return Number.isFinite(number) ? number : undefined;
};
-/** Whether a decimal's number is within the tolerance of a baseline value, read as the evaluators read a number, so a check by number would pass there too. */
+/** Whether a decimal's number is within the tolerance of a baseline value, read as the evaluators read a number, `"10"` and `"1e1"` included, so a check by number would pass there too. */
const nearBaseline = (number: number, before: JsonValue | undefined) => {
const old = before === undefined ? undefined : numberOf(before);
return old !== undefined && Math.abs(old - number) <= DECIMAL_TOLERANCE;
};
+/** Whether a `value_equals` passes on a value, as the evaluator decides it: by number within its tolerance, or as JSON. */
+export const passesOn = (
+ check: DesktopTaskCheckInput,
+ value: JsonValue | undefined,
+): boolean => {
+ if (check.kind !== "value_equals" || value === undefined) return false;
+ const number = numberOf(value);
+ return check.tolerance !== undefined && typeof check.value === "number"
+ ? number !== undefined && Math.abs(number - check.value) <= check.tolerance
+ : sameJson(value, check.value);
+};
+
/**
* A `value_equals` check of a value: a decimal string as its number within
* `DECIMAL_TOLERANCE`, unless `before`, the value the check must fail on, is
diff --git a/src/domain/desktop/appTaskDraftSteps.ts b/src/domain/desktop/appTaskDraftSteps.ts
new file mode 100644
index 0000000000..ebeb204bda
--- /dev/null
+++ b/src/domain/desktop/appTaskDraftSteps.ts
@@ -0,0 +1,81 @@
+import type { JsonValue } from "../jsonValue.js";
+import { isNameLikeKey } from "./attributeRank.js";
+import { isXmlElement, resolvePointer } from "./viewPointer.js";
+import { selectorOf } from "./viewSelector.js";
+
+/** Where a pointer steps into a list: the list, the member's segment and the rest of the pointer after it. */
+export interface ListStep {
+ readonly list: string;
+ readonly member: string;
+ readonly rest: readonly string[];
+}
+
+/** Where a pointer steps into a list, innermost first; by selector only, unless `positional` keeps steps by position too. */
+export const listSteps = (pointer: string, positional = false): ListStep[] => {
+ const segments = pointer.split("/");
+ const steps: ListStep[] = [];
+ for (let index = segments.length - 1; index >= 2; index -= 1) {
+ const member = segments[index] ?? "";
+ if (
+ segments[index - 1] === "children" &&
+ (positional || !/^\d+$/u.test(member))
+ )
+ steps.push({
+ list: segments.slice(0, index).join("/"),
+ member,
+ rest: segments.slice(index + 1),
+ });
+ }
+ return steps;
+};
+
+/** Whether a step's member is named as a field is, by a name-like attribute alone, as QElectroTech's `elementInformation[@name=label]`. */
+const namedAsField = ({ member }: ListStep) => {
+ const [only, ...more] = (
+ selectorOf(member.replaceAll("~1", "/").replaceAll("~0", "~")) ?? []
+ ).filter(({ prefix }) => prefix !== "");
+ return (
+ only !== undefined &&
+ more.length === 0 &&
+ only.prefix === "@" &&
+ isNameLikeKey(only.key)
+ );
+};
+
+/** Whether a like sibling of an outer step's member holds the field a step goes through, at the same path, as every component holds its `label`. */
+const sharedField = (
+ view: JsonValue | null,
+ step: ListStep,
+ outer: ListStep,
+) => {
+ const records = resolvePointer(view, outer.list);
+ const record = resolvePointer(view, `${outer.list}/${outer.member}`);
+ const inner = outer.rest.slice(0, outer.rest.length - step.rest.length);
+ return (
+ Array.isArray(records) &&
+ isXmlElement(record) &&
+ records.some(
+ (item) =>
+ item !== record &&
+ isXmlElement(item) &&
+ item.name === record.name &&
+ resolvePointer(item, `/${inner.join("/")}`) !== undefined,
+ )
+ );
+};
+
+/**
+ * The record a step's member is a field of: the nearest step out whose like
+ * siblings hold the same field at the same path, for a member named as a
+ * field (`namedAsField`), as every QElectroTech component holds its
+ * `elementInformation[@name=label]`, or a Glade button its `label` property;
+ * undefined for a member that is a record of its own.
+ */
+export const recordOfField = (
+ view: JsonValue | null,
+ step: ListStep,
+ outer: readonly ListStep[],
+): ListStep | undefined =>
+ namedAsField(step)
+ ? outer.find((candidate) => sharedField(view, step, candidate))
+ : undefined;
diff --git a/src/domain/desktop/appTaskDraftText.ts b/src/domain/desktop/appTaskDraftText.ts
index 98b623d600..dd36daf912 100644
--- a/src/domain/desktop/appTaskDraftText.ts
+++ b/src/domain/desktop/appTaskDraftText.ts
@@ -1,11 +1,12 @@
import type { JsonValue } from "../jsonValue.js";
-import type { DesktopTaskCheckInput } from "./appTask.js";
+import { collapseWhitespace, type DesktopTaskCheckInput } from "./appTask.js";
import {
holdsSplitDate,
isBookkeeping,
isStamp,
isVolatileValue,
} from "./appTaskDraftNoise.js";
+import { isPlacedItem, placedItemCheck } from "./appTaskDraftItems.js";
import { valueEqualsCheck } from "./appTaskDraftScalars.js";
import { rankAttributes } from "./attributeRank.js";
import type { DesktopStateDifference } from "./stateChanges.js";
@@ -34,6 +35,8 @@ export interface TextPlacement {
/** Text checks for the elements whose text changed, and the pointers those checks make redundant: text and whole runs holding text in them, or an element added whole checked itself, not an attribute or name of an element both ends hold, nor an element holding no text, as an image or a break. */
export interface WholeTextSuggestions {
readonly suggestions: readonly DesktopTaskCheckInput[];
+ /** Kept positioned items associated with the selected text checks. */
+ readonly guards: readonly DesktopTaskCheckInput[];
/** Elements whose text changed that no check took, because every place for one held more text than the limit. */
readonly long: readonly string[];
readonly covers: (pointer: string) => boolean;
@@ -168,6 +171,34 @@ const byOwnAttribute = (element: string, ends: Ends): Candidate | undefined => {
return undefined;
};
+/**
+ * Whether the element at a pointer is a field its tag names in a record of
+ * two or more fields holding text, at both ends, as QElectroTech's ``
+ * beside `` in a ``: a check reads it, not the
+ * record's text, which would join its fields; a Draw shape's one paragraph
+ * beside its geometry is the shape's text.
+ */
+const isField = (pointer: string, ends: Ends) => {
+ const parent = parentOf(pointer);
+ const tag = /^\[name=(?[^\]]+)\]$/u.exec(pointer.split("/").at(-1) ?? "")
+ ?.groups?.tag;
+ return (
+ parent !== undefined &&
+ tag !== undefined &&
+ [ends.baseline, ends.final].every((view) => {
+ const record = resolvePointer(view, parent);
+ const fields = isXmlElement(record)
+ ? record.children.filter(isXmlElement)
+ : [];
+ return (
+ holdsFields(record) &&
+ fields.filter((field) => elementText(field).trim() !== "").length > 1 &&
+ fields.filter(({ name }) => name === tag).length === 1
+ );
+ })
+ );
+};
+
/** How far a text check climbs from an element no attribute names to an ancestor its tag names. */
const MAX_TAG_CLIMB = 3;
/** A formatting run a later save may split, merge, or drop while keeping the paragraph. */
@@ -219,7 +250,8 @@ export const holdsNoisyText = (
* Where a text check goes for an element whose text changed, best first, so
* the check names the paragraph, which a later save keeps, rather than a run
* it may split or merge: up from an element in mixed content (its parent
- * holds text beside it, at either end) to the nearest that is not, then, from
+ * holds text beside it, at either end) to the nearest that is not, which a
+ * field its tag names in a record of fields (`isField`) stays; then, from
* one named by tag or position alone, to the nearest ancestor named by an
* attribute, as AbiWord's `p` by its `xid`, where one is below the document's
* root. An element passed on the way that an identifier of its own names, the
@@ -238,6 +270,8 @@ const outsideRuns = (pointer: string, ends: Ends): readonly Candidate[] => {
parent = parentOf(current)
)
current = parent;
+ if (isField(current, ends))
+ return [{ element: current, pointer: current, identifier: false }];
const passed: string[] = [];
let named = current;
while (!namedByAttribute(named)) {
@@ -342,46 +376,83 @@ const recordLike = (value: JsonValue | undefined): boolean =>
const TEXT_TAG = /^(?:p|h|h[1-6]|para|simpara|li|text|tspan|span|a)$/u;
/**
- * A tag of a formatting run inside a piece of text, compared without its
- * namespace and without case. `link` and `code` stay out: some records name a
- * field that, and a field is not a run.
+ * A tag of inline formatting, by its local name: bold, italic and the like,
+ * a span, link or code run, or Gnote's `size:` and `link:` runs. A record
+ * whose fields are only such, as `olive oil`, is text.
*/
-const FORMAT_TAG =
- /^(?:span|a|s|c|tspan|tab|line-break|soft-page-break|br|b|i|u|em|strong|bold|italic|underline|emphasis|sub|sup|small|font|mark|q|r|t|run|hyperlink)$/u;
+const INLINE_TAG =
+ /^(?:b|i|u|s|em|strong|bold|italic|underline|strikethrough|emphasis|span|a|code|sub|sup|small|big|mark|font|tt|monospace|highlight|link)$/u;
+const INLINE_PREFIX = /^(?:size|link):/u;
-const isFormatTag = (tag: string) =>
- FORMAT_TAG.test((tag.split(":").at(-1) ?? tag).toLowerCase());
+const isInline = (name: string) =>
+ INLINE_PREFIX.test(name) ||
+ INLINE_TAG.test((name.split(":").at(-1) ?? name).toLowerCase());
/**
* Whether an element holds fields: only elements, layout whitespace aside, as
* Tellico's `` holds `` and ``, or a record holds
* repeated `` elements, whatever they are called; unless it is text
- * made of runs: its tag names a paragraph or other text (`TEXT_TAG`), every
- * element child is a formatting run (`FORMAT_TAG`), as a list item
- * `olive oil`, or a child's text opens or closes with
- * whitespace, as a sentence splits into runs, `Meet ` and a linked `Monday`,
- * where a field holds a value whole.
+ * made of runs: its tag names a paragraph or other text (`TEXT_TAG`), its
+ * elements are all inline formatting (`INLINE_TAG`), or a child's text opens
+ * or closes with a space, as a sentence splits into runs, `Meet ` and a
+ * linked `Monday`, where a field holds a value whole; a line break and
+ * indentation the app lays out after a field's text, as KmPlot's, is none.
*/
export const holdsFields = (value: JsonValue | undefined): boolean =>
recordLike(value) &&
isXmlElement(value) &&
!TEXT_TAG.test(value.name.split(":").at(-1) ?? value.name) &&
!value.children.every(
- (child) => !isXmlElement(child) || isFormatTag(child.name),
+ (child) => !isXmlElement(child) || isInline(child.name),
) &&
!value.children.some((child) => {
const text = isXmlElement(child) ? elementText(child) : "";
- return text.trim() !== "" && text.trim() !== text;
+ // A space, not a line break the app lays out, at either edge splits a sentence.
+ return /^[ \t]+\S|\S[ \t]+$/u.test(text);
});
+/** Whether an element at a pointer sits in a paragraph or other text, an ancestor's tag naming one (`TEXT_TAG`), as Word's `` run in its ``. */
+const inText = (view: JsonValue | null, pointer: string): boolean => {
+ for (
+ let parent = parentOf(pointer);
+ parent !== undefined;
+ parent = parentOf(parent)
+ ) {
+ const element = resolvePointer(view, parent);
+ if (
+ isXmlElement(element) &&
+ TEXT_TAG.test(element.name.split(":").at(-1) ?? element.name)
+ )
+ return true;
+ }
+ return false;
+};
+
+/** Whether an element at a pointer is a record: it `holdsFields`, and sits in no paragraph, where it is a run of its text. */
+export const isRecordAt = (
+ view: JsonValue | null,
+ pointer: string,
+ value: JsonValue | undefined,
+): boolean => holdsFields(value) && !inText(view, pointer);
+
/**
* Whether the kept ancestor of a record added or removed whole is a list of
* its kind, whose whole text would fold in records the edit left alone: it
* holds no text of its own, and another element of that tag with text that
* both ends hold alike, as Tellico's `` holds the other entries.
*/
-const listOf = (holder: string, element: JsonValue, ends: Ends) => {
- if (!isXmlElement(element) || !holdsFields(element) || mixedAt(holder, ends))
+const listOf = (
+ holder: string,
+ pointer: string,
+ element: JsonValue,
+ ends: Ends,
+) => {
+ if (
+ !isXmlElement(element) ||
+ !isRecordAt(ends.final, pointer, element) ||
+ !isRecordAt(ends.baseline, pointer, element) ||
+ mixedAt(holder, ends)
+ )
return false;
const like = (view: JsonValue | null) => {
const list = resolvePointer(view, holder);
@@ -431,7 +502,8 @@ const textHolder = (
) => {
const kept = keptAncestor(pointer, ends);
const unnamed = removed && /^\d+$/u.test(pointer.split("/").at(-1) ?? "");
- const listed = kept !== undefined && !unnamed && listOf(kept, element, ends);
+ const listed =
+ kept !== undefined && !unnamed && listOf(kept, pointer, element, ends);
return { holder: listed ? undefined : kept, listed };
};
@@ -439,6 +511,8 @@ const textHolder = (
interface Placed {
readonly element: string;
readonly check: DesktopTaskCheckInput;
+ /** Positioned items can supply checks of their kept siblings. */
+ readonly guards?: readonly DesktopTaskCheckInput[];
}
/** What text checks read: the file, its two ends, and where checks go. */
@@ -448,6 +522,10 @@ interface TextContext {
readonly place: TextPlacement;
}
+/** Whether text is one line the app lays out between line breaks. */
+const isLaidOutLine = (value: string) =>
+ /[\r\n]/u.test(value) && !/[\r\n]/u.test(value.trim());
+
/**
* The check of an element's whole text: a decimal held as the element's one
* text node, as a spreadsheet keeps a cell's number, by its value within a
@@ -460,9 +538,18 @@ export const wholeTextCheck = (
value: string,
before: string | undefined,
): DesktopTaskCheckInput => {
- const numeric = valueEqualsCheck(file, pointer, value, before);
+ // A line between breaks is still that line. Trim it before the decimal test so `\\n437.8\\n` stays a number within tolerance.
+ const comparable = isLaidOutLine(value) ? value.trim() : value;
+ const earlier =
+ before !== undefined && isLaidOutLine(before) ? before.trim() : before;
+ // The element's joined text, not its first text node. A later save may split `437.8` into `437.` and a run holding `8`. When that text is a decimal, compare the number within the usual tolerance, so `437.80` and a long float still match.
+ const numeric = valueEqualsCheck(file, pointer, comparable, earlier);
if ("tolerance" in numeric) return numeric;
- return { kind: "text_equals", file, pointer, value };
+ // One line the app lays out between line breaks, as KmPlot's formula before its next element, compares with its whitespace collapsed.
+ const laidOut = isLaidOutLine(value)
+ ? { value: collapseWhitespace(value), normalize_whitespace: true }
+ : { value };
+ return { kind: "text_equals", file, pointer, ...laidOut };
};
/** The element a candidate names in the final view, whole, at the pointer `name` gives it. */
@@ -482,7 +569,8 @@ const candidateAt = (
* The text check a candidate takes: an element the final view holds whole at
* the pointer `name` gives it, its text at most the limit, no timestamp and
* holding no date stamped in parts, and changed from the baseline's or, for
- * an element `added` whole, absent there.
+ * an element `added` whole, absent there; never a line the app only laid
+ * out anew, which a check comparing collapsed whitespace finds at the seed.
*/
const textCheck = (
context: TextContext,
@@ -494,9 +582,16 @@ const textCheck = (
const { pointer, now } = found;
const before = resolvePointer(context.ends.baseline, pointer);
if (!added && !isXmlElement(before)) return undefined;
+ // A timestamp between other text is not part of the check. Joining across it asks for text the file never contains.
+ if (holdsNoisyText(now)) return undefined;
const value = elementText(now);
const old = isXmlElement(before) ? elementText(before) : undefined;
+ const relaidOut =
+ old !== undefined &&
+ isLaidOutLine(value) &&
+ collapseWhitespace(old) === collapseWhitespace(value);
return value !== old &&
+ !relaidOut &&
value.length <= context.place.limit.chars &&
!isStamp(value) &&
!holdsNoisyText(now) &&
@@ -527,6 +622,8 @@ const textHolders = (change: Listed, ends: Ends, place: TextPlacement) => {
const holders = new Set();
/** Elements added whole holding text, each with the ancestor checked for it, if any. */
const added = new Map();
+ /** Items placed by their own position, added whole. */
+ const items: string[] = [];
const note = (
pointer: string,
view: JsonValue | null,
@@ -549,6 +646,11 @@ const textHolders = (change: Listed, ends: Ends, place: TextPlacement) => {
!isWhitespace(elementText(value)) &&
place.inTree(pointer)
) {
+ // An item placed by its own position is no run of its list's text.
+ if (isPlacedItem(view, pointer)) {
+ if (how === "added") items.push(pointer);
+ return;
+ }
const { holder, listed } = textHolder(
pointer,
value,
@@ -567,7 +669,7 @@ const textHolders = (change: Listed, ends: Ends, place: TextPlacement) => {
}
for (const pointer of change.added) note(pointer, ends.final, "added");
for (const pointer of change.removed) note(pointer, ends.baseline, "removed");
- return { holders, added };
+ return { holders, added, items };
};
/**
@@ -588,7 +690,7 @@ export const wholeTextSuggestions = (
ends: Ends,
place: TextPlacement,
): WholeTextSuggestions => {
- const { holders, added } = textHolders(change, ends, place);
+ const { holders, added, items } = textHolders(change, ends, place);
const context = { file, ends, place };
const checks = new Map();
const check = (candidate: Candidate) => {
@@ -622,6 +724,13 @@ export const wholeTextSuggestions = (
if (found !== undefined && !placed.includes(found)) placed.push(found);
};
for (const holder of holders) keep(climb(holder));
+ for (const item of items)
+ keep(
+ placedItemCheck(
+ { file, ends, name: place.name, chars: place.limit.chars },
+ item,
+ ),
+ );
for (const [element, holder] of added)
if (holder === undefined || climb(holder) === undefined)
keep(
@@ -639,6 +748,7 @@ export const wholeTextSuggestions = (
.slice(0, place.limit.suggestions);
return {
suggestions: suggested.map(({ check }) => check),
+ guards: suggested.flatMap(({ guards }) => guards ?? []),
long: [...long],
covers: (pointer) => {
if (
diff --git a/src/domain/desktop/appTaskDraftTextAdded.test.ts b/src/domain/desktop/appTaskDraftTextAdded.test.ts
index 86344514fe..26d451c0be 100644
--- a/src/domain/desktop/appTaskDraftTextAdded.test.ts
+++ b/src/domain/desktop/appTaskDraftTextAdded.test.ts
@@ -27,7 +27,7 @@ describe("draftTaskFiles for lines added to free text", () => {
expect(
passing(FILE, xml(note(`${LIST}\nolive oil`)), checks).every(Boolean),
).toBe(true);
- // Free text has no item boundary the current checks can select.
+ // Whole free text has no item boundary the current checks can select.
expect(
passing(
FILE,
@@ -67,3 +67,24 @@ describe("draftTaskFiles for lines added to free text", () => {
expect(file.guards).toEqual([]);
});
});
+
+describe("draftTaskFiles for a line added to a note among others, as CherryTree saves them", () => {
+ const notebook = (chores: string) =>
+ `${chores}Oat milkPuzzle book`;
+ const CHORES =
+ "Water the tomato plants\nSweep the back porch\nReplace the hallway bulb\nReturn the library books";
+
+ it("keeps the sibling count guard beside a whole-text check", () => {
+ const file = xmlDraft(
+ notebook(CHORES),
+ notebook(`${CHORES}\nClean the gutters`),
+ );
+ expect(file.guards).toContainEqual({
+ kind: "member_count",
+ file: "Documents/doc.xml",
+ pointer: "/root/children",
+ matching: "[name=node]",
+ equals: 3,
+ });
+ });
+});
diff --git a/src/domain/desktop/appTaskDraftTree.ts b/src/domain/desktop/appTaskDraftTree.ts
new file mode 100644
index 0000000000..87c853679c
--- /dev/null
+++ b/src/domain/desktop/appTaskDraftTree.ts
@@ -0,0 +1,154 @@
+import type { JsonValue } from "../jsonValue.js";
+import type { AddedContext } from "./appTaskDraftAdded.js";
+import { changeNoise } from "./appTaskDraftNoise.js";
+import { draftValues, oneEnd, type DraftValue } from "./appTaskDraftValues.js";
+import { diffNamedMembers } from "./stateChanges.js";
+import {
+ entryWithoutView,
+ zipArrayLeavesOut,
+ zipEntryAt,
+ zipLeavesOutAt,
+ zipView,
+} from "./zipView.js";
+
+const isRecord = (
+ value: JsonValue | null | undefined,
+): value is Record =>
+ typeof value === "object" && value !== null && !Array.isArray(value);
+
+/**
+ * The entry a pointer into a zip view goes into, when it has a view and both
+ * evaluators read it alike: JSON, a key file or XML. A binary or unread
+ * entry also holds a `null` view, which is no JSON `null`.
+ */
+const readableEntry = (view: JsonValue | null, pointer: string) => {
+ const entry = zipEntryAt(view, pointer);
+ return typeof entry === "object" &&
+ entryWithoutView(entry) === undefined &&
+ (entry.format === "json" ||
+ entry.format === "keyfile" ||
+ entry.format === "xml")
+ ? entry
+ : undefined;
+};
+
+/**
+ * Where checks for a pointer the diff lists start: the view of an entry it
+ * names whole, or its root element for XML; the pointer itself inside that;
+ * undefined for an entry neither evaluator reads alike, or the entry's fields.
+ */
+const checkRoot = (
+ view: JsonValue | null,
+ pointer: string,
+): string | undefined => {
+ const entry = readableEntry(view, pointer);
+ const [entryPointer] = /^\/entries\/[^/]+/u.exec(pointer) ?? [];
+ if (entry === undefined || entryPointer === undefined) return undefined;
+ const inner = pointer.slice(entryPointer.length).split("/").slice(1);
+ const root = entry.format === "xml" ? ["view", "root"] : ["view"];
+ const prefix = (short: readonly string[], long: readonly string[]) =>
+ short.length <= long.length &&
+ short.every((part, index) => long[index] === part);
+ if (prefix(inner, root)) return `${entryPointer}/${root.join("/")}`;
+ return prefix(root, inner) ? pointer : undefined;
+};
+
+/**
+ * Where checks into a view can go: an archive's entries, or an XML file's root
+ * element. `root` is where checks for a pointer the diff lists start, undefined
+ * where the evaluators do not read alike; `xml` whether a pointer goes into an
+ * XML view; `leavesOut` whether the view may lack part of what a pointer
+ * reaches, and `arrayLeavesOut` members of the array a pointer names;
+ * `listed` whether a pointer is a value of the document a values list shows;
+ * `empty` the view of a file that holds nothing.
+ */
+export interface CheckTree {
+ readonly root: (
+ view: JsonValue | null,
+ pointer: string,
+ ) => string | undefined;
+ readonly xml: (view: JsonValue | null, pointer: string) => boolean;
+ readonly leavesOut: (view: JsonValue | null, pointer: string) => boolean;
+ readonly arrayLeavesOut: (view: JsonValue | null, array: string) => boolean;
+ readonly listed: (view: JsonValue | null, pointer: string) => boolean;
+ readonly empty: JsonValue | null;
+}
+
+export const ARCHIVE: CheckTree = {
+ root: checkRoot,
+ xml: (view, pointer) => readableEntry(view, pointer)?.format === "xml",
+ leavesOut: zipLeavesOutAt,
+ arrayLeavesOut: zipArrayLeavesOut,
+ // An entry added or removed whole, or a value inside a readable one; not an entry's size or digest.
+ listed: (view, pointer) =>
+ /^\/entries\/[^/]+$/u.test(pointer) ||
+ checkRoot(view, pointer) !== undefined,
+ // An archive with no entries, so each entry of one created or deleted is listed by its name.
+ empty: zipView([], 0),
+};
+
+/** An XML file's view leaves out what its `truncated` says, anywhere in the tree. */
+const truncatedXml = (view: JsonValue | null) =>
+ isRecord(view) && view.truncated === true;
+
+const inXmlRoot = (pointer: string) =>
+ pointer === "/root" || pointer.startsWith("/root/");
+
+export const XML_FILE: CheckTree = {
+ root: (_view, pointer) => (inXmlRoot(pointer) ? pointer : undefined),
+ xml: () => true,
+ leavesOut: truncatedXml,
+ arrayLeavesOut: truncatedXml,
+ listed: (_view, pointer) => inXmlRoot(pointer),
+ empty: null,
+};
+
+export const valueFormat = (
+ tree: CheckTree,
+ view: JsonValue | null,
+ pointer: string,
+) =>
+ tree === ARCHIVE && /^\/entries\/[^/]+$/u.test(pointer)
+ ? ("archive" as const)
+ : tree.xml(view, pointer)
+ ? ("xml" as const)
+ : ("json" as const);
+
+export const referenceRoots = (
+ tree: CheckTree,
+ view: JsonValue | null,
+): JsonValue[] => {
+ if (tree !== ARCHIVE) return view === null ? [] : [view];
+ if (!isRecord(view) || !Array.isArray(view.entries)) return [];
+ return view.entries.flatMap((entry) =>
+ isRecord(entry) &&
+ entry.format === "xml" &&
+ entry.view !== undefined &&
+ entry.view !== null
+ ? [entry.view]
+ : [],
+ );
+};
+
+/**
+ * The values of an archive or XML file created or deleted: what its one end
+ * holds against `tree.empty`, each entry, or the root element, whole once, as
+ * one added to a file that changed is listed, within the same limits.
+ */
+export const presenceValues = (
+ ends: AddedContext["ends"],
+ tree: CheckTree,
+ created: boolean,
+): { readonly values: DraftValue[]; readonly cut: boolean } => {
+ const from = created
+ ? { baseline: tree.empty, final: ends.final }
+ : { baseline: ends.baseline, final: tree.empty };
+ const change = oneEnd(diffNamedMembers(from.baseline, from.final), created);
+ return draftValues(change, {
+ ends: from,
+ listed: tree.listed,
+ noise: changeNoise(change, from, tree.xml),
+ format: (view, pointer) => valueFormat(tree, view, pointer),
+ referenceRoots: (view) => referenceRoots(tree, view),
+ });
+};
diff --git a/src/domain/desktop/appTaskDraftValues.test.ts b/src/domain/desktop/appTaskDraftValues.test.ts
index 197cab5c78..740f64ba19 100644
--- a/src/domain/desktop/appTaskDraftValues.test.ts
+++ b/src/domain/desktop/appTaskDraftValues.test.ts
@@ -1,8 +1,16 @@
import { describe, expect, it } from "vitest";
import type { JsonValue } from "../jsonValue.js";
-import { draftOf, statePath, xml, xmlDraft } from "./appTaskDraft.fixture.js";
-import { compactValue, draftValues } from "./appTaskDraftValues.js";
+import {
+ draftOf,
+ passing,
+ pointerOf,
+ statePath,
+ xml,
+ xmlDraft,
+} from "./appTaskDraft.fixture.js";
+import { draftValues } from "./appTaskDraftValues.js";
+import { compactValue } from "./compactValue.js";
import { resolvePointer } from "./viewPointer.js";
import { zipEntry, zipView } from "./zipView.js";
@@ -24,12 +32,13 @@ it("cuts XML previews after escaping, including entity and surrogate boundaries"
complete.length,
complete.length + 1,
])
- expect(compactValue(element, "xml", limit)).toBe(
+ expect(compactValue(element, limit, "xml")).toBe(
complete.length > limit ? `${complete.slice(0, limit)}…` : complete,
);
expect(
compactValue(
{ name: "n", attributes: { value: "&".repeat(1_000_000) }, children: [] },
+ 600,
"xml",
),
).toBe(`${(' {
const value = { '😀"\n': ["a\n😀\\\ud800"] };
const complete = JSON.stringify(value);
for (let limit = 0; limit <= complete.length + 1; limit += 1)
- expect(compactValue(value, "json", limit)).toBe(
+ expect(compactValue(value, limit, "json")).toBe(
complete.length > limit ? `${complete.slice(0, limit)}…` : complete,
);
});
@@ -58,12 +67,13 @@ describe("draftTaskFiles values", () => {
const file = xmlDraft(before, after);
expect(file.values_cut).toBe(false);
+ // Text first, so no cut ever drops it.
expect(file.values.map(({ pointer, kind }) => [kind, pointer])).toEqual([
- ["added", "/root/children/[name=ope][@wording=Weekly groceries]"],
["changed", "/root/children/[name=note]/children/0"],
+ ["added", "/root/children/[name=ope][@wording=Weekly groceries]"],
["removed", "/root/children/[name=tag]"],
]);
- const [added, changed, removed] = file.values;
+ const [changed, added, removed] = file.values;
expect(added).toMatchObject({ before: null, after: GROCERIES });
expect(removed).toMatchObject({ before: '', after: null });
// A scalar's before and after are what its pointer reads at each end.
@@ -570,3 +580,360 @@ describe("draftTaskFiles values of a file created or deleted", () => {
).toBeLessThanOrEqual(8 * 1024);
});
});
+
+describe("draftTaskFiles for a transaction added to a book, as GnuCash saves one", () => {
+ const GROCERIES = "98f0ee4c216a43e290133637df088843";
+ const CHECKING = "63fd8736eb444c57803945f330e68a28";
+ const account = (name: string, id: string) =>
+ `${name}${id}`;
+ const split = (id: string, value: string, to: string) =>
+ `${id}n${value}${to}`;
+ /** A transaction: its id, currency, dates, description, a slot the app keeps and its splits. */
+ const transaction = (
+ [id, description, posted, entered]: readonly string[],
+ splits: string,
+ ) =>
+ `${id ?? ""}USD${posted ?? ""}${entered ?? ""}${description ?? ""}date-posted${splits}`;
+ const CORNER = transaction(
+ [
+ "1b4e2dae15ea45c5889105a7ae939e04",
+ "Corner Grocer",
+ "2026-10-16 10:59:00 +0000",
+ "2026-11-06 08:49:09 +0000",
+ ],
+ split("0aa0a2a505ac40dd9c74f0abfe7e0ca1", "5275/100", GROCERIES) +
+ split("954a27fff0ad4d54a9a6f26436a60c54", "-5275/100", CHECKING),
+ );
+ const greenleaf = (ids: readonly string[], entered: string) =>
+ transaction(
+ [ids[0] ?? "", "Greenleaf Market", "2026-11-03 10:59:00 +0000", entered],
+ split(ids[1] ?? "", "6348/100", GROCERIES) +
+ split(ids[2] ?? "", "-6348/100", CHECKING),
+ );
+ /** A book: options GnuCash fills on reopening, from control bytes to an id, its accounts and transactions. */
+ const gnucash = (option: string, added: string, slots = "") =>
+ `Default Customer TaxTable${option}${slots}${account("Groceries", GROCERIES)}${account("Checking Account", CHECKING)}${CORNER}${added}`;
+ const SEED = gnucash("??\u007f", "");
+ const END = gnucash(
+ "99471669fefd47b7b6eb2131be4e6d00",
+ greenleaf(
+ [
+ "a893ecc337254562a3b167b4534dc020",
+ "c05c3906b2634257883cbf894462acc1",
+ "d15c3906b2634257883cbf894462acc2",
+ ],
+ "2026-11-06 08:53:08 +0000",
+ ),
+ "remove-color-not-set-slots",
+ );
+ const TRANSACTION =
+ "/root/children/[name=gnc:book]/children/[name=gnc:transaction][>trn:description=Greenleaf Market]";
+
+ it("names the transaction by its description, checks what the user entered under it, counts the transactions, and suggests none of what the app stamps or keeps", () => {
+ const file = xmlDraft(SEED, END);
+
+ expect(
+ file.suggested.map((check) => [
+ check.kind,
+ pointerOf(check)?.replace(TRANSACTION, "~"),
+ ]),
+ ).toEqual([
+ ["text_equals", "~/children/[name=trn:date-posted]"],
+ [
+ "text_equals",
+ "~/children/[name=trn:splits]/children/[name=trn:split][>split:value=6348~1100]/children/[name=split:account]",
+ ],
+ [
+ "text_equals",
+ "~/children/[name=trn:splits]/children/[name=trn:split][>split:value=-6348~1100]/children/[name=split:account]",
+ ],
+ ["member_count", "/root/children/[name=gnc:book]/children"],
+ ]);
+ // Another save of the same entry: new ids, another time entered.
+ const later = gnucash(
+ "11471669fefd47b7b6eb2131be4e6d01",
+ greenleaf(
+ [
+ "b893ecc337254562a3b167b4534dc021",
+ "e05c3906b2634257883cbf894462acc3",
+ "f15c3906b2634257883cbf894462acc4",
+ ],
+ "2026-11-09 14:02:41 +0000",
+ ),
+ );
+ expect(passing("Documents/doc.xml", xml(SEED), file.suggested)).toEqual([
+ false,
+ false,
+ false,
+ false,
+ ]);
+ expect(
+ passing("Documents/doc.xml", xml(later), file.suggested).every(Boolean),
+ ).toBe(true);
+ });
+
+ it("lists the transaction field by field and each split whole, naming the account a split's text refers to, and notes the app's ids, entry time and slots", () => {
+ const file = xmlDraft(SEED, END);
+
+ const listed = file.values.map(({ pointer, note, refers_to }) => [
+ pointer.replace(TRANSACTION, "~"),
+ note ?? null,
+ refers_to?.map(({ element }) => element) ?? null,
+ ]);
+ expect(listed).toEqual(
+ expect.arrayContaining([
+ ["~/children/[name=trn:description]", null, null],
+ [
+ "~/children/[name=trn:splits]/children/[name=trn:split][>split:value=6348~1100]",
+ null,
+ ['Groceries…'],
+ ],
+ [
+ "~/children/[name=trn:splits]/children/[name=trn:split][>split:value=-6348~1100]",
+ null,
+ [
+ 'Checking Account…',
+ ],
+ ],
+ ["~/children/[name=trn:id]", "volatile", null],
+ ["~/children/[name=trn:date-entered]", "timestamp", null],
+ ["~/children/[name=trn:slots]/children/[name=slot]", "upkeep", null],
+ ]),
+ );
+ const slots = file.values.filter(({ pointer }) =>
+ pointer.includes("[name=book:slots]"),
+ );
+ expect(slots.map(({ note }) => note)).toEqual(["upkeep", "upkeep"]);
+ });
+});
+
+/** A split of a transaction: its id, which KMyMoney numbers anew in every transaction, amount, account and payee. */
+const split = (id: string, value: string, account: string, payee: string) =>
+ ``;
+const transaction = (id: string, date: string, splits: string) =>
+ `${splits}`;
+const WATER = transaction(
+ "T000000000000000001",
+ "2026-10-12",
+ split("S0001", "-209/5", "A000001", "P000002") +
+ split("S0002", "209/5", "A000011", "P000002"),
+);
+const ELECTRIC = transaction(
+ "T000000000000000002",
+ "2026-10-20",
+ split("S0001", "-921/10", "A000001", "P000003") +
+ split("S0002", "921/10", "A000011", "P000003"),
+);
+const PAYMENT = transaction(
+ "T000000000000000003",
+ "2026-11-03",
+ split("S0001", "-1749/20", "A000001", "P000003") +
+ split("S0002", "1749/20", "A000011", "P000003"),
+);
+/** A book: a file version whose id is a small number, payees, accounts the institution and parent list by id, and transactions. */
+const kmy = (count: string, added: string) =>
+ `${WATER}${ELECTRIC}${added}`;
+const SEED = kmy("2", "");
+const END = kmy("3", PAYMENT);
+const SPLITS =
+ "/root/children/[name=TRANSACTIONS]/children/[name=TRANSACTION][@postdate=2026-11-03]/children/[name=SPLITS]/children";
+
+describe("draftTaskFiles for a transaction added to a book, as KMyMoney saves one", () => {
+ it("omits an oversized nested split field while retaining short checks and its note", () => {
+ const large = "x".repeat(100_000);
+ const end = kmy(
+ "3",
+ PAYMENT.replace(
+ 'memo="" account="A000001"',
+ `memo="${large}" account="A000001"`,
+ ),
+ );
+ const file = xmlDraft(SEED, end);
+ expect(
+ file.suggested
+ .filter((check) => JSON.stringify(check).length >= 10_000)
+ .map((check) => ({
+ kind: check.kind,
+ pointer: "pointer" in check ? check.pointer.slice(0, 220) : "",
+ })),
+ ).toEqual([]);
+ expect(file.suggested).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ kind: "value_equals", value: "-1749/20" }),
+ ]),
+ );
+ expect(file.notes).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ code: "long_text",
+ pointers: expect.arrayContaining([
+ `${SPLITS}/[name=SPLIT][@account=A000001]/attributes/memo`,
+ ]),
+ }),
+ ]),
+ );
+ });
+
+ it("checks each split of the new transaction by the account it names, its amount and payee, not by the split id every transaction repeats", () => {
+ const file = xmlDraft(SEED, END);
+
+ const field = (account: string, key: string, value: string) => ({
+ kind: "value_equals",
+ file: "Documents/doc.xml",
+ pointer: `${SPLITS}/[name=SPLIT][@account=${account}]/attributes/${key}`,
+ value,
+ });
+ // `shares` repeats `value` in every split, and `memo`, `price` and `reconcileflag` are alike in all.
+ expect(file.suggested.slice(0, 4)).toEqual([
+ field("A000001", "value", "-1749/20"),
+ field("A000001", "payee", "P000003"),
+ field("A000011", "value", "1749/20"),
+ field("A000011", "payee", "P000003"),
+ ]);
+ const checks = file.suggested.slice(0, 4);
+ expect(passing("Documents/doc.xml", xml(SEED), checks)).toEqual([
+ false,
+ false,
+ false,
+ false,
+ ]);
+ expect(passing("Documents/doc.xml", xml(END), checks)).toEqual([
+ true,
+ true,
+ true,
+ true,
+ ]);
+ // A wrong amount fails.
+ const wrong = END.replace('value="1749/20"', 'value="1700/20"');
+ expect(passing("Documents/doc.xml", xml(wrong), checks)).toEqual([
+ true,
+ true,
+ false,
+ true,
+ ]);
+ });
+});
+
+describe("draftTaskFiles for references in KMyMoney transactions", () => {
+ it("names the account and payee each split refers to, the account over an id list naming it too, and no element a plain count equals", () => {
+ const file = xmlDraft(SEED, END);
+
+ const added = file.values.find(({ kind }) => kind === "added");
+ expect(
+ added?.refers_to?.map(({ attribute, value, element }) => [
+ attribute,
+ value,
+ element,
+ ]),
+ ).toEqual([
+ [
+ "account",
+ "A000001",
+ '',
+ ],
+ ["payee", "P000003", ''],
+ ["account", "A000011", ''],
+ ]);
+ // count="3" equals FIXVERSION's id, but `count` names no element.
+ const count = file.values.find(({ pointer }) =>
+ pointer.endsWith("/attributes/count"),
+ );
+ expect(count).toMatchObject({ before: "2", after: "3" });
+ expect(count?.refers_to).toBeUndefined();
+ });
+
+ it("checks a split moved to a new account, though its value equals the id the new account took, so a solve that never moves it fails", () => {
+ const opened = (seed: string) =>
+ seed
+ .replace(
+ '',
+ '',
+ )
+ .replace(
+ '',
+ '',
+ );
+ const moved = opened(SEED).replace(
+ 'account="A000011" price="1/1" id="S0002" payee="P000002"',
+ 'account="A000012" price="1/1" id="S0002" payee="P000002"',
+ );
+ const file = xmlDraft(SEED, moved);
+
+ const account = file.values.find(
+ ({ pointer, after }) =>
+ pointer.endsWith("/attributes/account") && after === "A000012",
+ );
+ expect(account).toBeDefined();
+ expect(account?.note).toBeUndefined();
+ expect(
+ passing("Documents/doc.xml", xml(moved), file.suggested),
+ ).not.toContain(false);
+ // Opening the account alone leaves the water bill where it was.
+ expect(
+ passing("Documents/doc.xml", xml(opened(SEED)), file.suggested),
+ ).toContain(false);
+ });
+});
+
+describe("draftTaskFiles values' other pointers", () => {
+ it("lists a changed value's pointer by each other name its element holds alone, as an Inkscape text's label beside its id", () => {
+ const poster = (headline: string) =>
+ ``;
+
+ const file = xmlDraft(poster("Jazz Night"), poster("Jazz Festival"));
+
+ const [changed] = file.values;
+ expect(changed).toMatchObject({
+ pointer:
+ "/root/children/[name=text][@id=text12]/children/[name=tspan]/children/0",
+ after: "Jazz Festival",
+ alternatives: [
+ "/root/children/[name=text][@inkscape:label=Headline]/children/[name=tspan]/children/0",
+ ],
+ });
+ // Each reads the same value at the end.
+ expect(
+ resolvePointer(
+ xml(poster("Jazz Festival")),
+ changed?.alternatives?.[0] ?? "",
+ ),
+ ).toBe("Jazz Festival");
+ });
+});
+
+describe("draftTaskFiles values in an archive's entries", () => {
+ it("lists a chapter title's pointer by the id its h1 holds, though its tag alone names it", () => {
+ const chapter = (title: string) =>
+ `
${title}
The supply boat brought one letter.
`;
+ const epub = (title: string) => {
+ const entry = zipEntry(
+ {
+ name: "OEBPS/Text/Section0002.xhtml",
+ size: 1,
+ compressedSize: 1,
+ method: 8,
+ encrypted: false,
+ },
+ new TextEncoder().encode(chapter(title)),
+ );
+ return zipView([entry], 1);
+ };
+ const [file] = draftOf({
+ "Documents/book.epub": statePath(
+ "document",
+ "zip",
+ epub("A Letter from the Mainlnad"),
+ epub("A Letter from the Mainland"),
+ ),
+ });
+
+ const h1 =
+ "/entries/[name=OEBPS~1Text~1Section0002.xhtml]/view/root/children/[name=body]/children/[name=h1]";
+ expect(file?.values[0]).toMatchObject({
+ pointer: `${h1}/children/0`,
+ alternatives: [
+ "/entries/[name=OEBPS~1Text~1Section0002.xhtml]/view/root/children/[name=body]/children/[name=h1][@id=chapter-two]/children/0",
+ ],
+ });
+ });
+});
diff --git a/src/domain/desktop/appTaskDraftValues.ts b/src/domain/desktop/appTaskDraftValues.ts
index 786e37cc94..d42bcad0ff 100644
--- a/src/domain/desktop/appTaskDraftValues.ts
+++ b/src/domain/desktop/appTaskDraftValues.ts
@@ -6,9 +6,20 @@ import {
NOISE_REASONS,
type NoiseReason,
} from "./appTaskDraftNoise.js";
-import { isIdentifyingAttribute } from "./attributeRank.js";
+import { isIdentifyingAttribute, isViewStateName } from "./attributeRank.js";
import type { DesktopStateDifference } from "./stateChanges.js";
-import { isXmlElement, locatePointer, resolvePointer } from "./viewPointer.js";
+import { alternativePointers } from "./appTaskDraftAlternatives.js";
+import { isIdReference } from "./appTaskDraftMirrors.js";
+import { recordListsOf } from "./appTaskDraftNested.js";
+import { holdsFields, isRecordAt } from "./appTaskDraftText.js";
+import { attributesOf, compactValue, previewWriter } from "./compactValue.js";
+import { pairMembers, pairedAtSeed } from "./memberPairing.js";
+import {
+ escapeToken,
+ isXmlElement,
+ locatePointer,
+ resolvePointer,
+} from "./viewPointer.js";
/** Why a value's pointer cannot go into a check as it is, or why the value is noise. */
export const VALUE_NOTES = ["by_position", ...NOISE_REASONS] as const;
@@ -31,6 +42,7 @@ export interface DraftValue {
readonly after: JsonValue;
readonly note?: ValueNote;
readonly refers_to?: readonly ValueReference[];
+ readonly alternatives?: readonly string[];
}
/** One entry of a drafted file's `values`, as the draft writes it. */
@@ -52,7 +64,7 @@ export const draftValueSchema = z.strictObject({
.enum(VALUE_NOTES)
.optional()
.describe(
- "by_position: no selector names it, so no check can use the pointer; view_state, renumbered, volatile, timestamp or upkeep: noise the app writes, left out of the suggested checks.",
+ "by_position: no selector names it, so no check can use the pointer; view_state, renumbered, volatile, timestamp, upkeep or layout: noise the app writes, left out of the suggested checks.",
),
refers_to: z
.array(
@@ -66,6 +78,12 @@ export const draftValueSchema = z.strictObject({
.describe(
'Other elements a value names by their key or id, such as category 44 naming .',
),
+ alternatives: z
+ .array(z.string())
+ .optional()
+ .describe(
+ "A changed value's pointer naming its element by another identifier or name it holds alone, such as a shape's draw:name, for a check to use instead; at most three.",
+ ),
});
/** Bounds on the values a draft lists, so a large change cannot crowd out the rest of it. */
@@ -74,7 +92,7 @@ export const VALUE_LIMITS = {
fileBytes: 8 * 1024,
draftBytes: 24 * 1024,
chars: 600,
- references: 3,
+ references: 5,
} as const;
const isRecord = (
@@ -82,154 +100,6 @@ const isRecord = (
): value is Record =>
typeof value === "object" && value !== null && !Array.isArray(value);
-const cut = (text: string, max: number) =>
- text.length > max ? `${text.slice(0, max)}…` : text;
-
-/** An element's attributes; none for a value that holds none. */
-const attributesOf = (element: Record) => {
- const { attributes } = element;
- return isRecord(attributes) ? attributes : {};
-};
-
-/** Keep one character past the limit to distinguish an exact fit from a cut. */
-const previewWriter = (max: number) => {
- const parts: string[] = [];
- let length = 0;
- const push = (text: string): boolean => {
- const kept = text.slice(0, Math.max(0, max + 1 - length));
- parts.push(kept);
- length += kept.length;
- return length <= max;
- };
- const escaped = (text: string, attribute = false): boolean => {
- for (let index = 0; index < text.length; index += 1) {
- const char = text[index] ?? "";
- if (
- !push(
- char === "&"
- ? "&"
- : char === "<"
- ? "<"
- : attribute && char === '"'
- ? """
- : char,
- )
- )
- return false;
- }
- return true;
- };
- const jsonString = (text: string): boolean => {
- if (!push('"')) return false;
- for (let index = 0; index < text.length; index += 1) {
- const code = text.codePointAt(index);
- const width = code !== undefined && code > 0xffff ? 2 : 1;
- const encoded = JSON.stringify(text.slice(index, index + width));
- if (!push(encoded.slice(1, -1))) return false;
- index += width - 1;
- }
- return push('"');
- };
- const startTag = (
- element: Record & { name: string },
- ): boolean => {
- if (!push("<") || !push(element.name)) return false;
- const attributes = attributesOf(element);
- for (const key in attributes) {
- if (!Object.hasOwn(attributes, key)) continue;
- if (
- !push(" ") ||
- !push(key) ||
- !push('="') ||
- !escaped(String(attributes[key]), true) ||
- !push('"')
- )
- return false;
- }
- return true;
- };
- return {
- push,
- escaped,
- jsonString,
- startTag,
- result: () => cut(parts.join(""), max),
- };
-};
-
-/** An archive entry's own fields, without its view. */
-const isArchiveEntry = (value: JsonValue): value is Record =>
- isRecord(value) && "sha256" in value && "view" in value && "name" in value;
-
-/**
- * A value as an author reads it: a scalar as it is, cut past `max`
- * characters; an element as compact XML, with every text node the XML reader
- * kept, which already leaves out the whitespace an app lays out between
- * elements; an archive entry as its name, format and size; anything else as
- * JSON. Writing stops once past `max`, so a large subtree costs no more than
- * the cut.
- */
-export const compactValue = (
- value: JsonValue | undefined,
- format: "json" | "xml" | "archive",
- max: number = VALUE_LIMITS.chars,
-): JsonValue => {
- if (value === undefined) return null;
- if (typeof value === "string") return cut(value, max);
- if (value === null || typeof value !== "object") return value;
- const { push, escaped, jsonString, startTag, result } = previewWriter(max);
- const write = (node: JsonValue): boolean => {
- if (format === "xml" && isXmlElement(node)) {
- if (!startTag(node)) return false;
- if (node.children.length === 0) return push("/>");
- return (
- push(">") &&
- node.children.every((child) =>
- typeof child === "string" ? escaped(child) : write(child),
- ) &&
- push("") &&
- push(node.name) &&
- push(">")
- );
- }
- if (format === "archive" && isArchiveEntry(node))
- return write({
- ...(node.name === undefined ? {} : { name: node.name }),
- format: node.format ?? null,
- size: node.size ?? null,
- });
- if (Array.isArray(node))
- return (
- push("[") &&
- node.every(
- (item, index) => (index === 0 || push(",")) && write(item),
- ) &&
- push("]")
- );
- if (isRecord(node)) {
- if (!push("{")) return false;
- let first = true;
- for (const key in node) {
- if (!Object.hasOwn(node, key)) continue;
- if (
- (!first && !push(",")) ||
- !jsonString(key) ||
- !push(":") ||
- !write(node[key] ?? null)
- )
- return false;
- first = false;
- }
- return push("}");
- }
- return typeof node === "string"
- ? jsonString(node)
- : push(JSON.stringify(node));
- };
- write(value);
- return result();
-};
-
/** The longest value a reference is resolved from. */
const MAX_REFERENCE_CHARS = 64;
@@ -257,22 +127,37 @@ const indexUnder = (
};
/**
- * XML elements by the value of their `key` or `id` attribute, across a view,
- * and by tag: at most `PER_TAG` of a tag in document order, so a key that
- * records or archive entries repeat costs no more than that.
+ * XML elements by the value of their `key` or `id` attribute, `xmi:id`
+ * among them, across a view, and by tag: at most `PER_TAG` of a tag in
+ * document order, so a key that records or archive entries repeat costs no
+ * more than that. A view the app keeps of the document, as Umbrello's
+ * `listview` mirroring its model by id, names nothing.
*/
const referenceIndex = (roots: readonly JsonValue[]) => {
const index = new Map>();
const pending: JsonValue[] = [...roots];
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
- if (isXmlElement(node))
+ if (isXmlElement(node) && isViewStateName(node.name)) continue;
+ if (isXmlElement(node)) {
for (const [key, value] of Object.entries(attributesOf(node)))
if (
typeof value === "string" &&
referable(value) &&
- /^(?:key|id)$/iu.test(key.split(":").at(-1) ?? key)
+ /^(?:key|id)$/iu.test(localOf(key))
)
indexUnder(index, value, node);
+ // An id an app keeps as a child's text, as GnuCash's ``, names the element it sits in.
+ for (const child of node.children) {
+ const id = leafText(child);
+ if (
+ isXmlElement(child) &&
+ /^id$/iu.test(localOf(child.name)) &&
+ id !== undefined &&
+ referable(id)
+ )
+ indexUnder(index, id, node);
+ }
+ }
const children = isXmlElement(node)
? node.children
: Array.isArray(node)
@@ -285,6 +170,53 @@ const referenceIndex = (roots: readonly JsonValue[]) => {
return index;
};
+/** A name without its namespace prefix. */
+const localOf = (name: string) => name.split(":").at(-1) ?? name;
+
+/** An element's text when it holds only one text node, as `Groceries`. */
+const leafText = (node: JsonValue): string | undefined => {
+ const [only] = isXmlElement(node) ? node.children : [];
+ return isXmlElement(node) &&
+ node.children.length === 1 &&
+ typeof only === "string"
+ ? only
+ : undefined;
+};
+
+/** An element as a reference shows it: its start tag, and its name where a child holds one, as `Groceries…`. */
+const referenceTag = (
+ element: Record & { name: string },
+) => {
+ const writer = previewWriter(VALUE_LIMITS.chars);
+ if (!writer.startTag(element)) return writer.result();
+ const children = isXmlElement(element) ? element.children : [];
+ if (children.length === 0) {
+ writer.push("/>");
+ return writer.result();
+ }
+ if (!writer.push(">")) return writer.result();
+ const named = children.find(
+ (child) =>
+ isXmlElement(child) &&
+ /^(?:name|title|label)$/iu.test(localOf(child.name)) &&
+ leafText(child) !== undefined,
+ );
+ if (isXmlElement(named)) {
+ if (
+ !writer.push("<") ||
+ !writer.push(named.name) ||
+ !writer.push(">") ||
+ !writer.escaped(leafText(named) ?? "") ||
+ !writer.push("") ||
+ !writer.push(named.name) ||
+ !writer.push(">")
+ )
+ return writer.result();
+ }
+ writer.push("…");
+ return writer.result();
+};
+
/** Whether an attribute's name and an element's tag share a stem, as HomeBank's `payee` and ``. */
const stemOf = (attribute: string, tag: string) => {
const [left, right] = [attribute, tag].map((name) =>
@@ -298,7 +230,7 @@ const stemOf = (attribute: string, tag: string) => {
);
};
-/** What a value a pointer reads names: the one other element whose `key` or `id` it equals, or, among several, the one whose tag shares the attribute's stem. */
+/** What a value a pointer reads names: the one other element whose `key` or `id` it equals, or, among several, the one whose tag shares the attribute's stem, the one named for it first; a plain number only through an attribute so named, or one ending in `id`, `ref` or `key`. */
const referencesFrom = (
index: ReadonlyMap>,
) => {
@@ -308,15 +240,33 @@ const referencesFrom = (
self: JsonValue | undefined,
): ValueReference | undefined => {
if (!referable(value)) return undefined;
+ // A plain number, as a list's `count="5"`, names an element only through an attribute named for one.
+ const plain = /^-?\d+(?:\.\d+)?$/u.test(value);
const found = [...(index.get(value)?.values() ?? [])]
.flat()
- .filter((node) => node !== self);
- const hinted =
+ .filter(
+ (node) =>
+ node !== self &&
+ (!plain ||
+ (attribute !== undefined &&
+ isXmlElement(node) &&
+ (stemOf(attribute, node.name) ||
+ /(?:id|ref|key)$/iu.test(localOf(attribute))))),
+ );
+ const stemmed =
found.length === 1 || attribute === undefined
? found
: found.filter(
(node) => isXmlElement(node) && stemOf(attribute, node.name),
);
+ // Among several, the tag the attribute is named for, as `account` an `` before an ``.
+ const named = stemmed.filter(
+ (node) =>
+ isXmlElement(node) &&
+ localOf(node.name).toLowerCase() ===
+ localOf(attribute ?? "").toLowerCase(),
+ );
+ const hinted = stemmed.length > 1 && named.length === 1 ? named : stemmed;
const [element] = hinted;
return hinted.length === 1 && isXmlElement(element)
? {
@@ -329,17 +279,15 @@ const referencesFrom = (
return resolve;
};
-const referenceTag = (
- element: Record & { name: string },
-): string => {
- const writer = previewWriter(VALUE_LIMITS.chars);
- if (writer.startTag(element))
- writer.push(
- element.children instanceof Array && element.children.length === 0
- ? "/>"
- : ">…",
- );
- return writer.result();
+/** References without repeats, the first of each attribute and value. */
+const uniqueReferences = (references: readonly ValueReference[]) => {
+ const seen = new Set();
+ return references.filter(({ attribute, value }) => {
+ const key = JSON.stringify([attribute, value]);
+ if (seen.has(key)) return false;
+ seen.add(key);
+ return true;
+ });
};
/** What a values list reads: a file's two ends and how it treats each pointer. */
@@ -362,10 +310,28 @@ export interface ValueContext {
const byteLength = (value: unknown) =>
new TextEncoder().encode(JSON.stringify(value)).length;
+/** Records of attributes an element nests whose references a value lists, as the splits of a transaction. */
+const MAX_NESTED_RECORDS = 10;
+
+/** The references in an element's attributes other than its own identifiers; of one holding nothing but an identifier, as Parley's ``, that identifier's. */
+const attributeReferences = (
+ resolve: ReturnType,
+ element: JsonValue,
+) =>
+ Object.entries(isXmlElement(element) ? element.attributes : {}).flatMap(
+ ([key, item]) =>
+ typeof item === "string" &&
+ ((!isIdentifyingAttribute(key) && !/^key$/iu.test(key)) ||
+ isIdReference(element))
+ ? (resolve(item, key, element) ?? [])
+ : [],
+ );
+
/**
* The elements a value names: each attribute of an element by its own value,
- * other than the element's own identifiers, or a scalar, with the attribute
- * that holds it.
+ * other than the element's own identifiers, and of the records of
+ * attributes it nests, as a KMyMoney split's `account` and `payee`; or a
+ * scalar, with the attribute that holds it. Each once.
*/
const referencesOf = (
resolve: ReturnType,
@@ -374,13 +340,22 @@ const referencesOf = (
): ValueReference[] => {
const value = resolvePointer(view, pointer);
if (isXmlElement(value))
- return Object.entries(attributesOf(value)).flatMap(([key, item]) =>
- typeof item === "string" &&
- !isIdentifyingAttribute(key) &&
- !/^key$/iu.test(key)
- ? (resolve(item, key, value) ?? [])
- : [],
- );
+ return uniqueReferences([
+ ...attributeReferences(resolve, value),
+ ...recordListsOf(value)
+ .flatMap(({ records }) => records)
+ .slice(0, MAX_NESTED_RECORDS)
+ .flatMap((record) => attributeReferences(resolve, record)),
+ // A field holding another element's id as its text, as a split's ``.
+ ...[value, ...value.children].flatMap((field) => {
+ const text = leafText(field);
+ return isXmlElement(field) &&
+ text !== undefined &&
+ !/^id$/iu.test(localOf(field.name))
+ ? (resolve(text, field.name, value) ?? [])
+ : [];
+ }),
+ ]);
if (typeof value !== "string") return [];
const segments = pointer.split("/");
const attribute =
@@ -389,6 +364,15 @@ const referencesOf = (
return [resolve(value, attribute, owner) ?? []].flat();
};
+/** What a pointer names at the seed; an attribute of a member keyed by its new name, as a renamed layer, read from the member it pairs with there. */
+const seedValue = ({ ends }: ValueContext, pointer: string) => {
+ const key = attributeKeyOf(pointer);
+ const seen = resolvePointer(ends.baseline, pointer);
+ if (seen !== undefined || key === undefined) return seen;
+ const member = pairedAtSeed(ends, pointer.split("/").slice(0, -2).join("/"));
+ return isXmlElement(member) ? member.attributes[key] : undefined;
+};
+
/** A value's entry, with the references it holds, before any limit. */
const entryOf = (
context: ValueContext,
@@ -420,6 +404,12 @@ const entryOf = (
VALUE_LIMITS.references,
)
: [];
+ const alternatives =
+ kind === "changed" &&
+ note === undefined &&
+ context.format(view, pointer) === "xml"
+ ? alternativePointers(view, pointer)
+ : [];
return {
pointer,
kind,
@@ -427,7 +417,8 @@ const entryOf = (
kind === "added"
? null
: compactValue(
- resolvePointer(baseline, pointer),
+ seedValue(context, pointer),
+ VALUE_LIMITS.chars,
context.format(baseline, pointer),
),
after:
@@ -435,16 +426,71 @@ const entryOf = (
? null
: compactValue(
resolvePointer(final, pointer),
+ VALUE_LIMITS.chars,
context.format(final, pointer),
),
...(note === undefined ? {} : { note }),
...(refers.length === 0 ? {} : { refers_to: refers }),
+ ...(alternatives.length === 0 ? {} : { alternatives }),
};
};
-/** How an entry sorts: what a check can name first, then what only position reaches, then noise. */
+/**
+ * The pointers a value added whole is listed by: a record of text fields in
+ * a list of its kind, as GnuCash's ``, by each of its
+ * fields, and a field that lists such records, as its ``, by
+ * each record, so the cut on one value never hides a field; anything else
+ * whole, as a document's root or a FreeCAD object of property lists.
+ */
+const fieldsOf = (view: JsonValue | null, pointer: string): string[] => {
+ const value = resolvePointer(view, pointer);
+ const list = resolvePointer(view, pointer.replace(/\/[^/]+$/u, ""));
+ if (
+ !isXmlElement(value) ||
+ !/\/children\/[^/]+$/u.test(pointer) ||
+ !Array.isArray(list) ||
+ list.filter((item) => isXmlElement(item) && item.name === value.name)
+ .length < 2 ||
+ !isRecordAt(view, pointer, value) ||
+ !value.children.some((child) => leafText(child) !== undefined)
+ )
+ return [pointer];
+ const members = (element: JsonValue) => {
+ const { keys } = pairMembers(
+ isXmlElement(element) ? element.children : [],
+ [],
+ ).newer;
+ return (isXmlElement(element) ? element.children : []).flatMap(
+ (child, index) =>
+ isXmlElement(child)
+ ? [{ child, key: escapeToken(keys[index] ?? String(index)) }]
+ : [],
+ );
+ };
+ return members(value).flatMap(({ child, key }) => {
+ const at = `${pointer}/children/${key}`;
+ const records = members(child);
+ return records.length > 0 &&
+ records.every(
+ ({ child: record }) =>
+ record.name === records[0]?.child.name && holdsFields(record),
+ )
+ ? records.map((record) => `${at}/children/${record.key}`)
+ : [at];
+ });
+};
+
+/** How an entry sorts: what a check can name first, text before attributes and whole elements, so a cut never drops the edit; then what only position reaches; then noise. */
const rank = (entry: DraftValue) =>
- entry.note === undefined ? 0 : entry.note === "by_position" ? 1 : 2;
+ entry.note === undefined
+ ? typeof entry.after === "string" &&
+ !entry.after.startsWith("<") &&
+ /\/children\/\d+$/u.test(entry.pointer)
+ ? 0
+ : 1
+ : entry.note === "by_position"
+ ? 2
+ : 3;
/**
* The values a file's net change added, changed and removed, at the pointers
@@ -481,6 +527,11 @@ export const draftValues = (
const entries = [
...change.added
.filter((pointer) => context.listed(final, pointer))
+ .flatMap((pointer) =>
+ context.format(final, pointer) === "xml"
+ ? fieldsOf(final, pointer)
+ : [pointer],
+ )
.map((pointer) => entryOf(context, references, pointer, "added")),
...change.changed
.filter((pointer) => context.listed(final, pointer))
diff --git a/src/domain/desktop/appTaskPython.ts b/src/domain/desktop/appTaskPython.ts
index 5624fabb66..a8b50a59fb 100644
--- a/src/domain/desktop/appTaskPython.ts
+++ b/src/domain/desktop/appTaskPython.ts
@@ -175,7 +175,8 @@ def _xml_text(entry, view, pointer: str, found) -> bool:
def _near(at: str, found, expected, tolerance, join: bool):
"""A value compared within a tolerance: numbers, and numeral strings, by their difference. An element's text is
joined only when join says the view is XML."""
- number = _number(_element_text(found) if join else found)
+ read = _element_text(found) if join else found
+ number = _number(read.strip() if isinstance(read, str) else read)
within = f"within {_shown(tolerance)} of {_shown(expected)}"
if number is None:
return False, f"{at} is {_shown(found)}, not a number"
@@ -327,6 +328,8 @@ def _members(check, entry):
if kind == "unresolved":
return False, f"{at}: {found}"
if kind == "none":
+ if _array_leaves_out(form, view, pointer):
+ return _undecided(check)
return False, f"{at} has no value" if found is None else f"{at}: {found}"
if not isinstance(found, list):
return False, f"{at} is {_shown(found)}, not an array"
diff --git a/src/domain/desktop/appTaskPythonPointer.ts b/src/domain/desktop/appTaskPythonPointer.ts
index 0f9fec80f9..b2e0c15be0 100644
--- a/src/domain/desktop/appTaskPythonPointer.ts
+++ b/src/domain/desktop/appTaskPythonPointer.ts
@@ -41,11 +41,41 @@ def _selector(token: str):
return conditions
+_ISO_STAMP = re.compile(
+ r"^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(?::\d{2}(?:[.,]\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?$", re.ASCII
+)
+_ASCTIME_STAMP = re.compile(
+ r"^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [ 0-3]?\d \d{2}:\d{2}:\d{2} \d{4}$", re.ASCII
+)
+
+
+def _is_saved_time(text: str) -> bool:
+ stripped = text.strip()
+ return _ISO_STAMP.fullmatch(stripped) is not None or _ASCTIME_STAMP.fullmatch(stripped) is not None
+
+
+def _text_without_stamps(node) -> str:
+ """An element's text with save-time leaves removed, matching the selector that named it."""
+ if isinstance(node, str):
+ return "" if _is_saved_time(node) else node
+ if not _is_element(node):
+ return ""
+ return "".join(_text_without_stamps(child) for child in node["children"])
+
+
def _has_child_text(member, key: str, value: str) -> bool:
- """Whether an element has a child element named key whose text, its whitespace collapsed, is value."""
- return _is_element(member) and any(
- _is_element(child) and child["name"] == key and _collapse_whitespace(_element_text(child)) == value
- for child in member["children"])
+ """Whether an element has a child element named key whose text, its whitespace collapsed, is value. A nested
+ save time is not part of the name. A child that is only a time still matches that time."""
+ if not _is_element(member):
+ return False
+ for child in member["children"]:
+ if not _is_element(child) or child["name"] != key:
+ continue
+ plain = _collapse_whitespace(_text_without_stamps(child))
+ text = plain if plain else _collapse_whitespace(_element_text(child))
+ if text == value:
+ return True
+ return False
def _selects(member, conditions) -> bool:
diff --git a/src/domain/desktop/attributeRank.ts b/src/domain/desktop/attributeRank.ts
index d75cc6c4df..50895f1814 100644
--- a/src/domain/desktop/attributeRank.ts
+++ b/src/domain/desktop/attributeRank.ts
@@ -4,12 +4,14 @@ export const isIdentifyingAttribute = (key: string): boolean =>
/**
* Words in a name for how the app showed, printed or kept the document rather
- * than what it holds: its cursor, selection, scroll, zoom, windows, view and
- * configuration settings, printer, and a save's history, edit time and count,
- * generator and statistics.
+ * than what it holds: its cursor, selection (VYM's `