Skip to content

Update dom/form data #800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add MDN documentation strings for FormData
  • Loading branch information
zetashift committed Jul 26, 2023
commit 8989d97ef8016025054f5c5d8cb6afc8853e538f
76 changes: 67 additions & 9 deletions dom/src/main/scala/org/scalajs/dom/FormData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,101 @@ import scala.scalajs.js.|
@JSGlobal
class FormData(form: HTMLFormElement = js.native) extends js.Iterable[js.Tuple2[String, String]] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class FormData(form: HTMLFormElement = js.native) extends js.Iterable[js.Tuple2[String, String]] {
class FormData(form: HTMLFormElement = js.native) extends js.Iterable[js.Tuple2[String, String | Blob]] {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear

Copy link
Member

@armanbilge armanbilge Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it seems there are three possible constructors.

new FormData()
new FormData(form)
new FormData(form, submitter)
class FormData extends ... {
  def this(form: HTMLFormElement) = this()
  def this(form: HTMLFormElement, submitter: HTMLElement) = this()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class FormData(form: HTMLFormElement = js.native) extends ... {
  def this(form: HTMLFormElement) = this(form)
  def this(form: HTMLFormElement, submitter: HTMLElement) = this(form)
}

I think this is good point !
I've looked at other api codes using this() and i make this code,
but I'm not sure if this change is correct.

I'd be grateful if someone could fix this part for sure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked at other api codes using this() and i make this code,

That won't work, because there are two constructors that take a form. How wil it know which one to use?

I believe the correct change is the one I suggested in #800 (comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it !
I will add this part :)


/** Appends a key/value pair to the FormData object. */
/** The `append()` method of the `FormData` interface appends a new value onto an existing key inside a `FormData`
* object, or adds the key if it does not already exist.
*
* @param name
* The name of the field whose data is contained in value.
* @param value
* The field's value. This can be a string or `Blob` (including subclasses such as File). If none of these are
* specified the value is converted to a string.
*/
def append(name: js.Any, value: String): Unit = js.native
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is name: js.Any, isn't only String allowed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, the problem was caused by not fixing the original append method properly.
name must be a string. I just updated it, thank you !


/** The `append()` method of the `FormData` interface appends a new value onto an existing key inside a `FormData`
* object, or adds the key if it does not already exist.
*
* @param name
* The name of the field whose data is contained in value.
* @param value
* The field's value. This can be a string or `Blob` (including subclasses such as File). If none of these are
* specified the value is converted to a string.
* @param blobName
* The filename reported to the server (a string), when a `Blob` or `File` is passed as the second parameter. The
* default filename for `Blob` objects is "blob". The default filename for `File` objects is the file's filename.
*/
def append(name: js.Any, value: Blob, blobName: String): Unit = js.native

/** Deletes a key/value pair from the FormData object. */
/** The `delete()` method of the `FormData` interface deletes a key and its value(s) from a `FormData` object.
* @param name
* The name of the key you want to delete.
*/
def delete(name: String): Unit = js.native

/** Returns the first value associated with a given key from within a FormData object. */
/** The `get()` method of the `FormData` interface returns the first value associated with a given key from within a
* `FormData` object. If you expect multiple values and want all of them, use the `getAll()` method instead.
*
* @param name
* A string representing the name of the key you want to retrieve.
* @return
* A value whose key matches the specified name. Otherwise, `null`.
*/
def get(name: String): String | Blob = js.native

/** Returns whether a FormData object contains a certain key. */
/** The `has()` method of the `FormData` interface returns whether a `FormData` object contains a certain key.
*
* @param name
* A string representing the name of the key you want to test for.
* @return
* `true` if a key of `FormData` matches the specified name. Otherwise, `false`.
*/
def has(name: String): Boolean = js.native

/** Sets a new value for an existing key inside a FormData object, or adds the key/value if it does not already exist.
/** The `set()` method of the `FormData` interface sets a new value for an existing key inside a `FormData` object, or
* adds the key/value if it does not already exist.
*
* @param name
* The name of the field whose data is contained in value.
* @param value
* The field's value.
*/
def set(
name: String, value: String
): Unit = js.native

/** The `set()` method of the `FormData` interface sets a new value for an existing key inside a `FormData` object, or
* adds the key/value if it does not already exist.
*
* @param name
* The name of the field whose data is contained in value.
* @param value
* The field's value.
*/
def set(
name: String, value: Blob, blobName: String
): Unit = js.native

@JSName(js.Symbol.iterator)
override def jsIterator(): js.Iterator[js.Tuple2[String, String]] = js.native

/** Returns an iterator that iterates through all key/value pairs contained in the FormData. */
/** The `FormData.entries()` method returns an iterator which iterates through all key/value pairs contained in the
* `FormData`. The key of each pair is a string object, and the value is either a string or a `Blob`.
*/
def entries(): js.Iterator[js.Tuple2[String, String | Blob]] = js.native

/** Returns an array of all the values associated with a given key from within a FormData. */
/** The `getAll()` method of the `FormData` interface returns all the values associated with a given key from within a
* `FormData` object.
*/
def getAll(name: String): js.Array[String | Blob] = js.native

/** Returns an iterator iterates through all keys of the key/value pairs contained in the FormData. */
/** The `FormData.keys()` method returns an iterator which iterates through all keys contained in the `FormData`. The
* keys are strings.
*/
def keys(): js.Iterator[String] = js.native

/** Returns an iterator that iterates through all values contained in the FormData. */
/** The `FormData.values()` method returns an iterator which iterates through all values contained in the `FormData`.
* The values are strings or `Blob` objects.
*/
def values(): js.Iterator[String | Blob] = js.native
}

Expand Down