1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is Netscape
18
* Communications Corporation. Portions created by Netscape are
19
* Copyright (C) 1998 Netscape Communications Corporation. All
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the NPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
#include "nsLayoutUtils.h"
40
#include "nsIPresContext.h"
41
#include "nsIContent.h"
42
#include "nsFrameList.h"
43
#include "nsLayoutAtoms.h"
45
#include "nsCSSPseudoElements.h"
49
* A namespace class for static layout utilities.
53
* GetFirstChildFrame returns the first "real" child frame of a
54
* given frame. It will descend down into pseudo-frames (unless the
55
* pseudo-frame is the :before generated frame).
56
* @param aPresContext the prescontext
57
* @param aFrame the frame
58
* @param aFrame the frame's content node
61
GetFirstChildFrame(nsIPresContext* aPresContext,
65
NS_PRECONDITION(aFrame, "NULL frame pointer");
67
// Get the first child frame
68
nsIFrame* childFrame = aFrame->GetFirstChild(nsnull);
70
// If the child frame is a pseudo-frame, then return its first child.
71
// Note that the frame we create for the generated content is also a
72
// pseudo-frame and so don't drill down in that case
74
childFrame->IsPseudoFrame(aContent) &&
75
!childFrame->IsGeneratedContentFrame()) {
76
return GetFirstChildFrame(aPresContext, childFrame, aContent);
83
* GetLastChildFrame returns the last "real" child frame of a
84
* given frame. It will descend down into pseudo-frames (unless the
85
* pseudo-frame is the :after generated frame).
86
* @param aPresContext the prescontext
87
* @param aFrame the frame
88
* @param aFrame the frame's content node
91
GetLastChildFrame(nsIPresContext* aPresContext,
95
NS_PRECONDITION(aFrame, "NULL frame pointer");
97
// Get the last in flow frame
98
nsIFrame* lastInFlow = aFrame->GetLastInFlow();
100
// Get the last child frame
101
nsIFrame* firstChildFrame = lastInFlow->GetFirstChild(nsnull);
102
if (firstChildFrame) {
103
nsFrameList frameList(firstChildFrame);
104
nsIFrame* lastChildFrame = frameList.LastChild();
106
NS_ASSERTION(lastChildFrame, "unexpected error");
108
// Get the frame's first-in-flow. This matters in case the frame has
109
// been continuted across multiple lines
110
lastChildFrame = lastChildFrame->GetFirstInFlow();
112
// If the last child frame is a pseudo-frame, then return its last child.
113
// Note that the frame we create for the generated content is also a
114
// pseudo-frame and so don't drill down in that case
115
if (lastChildFrame &&
116
lastChildFrame->IsPseudoFrame(aContent) &&
117
!lastChildFrame->IsGeneratedContentFrame()) {
118
return GetLastChildFrame(aPresContext, lastChildFrame, aContent);
121
return lastChildFrame;
129
nsLayoutUtils::GetBeforeFrame(nsIFrame* aFrame, nsIPresContext* aPresContext)
131
NS_PRECONDITION(aFrame, "NULL frame pointer");
133
nsIFrame* prevInFlow = nsnull;
134
aFrame->GetPrevInFlow(&prevInFlow);
135
NS_ASSERTION(!prevInFlow, "aFrame must be first-in-flow");
138
nsIFrame* firstFrame = GetFirstChildFrame(aPresContext, aFrame, aFrame->GetContent());
140
if (firstFrame && IsGeneratedContentFor(nsnull, firstFrame,
141
nsCSSPseudoElements::before)) {
150
nsLayoutUtils::GetAfterFrame(nsIFrame* aFrame, nsIPresContext* aPresContext)
152
NS_PRECONDITION(aFrame, "NULL frame pointer");
154
nsIFrame* lastFrame = GetLastChildFrame(aPresContext, aFrame, aFrame->GetContent());
156
if (lastFrame && IsGeneratedContentFor(nsnull, lastFrame,
157
nsCSSPseudoElements::after)) {
166
nsLayoutUtils::GetPageFrame(nsIFrame* aFrame)
168
for (nsIFrame* frame = aFrame; frame; frame = frame->GetParent()) {
169
if (frame->GetType() == nsLayoutAtoms::pageFrame) {
178
nsLayoutUtils::IsGeneratedContentFor(nsIContent* aContent,
180
nsIAtom* aPseudoElement)
182
NS_PRECONDITION(aFrame, "Must have a frame");
183
NS_PRECONDITION(aPseudoElement, "Must have a pseudo name");
185
if (!aFrame->IsGeneratedContentFrame()) {
189
if (aContent && aFrame->GetContent() != aContent) {
193
return aFrame->GetStyleContext()->GetPseudoType() == aPseudoElement;
198
nsLayoutUtils::IsProperAncestorFrame(nsIFrame* aAncestorFrame, nsIFrame* aFrame,
199
nsIFrame* aCommonAncestor)
201
if (aFrame == aCommonAncestor) {
205
nsIFrame* parentFrame = aFrame->GetParent();
207
while (parentFrame != aCommonAncestor) {
208
if (parentFrame == aAncestorFrame) {
212
parentFrame = parentFrame->GetParent();
220
nsLayoutUtils::CompareTreePosition(nsIContent* aContent1, nsIContent* aContent2,
221
nsIContent* aCommonAncestor) {
222
NS_PRECONDITION(aContent1, "aContent1 must not be null");
223
NS_PRECONDITION(aContent2, "aContent2 must not be null");
225
nsAutoVoidArray content1Ancestors;
227
for (c1 = aContent1; c1 && c1 != aCommonAncestor; c1 = c1->GetParent()) {
228
content1Ancestors.AppendElement(c1);
230
if (!c1 && aCommonAncestor) {
231
// So, it turns out aCommonAncestor was not an ancestor of c1. Oops.
232
// Never mind. We can continue as if aCommonAncestor was null.
233
aCommonAncestor = nsnull;
236
nsAutoVoidArray content2Ancestors;
238
for (c2 = aContent2; c2 && c2 != aCommonAncestor; c2 = c2->GetParent()) {
239
content2Ancestors.AppendElement(c2);
241
if (!c2 && aCommonAncestor) {
242
// So, it turns out aCommonAncestor was not an ancestor of c2.
243
// We need to retry with no common ancestor hint.
244
return CompareTreePosition(aContent1, aContent2, nsnull);
247
int last1 = content1Ancestors.Count() - 1;
248
int last2 = content2Ancestors.Count() - 1;
249
nsIContent* content1Ancestor = nsnull;
250
nsIContent* content2Ancestor = nsnull;
251
while (last1 >= 0 && last2 >= 0
252
&& ((content1Ancestor = NS_STATIC_CAST(nsIContent*, content1Ancestors.ElementAt(last1)))
253
== (content2Ancestor = NS_STATIC_CAST(nsIContent*, content2Ancestors.ElementAt(last2))))) {
260
NS_ASSERTION(aContent1 == aContent2, "internal error?");
263
// aContent1 is an ancestor of aContent2
268
// aContent2 is an ancestor of aContent1
271
// content1Ancestor != content2Ancestor, so they must be siblings with the same parent
272
nsIContent* parent = content1Ancestor->GetParent();
273
NS_ASSERTION(parent, "no common ancestor at all???");
274
if (!parent) { // different documents??
278
PRInt32 index1 = parent->IndexOf(content1Ancestor);
279
PRInt32 index2 = parent->IndexOf(content2Ancestor);
280
if (index1 < 0 || index2 < 0) {
281
// one of them must be anonymous; we can't determine the order
285
return index1 - index2;
291
nsIFrame* nsLayoutUtils::GetLastSibling(nsIFrame* aFrame) {
297
while ((next = aFrame->GetNextSibling()) != nsnull) {
305
nsLayoutUtils::FindSiblingViewFor(nsIView* aParentView, nsIFrame* aFrame) {
306
nsIFrame* parentViewFrame = NS_STATIC_CAST(nsIFrame*, aParentView->GetClientData());
307
nsIContent* parentViewContent = parentViewFrame ? parentViewFrame->GetContent() : nsnull;
308
for (nsIView* insertBefore = aParentView->GetFirstChild(); insertBefore;
309
insertBefore = insertBefore->GetNextSibling()) {
310
nsIFrame* f = NS_STATIC_CAST(nsIFrame*, insertBefore->GetClientData());
312
// this view could be some anonymous view attached to a meaningful parent
313
for (nsIView* searchView = insertBefore->GetParent(); searchView;
314
searchView = searchView->GetParent()) {
315
f = NS_STATIC_CAST(nsIFrame*, searchView->GetClientData());
320
NS_ASSERTION(f, "Can't find a frame anywhere!");
322
if (f && CompareTreePosition(aFrame->GetContent(),
323
f->GetContent(), parentViewContent) > 0) {
324
// aFrame's content is after f's content, so put our view before f's view