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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use webcore::value::{Value, Reference};
use webcore::try_from::TryInto;
use webapi::cross_origin_setting::CrossOriginSetting;
use webapi::event_target::{IEventTarget, EventTarget};
use webapi::node::{INode, Node};
use webapi::element::{IElement, Element};
use webapi::html_element::{IHtmlElement, HtmlElement};

/// The HTML image element is used to manipulate the layout and presentation of
/// `<img>` elements.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement)
// https://html.spec.whatwg.org/#htmlimageelement
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "HTMLImageElement")]
#[reference(subclass_of(EventTarget, Node, Element, HtmlElement))]
pub struct ImageElement( Reference );

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

impl ImageElement {
    /// Constructs a new ImageElement.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image)
    // https://html.spec.whatwg.org/#the-img-element:dom-image
    pub fn new() -> ImageElement {
        js! (
            return new Image();
        ).try_into().unwrap()
    }

    /// Constructs a new ImageElement with the given width and height.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image)
    // https://html.spec.whatwg.org/#the-img-element:dom-image
    pub fn with_size(width: u32, height: u32) -> ImageElement {
        js! (
            return new Image(@{width}, @{height});
        ).try_into().unwrap()
    }

    /// Returns the HTML `alt` attribute, representing the fallback context for the image.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-alt
    #[inline]
    pub fn alt( &self ) -> String {
        js! (
            return @{self}.alt;
        ).try_into().unwrap()
    }

    /// Sets the HTML `alt` attribute, representing the fallback context for the image.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-alt
    pub fn set_alt( &self, value: &str ) {
        js! { @(no_return)
            @{self}.alt = @{value};
        }
    }

    /// Returns true if the browser has finished fetching the image, whether
    /// successful or not. It also return true if the image has no src value.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/complete)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-complete
    pub fn complete( &self ) -> bool {
        js! (
            return @{self}.complete;
        ).try_into().unwrap()
    }

    /// Returns the Cross-Origin Resource Sharing (CORS) setting for the image.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/crossOrigin)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-crossorigin
    pub fn cross_origin( &self ) -> CrossOriginSetting {
        match js!(
            return @{self}.crossOrigin;
        ) {
            Value::Null => CrossOriginSetting::None,
            Value::String( ref s ) if *s == "anonymous" => CrossOriginSetting::Anonymous,
            Value::String( ref s ) if *s == "use-credentials" => CrossOriginSetting::UseCredentials,
            _ => unreachable!("Unexpected crossOrigin value")
        }
    }

    /// Sets the Cross-Origin Resource Sharing (CORS) setting for the image.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/crossOrigin)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-crossorigin
    pub fn set_cross_origin( &self, value: CrossOriginSetting ) {
        js! { @(no_return)
            @{self}.crossOrigin = @{
                match value {
                    CrossOriginSetting::None => None,
                    CrossOriginSetting::Anonymous => Some("anonymous"),
                    CrossOriginSetting::UseCredentials => Some("use-credentials")
                }
            }
        }
    }

    /// Returns the the rendered height of the image in CSS pixels.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/height)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-height
    pub fn height( &self ) -> u32 {
        js! (
            return @{self}.height;
        ).try_into().unwrap()
    }

    /// Sets the the rendered height of the image in CSS pixels.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/height)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-height
    pub fn set_height( &self, value: u32 ) {
        js! { @(no_return)
            @{self}.height = @{value};
        }
    }

    /// Indicates whether the image is part of a server-side image map.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/isMap)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-ismap
    pub fn is_map( &self ) -> bool {
        js!(
            return @{self}.isMap;
        ).try_into().unwrap()
    }

    /// Sets whether the image is part of a server-side image map.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/isMap)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-ismap
    pub fn set_is_map( &self, value: bool ) {
        js! { @(no_return)
            @{self}.isMap = @{value};
        }
    }

    /// Returns the intrinsic height of the image in CSS pixels, if it is available.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeight)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-naturalheight
    pub fn natural_height( &self ) -> Option< u32 > {
        match js!(
            return @{self}.naturalHeight;
        ).try_into().unwrap() {
            0 => None,
            value => Some( value )
        }
    }

    /// Returns the intrinsic width of the image in CSS pixels, if it is available.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalWidth)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-naturalwidth
    pub fn natural_width( &self ) -> Option< u32 > {
        match js!(
            return @{self}.naturalWidth;
        ).try_into().unwrap() {
            0 => None,
            value => Some( value )
        }
    }

    /// Returns the full URL of the image, including the base URI.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/src)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-src
    pub fn src( &self ) -> String {
        js! (
            return @{self}.src;
        ).try_into().unwrap()
    }

    /// Sets the full URL of the image, including the base URI.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/src)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-src
    pub fn set_src( &self, value: &str ) {
        js! { @(no_return)
            @{self}.src = @{value};
        }
    }

    /// Returns the `usemap` HTML attribute, containing a partial URL of a map element.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/useMap)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-usemap
    pub fn use_map( &self ) -> String {
        js!(
            return @{self}.useMap;
        ).try_into().unwrap()
    }

    /// Sets the `usemap` HTML attribute, containing a partial URL of a map element.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/useMap)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-usemap
    pub fn set_use_map( &self, value: &str ) {
        js! { @(no_return)
             @{self}.useMap = @{value};
        }
    }

    /// Returns the rendered width of the image in CSS pixels.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/width)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-width
    pub fn width( &self ) -> u32 {
        js! (
            return @{self}.width;
        ).try_into().unwrap()
    }

    /// Sets the rendered width of the image in CSS pixels.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/width)
    // https://html.spec.whatwg.org/#the-img-element:dom-img-width
    pub fn set_width( &self, value: u32 ) {
        js! { @(no_return)
            @{self}.width = @{value};
        }
    }
}

#[cfg(all(test, feature = "web_test"))]
mod tests {
    use super::*;

    #[test]
    fn test_new() {
        let image = ImageElement::new();
        assert_eq!(image.alt(), "");
    }

    #[test]
    fn test_with_size() {
        let image = ImageElement::with_size(333, 444);
        assert_eq!(image.width(), 333);
        assert_eq!(image.height(), 444);
    }

    #[test]
    fn test_alt() {
        let image = ImageElement::new();
        assert_eq!(image.alt(), "");
        image.set_alt("test");
        assert_eq!(image.alt(), "test");
    }

    #[test]
    fn test_complete() {
        let image = ImageElement::new();
        assert_eq!(image.complete(), true);
    }

    #[test]
    fn test_width_height() {
        let image = ImageElement::new();
        assert_eq!(image.width(), 0);
        assert_eq!(image.height(), 0);
        image.set_width(4);
        image.set_height(5);
        assert_eq!(image.width(), 4);
        assert_eq!(image.height(), 5);
    }

    #[test]
    fn test_src() {
        let image = ImageElement::new();
        assert_eq!(image.src(), "");
        image.set_src("http://example.com/image.gif");
        assert_eq!(image.src(), "http://example.com/image.gif");
    }

    #[test]
    fn test_use_map() {
        let image = ImageElement::new();
        assert_eq!(image.use_map(), "");
        image.set_use_map("test");
        assert_eq!(image.use_map(), "test");
    }

    #[test]
    fn test_natural_width_height() {
        let image = ImageElement::new();
        assert_eq!(image.natural_width(), None);
        assert_eq!(image.natural_height(), None);
    }

    #[test]
    fn test_cross_origin() {
        let image = ImageElement::new();
        assert_eq!(image.cross_origin(), CrossOriginSetting::None);
        image.set_cross_origin(CrossOriginSetting::Anonymous);
        assert_eq!(image.cross_origin(), CrossOriginSetting::Anonymous);
        image.set_cross_origin(CrossOriginSetting::UseCredentials);
        assert_eq!(image.cross_origin(), CrossOriginSetting::UseCredentials);
        image.set_cross_origin(CrossOriginSetting::None);
        assert_eq!(image.cross_origin(), CrossOriginSetting::None);
    }
}