From dec2becf9f41a6ec12311bdb37719933ea456e73 Mon Sep 17 00:00:00 2001 From: Ajaypanchal93 Date: Mon, 1 Sep 2025 14:30:43 +0530 Subject: [PATCH] feat: fix form calculation fields, related to #20214 --- src/core/annotation.js | 40 +++--------------------------------- test/unit/annotation_spec.js | 4 ---- 2 files changed, 3 insertions(+), 41 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index eb8b6ba02..1f03ded80 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -1892,12 +1892,9 @@ class WidgetAnnotation extends Annotation { decodedValue = decodedValue.name; } if (Array.isArray(decodedValue)) { - decodedValue = decodedValue[0]; // just pick first if multiple + decodedValue = decodedValue[0]; } - // FILE: src/core/annotation.js - // LOCATION: In the WidgetAnnotation class constructor, around line 2800-2900 - class WidgetAnnotation extends Annotation { constructor(params) { super(params); @@ -1923,34 +1920,19 @@ class WidgetAnnotation extends Annotation { let decodedValue = this._decodeFormValue(fieldValue); - // Handle Name objects, Arrays, and force numeric conversion if (decodedValue instanceof Name) { decodedValue = decodedValue.name; } if (Array.isArray(decodedValue)) { - decodedValue = decodedValue[0]; // just pick first if multiple + decodedValue = decodedValue[0]; } - // REPLACE THIS SECTION: - // OLD CODE: - /* - if (typeof decodedValue === "string") { - // Remove commas from numbers like "37,037.03" - let numericStr = decodedValue.replace(/,/g, ""); - if (!isNaN(numericStr)) { - decodedValue = Number(numericStr); - } - } - */ - - // NEW CODE - REPLACE THE ABOVE SECTION WITH THIS: if (typeof decodedValue === "string" && decodedValue.length > 0) { // Handle potential calculation errors that result in repeating patterns const fixedValue = this._fixRepeatingCalculationValue(decodedValue); if (fixedValue !== null) { decodedValue = fixedValue; } else { - // Fall back to original numeric conversion logic let numericStr = decodedValue.replace(/,/g, ""); if (!isNaN(numericStr) && numericStr !== "") { decodedValue = Number(numericStr); @@ -1959,14 +1941,7 @@ class WidgetAnnotation extends Annotation { } data.fieldValue = decodedValue; - - // ... rest of constructor remains unchanged } - - // ADD THIS NEW METHOD to the WidgetAnnotation class - // LOCATION: Add this method anywhere within the WidgetAnnotation class, - // preferably near other private methods like _decodeFormValue - /** * Fix calculation values that show repeating patterns due to JavaScript execution errors * @private @@ -1974,12 +1949,10 @@ class WidgetAnnotation extends Annotation { * @returns {number|null} - Fixed numeric value or null if no fix needed */ _fixRepeatingCalculationValue(value) { - // Only process strings that look like malformed numbers if (!/^[\d.,]+$/.test(value)) { return null; } - // Pattern 1: Pure repeating digits (e.g., "37037037" -> "37") let match = value.match(/^(\d{1,4})\1{2,}$/); if (match) { const basePattern = match[1]; @@ -1992,15 +1965,12 @@ class WidgetAnnotation extends Annotation { } } - // Pattern 2: Repeating decimal patterns (e.g., "37.037.03" -> "37.0") if (value.includes(".")) { const parts = value.split("."); if (parts.length > 2) { - // Multiple decimal points indicate a calculation error const firstPart = parts[0]; const secondPart = parts[1]; - // Check if the pattern before first decimal repeats if ( firstPart.length > 0 && value.startsWith(firstPart + "." + firstPart) @@ -2015,7 +1985,6 @@ class WidgetAnnotation extends Annotation { } } - // Fallback: use first valid decimal number found const firstDecimal = parts[0] + "." + parts[1]; const numericValue = Number(firstDecimal); if (!isNaN(numericValue)) { @@ -2027,7 +1996,6 @@ class WidgetAnnotation extends Annotation { } } - // Pattern 3: Simple repeating short patterns for longer strings if (value.length >= 6) { for ( let patternLen = 1; @@ -2054,10 +2022,8 @@ class WidgetAnnotation extends Annotation { } } - return null; // No fix needed + return null; } - - // ... rest of the class methods remain unchanged } data.fieldValue = decodedValue; diff --git a/test/unit/annotation_spec.js b/test/unit/annotation_spec.js index bd5e7b8de..63d4d72c9 100644 --- a/test/unit/annotation_spec.js +++ b/test/unit/annotation_spec.js @@ -81,8 +81,6 @@ describe("annotation", function () { return this.ensure(this.pdfDocument, prop, args); } } - // FILE: test/unit/annotation_spec.js - // LOCATION: Add this test section at the end of the file, before the closing }); describe("WidgetAnnotation repeating calculation value fix", function () { let dict, xref, annotationGlobals; @@ -96,7 +94,6 @@ describe("annotation", function () { }); it("should fix pure repeating digit patterns", function () { - // Test case for "37037037" -> 37 dict.set("Subtype", Name.get("Widget")); dict.set("V", "37037037"); @@ -110,7 +107,6 @@ describe("annotation", function () { }); it("should fix repeating decimal patterns", function () { - // Test case for "37.037.03" -> 37 dict.set("Subtype", Name.get("Widget")); dict.set("V", "37.037.03");