Imported version 0.1
[videolink.git] / linkiterator.cpp
1 // Copyright 2005 Ben Hutchings <ben@decadentplace.org.uk>.
2 // See the file "COPYING" for licence details.
3
4 #include "linkiterator.hpp"
5
6 #include <cassert>
7
8 #include <nsIDOMHTMLCollection.h>
9 #include <nsIDOMHTMLDocument.h>
10
11 LinkIterator::LinkIterator()
12 {}
13
14 LinkIterator::LinkIterator(nsIDOMDocument * document)
15 {
16     nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(document));
17     if (!htmlDoc)
18         return;
19
20     htmlDoc->GetLinks(getter_AddRefs(collection_));
21     assert(collection_);
22
23     index_ = 0;
24     length_ = 0;
25     collection_->GetLength(&length_);
26     if (length_ == 0)
27         collection_ = 0;
28 }
29
30 already_AddRefed<nsIDOMNode> LinkIterator::operator*() const
31 {
32     assert(collection_);
33     nsIDOMNode * result = 0;
34     collection_->Item(index_, &result);
35     assert(result);
36     return dont_AddRef(result);
37 }
38
39 LinkIterator & LinkIterator::operator++()
40 {
41     assert(collection_);
42     ++index_;
43     if (index_ == length_)
44         collection_ = 0;
45     return *this;
46 }
47
48 bool LinkIterator::operator==(const LinkIterator & other) const
49 {
50     return (collection_ == other.collection_
51             && (!collection_ || index_ == other.index_));
52 }