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: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
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 : * Esben Mose Hansen.
20 : *
21 : * Contributor(s):
22 : * Esben Mose Hansen <esben@oek.dk> (original author)
23 : * L. David Baron <dbaron@dbaron.org>
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 MPL, 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 MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /* base class for nsCounterList and nsQuoteList */
40 :
41 : #include "nsGenConList.h"
42 : #include "nsLayoutUtils.h"
43 :
44 : void
45 0 : nsGenConList::Clear()
46 : {
47 : //Delete entire list
48 0 : if (!mFirstNode)
49 0 : return;
50 0 : for (nsGenConNode *node = Next(mFirstNode); node != mFirstNode;
51 0 : node = Next(mFirstNode))
52 : {
53 0 : Remove(node);
54 0 : delete node;
55 : }
56 0 : delete mFirstNode;
57 :
58 0 : mFirstNode = nsnull;
59 0 : mSize = 0;
60 : }
61 :
62 : bool
63 0 : nsGenConList::DestroyNodesFor(nsIFrame* aFrame)
64 : {
65 0 : if (!mFirstNode)
66 0 : return false; // list empty
67 : nsGenConNode* node;
68 0 : bool destroyed = false;
69 0 : while (mFirstNode->mPseudoFrame == aFrame) {
70 0 : destroyed = true;
71 0 : node = Next(mFirstNode);
72 0 : bool isLastNode = node == mFirstNode; // before they're dangling
73 0 : Remove(mFirstNode);
74 0 : delete mFirstNode;
75 0 : if (isLastNode) {
76 0 : mFirstNode = nsnull;
77 0 : return true;
78 : }
79 : else {
80 0 : mFirstNode = node;
81 : }
82 : }
83 0 : node = Next(mFirstNode);
84 0 : while (node != mFirstNode) {
85 0 : if (node->mPseudoFrame == aFrame) {
86 0 : destroyed = true;
87 0 : nsGenConNode *nextNode = Next(node);
88 0 : Remove(node);
89 0 : delete node;
90 0 : node = nextNode;
91 : } else {
92 0 : node = Next(node);
93 : }
94 : }
95 0 : return destroyed;
96 : }
97 :
98 : /**
99 : * Compute the type of the pseudo and the content for the pseudo that
100 : * we'll use for comparison purposes.
101 : * @param aContent the content to use is stored here; it's the element
102 : * that generated the ::before or ::after content, or (if not for generated
103 : * content), the frame's own element
104 : * @return -1 for ::before, +1 for ::after, and 0 otherwise.
105 : */
106 0 : inline PRInt32 PseudoCompareType(nsIFrame* aFrame, nsIContent** aContent)
107 : {
108 0 : nsIAtom *pseudo = aFrame->GetStyleContext()->GetPseudo();
109 0 : if (pseudo == nsCSSPseudoElements::before) {
110 0 : *aContent = aFrame->GetContent()->GetParent();
111 0 : return -1;
112 : }
113 0 : if (pseudo == nsCSSPseudoElements::after) {
114 0 : *aContent = aFrame->GetContent()->GetParent();
115 0 : return 1;
116 : }
117 0 : *aContent = aFrame->GetContent();
118 0 : return 0;
119 : }
120 :
121 : /* static */ bool
122 0 : nsGenConList::NodeAfter(const nsGenConNode* aNode1, const nsGenConNode* aNode2)
123 : {
124 0 : nsIFrame *frame1 = aNode1->mPseudoFrame;
125 0 : nsIFrame *frame2 = aNode2->mPseudoFrame;
126 0 : if (frame1 == frame2) {
127 0 : NS_ASSERTION(aNode2->mContentIndex != aNode1->mContentIndex, "identical");
128 0 : return aNode1->mContentIndex > aNode2->mContentIndex;
129 : }
130 : nsIContent *content1;
131 : nsIContent *content2;
132 0 : PRInt32 pseudoType1 = PseudoCompareType(frame1, &content1);
133 0 : PRInt32 pseudoType2 = PseudoCompareType(frame2, &content2);
134 0 : if (pseudoType1 == 0 || pseudoType2 == 0) {
135 0 : if (content1 == content2) {
136 0 : NS_ASSERTION(pseudoType1 != pseudoType2, "identical");
137 0 : return pseudoType2 == 0;
138 : }
139 : // We want to treat an element as coming before its :before (preorder
140 : // traversal), so treating both as :before now works.
141 0 : if (pseudoType1 == 0) pseudoType1 = -1;
142 0 : if (pseudoType2 == 0) pseudoType2 = -1;
143 : } else {
144 0 : if (content1 == content2) {
145 0 : NS_ASSERTION(pseudoType1 != pseudoType2, "identical");
146 0 : return pseudoType1 == 1;
147 : }
148 : }
149 : // XXX Switch to the frame version of DoCompareTreePosition?
150 : PRInt32 cmp = nsLayoutUtils::DoCompareTreePosition(content1, content2,
151 0 : pseudoType1, -pseudoType2);
152 0 : NS_ASSERTION(cmp != 0, "same content, different frames");
153 0 : return cmp > 0;
154 : }
155 :
156 : void
157 0 : nsGenConList::Insert(nsGenConNode* aNode)
158 : {
159 0 : if (mFirstNode) {
160 : // Check for append.
161 0 : if (NodeAfter(aNode, Prev(mFirstNode))) {
162 0 : PR_INSERT_BEFORE(aNode, mFirstNode);
163 : }
164 : else {
165 : // Binary search.
166 :
167 : // the range of indices at which |aNode| could end up.
168 : // (We already know it can't be at index mSize.)
169 0 : PRUint32 first = 0, last = mSize - 1;
170 :
171 : // A cursor to avoid walking more than the length of the list.
172 0 : nsGenConNode *curNode = Prev(mFirstNode);
173 0 : PRUint32 curIndex = mSize - 1;
174 :
175 0 : while (first != last) {
176 0 : PRUint32 test = (first + last) / 2;
177 0 : if (last == curIndex) {
178 0 : for ( ; curIndex != test; --curIndex)
179 0 : curNode = Prev(curNode);
180 : } else {
181 0 : for ( ; curIndex != test; ++curIndex)
182 0 : curNode = Next(curNode);
183 : }
184 :
185 0 : if (NodeAfter(aNode, curNode)) {
186 0 : first = test + 1;
187 : // if we exit the loop, we need curNode to be right
188 0 : ++curIndex;
189 0 : curNode = Next(curNode);
190 : } else {
191 0 : last = test;
192 : }
193 : }
194 0 : PR_INSERT_BEFORE(aNode, curNode);
195 0 : if (curNode == mFirstNode) {
196 0 : mFirstNode = aNode;
197 : }
198 : }
199 : }
200 : else {
201 : // initialize list with first node
202 0 : PR_INIT_CLIST(aNode);
203 0 : mFirstNode = aNode;
204 : }
205 0 : ++mSize;
206 :
207 0 : NS_ASSERTION(aNode == mFirstNode || NodeAfter(aNode, Prev(aNode)),
208 : "sorting error");
209 0 : NS_ASSERTION(IsLast(aNode) || NodeAfter(Next(aNode), aNode),
210 : "sorting error");
211 0 : }
|