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
//! Helper Functions

use crate::{
    error::Error,
    query::{Collection, Element},
};
use derive_more::{AsRef, Deref, DerefMut, From, Into};
use std::iter::FromIterator;
use web_sys::HtmlFormElement;

impl Element {
    /// Serialize a form into a data structure that holds a key-value
    /// type.
    ///
    /// The generic return type can be anything that supports the
    /// trait bounds, for example `Vec<(String, String)>` or
    /// `HashMap<String, String>`.  To get a jQuery-compatible return
    /// type, use the `FormData` type that is provided by this crate.
    pub fn serialize_array<T, V>(&self) -> Result<T, Error>
    where
        T: FromIterator<(String, V)>,
        V: From<String>,
    {
        let form = self.dyn_ref::<HtmlFormElement>()?;

        Collection::from(form.elements())
            .into_iter()
            .filter(|elem| {
                elem.attr("name").is_some()
                    && !elem.is(":disabled").unwrap_or_default()
                    && !((elem.is("option").unwrap_or_default()
                        || elem.attr("type").as_deref() == Some("radio")
                        || elem.attr("type").as_deref() == Some("checkbox"))
                        && !elem.is(":checked").unwrap_or_default())
            })
            .map(|elem| {
                let key = elem.attr("name").ok_or(Error::NoValue("name"))?;
                let value = elem.val()?;
                Ok((key, value.into()))
            })
            .collect::<Result<T, Error>>()
    }
}

impl Collection {
    /// Serialize a collection of forms into a data structure that
    /// holds a key-value type.
    ///
    /// The generic return type can be anything that supports the
    /// trait bounds, for example `Vec<(String, String)>` or
    /// `HashMap<String, String>`.  To get a jQuery-compatible return
    /// type, use the `FormData` type that is provided by this crate.
    pub fn serialize_array<T, V>(&self) -> Result<T, Error>
    where
        T: FromIterator<(String, V)> + IntoIterator + FromIterator<<T as IntoIterator>::Item>,
        V: From<String>,
    {
        Ok(self
            .0
            .iter()
            .map(|elem| elem.serialize_array())
            .collect::<Result<Vec<T>, Error>>()?
            .into_iter()
            .flatten()
            .collect::<T>())
    }
}

/// "name-value" representation of form data.
///
/// This struct can be used with the `.serialize_array()` function to
/// get jquery-compatible name-value representation of the result.
/// Some form elements, such as radio buttons or checkboxes, can
/// contain multiple elements with the same name, so this array of
/// name-value fields is returned instead of simple `HashMap`.
#[derive(AsRef, Debug, Deref, DerefMut, Eq, From, PartialEq, Into)]
#[cfg_attr(
    feature = "serde-serialize",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct FormData(Vec<FormValue>);

impl FromIterator<(String, String)> for FormData {
    fn from_iter<I: IntoIterator<Item = (String, String)>>(iter: I) -> Self {
        Self(iter.into_iter().map(Into::into).collect())
    }
}

impl FromIterator<FormValue> for FormData {
    fn from_iter<I: IntoIterator<Item = FormValue>>(iter: I) -> Self {
        Self(Vec::from_iter(iter))
    }
}

impl IntoIterator for FormData {
    type Item = FormValue;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

/// "name-value" representation of a single `FormData` field.
#[derive(Debug, Eq, From, Into, PartialEq)]
#[cfg_attr(
    feature = "serde-serialize",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct FormValue {
    pub name: String,
    pub value: String,
}