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
--- 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.