mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 05:07:49 +00:00
fix(desktop): anchor draft text checks through reflow (#13708)
This commit is contained in:
1 parent
ee3d0620f0
commit
7add65ddec
5 files changed
+132
-103
No files matched your search
@@ -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(
|
||||
`<body><section id="headline"><p><marker id="${marker}"/>${runs
|
||||
.map((run) => `<span>${run}</span>`)
|
||||
.join(
|
||||
"",
|
||||
)}</p><p><marker id="Empty continuation"/></p></section><section id="details"><p>Saturday at 7 PM</p></section></body>`,
|
||||
);
|
||||
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) =>
|
||||
|
||||
@@ -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[]) =>
|
||||
`<SCRIBUSUTF8NEW><DOCUMENT><PAGEOBJECT ANNAME="Headline" XPOS="10.5" ItemID="3"><StoryText><DefaultStyle/>${runs
|
||||
const FRAME =
|
||||
"/root/children/[name=DOCUMENT]/children/[name=PAGEOBJECT][@ANNAME=Headline]";
|
||||
|
||||
const flyer = (runs: readonly string[], style?: string) =>
|
||||
`<SCRIBUSUTF8NEW><DOCUMENT><PAGEOBJECT ANNAME="Headline">${style === undefined ? "" : "<Stories>"}<StoryText>${
|
||||
style === undefined ? "<DefaultStyle/>" : `<DefaultStyle id="${style}"/>`
|
||||
}${runs
|
||||
.map(
|
||||
(text, index) =>
|
||||
`<ITEXT FONT="${index === 0 ? "Regular" : "Bold"}" CH="${text}"/>`,
|
||||
)
|
||||
.join(
|
||||
"",
|
||||
)}<para/></StoryText></PAGEOBJECT><PAGEOBJECT ANNAME="Details" XPOS="10.5" ItemID="4"><StoryText><ITEXT CH="Saturday at 7 PM"/></StoryText></PAGEOBJECT></DOCUMENT></SCRIBUSUTF8NEW>`;
|
||||
const FRAME =
|
||||
"/root/children/[name=DOCUMENT]/children/[name=PAGEOBJECT][@ANNAME=Headline]";
|
||||
.join("")}</StoryText>${
|
||||
style === undefined
|
||||
? ""
|
||||
: '<StoryText><DefaultStyle id="Empty continuation"/></StoryText></Stories>'
|
||||
}</PAGEOBJECT><PAGEOBJECT ANNAME="Details"><StoryText><ITEXT CH="Saturday at 7 PM"/></StoryText></PAGEOBJECT></DOCUMENT></SCRIBUSUTF8NEW>`;
|
||||
|
||||
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) =>
|
||||
|
||||
@@ -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 =>
|
||||
/^(?<parent>.+)\/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) &&
|
||||
|
||||
@@ -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 = /\]\[@(?<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,
|
||||
|
||||
@@ -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<string, JsonValue>, key: string) =>
|
||||
|
||||
Reference in new issue
Block a user