From d07d28b53348b820a8a3769b5b1fd45f8096015d Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:35:18 +0000 Subject: [PATCH] fix(desktop): match title filenames at Unicode boundaries --- src/domain/desktop/stateRoles.test.ts | 19 +++++++++++++++++++ src/domain/desktop/stateRoles.ts | 11 +++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/domain/desktop/stateRoles.test.ts b/src/domain/desktop/stateRoles.test.ts index 87efc24945..fa2a05aded 100644 --- a/src/domain/desktop/stateRoles.test.ts +++ b/src/domain/desktop/stateRoles.test.ts @@ -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 = [ diff --git a/src/domain/desktop/stateRoles.ts b/src/domain/desktop/stateRoles.ts index ebd95b9c36..b86e8abd64 100644 --- a/src/domain/desktop/stateRoles.ts +++ b/src/domain/desktop/stateRoles.ts @@ -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;