fix(desktop): match title filenames at Unicode boundaries

This commit is contained in:
ShuaixinHuang authored and Ysopwnable committed 2026-11-21 20:22:43 +01:00
1 parent f85fae63e1
commit d07d28b533
2 files changed
+26 -4

No files matched your search

+19
View File
@@ -139,6 +139,25 @@ describe("stateReasons", () => {
expect(reasons("/home/ada/Documents/today.tx")).toEqual([]);
});
it.each(["𐐀notes.txt", "notes.txt𐐀"])(
"does not attribute an opened file to a title naming %s",
(name) => {
const path = "/home/ada/notes.txt";
const facts: DesktopRoleFacts = {
home: "/home/ada",
stateDirectories: [],
stateTails: [],
selectedRoots: [],
selections: [],
titles: [`${name} - Editor`],
openedFiles: new Set([path]),
};
expect(stateReasons(facts, path, [], false)).toEqual([
"opened_by_application",
]);
},
);
it("names a file by its whole name or path, never by a folder segment, a cache or a dconf database", () => {
// CherryTree shows the document's folder, whose path holds the segment "user".
const cherry = [
+7 -4
View File
@@ -204,6 +204,8 @@ export const roleFacts = (
};
const FILENAME_CHARACTER = /[\p{L}\p{M}\p{N}._~-]/u;
const BEFORE_FILENAME = new RegExp(`${FILENAME_CHARACTER.source}$`, "u");
const AFTER_FILENAME = new RegExp(`^${FILENAME_CHARACTER.source}`, "u");
/** Home folders whose files a title never names as a document: caches, and dconf's databases. */
const NEVER_NAMED = [".cache", ".config/dconf"];
@@ -220,11 +222,12 @@ const spelledIn = (title: string, spelling: string, bare = false): boolean => {
at = title.indexOf(spelling, at + 1)
) {
const before = title[at - 1];
const after = title[at + spelling.length];
const end = at + spelling.length;
// A neighboring Unicode code point can occupy two UTF-16 code units.
if (
(before === undefined || !FILENAME_CHARACTER.test(before)) &&
(after === undefined || !FILENAME_CHARACTER.test(after)) &&
after !== "/" &&
!BEFORE_FILENAME.test(title.slice(Math.max(0, at - 2), at)) &&
!AFTER_FILENAME.test(title.slice(end, end + 2)) &&
title[end] !== "/" &&
!(bare && before === "/")
)
return true;