Back to index

lightning-sunbird  0.9+nobinonly
nsFrameList.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include "nsFrameList.h"
00039 #ifdef NS_DEBUG
00040 #include "nsIFrameDebug.h"
00041 #endif
00042 #include "nsVoidArray.h"
00043 #include "nsLayoutUtils.h"
00044 
00045 #ifdef IBMBIDI
00046 #include "nsCOMPtr.h"
00047 #include "nsLayoutAtoms.h"
00048 #include "nsILineIterator.h"
00049 
00054 #define LINE_MIN 0
00055 #define XCOORD_MIN 0x80000000
00056 #define MY_LINE_MAX 0x7fffffff
00057 #define XCOORD_MAX 0x7fffffff
00058 class nsFrameOrigin {
00059 public:
00060   // default constructor
00061   nsFrameOrigin() {
00062     mLine = 0;
00063     mXCoord = 0;
00064     mDirection = NS_STYLE_DIRECTION_LTR;
00065   }
00066 
00067   nsFrameOrigin(PRInt32 line, nscoord xCoord, PRUint8 direction) {
00068     mLine = line;
00069     mXCoord = xCoord;
00070     mDirection = direction;
00071   }
00072 
00073   // copy constructor
00074   nsFrameOrigin(const nsFrameOrigin& aFrameOrigin) {
00075     mLine = aFrameOrigin.mLine;
00076     mXCoord = aFrameOrigin.mXCoord;
00077     mDirection = aFrameOrigin.mDirection;
00078   }
00079 
00080   ~nsFrameOrigin() {
00081   }
00082 
00083   nsFrameOrigin& operator=(const nsFrameOrigin& aFrameOrigin) { 
00084     mLine = aFrameOrigin.mLine;
00085     mXCoord = aFrameOrigin.mXCoord;
00086     mDirection = aFrameOrigin.mDirection;
00087     return *this;
00088   }
00089 
00090   PRBool operator<(const nsFrameOrigin& aFrameOrigin) {
00091     if (mLine < aFrameOrigin.mLine) {
00092       return PR_TRUE;
00093     }
00094     if (mLine > aFrameOrigin.mLine) {
00095       return PR_FALSE;
00096     }
00097     if (mDirection == NS_STYLE_DIRECTION_LTR && mXCoord < aFrameOrigin.mXCoord ||
00098         mDirection == NS_STYLE_DIRECTION_RTL && mXCoord > aFrameOrigin.mXCoord) {
00099       return PR_TRUE;
00100     }
00101     return PR_FALSE;
00102   }
00103   
00104   PRBool operator>(const nsFrameOrigin& aFrameOrigin) {
00105     if (mLine > aFrameOrigin.mLine) {
00106       return PR_TRUE;
00107     }
00108     if (mLine < aFrameOrigin.mLine) {
00109       return PR_FALSE;
00110     }
00111     if (mDirection == NS_STYLE_DIRECTION_LTR && mXCoord > aFrameOrigin.mXCoord ||
00112         mDirection == NS_STYLE_DIRECTION_RTL && mXCoord < aFrameOrigin.mXCoord) {
00113       return PR_TRUE;
00114     }
00115     return PR_FALSE;
00116   }
00117 
00118   PRBool operator==(const nsFrameOrigin& aFrameOrigin) {
00119     if (aFrameOrigin.mLine == mLine && aFrameOrigin.mXCoord == mXCoord) {
00120       return PR_TRUE;
00121     }
00122     return PR_FALSE;
00123   }
00124 
00125 protected:
00126   PRInt32 mLine;
00127   nscoord mXCoord;
00128   PRUint8 mDirection;
00129 };
00130 #endif // IBMBIDI
00131 
00132 void
00133 nsFrameList::DestroyFrames(nsPresContext* aPresContext)
00134 {
00135   nsIFrame* next;
00136   for (nsIFrame* frame = mFirstChild; frame; frame = next) {
00137     next = frame->GetNextSibling();
00138     frame->Destroy(aPresContext);
00139     mFirstChild = next;
00140   }
00141 }
00142 
00143 void
00144 nsFrameList::AppendFrames(nsIFrame* aParent, nsIFrame* aFrameList)
00145 {
00146   NS_PRECONDITION(nsnull != aFrameList, "null ptr");
00147   if (nsnull != aFrameList) {
00148     nsIFrame* lastChild = LastChild();
00149     if (nsnull == lastChild) {
00150       mFirstChild = aFrameList;
00151     }
00152     else {
00153       lastChild->SetNextSibling(aFrameList);
00154     }
00155     if (aParent) {
00156       for (nsIFrame* frame = aFrameList; frame;
00157            frame = frame->GetNextSibling()) {
00158         frame->SetParent(aParent);
00159       }
00160     }
00161   }
00162 #ifdef DEBUG
00163   CheckForLoops();
00164 #endif
00165 }
00166 
00167 void
00168 nsFrameList::AppendFrame(nsIFrame* aParent, nsIFrame* aFrame)
00169 {
00170   NS_PRECONDITION(nsnull != aFrame, "null ptr");
00171   if (nsnull != aFrame) {
00172     NS_PRECONDITION(!aFrame->GetNextSibling(), "Can only append one frame here");
00173     nsIFrame* lastChild = LastChild();
00174     if (nsnull == lastChild) {
00175       mFirstChild = aFrame;
00176     }
00177     else {
00178       lastChild->SetNextSibling(aFrame);
00179     }
00180     if (nsnull != aParent) {
00181       aFrame->SetParent(aParent);
00182     }
00183   }
00184 #ifdef DEBUG
00185   CheckForLoops();
00186 #endif
00187 }
00188 
00189 PRBool
00190 nsFrameList::RemoveFrame(nsIFrame* aFrame, nsIFrame* aPrevSiblingHint)
00191 {
00192   NS_PRECONDITION(nsnull != aFrame, "null ptr");
00193   if (aFrame) {
00194     nsIFrame* nextFrame = aFrame->GetNextSibling();
00195     if (aFrame == mFirstChild) {
00196       mFirstChild = nextFrame;
00197       aFrame->SetNextSibling(nsnull);
00198       return PR_TRUE;
00199     }
00200     else {
00201       nsIFrame* prevSibling = aPrevSiblingHint;
00202       if (!prevSibling || prevSibling->GetNextSibling() != aFrame) {
00203         prevSibling = GetPrevSiblingFor(aFrame);
00204       }
00205       if (prevSibling) {
00206         prevSibling->SetNextSibling(nextFrame);
00207         aFrame->SetNextSibling(nsnull);
00208         return PR_TRUE;
00209       }
00210     }
00211   }
00212   // aFrame was not in the list. 
00213   return PR_FALSE;
00214 }
00215 
00216 PRBool
00217 nsFrameList::RemoveFirstChild()
00218 {
00219   if (mFirstChild) {
00220     nsIFrame* nextFrame = mFirstChild->GetNextSibling();
00221     mFirstChild->SetNextSibling(nsnull);
00222     mFirstChild = nextFrame;
00223     return PR_TRUE;
00224   }
00225   return PR_FALSE;
00226 }
00227 
00228 PRBool
00229 nsFrameList::DestroyFrame(nsPresContext* aPresContext, nsIFrame* aFrame)
00230 {
00231   NS_PRECONDITION(nsnull != aFrame, "null ptr");
00232   if (RemoveFrame(aFrame)) {
00233     aFrame->Destroy(aPresContext);
00234     return PR_TRUE;
00235   }
00236   return PR_FALSE;
00237 }
00238 
00239 void
00240 nsFrameList::InsertFrame(nsIFrame* aParent,
00241                          nsIFrame* aPrevSibling,
00242                          nsIFrame* aNewFrame)
00243 {
00244   NS_PRECONDITION(nsnull != aNewFrame, "null ptr");
00245   if (nsnull != aNewFrame) {
00246     if (nsnull == aPrevSibling) {
00247       aNewFrame->SetNextSibling(mFirstChild);
00248       mFirstChild = aNewFrame;
00249     }
00250     else {
00251       nsIFrame* nextFrame = aPrevSibling->GetNextSibling();
00252       aPrevSibling->SetNextSibling(aNewFrame);
00253       aNewFrame->SetNextSibling(nextFrame);
00254     }
00255     if (aParent) {
00256       aNewFrame->SetParent(aParent);
00257     }
00258   }
00259 #ifdef DEBUG
00260   CheckForLoops();
00261 #endif
00262 }
00263 
00264 void
00265 nsFrameList::InsertFrames(nsIFrame* aParent,
00266                           nsIFrame* aPrevSibling,
00267                           nsIFrame* aFrameList)
00268 {
00269   NS_PRECONDITION(nsnull != aFrameList, "null ptr");
00270   if (nsnull != aFrameList) {
00271     nsIFrame* lastNewFrame = nsnull;
00272     if (aParent) {
00273       for (nsIFrame* frame = aFrameList; frame;
00274            frame = frame->GetNextSibling()) {
00275         frame->SetParent(aParent);
00276         lastNewFrame = frame;
00277       }
00278     }
00279 
00280     // Get the last new frame if necessary
00281     if (!lastNewFrame) {
00282       nsFrameList tmp(aFrameList);
00283       lastNewFrame = tmp.LastChild();
00284     }
00285 
00286     // Link the new frames into the child list
00287     if (nsnull == aPrevSibling) {
00288       lastNewFrame->SetNextSibling(mFirstChild);
00289       mFirstChild = aFrameList;
00290     }
00291     else {
00292       nsIFrame* nextFrame = aPrevSibling->GetNextSibling();
00293       aPrevSibling->SetNextSibling(aFrameList);
00294       lastNewFrame->SetNextSibling(nextFrame);
00295     }
00296   }
00297 #ifdef DEBUG
00298   CheckForLoops();
00299 #endif
00300 }
00301 
00302 PRBool
00303 nsFrameList::DoReplaceFrame(nsIFrame* aParent,
00304                             nsIFrame* aOldFrame,
00305                             nsIFrame* aNewFrame)
00306 {
00307   NS_PRECONDITION(aOldFrame, "null ptr");
00308   NS_PRECONDITION(aNewFrame, "null ptr");
00309   if (!aOldFrame || !aNewFrame) {
00310     return PR_FALSE;
00311   }
00312   
00313   nsIFrame* nextFrame = aOldFrame->GetNextSibling();
00314   if (aOldFrame == mFirstChild) {
00315     mFirstChild = aNewFrame;
00316   }
00317   else {
00318     nsIFrame* prevSibling = GetPrevSiblingFor(aOldFrame);
00319     if (!prevSibling) {
00320       NS_WARNING("nsFrameList::ReplaceFrame: aOldFrame not found in list");
00321       return PR_FALSE;
00322     }
00323     prevSibling->SetNextSibling(aNewFrame);
00324   }
00325 
00326   aNewFrame->SetNextSibling(nextFrame);
00327   
00328   if (aParent) {
00329     aNewFrame->SetParent(aParent);
00330   }
00331 #ifdef DEBUG
00332   CheckForLoops();
00333 #endif
00334   return PR_TRUE;
00335 }
00336 
00337 PRBool
00338 nsFrameList::ReplaceFrame(nsIFrame* aParent,
00339                           nsIFrame* aOldFrame,
00340                           nsIFrame* aNewFrame,
00341                           PRBool aDestroy)
00342 {
00343   NS_PRECONDITION(aOldFrame, "null ptr");
00344   NS_PRECONDITION(aNewFrame, "null ptr");
00345   if (DoReplaceFrame(aParent, aOldFrame, aNewFrame)) {
00346     if (aDestroy) {
00347       aOldFrame->Destroy(aOldFrame->GetPresContext());
00348     }
00349     return PR_TRUE;
00350   }
00351   return PR_FALSE;
00352 }
00353 
00354 PRBool
00355 nsFrameList::Split(nsIFrame* aAfterFrame, nsIFrame** aNextFrameResult)
00356 {
00357   NS_PRECONDITION(nsnull != aAfterFrame, "null ptr");
00358   NS_PRECONDITION(nsnull != aNextFrameResult, "null ptr");
00359   NS_ASSERTION(ContainsFrame(aAfterFrame), "split after unknown frame");
00360 
00361   if (aNextFrameResult && aAfterFrame) {
00362     nsIFrame* nextFrame = aAfterFrame->GetNextSibling();
00363     aAfterFrame->SetNextSibling(nsnull);
00364     *aNextFrameResult = nextFrame;
00365     return PR_TRUE;
00366   }
00367   return PR_FALSE;
00368 }
00369 
00370 nsIFrame*
00371 nsFrameList::LastChild() const
00372 {
00373   nsIFrame* frame = mFirstChild;
00374   if (!frame) {
00375     return nsnull;
00376   }
00377 
00378   nsIFrame* next = frame->GetNextSibling();
00379   while (next) {
00380     frame = next;
00381     next = frame->GetNextSibling();
00382   }
00383   return frame;
00384 }
00385 
00386 nsIFrame*
00387 nsFrameList::FrameAt(PRInt32 aIndex) const
00388 {
00389   NS_PRECONDITION(aIndex >= 0, "invalid arg");
00390   if (aIndex < 0) return nsnull;
00391   nsIFrame* frame = mFirstChild;
00392   while ((aIndex-- > 0) && frame) {
00393     frame = frame->GetNextSibling();
00394   }
00395   return frame;
00396 }
00397 
00398 PRBool
00399 nsFrameList::ContainsFrame(const nsIFrame* aFrame) const
00400 {
00401   NS_PRECONDITION(nsnull != aFrame, "null ptr");
00402   nsIFrame* frame = mFirstChild;
00403   while (frame) {
00404     if (frame == aFrame) {
00405       return PR_TRUE;
00406     }
00407     frame = frame->GetNextSibling();
00408   }
00409   return PR_FALSE;
00410 }
00411 
00412 PRInt32
00413 nsFrameList::GetLength() const
00414 {
00415   PRInt32 count = 0;
00416   nsIFrame* frame = mFirstChild;
00417   while (frame) {
00418     count++;
00419     frame = frame->GetNextSibling();
00420   }
00421   return count;
00422 }
00423 
00424 static int PR_CALLBACK CompareByContentOrder(const void* aF1, const void* aF2,
00425                                              void* aDummy)
00426 {
00427   const nsIFrame* f1 = NS_STATIC_CAST(const nsIFrame*, aF1);
00428   const nsIFrame* f2 = NS_STATIC_CAST(const nsIFrame*, aF2);
00429   if (f1->GetContent() != f2->GetContent()) {
00430     return nsLayoutUtils::CompareTreePosition(f1->GetContent(), f2->GetContent());
00431   }
00432 
00433   if (f1 == f2) {
00434     return 0;
00435   }
00436 
00437   const nsIFrame* f;
00438   for (f = f2; f; f = f->GetPrevInFlow()) {
00439     if (f == f1) {
00440       // f1 comes before f2 in the flow
00441       return -1;
00442     }
00443   }
00444   for (f = f1; f; f = f->GetPrevInFlow()) {
00445     if (f == f2) {
00446       // f1 comes after f2 in the flow
00447       return 1;
00448     }
00449   }
00450 
00451   NS_ASSERTION(PR_FALSE, "Frames for same content but not in relative flow order");
00452   return 0;
00453 }
00454 
00455 void
00456 nsFrameList::SortByContentOrder()
00457 {
00458   if (!mFirstChild)
00459     return;
00460 
00461   nsAutoVoidArray array;
00462   nsIFrame* f;
00463   for (f = mFirstChild; f; f = f->GetNextSibling()) {
00464     array.AppendElement(f);
00465   }
00466   array.Sort(CompareByContentOrder, nsnull);
00467   f = mFirstChild = NS_STATIC_CAST(nsIFrame*, array.FastElementAt(0));
00468   for (PRInt32 i = 1; i < array.Count(); ++i) {
00469     nsIFrame* ff = NS_STATIC_CAST(nsIFrame*, array.FastElementAt(i));
00470     f->SetNextSibling(ff);
00471     f = ff;
00472   }
00473   f->SetNextSibling(nsnull);
00474 }
00475 
00476 nsIFrame*
00477 nsFrameList::GetPrevSiblingFor(nsIFrame* aFrame) const
00478 {
00479   NS_PRECONDITION(nsnull != aFrame, "null ptr");
00480   if (aFrame == mFirstChild) {
00481     return nsnull;
00482   }
00483   nsIFrame* frame = mFirstChild;
00484   while (frame) {
00485     nsIFrame* next = frame->GetNextSibling();
00486     if (next == aFrame) {
00487       break;
00488     }
00489     frame = next;
00490   }
00491   return frame;
00492 }
00493 
00494 void
00495 nsFrameList::VerifyParent(nsIFrame* aParent) const
00496 {
00497 #ifdef NS_DEBUG
00498   for (nsIFrame* frame = mFirstChild; frame;
00499        frame = frame->GetNextSibling()) {
00500     NS_ASSERTION(frame->GetParent() == aParent, "bad parent");
00501   }
00502 #endif
00503 }
00504 
00505 #ifdef NS_DEBUG
00506 void
00507 nsFrameList::List(nsPresContext* aPresContext, FILE* out) const
00508 {
00509   fputs("<\n", out);
00510   for (nsIFrame* frame = mFirstChild; frame;
00511        frame = frame->GetNextSibling()) {
00512     nsIFrameDebug*  frameDebug;
00513     if (NS_SUCCEEDED(frame->QueryInterface(NS_GET_IID(nsIFrameDebug), (void**)&frameDebug))) {
00514       frameDebug->List(aPresContext, out, 1);
00515     }
00516   }
00517   fputs(">\n", out);
00518 }
00519 #endif
00520 
00521 #ifdef IBMBIDI
00522 nsIFrame*
00523 nsFrameList::GetPrevVisualFor(nsIFrame* aFrame) const
00524 {
00525   NS_PRECONDITION(nsnull != aFrame, "null ptr");
00526   nsILineIterator* iter;
00527 
00528   if (aFrame->GetType() == nsLayoutAtoms::blockFrame)
00529     return GetPrevSiblingFor(aFrame);
00530 
00531   nsIFrame* frame;
00532   nsIFrame* furthestFrame = nsnull;
00533 
00534   frame = mFirstChild;
00535 
00536   nsIFrame* blockFrame = aFrame->GetParent();
00537   if (!blockFrame)
00538     return GetPrevSiblingFor(aFrame);
00539 
00540   PRUint8 direction = blockFrame->GetStyleVisibility()->mDirection;
00541 
00542   nsresult result = blockFrame->QueryInterface(NS_GET_IID(nsILineIterator), (void**)&iter);
00543   if (NS_FAILED(result) || !iter) { // If the parent is not a block frame, just check all the siblings
00544 
00545     nsFrameOrigin maxOrig(0, direction == NS_STYLE_DIRECTION_LTR ? XCOORD_MIN : XCOORD_MAX, direction);
00546     nsFrameOrigin limOrig(0, aFrame->GetRect().x, direction);
00547     
00548     while (frame) {
00549       nsFrameOrigin testOrig(0, frame->GetRect().x, direction);
00550       if (testOrig > maxOrig && testOrig < limOrig) { // we are looking for the highest value less than the current one
00551         maxOrig = testOrig;
00552         furthestFrame = frame;
00553       }
00554       frame = frame->GetNextSibling();
00555     }
00556     return furthestFrame;
00557 
00558   }
00559 
00560   // Otherwise use the LineIterator to check the siblings on this line and the previous line
00561   if (!blockFrame || !iter)
00562     return nsnull;
00563 
00564   nsFrameOrigin maxOrig(LINE_MIN, direction == NS_STYLE_DIRECTION_LTR ? XCOORD_MIN : XCOORD_MAX, direction);
00565   PRInt32 testLine, thisLine;
00566 
00567   result = iter->FindLineContaining(aFrame, &thisLine);
00568   if (NS_FAILED(result) || thisLine < 0)
00569     return nsnull;
00570 
00571   nsFrameOrigin limOrig(thisLine, aFrame->GetRect().x, direction);
00572 
00573   while (frame) {
00574     if (NS_SUCCEEDED(iter->FindLineContaining(frame, &testLine))
00575         && testLine >= 0
00576         && (testLine == thisLine || testLine == thisLine - 1)) {
00577       nsFrameOrigin testOrig(testLine, frame->GetRect().x, direction);
00578       if (testOrig > maxOrig && testOrig < limOrig) { // we are looking for the highest value less than the current one
00579         maxOrig = testOrig;
00580         furthestFrame = frame;
00581       }
00582     }
00583     frame = frame->GetNextSibling();
00584   }
00585   return furthestFrame;
00586 }
00587 
00588 nsIFrame*
00589 nsFrameList::GetNextVisualFor(nsIFrame* aFrame) const
00590 {
00591   NS_PRECONDITION(nsnull != aFrame, "null ptr");
00592   nsILineIterator* iter;
00593 
00594   if (aFrame->GetType() == nsLayoutAtoms::blockFrame) {
00595     return aFrame->GetNextSibling();
00596   }
00597 
00598   nsIFrame* frame;
00599   nsIFrame* nearestFrame = nsnull;
00600 
00601   frame = mFirstChild;
00602 
00603   nsIFrame* blockFrame = aFrame->GetParent();
00604   if (!blockFrame)
00605     return GetPrevSiblingFor(aFrame);
00606 
00607   PRUint8 direction = blockFrame->GetStyleVisibility()->mDirection;
00608   
00609   nsresult result = blockFrame->QueryInterface(NS_GET_IID(nsILineIterator), (void**)&iter);
00610   if (NS_FAILED(result) || !iter) { // If the parent is not a block frame, just check all the siblings
00611 
00612     nsFrameOrigin minOrig(0, direction == NS_STYLE_DIRECTION_LTR ? XCOORD_MAX : XCOORD_MIN, direction);
00613     nsFrameOrigin limOrig(0, aFrame->GetRect().x, direction);
00614     while (frame) {
00615       nsFrameOrigin testOrig(0, frame->GetRect().x, direction);
00616       if (testOrig < minOrig && testOrig > limOrig) { // we are looking for the lowest value greater than the current one
00617         minOrig = testOrig;
00618         nearestFrame = frame;
00619       }
00620       frame = frame->GetNextSibling();
00621     }
00622     return nearestFrame;
00623   }
00624 
00625   // Otherwise use the LineIterator to check the siblings on this line and the previous line
00626   if (!blockFrame || !iter)
00627     return nsnull;
00628 
00629   nsFrameOrigin minOrig(MY_LINE_MAX, direction == NS_STYLE_DIRECTION_LTR ? XCOORD_MAX : XCOORD_MIN, direction);
00630   PRInt32 testLine, thisLine;
00631 
00632   result = iter->FindLineContaining(aFrame, &thisLine);
00633   if (NS_FAILED(result) || thisLine < 0)
00634     return nsnull;
00635 
00636   nsFrameOrigin limOrig(thisLine, aFrame->GetRect().x, direction);
00637 
00638   while (frame) {
00639     if (NS_SUCCEEDED(iter->FindLineContaining(frame, &testLine))
00640         && testLine >= 0
00641         && (testLine == thisLine || testLine == thisLine + 1)) {
00642       nsFrameOrigin testOrig(testLine, frame->GetRect().x, direction);
00643       if (testOrig < minOrig && testOrig > limOrig) { // we are looking for the lowest value greater than the current one
00644         minOrig = testOrig;
00645         nearestFrame = frame;
00646       }
00647     }
00648     frame = frame->GetNextSibling();
00649   }
00650   return nearestFrame;
00651 }
00652 #endif
00653 
00654 #ifdef DEBUG
00655 void
00656 nsFrameList::CheckForLoops()
00657 {
00658   if (!mFirstChild) {
00659     return;
00660   }
00661   
00662   // Simple algorithm to find a loop in a linked list -- advance pointers
00663   // through it at speeds of 1 and 2, and if they ever get to be equal bail
00664   nsIFrame *first = mFirstChild, *second = mFirstChild;
00665   do {
00666     first = first->GetNextSibling();
00667     second = second->GetNextSibling();
00668     if (!second) {
00669       break;
00670     }
00671     second = second->GetNextSibling();
00672     if (first == second) {
00673       // Loop detected!  Since second advances faster, they can't both be null;
00674       // we would have broken out of the loop long ago.
00675       NS_ERROR("loop in frame list.  This will probably hang soon.");
00676       break;
00677     }                           
00678   } while (first && second);
00679 }
00680 #endif