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
use document::Document;
use bit_set::{self, BitSet};
use node::{self, Node};
use predicate::Predicate;

#[derive(Clone, Debug, PartialEq)]
pub struct Selection<'a> {
    document: &'a Document,
    bitset: BitSet
}

impl<'a> Selection<'a> {
    pub fn new(document: &'a Document, bitset: BitSet) -> Selection<'a> {
        Selection {
            document: document,
            bitset: bitset
        }
    }

    pub fn iter(&'a self) -> Iter<'a> {
        Iter {
            selection: self,
            inner: self.bitset.iter()
        }
    }

    pub fn filter<P: Predicate>(&self, p: P) -> Selection<'a> {
        Selection {
            document: self.document,
            bitset: self.bitset.iter().filter(|&index| {
                p.matches(&self.document.nth(index))
            }).collect()
        }
    }

    pub fn find<P: Predicate>(&self, p: P) -> Selection<'a> {
        let mut bitset = BitSet::new();

        for index in self.bitset.iter() {
            recur(self.document, &mut bitset, index);
        }

        return Selection {
            document: self.document,
            bitset: bitset.iter().filter(|&index| {
                p.matches(&self.document.nth(index))
            }).collect()
        };

        fn recur(document: &Document, bitset: &mut BitSet, index: usize) {
            if bitset.contains(&index) {
                return
            }

            match document.nodes[index].data {
                node::Data::Text(..) => {},
                node::Data::Element(_, _, ref children) => {
                    for &child in children {
                        recur(document, bitset, child);
                        bitset.insert(child);
                    }
                },
                node::Data::Comment(..) => {}
            }
        }
    }

    pub fn parent(&self) -> Selection<'a> {
        Selection {
            document: self.document,
            bitset: self.iter().filter_map(|node| {
                node.parent().map(|parent| parent.index())
            }).collect()
        }
    }

    pub fn prev(&self) -> Selection<'a> {
        Selection {
            document: self.document,
            bitset: self.iter().filter_map(|node| {
                node.prev().map(|prev| prev.index())
            }).collect()
        }
    }

    pub fn next(&self) -> Selection<'a> {
        Selection {
            document: self.document,
            bitset: self.iter().filter_map(|node| {
                node.next().map(|next| next.index())
            }).collect()
        }
    }

    pub fn parents(&self) -> Selection<'a> {
        let mut bitset = BitSet::new();
        for mut node in self.iter() {
            while let Some(parent) = node.parent() {
                bitset.insert(parent.index());
                node = parent;
            }
        }

        Selection {
            document: self.document,
            bitset: bitset
        }
    }

    pub fn children(&self) -> Selection<'a> {
        let mut bitset = BitSet::new();
        for node in self.iter() {
            match self.document.nodes[node.index()].data {
                node::Data::Text(_) => {},
                node::Data::Element(_, _, ref children) => {
                    for &child in children {
                        bitset.insert(child);
                    }
                },
                node::Data::Comment(..) => {}
            }
        }

        Selection {
            document: self.document,
            bitset: bitset
        }
    }

    pub fn first(&self) -> Option<Node<'a>> {
        self.bitset.iter().next().map(|index| self.document.nth(index))
    }
}

#[derive(Clone)]
pub struct Iter<'a> {
    selection: &'a Selection<'a>,
    inner: bit_set::Iter<'a, u32>
}

impl<'a> Iterator for Iter<'a> {
    type Item = Node<'a>;

    fn next(&mut self) -> Option<Node<'a>> {
        self.inner.next().map(|index| self.selection.document.nth(index))
    }
}