1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use webapi::blob::IBlob;
use webapi::dom_exception::InvalidStateError;
use webapi::element::IElement;
use webapi::error::TypeError;
use webapi::file::File;
use webcore::try_from::TryFrom;
use webcore::try_from::TryInto;
use webcore::value::ConversionError;
use webcore::value::Reference;
use webcore::value::Value;

/// The `FormData` interface provides a way to easily construct a set of key/value pairs
/// representing form fields and their values, which can then be easily sent using the
/// `XMLHttpRequest.send()` method. It uses the same format a form would use if the encoding type
/// were set to `"multipart/form-data"`.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData)
// https://xhr.spec.whatwg.org/#formdata
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "FormData")]
pub struct FormData( Reference );

/// Represents a type of data stores in FormData.
#[derive(Clone, Debug, PartialEq)]
pub enum FormDataEntry {
    /// File data
    File( File ),
    /// Text data
    String( String )
}

error_enum_boilerplate! {
    FormDataFromElementError,
    InvalidStateError, TypeError
}

impl TryFrom< Value > for FormDataEntry {
    type Error = ConversionError;

    fn try_from(value: Value) -> Result< Self, Self::Error > {
        let entry = match value {
            Value::String(s) => FormDataEntry::String(s),
            Value::Reference(r) => FormDataEntry::File(File(r)),
            _ => return Err(ConversionError::type_mismatch(&value, "string or reference".into())),
        };

        Ok(entry)
    }
}

impl TryFrom< Value > for Option< FormDataEntry > {
    type Error = ConversionError;

    fn try_from(value: Value) -> Result< Self, Self::Error > {
        let entry = match value {
            Value::Null|Value::Undefined => None,
            Value::String(s) => Some(FormDataEntry::String(s)),
            Value::Reference(r) => Some(FormDataEntry::File(File(r))),
            _ => return Err(ConversionError::type_mismatch(&value, "null, string or reference".into())),
        };

        Ok(entry)
    }
}

impl FormData {
    /// Creates a new `FormData`.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData)
    pub fn new() -> Self {
        js! (
            return new FormData();
        ).try_into().unwrap()
    }

    /// Creates a new `FormData` from a form element.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData)
    pub fn from_element<T>( form: &T ) -> Result< Self, FormDataFromElementError > where T: IElement {
        js_try! (
            let form = @{form.as_ref()};

            if ( ! (form instanceof HTMLFormElement) ) {
                throw new TypeError("Argument 1 of FormData::from_element does not implement interface HTMLFormElement.");
            }

            return new FormData(form);
        ).unwrap()
    }

    /// Appends a new value onto an existing key, or adds the key if it does not already exist.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/append)
    // https://xhr.spec.whatwg.org/#dom-formdata-append
    pub fn append_string( &self, name: &str, value: &str ) {
        js! { @(no_return)
            @{self}.append(@{name}, @{value});
        }
    }

    /// Appends a new blob onto an existing key, or adds the key if it does not already exist.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/append)
    // https://xhr.spec.whatwg.org/#dom-formdata-append-blob
    pub fn append_blob<T>( &self, name: &str, value: &T, filename: Option< &str > ) where T: IBlob {
        js! { @(no_return)
            @{self}.append(@{name}, @{value.as_ref()}, @{filename});
        }
    }

    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/delete)
    // https://xhr.spec.whatwg.org/#dom-formdata-delete
    pub fn delete( &self, name: &str ) {
        js! { @(no_return)
            @{self}.delete(@{name});
        }
    }

    /// Deletes a key and its value(s).
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/get)
    // https://xhr.spec.whatwg.org/#dom-formdata-get
    pub fn get( &self, name: &str ) -> Option< FormDataEntry > {
        js! (
            return @{self}.get(@{name});
        ).try_into().unwrap()
    }

    /// Returns all the values associated with a given key.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/getAll)
    // https://xhr.spec.whatwg.org/#dom-formdata-getall
    pub fn get_all( &self, name: &str ) -> Vec< FormDataEntry > {
        js! (
            return @{self}.getAll(@{name});
        ).try_into().unwrap()
    }

    /// Returns a boolean stating whether a `FormData` object contains a certain key.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/has)
    // https://xhr.spec.whatwg.org/#dom-formdata-has
    pub fn has( &self, name: &str ) -> bool {
        js! (
            return @{self}.has(@{name});
        ).try_into().unwrap()
    }

    /// Sets a new value for an existing key, or adds the key/value if it does not already exist.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/set)
    // https://xhr.spec.whatwg.org/#dom-formdata-set
    pub fn set_string( &self, name: &str, value: &str ) {
        js! { @(no_return)
            @{self}.set(@{name}, @{value});
        }
    }

    /// Sets a new blob for an existing key, or adds the key/value if it does not already exist.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FormData/set)
    // https://xhr.spec.whatwg.org/#dom-formdata-set-blob
    pub fn set_blob<T>( &self, name: &str, value: &T, filename: Option< &str > ) where T: IBlob {
        js! { @(no_return)
            @{self}.set(@{name}, @{value.as_ref()}, @{filename});
        }
    }
}

#[cfg(all(test, feature = "web_test"))]
mod tests {
    use stdweb::webapi::blob::Blob;
    use stdweb::webapi::element::Element;
    use stdweb::webcore::try_from::TryInto;
    use super::*;

    fn form() -> Element {
        js!(
            let form = document.createElement("form");

            let inputs = [];

            for (let i = 1; i < 4; i++) {
                inputs[i] = document.createElement("input");
                inputs[i].name = "key" + i;
                inputs[i].value = "value" + i;
                form.appendChild(inputs[i]);
            }

            inputs[3].name = "key2";

            return form;
        ).try_into().unwrap()
    }

    fn data() -> FormData {
        let form = form();

        FormData::from_element(&form)
            .unwrap()
    }

    #[test]
    fn test_new() {
        FormData::new();
    }

    #[test]
    fn test_from_invalid_element() {
        use webapi::document::document;

        let div = document().create_element("div")
            .unwrap();

        assert!(FormData::from_element(&div).is_err());
    }

    #[test]
    fn test_append_string() {
        let data = data();
        assert!(data.get("key0").is_none());

        data.append_string("key0", "value0");
        assert_eq!(data.get("key0"), Some(FormDataEntry::String(String::from("value0"))));
    }

    #[test]
    fn test_append_blob() {
        let data = data();
        assert!(data.get("key0").is_none());

        data.append_blob("blob", &Blob::new(), Some("file.jpg"));
        assert!(data.get("blob").is_some());
    }

    #[test]
    fn test_delete() {
        let data = data();
        assert!(data.get("key1").is_some());

        data.delete("key1");
        assert!(data.get("key1").is_none());
    }

    #[test]
    fn test_get() {
        let data = data();

        assert_eq!(data.get("key1"), Some(FormDataEntry::String(String::from("value1"))));
    }

    #[test]
    fn test_get_all() {
        let data = data();

        assert_eq!(data.get_all("key2"), vec![
            FormDataEntry::String(String::from("value2")),
            FormDataEntry::String(String::from("value3"))
        ]);
        assert_eq!(data.get_all("unknow"), Vec::<FormDataEntry>::new());
    }

    #[test]
    fn test_has() {
        let data = data();

        assert_eq!(data.has("key1"), true);
    }

    #[test]
    fn test_set_string() {
        let data = data();
        assert_eq!(data.get("key1"), Some(FormDataEntry::String(String::from("value1"))));

        data.set_string("key1", "value");
        assert_eq!(data.get("key1"), Some(FormDataEntry::String(String::from("value"))));
    }

    #[test]
    fn test_set_blob() {
        let data = data();
        assert_eq!(data.get("key1"), Some(FormDataEntry::String(String::from("value1"))));

        data.set_blob("key1", &Blob::new(), None);
        assert!(data.get("key1").is_some());
    }
}