Skip to content

Commit 8c52373

Browse files
authored
Merge pull request #800 from G-yhlee/js-dom/formData
Update dom/form data
2 parents cabce07 + 1582e48 commit 8c52373

File tree

3 files changed

+126
-5
lines changed

3 files changed

+126
-5
lines changed

api-reports/2_12.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,18 @@ FocusEventInit[JT] var scoped: js.UndefOr[Boolean]
22912291
FocusEventInit[JT] var view: js.UndefOr[Window]
22922292
FocusOptions[JT] var focusVisible: js.UndefOr[Boolean]
22932293
FocusOptions[JT] var preventScroll: js.UndefOr[Boolean]
2294-
FormData[JC] def append(name: js.Any, value: js.Any, blobName: String?): Unit
2294+
FormData[JC] def append(name: String, value: Blob, blobName: String?): Unit
2295+
FormData[JC] def append(name: String, value: String): Unit
2296+
FormData[JC] def delete(name: String): Unit
2297+
FormData[JC] def entries(): js.Iterator[js.Tuple2[String, String | Blob]]
2298+
FormData[JC] def get(name: String): String | Blob
2299+
FormData[JC] def getAll(name: String): js.Array[String | Blob]
2300+
FormData[JC] def has(name: String): Boolean
2301+
FormData[JC] @JSName(js.Symbol.iterator) override def jsIterator(): js.Iterator[js.Tuple2[String, String | Blob]]
2302+
FormData[JC] def keys(): js.Iterator[String]
2303+
FormData[JC] def set(name: String, value: Blob, blobName: String?): Unit
2304+
FormData[JC] def set(name: String, value: String): Unit
2305+
FormData[JC] def values(): js.Iterator[String | Blob]
22952306
FormData[JO]
22962307
FrameType[JT]
22972308
FrameType[SO] val auxiliary: FrameType

api-reports/2_13.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,18 @@ FocusEventInit[JT] var scoped: js.UndefOr[Boolean]
22912291
FocusEventInit[JT] var view: js.UndefOr[Window]
22922292
FocusOptions[JT] var focusVisible: js.UndefOr[Boolean]
22932293
FocusOptions[JT] var preventScroll: js.UndefOr[Boolean]
2294-
FormData[JC] def append(name: js.Any, value: js.Any, blobName: String?): Unit
2294+
FormData[JC] def append(name: String, value: Blob, blobName: String?): Unit
2295+
FormData[JC] def append(name: String, value: String): Unit
2296+
FormData[JC] def delete(name: String): Unit
2297+
FormData[JC] def entries(): js.Iterator[js.Tuple2[String, String | Blob]]
2298+
FormData[JC] def get(name: String): String | Blob
2299+
FormData[JC] def getAll(name: String): js.Array[String | Blob]
2300+
FormData[JC] def has(name: String): Boolean
2301+
FormData[JC] @JSName(js.Symbol.iterator) override def jsIterator(): js.Iterator[js.Tuple2[String, String | Blob]]
2302+
FormData[JC] def keys(): js.Iterator[String]
2303+
FormData[JC] def set(name: String, value: Blob, blobName: String?): Unit
2304+
FormData[JC] def set(name: String, value: String): Unit
2305+
FormData[JC] def values(): js.Iterator[String | Blob]
22952306
FormData[JO]
22962307
FrameType[JT]
22972308
FrameType[SO] val auxiliary: FrameType

dom/src/main/scala/org/scalajs/dom/FormData.scala

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,116 @@ package org.scalajs.dom
88

99
import scala.scalajs.js
1010
import scala.scalajs.js.annotation._
11+
import scala.scalajs.js.|
1112

1213
/** XMLHttpRequest Level 2 adds support for the new FormData interface. FormData objects provide a way to easily
1314
* construct a set of key/value pairs representing form fields and their values, which can then be easily sent using
1415
* the XMLHttpRequest send() method.
1516
*/
1617
@js.native
1718
@JSGlobal
18-
class FormData(form: HTMLFormElement = js.native) extends js.Object {
19+
class FormData extends js.Iterable[js.Tuple2[String, String | Blob]] {
1920

20-
/** Appends a key/value pair to the FormData object. */
21-
def append(name: js.Any, value: js.Any, blobName: String = js.native): Unit = js.native
21+
def this(form: HTMLFormElement) = this()
22+
23+
def this(form: HTMLFormElement, submitter: HTMLElement) = this()
24+
25+
/** The `append()` method of the `FormData` interface appends a new value onto an existing key inside a `FormData`
26+
* object, or adds the key if it does not already exist.
27+
*
28+
* @param name
29+
* The name of the field whose data is contained in value.
30+
* @param value
31+
* The field's value. This can be a string or `Blob` (including subclasses such as File). If none of these are
32+
* specified the value is converted to a string.
33+
*/
34+
def append(name: String, value: String): Unit = js.native
35+
36+
/** The `append()` method of the `FormData` interface appends a new value onto an existing key inside a `FormData`
37+
* object, or adds the key if it does not already exist.
38+
*
39+
* @param name
40+
* The name of the field whose data is contained in value.
41+
* @param value
42+
* The field's value. This can be a string or `Blob` (including subclasses such as File). If none of these are
43+
* specified the value is converted to a string.
44+
* @param blobName
45+
* The filename reported to the server (a string), when a `Blob` or `File` is passed as the second parameter. The
46+
* default filename for `Blob` objects is "blob". The default filename for `File` objects is the file's filename.
47+
*/
48+
def append(name: String, value: Blob, blobName: String = js.native): Unit = js.native
49+
50+
/** The `delete()` method of the `FormData` interface deletes a key and its value(s) from a `FormData` object.
51+
* @param name
52+
* The name of the key you want to delete.
53+
*/
54+
def delete(name: String): Unit = js.native
55+
56+
/** The `get()` method of the `FormData` interface returns the first value associated with a given key from within a
57+
* `FormData` object. If you expect multiple values and want all of them, use the `getAll()` method instead.
58+
*
59+
* @param name
60+
* A string representing the name of the key you want to retrieve.
61+
* @return
62+
* A value whose key matches the specified name. Otherwise, `null`.
63+
*/
64+
def get(name: String): String | Blob = js.native
65+
66+
/** The `has()` method of the `FormData` interface returns whether a `FormData` object contains a certain key.
67+
*
68+
* @param name
69+
* A string representing the name of the key you want to test for.
70+
* @return
71+
* `true` if a key of `FormData` matches the specified name. Otherwise, `false`.
72+
*/
73+
def has(name: String): Boolean = js.native
74+
75+
/** The `set()` method of the `FormData` interface sets a new value for an existing key inside a `FormData` object, or
76+
* adds the key/value if it does not already exist.
77+
*
78+
* @param name
79+
* The name of the field whose data is contained in value.
80+
* @param value
81+
* The field's value.
82+
*/
83+
def set(
84+
name: String, value: String
85+
): Unit = js.native
86+
87+
/** The `set()` method of the `FormData` interface sets a new value for an existing key inside a `FormData` object, or
88+
* adds the key/value if it does not already exist.
89+
*
90+
* @param name
91+
* The name of the field whose data is contained in value.
92+
* @param value
93+
* The field's value.
94+
*/
95+
def set(
96+
name: String, value: Blob, blobName: String = js.native
97+
): Unit = js.native
98+
99+
@JSName(js.Symbol.iterator)
100+
override def jsIterator(): js.Iterator[js.Tuple2[String, String | Blob]] = js.native
101+
102+
/** The `FormData.entries()` method returns an iterator which iterates through all key/value pairs contained in the
103+
* `FormData`. The key of each pair is a string object, and the value is either a string or a `Blob`.
104+
*/
105+
def entries(): js.Iterator[js.Tuple2[String, String | Blob]] = js.native
106+
107+
/** The `getAll()` method of the `FormData` interface returns all the values associated with a given key from within a
108+
* `FormData` object.
109+
*/
110+
def getAll(name: String): js.Array[String | Blob] = js.native
111+
112+
/** The `FormData.keys()` method returns an iterator which iterates through all keys contained in the `FormData`. The
113+
* keys are strings.
114+
*/
115+
def keys(): js.Iterator[String] = js.native
116+
117+
/** The `FormData.values()` method returns an iterator which iterates through all values contained in the `FormData`.
118+
* The values are strings or `Blob` objects.
119+
*/
120+
def values(): js.Iterator[String | Blob] = js.native
22121
}
23122

24123
@js.native

0 commit comments

Comments
 (0)