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
use webapi::element::{Element, IElement};
use webapi::event_target::{EventTarget, IEventTarget};
use webapi::html_element::{HtmlElement, IHtmlElement};
use webapi::node::{INode, Node};
use webcore::try_from::TryInto;
use webcore::value::Reference;
use webapi::html_collection::HtmlCollection;
use webapi::html_elements::OptionElement;

/// Indicates that an invalid value is setted to an `SelectElement`.
/// It means there is no `<option>` element that has the given value.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnknownValueError(String);

impl std::fmt::Display for UnknownValueError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "There is no `<option>` element that has value='{}'", self.0)
    }
}

impl std::error::Error for UnknownValueError {
    fn description(&self) -> &str {
        "There is no `<option>` element that has the given value"
    }
}

/// The HTML `<select>` element represents a control that provides a menu of options.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)
// https://html.spec.whatwg.org/#htmlselectelement
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "HTMLSelectElement")]
#[reference(subclass_of(EventTarget, Node, Element, HtmlElement))]
pub struct SelectElement(Reference);

impl IEventTarget for SelectElement {}
impl INode for SelectElement {}
impl IElement for SelectElement {}
impl IHtmlElement for SelectElement {}

impl SelectElement {
    /// Returns the value attribute of the first selected `<option>` element or
    /// if it is missing, the text attribute. If there is no selection, return empty string.
    /// This method is just a wrapper for getting `HTMLSelectElement.value` directly
    // https://html.spec.whatwg.org/#the-select-element:dom-select-value
    pub fn raw_value(&self) -> String {
        js!(
            return @{self}.value
        ).try_into().unwrap()
    }

    /// Set the given value to `HTMLSelectElement.value` directly.
    // https://html.spec.whatwg.org/#the-select-element:dom-select-value
    pub fn set_raw_value(&self, value: &str) {
        js!{
            @(no_return)
            @{self}.value = @{value};
        }
    }
    
    /// Returns the `Some(index)` of the first selected item, if any, or `None` if there is no selected item.
    // https://html.spec.whatwg.org/#the-select-element:dom-select-selectedindex
    pub fn selected_index(&self) -> Option<u32> {
        js! (
            var self = @{self};
            if (self.selectedIndex < 0) {
                return null;
            }else{
                return self.selectedIndex;
            }
        ).try_into().unwrap()
    }

    /// Change selected index to the given value.
    // https://html.spec.whatwg.org/#the-select-element:dom-select-selectedindex
    pub fn set_selected_index(&self, selected_index: Option<u32>) {
        match selected_index {
            Some(si) => js!{
                @(no_return)
                @{self}.selectedIndex = @{si};
            },
            None => js!{
                @(no_return)
                @{self}.selectedIndex = -1;
            }
        };
    }

    /// Returns the `Some(value)` of the first selected item, if any, or `None` if there is no selected item.
    // https://html.spec.whatwg.org/#the-select-element:dom-select-value
    pub fn value(&self) -> Option<String> {
        match self.selected_index(){
            None => None,
            Some(_) => Some(self.raw_value())
        }
    }

    /// Change the selected value to the given value. If you provide an invalid value,
    /// the `<select>` element will have no item selected, and an `UnknownValueError` is returned.
    // https://html.spec.whatwg.org/#the-select-element:dom-select-value
    pub fn set_value(&self, value: Option<&str>) -> Result<(), UnknownValueError> {
        match value{
            Some(value) => {
                self.set_raw_value(value);
                if self.selected_index().is_none(){
                    Err(UnknownValueError(value.to_string()))
                }else{
                    Ok(())
                }
            },
            None => {
                self.set_selected_index(None);
                Ok(())
            }
        }
    }

    /// Indicates whether multiple items can be selected
    // https://html.spec.whatwg.org/#the-select-element:dom-select-multiple
    pub fn multiple(&self) -> bool {
        js!(
            return @{self}.multiple;
        ).try_into().unwrap()
    }

    /// An `HtmlCollection` representing
    /// the set of `<option>` elements that are selected.
    // https://html.spec.whatwg.org/#the-select-element:dom-select-selectedoptions
    pub fn selected_options(&self) -> HtmlCollection {
        js!(
            return @{self}.selectedOptions;
        ).try_into().unwrap()
    }

    /// A convenience method to get values of all selected `<option>` elements
    pub fn selected_values(&self) -> Vec<String> {
        self.selected_options()
            .iter().map(|e|{
                let e: OptionElement = e.try_into().unwrap();
                e.value()
            }).collect::<Vec<String>>()
    }

    /// A convenience method to get indices of all selected `<option>` elements
    pub fn selected_indices(&self) -> Vec<i32> {
        self.selected_options()
            .iter().map(|e|{
                let e: OptionElement = e.try_into().unwrap();
                e.index()
            }).collect::<Vec<i32>>()
    }
}

#[cfg(all(test, feature = "web_test"))]
mod tests{
    use super::{SelectElement, UnknownValueError};
    use webapi::node::Node;
    use webcore::try_from::TryInto;
    #[test]
    fn test_select_one() {
        let html = r#"<select><option value='first'>First option</option>
            <option value='second'>Second option</option>
            <option value='third' selected>Third option</option>
            <option value=''>Empty</option></select>"#;
        let se: SelectElement = Node::from_html(html).unwrap().try_into().unwrap();

        assert_eq!(se.multiple(), false);

        assert_eq!(se.selected_index(), Some(2));
        assert_eq!(se.value(), Some("third".to_string()));

        se.set_selected_index(Some(1));
        assert_eq!(se.selected_index(), Some(1));
        assert_eq!(se.value(), Some("second".to_string()));

        se.set_selected_index(None);
        assert_eq!(se.selected_index(), None);
        assert_eq!(se.value(), None);

        let rs = se.set_value(Some("first"));
        assert_eq!(rs, Ok(()));
        assert_eq!(se.selected_index(), Some(0));
        assert_eq!(se.value(), Some("first".to_string()));

        let rs = se.set_value(None);
        assert_eq!(rs, Ok(()));
        assert_eq!(se.selected_index(), None);
        assert_eq!(se.value(), None);

        let rs = se.set_value(Some(""));
        assert_eq!(rs, Ok(()));
        assert_eq!(se.selected_index(), Some(3));
        assert_eq!(se.value(), Some("".to_string()));

        let rs = se.set_value(Some("invalid_option"));
        assert_eq!(rs, Err(UnknownValueError("invalid_option".to_string())));
        assert_eq!(se.selected_index(), None);
        assert_eq!(se.value(), None);
    }

    #[test]
    fn test_select_multiple_noselection(){
         let html = r#"<select multiple><option value='first'>First option</option>
            <option value='second'>Second option</option>
            <option value='third'>Third option</option>
            <option value='4th'>4th option</option></select>"#;
        let se: SelectElement = Node::from_html(html).unwrap().try_into().unwrap();

        assert_eq!(se.multiple(), true);

        let empy_i32_vec: Vec<i32> = Vec::new();
        let empy_string_vec: Vec<String> = Vec::new();
        assert_eq!(se.selected_indices(), empy_i32_vec);
        assert_eq!(se.selected_values(), empy_string_vec);
    }

    #[test]
    fn test_select_multiple(){
         let html = r#"<select multiple><option value='first' selected>First option</option>
            <option value='second'>Second option</option>
            <option value='third' selected>Third option</option>
            <option value='4th'>4th option</option>
            <option value='' selected>Empty</option></select>"#;
        let se: SelectElement = Node::from_html(html).unwrap().try_into().unwrap();

        assert_eq!(se.multiple(), true);

        assert_eq!(se.selected_indices(), vec![0,2,4]);
        assert_eq!(se.selected_values(), vec!["first".to_string(), "third".to_string(), "".to_string()]);
    }
}