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
use webcore::value::Reference;
use webcore::try_from::TryInto;
use webapi::element::Element;

/// The `HtmlCollection` interface represents a generic collection
/// (array-like object similar to arguments) of elements (in document order)
/// and offers methods and properties for selecting from the list.
/// 
/// An `HtmlCollection` in the HTML DOM is live; it is automatically
/// updated when the underlying document is changed.
/// 
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)
// https://dom.spec.whatwg.org/#interface-htmlcollection
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "HTMLCollection")]
pub struct HtmlCollection( Reference );

impl HtmlCollection {
    /// Returns the number of elements in the collection.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)
    // https://dom.spec.whatwg.org/#ref-for-dom-htmlcollection-length
    pub fn len( &self ) -> u32 {
        js!( return @{self}.length; ).try_into().unwrap()
    }

    /// Returns an element from an `HtmlCollection` by index.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection/item)
    // https://dom.spec.whatwg.org/#ref-for-dom-htmlcollection-item
    pub fn item( &self, index: u32 ) -> Option< Element > {
        js!(
            return @{self}.item(@{index});
        ).try_into().unwrap()
    }

    /// Returns an iterator over the collection.
    pub fn iter( &self ) -> ElementIter {
        ElementIter {
            list: self.clone(),
            index: 0
        }
    }
}


impl IntoIterator for HtmlCollection {
    type Item = Element;
    type IntoIter = ElementIter;

    #[inline]
    fn into_iter( self ) -> Self::IntoIter {
        ElementIter {
            list: self,
            index: 0
        }
    }
}

impl< 'a > IntoIterator for &'a HtmlCollection {
    type Item = Element;
    type IntoIter = ElementIter;

    #[inline]
    fn into_iter( self ) -> Self::IntoIter {
        ElementIter {
            list: self.clone(),
            index: 0
        }
    }
}

#[derive(Debug)]
pub struct ElementIter {
    list: HtmlCollection,
    index: u32
}

impl Iterator for ElementIter {
    type Item = Element;
    fn next( &mut self ) -> Option< Self::Item > {
        let item = self.list.item(self.index);
        self.index += 1;
        item
    }
}