Bug 1360370 - Part 2: Support nsIDOMHTMLSelectElement in FormLikeFactory.createFromField.; r?MattN draft
authorSean Lee <selee@mozilla.com>
Mon, 08 May 2017 17:39:11 +0800
changeset 585186 4a00074596c0641c80f079d5db59a77b14bca954
parent 585185 7ec96df241aeebe76538a18cc12d512075fd78df
child 585187 79fba6de41db64cd9a3abf68b6d205aeb785f208
child 585219 821498aa2aa04ccba547830efea69f161bc7865b
child 585223 1d0c3cf5b54aafda11c305035cff8f2b14f2bcc3
push id61042
push userbmo:selee@mozilla.com
push dateFri, 26 May 2017 16:52:28 +0000
reviewersMattN
bugs1360370
milestone55.0a1
Bug 1360370 - Part 2: Support nsIDOMHTMLSelectElement in FormLikeFactory.createFromField.; r?MattN MozReview-Commit-ID: CHxELtW4wXP
toolkit/modules/FormLikeFactory.jsm
--- a/toolkit/modules/FormLikeFactory.jsm
+++ b/toolkit/modules/FormLikeFactory.jsm
@@ -42,45 +42,45 @@ let FormLikeFactory = {
     }
 
     this._addToJSONProperty(formLike);
 
     return formLike;
   },
 
   /**
-   * Create a FormLike object from an <input> in a document.
+   * Create a FormLike object from an <input>/<select> in a document.
    *
    * If the field is in a <form>, construct the FormLike from the form.
    * Otherwise, create a FormLike with a rootElement (wrapper) according to
-   * heuristics. Currently all <input> not in a <form> are one FormLike but this
-   * shouldn't be relied upon as the heuristics may change to detect multiple
-   * "forms" (e.g. registration and login) on one page with a <form>.
+   * heuristics. Currently all <input>/<select> not in a <form> are one FormLike
+   * but this shouldn't be relied upon as the heuristics may change to detect
+   * multiple "forms" (e.g. registration and login) on one page with a <form>.
    *
    * Note that two FormLikes created from the same field won't return the same FormLike object.
    * Use the `rootElement` property on the FormLike as a key instead.
    *
-   * @param {HTMLInputElement} aField - a field in a document
+   * @param {HTMLInputElement|HTMLSelectElement} aField - an <input> or <select> field in a document
    * @return {FormLike}
    * @throws Error if aField isn't a password or username field in a document
    */
   createFromField(aField) {
-    if (!(aField instanceof Ci.nsIDOMHTMLInputElement) ||
+    if ((!(aField instanceof Ci.nsIDOMHTMLInputElement) && !(aField instanceof Ci.nsIDOMHTMLSelectElement)) ||
         !aField.ownerDocument) {
       throw new Error("createFromField requires a field in a document");
     }
 
     let rootElement = this.findRootForField(aField);
     if (rootElement instanceof Ci.nsIDOMHTMLFormElement) {
       return this.createFromForm(rootElement);
     }
 
     let doc = aField.ownerDocument;
     let elements = [];
-    for (let el of rootElement.querySelectorAll("input")) {
+    for (let el of rootElement.querySelectorAll("input, select")) {
       // Exclude elements inside the rootElement that are already in a <form> as
       // they will be handled by their own FormLike.
       if (!el.form) {
         elements.push(el);
       }
     }
     let formLike = {
       action: doc.baseURI,
@@ -96,17 +96,17 @@ let FormLikeFactory = {
 
   /**
    * Determine the Element that encapsulates the related fields. For example, if
    * a page contains a login form and a checkout form which are "submitted"
    * separately, and the username field is passed in, ideally this would return
    * an ancestor Element of the username and password fields which doesn't
    * include any of the checkout fields.
    *
-   * @param {HTMLInputElement} aField - a field in a document
+   * @param {HTMLInputElement|HTMLSelectElement} aField - a field in a document
    * @return {HTMLElement} - the root element surrounding related fields
    */
   findRootForField(aField) {
     if (aField.form) {
       return aField.form;
     }
 
     return aField.ownerDocument.documentElement;