From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- toolkit/modules/FormLikeFactory.jsm | 199 ++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 toolkit/modules/FormLikeFactory.jsm (limited to 'toolkit/modules/FormLikeFactory.jsm') diff --git a/toolkit/modules/FormLikeFactory.jsm b/toolkit/modules/FormLikeFactory.jsm new file mode 100644 index 0000000000..0b9fa38c81 --- /dev/null +++ b/toolkit/modules/FormLikeFactory.jsm @@ -0,0 +1,199 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +var EXPORTED_SYMBOLS = ["FormLikeFactory"]; + +const { XPCOMUtils } = ChromeUtils.import( + "resource://gre/modules/XPCOMUtils.jsm" +); + +/** + * A factory to generate FormLike objects that represent a set of related fields + * which aren't necessarily marked up with a
element. FormLike's emulate + * the properties of an HTMLFormElement which are relevant to form tasks. + */ +let FormLikeFactory = { + _propsFromForm: ["action", "autocomplete", "ownerDocument"], + + /** + * Create a FormLike object from a . + * + * @param {HTMLFormElement} aForm + * @return {FormLike} + * @throws Error if aForm isn't an HTMLFormElement + */ + createFromForm(aForm) { + if (ChromeUtils.getClassName(aForm) !== "HTMLFormElement") { + throw new Error("createFromForm: aForm must be a HTMLFormElement"); + } + + let formLike = { + elements: [...aForm.elements], + rootElement: aForm, + }; + + for (let prop of this._propsFromForm) { + formLike[prop] = aForm[prop]; + } + + this._addToJSONProperty(formLike); + + return formLike; + }, + + /** + * Create a FormLike object from an // or