From 7add65ddec97017d7a7d7ba83057aeb9eb529e4d Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 18:41:58 -0500 Subject: [PATCH] fix(desktop): anchor draft text checks through reflow (#13708) --- src/domain/desktop/appTaskDraft.test.ts | 30 ++++ .../desktop/appTaskDraftAttributeText.test.ts | 141 +++++++++--------- .../desktop/appTaskDraftAttributeText.ts | 27 ++-- src/domain/desktop/appTaskDraftText.ts | 31 ++-- src/domain/desktop/viewPointer.ts | 6 + 5 files changed, 132 insertions(+), 103 deletions(-) diff --git a/src/domain/desktop/appTaskDraft.test.ts b/src/domain/desktop/appTaskDraft.test.ts index fc7ab282626..91083861285 100644 --- a/src/domain/desktop/appTaskDraft.test.ts +++ b/src/domain/desktop/appTaskDraft.test.ts @@ -7,6 +7,7 @@ import type { JsonValue } from "../jsonValue.js"; import type { DesktopTaskCheck } from "./appTask.js"; import { evaluateTaskChecks } from "./appTaskChecks.js"; import { draftTaskFiles, type TaskDraftFile } from "./appTaskDraft.js"; +import { xml } from "./appTaskDraft.fixture.js"; import { leftOutFiles } from "./appExportUnowned.js"; import type { DesktopStatePath } from "./desktopState.js"; import { xmlView } from "./stateFormats.js"; @@ -485,6 +486,35 @@ describe("draftTaskFiles for text split into runs", () => { } }); + it("keeps a text check through reflow when a nested marker contains selector-like text", () => { + const file = "Documents/headline.xml"; + const note = (runs: readonly string[], marker = "Headline[@draft=first") => + xml( + `

${runs + .map((run) => `${run}`) + .join( + "", + )}

Saturday at 7 PM

`, + ); + const seed = note(["Spring ", "Market"]); + const saved = note(["Summer ", "Fair"]); + const [document] = draft({ [file]: path("document", "xml", seed, saved) }); + + const checks = document?.suggested; + expect(checks).toEqual([ + { + kind: "text_equals", + file, + pointer: "/root/children/[name=section][@id=headline]", + value: "Summer Fair", + }, + ]); + expect( + passing(file, checks, note(["Summer Fair"], "Updated style")), + ).toEqual([true]); + expect(passing(file, checks, seed)).toEqual([false]); + }); + it("checks a rewritten archive paragraph and ignores formatting-only changes beside it", () => { const file = "Documents/list.odt"; const span = (text: string) => diff --git a/src/domain/desktop/appTaskDraftAttributeText.test.ts b/src/domain/desktop/appTaskDraftAttributeText.test.ts index 1f84336ca31..7a6f5bb2fdf 100644 --- a/src/domain/desktop/appTaskDraftAttributeText.test.ts +++ b/src/domain/desktop/appTaskDraftAttributeText.test.ts @@ -5,86 +5,79 @@ import { passing, pointerOf, xml, xmlDraft } from "./appTaskDraft.fixture.js"; const FILE = "Documents/doc.xml"; describe("draftTaskFiles for text an app keeps in an attribute", () => { - /** A flyer's frame as Scribus saves it: its text in `ITEXT` runs, one per run of formatting. */ - const flyer = (...runs: string[]) => - `${runs + const FRAME = + "/root/children/[name=DOCUMENT]/children/[name=PAGEOBJECT][@ANNAME=Headline]"; + + const flyer = (runs: readonly string[], style?: string) => + `${style === undefined ? "" : ""}${ + style === undefined ? "" : `` + }${runs .map( (text, index) => ``, ) - .join( - "", - )}`; - const FRAME = - "/root/children/[name=DOCUMENT]/children/[name=PAGEOBJECT][@ANNAME=Headline]"; + .join("")}${ + style === undefined + ? "" + : '' + }`; - it("checks the frame's text read from its runs' CH, which holds however a later save splits them", () => { - const before = flyer("Spring ", "Fair"); - const after = flyer("Summer ", "Fair"); - - const file = xmlDraft(before, after); - - expect(file.suggested).toEqual([ - { - kind: "text_equals", - file: FILE, - pointer: FRAME, - value: "Summer Fair", - text_attribute: "CH", - }, - ]); - expect( - [ - before, - after, - flyer("Summer Fair"), - flyer("Sum", "mer ", "Fair"), - flyer("Summer ", "Fete"), - ].map((text) => passing(FILE, xml(text), file.suggested)), - ).toEqual([[false], [true], [true], [true], [false]]); - const checks = [...file.suggested, ...file.guards]; - expect( - file.guards.some( - (guard) => - "matching" in guard && String(guard.matching).includes("ITEXT"), - ), - ).toBe(false); - expect( - passing(FILE, xml(flyer("Summer Fair")), checks).every(Boolean), - ).toBe(true); - expect( - passing(FILE, xml(flyer("Sum", "mer ", "Fair")), checks).every(Boolean), - ).toBe(true); - }); - - it("does not count formatting runs when the edit splits or merges them", () => { - for (const [before, after] of [ - [flyer("Spring Fair"), flyer("Summer ", "Fair")], - [flyer("Spring ", "Fair"), flyer("Summer Fair")], - ] as const) { - const file = xmlDraft(before, after); - const checks = [...file.suggested, ...file.guards]; - expect( - checks.some( - (check) => - check.kind === "text_equals" && check.text_attribute === "CH", - ), - ).toBe(true); - expect(passing(FILE, xml(before), file.suggested).every(Boolean)).toBe( - false, + it.each([ + { + edit: "keeps runs", + before: ["Spring ", "Fair"], + after: ["Summer ", "Fair"], + }, + { + edit: "splits runs", + before: ["Spring Fair"], + after: ["Summer ", "Fair"], + }, + { + edit: "merges runs", + before: ["Spring ", "Fair"], + after: ["Summer Fair"], + }, + { + edit: "keeps a style with literal selector text", + before: ["Spring ", "Fair"], + after: ["Summer ", "Fair"], + style: "Headline[@draft=first", + }, + ])( + "checks the frame's text when an edit $edit", + ({ before, after, style }) => { + const seed = flyer(before, style); + const saved = flyer(after, style); + const file = xmlDraft(seed, saved); + expect(file.suggested).toEqual([ + { + kind: "text_equals", + file: FILE, + pointer: FRAME, + value: "Summer Fair", + text_attribute: "CH", + }, + ]); + expect(file.guards).not.toContainEqual( + expect.objectContaining({ + kind: "member_count", + matching: expect.stringContaining("ITEXT"), + }), ); - expect(passing(FILE, xml(after), checks).every(Boolean)).toBe(true); - expect( - passing(FILE, xml(flyer("Summer Fair")), checks).every(Boolean), - ).toBe(true); - expect( - passing(FILE, xml(flyer("Sum", "mer ", "Fair")), checks).every(Boolean), - ).toBe(true); - expect( - passing(FILE, xml(flyer("Summer Fete")), file.suggested).every(Boolean), - ).toBe(false); - } - }); + const restyled = style === undefined ? undefined : "Updated style"; + const checks = [...file.suggested, ...file.guards]; + for (const followUp of [ + saved, + flyer(["Summer Fair"], restyled), + flyer(["Sum", "mer ", "Fair"], restyled), + ]) { + expect(passing(FILE, xml(followUp), checks)).not.toContain(false); + } + for (const wrong of [seed, flyer(["Summer Fete"], restyled)]) + expect(passing(FILE, xml(wrong), file.suggested)).toEqual([false]); + }, + ); it("keeps a check of a run's formatting the text check does not read, beside it", () => { const styled = (first: string, font: string) => diff --git a/src/domain/desktop/appTaskDraftAttributeText.ts b/src/domain/desktop/appTaskDraftAttributeText.ts index 35fe1a39afe..87e55500a3d 100644 --- a/src/domain/desktop/appTaskDraftAttributeText.ts +++ b/src/domain/desktop/appTaskDraftAttributeText.ts @@ -8,7 +8,12 @@ import { } from "./appTaskDraftNoise.js"; import { isIdentifyingAttribute, isNameLikeKey } from "./attributeRank.js"; import type { DesktopStateDifference } from "./stateChanges.js"; -import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js"; +import { + elementText, + isXmlElement, + lastSelectorOf, + resolvePointer, +} from "./viewPointer.js"; /** How far a text check climbs from a run to the element an attribute names. */ const MAX_CLIMB = 3; @@ -22,10 +27,6 @@ const isFormattingAttribute = (key: string) => const parentOf = (pointer: string): string | undefined => /^(?.+)\/children\/[^/]+$/u.exec(pointer)?.groups?.parent; -/** Whether a pointer's last step names its element by an attribute, as `[name=PAGEOBJECT][@ANNAME=Headline]`. */ -const namedByAttribute = (pointer: string) => - (pointer.split("/").at(-1) ?? "").includes("[@"); - /** An element's text from its text nodes, or from an attribute's values, at a pointer in a view. */ const textAt = ( view: JsonValue | null, @@ -129,16 +130,14 @@ export const attributeTextCheck = ( isStamp(value) ) return undefined; - let at = parentOf(owner); - for ( - let climbed = 1; - at !== undefined && !namedByAttribute(at) && climbed < MAX_CLIMB; - climbed += 1 - ) - at = parentOf(at); - if (at === undefined || parentOf(at) === undefined || !namedByAttribute(at)) + let ancestor = parentOf(owner); + for (let climbed = 1; ancestor !== undefined; climbed += 1) { + if (lastSelectorOf(ancestor).some(({ prefix }) => prefix === "@")) break; + if (climbed === MAX_CLIMB) return undefined; + ancestor = parentOf(ancestor); + } + if (ancestor === undefined || parentOf(ancestor) === undefined) return undefined; - const ancestor = at; const after = textAt(ends.final, ancestor, key); return holdsRuns(context, ancestor, key) && context.named(ancestor) && diff --git a/src/domain/desktop/appTaskDraftText.ts b/src/domain/desktop/appTaskDraftText.ts index 36c2e0bdbf4..534787845ef 100644 --- a/src/domain/desktop/appTaskDraftText.ts +++ b/src/domain/desktop/appTaskDraftText.ts @@ -11,7 +11,12 @@ import { isPlacedItem, placedItemCheck } from "./appTaskDraftItems.js"; import { valueEqualsCheck } from "./appTaskDraftScalars.js"; import { rankAttributes } from "./attributeRank.js"; import type { DesktopStateDifference } from "./stateChanges.js"; -import { elementText, isXmlElement, resolvePointer } from "./viewPointer.js"; +import { + elementText, + isXmlElement, + lastSelectorOf, + resolvePointer, +} from "./viewPointer.js"; import { isRecord } from "../../kernel/isRecord.js"; /** The pointers a diff lists, of every kind. */ @@ -76,10 +81,6 @@ const parentBelowRoot = (pointer: string): string | undefined => { : parent; }; -/** Whether the diff names an element by one of its attributes, as `[name=p][@xid=6]`, rather than by its tag alone or its position. */ -const namedByAttribute = (pointer: string) => - (pointer.split("/").at(-1) ?? "").includes("[@"); - /** An element's attributes; none for any other value. */ const attributesOf = (value: JsonValue | undefined) => isXmlElement(value) && isRecord(value.attributes) ? value.attributes : {}; @@ -87,13 +88,6 @@ const attributesOf = (value: JsonValue | undefined) => /** Whether an attribute identifies its element, as `id`, `xml:id` or AbiWord's `xid` do, rather than formatting it. */ const isIdentifier = (key: string) => /^(?:[^:]*:)?x?id$/iu.test(key); -/** Whether the diff names an element by an identifier, as `[name=p][@xid=6]`. */ -const namedByIdentifier = (pointer: string) => { - const key = /\]\[@(?[^=]+)=/u.exec(pointer.split("/").at(-1) ?? "") - ?.groups?.key; - return key !== undefined && isIdentifier(key); -}; - /** * Whether an end holds an element whose text a check reads as nothing, as an * image or a break. A run of only whitespace is text the paragraph check @@ -271,7 +265,8 @@ const outsideRuns = (pointer: string, ends: Ends): readonly Candidate[] => { return [{ element: current, pointer: current, identifier: false }]; const passed: string[] = []; let named = current; - while (!namedByAttribute(named)) { + let conditions = lastSelectorOf(named); + while (!conditions.some(({ prefix }) => prefix === "@")) { const parent = parentBelowRoot(named); if (parent === undefined) { const stopped = parentOf(named); @@ -300,6 +295,7 @@ const outsideRuns = (pointer: string, ends: Ends): readonly Candidate[] => { } passed.push(named); named = parent; + conditions = lastSelectorOf(named); } const noisy = (element: string) => holdsNoisyText(resolvePointer(ends.final, element, "xml")); @@ -320,7 +316,13 @@ const outsideRuns = (pointer: string, ends: Ends): readonly Candidate[] => { })); } const ranked = [ - { element: named, pointer: named, identifier: namedByIdentifier(named) }, + { + element: named, + pointer: named, + identifier: conditions.some( + ({ prefix, key }) => prefix === "@" && isIdentifier(key), + ), + }, ...passed .toReversed() .flatMap((element) => byOwnAttribute(element, ends) ?? []), @@ -594,7 +596,6 @@ const textCheck = ( !relaidOut && value.length <= context.place.limit.chars && !isStamp(value) && - !holdsNoisyText(now) && !holdsSplitDate(context.ends.final, pointer) ? { element: candidate.element, diff --git a/src/domain/desktop/viewPointer.ts b/src/domain/desktop/viewPointer.ts index 01b5b4c7827..5b0db00c47e 100644 --- a/src/domain/desktop/viewPointer.ts +++ b/src/domain/desktop/viewPointer.ts @@ -32,6 +32,12 @@ type Stepped = | { readonly kind: "none"; readonly reason?: string } | { readonly kind: "unresolved"; readonly problem: string }; +/** The conditions in a pointer's last reference token, when it is a selector. */ +export const lastSelectorOf = (pointer: string): readonly Condition[] => + selectorOf( + unescapeJsonPointerToken(pointer.slice(pointer.lastIndexOf("/") + 1)), + ) ?? []; + const INDEX = /^(?:0|[1-9][0-9]*)$/u; const own = (record: Record, key: string) =>