-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Fix AForm Validation for fields with non-digit characters #14307
Changes from all commits
5622906
989dd0f
b6e1309
cc8da7a
6b73063
e9e853f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -486,15 +486,15 @@ class AForm { | |
]); | ||
|
||
function _checkValidity(_value, _cMask) { | ||
for (let i = 0, ii = value.length; i < ii; i++) { | ||
const mask = _cMask.charAt(i); | ||
const char = _value.charAt(i); | ||
const checker = checkers.get(mask); | ||
for (let i = 0, ii = _value.length; i < ii; i++) { | ||
const maskChar = _cMask.charAt(i); | ||
const valueChar = _value.charAt(i); | ||
const checker = checkers.get(maskChar); | ||
if (checker) { | ||
if (!checker(char)) { | ||
if (!checker(valueChar)) { | ||
return false; | ||
} | ||
} else if (mask !== char) { | ||
} else if (maskChar !== valueChar) { | ||
return false; | ||
} | ||
} | ||
|
@@ -564,7 +564,7 @@ class AForm { | |
event.change.length + | ||
event.selStart - | ||
event.selEnd; | ||
if (finalLen >= 8) { | ||
if (finalLen > 8 || event.value[0] === "(") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the rhs of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The RHS is necessary because we validate every time the field changes. For final validation you're right that it's unnecessary, but for the validation-during-typing, without the RHS if someone starts typing This is because I can add some tests around this too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes sense, +1. |
||
formatStr = "(999) 999-9999"; | ||
} else { | ||
formatStr = "999-9999"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,11 +47,32 @@ class EventDispatcher { | |
this._document.obj._eventDispatcher = this; | ||
} | ||
|
||
/* | ||
* Take an event object and reconstruct what we think the result will be once | ||
* the change is applied, so we can validate against it and cancel the event | ||
* if need be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Here, and everywhere in the patch, please make sure to always end all sentences with a period. Also, please keep https://github.com/mozilla/pdf.js/wiki/Squashing-Commits in mind when updating the patch. |
||
* | ||
* TODO: Given the event info we currently have, we can't determine where in | ||
* the value a single-character insertion/deletion happened. For now we just | ||
* assume they happen at the end of the string, which is by far the most | ||
* common case, but this leaves some edge cases, see issue #14307. | ||
*/ | ||
mergeChange(event) { | ||
let value = event.value; | ||
if (typeof value !== "string") { | ||
value = value.toString(); | ||
} | ||
// If there was no selection, it's a single-character change | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where did you find this information ?
My understanding is that the selStart is the position of the caret before the change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found it through some testing and logging with pdf.js, and seeing what the events being created actually looked like, and they had Like I said (maybe here or on the bug, I don't remember), I wasn't sure whether the behavior of these events was in a specification or something somewhere or not - it looks like it is, which I guess means the bug is that something else in pdf.js isn't populating selStart/selEnd for these events? That would also make sense, and would solve the edge cases as well. |
||
if (event.selStart === -1 && event.selEnd === -1) { | ||
if (event.change === "") { | ||
// Empty change indicates a deletion | ||
return value.slice(0, -1); | ||
} | ||
// Otherwise, assume it's an append | ||
return value + event.change; | ||
} | ||
|
||
// Otherwise, splice in the change to replace the selection | ||
const prefix = | ||
event.selStart >= 0 ? value.substring(0, event.selStart) : ""; | ||
const postfix = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -361,15 +361,15 @@ describe("Scripting", function () { | |
name: "Keystroke", | ||
willCommit: false, | ||
change: "o", | ||
selStart: 4, | ||
selEnd: 4, | ||
selStart: -1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the changes in this file are very likely wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes may be wrong in that they are against spec, but they are correct in that they are what pdf.js is emitting currently. It sounds like pdf.js's behavior in creating these events is the actual bug though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So to be clear, I agree these will probably need to end up being reverted, hopefully replaced by some tests (or fixed test) of the generation of events for single-character edits, to validate that they are populating |
||
selEnd: -1, | ||
}); | ||
|
||
expect(send_queue.has(refId)).toEqual(true); | ||
expect(send_queue.get(refId)).toEqual({ | ||
id: refId, | ||
value: "hell", | ||
selRange: [4, 4], | ||
selRange: [-1, -1], | ||
}); | ||
}); | ||
|
||
|
@@ -398,8 +398,8 @@ describe("Scripting", function () { | |
name: "Keystroke", | ||
willCommit: false, | ||
change: "o", | ||
selStart: 4, | ||
selEnd: 4, | ||
selStart: -1, | ||
selEnd: -1, | ||
}); | ||
|
||
expect(send_queue.has(refId)).toEqual(true); | ||
|
@@ -1119,8 +1119,8 @@ describe("Scripting", function () { | |
change: "3", | ||
name: "Keystroke", | ||
willCommit: false, | ||
selStart: 0, | ||
selEnd: 0, | ||
selStart: -1, | ||
selEnd: -1, | ||
}); | ||
expect(send_queue.has(refId)).toEqual(false); | ||
|
||
|
@@ -1130,8 +1130,8 @@ describe("Scripting", function () { | |
change: "F", | ||
name: "Keystroke", | ||
willCommit: false, | ||
selStart: 1, | ||
selEnd: 1, | ||
selStart: -1, | ||
selEnd: -1, | ||
}); | ||
expect(send_queue.has(refId)).toEqual(false); | ||
|
||
|
@@ -1141,8 +1141,8 @@ describe("Scripting", function () { | |
change: "?", | ||
name: "Keystroke", | ||
willCommit: false, | ||
selStart: 2, | ||
selEnd: 2, | ||
selStart: -1, | ||
selEnd: -1, | ||
}); | ||
expect(send_queue.has(refId)).toEqual(false); | ||
|
||
|
@@ -1152,14 +1152,14 @@ describe("Scripting", function () { | |
change: "@", | ||
name: "Keystroke", | ||
willCommit: false, | ||
selStart: 3, | ||
selEnd: 3, | ||
selStart: -1, | ||
selEnd: -1, | ||
}); | ||
expect(send_queue.has(refId)).toEqual(true); | ||
expect(send_queue.get(refId)).toEqual({ | ||
id: refId, | ||
value: "3F?", | ||
selRange: [3, 3], | ||
selRange: [-1, -1], | ||
}); | ||
|
||
send_queue.delete(refId); | ||
|
@@ -1169,7 +1169,7 @@ describe("Scripting", function () { | |
change: "0", | ||
name: "Keystroke", | ||
willCommit: true, | ||
selStart: 3, | ||
selStart: -1, | ||
selEnd: 3, | ||
}); | ||
expect(send_queue.has(refId)).toEqual(false); | ||
|
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.
my bad... thank you to point that out.