From a8d6a9f439aed970a458e6e61575dd9dad7879e2 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 07:16:53 +0800 Subject: [PATCH] refactor(desktop): share sibling text guard checks (#16504) --- src/domain/desktop/appTaskDraftGuards.ts | 58 ++++++++++-------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/domain/desktop/appTaskDraftGuards.ts b/src/domain/desktop/appTaskDraftGuards.ts index 01d1890536a..3a906b95f29 100644 --- a/src/domain/desktop/appTaskDraftGuards.ts +++ b/src/domain/desktop/appTaskDraftGuards.ts @@ -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) &&