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
use webcore::value::{Value, Reference};
use webcore::try_from::TryInto;
use webcore::reference_type::ReferenceType;
use webapi::node::Node;
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "NodeList")]
pub struct NodeList( Reference );
impl NodeList {
pub fn len( &self ) -> u32 {
js!( return @{self}.length; ).try_into().unwrap()
}
pub fn item( &self, index: u32 ) -> Option< Node > {
js!(
return @{self}[ @{index} ];
).try_into().unwrap()
}
pub fn iter( &self ) -> NodeIter {
NodeIter {
list: self.clone(),
index: 0
}
}
}
impl IntoIterator for NodeList {
type Item = Node;
type IntoIter = NodeIter;
#[inline]
fn into_iter( self ) -> Self::IntoIter {
NodeIter {
list: self,
index: 0
}
}
}
impl< 'a > IntoIterator for &'a NodeList {
type Item = Node;
type IntoIter = NodeIter;
#[inline]
fn into_iter( self ) -> Self::IntoIter {
NodeIter {
list: self.clone(),
index: 0
}
}
}
#[derive(Debug)]
pub struct NodeIter {
list: NodeList,
index: i32
}
impl Iterator for NodeIter {
type Item = Node;
fn next( &mut self ) -> Option< Self::Item > {
let value = js!(
return @{&self.list}[ @{self.index} ];
);
let node = match value {
Value::Undefined => return None,
Value::Reference( reference ) => unsafe { Node::from_reference_unchecked( reference ) },
_ => unreachable!()
};
self.index += 1;
Some( node )
}
}