-
Notifications
You must be signed in to change notification settings - Fork 10.5k
feat: fix form calculation fields, related to #20214 #20223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ajaypanchal4326
wants to merge
5
commits into
mozilla:master
Choose a base branch
from
Ajaypanchal4326:fix-repeating-calculation-values
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c0e5da
feat: fix form calculation fields
Ajaypanchal4326 dec2bec
feat: fix form calculation fields, related to #20214
Ajaypanchal4326 10caf30
class issue solved
Ajaypanchal4326 ca937e5
Update yarn.lock
Ajaypanchal4326 01915d4
Delete yarn.lock
Ajaypanchal4326 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1884,7 +1884,148 @@ class WidgetAnnotation extends Annotation { | |
| key: "V", | ||
| getArray: true, | ||
| }); | ||
| data.fieldValue = this._decodeFormValue(fieldValue); | ||
|
|
||
| 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]; | ||
| } | ||
|
|
||
| class WidgetAnnotation extends Annotation { | ||
| constructor(params) { | ||
| super(params); | ||
|
|
||
| const { dict, xref, annotationGlobals } = params; | ||
| const data = this.data; | ||
| this._needAppearances = params.needAppearances; | ||
|
|
||
| data.annotationType = AnnotationType.WIDGET; | ||
| if (data.fieldName === undefined) { | ||
| data.fieldName = this._constructFieldName(dict); | ||
| } | ||
|
|
||
| if (data.actions === undefined) { | ||
| data.actions = collectActions(xref, dict, AnnotationActionEventType); | ||
| } | ||
|
|
||
| let fieldValue = getInheritableProperty({ | ||
| dict, | ||
| key: "V", | ||
| getArray: true, | ||
| }); | ||
|
|
||
| let decodedValue = this._decodeFormValue(fieldValue); | ||
|
|
||
| if (decodedValue instanceof Name) { | ||
| decodedValue = decodedValue.name; | ||
| } | ||
| if (Array.isArray(decodedValue)) { | ||
| decodedValue = decodedValue[0]; | ||
| } | ||
|
|
||
| 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 { | ||
| let numericStr = decodedValue.replace(/,/g, ""); | ||
| if (!isNaN(numericStr) && numericStr !== "") { | ||
| decodedValue = Number(numericStr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| data.fieldValue = decodedValue; | ||
| } | ||
| /** | ||
| * Fix calculation values that show repeating patterns due to JavaScript execution errors | ||
|
||
| * @private | ||
| * @param {string} value - The potentially malformed calculation result | ||
| * @returns {number|null} - Fixed numeric value or null if no fix needed | ||
| */ | ||
| _fixRepeatingCalculationValue(value) { | ||
| if (!/^[\d.,]+$/.test(value)) { | ||
| return null; | ||
| } | ||
|
|
||
| let match = value.match(/^(\d{1,4})\1{2,}$/); | ||
| if (match) { | ||
| const basePattern = match[1]; | ||
| const numericValue = Number(basePattern); | ||
| if (!isNaN(numericValue)) { | ||
| warn( | ||
| `PDF.js: Fixed repeating calculation value "${value}" -> "${basePattern}"` | ||
| ); | ||
| return numericValue; | ||
| } | ||
| } | ||
|
|
||
| if (value.includes(".")) { | ||
| const parts = value.split("."); | ||
| if (parts.length > 2) { | ||
| const firstPart = parts[0]; | ||
| const secondPart = parts[1]; | ||
|
|
||
| if ( | ||
| firstPart.length > 0 && | ||
| value.startsWith(firstPart + "." + firstPart) | ||
| ) { | ||
| const candidate = firstPart; | ||
| const numericValue = Number(candidate); | ||
| if (!isNaN(numericValue)) { | ||
| warn( | ||
| `PDF.js: Fixed repeating decimal calculation "${value}" -> "${candidate}"` | ||
| ); | ||
| return numericValue; | ||
| } | ||
| } | ||
|
|
||
| const firstDecimal = parts[0] + "." + parts[1]; | ||
| const numericValue = Number(firstDecimal); | ||
| if (!isNaN(numericValue)) { | ||
| warn( | ||
| `PDF.js: Fixed multiple decimal calculation "${value}" -> "${firstDecimal}"` | ||
| ); | ||
| return numericValue; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (value.length >= 6) { | ||
| for ( | ||
| let patternLen = 1; | ||
| patternLen <= Math.floor(value.length / 3); | ||
| patternLen++ | ||
| ) { | ||
| const pattern = value.substring(0, patternLen); | ||
| const expectedRepeated = pattern.repeat( | ||
| Math.floor(value.length / patternLen) | ||
| ); | ||
|
|
||
| if ( | ||
| value.startsWith(expectedRepeated) && | ||
| expectedRepeated.length >= value.length * 0.8 | ||
| ) { | ||
| const numericValue = Number(pattern); | ||
| if (!isNaN(numericValue) && pattern !== "0") { | ||
| warn( | ||
| `PDF.js: Fixed repeating pattern calculation "${value}" -> "${pattern}"` | ||
| ); | ||
| return numericValue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| data.fieldValue = decodedValue; | ||
|
|
||
| const defaultFieldValue = getInheritableProperty({ | ||
| dict, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this class repeated inside the class with the exact same name and same of the exact same code? Does that actually work?