Bug 1360370 - Part 2: Support nsIDOMHTMLSelectElement in FormLikeFactory.createFromField.; r?MattN
MozReview-Commit-ID: CHxELtW4wXP
--- 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;