Bug 1660060 - Add a keypress listener to the percentScale element. r=emalysz a=RyanVM DEVEDITION_81_0b4_BUILD1 DEVEDITION_81_0b4_RELEASE FIREFOX_81_0b4_BUILD1 FIREFOX_81_0b4_RELEASE
authorMicah Tigley <mtigley@mozilla.com>
Fri, 28 Aug 2020 18:58:24 +0000 (2020-08-28)
changeset 610619 dcc7a303edfb16d069cba8e3f10c919a0e0f3b29
parent 610618 3873b672bd64fdcf49891dcf3fdde2270d9127f0
child 610620 699eb6a563a3c2a1c4a9a0a52a4dc6cbf78525e9
push id13670
push userbtara@mozilla.com
push dateSat, 29 Aug 2020 20:08:10 +0000 (2020-08-29)
treeherdermozilla-beta@dcc7a303edfb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemalysz, RyanVM
bugs1660060
milestone81.0
Bug 1660060 - Add a keypress listener to the percentScale element. r=emalysz a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D88446
toolkit/components/printing/content/print.js
--- 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);