mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
refactor(desktop): share XML selector container checks (#16329)
This commit is contained in:
1 parent
dd20f6d42b
commit
57274a7cdf
1 file changed
+18
-26
@@ -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))
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user