Bug 1430575 - Add element.satisfiesContraints. r?automatedtester draft
authorAndreas Tolfsen <ato@sny.no>
Mon, 15 Jan 2018 17:17:11 +0000
changeset 720607 261a065a52b3afed818745b0c06da4a1e1bd86b4
parent 720606 e1f01da8f7249f6b3a3c9fa850493680bec6939d
child 720608 2d0ad5da23eae08fcab0f920ffd3fabe74b32bd3
push id95583
push userbmo:ato@sny.no
push dateMon, 15 Jan 2018 20:46:43 +0000
reviewersautomatedtester
bugs1430575
milestone59.0a1
Bug 1430575 - Add element.satisfiesContraints. r?automatedtester Adds new shorthand function, satisfiesConstraints, to the Marionette element module, that tests if the mutable form control element satisfies its constraints, as it is described in the HTML standard. This tests for example that the input to <input type=number> is a number. MozReview-Commit-ID: H8eAt6Gr7jy
testing/marionette/element.js
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -6,16 +6,17 @@
 /* global XPCNativeWrapper */
 
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 Cu.import("chrome://marionette/content/assert.js");
 Cu.import("chrome://marionette/content/atom.js");
 const {
   InvalidArgumentError,
+  InvalidElementStateError,
   InvalidSelectorError,
   NoSuchElementError,
   StaleElementReferenceError,
 } = Cu.import("chrome://marionette/content/error.js", {});
 const {pprint} = Cu.import("chrome://marionette/content/format.js", {});
 const {PollPromise} = Cu.import("chrome://marionette/content/sync.js", {});
 
 this.EXPORTED_SYMBOLS = [
@@ -936,16 +937,43 @@ element.isEditable = function(el) {
   if (element.isReadOnly(el) || element.isDisabled(el)) {
     return false;
   }
 
   return element.isMutableFormControl(el) || element.isEditingHost(el);
 };
 
 /**
+ * Tests that <var>el</var> satisfies all its form control constraints.
+ *
+ * An element is considered <em>not</em> to satisfy its constraints
+ * if it suffers from being missing, its input has a value that is
+ * not the correct syntax (email, URL), does not satisfy the
+ * <code>pattern</code> attribute, is too long (<code>maxlength</code>)
+ * or too short (<code>minlength</code>), overflows or underflows,
+ * does not fit the rules of the <code>step</code> attribute,
+ * or otherwise has incomplete input according to Gecko.
+ *
+ * @param {Element} el
+ *     Mutable form control element.
+ *
+ * @return {boolean}
+ *     True if it satisfies all its constraints.
+ *
+ * @throws {InvalidElementStateError}
+ *     If <var>el</var> is not a mutable form control element.
+ */
+element.satisfiesConstraints = function(el) {
+  if (!element.isMutableFormControl(el)) {
+    throw new InvalidElementStateError(pprint`Not an editable form control: ${el}`);
+  }
+  return el.validity.valid;
+};
+
+/**
  * This function generates a pair of coordinates relative to the viewport
  * given a target element and coordinates relative to that element's
  * top-left corner.
  *
  * @param {Node} node
  *     Target node.
  * @param {number=} xOffset
  *     Horizontal offset relative to target's top-left corner.