Bug 1660060 - Add a keypress listener to the percentScale element. r=emalysz a=RyanVM
Differential Revision:
https://phabricator.services.mozilla.com/D88446
--- a/toolkit/components/printing/content/print.js
+++ b/toolkit/components/printing/content/print.js
@@ -893,16 +893,17 @@ class ScaleInput extends PrintUIControlM
super.initialize();
this._percentScale = this.querySelector("#percent-scale");
this._shrinkToFitChoice = this.querySelector("#fit-choice");
this._scaleChoice = this.querySelector("#percent-scale-choice");
this._scaleError = this.querySelector("#error-invalid-scale");
this._percentScale.addEventListener("input", this);
+ this._percentScale.addEventListener("keypress", this);
this.addEventListener("input", this);
}
update(settings) {
let { scaling, shrinkToFit } = settings;
this._shrinkToFitChoice.checked = shrinkToFit;
this._scaleChoice.checked = !shrinkToFit;
this._percentScale.disabled = shrinkToFit;
@@ -915,16 +916,29 @@ class ScaleInput extends PrintUIControlM
(this._shrinkToFitChoice.checked && !this._percentScale.checkValidity())
) {
// Only allow whole numbers. 0.14 * 100 would have decimal places, etc.
this._percentScale.value = parseInt(scaling * 100, 10);
}
}
handleEvent(e) {
+ if (e.type == "keypress") {
+ let char = String.fromCharCode(e.charCode);
+ if (
+ !char.match(/^[0-9]$/) &&
+ !char.match("\x00") &&
+ !e.ctrlKey &&
+ !e.metaKey
+ ) {
+ e.preventDefault();
+ }
+ return;
+ }
+
if (e.target == this._shrinkToFitChoice || e.target == this._scaleChoice) {
if (!this._percentScale.checkValidity()) {
this._percentScale.value = 100;
}
let scale =
e.target == this._shrinkToFitChoice
? 1
: Number(this._percentScale.value / 100);