refactor(desktop): share sibling text guard checks (#16504)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-04 07:16:53 +08:00
1 parent e6caca151f
commit a8d6a9f439
1 file changed
+24 -34
+24 -34
View File
@@ -62,57 +62,47 @@ const guardAt = (
const before = resolvePointer(ends.baseline, pointer, "xml");
const after = resolvePointer(ends.final, pointer, "xml");
if (before === undefined || after === undefined) return undefined;
const textCheck = check.kind === "text_equals";
const numericCheck = check.kind === "value_equals" && "tolerance" in check;
if (
check.kind === "value_equals" &&
"tolerance" in check &&
(textCheck || numericCheck) &&
isXmlElement(before) &&
isXmlElement(after)
) {
const value = elementText(before);
if (
value.trim() === "" ||
value !== elementText(after) ||
value.length > context.chars ||
holdsNoisyText(before) ||
holdsNoisyText(after) ||
holdsSplitDate(ends.baseline, pointer) ||
holdsSplitDate(ends.final, pointer) ||
isStamp(value)
)
return undefined;
return decimalOf(value) === undefined
? { kind: "text_equals", file, pointer, value }
: valueEqualsCheck(file, pointer, value);
}
if (check.kind === "text_equals") {
if (!isXmlElement(before) || !isXmlElement(after)) return undefined;
const attribute = check.text_attribute;
const attribute = textCheck ? check.text_attribute : undefined;
// A check comparing collapsed whitespace has guards that do too.
const collapsed = check.normalize_whitespace === true;
const collapsed = textCheck && check.normalize_whitespace === true;
const read = (element: JsonValue) => {
const text = elementText(element, attribute);
return collapsed ? collapseWhitespace(text) : text;
};
const value = read(before);
return value.trim() !== "" &&
value === read(after) &&
value.length <= context.chars &&
(attribute !== undefined ||
(!holdsNoisyText(before) &&
!holdsNoisyText(after) &&
!holdsSplitDate(ends.baseline, pointer) &&
!holdsSplitDate(ends.final, pointer))) &&
!isStamp(value)
? {
if (
value.trim() === "" ||
value !== read(after) ||
(numericCheck
? value.length > context.chars
: !(value.length <= context.chars)) ||
(attribute === undefined &&
(holdsNoisyText(before) ||
holdsNoisyText(after) ||
holdsSplitDate(ends.baseline, pointer) ||
holdsSplitDate(ends.final, pointer))) ||
isStamp(value)
)
return undefined;
return numericCheck && decimalOf(value) !== undefined
? valueEqualsCheck(file, pointer, value)
: {
kind: "text_equals",
file,
pointer,
value,
...(attribute === undefined ? {} : { text_attribute: attribute }),
...(collapsed ? { normalize_whitespace: true } : {}),
}
: undefined;
};
}
if (textCheck) return undefined;
return typeof before !== "object" &&
equalJsonValues(before, after) &&
!isStamp(before) &&