~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/layout/html/style/src/nsChildIterator.h

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
// vim:cindent:ts=2:et:sw=2:
 
3
/* ***** BEGIN LICENSE BLOCK *****
 
4
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
5
 *
 
6
 * The contents of this file are subject to the Netscape Public License
 
7
 * Version 1.1 (the "License"); you may not use this file except in
 
8
 * compliance with the License. You may obtain a copy of the License at
 
9
 * http://www.mozilla.org/NPL/
 
10
 *
 
11
 * Software distributed under the License is distributed on an "AS IS" basis,
 
12
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
13
 * for the specific language governing rights and limitations under the
 
14
 * License.
 
15
 *
 
16
 * The Original Code is mozilla.org code.
 
17
 *
 
18
 * The Initial Developer of the Original Code is 
 
19
 * Netscape Communications Corporation.
 
20
 * Portions created by the Initial Developer are Copyright (C) 1998
 
21
 * the Initial Developer. All Rights Reserved.
 
22
 *
 
23
 * Contributor(s):
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsCOMPtr.h"
 
40
#include "nsIContent.h"
 
41
#include "nsIDOMNodeList.h"
 
42
#include "nsIDOMNode.h"
 
43
 
 
44
/**
 
45
 * Helper class for iterating children during frame construction.
 
46
 * This class should always be used in lieu of the straight content
 
47
 * node APIs, since it handles XBL-generated anonymous content as
 
48
 * well.
 
49
 */
 
50
class ChildIterator
 
51
{
 
52
protected:
 
53
  nsCOMPtr<nsIContent> mContent;
 
54
  PRUint32 mIndex;
 
55
  nsCOMPtr<nsIDOMNodeList> mNodes;
 
56
 
 
57
public:
 
58
  ChildIterator()
 
59
    : mIndex(0) {}
 
60
 
 
61
  ChildIterator(const ChildIterator& aOther)
 
62
    : mContent(aOther.mContent),
 
63
      mIndex(aOther.mIndex),
 
64
      mNodes(aOther.mNodes) {}
 
65
 
 
66
  ChildIterator& operator=(const ChildIterator& aOther) {
 
67
    mContent = aOther.mContent;
 
68
    mIndex = aOther.mIndex;
 
69
    mNodes = aOther.mNodes;
 
70
    return *this;
 
71
  }
 
72
 
 
73
  ChildIterator& operator++() {
 
74
    ++mIndex;
 
75
    return *this;
 
76
  }
 
77
 
 
78
  ChildIterator operator++(int) {
 
79
    ChildIterator result(*this);
 
80
    ++mIndex;
 
81
    return result;
 
82
  }
 
83
 
 
84
  ChildIterator& operator--() {
 
85
    --mIndex;
 
86
    return *this;
 
87
  }
 
88
 
 
89
  ChildIterator operator--(int) {
 
90
    ChildIterator result(*this);
 
91
    --mIndex;
 
92
    return result;
 
93
  }
 
94
 
 
95
  already_AddRefed<nsIContent> get() const {
 
96
    nsIContent* result = nsnull;
 
97
    if (mNodes) {
 
98
      nsCOMPtr<nsIDOMNode> node;
 
99
      mNodes->Item(mIndex, getter_AddRefs(node));
 
100
      CallQueryInterface(node, &result);
 
101
    } else {
 
102
      result = mContent->GetChildAt(PRInt32(mIndex));
 
103
      NS_IF_ADDREF(result);
 
104
    }
 
105
 
 
106
    return result;
 
107
  }
 
108
 
 
109
  already_AddRefed<nsIContent> operator*() const { return get(); }
 
110
 
 
111
  PRBool operator==(const ChildIterator& aOther) const {
 
112
    return mContent == aOther.mContent && mIndex == aOther.mIndex;
 
113
  }
 
114
 
 
115
  PRBool operator!=(const ChildIterator& aOther) const {
 
116
    return !aOther.operator==(*this);
 
117
  }
 
118
 
 
119
  PRUint32 index() {
 
120
    return mIndex;
 
121
  }
 
122
 
 
123
  void seek(PRUint32 aIndex) {
 
124
    // Make sure that aIndex is reasonable.  This should be |#ifdef
 
125
    // DEBUG|, but we need these numbers for the temporary workaround
 
126
    // for bug 133219.
 
127
    PRUint32 length;
 
128
    if (mNodes)
 
129
      mNodes->GetLength(&length);
 
130
    else
 
131
      length = mContent->GetChildCount();
 
132
 
 
133
    NS_ASSERTION(PRInt32(aIndex) >= 0 && aIndex <= length, "out of bounds");
 
134
 
 
135
    // Temporary workaround for bug 133219.
 
136
    if (aIndex > length)
 
137
      aIndex = length;
 
138
 
 
139
    mIndex = aIndex;
 
140
  }
 
141
 
 
142
  /**
 
143
   * Create a pair of ChildIterators for a content node. aFirst will
 
144
   * point to the first child of aContent; aLast will point one past
 
145
   * the last child of aContent.
 
146
   */
 
147
  static nsresult Init(nsIContent*    aContent,
 
148
                       ChildIterator* aFirst,
 
149
                       ChildIterator* aLast);
 
150
};