michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsFrameList.h" michael@0: #include "nsIFrame.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsIPresShell.h" michael@0: michael@0: #include "nsGkAtoms.h" michael@0: #include "nsILineIterator.h" michael@0: #include "nsBidiPresUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace layout { michael@0: namespace detail { michael@0: const AlignedFrameListBytes gEmptyFrameListBytes = { 0 }; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void* michael@0: nsFrameList::operator new(size_t sz, nsIPresShell* aPresShell) CPP_THROW_NEW michael@0: { michael@0: return aPresShell->AllocateByObjectID(nsPresArena::nsFrameList_id, sz); michael@0: } michael@0: michael@0: void michael@0: nsFrameList::Delete(nsIPresShell* aPresShell) michael@0: { michael@0: NS_PRECONDITION(this != &EmptyList(), "Shouldn't Delete() this list"); michael@0: NS_ASSERTION(IsEmpty(), "Shouldn't Delete() a non-empty list"); michael@0: michael@0: aPresShell->FreeByObjectID(nsPresArena::nsFrameList_id, this); michael@0: } michael@0: michael@0: void michael@0: nsFrameList::DestroyFrames() michael@0: { michael@0: while (nsIFrame* frame = RemoveFirstChild()) { michael@0: frame->Destroy(); michael@0: } michael@0: mLastChild = nullptr; michael@0: } michael@0: michael@0: void michael@0: nsFrameList::DestroyFramesFrom(nsIFrame* aDestructRoot) michael@0: { michael@0: NS_PRECONDITION(aDestructRoot, "Missing destruct root"); michael@0: michael@0: while (nsIFrame* frame = RemoveFirstChild()) { michael@0: frame->DestroyFrom(aDestructRoot); michael@0: } michael@0: mLastChild = nullptr; michael@0: } michael@0: michael@0: void michael@0: nsFrameList::SetFrames(nsIFrame* aFrameList) michael@0: { michael@0: NS_PRECONDITION(!mFirstChild, "Losing frames"); michael@0: michael@0: mFirstChild = aFrameList; michael@0: mLastChild = nsLayoutUtils::GetLastSibling(mFirstChild); michael@0: } michael@0: michael@0: void michael@0: nsFrameList::RemoveFrame(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "null ptr"); michael@0: #ifdef DEBUG_FRAME_LIST michael@0: // ContainsFrame is O(N) michael@0: NS_PRECONDITION(ContainsFrame(aFrame), "wrong list"); michael@0: #endif michael@0: michael@0: nsIFrame* nextFrame = aFrame->GetNextSibling(); michael@0: if (aFrame == mFirstChild) { michael@0: mFirstChild = nextFrame; michael@0: aFrame->SetNextSibling(nullptr); michael@0: if (!nextFrame) { michael@0: mLastChild = nullptr; michael@0: } michael@0: } michael@0: else { michael@0: nsIFrame* prevSibling = aFrame->GetPrevSibling(); michael@0: NS_ASSERTION(prevSibling && prevSibling->GetNextSibling() == aFrame, michael@0: "Broken frame linkage"); michael@0: prevSibling->SetNextSibling(nextFrame); michael@0: aFrame->SetNextSibling(nullptr); michael@0: if (!nextFrame) { michael@0: mLastChild = prevSibling; michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsFrameList michael@0: nsFrameList::RemoveFramesAfter(nsIFrame* aAfterFrame) michael@0: { michael@0: if (!aAfterFrame) { michael@0: nsFrameList result; michael@0: result.InsertFrames(nullptr, nullptr, *this); michael@0: return result; michael@0: } michael@0: michael@0: NS_PRECONDITION(NotEmpty(), "illegal operation on empty list"); michael@0: #ifdef DEBUG_FRAME_LIST michael@0: NS_PRECONDITION(ContainsFrame(aAfterFrame), "wrong list"); michael@0: #endif michael@0: michael@0: nsIFrame* tail = aAfterFrame->GetNextSibling(); michael@0: // if (!tail) return EmptyList(); -- worth optimizing this case? michael@0: nsIFrame* oldLastChild = mLastChild; michael@0: mLastChild = aAfterFrame; michael@0: aAfterFrame->SetNextSibling(nullptr); michael@0: return nsFrameList(tail, tail ? oldLastChild : nullptr); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsFrameList::RemoveFirstChild() michael@0: { michael@0: if (mFirstChild) { michael@0: nsIFrame* firstChild = mFirstChild; michael@0: RemoveFrame(firstChild); michael@0: return firstChild; michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: nsFrameList::DestroyFrame(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "null ptr"); michael@0: RemoveFrame(aFrame); michael@0: aFrame->Destroy(); michael@0: } michael@0: michael@0: nsFrameList::Slice michael@0: nsFrameList::InsertFrames(nsIFrame* aParent, nsIFrame* aPrevSibling, michael@0: nsFrameList& aFrameList) michael@0: { michael@0: NS_PRECONDITION(aFrameList.NotEmpty(), "Unexpected empty list"); michael@0: michael@0: if (aParent) { michael@0: aFrameList.ApplySetParent(aParent); michael@0: } michael@0: michael@0: NS_ASSERTION(IsEmpty() || michael@0: FirstChild()->GetParent() == aFrameList.FirstChild()->GetParent(), michael@0: "frame to add has different parent"); michael@0: NS_ASSERTION(!aPrevSibling || michael@0: aPrevSibling->GetParent() == aFrameList.FirstChild()->GetParent(), michael@0: "prev sibling has different parent"); michael@0: #ifdef DEBUG_FRAME_LIST michael@0: // ContainsFrame is O(N) michael@0: NS_ASSERTION(!aPrevSibling || ContainsFrame(aPrevSibling), michael@0: "prev sibling is not on this list"); michael@0: #endif michael@0: michael@0: nsIFrame* firstNewFrame = aFrameList.FirstChild(); michael@0: nsIFrame* nextSibling; michael@0: if (aPrevSibling) { michael@0: nextSibling = aPrevSibling->GetNextSibling(); michael@0: aPrevSibling->SetNextSibling(firstNewFrame); michael@0: } michael@0: else { michael@0: nextSibling = mFirstChild; michael@0: mFirstChild = firstNewFrame; michael@0: } michael@0: michael@0: nsIFrame* lastNewFrame = aFrameList.LastChild(); michael@0: lastNewFrame->SetNextSibling(nextSibling); michael@0: if (!nextSibling) { michael@0: mLastChild = lastNewFrame; michael@0: } michael@0: michael@0: VerifyList(); michael@0: michael@0: aFrameList.Clear(); michael@0: return Slice(*this, firstNewFrame, nextSibling); michael@0: } michael@0: michael@0: nsFrameList michael@0: nsFrameList::ExtractHead(FrameLinkEnumerator& aLink) michael@0: { michael@0: NS_PRECONDITION(&aLink.List() == this, "Unexpected list"); michael@0: NS_PRECONDITION(!aLink.PrevFrame() || michael@0: aLink.PrevFrame()->GetNextSibling() == michael@0: aLink.NextFrame(), michael@0: "Unexpected PrevFrame()"); michael@0: NS_PRECONDITION(aLink.PrevFrame() || michael@0: aLink.NextFrame() == FirstChild(), michael@0: "Unexpected NextFrame()"); michael@0: NS_PRECONDITION(!aLink.PrevFrame() || michael@0: aLink.NextFrame() != FirstChild(), michael@0: "Unexpected NextFrame()"); michael@0: NS_PRECONDITION(aLink.mEnd == nullptr, michael@0: "Unexpected mEnd for frame link enumerator"); michael@0: michael@0: nsIFrame* prev = aLink.PrevFrame(); michael@0: nsIFrame* newFirstFrame = nullptr; michael@0: if (prev) { michael@0: // Truncate the list after |prev| and hand the first part to our new list. michael@0: prev->SetNextSibling(nullptr); michael@0: newFirstFrame = mFirstChild; michael@0: mFirstChild = aLink.NextFrame(); michael@0: if (!mFirstChild) { // we handed over the whole list michael@0: mLastChild = nullptr; michael@0: } michael@0: michael@0: // Now make sure aLink doesn't point to a frame we no longer have. michael@0: aLink.mPrev = nullptr; michael@0: } michael@0: // else aLink is pointing to before our first frame. Nothing to do. michael@0: michael@0: return nsFrameList(newFirstFrame, prev); michael@0: } michael@0: michael@0: nsFrameList michael@0: nsFrameList::ExtractTail(FrameLinkEnumerator& aLink) michael@0: { michael@0: NS_PRECONDITION(&aLink.List() == this, "Unexpected list"); michael@0: NS_PRECONDITION(!aLink.PrevFrame() || michael@0: aLink.PrevFrame()->GetNextSibling() == michael@0: aLink.NextFrame(), michael@0: "Unexpected PrevFrame()"); michael@0: NS_PRECONDITION(aLink.PrevFrame() || michael@0: aLink.NextFrame() == FirstChild(), michael@0: "Unexpected NextFrame()"); michael@0: NS_PRECONDITION(!aLink.PrevFrame() || michael@0: aLink.NextFrame() != FirstChild(), michael@0: "Unexpected NextFrame()"); michael@0: NS_PRECONDITION(aLink.mEnd == nullptr, michael@0: "Unexpected mEnd for frame link enumerator"); michael@0: michael@0: nsIFrame* prev = aLink.PrevFrame(); michael@0: nsIFrame* newFirstFrame; michael@0: nsIFrame* newLastFrame; michael@0: if (prev) { michael@0: // Truncate the list after |prev| and hand the second part to our new list michael@0: prev->SetNextSibling(nullptr); michael@0: newFirstFrame = aLink.NextFrame(); michael@0: newLastFrame = newFirstFrame ? mLastChild : nullptr; michael@0: mLastChild = prev; michael@0: } else { michael@0: // Hand the whole list over to our new list michael@0: newFirstFrame = mFirstChild; michael@0: newLastFrame = mLastChild; michael@0: Clear(); michael@0: } michael@0: michael@0: // Now make sure aLink doesn't point to a frame we no longer have. michael@0: aLink.mFrame = nullptr; michael@0: michael@0: NS_POSTCONDITION(aLink.AtEnd(), "What's going on here?"); michael@0: michael@0: return nsFrameList(newFirstFrame, newLastFrame); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsFrameList::FrameAt(int32_t aIndex) const michael@0: { michael@0: NS_PRECONDITION(aIndex >= 0, "invalid arg"); michael@0: if (aIndex < 0) return nullptr; michael@0: nsIFrame* frame = mFirstChild; michael@0: while ((aIndex-- > 0) && frame) { michael@0: frame = frame->GetNextSibling(); michael@0: } michael@0: return frame; michael@0: } michael@0: michael@0: int32_t michael@0: nsFrameList::IndexOf(nsIFrame* aFrame) const michael@0: { michael@0: int32_t count = 0; michael@0: for (nsIFrame* f = mFirstChild; f; f = f->GetNextSibling()) { michael@0: if (f == aFrame) michael@0: return count; michael@0: ++count; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: bool michael@0: nsFrameList::ContainsFrame(const nsIFrame* aFrame) const michael@0: { michael@0: NS_PRECONDITION(aFrame, "null ptr"); michael@0: michael@0: nsIFrame* frame = mFirstChild; michael@0: while (frame) { michael@0: if (frame == aFrame) { michael@0: return true; michael@0: } michael@0: frame = frame->GetNextSibling(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: int32_t michael@0: nsFrameList::GetLength() const michael@0: { michael@0: int32_t count = 0; michael@0: nsIFrame* frame = mFirstChild; michael@0: while (frame) { michael@0: count++; michael@0: frame = frame->GetNextSibling(); michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: void michael@0: nsFrameList::ApplySetParent(nsIFrame* aParent) const michael@0: { michael@0: NS_ASSERTION(aParent, "null ptr"); michael@0: michael@0: for (nsIFrame* f = FirstChild(); f; f = f->GetNextSibling()) { michael@0: f->SetParent(aParent); michael@0: } michael@0: } michael@0: michael@0: /* static */ void michael@0: nsFrameList::UnhookFrameFromSiblings(nsIFrame* aFrame) michael@0: { michael@0: MOZ_ASSERT(aFrame->GetPrevSibling() && aFrame->GetNextSibling()); michael@0: nsIFrame* const nextSibling = aFrame->GetNextSibling(); michael@0: nsIFrame* const prevSibling = aFrame->GetPrevSibling(); michael@0: aFrame->SetNextSibling(nullptr); michael@0: prevSibling->SetNextSibling(nextSibling); michael@0: MOZ_ASSERT(!aFrame->GetPrevSibling() && !aFrame->GetNextSibling()); michael@0: } michael@0: michael@0: #ifdef DEBUG_FRAME_DUMP michael@0: void michael@0: nsFrameList::List(FILE* out) const michael@0: { michael@0: fprintf_stderr(out, "<\n"); michael@0: for (nsIFrame* frame = mFirstChild; frame; michael@0: frame = frame->GetNextSibling()) { michael@0: frame->List(out, " "); michael@0: } michael@0: fprintf_stderr(out, ">\n"); michael@0: } michael@0: #endif michael@0: michael@0: nsIFrame* michael@0: nsFrameList::GetPrevVisualFor(nsIFrame* aFrame) const michael@0: { michael@0: if (!mFirstChild) michael@0: return nullptr; michael@0: michael@0: nsIFrame* parent = mFirstChild->GetParent(); michael@0: if (!parent) michael@0: return aFrame ? aFrame->GetPrevSibling() : LastChild(); michael@0: michael@0: nsBidiLevel baseLevel = nsBidiPresUtils::GetFrameBaseLevel(mFirstChild); michael@0: michael@0: nsAutoLineIterator iter = parent->GetLineIterator(); michael@0: if (!iter) { michael@0: // Parent is not a block Frame michael@0: if (parent->GetType() == nsGkAtoms::lineFrame) { michael@0: // Line frames are not bidi-splittable, so need to consider bidi reordering michael@0: if (baseLevel == NSBIDI_LTR) { michael@0: return nsBidiPresUtils::GetFrameToLeftOf(aFrame, mFirstChild, -1); michael@0: } else { // RTL michael@0: return nsBidiPresUtils::GetFrameToRightOf(aFrame, mFirstChild, -1); michael@0: } michael@0: } else { michael@0: // Just get the next or prev sibling, depending on block and frame direction. michael@0: nsBidiLevel frameEmbeddingLevel = nsBidiPresUtils::GetFrameEmbeddingLevel(mFirstChild); michael@0: if ((frameEmbeddingLevel & 1) == (baseLevel & 1)) { michael@0: return aFrame ? aFrame->GetPrevSibling() : LastChild(); michael@0: } else { michael@0: return aFrame ? aFrame->GetNextSibling() : mFirstChild; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Parent is a block frame, so use the LineIterator to find the previous visual michael@0: // sibling on this line, or the last one on the previous line. michael@0: michael@0: int32_t thisLine; michael@0: if (aFrame) { michael@0: thisLine = iter->FindLineContaining(aFrame); michael@0: if (thisLine < 0) michael@0: return nullptr; michael@0: } else { michael@0: thisLine = iter->GetNumLines(); michael@0: } michael@0: michael@0: nsIFrame* frame = nullptr; michael@0: nsIFrame* firstFrameOnLine; michael@0: int32_t numFramesOnLine; michael@0: nsRect lineBounds; michael@0: uint32_t lineFlags; michael@0: michael@0: if (aFrame) { michael@0: iter->GetLine(thisLine, &firstFrameOnLine, &numFramesOnLine, lineBounds, &lineFlags); michael@0: michael@0: if (baseLevel == NSBIDI_LTR) { michael@0: frame = nsBidiPresUtils::GetFrameToLeftOf(aFrame, firstFrameOnLine, numFramesOnLine); michael@0: } else { // RTL michael@0: frame = nsBidiPresUtils::GetFrameToRightOf(aFrame, firstFrameOnLine, numFramesOnLine); michael@0: } michael@0: } michael@0: michael@0: if (!frame && thisLine > 0) { michael@0: // Get the last frame of the previous line michael@0: iter->GetLine(thisLine - 1, &firstFrameOnLine, &numFramesOnLine, lineBounds, &lineFlags); michael@0: michael@0: if (baseLevel == NSBIDI_LTR) { michael@0: frame = nsBidiPresUtils::GetFrameToLeftOf(nullptr, firstFrameOnLine, numFramesOnLine); michael@0: } else { // RTL michael@0: frame = nsBidiPresUtils::GetFrameToRightOf(nullptr, firstFrameOnLine, numFramesOnLine); michael@0: } michael@0: } michael@0: return frame; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsFrameList::GetNextVisualFor(nsIFrame* aFrame) const michael@0: { michael@0: if (!mFirstChild) michael@0: return nullptr; michael@0: michael@0: nsIFrame* parent = mFirstChild->GetParent(); michael@0: if (!parent) michael@0: return aFrame ? aFrame->GetPrevSibling() : mFirstChild; michael@0: michael@0: nsBidiLevel baseLevel = nsBidiPresUtils::GetFrameBaseLevel(mFirstChild); michael@0: michael@0: nsAutoLineIterator iter = parent->GetLineIterator(); michael@0: if (!iter) { michael@0: // Parent is not a block Frame michael@0: if (parent->GetType() == nsGkAtoms::lineFrame) { michael@0: // Line frames are not bidi-splittable, so need to consider bidi reordering michael@0: if (baseLevel == NSBIDI_LTR) { michael@0: return nsBidiPresUtils::GetFrameToRightOf(aFrame, mFirstChild, -1); michael@0: } else { // RTL michael@0: return nsBidiPresUtils::GetFrameToLeftOf(aFrame, mFirstChild, -1); michael@0: } michael@0: } else { michael@0: // Just get the next or prev sibling, depending on block and frame direction. michael@0: nsBidiLevel frameEmbeddingLevel = nsBidiPresUtils::GetFrameEmbeddingLevel(mFirstChild); michael@0: if ((frameEmbeddingLevel & 1) == (baseLevel & 1)) { michael@0: return aFrame ? aFrame->GetNextSibling() : mFirstChild; michael@0: } else { michael@0: return aFrame ? aFrame->GetPrevSibling() : LastChild(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Parent is a block frame, so use the LineIterator to find the next visual michael@0: // sibling on this line, or the first one on the next line. michael@0: michael@0: int32_t thisLine; michael@0: if (aFrame) { michael@0: thisLine = iter->FindLineContaining(aFrame); michael@0: if (thisLine < 0) michael@0: return nullptr; michael@0: } else { michael@0: thisLine = -1; michael@0: } michael@0: michael@0: nsIFrame* frame = nullptr; michael@0: nsIFrame* firstFrameOnLine; michael@0: int32_t numFramesOnLine; michael@0: nsRect lineBounds; michael@0: uint32_t lineFlags; michael@0: michael@0: if (aFrame) { michael@0: iter->GetLine(thisLine, &firstFrameOnLine, &numFramesOnLine, lineBounds, &lineFlags); michael@0: michael@0: if (baseLevel == NSBIDI_LTR) { michael@0: frame = nsBidiPresUtils::GetFrameToRightOf(aFrame, firstFrameOnLine, numFramesOnLine); michael@0: } else { // RTL michael@0: frame = nsBidiPresUtils::GetFrameToLeftOf(aFrame, firstFrameOnLine, numFramesOnLine); michael@0: } michael@0: } michael@0: michael@0: int32_t numLines = iter->GetNumLines(); michael@0: if (!frame && thisLine < numLines - 1) { michael@0: // Get the first frame of the next line michael@0: iter->GetLine(thisLine + 1, &firstFrameOnLine, &numFramesOnLine, lineBounds, &lineFlags); michael@0: michael@0: if (baseLevel == NSBIDI_LTR) { michael@0: frame = nsBidiPresUtils::GetFrameToRightOf(nullptr, firstFrameOnLine, numFramesOnLine); michael@0: } else { // RTL michael@0: frame = nsBidiPresUtils::GetFrameToLeftOf(nullptr, firstFrameOnLine, numFramesOnLine); michael@0: } michael@0: } michael@0: return frame; michael@0: } michael@0: michael@0: #ifdef DEBUG_FRAME_LIST michael@0: void michael@0: nsFrameList::VerifyList() const michael@0: { michael@0: NS_ASSERTION((mFirstChild == nullptr) == (mLastChild == nullptr), michael@0: "bad list state"); michael@0: michael@0: if (IsEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: // Simple algorithm to find a loop in a linked list -- advance pointers michael@0: // through it at speeds of 1 and 2, and if they ever get to be equal bail michael@0: NS_ASSERTION(!mFirstChild->GetPrevSibling(), "bad prev sibling pointer"); michael@0: nsIFrame *first = mFirstChild, *second = mFirstChild; michael@0: for (;;) { michael@0: first = first->GetNextSibling(); michael@0: second = second->GetNextSibling(); michael@0: if (!second) { michael@0: break; michael@0: } michael@0: NS_ASSERTION(second->GetPrevSibling()->GetNextSibling() == second, michael@0: "bad prev sibling pointer"); michael@0: second = second->GetNextSibling(); michael@0: if (first == second) { michael@0: // Loop detected! Since second advances faster, they can't both be null; michael@0: // we would have broken out of the loop long ago. michael@0: NS_ERROR("loop in frame list. This will probably hang soon."); michael@0: return; michael@0: } michael@0: if (!second) { michael@0: break; michael@0: } michael@0: NS_ASSERTION(second->GetPrevSibling()->GetNextSibling() == second, michael@0: "bad prev sibling pointer"); michael@0: } michael@0: michael@0: NS_ASSERTION(mLastChild == nsLayoutUtils::GetLastSibling(mFirstChild), michael@0: "bogus mLastChild"); michael@0: // XXX we should also assert that all GetParent() are either null or michael@0: // the same non-null value, but nsCSSFrameConstructor::nsFrameItems michael@0: // prevents that, e.g. table captions. michael@0: } michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: namespace layout { michael@0: michael@0: AutoFrameListPtr::~AutoFrameListPtr() michael@0: { michael@0: if (mFrameList) { michael@0: mFrameList->Delete(mPresContext->PresShell()); michael@0: } michael@0: } michael@0: michael@0: } michael@0: }