From 57274a7cdff6da87d4d84a01eb6720c3cbe2c349 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 3 Dec 2026 19:44:05 +0800 Subject: [PATCH] refactor(desktop): share XML selector container checks (#16329) --- src/domain/desktop/selectorCandidates.ts | 44 ++++++++++-------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/domain/desktop/selectorCandidates.ts b/src/domain/desktop/selectorCandidates.ts index b81b0c3c3bf..6879e0e8de7 100644 --- a/src/domain/desktop/selectorCandidates.ts +++ b/src/domain/desktop/selectorCandidates.ts @@ -287,28 +287,20 @@ const keptIdentifier = ( : undefined; }; -/** Whether an element holds elements and no text of its own, as Gnumeric's `gnm:Sheet` its name and cells. */ -const holdsElements = (member: JsonValue) => { +/** Root containers hold repeated tags; nested members can hold any elements. */ +const holdsChildren = (member: JsonValue, nested: boolean) => { const children = isRecord(member) ? own(member, "children") : undefined; - const all = Array.isArray(children) ? children : []; - return ( - all.some((child) => isRecord(child)) && - all.every((child) => typeof child !== "string" || child.trim() === "") - ); -}; - -/** Whether an element holds a list, two or more elements of one tag and no text of its own, as Dia's layer its boxes. */ -const isContainer = (member: JsonValue) => { - const children = isRecord(member) ? own(member, "children") : undefined; - const all = Array.isArray(children) ? children : []; - const tags = all.flatMap((child) => { + if ( + !Array.isArray(children) || + children.some((child) => typeof child === "string" && child.trim() !== "") + ) + return false; + if (nested) return children.some((child) => isRecord(child)); + const tags = children.flatMap((child) => { const tag = isRecord(child) ? own(child, "name") : undefined; return typeof tag === "string" ? [tag] : []; }); - return ( - new Set(tags).size < tags.length && - all.every((child) => typeof child !== "string" || child.trim() === "") - ); + return new Set(tags).size < tags.length; }; /** @@ -327,10 +319,10 @@ export const holdingCondition = ( older: JsonValue, nested = false, ): string | undefined => { + if (!holdsChildren(newer, nested)) return undefined; const [after, before] = [attributesOf(newer), attributesOf(older)]; - const identifiers = (nested ? holdsElements : isContainer)(newer) - ? Object.keys(after).filter(isIdentifyingAttribute) - : []; + const identifiers = Object.keys(after).filter(isIdentifyingAttribute); + if (!nested && identifiers.length === 0) return undefined; const named = (key: string) => key.toLowerCase() === "name"; const theirs = new Set(childConditions(older)); const children = childConditions(newer).filter( @@ -340,9 +332,9 @@ export const holdingCondition = ( ); const kept = (keys: readonly string[]) => keys.flatMap((key) => keptIdentifier(after, before, key) ?? [])[0]; - return identifiers.length === 0 && !(nested && holdsElements(newer)) - ? undefined - : (kept(identifiers.filter((key) => !named(key))) ?? - children[0] ?? - kept(identifiers.filter(named))); + return ( + kept(identifiers.filter((key) => !named(key))) ?? + children[0] ?? + kept(identifiers.filter(named)) + ); };