Bug 54488 - "[Mac] Non-draggable widgets in background windows should look disabled...
[mozilla-central.git] / layout / base / nsCSSFrameConstructor.cpp
blob3c28319a91f0cbcb735d4a9b7198849722286677
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
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/
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.
16 * The Original Code is mozilla.org code.
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.
23 * Contributor(s):
24 * Dan Rosen <dr@netscape.com>
25 * Mats Palmgren <mats.palmgren@bredband.net>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * construction of a frame tree that is nearly isomorphic to the content
43 * tree and updating of that tree in response to dynamic changes
46 #include "nsCSSFrameConstructor.h"
47 #include "nsCRT.h"
48 #include "nsIAtom.h"
49 #include "nsIURL.h"
50 #include "nsISupportsArray.h"
51 #include "nsHashtable.h"
52 #include "nsIHTMLDocument.h"
53 #include "nsIStyleRule.h"
54 #include "nsIFrame.h"
55 #include "nsGkAtoms.h"
56 #include "nsPresContext.h"
57 #include "nsILinkHandler.h"
58 #include "nsIDocument.h"
59 #include "nsTableFrame.h"
60 #include "nsTableColGroupFrame.h"
61 #include "nsTableColFrame.h"
62 #include "nsIDOMHTMLDocument.h"
63 #include "nsIDOMHTMLTableColElement.h"
64 #include "nsIDOMHTMLTableCaptionElem.h"
65 #include "nsHTMLParts.h"
66 #include "nsIPresShell.h"
67 #include "nsStyleSet.h"
68 #include "nsIViewManager.h"
69 #include "nsIEventStateManager.h"
70 #include "nsIScrollableView.h"
71 #include "nsStyleConsts.h"
72 #include "nsTableOuterFrame.h"
73 #include "nsIDOMXULElement.h"
74 #include "nsHTMLContainerFrame.h"
75 #include "nsINameSpaceManager.h"
76 #include "nsIDOMHTMLSelectElement.h"
77 #include "nsIDOMHTMLLegendElement.h"
78 #include "nsIComboboxControlFrame.h"
79 #include "nsIListControlFrame.h"
80 #include "nsISelectControlFrame.h"
81 #include "nsIRadioControlFrame.h"
82 #include "nsICheckboxControlFrame.h"
83 #include "nsIDOMCharacterData.h"
84 #include "nsIDOMHTMLImageElement.h"
85 #include "nsPlaceholderFrame.h"
86 #include "nsTableRowGroupFrame.h"
87 #include "nsStyleChangeList.h"
88 #include "nsIFormControl.h"
89 #include "nsCSSAnonBoxes.h"
90 #include "nsCSSPseudoElements.h"
91 #include "nsIDeviceContext.h"
92 #include "nsTextFragment.h"
93 #include "nsISupportsArray.h"
94 #include "nsIAnonymousContentCreator.h"
95 #include "nsFrameManager.h"
96 #include "nsLegendFrame.h"
97 #include "nsIContentIterator.h"
98 #include "nsBoxLayoutState.h"
99 #include "nsBindingManager.h"
100 #include "nsXBLBinding.h"
101 #include "nsITheme.h"
102 #include "nsContentCID.h"
103 #include "nsContentUtils.h"
104 #include "nsIScriptError.h"
105 #include "nsIDocShell.h"
106 #include "nsIDocShellTreeItem.h"
107 #include "nsObjectFrame.h"
108 #include "nsRuleNode.h"
109 #include "nsIDOMMutationEvent.h"
110 #include "nsChildIterator.h"
111 #include "nsCSSRendering.h"
112 #include "nsISelectElement.h"
113 #include "nsLayoutErrors.h"
114 #include "nsLayoutUtils.h"
115 #include "nsAutoPtr.h"
116 #include "nsBoxFrame.h"
117 #include "nsIBoxLayout.h"
118 #include "nsImageFrame.h"
119 #include "nsIObjectLoadingContent.h"
120 #include "nsContentErrors.h"
121 #include "nsIPrincipal.h"
122 #include "nsIDOMWindowInternal.h"
123 #include "nsStyleUtil.h"
124 #include "nsIFocusEventSuppressor.h"
125 #include "nsBox.h"
127 #ifdef MOZ_XUL
128 #include "nsIRootBox.h"
129 #include "nsIDOMXULCommandDispatcher.h"
130 #include "nsIDOMXULDocument.h"
131 #include "nsIXULDocument.h"
132 #endif
133 #ifdef ACCESSIBILITY
134 #include "nsIAccessibilityService.h"
135 #include "nsIAccessibleEvent.h"
136 #endif
138 #include "nsInlineFrame.h"
139 #include "nsBlockFrame.h"
141 #include "nsIScrollableFrame.h"
143 #include "nsIXBLService.h"
145 #undef NOISY_FIRST_LETTER
147 #ifdef MOZ_MATHML
148 #include "nsMathMLParts.h"
149 #endif
150 #ifdef MOZ_SVG
151 #include "nsSVGUtils.h"
152 #endif
154 nsIFrame*
155 NS_NewHTMLCanvasFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
157 #if defined(MOZ_MEDIA)
158 nsIFrame*
159 NS_NewHTMLVideoFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
160 #endif
162 #ifdef MOZ_SVG
163 #include "nsISVGTextContentMetrics.h"
165 PRBool
166 NS_SVGEnabled();
167 nsIFrame*
168 NS_NewSVGOuterSVGFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
169 nsIFrame*
170 NS_NewSVGInnerSVGFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
171 nsIFrame*
172 NS_NewSVGPathGeometryFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
173 nsIFrame*
174 NS_NewSVGGFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
175 nsIFrame*
176 NS_NewSVGGenericContainerFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
177 nsIFrame*
178 NS_NewSVGForeignObjectFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
179 nsIFrame*
180 NS_NewSVGAFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
181 nsIFrame*
182 NS_NewSVGGlyphFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame* parent, nsStyleContext* aContext);
183 nsIFrame*
184 NS_NewSVGSwitchFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
185 nsIFrame*
186 NS_NewSVGTextFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
187 nsIFrame*
188 NS_NewSVGTSpanFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame* parent, nsStyleContext* aContext);
189 nsIFrame*
190 NS_NewSVGContainerFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
191 nsIFrame*
192 NS_NewSVGUseFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
193 PRBool
194 NS_SVG_PassesConditionalProcessingTests(nsIContent *aContent);
195 extern nsIFrame*
196 NS_NewSVGLinearGradientFrame(nsIPresShell *aPresShell, nsIContent *aContent, nsStyleContext* aContext);
197 extern nsIFrame*
198 NS_NewSVGRadialGradientFrame(nsIPresShell *aPresShell, nsIContent *aContent, nsStyleContext* aContext);
199 extern nsIFrame*
200 NS_NewSVGStopFrame(nsIPresShell *aPresShell, nsIContent *aContent, nsIFrame *aParentFrame, nsStyleContext* aContext);
201 nsIFrame*
202 NS_NewSVGMarkerFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
203 extern nsIFrame*
204 NS_NewSVGImageFrame(nsIPresShell *aPresShell, nsIContent *aContent, nsStyleContext* aContext);
205 nsIFrame*
206 NS_NewSVGClipPathFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
207 nsIFrame*
208 NS_NewSVGTextPathFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame* parent, nsStyleContext* aContext);
209 nsIFrame*
210 NS_NewSVGFilterFrame(nsIPresShell *aPresShell, nsIContent *aContent, nsStyleContext* aContext);
211 nsIFrame*
212 NS_NewSVGPatternFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
213 nsIFrame*
214 NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext);
215 nsIFrame*
216 NS_NewSVGLeafFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
217 #endif
219 #include "nsIDocument.h"
220 #include "nsIDOMElement.h"
221 #include "nsIDOMNodeList.h"
222 #include "nsIDOMDocument.h"
223 #include "nsIDOMDocumentXBL.h"
224 #include "nsIScrollable.h"
225 #include "nsINodeInfo.h"
226 #include "prenv.h"
227 #include "nsWidgetsCID.h"
228 #include "nsNodeInfoManager.h"
229 #include "nsContentCreatorFunctions.h"
230 #include "nsIServiceManager.h"
232 // Global object maintenance
233 nsIXBLService * nsCSSFrameConstructor::gXBLService = nsnull;
235 // Global prefs
236 static PRBool gGotXBLFormPrefs = PR_FALSE;
237 static PRBool gUseXBLForms = PR_FALSE;
239 #ifdef DEBUG
240 // Set the environment variable GECKO_FRAMECTOR_DEBUG_FLAGS to one or
241 // more of the following flags (comma separated) for handy debug
242 // output.
243 static PRBool gNoisyContentUpdates = PR_FALSE;
244 static PRBool gReallyNoisyContentUpdates = PR_FALSE;
245 static PRBool gNoisyInlineConstruction = PR_FALSE;
246 static PRBool gVerifyFastFindFrame = PR_FALSE;
247 static PRBool gTablePseudoFrame = PR_FALSE;
249 struct FrameCtorDebugFlags {
250 const char* name;
251 PRBool* on;
254 static FrameCtorDebugFlags gFlags[] = {
255 { "content-updates", &gNoisyContentUpdates },
256 { "really-noisy-content-updates", &gReallyNoisyContentUpdates },
257 { "noisy-inline", &gNoisyInlineConstruction },
258 { "fast-find-frame", &gVerifyFastFindFrame },
259 { "table-pseudo", &gTablePseudoFrame },
262 #define NUM_DEBUG_FLAGS (sizeof(gFlags) / sizeof(gFlags[0]))
263 #endif
266 #ifdef MOZ_XUL
267 #include "nsMenuFrame.h"
268 #include "nsPopupSetFrame.h"
269 #include "nsTreeColFrame.h"
270 #include "nsIBoxObject.h"
271 #include "nsPIListBoxObject.h"
272 #include "nsListBoxBodyFrame.h"
273 #include "nsListItemFrame.h"
275 //------------------------------------------------------------------
277 nsIFrame*
278 NS_NewAutoRepeatBoxFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
280 nsIFrame*
281 NS_NewRootBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
283 nsIFrame*
284 NS_NewDocElementBoxFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
286 nsIFrame*
287 NS_NewThumbFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
289 nsIFrame*
290 NS_NewDeckFrame (nsIPresShell* aPresShell, nsStyleContext* aContext, nsIBoxLayout* aLayoutManager = nsnull);
292 nsIFrame*
293 NS_NewLeafBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
295 nsIFrame*
296 NS_NewStackFrame (nsIPresShell* aPresShell, nsStyleContext* aContext, nsIBoxLayout* aLayoutManager = nsnull);
298 nsIFrame*
299 NS_NewProgressMeterFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
301 nsIFrame*
302 NS_NewImageBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
304 nsIFrame*
305 NS_NewTextBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
307 nsIFrame*
308 NS_NewGroupBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
310 nsIFrame*
311 NS_NewButtonBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
313 nsIFrame*
314 NS_NewSplitterFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
316 nsIFrame*
317 NS_NewMenuPopupFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
319 nsIFrame*
320 NS_NewPopupSetFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
322 nsIFrame*
323 NS_NewMenuFrame (nsIPresShell* aPresShell, nsStyleContext* aContext, PRUint32 aFlags);
325 nsIFrame*
326 NS_NewMenuBarFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
328 nsIFrame*
329 NS_NewTreeBodyFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
331 // grid
332 nsresult
333 NS_NewGridLayout2 ( nsIPresShell* aPresShell, nsIBoxLayout** aNewLayout );
334 nsresult
335 NS_NewGridRowLeafLayout ( nsIPresShell* aPresShell, nsIBoxLayout** aNewLayout );
336 nsIFrame*
337 NS_NewGridRowLeafFrame (nsIPresShell* aPresShell, nsStyleContext* aContext, PRBool aIsRoot, nsIBoxLayout* aLayout);
338 nsresult
339 NS_NewGridRowGroupLayout ( nsIPresShell* aPresShell, nsIBoxLayout** aNewLayout );
340 nsIFrame*
341 NS_NewGridRowGroupFrame (nsIPresShell* aPresShell, nsStyleContext* aContext, PRBool aIsRoot, nsIBoxLayout* aLayout);
343 nsresult
344 NS_NewListBoxLayout ( nsIPresShell* aPresShell, nsCOMPtr<nsIBoxLayout>& aNewLayout );
346 // end grid
348 nsIFrame*
349 NS_NewTitleBarFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
351 nsIFrame*
352 NS_NewResizerFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
355 #endif
357 nsIFrame*
358 NS_NewHTMLScrollFrame (nsIPresShell* aPresShell, nsStyleContext* aContext, PRBool aIsRoot);
360 nsIFrame*
361 NS_NewXULScrollFrame (nsIPresShell* aPresShell, nsStyleContext* aContext, PRBool aIsRoot);
363 nsIFrame*
364 NS_NewSliderFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
366 nsIFrame*
367 NS_NewScrollbarFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
369 nsIFrame*
370 NS_NewScrollbarButtonFrame (nsIPresShell* aPresShell, nsStyleContext* aContext);
373 #ifdef NOISY_FINDFRAME
374 static PRInt32 FFWC_totalCount=0;
375 static PRInt32 FFWC_doLoop=0;
376 static PRInt32 FFWC_doSibling=0;
377 static PRInt32 FFWC_recursions=0;
378 static PRInt32 FFWC_nextInFlows=0;
379 static PRInt32 FFWC_slowSearchForText=0;
380 #endif
382 static nsresult
383 DeletingFrameSubtree(nsFrameManager* aFrameManager,
384 nsIFrame* aFrame);
386 #ifdef MOZ_SVG
388 static nsIFrame *
389 SVG_GetFirstNonAAncestorFrame(nsIFrame *aParentFrame)
391 for (nsIFrame *ancestorFrame = aParentFrame; ancestorFrame != nsnull;
392 ancestorFrame = ancestorFrame->GetParent()) {
393 if (ancestorFrame->GetType() != nsGkAtoms::svgAFrame) {
394 return ancestorFrame;
397 return nsnull;
399 #endif
401 static inline nsIFrame*
402 GetFieldSetAreaFrame(nsIFrame* aFieldsetFrame)
404 // Depends on the fieldset child frame order - see ConstructFieldSetFrame() below.
405 nsIFrame* firstChild = aFieldsetFrame->GetFirstChild(nsnull);
406 return firstChild && firstChild->GetNextSibling() ? firstChild->GetNextSibling() : firstChild;
409 //----------------------------------------------------------------------
411 static PRBool
412 IsInlineOutside(nsIFrame* aFrame)
414 return aFrame->GetStyleDisplay()->IsInlineOutside();
418 * True if aFrame is an actual inline frame in the sense of non-replaced
419 * display:inline CSS boxes. In other words, it can be affected by {ib}
420 * splitting and can contain first-letter frames. Basically, this is either an
421 * inline frame (positioned or otherwise) or an line frame (this last because
422 * it can contain first-letter and because inserting blocks in the middle of it
423 * needs to terminate it).
425 static PRBool
426 IsInlineFrame(const nsIFrame* aFrame)
428 return aFrame->IsFrameOfType(nsIFrame::eLineParticipant);
432 * If any children require a block parent, return the first such child.
433 * Otherwise return null.
435 static nsIContent*
436 AnyKidsNeedBlockParent(nsIFrame *aFrameList)
438 for (nsIFrame *k = aFrameList; k; k = k->GetNextSibling()) {
439 // Line participants, such as text and inline frames, can't be
440 // directly inside a XUL box; they must be wrapped in an
441 // intermediate block.
442 if (k->IsFrameOfType(nsIFrame::eLineParticipant)) {
443 return k->GetContent();
446 return nsnull;
449 // Reparent a frame into a wrapper frame that is a child of its old parent.
450 static void
451 ReparentFrame(nsFrameManager* aFrameManager,
452 nsIFrame* aNewParentFrame,
453 nsIFrame* aFrame)
455 aFrame->SetParent(aNewParentFrame);
456 aFrameManager->ReParentStyleContext(aFrame);
457 if (aFrame->GetStateBits() &
458 (NS_FRAME_HAS_VIEW | NS_FRAME_HAS_CHILD_WITH_VIEW)) {
459 // No need to walk up the tree, since the bits are already set
460 // right on the parent of aNewParentFrame.
461 NS_ASSERTION(aNewParentFrame->GetParent()->GetStateBits() &
462 NS_FRAME_HAS_CHILD_WITH_VIEW,
463 "aNewParentFrame's parent should have this bit set!");
464 aNewParentFrame->AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
468 //----------------------------------------------------------------------
470 // When inline frames get weird and have block frames in them, we
471 // annotate them to help us respond to incremental content changes
472 // more easily.
474 static inline PRBool
475 IsFrameSpecial(nsIFrame* aFrame)
477 return (aFrame->GetStateBits() & NS_FRAME_IS_SPECIAL) != 0;
480 static nsIFrame* GetSpecialSibling(nsIFrame* aFrame)
482 // We only store the "special sibling" annotation with the first
483 // frame in the continuation chain. Walk back to find that frame now.
484 aFrame = aFrame->GetFirstContinuation();
486 void* value = aFrame->GetProperty(nsGkAtoms::IBSplitSpecialSibling);
488 return static_cast<nsIFrame*>(value);
491 static nsIFrame*
492 GetIBSplitSpecialPrevSiblingForAnonymousBlock(nsIFrame* aFrame)
494 NS_PRECONDITION(IsFrameSpecial(aFrame) && !IsInlineFrame(aFrame),
495 "Shouldn't call this");
497 // We only store the "special sibling" annotation with the first
498 // frame in the continuation chain. Walk back to find that frame now.
499 return
500 static_cast<nsIFrame*>
501 (aFrame->GetFirstContinuation()->
502 GetProperty(nsGkAtoms::IBSplitSpecialPrevSibling));
505 static nsIFrame*
506 GetLastSpecialSibling(nsIFrame* aFrame)
508 for (nsIFrame *frame = aFrame, *next; ; frame = next) {
509 next = GetSpecialSibling(frame);
510 if (!next)
511 return frame;
513 NS_NOTREACHED("unreachable code");
514 return nsnull;
517 static void
518 SetFrameIsSpecial(nsIFrame* aFrame, nsIFrame* aSpecialSibling)
520 NS_PRECONDITION(aFrame, "bad args!");
522 // Mark the frame and all of its siblings as "special".
523 for (nsIFrame* frame = aFrame; frame != nsnull; frame = frame->GetNextContinuation()) {
524 frame->AddStateBits(NS_FRAME_IS_SPECIAL);
527 if (aSpecialSibling) {
528 // We should be the first-in-flow
529 NS_ASSERTION(!aFrame->GetPrevInFlow(),
530 "assigning special sibling to other than first-in-flow!");
532 // Store the "special sibling" (if we were given one) with the
533 // first frame in the flow.
534 aFrame->SetProperty(nsGkAtoms::IBSplitSpecialSibling, aSpecialSibling);
538 static nsIFrame*
539 GetIBContainingBlockFor(nsIFrame* aFrame)
541 NS_PRECONDITION(IsFrameSpecial(aFrame),
542 "GetIBContainingBlockFor() should only be called on known IB frames");
544 // Get the first "normal" ancestor of the target frame.
545 nsIFrame* parentFrame;
546 do {
547 parentFrame = aFrame->GetParent();
549 if (! parentFrame) {
550 NS_ERROR("no unsplit block frame in IB hierarchy");
551 return aFrame;
554 // Note that we ignore non-special frames which have a pseudo on their
555 // style context -- they're not the frames we're looking for! In
556 // particular, they may be hiding a real parent that _is_ special.
557 if (!IsFrameSpecial(parentFrame) &&
558 !parentFrame->GetStyleContext()->GetPseudoType())
559 break;
561 aFrame = parentFrame;
562 } while (1);
564 // post-conditions
565 NS_ASSERTION(parentFrame, "no normal ancestor found for special frame in GetIBContainingBlockFor");
566 NS_ASSERTION(parentFrame != aFrame, "parentFrame is actually the child frame - bogus reslt");
568 return parentFrame;
571 //----------------------------------------------------------------------
573 // Block/inline frame construction logic. We maintain a few invariants here:
575 // 1. Block frames contain block and inline frames.
577 // 2. Inline frames only contain inline frames. If an inline parent has a block
578 // child then the block child is migrated upward until it lands in a block
579 // parent (the inline frames containing block is where it will end up).
581 static nsIFrame*
582 FindFirstBlock(nsIFrame* aKid, nsIFrame** aPrevKid)
584 nsIFrame* prevKid = nsnull;
585 while (aKid) {
586 if (!IsInlineOutside(aKid)) {
587 *aPrevKid = prevKid;
588 return aKid;
590 prevKid = aKid;
591 aKid = aKid->GetNextSibling();
593 *aPrevKid = nsnull;
594 return nsnull;
597 static nsIFrame*
598 FindLastBlock(nsIFrame* aKid)
600 nsIFrame* lastBlock = nsnull;
601 while (aKid) {
602 if (!IsInlineOutside(aKid)) {
603 lastBlock = aKid;
605 aKid = aKid->GetNextSibling();
607 return lastBlock;
611 * The special-prev-sibling is useful for
612 * finding the "special parent" of a frame (i.e., a frame from which a
613 * good parent style context can be obtained), one looks at the
614 * special previous sibling annotation of the real parent of the frame
615 * (if the real parent has NS_FRAME_IS_SPECIAL).
617 inline void
618 MarkIBSpecialPrevSibling(nsIFrame *aAnonymousFrame,
619 nsIFrame *aSpecialParent)
621 aAnonymousFrame->SetProperty(nsGkAtoms::IBSplitSpecialPrevSibling,
622 aSpecialParent, nsnull, nsnull);
625 // -----------------------------------------------------------
627 static PRBool
628 IsOutOfFlowList(nsIAtom* aListName)
630 return
631 aListName == nsGkAtoms::floatList ||
632 aListName == nsGkAtoms::absoluteList ||
633 aListName == nsGkAtoms::overflowOutOfFlowList ||
634 aListName == nsGkAtoms::fixedList;
637 // Helper function that recursively removes content to frame mappings and
638 // undisplayed content mappings.
639 // This differs from DeletingFrameSubtree() because the frames have not yet been
640 // added to the frame hierarchy.
641 // XXXbz it would really help if we merged the two methods somehow... :(
642 static void
643 DoCleanupFrameReferences(nsFrameManager* aFrameManager,
644 nsIFrame* aFrameIn)
646 nsIContent* content = aFrameIn->GetContent();
648 if (aFrameIn->GetType() == nsGkAtoms::placeholderFrame) {
649 nsPlaceholderFrame* placeholder = static_cast<nsPlaceholderFrame*>
650 (aFrameIn);
651 // if the frame is a placeholder use the out of flow frame
652 aFrameIn = nsPlaceholderFrame::GetRealFrameForPlaceholder(placeholder);
654 // And don't forget to unregister the placeholder mapping. Note that this
655 // means it's the caller's responsibility to actually destroy the
656 // out-of-flow pointed to by the placeholder, since after this point the
657 // out-of-flow is not reachable via the placeholder.
658 aFrameManager->UnregisterPlaceholderFrame(placeholder);
661 // Remove the mapping from the content object to its frame
662 aFrameManager->RemoveAsPrimaryFrame(content, aFrameIn);
663 aFrameManager->ClearAllUndisplayedContentIn(content);
665 // Recursively walk the child frames.
666 nsIAtom* childListName = nsnull;
667 PRInt32 childListIndex = 0;
668 do {
669 nsIFrame* childFrame = aFrameIn->GetFirstChild(childListName);
670 while (childFrame) {
671 DoCleanupFrameReferences(aFrameManager, childFrame);
673 // Get the next sibling child frame
674 childFrame = childFrame->GetNextSibling();
677 childListName = aFrameIn->GetAdditionalChildListName(childListIndex++);
678 } while (childListName);
681 // Helper function that walks a frame list and calls DoCleanupFrameReference()
682 static void
683 CleanupFrameReferences(nsFrameManager* aFrameManager,
684 nsIFrame* aFrameList)
686 while (aFrameList) {
687 DoCleanupFrameReferences(aFrameManager, aFrameList);
689 // Get the sibling frame
690 aFrameList = aFrameList->GetNextSibling();
694 // -----------------------------------------------------------
696 // Structure used when constructing formatting object trees.
697 struct nsFrameItems {
698 nsIFrame* childList;
699 nsIFrame* lastChild;
701 nsFrameItems(nsIFrame* aFrame = nsnull);
703 // Appends the frame to the end of the list
704 void AddChild(nsIFrame* aChild);
706 // Inserts the frame somewhere in the list
707 void InsertChildAfter(nsIFrame* aChild, nsIFrame* aAfter);
709 // Remove the frame from the list, return PR_FALSE if not found. If
710 // aPrevSibling is given, it must have aChild as its GetNextSibling().
711 // aPrevSibling may be null to indicate that the list should be searched.
712 PRBool RemoveChild(nsIFrame* aChild, nsIFrame* aPrevSibling);
715 nsFrameItems::nsFrameItems(nsIFrame* aFrame)
716 : childList(aFrame), lastChild(aFrame)
720 void
721 nsFrameItems::AddChild(nsIFrame* aChild)
723 #ifdef DEBUG
724 nsIFrame* oldLastChild = lastChild;
725 #endif
727 if (childList == nsnull) {
728 childList = lastChild = aChild;
730 else
732 NS_ASSERTION(aChild != lastChild,
733 "Same frame being added to frame list twice?");
734 lastChild->SetNextSibling(aChild);
735 lastChild = aChild;
737 // if aChild has siblings, lastChild needs to be the last one
738 for (nsIFrame* sib = lastChild->GetNextSibling(); sib;
739 sib = sib->GetNextSibling()) {
740 NS_ASSERTION(oldLastChild != sib, "Loop in frame list");
741 lastChild = sib;
745 void
746 nsFrameItems::InsertChildAfter(nsIFrame* aChild, nsIFrame* aAfter)
748 if (!childList || (aAfter && !aAfter->GetNextSibling())) {
749 // Appending to the end of the list
750 AddChild(aChild);
751 return;
753 if (!aAfter) {
754 // Inserting at beginning of list
755 aChild->SetNextSibling(childList);
756 childList = aChild;
757 return;
759 aChild->SetNextSibling(aAfter->GetNextSibling());
760 aAfter->SetNextSibling(aChild);
763 PRBool
764 nsFrameItems::RemoveChild(nsIFrame* aFrame, nsIFrame* aPrevSibling)
766 NS_PRECONDITION(aFrame, "null ptr");
768 nsIFrame* prev;
769 if (aPrevSibling) {
770 prev = aPrevSibling;
771 } else {
772 prev = nsnull;
773 nsIFrame* sib;
774 for (sib = childList; sib && sib != aFrame; sib = sib->GetNextSibling()) {
775 prev = sib;
777 if (!sib) {
778 return PR_FALSE;
782 NS_ASSERTION(!prev || prev->GetNextSibling() == aFrame,
783 "Unexpected prevsibling");
785 if (aFrame == childList) {
786 childList = aFrame->GetNextSibling();
787 } else {
788 prev->SetNextSibling(aFrame->GetNextSibling());
790 if (aFrame == lastChild) {
791 lastChild = prev;
793 aFrame->SetNextSibling(nsnull);
794 return PR_TRUE;
797 // -----------------------------------------------------------
799 // Structure used when constructing formatting object trees. Contains
800 // state information needed for absolutely positioned elements
801 struct nsAbsoluteItems : nsFrameItems {
802 // containing block for absolutely positioned elements
803 nsIFrame* containingBlock;
805 nsAbsoluteItems(nsIFrame* aContainingBlock);
806 #ifdef DEBUG
807 // XXXbz Does this need a debug-only assignment operator that nulls out the
808 // childList in the nsAbsoluteItems we're copying? Introducing a difference
809 // between debug and non-debug behavior seems bad, so I guess not...
810 ~nsAbsoluteItems() {
811 NS_ASSERTION(!childList,
812 "Dangling child list. Someone forgot to insert it?");
814 #endif
816 // Appends the frame to the end of the list
817 void AddChild(nsIFrame* aChild);
820 nsAbsoluteItems::nsAbsoluteItems(nsIFrame* aContainingBlock)
821 : containingBlock(aContainingBlock)
825 // Additional behavior is that it sets the frame's NS_FRAME_OUT_OF_FLOW flag
826 void
827 nsAbsoluteItems::AddChild(nsIFrame* aChild)
829 NS_ASSERTION(aChild->PresContext()->FrameManager()->
830 GetPlaceholderFrameFor(aChild),
831 "Child without placeholder being added to nsAbsoluteItems?");
832 aChild->AddStateBits(NS_FRAME_OUT_OF_FLOW);
833 nsFrameItems::AddChild(aChild);
836 // Structures used to record the creation of pseudo table frames where
837 // the content belongs to some ancestor.
838 // PseudoFrames are necessary when the childframe cannot be the direct
839 // ancestor of the content based parent frame. The amount of necessary pseudo
840 // frames is limited as the worst case would be table frame nested directly
841 // into another table frame. So the member structures of nsPseudoFrames can be
842 // viewed as a ring buffer where you start with the necessary frame type and
843 // add higher frames as long as necessary to fit into the initial parent frame.
844 // mLowestType is some sort of stack pointer which shows the start of the
845 // ringbuffer. The insertion of pseudo frames can happen between every
846 // two frames so we need to push and pop the pseudo frame data when children
847 // of a frame are created.
848 // The colgroup frame is special as it can harbour only col children.
849 // Once all children of given frame are known, the pseudo frames can be
850 // processed that means attached to the corresponding parent frames.
851 // The behaviour is in general described at
852 // http://www.w3.org/TR/CSS21/tables.html#anonymous-boxes
853 // however there are implementation details that extend the CSS 2.1
854 // specification:
855 // 1. every table frame is wrapped in an outer table frame, which is always a
856 // pseudo frame.
857 // 2. the outer table frame will be also created to hold a caption.
858 // 3. each table cell will have a pseudo inner table cell frame.
859 // 4. a colgroup frame is created between a column and a table
860 // 5. a rowgroup frame is created between a row and a table
861 // A table frame can only have rowgroups or column groups as children.
862 // A outer table frame can only have one caption and one table frame
863 // as children.
864 // Every table even if all table frames are specified will require the
865 // creation of two types of pseudo frames: the outer table frame and the inner
866 // table cell frames.
868 struct nsPseudoFrameData {
869 nsIFrame* mFrame; // created pseudo frame
870 nsFrameItems mChildList; // child frames pending to be added to the pseudo
871 nsFrameItems mChildList2; // child frames pending to be added to the pseudo
873 nsPseudoFrameData();
874 nsPseudoFrameData(nsPseudoFrameData& aOther);
875 void Reset();
876 #ifdef DEBUG
877 void Dump();
878 #endif
881 struct nsPseudoFrames {
882 nsPseudoFrameData mTableOuter;
883 nsPseudoFrameData mTableInner;
884 nsPseudoFrameData mRowGroup;
885 nsPseudoFrameData mColGroup;
886 nsPseudoFrameData mRow;
887 nsPseudoFrameData mCellOuter;
888 nsPseudoFrameData mCellInner;
890 // the frame type of the most descendant pseudo frame, no AddRef
891 nsIAtom* mLowestType;
893 nsPseudoFrames();
894 nsPseudoFrames& operator=(const nsPseudoFrames& aOther);
895 void Reset(nsPseudoFrames* aSave = nsnull);
896 PRBool IsEmpty() { return (!mLowestType && !mColGroup.mFrame); }
897 #ifdef DEBUG
898 void Dump();
899 #endif
902 nsPseudoFrameData::nsPseudoFrameData()
903 : mFrame(nsnull), mChildList(), mChildList2()
906 nsPseudoFrameData::nsPseudoFrameData(nsPseudoFrameData& aOther)
907 : mFrame(aOther.mFrame), mChildList(aOther.mChildList),
908 mChildList2(aOther.mChildList2)
911 void
912 nsPseudoFrameData::Reset()
914 mFrame = nsnull;
915 mChildList.childList = mChildList.lastChild = nsnull;
916 mChildList2.childList = mChildList2.lastChild = nsnull;
919 #ifdef DEBUG
920 void
921 nsPseudoFrameData::Dump()
923 nsIFrame* main = nsnull;
924 nsIFrame* second = nsnull;
925 printf(" %p\n", static_cast<void*>(mFrame));
926 main = mChildList.childList;
929 second = mChildList2.childList;
930 while (main || second) {
931 printf(" %p %p\n", static_cast<void*>(main),
932 static_cast<void*>(second));
933 if (main)
934 main = main->GetNextSibling();
935 if (second)
936 second = second->GetNextSibling();
939 #endif
940 nsPseudoFrames::nsPseudoFrames()
941 : mTableOuter(), mTableInner(), mRowGroup(), mColGroup(),
942 mRow(), mCellOuter(), mCellInner(), mLowestType(nsnull)
945 nsPseudoFrames& nsPseudoFrames::operator=(const nsPseudoFrames& aOther)
947 mTableOuter = aOther.mTableOuter;
948 mTableInner = aOther.mTableInner;
949 mColGroup = aOther.mColGroup;
950 mRowGroup = aOther.mRowGroup;
951 mRow = aOther.mRow;
952 mCellOuter = aOther.mCellOuter;
953 mCellInner = aOther.mCellInner;
954 mLowestType = aOther.mLowestType;
956 return *this;
958 void
959 nsPseudoFrames::Reset(nsPseudoFrames* aSave)
961 if (aSave) {
962 *aSave = *this;
965 mTableOuter.Reset();
966 mTableInner.Reset();
967 mColGroup.Reset();
968 mRowGroup.Reset();
969 mRow.Reset();
970 mCellOuter.Reset();
971 mCellInner.Reset();
972 mLowestType = nsnull;
975 #ifdef DEBUG
976 void
977 nsPseudoFrames::Dump()
979 if (IsEmpty()) {
980 // check that it is really empty, warn otherwise
981 NS_ASSERTION(!mTableOuter.mFrame, "Pseudo Outer Table Frame not empty");
982 NS_ASSERTION(!mTableOuter.mChildList.childList, "Pseudo Outer Table Frame has primary children");
983 NS_ASSERTION(!mTableOuter.mChildList2.childList,"Pseudo Outer Table Frame has secondary children");
984 NS_ASSERTION(!mTableInner.mFrame, "Pseudo Inner Table Frame not empty");
985 NS_ASSERTION(!mTableInner.mChildList.childList, "Pseudo Inner Table Frame has primary children");
986 NS_ASSERTION(!mTableInner.mChildList2.childList,"Pseudo Inner Table Frame has secondary children");
987 NS_ASSERTION(!mColGroup.mFrame, "Pseudo Colgroup Frame not empty");
988 NS_ASSERTION(!mColGroup.mChildList.childList, "Pseudo Colgroup Table Frame has primary children");
989 NS_ASSERTION(!mColGroup.mChildList2.childList, "Pseudo Colgroup Table Frame has secondary children");
990 NS_ASSERTION(!mRowGroup.mFrame, "Pseudo Rowgroup Frame not empty");
991 NS_ASSERTION(!mRowGroup.mChildList.childList, "Pseudo Rowgroup Frame has primary children");
992 NS_ASSERTION(!mRowGroup.mChildList2.childList, "Pseudo Rowgroup Frame has secondary children");
993 NS_ASSERTION(!mRow.mFrame, "Pseudo Row Frame not empty");
994 NS_ASSERTION(!mRow.mChildList.childList, "Pseudo Row Frame has primary children");
995 NS_ASSERTION(!mRow.mChildList2.childList, "Pseudo Row Frame has secondary children");
996 NS_ASSERTION(!mCellOuter.mFrame, "Pseudo Outer Cell Frame not empty");
997 NS_ASSERTION(!mCellOuter.mChildList.childList, "Pseudo Outer Cell Frame has primary children");
998 NS_ASSERTION(!mCellOuter.mChildList2.childList, "Pseudo Outer Cell Frame has secondary children");
999 NS_ASSERTION(!mCellInner.mFrame, "Pseudo Inner Cell Frame not empty");
1000 NS_ASSERTION(!mCellInner.mChildList.childList, "Pseudo Inner Cell Frame has primary children");
1001 NS_ASSERTION(!mCellInner.mChildList2.childList, "Pseudo inner Cell Frame has secondary children");
1003 else {
1004 if (mTableOuter.mFrame || mTableOuter.mChildList.childList || mTableOuter.mChildList2.childList) {
1005 if (nsGkAtoms::tableOuterFrame == mLowestType) {
1006 printf("LOW OuterTable\n");
1008 else {
1009 printf(" OuterTable\n");
1011 mTableOuter.Dump();
1013 if (mTableInner.mFrame || mTableInner.mChildList.childList || mTableInner.mChildList2.childList) {
1014 if (nsGkAtoms::tableFrame == mLowestType) {
1015 printf("LOW InnerTable\n");
1017 else {
1018 printf(" InnerTable\n");
1020 mTableInner.Dump();
1022 if (mColGroup.mFrame || mColGroup.mChildList.childList || mColGroup.mChildList2.childList) {
1023 if (nsGkAtoms::tableColGroupFrame == mLowestType) {
1024 printf("LOW ColGroup\n");
1026 else {
1027 printf(" ColGroup\n");
1029 mColGroup.Dump();
1031 if (mRowGroup.mFrame || mRowGroup.mChildList.childList || mRowGroup.mChildList2.childList) {
1032 if (nsGkAtoms::tableRowGroupFrame == mLowestType) {
1033 printf("LOW RowGroup\n");
1035 else {
1036 printf(" RowGroup\n");
1038 mRowGroup.Dump();
1040 if (mRow.mFrame || mRow.mChildList.childList || mRow.mChildList2.childList) {
1041 if (nsGkAtoms::tableRowFrame == mLowestType) {
1042 printf("LOW Row\n");
1044 else {
1045 printf(" Row\n");
1047 mRow.Dump();
1050 if (mCellOuter.mFrame || mCellOuter.mChildList.childList || mCellOuter.mChildList2.childList) {
1051 if (IS_TABLE_CELL(mLowestType)) {
1052 printf("LOW OuterCell\n");
1054 else {
1055 printf(" OuterCell\n");
1057 mCellOuter.Dump();
1059 if (mCellInner.mFrame || mCellInner.mChildList.childList || mCellInner.mChildList2.childList) {
1060 printf(" InnerCell\n");
1061 mCellInner.Dump();
1065 #endif
1066 // -----------------------------------------------------------
1068 // Structure for saving the existing state when pushing/poping containing
1069 // blocks. The destructor restores the state to its previous state
1070 class nsFrameConstructorSaveState {
1071 public:
1072 nsFrameConstructorSaveState();
1073 ~nsFrameConstructorSaveState();
1075 private:
1076 nsAbsoluteItems* mItems; // pointer to struct whose data we save/restore
1077 PRBool* mFirstLetterStyle;
1078 PRBool* mFirstLineStyle;
1079 PRBool* mFixedPosIsAbsPos;
1081 nsAbsoluteItems mSavedItems; // copy of original data
1082 PRBool mSavedFirstLetterStyle;
1083 PRBool mSavedFirstLineStyle;
1084 PRBool mSavedFixedPosIsAbsPos;
1086 // The name of the child list in which our frames would belong
1087 nsIAtom* mChildListName;
1088 nsFrameConstructorState* mState;
1090 friend class nsFrameConstructorState;
1093 // Structure used for maintaining state information during the
1094 // frame construction process
1095 class NS_STACK_CLASS nsFrameConstructorState {
1096 public:
1097 nsPresContext *mPresContext;
1098 nsIPresShell *mPresShell;
1099 nsFrameManager *mFrameManager;
1101 #ifdef MOZ_XUL
1102 // The root box, if any.
1103 nsIRootBox* mRootBox;
1104 // Frames destined for the nsGkAtoms::popupList.
1105 nsAbsoluteItems mPopupItems;
1106 #endif
1108 // Containing block information for out-of-flow frames.
1109 nsAbsoluteItems mFixedItems;
1110 nsAbsoluteItems mAbsoluteItems;
1111 nsAbsoluteItems mFloatedItems;
1112 PRBool mFirstLetterStyle;
1113 PRBool mFirstLineStyle;
1115 // When working with the -moz-transform property, we want to hook
1116 // the abs-pos and fixed-pos lists together, since transformed
1117 // elements are fixed-pos containing blocks. This flag determines
1118 // whether or not we want to wire the fixed-pos and abs-pos lists
1119 // together.
1120 PRBool mFixedPosIsAbsPos;
1122 nsCOMPtr<nsILayoutHistoryState> mFrameState;
1123 nsPseudoFrames mPseudoFrames;
1124 // These bits will be added to the state bits of any frame we construct
1125 // using this state.
1126 nsFrameState mAdditionalStateBits;
1128 // Constructor
1129 // Use the passed-in history state.
1130 nsFrameConstructorState(nsIPresShell* aPresShell,
1131 nsIFrame* aFixedContainingBlock,
1132 nsIFrame* aAbsoluteContainingBlock,
1133 nsIFrame* aFloatContainingBlock,
1134 nsILayoutHistoryState* aHistoryState);
1135 // Get the history state from the pres context's pres shell.
1136 nsFrameConstructorState(nsIPresShell* aPresShell,
1137 nsIFrame* aFixedContainingBlock,
1138 nsIFrame* aAbsoluteContainingBlock,
1139 nsIFrame* aFloatContainingBlock);
1141 ~nsFrameConstructorState();
1143 // Function to push the existing absolute containing block state and
1144 // create a new scope. Code that uses this function should get matching
1145 // logic in GetAbsoluteContainingBlock.
1146 void PushAbsoluteContainingBlock(nsIFrame* aNewAbsoluteContainingBlock,
1147 nsFrameConstructorSaveState& aSaveState);
1149 // Function to push the existing float containing block state and
1150 // create a new scope. Code that uses this function should get matching
1151 // logic in GetFloatContainingBlock.
1152 // Pushing a null float containing block forbids any frames from being
1153 // floated until a new float containing block is pushed.
1154 // XXX we should get rid of null float containing blocks and teach the
1155 // various frame classes to deal with floats instead.
1156 void PushFloatContainingBlock(nsIFrame* aNewFloatContainingBlock,
1157 nsFrameConstructorSaveState& aSaveState,
1158 PRBool aFirstLetterStyle,
1159 PRBool aFirstLineStyle);
1161 // Function to return the proper geometric parent for a frame with display
1162 // struct given by aStyleDisplay and parent's frame given by
1163 // aContentParentFrame. If the frame is not allowed to be positioned, pass
1164 // false for aCanBePositioned.
1165 nsIFrame* GetGeometricParent(const nsStyleDisplay* aStyleDisplay,
1166 nsIFrame* aContentParentFrame);
1169 * Function to add a new frame to the right frame list. This MUST be called
1170 * on frames before their children have been processed if the frames might
1171 * conceivably be out-of-flow; otherwise cleanup in error cases won't work
1172 * right. Also, this MUST be called on frames after they have been
1173 * initialized.
1174 * @param aNewFrame the frame to add
1175 * @param aFrameItems the list to add in-flow frames to
1176 * @param aContent the content pointer for aNewFrame
1177 * @param aStyleContext the style context resolved for aContent
1178 * @param aParentFrame the parent frame for the content if it were in-flow
1179 * @param aCanBePositioned pass false if the frame isn't allowed to be
1180 * positioned
1181 * @param aCanBeFloated pass false if the frame isn't allowed to be
1182 * floated
1183 * @param aIsOutOfFlowPopup pass true if the frame is an out-of-flow popup
1184 * (XUL-only)
1185 * @throws NS_ERROR_OUT_OF_MEMORY if it happens.
1186 * @note If this method throws, that means that aNewFrame was not inserted
1187 * into any frame lists. Furthermore, this method will handle cleanup
1188 * of aNewFrame (via calling CleanupFrameReferences() and Destroy() on
1189 * it).
1191 nsresult AddChild(nsIFrame* aNewFrame,
1192 nsFrameItems& aFrameItems,
1193 nsIContent* aContent,
1194 nsStyleContext* aStyleContext,
1195 nsIFrame* aParentFrame,
1196 PRBool aCanBePositioned = PR_TRUE,
1197 PRBool aCanBeFloated = PR_TRUE,
1198 PRBool aIsOutOfFlowPopup = PR_FALSE,
1199 PRBool aInsertAfter = PR_FALSE,
1200 nsIFrame* aInsertAfterFrame = nsnull);
1203 * Function to return the fixed-pos element list. Normally this will just hand back the
1204 * fixed-pos element list, but in case we're dealing with a transformed element that's
1205 * acting as an abs-pos and fixed-pos container, we'll hand back the abs-pos list. Callers should
1206 * use this function if they want to get the list acting as the fixed-pos item parent.
1208 nsAbsoluteItems& GetFixedItems()
1210 return mFixedPosIsAbsPos ? mAbsoluteItems : mFixedItems;
1212 const nsAbsoluteItems& GetFixedItems() const
1214 return mFixedPosIsAbsPos ? mAbsoluteItems : mFixedItems;
1217 protected:
1218 friend class nsFrameConstructorSaveState;
1221 * ProcessFrameInsertions takes the frames in aFrameItems and adds them as
1222 * kids to the aChildListName child list of |aFrameItems.containingBlock|.
1224 void ProcessFrameInsertions(nsAbsoluteItems& aFrameItems,
1225 nsIAtom* aChildListName);
1228 nsFrameConstructorState::nsFrameConstructorState(nsIPresShell* aPresShell,
1229 nsIFrame* aFixedContainingBlock,
1230 nsIFrame* aAbsoluteContainingBlock,
1231 nsIFrame* aFloatContainingBlock,
1232 nsILayoutHistoryState* aHistoryState)
1233 : mPresContext(aPresShell->GetPresContext()),
1234 mPresShell(aPresShell),
1235 mFrameManager(aPresShell->FrameManager()),
1236 #ifdef MOZ_XUL
1237 mRootBox(nsIRootBox::GetRootBox(aPresShell)),
1238 mPopupItems(mRootBox ? mRootBox->GetPopupSetFrame() : nsnull),
1239 #endif
1240 mFixedItems(aFixedContainingBlock),
1241 mAbsoluteItems(aAbsoluteContainingBlock),
1242 mFloatedItems(aFloatContainingBlock),
1243 mFirstLetterStyle(PR_FALSE),
1244 mFirstLineStyle(PR_FALSE),
1245 mFixedPosIsAbsPos(PR_FALSE),
1246 mFrameState(aHistoryState),
1247 mPseudoFrames(),
1248 mAdditionalStateBits(0)
1250 MOZ_COUNT_CTOR(nsFrameConstructorState);
1253 nsFrameConstructorState::nsFrameConstructorState(nsIPresShell* aPresShell,
1254 nsIFrame* aFixedContainingBlock,
1255 nsIFrame* aAbsoluteContainingBlock,
1256 nsIFrame* aFloatContainingBlock)
1257 : mPresContext(aPresShell->GetPresContext()),
1258 mPresShell(aPresShell),
1259 mFrameManager(aPresShell->FrameManager()),
1260 #ifdef MOZ_XUL
1261 mRootBox(nsIRootBox::GetRootBox(aPresShell)),
1262 mPopupItems(mRootBox ? mRootBox->GetPopupSetFrame() : nsnull),
1263 #endif
1264 mFixedItems(aFixedContainingBlock),
1265 mAbsoluteItems(aAbsoluteContainingBlock),
1266 mFloatedItems(aFloatContainingBlock),
1267 mFirstLetterStyle(PR_FALSE),
1268 mFirstLineStyle(PR_FALSE),
1269 mFixedPosIsAbsPos(PR_FALSE),
1270 mPseudoFrames(),
1271 mAdditionalStateBits(0)
1273 MOZ_COUNT_CTOR(nsFrameConstructorState);
1274 mFrameState = aPresShell->GetDocument()->GetLayoutHistoryState();
1277 nsFrameConstructorState::~nsFrameConstructorState()
1279 // Frame order comparison functions only work properly when the placeholders
1280 // have been inserted into the frame tree. So for example if we have a new float
1281 // containing the placeholder for a new abs-pos frame, and we process the abs-pos
1282 // insertion first, then we won't be able to find the right place to insert in
1283 // in the abs-pos list. So put floats in first, because they can contain placeholders
1284 // for abs-pos and fixed-pos items whose containing blocks are outside the floats.
1285 // Then put abs-pos frames in, because they can contain placeholders for fixed-pos
1286 // items whose containing block is outside the abs-pos frames.
1287 MOZ_COUNT_DTOR(nsFrameConstructorState);
1288 ProcessFrameInsertions(mFloatedItems, nsGkAtoms::floatList);
1289 ProcessFrameInsertions(mAbsoluteItems, nsGkAtoms::absoluteList);
1290 ProcessFrameInsertions(mFixedItems, nsGkAtoms::fixedList);
1291 #ifdef MOZ_XUL
1292 ProcessFrameInsertions(mPopupItems, nsGkAtoms::popupList);
1293 #endif
1296 static nsIFrame*
1297 AdjustAbsoluteContainingBlock(nsIFrame* aContainingBlockIn)
1299 if (!aContainingBlockIn) {
1300 return nsnull;
1303 // Always use the container's first continuation. (Inline frames can have
1304 // non-fluid bidi continuations...)
1305 return aContainingBlockIn->GetFirstContinuation();
1308 void
1309 nsFrameConstructorState::PushAbsoluteContainingBlock(nsIFrame* aNewAbsoluteContainingBlock,
1310 nsFrameConstructorSaveState& aSaveState)
1312 aSaveState.mItems = &mAbsoluteItems;
1313 aSaveState.mSavedItems = mAbsoluteItems;
1314 aSaveState.mChildListName = nsGkAtoms::absoluteList;
1315 aSaveState.mState = this;
1317 /* Store whether we're wiring the abs-pos and fixed-pos lists together. */
1318 aSaveState.mFixedPosIsAbsPos = &mFixedPosIsAbsPos;
1319 aSaveState.mSavedFixedPosIsAbsPos = mFixedPosIsAbsPos;
1321 mAbsoluteItems =
1322 nsAbsoluteItems(AdjustAbsoluteContainingBlock(aNewAbsoluteContainingBlock));
1324 /* See if we're wiring the fixed-pos and abs-pos lists together. This happens iff
1325 * we're a transformed element.
1327 mFixedPosIsAbsPos = (aNewAbsoluteContainingBlock &&
1328 aNewAbsoluteContainingBlock->GetStyleDisplay()->HasTransform());
1331 void
1332 nsFrameConstructorState::PushFloatContainingBlock(nsIFrame* aNewFloatContainingBlock,
1333 nsFrameConstructorSaveState& aSaveState,
1334 PRBool aFirstLetterStyle,
1335 PRBool aFirstLineStyle)
1337 // XXXbz we should probably just be able to assert that
1338 // aNewFloatContainingBlock is a float containing block... see XXX comment at
1339 // the top of ProcessChildren.
1340 NS_PRECONDITION(!aNewFloatContainingBlock ||
1341 aNewFloatContainingBlock->GetContentInsertionFrame()->
1342 IsFloatContainingBlock(),
1343 "Please push a real float containing block!");
1344 aSaveState.mItems = &mFloatedItems;
1345 aSaveState.mFirstLetterStyle = &mFirstLetterStyle;
1346 aSaveState.mFirstLineStyle = &mFirstLineStyle;
1347 aSaveState.mSavedItems = mFloatedItems;
1348 aSaveState.mSavedFirstLetterStyle = mFirstLetterStyle;
1349 aSaveState.mSavedFirstLineStyle = mFirstLineStyle;
1350 aSaveState.mChildListName = nsGkAtoms::floatList;
1351 aSaveState.mState = this;
1352 mFloatedItems = nsAbsoluteItems(aNewFloatContainingBlock);
1353 mFirstLetterStyle = aFirstLetterStyle;
1354 mFirstLineStyle = aFirstLineStyle;
1357 nsIFrame*
1358 nsFrameConstructorState::GetGeometricParent(const nsStyleDisplay* aStyleDisplay,
1359 nsIFrame* aContentParentFrame)
1361 NS_PRECONDITION(aStyleDisplay, "Must have display struct!");
1363 // If there is no container for a fixed, absolute, or floating root
1364 // frame, we will ignore the positioning. This hack is originally
1365 // brought to you by the letter T: tables, since other roots don't
1366 // even call into this code. See bug 178855.
1368 // XXX Disabling positioning in this case is a hack. If one was so inclined,
1369 // one could support this either by (1) inserting a dummy block between the
1370 // table and the canvas or (2) teaching the canvas how to reflow positioned
1371 // elements. (1) has the usual problems when multiple frames share the same
1372 // content (notice all the special cases in this file dealing with inner
1373 // tables and outer tables which share the same content). (2) requires some
1374 // work and possible factoring.
1376 // XXXbz couldn't we just force position to "static" on roots and
1377 // float to "none"? That's OK per CSS 2.1, as far as I can tell.
1379 if (aStyleDisplay->IsFloating() && mFloatedItems.containingBlock) {
1380 NS_ASSERTION(!aStyleDisplay->IsAbsolutelyPositioned(),
1381 "Absolutely positioned _and_ floating?");
1382 return mFloatedItems.containingBlock;
1385 if (aStyleDisplay->mPosition == NS_STYLE_POSITION_ABSOLUTE &&
1386 mAbsoluteItems.containingBlock) {
1387 return mAbsoluteItems.containingBlock;
1390 if (aStyleDisplay->mPosition == NS_STYLE_POSITION_FIXED &&
1391 GetFixedItems().containingBlock) {
1392 return GetFixedItems().containingBlock;
1395 return aContentParentFrame;
1398 nsresult
1399 nsFrameConstructorState::AddChild(nsIFrame* aNewFrame,
1400 nsFrameItems& aFrameItems,
1401 nsIContent* aContent,
1402 nsStyleContext* aStyleContext,
1403 nsIFrame* aParentFrame,
1404 PRBool aCanBePositioned,
1405 PRBool aCanBeFloated,
1406 PRBool aIsOutOfFlowPopup,
1407 PRBool aInsertAfter,
1408 nsIFrame* aInsertAfterFrame)
1410 const nsStyleDisplay* disp = aNewFrame->GetStyleDisplay();
1412 // The comments in GetGeometricParent regarding root table frames
1413 // all apply here, unfortunately.
1415 PRBool needPlaceholder = PR_FALSE;
1416 nsFrameItems* frameItems = &aFrameItems;
1417 #ifdef MOZ_XUL
1418 if (NS_UNLIKELY(aIsOutOfFlowPopup)) {
1419 NS_ASSERTION(aNewFrame->GetParent() == mPopupItems.containingBlock,
1420 "Popup whose parent is not the popup containing block?");
1421 NS_ASSERTION(mPopupItems.containingBlock, "Must have a popup set frame!");
1422 needPlaceholder = PR_TRUE;
1423 frameItems = &mPopupItems;
1425 else
1426 #endif // MOZ_XUL
1427 if (aCanBeFloated && disp->IsFloating() &&
1428 mFloatedItems.containingBlock) {
1429 NS_ASSERTION(aNewFrame->GetParent() == mFloatedItems.containingBlock,
1430 "Float whose parent is not the float containing block?");
1431 needPlaceholder = PR_TRUE;
1432 frameItems = &mFloatedItems;
1434 else if (aCanBePositioned) {
1435 if (disp->mPosition == NS_STYLE_POSITION_ABSOLUTE &&
1436 mAbsoluteItems.containingBlock) {
1437 NS_ASSERTION(aNewFrame->GetParent() == mAbsoluteItems.containingBlock,
1438 "Abs pos whose parent is not the abs pos containing block?");
1439 needPlaceholder = PR_TRUE;
1440 frameItems = &mAbsoluteItems;
1442 if (disp->mPosition == NS_STYLE_POSITION_FIXED &&
1443 GetFixedItems().containingBlock) {
1444 NS_ASSERTION(aNewFrame->GetParent() == GetFixedItems().containingBlock,
1445 "Fixed pos whose parent is not the fixed pos containing block?");
1446 needPlaceholder = PR_TRUE;
1447 frameItems = &GetFixedItems();
1451 if (needPlaceholder) {
1452 NS_ASSERTION(frameItems != &aFrameItems,
1453 "Putting frame in-flow _and_ want a placeholder?");
1454 nsIFrame* placeholderFrame;
1455 nsresult rv =
1456 nsCSSFrameConstructor::CreatePlaceholderFrameFor(mPresShell,
1457 aContent,
1458 aNewFrame,
1459 aStyleContext,
1460 aParentFrame,
1461 nsnull,
1462 &placeholderFrame);
1463 if (NS_FAILED(rv)) {
1464 // Note that aNewFrame could be the top frame for a scrollframe setup,
1465 // hence already set as the primary frame. So we have to clean up here.
1466 // But it shouldn't have any out-of-flow kids.
1467 // XXXbz Maybe add a utility function to assert that?
1468 CleanupFrameReferences(mFrameManager, aNewFrame);
1469 aNewFrame->Destroy();
1470 return rv;
1473 placeholderFrame->AddStateBits(mAdditionalStateBits);
1474 // Add the placeholder frame to the flow
1475 aFrameItems.AddChild(placeholderFrame);
1477 #ifdef DEBUG
1478 else {
1479 NS_ASSERTION(aNewFrame->GetParent() == aParentFrame,
1480 "In-flow frame has wrong parent");
1482 #endif
1484 if (aInsertAfter) {
1485 frameItems->InsertChildAfter(aNewFrame, aInsertAfterFrame);
1486 } else {
1487 frameItems->AddChild(aNewFrame);
1490 // Now add the special siblings too.
1491 nsIFrame* specialSibling = aNewFrame;
1492 while (specialSibling && IsFrameSpecial(specialSibling)) {
1493 specialSibling = GetSpecialSibling(specialSibling);
1494 if (specialSibling) {
1495 NS_ASSERTION(frameItems == &aFrameItems,
1496 "IB split ending up in an out-of-flow childlist?");
1497 frameItems->AddChild(specialSibling);
1501 return NS_OK;
1504 void
1505 nsFrameConstructorState::ProcessFrameInsertions(nsAbsoluteItems& aFrameItems,
1506 nsIAtom* aChildListName)
1508 #define NS_NONXUL_LIST_TEST (&aFrameItems == &mFloatedItems && \
1509 aChildListName == nsGkAtoms::floatList) || \
1510 (&aFrameItems == &mAbsoluteItems && \
1511 aChildListName == nsGkAtoms::absoluteList) || \
1512 (&aFrameItems == &mFixedItems && \
1513 aChildListName == nsGkAtoms::fixedList)
1514 #ifdef MOZ_XUL
1515 NS_PRECONDITION(NS_NONXUL_LIST_TEST ||
1516 (&aFrameItems == &mPopupItems &&
1517 aChildListName == nsGkAtoms::popupList),
1518 "Unexpected aFrameItems/aChildListName combination");
1519 #else
1520 NS_PRECONDITION(NS_NONXUL_LIST_TEST,
1521 "Unexpected aFrameItems/aChildListName combination");
1522 #endif
1524 nsIFrame* firstNewFrame = aFrameItems.childList;
1526 if (!firstNewFrame) {
1527 return;
1530 nsIFrame* containingBlock = aFrameItems.containingBlock;
1532 NS_ASSERTION(containingBlock,
1533 "Child list without containing block?");
1535 // Insert the frames hanging out in aItems. We can use SetInitialChildList()
1536 // if the containing block hasn't been reflown yet (so NS_FRAME_FIRST_REFLOW
1537 // is set) and doesn't have any frames in the aChildListName child list yet.
1538 nsIFrame* firstChild = containingBlock->GetFirstChild(aChildListName);
1539 nsresult rv = NS_OK;
1540 if (!firstChild && (containingBlock->GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
1541 rv = containingBlock->SetInitialChildList(aChildListName, firstNewFrame);
1542 } else {
1543 // Note that whether the frame construction context is doing an append or
1544 // not is not helpful here, since it could be appending to some frame in
1545 // the middle of the document, which means we're not necessarily
1546 // appending to the children of the containing block.
1548 // We need to make sure the 'append to the end of document' case is fast.
1549 // So first test the last child of the containing block
1550 nsIFrame* lastChild = nsLayoutUtils::GetLastSibling(firstChild);
1552 // CompareTreePosition uses placeholder hierarchy for out of flow frames,
1553 // so this will make out-of-flows respect the ordering of placeholders,
1554 // which is great because it takes care of anonymous content.
1555 if (!lastChild ||
1556 nsLayoutUtils::CompareTreePosition(lastChild, firstNewFrame, containingBlock) < 0) {
1557 // no lastChild, or lastChild comes before the new children, so just append
1558 rv = containingBlock->AppendFrames(aChildListName, firstNewFrame);
1559 } else {
1560 nsIFrame* insertionPoint = nsnull;
1561 // try the other children
1562 for (nsIFrame* f = firstChild; f != lastChild; f = f->GetNextSibling()) {
1563 PRInt32 compare =
1564 nsLayoutUtils::CompareTreePosition(f, firstNewFrame, containingBlock);
1565 if (compare > 0) {
1566 // f comes after the new children, so stop here and insert after
1567 // the previous frame
1568 break;
1570 insertionPoint = f;
1573 rv = containingBlock->InsertFrames(aChildListName, insertionPoint,
1574 firstNewFrame);
1577 aFrameItems.childList = nsnull;
1578 // XXXbz And if NS_FAILED(rv), what? I guess we need to clean up the list
1579 // and deal with all the placeholders... but what if the placeholders aren't
1580 // in the document yet? Could that happen?
1581 NS_ASSERTION(NS_SUCCEEDED(rv), "Frames getting lost!");
1585 nsFrameConstructorSaveState::nsFrameConstructorSaveState()
1586 : mItems(nsnull),
1587 mFirstLetterStyle(nsnull),
1588 mFirstLineStyle(nsnull),
1589 mFixedPosIsAbsPos(nsnull),
1590 mSavedItems(nsnull),
1591 mSavedFirstLetterStyle(PR_FALSE),
1592 mSavedFirstLineStyle(PR_FALSE),
1593 mSavedFixedPosIsAbsPos(PR_FALSE),
1594 mChildListName(nsnull),
1595 mState(nsnull)
1599 nsFrameConstructorSaveState::~nsFrameConstructorSaveState()
1601 // Restore the state
1602 if (mItems) {
1603 NS_ASSERTION(mState, "Can't have mItems set without having a state!");
1604 mState->ProcessFrameInsertions(*mItems, mChildListName);
1605 *mItems = mSavedItems;
1606 #ifdef DEBUG
1607 // We've transferred the child list, so drop the pointer we held to it.
1608 // Note that this only matters for the assert in ~nsAbsoluteItems.
1609 mSavedItems.childList = nsnull;
1610 #endif
1612 if (mFirstLetterStyle) {
1613 *mFirstLetterStyle = mSavedFirstLetterStyle;
1615 if (mFirstLineStyle) {
1616 *mFirstLineStyle = mSavedFirstLineStyle;
1618 if (mFixedPosIsAbsPos) {
1619 *mFixedPosIsAbsPos = mSavedFixedPosIsAbsPos;
1623 static
1624 PRBool IsBorderCollapse(nsIFrame* aFrame)
1626 for (nsIFrame* frame = aFrame; frame; frame = frame->GetParent()) {
1627 if (nsGkAtoms::tableFrame == frame->GetType()) {
1628 return ((nsTableFrame*)frame)->IsBorderCollapse();
1631 NS_ASSERTION(PR_FALSE, "program error");
1632 return PR_FALSE;
1636 * Utility method, called from MoveChildrenTo(), that recursively
1637 * descends down the frame hierarchy looking for floating frames that
1638 * need parent pointer adjustments to account for the containment block
1639 * changes that could occur as the result of the reparenting done in
1640 * MoveChildrenTo().
1642 static void
1643 AdjustFloatParentPtrs(nsIFrame* aFrame,
1644 nsFrameConstructorState& aState,
1645 nsFrameConstructorState& aOuterState)
1647 NS_PRECONDITION(aFrame, "must have frame to work with");
1649 nsIFrame *outOfFlowFrame = nsPlaceholderFrame::GetRealFrameFor(aFrame);
1650 if (outOfFlowFrame != aFrame) {
1651 if (outOfFlowFrame->GetStyleDisplay()->IsFloating()) {
1652 // Update the parent pointer for outOfFlowFrame since its
1653 // containing block has changed as the result of reparenting
1654 // and move it from the outer state to the inner, bug 307277.
1656 nsIFrame *parent = aState.mFloatedItems.containingBlock;
1657 NS_ASSERTION(parent, "Should have float containing block here!");
1658 NS_ASSERTION(outOfFlowFrame->GetParent() == aOuterState.mFloatedItems.containingBlock,
1659 "expected the float to be a child of the outer CB");
1661 if (aOuterState.mFloatedItems.RemoveChild(outOfFlowFrame, nsnull)) {
1662 aState.mFloatedItems.AddChild(outOfFlowFrame);
1663 } else {
1664 NS_NOTREACHED("float wasn't in the outer state float list");
1667 outOfFlowFrame->SetParent(parent);
1668 if (outOfFlowFrame->GetStateBits() &
1669 (NS_FRAME_HAS_VIEW | NS_FRAME_HAS_CHILD_WITH_VIEW)) {
1670 // We don't need to walk up the tree, since we're doing this
1671 // recursively.
1672 parent->AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
1676 // All out-of-flows are automatically float containing blocks, so we're
1677 // done here.
1678 return;
1681 if (aFrame->IsFloatContainingBlock()) {
1682 // No need to recurse further; floats whose placeholders are
1683 // inside a block already have the right parent.
1684 return;
1687 // Dive down into children to see if any of their
1688 // placeholders need adjusting.
1689 nsIFrame *childFrame = aFrame->GetFirstChild(nsnull);
1690 while (childFrame) {
1691 // XXX_kin: Do we need to prevent descent into anonymous content here?
1693 AdjustFloatParentPtrs(childFrame, aState, aOuterState);
1694 childFrame = childFrame->GetNextSibling();
1699 * Moves frames to a new parent, updating the style context and propagating
1700 * relevant frame state bits. |aState| may be null, in which case the parent
1701 * pointers of out-of-flow frames will remain untouched.
1703 static void
1704 MoveChildrenTo(nsFrameManager* aFrameManager,
1705 nsIFrame* aNewParent,
1706 nsIFrame* aFrameList,
1707 nsIFrame* aFrameListEnd,
1708 nsFrameConstructorState* aState,
1709 nsFrameConstructorState* aOuterState)
1711 PRBool setHasChildWithView = PR_FALSE;
1713 while (aFrameList && aFrameList != aFrameListEnd) {
1714 if (!setHasChildWithView
1715 && (aFrameList->GetStateBits() & (NS_FRAME_HAS_VIEW | NS_FRAME_HAS_CHILD_WITH_VIEW))) {
1716 setHasChildWithView = PR_TRUE;
1719 aFrameList->SetParent(aNewParent);
1721 // If aState is not null, the caller expects us to make adjustments so that
1722 // floats whose placeholders are descendants of frames in aFrameList point
1723 // to the correct parent.
1724 if (aState) {
1725 NS_ASSERTION(aOuterState, "need an outer state too");
1726 AdjustFloatParentPtrs(aFrameList, *aState, *aOuterState);
1729 aFrameList = aFrameList->GetNextSibling();
1732 if (setHasChildWithView) {
1733 do {
1734 aNewParent->AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
1735 aNewParent = aNewParent->GetParent();
1736 } while (aNewParent &&
1737 !(aNewParent->GetStateBits() & NS_FRAME_HAS_CHILD_WITH_VIEW));
1741 // -----------------------------------------------------------
1744 // Structure used to ensure that bindings are properly enqueued in the
1745 // binding manager's attached queue.
1746 struct NS_STACK_CLASS nsAutoEnqueueBinding
1748 nsAutoEnqueueBinding(nsIDocument* aDocument) :
1749 mDocument(aDocument)
1752 ~nsAutoEnqueueBinding();
1754 nsRefPtr<nsXBLBinding> mBinding;
1755 private:
1756 nsIDocument* mDocument;
1759 nsAutoEnqueueBinding::~nsAutoEnqueueBinding()
1761 if (mBinding) {
1762 mDocument->BindingManager()->AddToAttachedQueue(mBinding);
1767 // Helper function that determines the child list name that aChildFrame
1768 // is contained in
1769 static nsIAtom*
1770 GetChildListNameFor(nsIFrame* aChildFrame)
1772 nsIAtom* listName;
1774 if (aChildFrame->GetStateBits() & NS_FRAME_IS_OVERFLOW_CONTAINER) {
1775 listName = nsGkAtoms::overflowContainersList;
1777 // See if the frame is moved out of the flow
1778 else if (aChildFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW) {
1779 // Look at the style information to tell
1780 const nsStyleDisplay* disp = aChildFrame->GetStyleDisplay();
1782 if (NS_STYLE_POSITION_ABSOLUTE == disp->mPosition) {
1783 listName = nsGkAtoms::absoluteList;
1784 } else if (NS_STYLE_POSITION_FIXED == disp->mPosition) {
1785 listName = nsGkAtoms::fixedList;
1786 #ifdef MOZ_XUL
1787 } else if (NS_STYLE_DISPLAY_POPUP == disp->mDisplay) {
1788 // Out-of-flows that are DISPLAY_POPUP must be kids of the root popup set
1789 #ifdef DEBUG
1790 nsIFrame* parent = aChildFrame->GetParent();
1791 NS_ASSERTION(parent && parent->GetType() == nsGkAtoms::popupSetFrame,
1792 "Unexpected parent");
1793 #endif // DEBUG
1795 // XXX FIXME: Bug 350740
1796 // Return here, because the postcondition for this function actually
1797 // fails for this case, since the popups are not in a "real" frame list
1798 // in the popup set.
1799 return nsGkAtoms::popupList;
1800 #endif // MOZ_XUL
1801 } else {
1802 NS_ASSERTION(aChildFrame->GetStyleDisplay()->IsFloating(),
1803 "not a floated frame");
1804 listName = nsGkAtoms::floatList;
1807 } else {
1808 listName = nsnull;
1811 #ifdef NS_DEBUG
1812 // Verify that the frame is actually in that child list or in the
1813 // corresponding overflow list.
1814 nsIFrame* parent = aChildFrame->GetParent();
1815 PRBool found = nsFrameList(parent->GetFirstChild(listName))
1816 .ContainsFrame(aChildFrame);
1817 if (!found) {
1818 if (!(aChildFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW)) {
1819 found = nsFrameList(parent->GetFirstChild(nsGkAtoms::overflowList))
1820 .ContainsFrame(aChildFrame);
1822 else if (aChildFrame->GetStyleDisplay()->IsFloating()) {
1823 found = nsFrameList(parent->GetFirstChild(nsGkAtoms::overflowOutOfFlowList))
1824 .ContainsFrame(aChildFrame);
1826 // else it's positioned and should have been on the 'listName' child list.
1827 NS_POSTCONDITION(found, "not in child list");
1829 #endif
1831 return listName;
1834 //----------------------------------------------------------------------
1836 nsCSSFrameConstructor::nsCSSFrameConstructor(nsIDocument *aDocument,
1837 nsIPresShell *aPresShell)
1838 : mDocument(aDocument)
1839 , mPresShell(aPresShell)
1840 , mInitialContainingBlock(nsnull)
1841 , mRootElementStyleFrame(nsnull)
1842 , mFixedContainingBlock(nsnull)
1843 , mDocElementContainingBlock(nsnull)
1844 , mGfxScrollFrame(nsnull)
1845 , mPageSequenceFrame(nsnull)
1846 , mUpdateCount(0)
1847 , mQuotesDirty(PR_FALSE)
1848 , mCountersDirty(PR_FALSE)
1849 , mIsDestroyingFrameTree(PR_FALSE)
1850 , mRebuildAllStyleData(PR_FALSE)
1851 , mHasRootAbsPosContainingBlock(PR_FALSE)
1853 if (!gGotXBLFormPrefs) {
1854 gGotXBLFormPrefs = PR_TRUE;
1856 gUseXBLForms =
1857 nsContentUtils::GetBoolPref("nglayout.debug.enable_xbl_forms");
1860 // XXXbz this should be in Init() or something!
1861 if (!mPendingRestyles.Init()) {
1862 // now what?
1865 #ifdef DEBUG
1866 static PRBool gFirstTime = PR_TRUE;
1867 if (gFirstTime) {
1868 gFirstTime = PR_FALSE;
1869 char* flags = PR_GetEnv("GECKO_FRAMECTOR_DEBUG_FLAGS");
1870 if (flags) {
1871 PRBool error = PR_FALSE;
1872 for (;;) {
1873 char* comma = PL_strchr(flags, ',');
1874 if (comma)
1875 *comma = '\0';
1877 PRBool found = PR_FALSE;
1878 FrameCtorDebugFlags* flag = gFlags;
1879 FrameCtorDebugFlags* limit = gFlags + NUM_DEBUG_FLAGS;
1880 while (flag < limit) {
1881 if (PL_strcasecmp(flag->name, flags) == 0) {
1882 *(flag->on) = PR_TRUE;
1883 printf("nsCSSFrameConstructor: setting %s debug flag on\n", flag->name);
1884 found = PR_TRUE;
1885 break;
1887 ++flag;
1890 if (! found)
1891 error = PR_TRUE;
1893 if (! comma)
1894 break;
1896 *comma = ',';
1897 flags = comma + 1;
1900 if (error) {
1901 printf("Here are the available GECKO_FRAMECTOR_DEBUG_FLAGS:\n");
1902 FrameCtorDebugFlags* flag = gFlags;
1903 FrameCtorDebugFlags* limit = gFlags + NUM_DEBUG_FLAGS;
1904 while (flag < limit) {
1905 printf(" %s\n", flag->name);
1906 ++flag;
1908 printf("Note: GECKO_FRAMECTOR_DEBUG_FLAGS is a comma separated list of flag\n");
1909 printf("names (no whitespace)\n");
1913 #endif
1916 nsIXBLService * nsCSSFrameConstructor::GetXBLService()
1918 if (!gXBLService) {
1919 nsresult rv = CallGetService("@mozilla.org/xbl;1", &gXBLService);
1920 if (NS_FAILED(rv))
1921 gXBLService = nsnull;
1924 return gXBLService;
1927 void
1928 nsCSSFrameConstructor::NotifyDestroyingFrame(nsIFrame* aFrame)
1930 NS_PRECONDITION(mUpdateCount != 0,
1931 "Should be in an update while destroying frames");
1933 if (aFrame->GetStateBits() & NS_FRAME_GENERATED_CONTENT) {
1934 if (mQuoteList.DestroyNodesFor(aFrame))
1935 QuotesDirty();
1938 if (mCounterManager.DestroyNodesFor(aFrame)) {
1939 // Technically we don't need to update anything if we destroyed only
1940 // USE nodes. However, this is unlikely to happen in the real world
1941 // since USE nodes generally go along with INCREMENT nodes.
1942 CountersDirty();
1946 struct nsGenConInitializer {
1947 nsAutoPtr<nsGenConNode> mNode;
1948 nsGenConList* mList;
1949 void (nsCSSFrameConstructor::*mDirtyAll)();
1951 nsGenConInitializer(nsGenConNode* aNode, nsGenConList* aList,
1952 void (nsCSSFrameConstructor::*aDirtyAll)())
1953 : mNode(aNode), mList(aList), mDirtyAll(aDirtyAll) {}
1956 static void
1957 DestroyGenConInitializer(void* aFrame,
1958 nsIAtom* aPropertyName,
1959 void* aPropertyValue,
1960 void* aDtorData)
1962 delete static_cast<nsGenConInitializer*>(aPropertyValue);
1965 already_AddRefed<nsIContent>
1966 nsCSSFrameConstructor::CreateGenConTextNode(const nsString& aString,
1967 nsCOMPtr<nsIDOMCharacterData>* aText,
1968 nsGenConInitializer* aInitializer)
1970 nsCOMPtr<nsIContent> content;
1971 NS_NewTextNode(getter_AddRefs(content), mDocument->NodeInfoManager());
1972 if (!content) {
1973 // XXX The quotes/counters code doesn't like the text pointer
1974 // being null in case of dynamic changes!
1975 NS_ASSERTION(!aText, "this OOM case isn't handled very well");
1976 return nsnull;
1978 content->SetText(aString, PR_FALSE);
1979 if (aText) {
1980 *aText = do_QueryInterface(content);
1982 if (aInitializer) {
1983 content->SetProperty(nsGkAtoms::genConInitializerProperty, aInitializer,
1984 DestroyGenConInitializer);
1986 return content.forget();
1989 already_AddRefed<nsIContent>
1990 nsCSSFrameConstructor::CreateGeneratedContent(nsIContent* aParentContent,
1991 nsStyleContext* aStyleContext,
1992 PRUint32 aContentIndex)
1994 // Get the content value
1995 const nsStyleContentData &data =
1996 aStyleContext->GetStyleContent()->ContentAt(aContentIndex);
1997 nsStyleContentType type = data.mType;
1999 if (eStyleContentType_Image == type) {
2000 if (!data.mContent.mImage) {
2001 // CSS had something specified that couldn't be converted to an
2002 // image object
2003 return nsnull;
2006 // Create an image content object and pass it the image request.
2007 // XXX Check if it's an image type we can handle...
2009 nsCOMPtr<nsINodeInfo> nodeInfo;
2010 nodeInfo = mDocument->NodeInfoManager()->GetNodeInfo(nsGkAtoms::mozgeneratedcontentimage, nsnull,
2011 kNameSpaceID_XHTML);
2013 nsCOMPtr<nsIContent> content;
2014 NS_NewGenConImageContent(getter_AddRefs(content), nodeInfo,
2015 data.mContent.mImage);
2016 return content.forget();
2019 switch (type) {
2020 case eStyleContentType_String:
2021 return CreateGenConTextNode(nsDependentString(data.mContent.mString), nsnull,
2022 nsnull);
2024 case eStyleContentType_Attr:
2026 nsCOMPtr<nsIAtom> attrName;
2027 PRInt32 attrNameSpace = kNameSpaceID_None;
2028 nsAutoString contentString(data.mContent.mString);
2029 PRInt32 barIndex = contentString.FindChar('|'); // CSS namespace delimiter
2030 if (-1 != barIndex) {
2031 nsAutoString nameSpaceVal;
2032 contentString.Left(nameSpaceVal, barIndex);
2033 PRInt32 error;
2034 attrNameSpace = nameSpaceVal.ToInteger(&error, 10);
2035 contentString.Cut(0, barIndex + 1);
2036 if (contentString.Length()) {
2037 attrName = do_GetAtom(contentString);
2040 else {
2041 attrName = do_GetAtom(contentString);
2044 if (!attrName) {
2045 return nsnull;
2048 nsCOMPtr<nsIContent> content;
2049 NS_NewAttributeContent(mDocument->NodeInfoManager(),
2050 attrNameSpace, attrName, getter_AddRefs(content));
2051 return content.forget();
2054 case eStyleContentType_Counter:
2055 case eStyleContentType_Counters:
2057 nsCSSValue::Array* counters = data.mContent.mCounters;
2058 nsCounterList* counterList = mCounterManager.CounterListFor(
2059 nsDependentString(counters->Item(0).GetStringBufferValue()));
2060 if (!counterList)
2061 return nsnull;
2063 nsCounterUseNode* node =
2064 new nsCounterUseNode(counters, aContentIndex,
2065 type == eStyleContentType_Counters);
2066 if (!node)
2067 return nsnull;
2069 nsGenConInitializer* initializer =
2070 new nsGenConInitializer(node, counterList,
2071 &nsCSSFrameConstructor::CountersDirty);
2072 return CreateGenConTextNode(EmptyString(), &node->mText, initializer);
2075 case eStyleContentType_Image:
2076 NS_NOTREACHED("handled by if above");
2077 return nsnull;
2079 case eStyleContentType_OpenQuote:
2080 case eStyleContentType_CloseQuote:
2081 case eStyleContentType_NoOpenQuote:
2082 case eStyleContentType_NoCloseQuote:
2084 nsQuoteNode* node =
2085 new nsQuoteNode(type, aContentIndex);
2086 if (!node)
2087 return nsnull;
2089 nsGenConInitializer* initializer =
2090 new nsGenConInitializer(node, &mQuoteList,
2091 &nsCSSFrameConstructor::QuotesDirty);
2092 return CreateGenConTextNode(EmptyString(), &node->mText, initializer);
2095 case eStyleContentType_AltContent:
2097 // Use the "alt" attribute; if that fails and the node is an HTML
2098 // <input>, try the value attribute and then fall back to some default
2099 // localized text we have.
2100 // XXX what if the 'alt' attribute is added later, how will we
2101 // detect that and do the right thing here?
2102 if (aParentContent->HasAttr(kNameSpaceID_None, nsGkAtoms::alt)) {
2103 nsCOMPtr<nsIContent> content;
2104 NS_NewAttributeContent(mDocument->NodeInfoManager(),
2105 kNameSpaceID_None, nsGkAtoms::alt, getter_AddRefs(content));
2106 return content.forget();
2109 if (aParentContent->IsNodeOfType(nsINode::eHTML) &&
2110 aParentContent->NodeInfo()->Equals(nsGkAtoms::input)) {
2111 if (aParentContent->HasAttr(kNameSpaceID_None, nsGkAtoms::value)) {
2112 nsCOMPtr<nsIContent> content;
2113 NS_NewAttributeContent(mDocument->NodeInfoManager(),
2114 kNameSpaceID_None, nsGkAtoms::value, getter_AddRefs(content));
2115 return content.forget();
2118 nsXPIDLString temp;
2119 nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
2120 "Submit", temp);
2121 return CreateGenConTextNode(temp, nsnull, nsnull);
2124 break;
2126 } // switch
2128 return nsnull;
2131 static void DestroyContent(void *aObject,
2132 nsIAtom *aPropertyName,
2133 void *aPropertyValue,
2134 void *aData)
2136 nsIContent* content = static_cast<nsIContent*>(aPropertyValue);
2137 content->UnbindFromTree();
2138 NS_RELEASE(content);
2142 * aParentFrame - the frame that should be the parent of the generated
2143 * content. This is the frame for the corresponding content node,
2144 * which must not be a leaf frame.
2146 * Any frames created are added to aFrameItems (or possibly left
2147 * in the table pseudoframe state in aState).
2149 * We create an XML element (tag _moz_generated_content_before or
2150 * _moz_generated_content_after) representing the pseudoelement. We
2151 * create a DOM node for each 'content' item and make those nodes the
2152 * children of the XML element. Then we create a frame subtree for
2153 * the XML element as if it were a regular child of
2154 * aParentFrame/aParentContent, giving the XML element the ::before or
2155 * ::after style.
2157 void
2158 nsCSSFrameConstructor::CreateGeneratedContentFrame(nsFrameConstructorState& aState,
2159 nsIFrame* aParentFrame,
2160 nsIContent* aParentContent,
2161 nsStyleContext* aStyleContext,
2162 nsIAtom* aPseudoElement,
2163 nsFrameItems& aFrameItems)
2165 if (!aParentContent->IsNodeOfType(nsINode::eELEMENT))
2166 return;
2168 nsStyleSet *styleSet = mPresShell->StyleSet();
2170 // Probe for the existence of the pseudo-element
2171 nsRefPtr<nsStyleContext> pseudoStyleContext;
2172 pseudoStyleContext = styleSet->ProbePseudoStyleFor(aParentContent,
2173 aPseudoElement,
2174 aStyleContext);
2175 if (!pseudoStyleContext)
2176 return;
2177 // |ProbePseudoStyleFor| checked the 'display' property and the
2178 // |ContentCount()| of the 'content' property for us.
2179 nsCOMPtr<nsINodeInfo> nodeInfo;
2180 nsIAtom* elemName = aPseudoElement == nsCSSPseudoElements::before ?
2181 nsGkAtoms::mozgeneratedcontentbefore : nsGkAtoms::mozgeneratedcontentafter;
2182 nodeInfo = mDocument->NodeInfoManager()->GetNodeInfo(elemName, nsnull,
2183 kNameSpaceID_None);
2184 nsIContent* container;
2185 nsresult rv = NS_NewXMLElement(&container, nodeInfo);
2186 if (NS_FAILED(rv))
2187 return;
2188 container->SetNativeAnonymous();
2189 // Transfer ownership to the frame
2190 aParentFrame->SetProperty(aPseudoElement, container, DestroyContent);
2192 rv = container->BindToTree(mDocument, aParentContent, aParentContent, PR_TRUE);
2193 if (NS_FAILED(rv)) {
2194 container->UnbindFromTree();
2195 return;
2198 PRUint32 contentCount = pseudoStyleContext->GetStyleContent()->ContentCount();
2199 for (PRUint32 contentIndex = 0; contentIndex < contentCount; contentIndex++) {
2200 nsCOMPtr<nsIContent> content =
2201 CreateGeneratedContent(aParentContent, pseudoStyleContext, contentIndex);
2202 if (content) {
2203 container->AppendChildTo(content, PR_FALSE);
2207 nsFrameState savedStateBits = aState.mAdditionalStateBits;
2208 // Ensure that frames created here are all tagged with
2209 // NS_FRAME_GENERATED_CONTENT.
2210 aState.mAdditionalStateBits |= NS_FRAME_GENERATED_CONTENT;
2212 ConstructFrameInternal(aState, container, aParentFrame,
2213 elemName, kNameSpaceID_None, pseudoStyleContext, aFrameItems, PR_TRUE);
2214 aState.mAdditionalStateBits = savedStateBits;
2217 nsresult
2218 nsCSSFrameConstructor::CreateInputFrame(nsFrameConstructorState& aState,
2219 nsIContent* aContent,
2220 nsIFrame* aParentFrame,
2221 nsIAtom* aTag,
2222 nsStyleContext* aStyleContext,
2223 nsIFrame** aFrame,
2224 const nsStyleDisplay* aStyleDisplay,
2225 PRBool& aFrameHasBeenInitialized,
2226 PRBool& aAddedToFrameList,
2227 nsFrameItems& aFrameItems,
2228 PRBool aHasPseudoParent)
2230 // Make sure to keep IsSpecialContent in synch with this code
2232 // Note: do not do anything in this method that assumes pseudo-frames have
2233 // been processed. If you feel the urge to do something like that, fix
2234 // callers accordingly.
2235 nsCOMPtr<nsIFormControl> control = do_QueryInterface(aContent);
2236 NS_ASSERTION(control, "input is not an nsIFormControl!");
2238 switch (control->GetType()) {
2239 case NS_FORM_INPUT_SUBMIT:
2240 case NS_FORM_INPUT_RESET:
2241 case NS_FORM_INPUT_BUTTON:
2243 if (gUseXBLForms)
2244 return NS_OK; // update IsSpecialContent if this becomes functional
2246 nsresult rv = ConstructButtonFrame(aState, aContent, aParentFrame,
2247 aTag, aStyleContext, aFrame,
2248 aStyleDisplay, aFrameItems,
2249 aHasPseudoParent);
2250 aAddedToFrameList = PR_TRUE;
2251 aFrameHasBeenInitialized = PR_TRUE;
2252 return rv;
2255 case NS_FORM_INPUT_CHECKBOX:
2256 if (gUseXBLForms)
2257 return NS_OK; // see comment above
2258 return ConstructCheckboxControlFrame(aFrame, aContent, aStyleContext);
2260 case NS_FORM_INPUT_RADIO:
2261 if (gUseXBLForms)
2262 return NS_OK; // see comment above
2263 return ConstructRadioControlFrame(aFrame, aContent, aStyleContext);
2265 case NS_FORM_INPUT_FILE:
2267 *aFrame = NS_NewFileControlFrame(mPresShell, aStyleContext);
2269 if (*aFrame) {
2270 // The (block-like) file control frame should have a space manager
2271 (*aFrame)->AddStateBits(NS_BLOCK_SPACE_MGR);
2272 return NS_OK;
2274 else {
2275 return NS_ERROR_OUT_OF_MEMORY;
2279 case NS_FORM_INPUT_HIDDEN:
2280 return NS_OK; // this does not create a frame so it needs special handling
2281 // in IsSpecialContent
2283 case NS_FORM_INPUT_IMAGE:
2284 return CreateHTMLImageFrame(aContent, aStyleContext,
2285 NS_NewImageControlFrame, aFrame);
2287 case NS_FORM_INPUT_TEXT:
2288 case NS_FORM_INPUT_PASSWORD:
2290 *aFrame = NS_NewTextControlFrame(mPresShell, aStyleContext);
2292 return NS_UNLIKELY(!*aFrame) ? NS_ERROR_OUT_OF_MEMORY : NS_OK;
2295 default:
2296 NS_ASSERTION(0, "Unknown input type!");
2297 return NS_ERROR_INVALID_ARG;
2301 nsresult
2302 nsCSSFrameConstructor::CreateHTMLImageFrame(nsIContent* aContent,
2303 nsStyleContext* aStyleContext,
2304 ImageFrameCreatorFunc aFunc,
2305 nsIFrame** aFrame)
2307 *aFrame = nsnull;
2309 // Make sure to keep IsSpecialContent in synch with this code
2310 if (nsImageFrame::ShouldCreateImageFrameFor(aContent, aStyleContext)) {
2311 *aFrame = (*aFunc)(mPresShell, aStyleContext);
2313 if (NS_UNLIKELY(!*aFrame))
2314 return NS_ERROR_OUT_OF_MEMORY;
2317 return NS_OK;
2320 static PRBool
2321 TextIsOnlyWhitespace(nsIContent* aContent)
2323 return aContent->IsNodeOfType(nsINode::eTEXT) &&
2324 aContent->TextIsOnlyWhitespace();
2327 /****************************************************
2328 ** BEGIN TABLE SECTION
2329 ****************************************************/
2331 // The term pseudo frame is being used instead of anonymous frame, since anonymous
2332 // frame has been used elsewhere to refer to frames that have generated content
2334 // aIncludeSpecial applies to captions, col groups, cols and cells.
2335 // These do not generate pseudo frame wrappers for foreign children.
2337 static PRBool
2338 IsTableRelated(PRUint8 aDisplay,
2339 PRBool aIncludeSpecial)
2341 if ((aDisplay == NS_STYLE_DISPLAY_TABLE) ||
2342 (aDisplay == NS_STYLE_DISPLAY_INLINE_TABLE) ||
2343 (aDisplay == NS_STYLE_DISPLAY_TABLE_HEADER_GROUP) ||
2344 (aDisplay == NS_STYLE_DISPLAY_TABLE_ROW_GROUP) ||
2345 (aDisplay == NS_STYLE_DISPLAY_TABLE_FOOTER_GROUP) ||
2346 (aDisplay == NS_STYLE_DISPLAY_TABLE_ROW)) {
2347 return PR_TRUE;
2349 else if (aIncludeSpecial &&
2350 ((aDisplay == NS_STYLE_DISPLAY_TABLE_CAPTION) ||
2351 (aDisplay == NS_STYLE_DISPLAY_TABLE_COLUMN_GROUP) ||
2352 (aDisplay == NS_STYLE_DISPLAY_TABLE_COLUMN) ||
2353 (aDisplay == NS_STYLE_DISPLAY_TABLE_CELL))) {
2354 return PR_TRUE;
2356 else return PR_FALSE;
2359 static PRBool
2360 IsTableRelated(nsIAtom* aParentType,
2361 PRBool aIncludeSpecial)
2363 if ((nsGkAtoms::tableFrame == aParentType) ||
2364 (nsGkAtoms::tableRowGroupFrame == aParentType) ||
2365 (nsGkAtoms::tableRowFrame == aParentType)) {
2366 return PR_TRUE;
2368 else if (aIncludeSpecial &&
2369 ((nsGkAtoms::tableCaptionFrame == aParentType) ||
2370 (nsGkAtoms::tableColGroupFrame == aParentType) ||
2371 (nsGkAtoms::tableColFrame == aParentType) ||
2372 IS_TABLE_CELL(aParentType))) {
2373 return PR_TRUE;
2375 else return PR_FALSE;
2378 static nsIFrame*
2379 AdjustCaptionParentFrame(nsIFrame* aParentFrame)
2381 if (nsGkAtoms::tableFrame == aParentFrame->GetType()) {
2382 return aParentFrame->GetParent();;
2384 return aParentFrame;
2388 * If the parent frame is a |tableFrame| and the child is a
2389 * |captionFrame|, then we want to insert the frames beneath the
2390 * |tableFrame|'s parent frame. Returns |PR_TRUE| if the parent frame
2391 * needed to be fixed up.
2393 static PRBool
2394 GetCaptionAdjustedParent(nsIFrame* aParentFrame,
2395 const nsIFrame* aChildFrame,
2396 nsIFrame** aAdjParentFrame)
2398 *aAdjParentFrame = aParentFrame;
2399 PRBool haveCaption = PR_FALSE;
2401 if (nsGkAtoms::tableCaptionFrame == aChildFrame->GetType()) {
2402 haveCaption = PR_TRUE;
2403 *aAdjParentFrame = AdjustCaptionParentFrame(aParentFrame);
2405 return haveCaption;
2408 static nsresult
2409 ProcessPseudoFrame(nsPseudoFrameData& aPseudoData,
2410 nsIFrame*& aParent)
2412 nsresult rv = NS_OK;
2414 aParent = aPseudoData.mFrame;
2415 nsFrameItems* items = &aPseudoData.mChildList;
2416 if (items && items->childList) {
2417 rv = aParent->SetInitialChildList(nsnull, items->childList);
2418 if (NS_FAILED(rv)) return rv;
2420 aPseudoData.Reset();
2421 return rv;
2424 static nsresult
2425 ProcessPseudoRowGroupFrame(nsPseudoFrameData& aPseudoData,
2426 nsIFrame*& aParent)
2428 nsresult rv = NS_OK;
2430 aParent = aPseudoData.mFrame;
2431 nsFrameItems* items = &aPseudoData.mChildList;
2432 if (items && items->childList) {
2433 nsTableRowGroupFrame* rgFrame = nsTableFrame::GetRowGroupFrame(aParent);
2434 rv = rgFrame->SetInitialChildList(nsnull, items->childList);
2435 if (NS_FAILED(rv)) return rv;
2437 aPseudoData.Reset();
2438 return rv;
2441 static nsresult
2442 ProcessPseudoTableFrame(nsPseudoFrames& aPseudoFrames,
2443 nsIFrame*& aParent)
2445 nsresult rv = NS_OK;
2447 // process the col group frame, if it exists
2448 if (aPseudoFrames.mColGroup.mFrame) {
2449 rv = ProcessPseudoFrame(aPseudoFrames.mColGroup, aParent);
2452 // process the inner table frame
2453 rv = ProcessPseudoFrame(aPseudoFrames.mTableInner, aParent);
2455 // process the outer table frame
2456 aParent = aPseudoFrames.mTableOuter.mFrame;
2457 nsFrameItems* items = &aPseudoFrames.mTableOuter.mChildList;
2458 if (items && items->childList) {
2459 rv = aParent->SetInitialChildList(nsnull, items->childList);
2460 if (NS_FAILED(rv)) return rv;
2462 nsFrameItems* captions = &aPseudoFrames.mTableOuter.mChildList2;
2463 if (captions && captions->childList) {
2464 rv = aParent->SetInitialChildList(nsGkAtoms::captionList, captions->childList);
2466 aPseudoFrames.mTableOuter.Reset();
2467 return rv;
2470 static nsresult
2471 ProcessPseudoCellFrame(nsPseudoFrames& aPseudoFrames,
2472 nsIFrame*& aParent)
2474 nsresult rv = NS_OK;
2476 rv = ProcessPseudoFrame(aPseudoFrames.mCellInner, aParent);
2477 if (NS_FAILED(rv)) return rv;
2478 rv = ProcessPseudoFrame(aPseudoFrames.mCellOuter, aParent);
2479 return rv;
2482 // limit the processing up to the frame type indicated by aHighestType.
2483 // make a complete processing when aHighestType is null
2484 static nsresult
2485 ProcessPseudoFrames(nsFrameConstructorState& aState,
2486 nsIAtom* aHighestType,
2487 nsIFrame*& aHighestFrame)
2489 nsresult rv = NS_OK;
2491 aHighestFrame = nsnull;
2493 #ifdef DEBUG
2494 if (gTablePseudoFrame) {
2495 printf("*** ProcessPseudoFrames enter***\n");
2496 aState.mPseudoFrames.Dump();
2498 #endif
2500 nsPseudoFrames& pseudoFrames = aState.mPseudoFrames;
2502 if (nsGkAtoms::tableFrame == pseudoFrames.mLowestType) {
2503 if (pseudoFrames.mColGroup.mFrame) {
2504 rv = ProcessPseudoFrame(pseudoFrames.mColGroup, aHighestFrame);
2505 if (nsGkAtoms::tableColGroupFrame == aHighestType) return rv;
2507 rv = ProcessPseudoTableFrame(pseudoFrames, aHighestFrame);
2508 if (nsGkAtoms::tableOuterFrame == aHighestType) return rv;
2510 if (pseudoFrames.mCellOuter.mFrame) {
2511 rv = ProcessPseudoCellFrame(pseudoFrames, aHighestFrame);
2512 if (IS_TABLE_CELL(aHighestType)) return rv;
2514 if (pseudoFrames.mRow.mFrame) {
2515 rv = ProcessPseudoFrame(pseudoFrames.mRow, aHighestFrame);
2516 if (nsGkAtoms::tableRowFrame == aHighestType) return rv;
2518 if (pseudoFrames.mRowGroup.mFrame) {
2519 rv = ProcessPseudoRowGroupFrame(pseudoFrames.mRowGroup, aHighestFrame);
2520 if (nsGkAtoms::tableRowGroupFrame == aHighestType) return rv;
2523 else if (nsGkAtoms::tableRowGroupFrame == pseudoFrames.mLowestType) {
2524 rv = ProcessPseudoRowGroupFrame(pseudoFrames.mRowGroup, aHighestFrame);
2525 if (nsGkAtoms::tableRowGroupFrame == aHighestType) return rv;
2526 if (pseudoFrames.mColGroup.mFrame) {
2527 nsIFrame* colGroupHigh;
2528 rv = ProcessPseudoFrame(pseudoFrames.mColGroup, colGroupHigh);
2529 if (aHighestFrame &&
2530 nsGkAtoms::tableRowGroupFrame == aHighestFrame->GetType() &&
2531 !pseudoFrames.mTableInner.mFrame) {
2532 // table frames are special they can have two types of pseudo frames as
2533 // children that need to be processed in one pass, we only need to link
2534 // them if the parent is not a pseudo where the link is already done
2535 // We sort this later out inside nsTableFrame.
2536 colGroupHigh->SetNextSibling(aHighestFrame);
2538 aHighestFrame = colGroupHigh;
2539 if (nsGkAtoms::tableColGroupFrame == aHighestType) return rv;
2541 if (pseudoFrames.mTableOuter.mFrame) {
2542 rv = ProcessPseudoTableFrame(pseudoFrames, aHighestFrame);
2543 if (nsGkAtoms::tableOuterFrame == aHighestType) return rv;
2545 if (pseudoFrames.mCellOuter.mFrame) {
2546 rv = ProcessPseudoCellFrame(pseudoFrames, aHighestFrame);
2547 if (IS_TABLE_CELL(aHighestType)) return rv;
2549 if (pseudoFrames.mRow.mFrame) {
2550 rv = ProcessPseudoFrame(pseudoFrames.mRow, aHighestFrame);
2551 if (nsGkAtoms::tableRowFrame == aHighestType) return rv;
2554 else if (nsGkAtoms::tableRowFrame == pseudoFrames.mLowestType) {
2555 rv = ProcessPseudoFrame(pseudoFrames.mRow, aHighestFrame);
2556 if (nsGkAtoms::tableRowFrame == aHighestType) return rv;
2558 if (pseudoFrames.mRowGroup.mFrame) {
2559 rv = ProcessPseudoRowGroupFrame(pseudoFrames.mRowGroup, aHighestFrame);
2560 if (nsGkAtoms::tableRowGroupFrame == aHighestType) return rv;
2562 if (pseudoFrames.mColGroup.mFrame) {
2563 nsIFrame* colGroupHigh;
2564 rv = ProcessPseudoFrame(pseudoFrames.mColGroup, colGroupHigh);
2565 if (aHighestFrame &&
2566 nsGkAtoms::tableRowGroupFrame == aHighestFrame->GetType() &&
2567 !pseudoFrames.mTableInner.mFrame) {
2568 // table frames are special they can have two types of pseudo frames as
2569 // children that need to be processed in one pass, we only need to link
2570 // them if the parent is not a pseudo where the link is already done
2571 // We sort this later out inside nsTableFrame.
2572 colGroupHigh->SetNextSibling(aHighestFrame);
2574 aHighestFrame = colGroupHigh;
2575 if (nsGkAtoms::tableColGroupFrame == aHighestType) return rv;
2577 if (pseudoFrames.mTableOuter.mFrame) {
2578 rv = ProcessPseudoTableFrame(pseudoFrames, aHighestFrame);
2579 if (nsGkAtoms::tableOuterFrame == aHighestType) return rv;
2581 if (pseudoFrames.mCellOuter.mFrame) {
2582 rv = ProcessPseudoCellFrame(pseudoFrames, aHighestFrame);
2583 if (IS_TABLE_CELL(aHighestType)) return rv;
2586 else if (IS_TABLE_CELL(pseudoFrames.mLowestType)) {
2587 rv = ProcessPseudoCellFrame(pseudoFrames, aHighestFrame);
2588 if (IS_TABLE_CELL(aHighestType)) return rv;
2590 if (pseudoFrames.mRow.mFrame) {
2591 rv = ProcessPseudoFrame(pseudoFrames.mRow, aHighestFrame);
2592 if (nsGkAtoms::tableRowFrame == aHighestType) return rv;
2594 if (pseudoFrames.mRowGroup.mFrame) {
2595 rv = ProcessPseudoRowGroupFrame(pseudoFrames.mRowGroup, aHighestFrame);
2596 if (nsGkAtoms::tableRowGroupFrame == aHighestType) return rv;
2598 if (pseudoFrames.mColGroup.mFrame) {
2599 nsIFrame* colGroupHigh;
2600 rv = ProcessPseudoFrame(pseudoFrames.mColGroup, colGroupHigh);
2601 if (aHighestFrame &&
2602 nsGkAtoms::tableRowGroupFrame == aHighestFrame->GetType() &&
2603 !pseudoFrames.mTableInner.mFrame) {
2604 // table frames are special they can have two types of pseudo frames as
2605 // children that need to be processed in one pass, we only need to link
2606 // them if the parent is not a pseudo where the link is already done
2607 // We sort this later out inside nsTableFrame.
2608 colGroupHigh->SetNextSibling(aHighestFrame);
2610 aHighestFrame = colGroupHigh;
2611 if (nsGkAtoms::tableColGroupFrame == aHighestType) return rv;
2613 if (pseudoFrames.mTableOuter.mFrame) {
2614 rv = ProcessPseudoTableFrame(pseudoFrames, aHighestFrame);
2617 else if (pseudoFrames.mColGroup.mFrame) {
2618 // process the col group frame
2619 rv = ProcessPseudoFrame(pseudoFrames.mColGroup, aHighestFrame);
2622 return rv;
2625 static nsresult
2626 ProcessPseudoFrames(nsFrameConstructorState& aState,
2627 nsFrameItems& aItems)
2630 #ifdef DEBUG
2631 if (gTablePseudoFrame) {
2632 printf("*** ProcessPseudoFrames complete enter***\n");
2633 aState.mPseudoFrames.Dump();
2635 #endif
2637 nsIFrame* highestFrame;
2638 nsresult rv = ProcessPseudoFrames(aState, nsnull, highestFrame);
2639 if (highestFrame) {
2640 aItems.AddChild(highestFrame);
2643 #ifdef DEBUG
2644 if (gTablePseudoFrame) {
2645 printf("*** ProcessPseudoFrames complete leave, highestframe:%p***\n",
2646 static_cast<void*>(highestFrame));
2647 aState.mPseudoFrames.Dump();
2649 #endif
2650 aState.mPseudoFrames.Reset();
2651 return rv;
2654 static nsresult
2655 ProcessPseudoFrames(nsFrameConstructorState& aState,
2656 nsIAtom* aHighestType)
2658 #ifdef DEBUG
2659 if (gTablePseudoFrame) {
2660 printf("*** ProcessPseudoFrames limited enter highest:");
2661 if (nsGkAtoms::tableOuterFrame == aHighestType)
2662 printf("OuterTable");
2663 else if (nsGkAtoms::tableFrame == aHighestType)
2664 printf("InnerTable");
2665 else if (nsGkAtoms::tableColGroupFrame == aHighestType)
2666 printf("ColGroup");
2667 else if (nsGkAtoms::tableRowGroupFrame == aHighestType)
2668 printf("RowGroup");
2669 else if (nsGkAtoms::tableRowFrame == aHighestType)
2670 printf("Row");
2671 else if (IS_TABLE_CELL(aHighestType))
2672 printf("Cell");
2673 else
2674 NS_ASSERTION(PR_FALSE, "invalid call to ProcessPseudoFrames ");
2675 printf("***\n");
2676 aState.mPseudoFrames.Dump();
2678 #endif
2680 nsIFrame* highestFrame;
2681 nsresult rv = ProcessPseudoFrames(aState, aHighestType, highestFrame);
2683 #ifdef DEBUG
2684 if (gTablePseudoFrame) {
2685 printf("*** ProcessPseudoFrames limited leave:%p***\n",
2686 static_cast<void*>(highestFrame));
2687 aState.mPseudoFrames.Dump();
2689 #endif
2690 return rv;
2693 nsresult
2694 nsCSSFrameConstructor::CreatePseudoTableFrame(PRInt32 aNameSpaceID,
2695 nsFrameConstructorState& aState,
2696 nsIFrame* aParentFrameIn)
2698 nsresult rv = NS_OK;
2700 nsIFrame* parentFrame = (aState.mPseudoFrames.mCellInner.mFrame)
2701 ? aState.mPseudoFrames.mCellInner.mFrame : aParentFrameIn;
2702 if (!parentFrame) return rv;
2704 nsStyleContext *parentStyle;
2705 nsRefPtr<nsStyleContext> childStyle;
2707 parentStyle = parentFrame->GetStyleContext();
2708 nsIContent* parentContent = parentFrame->GetContent();
2710 // Thankfully, the parent can't change display type without causing
2711 // frame reconstruction, so this won't need to change.
2712 nsIAtom *pseudoType;
2713 if (parentStyle->GetStyleDisplay()->mDisplay == NS_STYLE_DISPLAY_INLINE)
2714 pseudoType = nsCSSAnonBoxes::inlineTable;
2715 else
2716 pseudoType = nsCSSAnonBoxes::table;
2718 // create the SC for the inner table which will be the parent of the outer table's SC
2719 childStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(parentContent,
2720 pseudoType,
2721 parentStyle);
2723 nsPseudoFrameData& pseudoOuter = aState.mPseudoFrames.mTableOuter;
2724 nsPseudoFrameData& pseudoInner = aState.mPseudoFrames.mTableInner;
2726 // construct the pseudo outer and inner as part of the pseudo frames
2727 nsFrameItems items;
2728 rv = ConstructTableFrame(aState, parentContent,
2729 parentFrame, childStyle, aNameSpaceID,
2730 PR_TRUE, items, pseudoOuter.mFrame,
2731 pseudoInner.mFrame);
2733 if (NS_FAILED(rv)) return rv;
2735 // set pseudo data for the newly created frames
2736 pseudoOuter.mChildList.AddChild(pseudoInner.mFrame);
2737 aState.mPseudoFrames.mLowestType = nsGkAtoms::tableFrame;
2739 // set pseudo data for the parent
2740 if (aState.mPseudoFrames.mCellInner.mFrame) {
2741 aState.mPseudoFrames.mCellInner.mChildList.AddChild(pseudoOuter.mFrame);
2743 #ifdef DEBUG
2744 if (gTablePseudoFrame) {
2745 printf("*** CreatePseudoTableFrame ***\n");
2746 aState.mPseudoFrames.Dump();
2748 #endif
2749 return rv;
2752 nsresult
2753 nsCSSFrameConstructor::CreatePseudoRowGroupFrame(PRInt32 aNameSpaceID,
2754 nsFrameConstructorState& aState,
2755 nsIFrame* aParentFrameIn)
2757 nsresult rv = NS_OK;
2759 nsIFrame* parentFrame = (aState.mPseudoFrames.mTableInner.mFrame)
2760 ? aState.mPseudoFrames.mTableInner.mFrame : aParentFrameIn;
2761 if (!parentFrame) return rv;
2763 nsStyleContext *parentStyle;
2764 nsRefPtr<nsStyleContext> childStyle;
2766 parentStyle = parentFrame->GetStyleContext();
2767 nsIContent* parentContent = parentFrame->GetContent();
2769 childStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(parentContent,
2770 nsCSSAnonBoxes::tableRowGroup,
2771 parentStyle);
2773 nsPseudoFrameData& pseudo = aState.mPseudoFrames.mRowGroup;
2775 // construct the pseudo row group as part of the pseudo frames
2776 PRBool pseudoParent;
2777 nsFrameItems items;
2778 rv = ConstructTableRowGroupFrame(aState, parentContent,
2779 parentFrame, childStyle, aNameSpaceID,
2780 PR_TRUE, items, pseudo.mFrame, pseudoParent);
2781 if (NS_FAILED(rv)) return rv;
2783 // set pseudo data for the newly created frames
2784 aState.mPseudoFrames.mLowestType = nsGkAtoms::tableRowGroupFrame;
2786 // set pseudo data for the parent
2787 if (aState.mPseudoFrames.mTableInner.mFrame) {
2788 aState.mPseudoFrames.mTableInner.mChildList.AddChild(pseudo.mFrame);
2790 #ifdef DEBUG
2791 if (gTablePseudoFrame) {
2792 printf("*** CreatePseudoRowGroupFrame ***\n");
2793 aState.mPseudoFrames.Dump();
2795 #endif
2796 return rv;
2799 nsresult
2800 nsCSSFrameConstructor::CreatePseudoColGroupFrame(PRInt32 aNameSpaceID,
2801 nsFrameConstructorState& aState,
2802 nsIFrame* aParentFrameIn)
2804 nsresult rv = NS_OK;
2806 nsIFrame* parentFrame = (aState.mPseudoFrames.mTableInner.mFrame)
2807 ? aState.mPseudoFrames.mTableInner.mFrame : aParentFrameIn;
2808 if (!parentFrame) return rv;
2810 nsStyleContext *parentStyle;
2811 nsRefPtr<nsStyleContext> childStyle;
2813 parentStyle = parentFrame->GetStyleContext();
2814 nsIContent* parentContent = parentFrame->GetContent();
2816 childStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(parentContent,
2817 nsCSSAnonBoxes::tableColGroup,
2818 parentStyle);
2820 nsPseudoFrameData& pseudo = aState.mPseudoFrames.mColGroup;
2822 // construct the pseudo col group as part of the pseudo frames
2823 PRBool pseudoParent;
2824 nsFrameItems items;
2825 rv = ConstructTableColGroupFrame(aState, parentContent,
2826 parentFrame, childStyle, aNameSpaceID,
2827 PR_TRUE, items, pseudo.mFrame, pseudoParent);
2828 if (NS_FAILED(rv)) return rv;
2829 ((nsTableColGroupFrame*)pseudo.mFrame)->SetColType(eColGroupAnonymousCol);
2831 // Do not set aState.mPseudoFrames.mLowestType here as colgroup frame will
2832 // be always below a table frame but we can not descent any further as col
2833 // frames can not have children and will not wrap table foreign frames.
2835 // set pseudo data for the parent
2836 if (aState.mPseudoFrames.mTableInner.mFrame) {
2837 aState.mPseudoFrames.mTableInner.mChildList.AddChild(pseudo.mFrame);
2839 #ifdef DEBUG
2840 if (gTablePseudoFrame) {
2841 printf("*** CreatePseudoColGroupFrame ***\n");
2842 aState.mPseudoFrames.Dump();
2844 #endif
2845 return rv;
2848 nsresult
2849 nsCSSFrameConstructor::CreatePseudoRowFrame(PRInt32 aNameSpaceID,
2850 nsFrameConstructorState& aState,
2851 nsIFrame* aParentFrameIn)
2853 nsresult rv = NS_OK;
2855 nsIFrame* parentFrame = aParentFrameIn;
2856 if (aState.mPseudoFrames.mRowGroup.mFrame) {
2857 parentFrame = (nsIFrame*) nsTableFrame::GetRowGroupFrame(aState.mPseudoFrames.mRowGroup.mFrame);
2859 if (!parentFrame) return rv;
2861 nsStyleContext *parentStyle;
2862 nsRefPtr<nsStyleContext> childStyle;
2864 parentStyle = parentFrame->GetStyleContext();
2865 nsIContent* parentContent = parentFrame->GetContent();
2867 childStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(parentContent,
2868 nsCSSAnonBoxes::tableRow,
2869 parentStyle);
2871 nsPseudoFrameData& pseudo = aState.mPseudoFrames.mRow;
2873 // construct the pseudo row as part of the pseudo frames
2874 PRBool pseudoParent;
2875 nsFrameItems items;
2876 rv = ConstructTableRowFrame(aState, parentContent,
2877 parentFrame, childStyle, aNameSpaceID,
2878 PR_TRUE, items, pseudo.mFrame, pseudoParent);
2879 if (NS_FAILED(rv)) return rv;
2881 aState.mPseudoFrames.mLowestType = nsGkAtoms::tableRowFrame;
2883 // set pseudo data for the parent
2884 if (aState.mPseudoFrames.mRowGroup.mFrame) {
2885 aState.mPseudoFrames.mRowGroup.mChildList.AddChild(pseudo.mFrame);
2887 #ifdef DEBUG
2888 if (gTablePseudoFrame) {
2889 printf("*** CreatePseudoRowFrame ***\n");
2890 aState.mPseudoFrames.Dump();
2892 #endif
2893 return rv;
2896 nsresult
2897 nsCSSFrameConstructor::CreatePseudoCellFrame(PRInt32 aNameSpaceID,
2898 nsFrameConstructorState& aState,
2899 nsIFrame* aParentFrameIn)
2901 nsresult rv = NS_OK;
2903 nsIFrame* parentFrame = (aState.mPseudoFrames.mRow.mFrame)
2904 ? aState.mPseudoFrames.mRow.mFrame : aParentFrameIn;
2905 if (!parentFrame) return rv;
2907 nsStyleContext *parentStyle;
2908 nsRefPtr<nsStyleContext> childStyle;
2910 parentStyle = parentFrame->GetStyleContext();
2911 nsIContent* parentContent = parentFrame->GetContent();
2913 childStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(parentContent,
2914 nsCSSAnonBoxes::tableCell,
2915 parentStyle);
2917 nsPseudoFrameData& pseudoOuter = aState.mPseudoFrames.mCellOuter;
2918 nsPseudoFrameData& pseudoInner = aState.mPseudoFrames.mCellInner;
2920 // construct the pseudo outer and inner as part of the pseudo frames
2921 PRBool pseudoParent;
2922 nsFrameItems items;
2923 rv = ConstructTableCellFrame(aState, parentContent, parentFrame, childStyle,
2924 aNameSpaceID, PR_TRUE, items,
2925 pseudoOuter.mFrame, pseudoInner.mFrame,
2926 pseudoParent);
2927 if (NS_FAILED(rv)) return rv;
2929 // set pseudo data for the newly created frames
2930 pseudoOuter.mChildList.AddChild(pseudoInner.mFrame);
2931 // give it nsGkAtoms::tableCellFrame, if it is really nsGkAtoms::bcTableCellFrame, it will match later
2932 aState.mPseudoFrames.mLowestType = nsGkAtoms::tableCellFrame;
2934 // set pseudo data for the parent
2935 if (aState.mPseudoFrames.mRow.mFrame) {
2936 aState.mPseudoFrames.mRow.mChildList.AddChild(pseudoOuter.mFrame);
2938 #ifdef DEBUG
2939 if (gTablePseudoFrame) {
2940 printf("*** CreatePseudoCellFrame ***\n");
2941 aState.mPseudoFrames.Dump();
2943 #endif
2944 return rv;
2947 // called if the parent is not a table
2948 nsresult
2949 nsCSSFrameConstructor::GetPseudoTableFrame(PRInt32 aNameSpaceID,
2950 nsFrameConstructorState& aState,
2951 nsIFrame& aParentFrameIn)
2953 nsresult rv = NS_OK;
2955 nsPseudoFrames& pseudoFrames = aState.mPseudoFrames;
2956 nsIAtom* parentFrameType = aParentFrameIn.GetType();
2958 if (pseudoFrames.IsEmpty()) {
2959 PRBool created = PR_FALSE;
2960 if (nsGkAtoms::tableRowGroupFrame == parentFrameType) { // row group parent
2961 rv = CreatePseudoRowFrame(aNameSpaceID, aState, &aParentFrameIn);
2962 if (NS_FAILED(rv)) return rv;
2963 created = PR_TRUE;
2965 if (created || (nsGkAtoms::tableRowFrame == parentFrameType)) { // row parent
2966 rv = CreatePseudoCellFrame(aNameSpaceID, aState, &aParentFrameIn);
2967 if (NS_FAILED(rv)) return rv;
2969 rv = CreatePseudoTableFrame(aNameSpaceID, aState, &aParentFrameIn);
2971 else {
2972 if (!pseudoFrames.mTableInner.mFrame) {
2973 if (pseudoFrames.mRowGroup.mFrame && !(pseudoFrames.mRow.mFrame)) {
2974 rv = CreatePseudoRowFrame(aNameSpaceID, aState);
2975 if (NS_FAILED(rv)) return rv;
2977 if (pseudoFrames.mRow.mFrame && !(pseudoFrames.mCellOuter.mFrame)) {
2978 rv = CreatePseudoCellFrame(aNameSpaceID, aState);
2979 if (NS_FAILED(rv)) return rv;
2981 CreatePseudoTableFrame(aNameSpaceID, aState);
2984 return rv;
2987 // called if the parent is not a col group
2988 nsresult
2989 nsCSSFrameConstructor::GetPseudoColGroupFrame(PRInt32 aNameSpaceID,
2990 nsFrameConstructorState& aState,
2991 nsIFrame& aParentFrameIn)
2993 nsresult rv = NS_OK;
2995 nsPseudoFrames& pseudoFrames = aState.mPseudoFrames;
2996 nsIAtom* parentFrameType = aParentFrameIn.GetType();
2998 if (pseudoFrames.IsEmpty()) {
2999 PRBool created = PR_FALSE;
3000 if (nsGkAtoms::tableRowGroupFrame == parentFrameType) { // row group parent
3001 rv = CreatePseudoRowFrame(aNameSpaceID, aState, &aParentFrameIn);
3002 created = PR_TRUE;
3004 if (created || (nsGkAtoms::tableRowFrame == parentFrameType)) { // row parent
3005 rv = CreatePseudoCellFrame(aNameSpaceID, aState, &aParentFrameIn);
3006 created = PR_TRUE;
3008 if (created || IS_TABLE_CELL(parentFrameType) || // cell parent
3009 (nsGkAtoms::tableCaptionFrame == parentFrameType) || // caption parent
3010 !IsTableRelated(parentFrameType, PR_TRUE)) { // block parent
3011 rv = CreatePseudoTableFrame(aNameSpaceID, aState, &aParentFrameIn);
3013 rv = CreatePseudoColGroupFrame(aNameSpaceID, aState, &aParentFrameIn);
3015 else {
3016 if (!pseudoFrames.mColGroup.mFrame) {
3017 if (!pseudoFrames.mTableInner.mFrame) {
3018 if (pseudoFrames.mRowGroup.mFrame && !(pseudoFrames.mRow.mFrame)) {
3019 rv = CreatePseudoRowFrame(aNameSpaceID, aState);
3021 if (pseudoFrames.mRow.mFrame && !(pseudoFrames.mCellOuter.mFrame)) {
3022 rv = CreatePseudoCellFrame(aNameSpaceID, aState);
3024 if (pseudoFrames.mCellOuter.mFrame && !(pseudoFrames.mTableOuter.mFrame)) {
3025 rv = CreatePseudoTableFrame(aNameSpaceID, aState);
3028 rv = CreatePseudoColGroupFrame(aNameSpaceID, aState);
3031 return rv;
3034 // called if the parent is not a row group
3035 nsresult
3036 nsCSSFrameConstructor::GetPseudoRowGroupFrame(PRInt32 aNameSpaceID,
3037 nsFrameConstructorState& aState,
3038 nsIFrame& aParentFrameIn)
3040 nsresult rv = NS_OK;
3042 nsPseudoFrames& pseudoFrames = aState.mPseudoFrames;
3043 nsIAtom* parentFrameType = aParentFrameIn.GetType();
3045 if (!pseudoFrames.mLowestType) {
3046 PRBool created = PR_FALSE;
3047 if (nsGkAtoms::tableRowFrame == parentFrameType) { // row parent
3048 rv = CreatePseudoCellFrame(aNameSpaceID, aState, &aParentFrameIn);
3049 created = PR_TRUE;
3051 if (created || IS_TABLE_CELL(parentFrameType) || // cell parent
3052 (nsGkAtoms::tableCaptionFrame == parentFrameType) || // caption parent
3053 !IsTableRelated(parentFrameType, PR_TRUE)) { // block parent
3054 rv = CreatePseudoTableFrame(aNameSpaceID, aState, &aParentFrameIn);
3056 rv = CreatePseudoRowGroupFrame(aNameSpaceID, aState, &aParentFrameIn);
3058 else {
3059 if (!pseudoFrames.mRowGroup.mFrame) {
3060 if (pseudoFrames.mRow.mFrame && !(pseudoFrames.mCellOuter.mFrame)) {
3061 rv = CreatePseudoCellFrame(aNameSpaceID, aState);
3063 if (pseudoFrames.mCellOuter.mFrame && !(pseudoFrames.mTableOuter.mFrame)) {
3064 rv = CreatePseudoTableFrame(aNameSpaceID, aState);
3066 rv = CreatePseudoRowGroupFrame(aNameSpaceID, aState);
3069 return rv;
3072 // called if the parent is not a row
3073 nsresult
3074 nsCSSFrameConstructor::GetPseudoRowFrame(PRInt32 aNameSpaceID,
3075 nsFrameConstructorState& aState,
3076 nsIFrame& aParentFrameIn)
3078 nsresult rv = NS_OK;
3080 nsPseudoFrames& pseudoFrames = aState.mPseudoFrames;
3081 nsIAtom* parentFrameType = aParentFrameIn.GetType();
3083 if (!pseudoFrames.mLowestType) {
3084 PRBool created = PR_FALSE;
3085 if (IS_TABLE_CELL(parentFrameType) || // cell parent
3086 (nsGkAtoms::tableCaptionFrame == parentFrameType) || // caption parent
3087 !IsTableRelated(parentFrameType, PR_TRUE)) { // block parent
3088 rv = CreatePseudoTableFrame(aNameSpaceID, aState, &aParentFrameIn);
3089 created = PR_TRUE;
3091 if (created || (nsGkAtoms::tableFrame == parentFrameType)) { // table parent
3092 rv = CreatePseudoRowGroupFrame(aNameSpaceID, aState, &aParentFrameIn);
3094 rv = CreatePseudoRowFrame(aNameSpaceID, aState, &aParentFrameIn);
3096 else {
3097 if (!pseudoFrames.mRow.mFrame) {
3098 if (pseudoFrames.mCellOuter.mFrame && !pseudoFrames.mTableOuter.mFrame) {
3099 rv = CreatePseudoTableFrame(aNameSpaceID, aState);
3101 if (pseudoFrames.mTableInner.mFrame && !(pseudoFrames.mRowGroup.mFrame)) {
3102 rv = CreatePseudoRowGroupFrame(aNameSpaceID, aState);
3104 rv = CreatePseudoRowFrame(aNameSpaceID, aState);
3107 return rv;
3110 // called if the parent is not a cell or block
3111 nsresult
3112 nsCSSFrameConstructor::GetPseudoCellFrame(PRInt32 aNameSpaceID,
3113 nsFrameConstructorState& aState,
3114 nsIFrame& aParentFrameIn)
3116 nsresult rv = NS_OK;
3118 nsPseudoFrames& pseudoFrames = aState.mPseudoFrames;
3119 nsIAtom* parentFrameType = aParentFrameIn.GetType();
3121 if (!pseudoFrames.mLowestType) {
3122 PRBool created = PR_FALSE;
3123 if (nsGkAtoms::tableFrame == parentFrameType) { // table parent
3124 rv = CreatePseudoRowGroupFrame(aNameSpaceID, aState, &aParentFrameIn);
3125 created = PR_TRUE;
3127 if (created || (nsGkAtoms::tableRowGroupFrame == parentFrameType)) { // row group parent
3128 rv = CreatePseudoRowFrame(aNameSpaceID, aState, &aParentFrameIn);
3129 created = PR_TRUE;
3131 rv = CreatePseudoCellFrame(aNameSpaceID, aState, &aParentFrameIn);
3133 else if (!pseudoFrames.mCellOuter.mFrame) {
3134 if (pseudoFrames.mTableInner.mFrame && !(pseudoFrames.mRowGroup.mFrame)) {
3135 rv = CreatePseudoRowGroupFrame(aNameSpaceID, aState);
3137 if (pseudoFrames.mRowGroup.mFrame && !(pseudoFrames.mRow.mFrame)) {
3138 rv = CreatePseudoRowFrame(aNameSpaceID, aState);
3140 rv = CreatePseudoCellFrame(aNameSpaceID, aState);
3142 return rv;
3145 nsresult
3146 nsCSSFrameConstructor::GetParentFrame(PRInt32 aNameSpaceID,
3147 nsIFrame& aParentFrameIn,
3148 nsIAtom* aChildFrameType,
3149 nsFrameConstructorState& aState,
3150 nsIFrame*& aParentFrame,
3151 PRBool& aIsPseudoParent)
3153 nsresult rv = NS_OK;
3155 nsIAtom* parentFrameType = aParentFrameIn.GetType();
3156 nsIFrame* pseudoParentFrame = nsnull;
3157 nsPseudoFrames& pseudoFrames = aState.mPseudoFrames;
3158 aParentFrame = &aParentFrameIn;
3159 aIsPseudoParent = PR_FALSE;
3161 if (nsGkAtoms::tableOuterFrame == aChildFrameType) { // table child
3162 if (IsTableRelated(parentFrameType, PR_TRUE) &&
3163 (nsGkAtoms::tableCaptionFrame != parentFrameType) ) { // need pseudo cell parent
3164 rv = GetPseudoCellFrame(aNameSpaceID, aState, aParentFrameIn);
3165 if (NS_FAILED(rv)) return rv;
3166 pseudoParentFrame = pseudoFrames.mCellInner.mFrame;
3169 else if (nsGkAtoms::tableCaptionFrame == aChildFrameType) { // caption child
3170 if (nsGkAtoms::tableOuterFrame != parentFrameType) { // need pseudo table parent
3171 rv = GetPseudoTableFrame(aNameSpaceID, aState, aParentFrameIn);
3172 if (NS_FAILED(rv)) return rv;
3173 pseudoParentFrame = pseudoFrames.mTableOuter.mFrame;
3176 else if (nsGkAtoms::tableColGroupFrame == aChildFrameType) { // col group child
3177 if (nsGkAtoms::tableFrame != parentFrameType) { // need pseudo table parent
3178 rv = GetPseudoTableFrame(aNameSpaceID, aState, aParentFrameIn);
3179 if (NS_FAILED(rv)) return rv;
3180 pseudoParentFrame = pseudoFrames.mTableInner.mFrame;
3183 else if (nsGkAtoms::tableColFrame == aChildFrameType) { // col child
3184 if (nsGkAtoms::tableColGroupFrame != parentFrameType) { // need pseudo col group parent
3185 rv = GetPseudoColGroupFrame(aNameSpaceID, aState, aParentFrameIn);
3186 if (NS_FAILED(rv)) return rv;
3187 pseudoParentFrame = pseudoFrames.mColGroup.mFrame;
3190 else if (nsGkAtoms::tableRowGroupFrame == aChildFrameType) { // row group child
3191 // XXX can this go away?
3192 if (nsGkAtoms::tableFrame != parentFrameType) {
3193 // trees allow row groups to contain row groups, so don't create pseudo frames
3194 rv = GetPseudoTableFrame(aNameSpaceID, aState, aParentFrameIn);
3195 if (NS_FAILED(rv)) return rv;
3196 pseudoParentFrame = pseudoFrames.mTableInner.mFrame;
3199 else if (nsGkAtoms::tableRowFrame == aChildFrameType) { // row child
3200 if (nsGkAtoms::tableRowGroupFrame != parentFrameType) { // need pseudo row group parent
3201 rv = GetPseudoRowGroupFrame(aNameSpaceID, aState, aParentFrameIn);
3202 if (NS_FAILED(rv)) return rv;
3203 pseudoParentFrame = pseudoFrames.mRowGroup.mFrame;
3206 else if (IS_TABLE_CELL(aChildFrameType)) { // cell child
3207 if (nsGkAtoms::tableRowFrame != parentFrameType) { // need pseudo row parent
3208 rv = GetPseudoRowFrame(aNameSpaceID, aState, aParentFrameIn);
3209 if (NS_FAILED(rv)) return rv;
3210 pseudoParentFrame = pseudoFrames.mRow.mFrame;
3213 else if (nsGkAtoms::tableFrame == aChildFrameType) { // invalid
3214 NS_ASSERTION(PR_FALSE, "GetParentFrame called on nsGkAtoms::tableFrame child");
3216 else { // foreign frame
3217 if (IsTableRelated(parentFrameType, PR_FALSE)) { // need pseudo cell parent
3218 rv = GetPseudoCellFrame(aNameSpaceID, aState, aParentFrameIn);
3219 if (NS_FAILED(rv)) return rv;
3220 pseudoParentFrame = pseudoFrames.mCellInner.mFrame;
3224 if (pseudoParentFrame) {
3225 aParentFrame = pseudoParentFrame;
3226 aIsPseudoParent = PR_TRUE;
3229 return rv;
3232 static PRBool
3233 IsSpecialContent(nsIContent* aContent,
3234 nsIAtom* aTag,
3235 PRInt32 aNameSpaceID,
3236 nsStyleContext* aStyleContext)
3238 // Gross hack. Return true if this is a content node that we'd create a
3239 // frame for based on something other than display -- in other words if this
3240 // is a node that could never have a nsTableCellFrame, for example.
3241 if (aContent->IsNodeOfType(nsINode::eHTML) ||
3242 aNameSpaceID == kNameSpaceID_XHTML) {
3243 // XXXbz this is duplicating some logic from ConstructHTMLFrame....
3244 // Would be nice to avoid that. :(
3246 if (aTag == nsGkAtoms::input) {
3247 nsCOMPtr<nsIFormControl> control = do_QueryInterface(aContent);
3248 if (control) {
3249 PRInt32 type = control->GetType();
3250 if (NS_FORM_INPUT_HIDDEN == type) {
3251 return PR_FALSE; // input hidden does not create a special frame
3253 else if (NS_FORM_INPUT_IMAGE == type) {
3254 return nsImageFrame::ShouldCreateImageFrameFor(aContent, aStyleContext);
3258 return PR_TRUE;
3261 if (aTag == nsGkAtoms::img ||
3262 aTag == nsGkAtoms::mozgeneratedcontentimage) {
3263 return nsImageFrame::ShouldCreateImageFrameFor(aContent, aStyleContext);
3266 if (aTag == nsGkAtoms::object ||
3267 aTag == nsGkAtoms::applet ||
3268 aTag == nsGkAtoms::embed) {
3269 return !(aContent->IntrinsicState() &
3270 (NS_EVENT_STATE_BROKEN | NS_EVENT_STATE_USERDISABLED |
3271 NS_EVENT_STATE_SUPPRESSED));
3274 return
3275 aTag == nsGkAtoms::br ||
3276 aTag == nsGkAtoms::wbr ||
3277 aTag == nsGkAtoms::textarea ||
3278 aTag == nsGkAtoms::select ||
3279 aTag == nsGkAtoms::fieldset ||
3280 aTag == nsGkAtoms::legend ||
3281 aTag == nsGkAtoms::frameset ||
3282 aTag == nsGkAtoms::iframe ||
3283 aTag == nsGkAtoms::spacer ||
3284 aTag == nsGkAtoms::button ||
3285 aTag == nsGkAtoms::isindex ||
3286 aTag == nsGkAtoms::canvas;
3290 if (aNameSpaceID == kNameSpaceID_XUL)
3291 return
3292 #ifdef MOZ_XUL
3293 aTag == nsGkAtoms::button ||
3294 aTag == nsGkAtoms::checkbox ||
3295 aTag == nsGkAtoms::radio ||
3296 aTag == nsGkAtoms::autorepeatbutton ||
3297 aTag == nsGkAtoms::titlebar ||
3298 aTag == nsGkAtoms::resizer ||
3299 aTag == nsGkAtoms::image ||
3300 aTag == nsGkAtoms::spring ||
3301 aTag == nsGkAtoms::spacer ||
3302 aTag == nsGkAtoms::treechildren ||
3303 aTag == nsGkAtoms::treecol ||
3304 aTag == nsGkAtoms::text ||
3305 aTag == nsGkAtoms::description ||
3306 aTag == nsGkAtoms::label ||
3307 aTag == nsGkAtoms::menu ||
3308 aTag == nsGkAtoms::menuitem ||
3309 aTag == nsGkAtoms::menubutton ||
3310 aTag == nsGkAtoms::menubar ||
3311 (aTag == nsGkAtoms::popupgroup &&
3312 aContent->IsRootOfNativeAnonymousSubtree()) ||
3313 aTag == nsGkAtoms::iframe ||
3314 aTag == nsGkAtoms::editor ||
3315 aTag == nsGkAtoms::browser ||
3316 aTag == nsGkAtoms::progressmeter ||
3317 #endif
3318 aTag == nsGkAtoms::slider ||
3319 aTag == nsGkAtoms::scrollbar ||
3320 aTag == nsGkAtoms::scrollbarbutton ||
3321 #ifdef MOZ_XUL
3322 aTag == nsGkAtoms::splitter ||
3323 #endif
3324 PR_FALSE;
3326 #ifdef MOZ_SVG
3327 if (aNameSpaceID == kNameSpaceID_SVG && NS_SVGEnabled()) {
3328 // All SVG content is special...
3329 return PR_TRUE;
3331 #endif
3333 #ifdef MOZ_MATHML
3334 if (aNameSpaceID == kNameSpaceID_MathML)
3335 return
3336 aTag == nsGkAtoms::mi_ ||
3337 aTag == nsGkAtoms::mn_ ||
3338 aTag == nsGkAtoms::ms_ ||
3339 aTag == nsGkAtoms::mtext_ ||
3340 aTag == nsGkAtoms::mo_ ||
3341 aTag == nsGkAtoms::mfrac_ ||
3342 aTag == nsGkAtoms::msup_ ||
3343 aTag == nsGkAtoms::msub_ ||
3344 aTag == nsGkAtoms::msubsup_ ||
3345 aTag == nsGkAtoms::munder_ ||
3346 aTag == nsGkAtoms::mover_ ||
3347 aTag == nsGkAtoms::munderover_ ||
3348 aTag == nsGkAtoms::mphantom_ ||
3349 aTag == nsGkAtoms::mpadded_ ||
3350 aTag == nsGkAtoms::mspace_ ||
3351 aTag == nsGkAtoms::mfenced_ ||
3352 aTag == nsGkAtoms::mmultiscripts_ ||
3353 aTag == nsGkAtoms::mstyle_ ||
3354 aTag == nsGkAtoms::msqrt_ ||
3355 aTag == nsGkAtoms::mroot_ ||
3356 aTag == nsGkAtoms::maction_ ||
3357 aTag == nsGkAtoms::mrow_ ||
3358 aTag == nsGkAtoms::merror_ ||
3359 aTag == nsGkAtoms::none ||
3360 aTag == nsGkAtoms::mprescripts_ ||
3361 aTag == nsGkAtoms::math;
3362 #endif
3363 return PR_FALSE;
3366 nsresult
3367 nsCSSFrameConstructor::AdjustParentFrame(nsFrameConstructorState& aState,
3368 nsIContent* aChildContent,
3369 nsIFrame* & aParentFrame,
3370 nsIAtom* aTag,
3371 PRInt32 aNameSpaceID,
3372 nsStyleContext* aChildStyle,
3373 nsFrameItems* & aFrameItems,
3374 nsFrameConstructorSaveState& aSaveState,
3375 PRBool& aSuppressFrame,
3376 PRBool& aCreatedPseudo)
3378 NS_PRECONDITION(aChildStyle, "Must have child's style context");
3379 NS_PRECONDITION(aFrameItems, "Must have frame items to work with");
3381 aSuppressFrame = PR_FALSE;
3382 aCreatedPseudo = PR_FALSE;
3383 if (!aParentFrame) {
3384 // Nothing to do here
3385 return NS_OK;
3388 PRBool childIsSpecialContent = PR_FALSE; // lazy lookup
3389 // Only use the outer table frame as parent if the child is going to use a
3390 // tableCaptionFrame, otherwise the inner table frame is the parent
3391 // (bug 341858).
3392 nsIAtom* parentType = aParentFrame->GetType();
3393 NS_ASSERTION(parentType != nsGkAtoms::tableOuterFrame,
3394 "Shouldn't be happening");
3395 if (parentType == nsGkAtoms::tableColGroupFrame) {
3396 childIsSpecialContent = IsSpecialContent(aChildContent, aTag, aNameSpaceID,
3397 aChildStyle);
3398 if (childIsSpecialContent ||
3399 (aChildStyle->GetStyleDisplay()->mDisplay !=
3400 NS_STYLE_DISPLAY_TABLE_COLUMN)) {
3401 aSuppressFrame = PR_TRUE;
3402 return NS_OK;
3406 // If our parent is a table, table-row-group, or table-row, and
3407 // we're not table-related in any way, we have to create table
3408 // pseudo-frames so that we have a table cell to live in.
3409 if (IsTableRelated(aParentFrame->GetType(), PR_FALSE) &&
3410 (!IsTableRelated(aChildStyle->GetStyleDisplay()->mDisplay, PR_TRUE) ||
3411 // Also need to create a pseudo-parent if the child is going to end up
3412 // with a frame based on something other than display.
3413 childIsSpecialContent || // looked it up before
3414 IsSpecialContent(aChildContent, aTag, aNameSpaceID, aChildStyle))) {
3415 nsresult rv = GetPseudoCellFrame(aNameSpaceID, aState, *aParentFrame);
3416 if (NS_FAILED(rv)) {
3417 return rv;
3420 NS_ASSERTION(aState.mPseudoFrames.mCellInner.mFrame,
3421 "Must have inner cell frame now!");
3423 aParentFrame = aState.mPseudoFrames.mCellInner.mFrame;
3424 aFrameItems = &aState.mPseudoFrames.mCellInner.mChildList;
3425 // We pushed an anonymous table cell. The inner block of this
3426 // needs to become the float containing block.
3427 aState.PushFloatContainingBlock(aParentFrame, aSaveState, PR_FALSE,
3428 PR_FALSE);
3429 aCreatedPseudo = PR_TRUE;
3431 return NS_OK;
3434 // Pull all the captions present in aItems out into aCaptions
3435 static void
3436 PullOutCaptionFrames(nsFrameItems& aItems, nsFrameItems& aCaptions)
3438 nsIFrame *child = aItems.childList;
3439 nsIFrame* prev = nsnull;
3440 while (child) {
3441 nsIFrame *nextSibling = child->GetNextSibling();
3442 if (nsGkAtoms::tableCaptionFrame == child->GetType()) {
3443 aItems.RemoveChild(child, prev);
3444 aCaptions.AddChild(child);
3445 } else {
3446 prev = child;
3448 child = nextSibling;
3453 // Construct the outer, inner table frames and the children frames for the table.
3454 // XXX Page break frames for pseudo table frames are not constructed to avoid the risk
3455 // associated with revising the pseudo frame mechanism. The long term solution
3456 // of having frames handle page-break-before/after will solve the problem.
3457 nsresult
3458 nsCSSFrameConstructor::ConstructTableFrame(nsFrameConstructorState& aState,
3459 nsIContent* aContent,
3460 nsIFrame* aContentParent,
3461 nsStyleContext* aStyleContext,
3462 PRInt32 aNameSpaceID,
3463 PRBool aIsPseudo,
3464 nsFrameItems& aChildItems,
3465 nsIFrame*& aNewOuterFrame,
3466 nsIFrame*& aNewInnerFrame)
3468 nsresult rv = NS_OK;
3471 // create the pseudo SC for the outer table as a child of the inner SC
3472 nsRefPtr<nsStyleContext> outerStyleContext;
3473 outerStyleContext = mPresShell->StyleSet()->
3474 ResolvePseudoStyleFor(aContent, nsCSSAnonBoxes::tableOuter, aStyleContext);
3476 // Create the outer table frame which holds the caption and inner table frame
3477 #ifdef MOZ_MATHML
3478 if (kNameSpaceID_MathML == aNameSpaceID)
3479 aNewOuterFrame = NS_NewMathMLmtableOuterFrame(mPresShell,
3480 outerStyleContext);
3481 else
3482 #endif
3483 aNewOuterFrame = NS_NewTableOuterFrame(mPresShell, outerStyleContext);
3485 nsIFrame* parentFrame = aContentParent;
3486 nsFrameItems* frameItems = &aChildItems;
3487 // We may need to push a float containing block
3488 nsFrameConstructorSaveState floatSaveState;
3489 if (!aIsPseudo) {
3490 // this frame may have a pseudo parent
3491 PRBool hasPseudoParent = PR_FALSE;
3492 GetParentFrame(aNameSpaceID,*parentFrame, nsGkAtoms::tableOuterFrame,
3493 aState, parentFrame, hasPseudoParent);
3494 if (!hasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
3495 ProcessPseudoFrames(aState, aChildItems);
3497 if (hasPseudoParent) {
3498 aState.PushFloatContainingBlock(parentFrame, floatSaveState,
3499 PR_FALSE, PR_FALSE);
3500 frameItems = &aState.mPseudoFrames.mCellInner.mChildList;
3501 if (aState.mPseudoFrames.mTableOuter.mFrame) {
3502 ProcessPseudoFrames(aState, nsGkAtoms::tableOuterFrame);
3507 nsIFrame* geometricParent = aState.GetGeometricParent
3508 (outerStyleContext->GetStyleDisplay(),
3509 parentFrame);
3511 // Init the table outer frame and see if we need to create a view, e.g.
3512 // the frame is absolutely positioned
3513 InitAndRestoreFrame(aState, aContent, geometricParent, nsnull, aNewOuterFrame);
3514 nsHTMLContainerFrame::CreateViewForFrame(aNewOuterFrame, aContentParent,
3515 PR_FALSE);
3517 // Create the inner table frame
3518 #ifdef MOZ_MATHML
3519 if (kNameSpaceID_MathML == aNameSpaceID)
3520 aNewInnerFrame = NS_NewMathMLmtableFrame(mPresShell, aStyleContext);
3521 else
3522 #endif
3523 aNewInnerFrame = NS_NewTableFrame(mPresShell, aStyleContext);
3525 InitAndRestoreFrame(aState, aContent, aNewOuterFrame, nsnull,
3526 aNewInnerFrame);
3528 if (!aIsPseudo) {
3529 // Put the newly created frames into the right child list
3530 aNewOuterFrame->SetInitialChildList(nsnull, aNewInnerFrame);
3532 rv = aState.AddChild(aNewOuterFrame, *frameItems, aContent,
3533 aStyleContext, parentFrame);
3534 if (NS_FAILED(rv)) {
3535 return rv;
3538 nsFrameItems childItems;
3539 rv = ProcessChildren(aState, aContent, aNewInnerFrame, PR_TRUE, childItems,
3540 PR_FALSE);
3541 // XXXbz what about cleaning up?
3542 if (NS_FAILED(rv)) return rv;
3544 // if there are any anonymous children for the table, create frames for them
3545 CreateAnonymousFrames(nsnull, aState, aContent, aNewInnerFrame,
3546 PR_FALSE, childItems);
3548 nsFrameItems captionItems;
3549 PullOutCaptionFrames(childItems, captionItems);
3551 // Set the inner table frame's initial primary list
3552 aNewInnerFrame->SetInitialChildList(nsnull, childItems.childList);
3554 // Set the outer table frame's secondary childlist lists
3555 if (captionItems.childList) {
3556 aNewOuterFrame->SetInitialChildList(nsGkAtoms::captionList,
3557 captionItems.childList);
3561 return rv;
3564 nsresult
3565 nsCSSFrameConstructor::ConstructTableCaptionFrame(nsFrameConstructorState& aState,
3566 nsIContent* aContent,
3567 nsIFrame* aParentFrameIn,
3568 nsStyleContext* aStyleContext,
3569 PRInt32 aNameSpaceID,
3570 nsFrameItems& aChildItems,
3571 nsIFrame*& aNewFrame,
3572 PRBool& aIsPseudoParent)
3575 nsresult rv = NS_OK;
3576 if (!aParentFrameIn) return rv;
3578 nsIFrame* parentFrame = aParentFrameIn;
3579 aIsPseudoParent = PR_FALSE;
3580 // this frame may have a pseudo parent
3581 GetParentFrame(aNameSpaceID, *aParentFrameIn,
3582 nsGkAtoms::tableCaptionFrame, aState, parentFrame,
3583 aIsPseudoParent);
3584 if (!aIsPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
3585 ProcessPseudoFrames(aState, aChildItems);
3588 aNewFrame = NS_NewTableCaptionFrame(mPresShell, aStyleContext);
3589 if (NS_UNLIKELY(!aNewFrame)) {
3590 return NS_ERROR_OUT_OF_MEMORY;
3592 InitAndRestoreFrame(aState, aContent, parentFrame, nsnull, aNewFrame);
3593 // XXXbz should we be passing in a non-null aContentParentFrame?
3594 nsHTMLContainerFrame::CreateViewForFrame(aNewFrame, nsnull, PR_FALSE);
3596 PRBool haveFirstLetterStyle, haveFirstLineStyle;
3597 ShouldHaveSpecialBlockStyle(aContent, aStyleContext,
3598 &haveFirstLetterStyle, &haveFirstLineStyle);
3600 // The caption frame is a float container
3601 nsFrameConstructorSaveState floatSaveState;
3602 aState.PushFloatContainingBlock(aNewFrame, floatSaveState,
3603 haveFirstLetterStyle, haveFirstLineStyle);
3604 nsFrameItems childItems;
3605 rv = ProcessChildren(aState, aContent, aNewFrame,
3606 PR_TRUE, childItems, PR_TRUE);
3607 if (NS_FAILED(rv)) return rv;
3608 aNewFrame->SetInitialChildList(nsnull, childItems.childList);
3609 if (aIsPseudoParent) {
3610 aState.mPseudoFrames.mTableOuter.mChildList2.AddChild(aNewFrame);
3613 return rv;
3617 nsresult
3618 nsCSSFrameConstructor::ConstructTableRowGroupFrame(nsFrameConstructorState& aState,
3619 nsIContent* aContent,
3620 nsIFrame* aParentFrameIn,
3621 nsStyleContext* aStyleContext,
3622 PRInt32 aNameSpaceID,
3623 PRBool aIsPseudo,
3624 nsFrameItems& aChildItems,
3625 nsIFrame*& aNewFrame,
3626 PRBool& aIsPseudoParent)
3628 nsresult rv = NS_OK;
3629 if (!aParentFrameIn) return rv;
3631 nsIFrame* parentFrame = aParentFrameIn;
3632 aIsPseudoParent = PR_FALSE;
3633 if (!aIsPseudo) {
3634 // this frame may have a pseudo parent
3635 GetParentFrame(aNameSpaceID, *aParentFrameIn,
3636 nsGkAtoms::tableRowGroupFrame, aState, parentFrame,
3637 aIsPseudoParent);
3638 if (!aIsPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
3639 ProcessPseudoFrames(aState, aChildItems);
3641 if (!aIsPseudo && aIsPseudoParent && aState.mPseudoFrames.mRowGroup.mFrame) {
3642 ProcessPseudoFrames(aState, nsGkAtoms::tableRowGroupFrame);
3646 const nsStyleDisplay* styleDisplay = aStyleContext->GetStyleDisplay();
3648 aNewFrame = NS_NewTableRowGroupFrame(mPresShell, aStyleContext);
3650 nsIFrame* scrollFrame = nsnull;
3651 if (styleDisplay->IsScrollableOverflow()) {
3652 // Create an area container for the frame
3653 BuildScrollFrame(aState, aContent, aStyleContext, aNewFrame, parentFrame,
3654 nsnull, scrollFrame, aStyleContext);
3657 else {
3658 if (NS_UNLIKELY(!aNewFrame)) {
3659 return NS_ERROR_OUT_OF_MEMORY;
3661 InitAndRestoreFrame(aState, aContent, parentFrame, nsnull, aNewFrame);
3662 // XXXbz should we be passing in a non-null aContentParentFrame?
3663 nsHTMLContainerFrame::CreateViewForFrame(aNewFrame, nsnull, PR_FALSE);
3666 if (!aIsPseudo) {
3667 nsFrameItems childItems;
3668 rv = ProcessChildren(aState, aContent, aNewFrame, PR_TRUE, childItems,
3669 PR_FALSE);
3671 if (NS_FAILED(rv)) return rv;
3673 // if there are any anonymous children for the table, create frames for them
3674 CreateAnonymousFrames(nsnull, aState, aContent, aNewFrame,
3675 PR_FALSE, childItems);
3677 aNewFrame->SetInitialChildList(nsnull, childItems.childList);
3678 if (aIsPseudoParent) {
3679 nsIFrame* child = (scrollFrame) ? scrollFrame : aNewFrame;
3680 aState.mPseudoFrames.mTableInner.mChildList.AddChild(child);
3684 // if there is a scroll frame, use it as the one constructed
3685 if (scrollFrame) {
3686 aNewFrame = scrollFrame;
3689 return rv;
3692 nsresult
3693 nsCSSFrameConstructor::ConstructTableColGroupFrame(nsFrameConstructorState& aState,
3694 nsIContent* aContent,
3695 nsIFrame* aParentFrameIn,
3696 nsStyleContext* aStyleContext,
3697 PRInt32 aNameSpaceID,
3698 PRBool aIsPseudo,
3699 nsFrameItems& aChildItems,
3700 nsIFrame*& aNewFrame,
3701 PRBool& aIsPseudoParent)
3703 nsresult rv = NS_OK;
3704 if (!aParentFrameIn) return rv;
3706 nsIFrame* parentFrame = aParentFrameIn;
3707 aIsPseudoParent = PR_FALSE;
3708 if (!aIsPseudo) {
3709 // this frame may have a pseudo parent
3710 GetParentFrame(aNameSpaceID, *aParentFrameIn,
3711 nsGkAtoms::tableColGroupFrame, aState, parentFrame,
3712 aIsPseudoParent);
3713 if (!aIsPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
3714 ProcessPseudoFrames(aState, aChildItems);
3716 if (!aIsPseudo && aIsPseudoParent && aState.mPseudoFrames.mColGroup.mFrame) {
3717 ProcessPseudoFrames(aState, nsGkAtoms::tableColGroupFrame);
3721 aNewFrame = NS_NewTableColGroupFrame(mPresShell, aStyleContext);
3722 if (NS_UNLIKELY(!aNewFrame)) {
3723 return NS_ERROR_OUT_OF_MEMORY;
3725 InitAndRestoreFrame(aState, aContent, parentFrame, nsnull, aNewFrame);
3727 if (!aIsPseudo) {
3728 nsFrameItems childItems;
3729 rv = ProcessChildren(aState, aContent, aNewFrame, PR_TRUE, childItems,
3730 PR_FALSE);
3731 if (NS_FAILED(rv)) return rv;
3732 aNewFrame->SetInitialChildList(nsnull, childItems.childList);
3733 if (aIsPseudoParent) {
3734 aState.mPseudoFrames.mTableInner.mChildList.AddChild(aNewFrame);
3738 return rv;
3741 nsresult
3742 nsCSSFrameConstructor::ConstructTableRowFrame(nsFrameConstructorState& aState,
3743 nsIContent* aContent,
3744 nsIFrame* aParentFrameIn,
3745 nsStyleContext* aStyleContext,
3746 PRInt32 aNameSpaceID,
3747 PRBool aIsPseudo,
3748 nsFrameItems& aChildItems,
3749 nsIFrame*& aNewFrame,
3750 PRBool& aIsPseudoParent)
3752 nsresult rv = NS_OK;
3753 if (!aParentFrameIn) return rv;
3755 nsIFrame* parentFrame = aParentFrameIn;
3756 aIsPseudoParent = PR_FALSE;
3757 if (!aIsPseudo) {
3758 // this frame may have a pseudo parent
3759 GetParentFrame(aNameSpaceID, *aParentFrameIn,
3760 nsGkAtoms::tableRowFrame, aState, parentFrame,
3761 aIsPseudoParent);
3762 if (!aIsPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
3763 ProcessPseudoFrames(aState, aChildItems);
3765 if (!aIsPseudo && aIsPseudoParent && aState.mPseudoFrames.mRow.mFrame) {
3766 ProcessPseudoFrames(aState, nsGkAtoms::tableRowFrame);
3770 #ifdef MOZ_MATHML
3771 if (kNameSpaceID_MathML == aNameSpaceID)
3772 aNewFrame = NS_NewMathMLmtrFrame(mPresShell, aStyleContext);
3773 else
3774 #endif
3775 aNewFrame = NS_NewTableRowFrame(mPresShell, aStyleContext);
3777 if (NS_UNLIKELY(!aNewFrame)) {
3778 return NS_ERROR_OUT_OF_MEMORY;
3780 InitAndRestoreFrame(aState, aContent, parentFrame, nsnull, aNewFrame);
3781 // XXXbz should we be passing in a non-null aContentParentFrame?
3782 nsHTMLContainerFrame::CreateViewForFrame(aNewFrame, nsnull, PR_FALSE);
3783 if (!aIsPseudo) {
3784 nsFrameItems childItems;
3785 rv = ProcessChildren(aState, aContent, aNewFrame, PR_TRUE, childItems,
3786 PR_FALSE);
3787 if (NS_FAILED(rv)) return rv;
3788 // if there are any anonymous children for the table, create frames for them
3789 CreateAnonymousFrames(nsnull, aState, aContent, aNewFrame,
3790 PR_FALSE, childItems);
3792 aNewFrame->SetInitialChildList(nsnull, childItems.childList);
3793 if (aIsPseudoParent) {
3794 aState.mPseudoFrames.mRowGroup.mChildList.AddChild(aNewFrame);
3798 return rv;
3801 nsresult
3802 nsCSSFrameConstructor::ConstructTableColFrame(nsFrameConstructorState& aState,
3803 nsIContent* aContent,
3804 nsIFrame* aParentFrameIn,
3805 nsStyleContext* aStyleContext,
3806 PRInt32 aNameSpaceID,
3807 PRBool aIsPseudo,
3808 nsFrameItems& aChildItems,
3809 nsIFrame*& aNewFrame,
3810 PRBool& aIsPseudoParent)
3812 nsresult rv = NS_OK;
3813 if (!aParentFrameIn || !aStyleContext) return rv;
3815 nsIFrame* parentFrame = aParentFrameIn;
3816 aIsPseudoParent = PR_FALSE;
3817 if (!aIsPseudo) {
3818 // this frame may have a pseudo parent
3819 GetParentFrame(aNameSpaceID, *aParentFrameIn,
3820 nsGkAtoms::tableColFrame, aState, parentFrame,
3821 aIsPseudoParent);
3822 if (!aIsPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
3823 ProcessPseudoFrames(aState, aChildItems);
3827 nsTableColFrame* colFrame = NS_NewTableColFrame(mPresShell, aStyleContext);
3828 aNewFrame = colFrame;
3829 if (NS_UNLIKELY(!aNewFrame)) {
3830 return NS_ERROR_OUT_OF_MEMORY;
3832 InitAndRestoreFrame(aState, aContent, parentFrame, nsnull, aNewFrame);
3834 // construct additional col frames if the col frame has a span > 1
3835 PRInt32 span = colFrame->GetSpan();
3836 nsIFrame* lastCol = aNewFrame;
3837 nsStyleContext* styleContext = nsnull;
3838 for (PRInt32 spanX = 1; spanX < span; spanX++) {
3839 // The same content node should always resolve to the same style context.
3840 if (1 == spanX)
3841 styleContext = aNewFrame->GetStyleContext();
3842 nsTableColFrame* newCol = NS_NewTableColFrame(mPresShell, styleContext);
3843 if (NS_UNLIKELY(!newCol)) {
3844 return NS_ERROR_OUT_OF_MEMORY;
3846 InitAndRestoreFrame(aState, aContent, parentFrame, nsnull, newCol, PR_FALSE);
3847 lastCol->SetNextSibling(newCol);
3848 lastCol->SetNextContinuation(newCol);
3849 newCol->SetPrevContinuation(lastCol);
3850 newCol->SetColType(eColAnonymousCol);
3851 lastCol = newCol;
3854 if (!aIsPseudo && aIsPseudoParent) {
3855 aState.mPseudoFrames.mColGroup.mChildList.AddChild(aNewFrame);
3858 return rv;
3861 nsresult
3862 nsCSSFrameConstructor::ConstructTableCellFrame(nsFrameConstructorState& aState,
3863 nsIContent* aContent,
3864 nsIFrame* aParentFrameIn,
3865 nsStyleContext* aStyleContext,
3866 PRInt32 aNameSpaceID,
3867 PRBool aIsPseudo,
3868 nsFrameItems& aChildItems,
3869 nsIFrame*& aNewCellOuterFrame,
3870 nsIFrame*& aNewCellInnerFrame,
3871 PRBool& aIsPseudoParent)
3873 nsresult rv = NS_OK;
3874 if (!aParentFrameIn) return rv;
3876 nsIFrame* parentFrame = aParentFrameIn;
3877 aIsPseudoParent = PR_FALSE;
3878 if (!aIsPseudo) {
3879 // this frame may have a pseudo parent
3880 // use nsGkAtoms::tableCellFrame which will match if it is really nsGkAtoms::bcTableCellFrame
3881 GetParentFrame(aNameSpaceID, *aParentFrameIn,
3882 nsGkAtoms::tableCellFrame, aState, parentFrame,
3883 aIsPseudoParent);
3884 if (!aIsPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
3885 ProcessPseudoFrames(aState, aChildItems);
3887 if (!aIsPseudo && aIsPseudoParent && aState.mPseudoFrames.mCellOuter.mFrame) {
3888 ProcessPseudoFrames(aState, nsGkAtoms::tableCellFrame);
3891 #ifdef MOZ_MATHML
3892 // <mtable> is border separate in mathml.css and the MathML code doesn't implement
3893 // border collapse. For those users who style <mtable> with border collapse,
3894 // give them the default non-MathML table frames that understand border collpase.
3895 // This won't break us because MathML table frames are all subclasses of the default
3896 // table code, and so we can freely mix <mtable> with <mtr> or <tr>, <mtd> or <td>.
3897 // What will happen is just that non-MathML frames won't understand MathML attributes
3898 // and will therefore miss the special handling that the MathML code does.
3899 if (kNameSpaceID_MathML == aNameSpaceID && !IsBorderCollapse(parentFrame))
3900 aNewCellOuterFrame = NS_NewMathMLmtdFrame(mPresShell, aStyleContext);
3901 else
3902 #endif
3903 // Warning: If you change this and add a wrapper frame around table cell
3904 // frames, make sure Bug 368554 doesn't regress!
3905 // See IsInAutoWidthTableCellForQuirk() in nsImageFrame.cpp.
3906 aNewCellOuterFrame = NS_NewTableCellFrame(mPresShell, aStyleContext,
3907 IsBorderCollapse(parentFrame));
3909 if (NS_UNLIKELY(!aNewCellOuterFrame)) {
3910 return NS_ERROR_OUT_OF_MEMORY;
3913 // Initialize the table cell frame
3914 InitAndRestoreFrame(aState, aContent, parentFrame, nsnull, aNewCellOuterFrame);
3915 // XXXbz should we be passing in a non-null aContentParentFrame?
3916 nsHTMLContainerFrame::CreateViewForFrame(aNewCellOuterFrame, nsnull, PR_FALSE);
3918 // Resolve pseudo style and initialize the body cell frame
3919 nsRefPtr<nsStyleContext> innerPseudoStyle;
3920 innerPseudoStyle = mPresShell->StyleSet()->
3921 ResolvePseudoStyleFor(aContent,
3922 nsCSSAnonBoxes::cellContent, aStyleContext);
3924 // Create a block frame that will format the cell's content
3925 PRBool isBlock;
3926 #ifdef MOZ_MATHML
3927 if (kNameSpaceID_MathML == aNameSpaceID) {
3928 aNewCellInnerFrame = NS_NewMathMLmtdInnerFrame(mPresShell, innerPseudoStyle);
3929 isBlock = PR_FALSE;
3931 else
3932 #endif
3934 aNewCellInnerFrame = NS_NewTableCellInnerFrame(mPresShell, innerPseudoStyle);
3935 isBlock = PR_TRUE;
3939 if (NS_UNLIKELY(!aNewCellInnerFrame)) {
3940 aNewCellOuterFrame->Destroy();
3941 aNewCellOuterFrame = nsnull;
3942 return NS_ERROR_OUT_OF_MEMORY;
3945 InitAndRestoreFrame(aState, aContent, aNewCellOuterFrame, nsnull, aNewCellInnerFrame);
3947 if (!aIsPseudo) {
3948 PRBool haveFirstLetterStyle = PR_FALSE, haveFirstLineStyle = PR_FALSE;
3949 if (isBlock) {
3950 ShouldHaveSpecialBlockStyle(aContent, aStyleContext,
3951 &haveFirstLetterStyle, &haveFirstLineStyle);
3954 // The block frame is a float container
3955 nsFrameConstructorSaveState floatSaveState;
3956 aState.PushFloatContainingBlock(isBlock ? aNewCellInnerFrame : nsnull,
3957 floatSaveState,
3958 haveFirstLetterStyle, haveFirstLineStyle);
3960 // Process the child content
3961 nsFrameItems childItems;
3962 rv = ProcessChildren(aState, aContent, aNewCellInnerFrame,
3963 PR_TRUE, childItems, isBlock);
3965 if (NS_FAILED(rv)) {
3966 // Clean up
3967 // XXXbz kids of this stuff need to be cleaned up too!
3968 aNewCellInnerFrame->Destroy();
3969 aNewCellInnerFrame = nsnull;
3970 aNewCellOuterFrame->Destroy();
3971 aNewCellOuterFrame = nsnull;
3972 return rv;
3975 aNewCellInnerFrame->SetInitialChildList(nsnull, childItems.childList);
3976 aNewCellOuterFrame->SetInitialChildList(nsnull, aNewCellInnerFrame);
3977 if (aIsPseudoParent) {
3978 aState.mPseudoFrames.mRow.mChildList.AddChild(aNewCellOuterFrame);
3982 return rv;
3985 static PRBool
3986 NeedFrameFor(nsIFrame* aParentFrame,
3987 nsIContent* aChildContent)
3989 // don't create a whitespace frame if aParentFrame doesn't want it.
3990 // always create frames for children in generated content. counter(),
3991 // quotes, and attr() content can easily change dynamically and we don't
3992 // want to be reconstructing frames. It's not even clear that these
3993 // should be considered ignorable just because they evaluate to
3994 // whitespace.
3995 return !aParentFrame->IsFrameOfType(nsIFrame::eExcludesIgnorableWhitespace)
3996 || !TextIsOnlyWhitespace(aChildContent)
3997 || aParentFrame->IsGeneratedContentFrame();
4000 const nsStyleDisplay*
4001 nsCSSFrameConstructor::GetDisplay(nsIFrame* aFrame)
4003 if (nsnull == aFrame) {
4004 return nsnull;
4006 return aFrame->GetStyleContext()->GetStyleDisplay();
4009 /***********************************************
4010 * END TABLE SECTION
4011 ***********************************************/
4013 static PRBool CheckOverflow(nsPresContext* aPresContext,
4014 const nsStyleDisplay* aDisplay)
4016 if (aDisplay->mOverflowX == NS_STYLE_OVERFLOW_VISIBLE)
4017 return PR_FALSE;
4019 if (aDisplay->mOverflowX == NS_STYLE_OVERFLOW_CLIP)
4020 aPresContext->SetViewportOverflowOverride(NS_STYLE_OVERFLOW_HIDDEN,
4021 NS_STYLE_OVERFLOW_HIDDEN);
4022 else
4023 aPresContext->SetViewportOverflowOverride(aDisplay->mOverflowX,
4024 aDisplay->mOverflowY);
4025 return PR_TRUE;
4029 * This checks the root element and the HTML BODY, if any, for an "overflow" property
4030 * that should be applied to the viewport. If one is found then we return the
4031 * element that we took the overflow from (which should then be treated as
4032 * "overflow:visible"), and we store the overflow style in the prescontext.
4033 * @return if scroll was propagated from some content node, the content node it
4034 * was propagated from.
4036 nsIContent*
4037 nsCSSFrameConstructor::PropagateScrollToViewport()
4039 // Set default
4040 nsPresContext* presContext = mPresShell->GetPresContext();
4041 presContext->SetViewportOverflowOverride(NS_STYLE_OVERFLOW_AUTO,
4042 NS_STYLE_OVERFLOW_AUTO);
4044 // We never mess with the viewport scroll state
4045 // when printing or in print preview
4046 if (presContext->IsPaginated()) {
4047 return nsnull;
4050 nsIContent* docElement = mDocument->GetRootContent();
4052 // Check the style on the document root element
4053 nsStyleSet *styleSet = mPresShell->StyleSet();
4054 nsRefPtr<nsStyleContext> rootStyle;
4055 rootStyle = styleSet->ResolveStyleFor(docElement, nsnull);
4056 if (!rootStyle) {
4057 return nsnull;
4059 if (CheckOverflow(presContext, rootStyle->GetStyleDisplay())) {
4060 // tell caller we stole the overflow style from the root element
4061 return docElement;
4064 // Don't look in the BODY for non-HTML documents or HTML documents
4065 // with non-HTML roots
4066 // XXX this should be earlier; we shouldn't even look at the document root
4067 // for non-HTML documents. Fix this once we support explicit CSS styling
4068 // of the viewport
4069 // XXX what about XHTML?
4070 nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument));
4071 if (!htmlDoc || !docElement->IsNodeOfType(nsINode::eHTML)) {
4072 return nsnull;
4075 nsCOMPtr<nsIDOMHTMLElement> body;
4076 htmlDoc->GetBody(getter_AddRefs(body));
4077 nsCOMPtr<nsIContent> bodyElement = do_QueryInterface(body);
4079 if (!bodyElement ||
4080 !bodyElement->NodeInfo()->Equals(nsGkAtoms::body)) {
4081 // The body is not a <body> tag, it's a <frameset>.
4082 return nsnull;
4085 nsRefPtr<nsStyleContext> bodyStyle;
4086 bodyStyle = styleSet->ResolveStyleFor(bodyElement, rootStyle);
4087 if (!bodyStyle) {
4088 return nsnull;
4091 if (CheckOverflow(presContext, bodyStyle->GetStyleDisplay())) {
4092 // tell caller we stole the overflow style from the body element
4093 return bodyElement;
4096 return nsnull;
4100 * New one
4102 nsresult
4103 nsCSSFrameConstructor::ConstructDocElementFrame(nsFrameConstructorState& aState,
4104 nsIContent* aDocElement,
4105 nsIFrame* aParentFrame,
4106 nsIFrame** aNewFrame)
4108 *aNewFrame = nsnull;
4110 if (!mTempFrameTreeState)
4111 aState.mPresShell->CaptureHistoryState(getter_AddRefs(mTempFrameTreeState));
4113 // ----- reattach gfx scrollbars ------
4114 // Gfx scrollframes were created in the root frame but the primary frame map may have been destroyed if a
4115 // new style sheet was loaded so lets reattach the frames to their content.
4116 // XXX this seems truly bogus, we wipe out mGfxScrollFrame below
4117 if (mGfxScrollFrame) {
4118 nsIFrame* gfxScrollbarFrame1 = mGfxScrollFrame->GetFirstChild(nsnull);
4119 if (gfxScrollbarFrame1) {
4120 // XXX This works, but why?
4121 aState.mFrameManager->
4122 SetPrimaryFrameFor(gfxScrollbarFrame1->GetContent(), gfxScrollbarFrame1);
4124 nsIFrame* gfxScrollbarFrame2 = gfxScrollbarFrame1->GetNextSibling();
4125 if (gfxScrollbarFrame2) {
4126 // XXX This works, but why?
4127 aState.mFrameManager->
4128 SetPrimaryFrameFor(gfxScrollbarFrame2->GetContent(), gfxScrollbarFrame2);
4133 // --------- CREATE AREA OR BOX FRAME -------
4134 nsRefPtr<nsStyleContext> styleContext;
4135 styleContext = mPresShell->StyleSet()->ResolveStyleFor(aDocElement,
4136 nsnull);
4138 const nsStyleDisplay* display = styleContext->GetStyleDisplay();
4140 // Ensure that our XBL bindings are installed.
4141 if (display->mBinding) {
4142 // Get the XBL loader.
4143 nsresult rv;
4144 PRBool resolveStyle;
4146 nsIXBLService * xblService = GetXBLService();
4147 if (!xblService)
4148 return NS_ERROR_FAILURE;
4150 nsRefPtr<nsXBLBinding> binding;
4151 rv = xblService->LoadBindings(aDocElement, display->mBinding->mURI,
4152 display->mBinding->mOriginPrincipal,
4153 PR_FALSE, getter_AddRefs(binding),
4154 &resolveStyle);
4155 if (NS_FAILED(rv))
4156 return NS_OK; // Binding will load asynchronously.
4158 if (binding) {
4159 mDocument->BindingManager()->AddToAttachedQueue(binding);
4162 if (resolveStyle) {
4163 styleContext = mPresShell->StyleSet()->ResolveStyleFor(aDocElement,
4164 nsnull);
4165 display = styleContext->GetStyleDisplay();
4169 // --------- IF SCROLLABLE WRAP IN SCROLLFRAME --------
4171 #ifdef DEBUG
4172 PRBool propagatedScrollToViewport =
4173 PropagateScrollToViewport() == aDocElement;
4175 NS_ASSERTION(!display->IsScrollableOverflow() ||
4176 aState.mPresContext->IsPaginated() ||
4177 propagatedScrollToViewport,
4178 "Scrollbars should have been propagated to the viewport");
4179 #endif
4181 if (NS_UNLIKELY(display->mDisplay == NS_STYLE_DISPLAY_NONE)) {
4182 mInitialContainingBlock = nsnull;
4183 mRootElementStyleFrame = nsnull;
4184 return NS_OK;
4187 nsFrameConstructorSaveState absoluteSaveState;
4188 if (mHasRootAbsPosContainingBlock) {
4189 // Push the absolute containing block now so we can absolutely position
4190 // the root element
4191 aState.PushAbsoluteContainingBlock(mDocElementContainingBlock, absoluteSaveState);
4194 nsresult rv;
4196 // The rules from CSS 2.1, section 9.2.4, have already been applied
4197 // by the style system, so we can assume that display->mDisplay is
4198 // either NONE, BLOCK, or TABLE.
4200 PRBool docElemIsTable = (display->mDisplay == NS_STYLE_DISPLAY_TABLE) &&
4201 !IsSpecialContent(aDocElement, aDocElement->Tag(),
4202 aDocElement->GetNameSpaceID(),
4203 styleContext);
4205 // contentFrame is the primary frame for the root element. *aNewFrame
4206 // is the frame that will be the child of the initial containing block.
4207 // These are usually the same frame but they can be different, in
4208 // particular if the root frame is positioned, in which case
4209 // contentFrame is the out-of-flow frame and *aNewFrame is the
4210 // placeholder.
4211 nsIFrame* contentFrame;
4212 PRBool processChildren = PR_FALSE;
4213 if (docElemIsTable) {
4214 nsIFrame* innerTableFrame;
4215 nsFrameItems frameItems;
4216 // if the document is a table then just populate it.
4217 rv = ConstructTableFrame(aState, aDocElement,
4218 aParentFrame, styleContext,
4219 kNameSpaceID_None, PR_FALSE, frameItems, contentFrame,
4220 innerTableFrame);
4221 if (NS_FAILED(rv))
4222 return rv;
4223 if (!contentFrame || !frameItems.childList)
4224 return NS_ERROR_FAILURE;
4225 *aNewFrame = frameItems.childList;
4226 NS_ASSERTION(!frameItems.childList->GetNextSibling(),
4227 "multiple root element frames");
4228 } else {
4229 // otherwise build a box or a block
4230 #ifdef MOZ_XUL
4231 if (aDocElement->IsNodeOfType(nsINode::eXUL)) {
4232 contentFrame = NS_NewDocElementBoxFrame(mPresShell, styleContext);
4233 if (NS_UNLIKELY(!contentFrame)) {
4234 return NS_ERROR_OUT_OF_MEMORY;
4236 InitAndRestoreFrame(aState, aDocElement, aParentFrame, nsnull, contentFrame);
4237 *aNewFrame = contentFrame;
4238 processChildren = PR_TRUE;
4240 else
4241 #endif
4242 #ifdef MOZ_SVG
4243 if (aDocElement->GetNameSpaceID() == kNameSpaceID_SVG) {
4244 if (aDocElement->Tag() == nsGkAtoms::svg && NS_SVGEnabled()) {
4245 contentFrame = NS_NewSVGOuterSVGFrame(mPresShell, aDocElement, styleContext);
4246 if (NS_UNLIKELY(!contentFrame)) {
4247 return NS_ERROR_OUT_OF_MEMORY;
4249 InitAndRestoreFrame(aState, aDocElement,
4250 aState.GetGeometricParent(display, aParentFrame),
4251 nsnull, contentFrame);
4253 // AddChild takes care of transforming the frame tree for fixed-pos
4254 // or abs-pos situations
4255 nsFrameItems frameItems;
4256 rv = aState.AddChild(contentFrame, frameItems, aDocElement,
4257 styleContext, aParentFrame);
4258 if (NS_FAILED(rv) || !frameItems.childList) {
4259 return rv;
4261 *aNewFrame = frameItems.childList;
4262 processChildren = PR_TRUE;
4264 // See if we need to create a view, e.g. the frame is absolutely positioned
4265 nsHTMLContainerFrame::CreateViewForFrame(contentFrame, aParentFrame, PR_FALSE);
4266 } else {
4267 return NS_ERROR_FAILURE;
4270 else
4271 #endif
4273 contentFrame = NS_NewBlockFrame(mPresShell, styleContext,
4274 NS_BLOCK_SPACE_MGR|NS_BLOCK_MARGIN_ROOT);
4275 if (!contentFrame)
4276 return NS_ERROR_OUT_OF_MEMORY;
4277 nsFrameItems frameItems;
4278 rv = ConstructBlock(aState, display, aDocElement,
4279 aState.GetGeometricParent(display, aParentFrame),
4280 aParentFrame, styleContext, &contentFrame,
4281 frameItems, display->IsPositioned());
4282 if (NS_FAILED(rv) || !frameItems.childList)
4283 return rv;
4284 *aNewFrame = frameItems.childList;
4285 NS_ASSERTION(!frameItems.childList->GetNextSibling(),
4286 "multiple root element frames");
4290 // set the primary frame
4291 aState.mFrameManager->SetPrimaryFrameFor(aDocElement, contentFrame);
4293 mInitialContainingBlock = contentFrame;
4295 // Figure out which frame has the main style for the document element,
4296 // assigning it to mRootElementStyleFrame.
4297 // Backgrounds should be propagated from that frame to the viewport.
4298 PRBool isChild;
4299 contentFrame->GetParentStyleContextFrame(aState.mPresContext,
4300 &mRootElementStyleFrame, &isChild);
4301 if (!isChild) {
4302 mRootElementStyleFrame = mInitialContainingBlock;
4305 if (processChildren) {
4306 // Still need to process the child content
4307 nsFrameItems childItems;
4309 // Create any anonymous frames the doc element frame requires
4310 // This must happen before ProcessChildren to ensure that popups are
4311 // never constructed before the popupset.
4312 CreateAnonymousFrames(nsnull, aState, aDocElement, contentFrame,
4313 PR_FALSE, childItems, PR_TRUE);
4314 NS_ASSERTION(!nsLayoutUtils::GetAsBlock(contentFrame),
4315 "Only XUL and SVG frames should reach here");
4316 ProcessChildren(aState, aDocElement, contentFrame, PR_TRUE, childItems,
4317 PR_FALSE);
4319 // Set the initial child lists
4320 contentFrame->SetInitialChildList(nsnull, childItems.childList);
4323 return NS_OK;
4327 nsresult
4328 nsCSSFrameConstructor::ConstructRootFrame(nsIContent* aDocElement,
4329 nsIFrame** aNewFrame)
4331 AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
4332 NS_PRECONDITION(aNewFrame, "null out param");
4335 how the root frame hierarchy should look
4337 Galley presentation, non-XUL, with scrolling (i.e. not a frameset):
4339 ViewportFrame [fixed-cb]
4340 nsHTMLScrollFrame
4341 CanvasFrame [abs-cb]
4342 root element frame (nsBlockFrame, nsSVGOuterSVGFrame,
4343 nsTableOuterFrame, nsPlaceholderFrame)
4345 Galley presentation, non-XUL, without scrolling (i.e. a frameset):
4347 ViewportFrame [fixed-cb]
4348 CanvasFrame [abs-cb]
4349 root element frame (nsBlockFrame)
4351 Galley presentation, XUL
4353 ViewportFrame [fixed-cb]
4354 nsRootBoxFrame
4355 root element frame (nsDocElementBoxFrame)
4357 Print presentation, non-XUL
4359 ViewportFrame
4360 nsSimplePageSequenceFrame
4361 nsPageFrame [fixed-cb]
4362 nsPageContentFrame
4363 CanvasFrame [abs-cb]
4364 root element frame (nsBlockFrame, nsSVGOuterSVGFrame,
4365 nsTableOuterFrame, nsPlaceholderFrame)
4367 Print-preview presentation, non-XUL
4369 ViewportFrame
4370 nsHTMLScrollFrame
4371 nsSimplePageSequenceFrame
4372 nsPageFrame [fixed-cb]
4373 nsPageContentFrame
4374 CanvasFrame [abs-cb]
4375 root element frame (nsBlockFrame, nsSVGOuterSVGFrame,
4376 nsTableOuterFrame, nsPlaceholderFrame)
4378 Print/print preview of XUL is not supported.
4379 [fixed-cb]: the default containing block for fixed-pos content
4380 [abs-cb]: the default containing block for abs-pos content
4382 Meaning of nsCSSFrameConstructor fields:
4383 mInitialContainingBlock is "root element frame".
4384 mDocElementContainingBlock is the parent of mInitialContainingBlock
4385 (i.e. CanvasFrame or nsRootBoxFrame)
4386 mFixedContainingBlock is the [fixed-cb]
4387 mGfxScrollFrame is the nsHTMLScrollFrame mentioned above, or null if there isn't one
4388 mPageSequenceFrame is the nsSimplePageSequenceFrame, or null if there isn't one
4391 // Set up our style rule observer.
4393 mPresShell->StyleSet()->SetBindingManager(mDocument->BindingManager());
4396 // --------- BUILD VIEWPORT -----------
4397 nsIFrame* viewportFrame = nsnull;
4398 nsRefPtr<nsStyleContext> viewportPseudoStyle;
4399 nsStyleSet *styleSet = mPresShell->StyleSet();
4401 viewportPseudoStyle = styleSet->ResolvePseudoStyleFor(nsnull,
4402 nsCSSAnonBoxes::viewport,
4403 nsnull);
4405 viewportFrame = NS_NewViewportFrame(mPresShell, viewportPseudoStyle);
4407 nsPresContext* presContext = mPresShell->GetPresContext();
4409 // XXXbz do we _have_ to pass a null content pointer to that frame?
4410 // Would it really kill us to pass in the root element or something?
4411 // What would that break?
4412 viewportFrame->Init(nsnull, nsnull, nsnull);
4414 // Bind the viewport frame to the root view
4415 nsIViewManager* viewManager = mPresShell->GetViewManager();
4416 nsIView* rootView;
4418 viewManager->GetRootView(rootView);
4419 viewportFrame->SetView(rootView);
4421 nsContainerFrame::SyncFrameViewProperties(presContext, viewportFrame,
4422 viewportPseudoStyle, rootView);
4424 // The viewport is the containing block for 'fixed' elements
4425 mFixedContainingBlock = viewportFrame;
4427 // --------- CREATE ROOT FRAME -------
4430 // Create the root frame. The document element's frame is a child of the
4431 // root frame.
4433 // The root frame serves two purposes:
4434 // - reserves space for any margins needed for the document element's frame
4435 // - renders the document element's background. This ensures the background covers
4436 // the entire canvas as specified by the CSS2 spec
4438 PRBool isPaginated = presContext->IsRootPaginatedDocument();
4440 nsIFrame* rootFrame = nsnull;
4441 nsIAtom* rootPseudo;
4443 if (!isPaginated) {
4444 #ifdef MOZ_XUL
4445 if (aDocElement->IsNodeOfType(nsINode::eXUL))
4447 // pass a temporary stylecontext, the correct one will be set later
4448 rootFrame = NS_NewRootBoxFrame(mPresShell, viewportPseudoStyle);
4449 } else
4450 #endif
4452 // pass a temporary stylecontext, the correct one will be set later
4453 rootFrame = NS_NewCanvasFrame(mPresShell, viewportPseudoStyle);
4454 mHasRootAbsPosContainingBlock = PR_TRUE;
4457 rootPseudo = nsCSSAnonBoxes::canvas;
4458 mDocElementContainingBlock = rootFrame;
4459 } else {
4460 // Create a page sequence frame
4461 rootFrame = NS_NewSimplePageSequenceFrame(mPresShell, viewportPseudoStyle);
4462 mPageSequenceFrame = rootFrame;
4463 rootPseudo = nsCSSAnonBoxes::pageSequence;
4467 // --------- IF SCROLLABLE WRAP IN SCROLLFRAME --------
4469 // If the device supports scrolling (e.g., in galley mode on the screen and
4470 // for print-preview, but not when printing), then create a scroll frame that
4471 // will act as the scrolling mechanism for the viewport.
4472 // XXX Do we even need a viewport when printing to a printer?
4474 // As long as the webshell doesn't prohibit it, and the device supports
4475 // it, create a scroll frame that will act as the scolling mechanism for
4476 // the viewport.
4478 // Threre are three possible values stored in the docshell:
4479 // 1) nsIScrollable::Scrollbar_Never = no scrollbars
4480 // 2) nsIScrollable::Scrollbar_Auto = scrollbars appear if needed
4481 // 3) nsIScrollable::Scrollbar_Always = scrollbars always
4482 // Only need to create a scroll frame/view for cases 2 and 3.
4484 PRBool isHTML = aDocElement->IsNodeOfType(nsINode::eHTML);
4485 PRBool isXUL = PR_FALSE;
4487 if (!isHTML) {
4488 isXUL = aDocElement->IsNodeOfType(nsINode::eXUL);
4491 // Never create scrollbars for XUL documents
4492 PRBool isScrollable = !isXUL;
4494 // Never create scrollbars for frameset documents.
4495 if (isHTML) {
4496 nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(mDocument);
4497 if (htmlDoc && htmlDoc->GetIsFrameset())
4498 isScrollable = PR_FALSE;
4501 if (isPaginated) {
4502 isScrollable = presContext->HasPaginatedScrolling();
4505 // We no longer need to do overflow propagation here. It's taken care of
4506 // when we construct frames for the element whose overflow might be
4507 // propagated
4508 NS_ASSERTION(!isScrollable || !isXUL,
4509 "XUL documents should never be scrollable - see above");
4511 nsIFrame* newFrame = rootFrame;
4512 nsRefPtr<nsStyleContext> rootPseudoStyle;
4513 // we must create a state because if the scrollbars are GFX it needs the
4514 // state to build the scrollbar frames.
4515 nsFrameConstructorState state(mPresShell, nsnull, nsnull, nsnull);
4517 nsIFrame* parentFrame = viewportFrame;
4519 // If paginated, make sure we don't put scrollbars in
4520 if (!isScrollable) {
4521 rootPseudoStyle = styleSet->ResolvePseudoStyleFor(nsnull,
4522 rootPseudo,
4523 viewportPseudoStyle);
4524 } else {
4525 if (rootPseudo == nsCSSAnonBoxes::canvas) {
4526 rootPseudo = nsCSSAnonBoxes::scrolledCanvas;
4527 } else {
4528 NS_ASSERTION(rootPseudo == nsCSSAnonBoxes::pageSequence,
4529 "Unknown root pseudo");
4530 rootPseudo = nsCSSAnonBoxes::scrolledPageSequence;
4533 // Build the frame. We give it the content we are wrapping which is the document,
4534 // the root frame, the parent view port frame, and we should get back the new
4535 // frame and the scrollable view if one was created.
4537 // resolve a context for the scrollframe
4538 nsRefPtr<nsStyleContext> styleContext;
4539 styleContext = styleSet->ResolvePseudoStyleFor(nsnull,
4540 nsCSSAnonBoxes::viewportScroll,
4541 viewportPseudoStyle);
4543 // Note that the viewport scrollframe is always built with
4544 // overflow:auto style. This forces the scroll frame to create
4545 // anonymous content for both scrollbars. This is necessary even
4546 // if the HTML or BODY elements are overriding the viewport
4547 // scroll style to 'hidden' --- dynamic style changes might put
4548 // scrollbars back on the viewport and we don't want to have to
4549 // reframe the viewport to create the scrollbar content.
4550 newFrame = nsnull;
4551 rootPseudoStyle = BeginBuildingScrollFrame( state,
4552 aDocElement,
4553 styleContext,
4554 viewportFrame,
4555 nsnull,
4556 rootPseudo,
4557 PR_TRUE,
4558 newFrame);
4560 nsIScrollableFrame* scrollable;
4561 CallQueryInterface(newFrame, &scrollable);
4562 NS_ENSURE_TRUE(scrollable, NS_ERROR_FAILURE);
4564 nsIScrollableView* scrollableView = scrollable->GetScrollableView();
4565 NS_ENSURE_TRUE(scrollableView, NS_ERROR_FAILURE);
4567 viewManager->SetRootScrollableView(scrollableView);
4568 parentFrame = newFrame;
4570 mGfxScrollFrame = newFrame;
4573 rootFrame->SetStyleContextWithoutNotification(rootPseudoStyle);
4574 rootFrame->Init(aDocElement, parentFrame, nsnull);
4576 if (isScrollable) {
4577 FinishBuildingScrollFrame(parentFrame, rootFrame);
4580 if (isPaginated) { // paginated
4581 // Create the first page
4582 // Set the initial child lists
4583 nsIFrame *pageFrame, *canvasFrame;
4584 ConstructPageFrame(mPresShell, presContext, rootFrame, nsnull,
4585 pageFrame, canvasFrame);
4586 rootFrame->SetInitialChildList(nsnull, pageFrame);
4588 // The eventual parent of the document element frame.
4589 // XXX should this be set for every new page (in ConstructPageFrame)?
4590 mDocElementContainingBlock = canvasFrame;
4591 mHasRootAbsPosContainingBlock = PR_TRUE;
4594 viewportFrame->SetInitialChildList(nsnull, newFrame);
4596 *aNewFrame = viewportFrame;
4598 return NS_OK;
4601 nsresult
4602 nsCSSFrameConstructor::ConstructPageFrame(nsIPresShell* aPresShell,
4603 nsPresContext* aPresContext,
4604 nsIFrame* aParentFrame,
4605 nsIFrame* aPrevPageFrame,
4606 nsIFrame*& aPageFrame,
4607 nsIFrame*& aCanvasFrame)
4609 nsStyleContext* parentStyleContext = aParentFrame->GetStyleContext();
4610 nsStyleSet *styleSet = aPresShell->StyleSet();
4612 nsRefPtr<nsStyleContext> pagePseudoStyle;
4613 pagePseudoStyle = styleSet->ResolvePseudoStyleFor(nsnull,
4614 nsCSSAnonBoxes::page,
4615 parentStyleContext);
4617 aPageFrame = NS_NewPageFrame(aPresShell, pagePseudoStyle);
4618 if (NS_UNLIKELY(!aPageFrame))
4619 return NS_ERROR_OUT_OF_MEMORY;
4621 // Initialize the page frame and force it to have a view. This makes printing of
4622 // the pages easier and faster.
4623 aPageFrame->Init(nsnull, aParentFrame, aPrevPageFrame);
4625 nsRefPtr<nsStyleContext> pageContentPseudoStyle;
4626 pageContentPseudoStyle = styleSet->ResolvePseudoStyleFor(nsnull,
4627 nsCSSAnonBoxes::pageContent,
4628 pagePseudoStyle);
4630 nsIFrame* pageContentFrame = NS_NewPageContentFrame(aPresShell, pageContentPseudoStyle);
4631 if (NS_UNLIKELY(!pageContentFrame))
4632 return NS_ERROR_OUT_OF_MEMORY;
4634 // Initialize the page content frame and force it to have a view. Also make it the
4635 // containing block for fixed elements which are repeated on every page.
4636 nsIFrame* prevPageContentFrame = nsnull;
4637 if (aPrevPageFrame) {
4638 prevPageContentFrame = aPrevPageFrame->GetFirstChild(nsnull);
4639 NS_ASSERTION(prevPageContentFrame, "missing page content frame");
4641 pageContentFrame->Init(nsnull, aPageFrame, prevPageContentFrame);
4642 aPageFrame->SetInitialChildList(nsnull, pageContentFrame);
4643 mFixedContainingBlock = pageContentFrame;
4645 nsRefPtr<nsStyleContext> canvasPseudoStyle;
4646 canvasPseudoStyle = styleSet->ResolvePseudoStyleFor(nsnull,
4647 nsCSSAnonBoxes::canvas,
4648 pageContentPseudoStyle);
4650 aCanvasFrame = NS_NewCanvasFrame(aPresShell, canvasPseudoStyle);
4651 if (NS_UNLIKELY(!aCanvasFrame))
4652 return NS_ERROR_OUT_OF_MEMORY;
4654 nsIFrame* prevCanvasFrame = nsnull;
4655 if (prevPageContentFrame) {
4656 prevCanvasFrame = prevPageContentFrame->GetFirstChild(nsnull);
4657 NS_ASSERTION(prevCanvasFrame, "missing canvas frame");
4659 aCanvasFrame->Init(nsnull, pageContentFrame, prevCanvasFrame);
4660 pageContentFrame->SetInitialChildList(nsnull, aCanvasFrame);
4662 return NS_OK;
4665 /* static */
4666 nsresult
4667 nsCSSFrameConstructor::CreatePlaceholderFrameFor(nsIPresShell* aPresShell,
4668 nsIContent* aContent,
4669 nsIFrame* aFrame,
4670 nsStyleContext* aStyleContext,
4671 nsIFrame* aParentFrame,
4672 nsIFrame* aPrevInFlow,
4673 nsIFrame** aPlaceholderFrame)
4675 nsRefPtr<nsStyleContext> placeholderStyle = aPresShell->StyleSet()->
4676 ResolveStyleForNonElement(aStyleContext->GetParent());
4678 // The placeholder frame gets a pseudo style context
4679 nsPlaceholderFrame* placeholderFrame =
4680 (nsPlaceholderFrame*)NS_NewPlaceholderFrame(aPresShell, placeholderStyle);
4682 if (placeholderFrame) {
4683 placeholderFrame->Init(aContent, aParentFrame, aPrevInFlow);
4685 // The placeholder frame has a pointer back to the out-of-flow frame
4686 placeholderFrame->SetOutOfFlowFrame(aFrame);
4688 aFrame->AddStateBits(NS_FRAME_OUT_OF_FLOW);
4690 // Add mapping from absolutely positioned frame to its placeholder frame
4691 aPresShell->FrameManager()->RegisterPlaceholderFrame(placeholderFrame);
4693 *aPlaceholderFrame = static_cast<nsIFrame*>(placeholderFrame);
4695 return NS_OK;
4697 else {
4698 return NS_ERROR_OUT_OF_MEMORY;
4702 nsresult
4703 nsCSSFrameConstructor::ConstructRadioControlFrame(nsIFrame** aNewFrame,
4704 nsIContent* aContent,
4705 nsStyleContext* aStyleContext)
4707 *aNewFrame = NS_NewGfxRadioControlFrame(mPresShell, aStyleContext);
4708 if (NS_UNLIKELY(!*aNewFrame)) {
4709 return NS_ERROR_OUT_OF_MEMORY;
4712 nsRefPtr<nsStyleContext> radioStyle;
4713 radioStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(aContent,
4714 nsCSSAnonBoxes::radio,
4715 aStyleContext);
4716 nsIRadioControlFrame* radio = nsnull;
4717 if (*aNewFrame && NS_SUCCEEDED(CallQueryInterface(*aNewFrame, &radio))) {
4718 radio->SetRadioButtonFaceStyleContext(radioStyle);
4720 return NS_OK;
4723 nsresult
4724 nsCSSFrameConstructor::ConstructCheckboxControlFrame(nsIFrame** aNewFrame,
4725 nsIContent* aContent,
4726 nsStyleContext* aStyleContext)
4728 *aNewFrame = NS_NewGfxCheckboxControlFrame(mPresShell, aStyleContext);
4729 if (NS_UNLIKELY(!*aNewFrame)) {
4730 return NS_ERROR_OUT_OF_MEMORY;
4733 nsRefPtr<nsStyleContext> checkboxStyle;
4734 checkboxStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(aContent,
4735 nsCSSAnonBoxes::check,
4736 aStyleContext);
4737 nsICheckboxControlFrame* checkbox = nsnull;
4738 if (*aNewFrame && NS_SUCCEEDED(CallQueryInterface(*aNewFrame, &checkbox))) {
4739 checkbox->SetCheckboxFaceStyleContext(checkboxStyle);
4741 return NS_OK;
4744 nsresult
4745 nsCSSFrameConstructor::ConstructButtonFrame(nsFrameConstructorState& aState,
4746 nsIContent* aContent,
4747 nsIFrame* aParentFrame,
4748 nsIAtom* aTag,
4749 nsStyleContext* aStyleContext,
4750 nsIFrame** aNewFrame,
4751 const nsStyleDisplay* aStyleDisplay,
4752 nsFrameItems& aFrameItems,
4753 PRBool aHasPseudoParent)
4755 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
4756 ProcessPseudoFrames(aState, aFrameItems);
4759 *aNewFrame = nsnull;
4760 nsIFrame* buttonFrame = nsnull;
4762 if (nsGkAtoms::button == aTag) {
4763 buttonFrame = NS_NewHTMLButtonControlFrame(mPresShell, aStyleContext);
4765 else {
4766 buttonFrame = NS_NewGfxButtonControlFrame(mPresShell, aStyleContext);
4768 if (NS_UNLIKELY(!buttonFrame)) {
4769 return NS_ERROR_OUT_OF_MEMORY;
4771 // Initialize the button frame
4772 nsresult rv = InitAndRestoreFrame(aState, aContent,
4773 aState.GetGeometricParent(aStyleDisplay, aParentFrame),
4774 nsnull, buttonFrame);
4775 if (NS_FAILED(rv)) {
4776 buttonFrame->Destroy();
4777 return rv;
4779 // See if we need to create a view, e.g. the frame is absolutely positioned
4780 nsHTMLContainerFrame::CreateViewForFrame(buttonFrame, aParentFrame, PR_FALSE);
4784 nsRefPtr<nsStyleContext> styleContext;
4785 styleContext = mPresShell->StyleSet()->ResolvePseudoStyleFor(aContent,
4786 nsCSSAnonBoxes::buttonContent,
4787 aStyleContext);
4789 nsIFrame* areaFrame = NS_NewAreaFrame(mPresShell, styleContext,
4790 NS_BLOCK_SPACE_MGR);
4792 if (NS_UNLIKELY(!areaFrame)) {
4793 buttonFrame->Destroy();
4794 return NS_ERROR_OUT_OF_MEMORY;
4796 rv = InitAndRestoreFrame(aState, aContent, buttonFrame, nsnull, areaFrame);
4797 if (NS_FAILED(rv)) {
4798 areaFrame->Destroy();
4799 buttonFrame->Destroy();
4800 return rv;
4803 rv = aState.AddChild(buttonFrame, aFrameItems, aContent, aStyleContext,
4804 aParentFrame);
4805 if (NS_FAILED(rv)) {
4806 areaFrame->Destroy();
4807 buttonFrame->Destroy();
4808 return rv;
4812 if (!buttonFrame->IsLeaf()) {
4813 // input type="button" have only anonymous content
4814 // The area frame is a float container
4815 PRBool haveFirstLetterStyle, haveFirstLineStyle;
4816 ShouldHaveSpecialBlockStyle(aContent, aStyleContext,
4817 &haveFirstLetterStyle, &haveFirstLineStyle);
4818 nsFrameConstructorSaveState floatSaveState;
4819 aState.PushFloatContainingBlock(areaFrame, floatSaveState,
4820 haveFirstLetterStyle,
4821 haveFirstLineStyle);
4823 // Process children
4824 nsFrameConstructorSaveState absoluteSaveState;
4825 nsFrameItems childItems;
4827 if (aStyleDisplay->IsPositioned()) {
4828 // The area frame becomes a container for child frames that are
4829 // absolutely positioned
4830 aState.PushAbsoluteContainingBlock(areaFrame, absoluteSaveState);
4833 rv = ProcessChildren(aState, aContent, areaFrame, PR_TRUE, childItems,
4834 buttonFrame->GetStyleDisplay()->IsBlockOutside());
4835 if (NS_FAILED(rv)) return rv;
4837 // Set the areas frame's initial child lists
4838 areaFrame->SetInitialChildList(nsnull, childItems.childList);
4841 buttonFrame->SetInitialChildList(nsnull, areaFrame);
4843 nsFrameItems anonymousChildItems;
4844 // if there are any anonymous children create frames for them
4845 CreateAnonymousFrames(aTag, aState, aContent, buttonFrame,
4846 PR_FALSE, anonymousChildItems);
4847 if (anonymousChildItems.childList) {
4848 // the anonymous content is already parented to the area frame
4849 aState.mFrameManager->AppendFrames(areaFrame, nsnull, anonymousChildItems.childList);
4852 // our new button frame returned is the top frame.
4853 *aNewFrame = buttonFrame;
4855 return NS_OK;
4858 nsresult
4859 nsCSSFrameConstructor::ConstructSelectFrame(nsFrameConstructorState& aState,
4860 nsIContent* aContent,
4861 nsIFrame* aParentFrame,
4862 nsIAtom* aTag,
4863 nsStyleContext* aStyleContext,
4864 nsIFrame*& aNewFrame,
4865 const nsStyleDisplay* aStyleDisplay,
4866 PRBool& aFrameHasBeenInitialized,
4867 nsFrameItems& aFrameItems)
4869 nsresult rv = NS_OK;
4870 const PRInt32 kNoSizeSpecified = -1;
4872 // Construct a frame-based listbox or combobox
4873 nsCOMPtr<nsIDOMHTMLSelectElement> sel(do_QueryInterface(aContent));
4874 PRInt32 size = 1;
4875 if (sel) {
4876 sel->GetSize(&size);
4877 PRBool multipleSelect = PR_FALSE;
4878 sel->GetMultiple(&multipleSelect);
4879 // Construct a combobox if size=1 or no size is specified and its multiple select
4880 if (((1 == size || 0 == size) || (kNoSizeSpecified == size)) && (PR_FALSE == multipleSelect)) {
4881 // Construct a frame-based combo box.
4882 // The frame-based combo box is built out of three parts. A display area, a button and
4883 // a dropdown list. The display area and button are created through anonymous content.
4884 // The drop-down list's frame is created explicitly. The combobox frame shares its content
4885 // with the drop-down list.
4886 PRUint32 flags = NS_BLOCK_SPACE_MGR;
4887 nsIFrame* comboboxFrame = NS_NewComboboxControlFrame(mPresShell, aStyleContext, flags);
4889 // Save the history state so we don't restore during construction
4890 // since the complete tree is required before we restore.
4891 nsILayoutHistoryState *historyState = aState.mFrameState;
4892 aState.mFrameState = nsnull;
4893 // Initialize the combobox frame
4894 InitAndRestoreFrame(aState, aContent,
4895 aState.GetGeometricParent(aStyleDisplay, aParentFrame),
4896 nsnull, comboboxFrame);
4898 nsHTMLContainerFrame::CreateViewForFrame(comboboxFrame, aParentFrame, PR_FALSE);
4900 rv = aState.AddChild(comboboxFrame, aFrameItems, aContent, aStyleContext,
4901 aParentFrame);
4902 if (NS_FAILED(rv)) {
4903 return rv;
4906 ///////////////////////////////////////////////////////////////////
4907 // Combobox - Old Native Implementation
4908 ///////////////////////////////////////////////////////////////////
4909 nsIComboboxControlFrame* comboBox = nsnull;
4910 CallQueryInterface(comboboxFrame, &comboBox);
4911 NS_ASSERTION(comboBox, "NS_NewComboboxControlFrame returned frame that "
4912 "doesn't implement nsIComboboxControlFrame");
4914 // Resolve pseudo element style for the dropdown list
4915 nsRefPtr<nsStyleContext> listStyle;
4916 listStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(aContent,
4917 nsCSSAnonBoxes::dropDownList,
4918 aStyleContext);
4920 // Create a listbox
4921 nsIFrame* listFrame = NS_NewListControlFrame(mPresShell, listStyle);
4923 // Notify the listbox that it is being used as a dropdown list.
4924 nsIListControlFrame * listControlFrame;
4925 rv = CallQueryInterface(listFrame, &listControlFrame);
4926 if (NS_SUCCEEDED(rv)) {
4927 listControlFrame->SetComboboxFrame(comboboxFrame);
4929 // Notify combobox that it should use the listbox as it's popup
4930 comboBox->SetDropDown(listFrame);
4932 NS_ASSERTION(!listStyle->GetStyleDisplay()->IsPositioned(),
4933 "Ended up with positioned dropdown list somehow.");
4934 NS_ASSERTION(!listStyle->GetStyleDisplay()->IsFloating(),
4935 "Ended up with floating dropdown list somehow.");
4937 // Initialize the scroll frame positioned. Note that it is NOT
4938 // initialized as absolutely positioned.
4939 nsIFrame* scrolledFrame = NS_NewSelectsAreaFrame(mPresShell, aStyleContext, flags);
4941 InitializeSelectFrame(aState, listFrame, scrolledFrame, aContent,
4942 comboboxFrame, listStyle, PR_TRUE, aFrameItems);
4944 // Set flag so the events go to the listFrame not child frames.
4945 // XXX: We should replace this with a real widget manager similar
4946 // to how the nsFormControlFrame works. Re-directing events is a temporary Kludge.
4947 NS_ASSERTION(listFrame->GetView(), "ListFrame's view is nsnull");
4948 //listFrame->GetView()->SetViewFlags(NS_VIEW_PUBLIC_FLAG_DONT_CHECK_CHILDREN);
4950 // Create display and button frames from the combobox's anonymous content.
4951 // The anonymous content is appended to existing anonymous content for this
4952 // element (the scrollbars).
4954 nsFrameItems childItems;
4955 CreateAnonymousFrames(nsGkAtoms::combobox, aState, aContent,
4956 comboboxFrame, PR_TRUE, childItems);
4958 comboboxFrame->SetInitialChildList(nsnull, childItems.childList);
4960 // Initialize the additional popup child list which contains the
4961 // dropdown list frame.
4962 nsFrameItems popupItems;
4963 popupItems.AddChild(listFrame);
4964 comboboxFrame->SetInitialChildList(nsGkAtoms::selectPopupList,
4965 popupItems.childList);
4967 aNewFrame = comboboxFrame;
4968 aFrameHasBeenInitialized = PR_TRUE;
4969 aState.mFrameState = historyState;
4970 if (aState.mFrameState && aState.mFrameManager) {
4971 // Restore frame state for the entire subtree of |comboboxFrame|.
4972 aState.mFrameManager->RestoreFrameState(comboboxFrame,
4973 aState.mFrameState);
4975 } else {
4976 ///////////////////////////////////////////////////////////////////
4977 // ListBox - Old Native Implementation
4978 ///////////////////////////////////////////////////////////////////
4979 nsIFrame* listFrame = NS_NewListControlFrame(mPresShell, aStyleContext);
4980 if (listFrame) {
4981 rv = NS_OK;
4983 else {
4984 rv = NS_ERROR_OUT_OF_MEMORY;
4987 nsIFrame* scrolledFrame = NS_NewSelectsAreaFrame(
4988 mPresShell, aStyleContext, NS_BLOCK_SPACE_MGR);
4990 // ******* this code stolen from Initialze ScrollFrame ********
4991 // please adjust this code to use BuildScrollFrame.
4993 InitializeSelectFrame(aState, listFrame, scrolledFrame, aContent,
4994 aParentFrame, aStyleContext, PR_FALSE, aFrameItems);
4996 aNewFrame = listFrame;
4998 aFrameHasBeenInitialized = PR_TRUE;
5001 return rv;
5006 * Used to be InitializeScrollFrame but now it's only used for the select tag
5007 * But the select tag should really be fixed to use GFX scrollbars that can
5008 * be create with BuildScrollFrame.
5010 nsresult
5011 nsCSSFrameConstructor::InitializeSelectFrame(nsFrameConstructorState& aState,
5012 nsIFrame* scrollFrame,
5013 nsIFrame* scrolledFrame,
5014 nsIContent* aContent,
5015 nsIFrame* aParentFrame,
5016 nsStyleContext* aStyleContext,
5017 PRBool aBuildCombobox,
5018 nsFrameItems& aFrameItems)
5020 const nsStyleDisplay* display = aStyleContext->GetStyleDisplay();
5022 // Initialize it
5023 nsIFrame* geometricParent = aState.GetGeometricParent(display, aParentFrame);
5025 // We don't call InitAndRestoreFrame for scrollFrame because we can only
5026 // restore the frame state after its parts have been created (in particular,
5027 // the scrollable view). So we have to split Init and Restore.
5029 // Initialize the frame
5030 scrollFrame->Init(aContent, geometricParent, nsnull);
5032 if (!aBuildCombobox) {
5033 nsresult rv = aState.AddChild(scrollFrame, aFrameItems, aContent,
5034 aStyleContext, aParentFrame);
5035 if (NS_FAILED(rv)) {
5036 return rv;
5040 nsHTMLContainerFrame::CreateViewForFrame(scrollFrame, aParentFrame,
5041 aBuildCombobox);
5042 if (aBuildCombobox) {
5043 // Give the drop-down list a popup widget
5044 nsIView* view = scrollFrame->GetView();
5045 NS_ASSERTION(view, "We asked for a view but didn't get one");
5046 if (view) {
5047 view->GetViewManager()->SetViewFloating(view, PR_TRUE);
5049 nsWidgetInitData widgetData;
5050 widgetData.mWindowType = eWindowType_popup;
5051 widgetData.mBorderStyle = eBorderStyle_default;
5053 #if defined(XP_MACOSX) || defined(XP_BEOS)
5054 static NS_DEFINE_IID(kCPopUpCID, NS_POPUP_CID);
5055 view->CreateWidget(kCPopUpCID, &widgetData, nsnull);
5056 #else
5057 static NS_DEFINE_IID(kCChildCID, NS_CHILD_CID);
5058 view->CreateWidget(kCChildCID, &widgetData, nsnull);
5059 #endif
5063 nsStyleContext* scrolledPseudoStyle;
5064 BuildScrollFrame(aState, aContent, aStyleContext, scrolledFrame,
5065 geometricParent, aParentFrame, scrollFrame,
5066 scrolledPseudoStyle);
5068 if (aState.mFrameState && aState.mFrameManager) {
5069 // Restore frame state for the scroll frame
5070 aState.mFrameManager->RestoreFrameStateFor(scrollFrame, aState.mFrameState);
5073 // The area frame is a float container
5074 PRBool haveFirstLetterStyle, haveFirstLineStyle;
5075 ShouldHaveSpecialBlockStyle(aContent, aStyleContext,
5076 &haveFirstLetterStyle, &haveFirstLineStyle);
5077 nsFrameConstructorSaveState floatSaveState;
5078 aState.PushFloatContainingBlock(scrolledFrame, floatSaveState,
5079 haveFirstLetterStyle, haveFirstLineStyle);
5081 // Process children
5082 nsFrameConstructorSaveState absoluteSaveState;
5083 nsFrameItems childItems;
5085 if (display->IsPositioned()) {
5086 // The area frame becomes a container for child frames that are
5087 // absolutely positioned
5088 aState.PushAbsoluteContainingBlock(scrolledFrame, absoluteSaveState);
5091 ProcessChildren(aState, aContent, scrolledFrame, PR_FALSE,
5092 childItems, PR_TRUE);
5094 // Set the scrolled frame's initial child lists
5095 scrolledFrame->SetInitialChildList(nsnull, childItems.childList);
5096 return NS_OK;
5099 nsresult
5100 nsCSSFrameConstructor::ConstructFieldSetFrame(nsFrameConstructorState& aState,
5101 nsIContent* aContent,
5102 nsIFrame* aParentFrame,
5103 nsIAtom* aTag,
5104 nsStyleContext* aStyleContext,
5105 nsIFrame*& aNewFrame,
5106 nsFrameItems& aFrameItems,
5107 const nsStyleDisplay* aStyleDisplay,
5108 PRBool& aFrameHasBeenInitialized)
5110 nsIFrame* newFrame = NS_NewFieldSetFrame(mPresShell, aStyleContext);
5111 if (NS_UNLIKELY(!newFrame)) {
5112 return NS_ERROR_OUT_OF_MEMORY;
5115 // Initialize it
5116 InitAndRestoreFrame(aState, aContent,
5117 aState.GetGeometricParent(aStyleDisplay, aParentFrame),
5118 nsnull, newFrame);
5120 // See if we need to create a view, e.g. the frame is absolutely
5121 // positioned
5122 nsHTMLContainerFrame::CreateViewForFrame(newFrame, aParentFrame, PR_FALSE);
5124 // Resolve style and initialize the frame
5125 nsRefPtr<nsStyleContext> styleContext;
5126 styleContext = mPresShell->StyleSet()->ResolvePseudoStyleFor(aContent,
5127 nsCSSAnonBoxes::fieldsetContent,
5128 aStyleContext);
5130 nsIFrame* areaFrame = NS_NewAreaFrame(mPresShell, styleContext,
5131 NS_BLOCK_SPACE_MGR | NS_BLOCK_MARGIN_ROOT);
5132 InitAndRestoreFrame(aState, aContent, newFrame, nsnull, areaFrame);
5134 nsresult rv = aState.AddChild(newFrame, aFrameItems, aContent, aStyleContext,
5135 aParentFrame);
5136 if (NS_FAILED(rv)) {
5137 return rv;
5141 // The area frame is a float container
5142 PRBool haveFirstLetterStyle, haveFirstLineStyle;
5143 ShouldHaveSpecialBlockStyle(aContent, aStyleContext,
5144 &haveFirstLetterStyle, &haveFirstLineStyle);
5145 nsFrameConstructorSaveState floatSaveState;
5146 aState.PushFloatContainingBlock(areaFrame, floatSaveState,
5147 haveFirstLetterStyle,
5148 haveFirstLineStyle);
5150 // Process children
5151 nsFrameConstructorSaveState absoluteSaveState;
5152 nsFrameItems childItems;
5154 if (aStyleDisplay->IsPositioned()) {
5155 // The area frame becomes a container for child frames that are
5156 // absolutely positioned
5157 aState.PushAbsoluteContainingBlock(areaFrame, absoluteSaveState);
5160 ProcessChildren(aState, aContent, areaFrame, PR_TRUE,
5161 childItems, PR_TRUE);
5163 static NS_DEFINE_IID(kLegendFrameCID, NS_LEGEND_FRAME_CID);
5164 nsIFrame * child = childItems.childList;
5165 nsIFrame * previous = nsnull;
5166 nsIFrame* legendFrame = nsnull;
5167 while (nsnull != child) {
5168 nsresult result = child->QueryInterface(kLegendFrameCID, (void**)&legendFrame);
5169 if (NS_SUCCEEDED(result) && legendFrame) {
5170 // We want the legend to be the first frame in the fieldset child list.
5171 // That way the EventStateManager will do the right thing when tabbing
5172 // from a selection point within the legend (bug 236071), which is
5173 // used for implementing legend access keys (bug 81481).
5174 // GetAdjustedParentFrame() below depends on this frame order.
5175 if (nsnull != previous) {
5176 previous->SetNextSibling(legendFrame->GetNextSibling());
5177 } else {
5178 childItems.childList = legendFrame->GetNextSibling();
5180 legendFrame->SetNextSibling(areaFrame);
5181 legendFrame->SetParent(newFrame);
5182 break;
5184 previous = child;
5185 child = child->GetNextSibling();
5188 // Set the scrolled frame's initial child lists
5189 areaFrame->SetInitialChildList(nsnull, childItems.childList);
5191 // Set the scroll frame's initial child list
5192 newFrame->SetInitialChildList(nsnull, legendFrame ? legendFrame : areaFrame);
5194 // our new frame returned is the top frame which is the list frame.
5195 aNewFrame = newFrame;
5197 // yes we have already initialized our frame
5198 aFrameHasBeenInitialized = PR_TRUE;
5200 return NS_OK;
5203 static nsIFrame*
5204 FindAncestorWithGeneratedContentPseudo(nsIFrame* aFrame)
5206 for (nsIFrame* f = aFrame->GetParent(); f; f = f->GetParent()) {
5207 NS_ASSERTION(f->IsGeneratedContentFrame(),
5208 "should not have exited generated content");
5209 nsIAtom* pseudo = f->GetStyleContext()->GetPseudoType();
5210 if (pseudo == nsCSSPseudoElements::before ||
5211 pseudo == nsCSSPseudoElements::after)
5212 return f;
5214 return nsnull;
5217 nsresult
5218 nsCSSFrameConstructor::ConstructTextFrame(nsFrameConstructorState& aState,
5219 nsIContent* aContent,
5220 nsIFrame* aParentFrame,
5221 nsStyleContext* aStyleContext,
5222 nsFrameItems& aFrameItems,
5223 PRBool aPseudoParent)
5225 // process pending pseudo frames. whitespace doesn't have an effect.
5226 if (!aPseudoParent && !aState.mPseudoFrames.IsEmpty() &&
5227 !TextIsOnlyWhitespace(aContent))
5228 ProcessPseudoFrames(aState, aFrameItems);
5230 nsIFrame* newFrame = nsnull;
5232 #ifdef MOZ_SVG
5233 if (aParentFrame->IsFrameOfType(nsIFrame::eSVG)) {
5234 nsIFrame *ancestorFrame = SVG_GetFirstNonAAncestorFrame(aParentFrame);
5235 if (ancestorFrame) {
5236 nsISVGTextContentMetrics* metrics;
5237 CallQueryInterface(ancestorFrame, &metrics);
5238 if (!metrics) {
5239 return NS_OK;
5241 newFrame = NS_NewSVGGlyphFrame(mPresShell, aContent,
5242 ancestorFrame, aStyleContext);
5245 else
5246 #endif
5248 newFrame = NS_NewTextFrame(mPresShell, aStyleContext);
5251 if (NS_UNLIKELY(!newFrame))
5252 return NS_ERROR_OUT_OF_MEMORY;
5254 nsresult rv = InitAndRestoreFrame(aState, aContent, aParentFrame,
5255 nsnull, newFrame);
5257 if (NS_FAILED(rv)) {
5258 newFrame->Destroy();
5259 return rv;
5262 // We never need to create a view for a text frame.
5264 if (newFrame->IsGeneratedContentFrame()) {
5265 nsAutoPtr<nsGenConInitializer> initializer;
5266 initializer =
5267 static_cast<nsGenConInitializer*>(
5268 aContent->UnsetProperty(nsGkAtoms::genConInitializerProperty));
5269 if (initializer) {
5270 if (initializer->mNode->InitTextFrame(initializer->mList,
5271 FindAncestorWithGeneratedContentPseudo(newFrame), newFrame)) {
5272 (this->*(initializer->mDirtyAll))();
5274 initializer->mNode.forget();
5278 // Add the newly constructed frame to the flow
5279 aFrameItems.AddChild(newFrame);
5281 // Text frames don't go in the content->frame hash table, because
5282 // they're anonymous. This keeps the hash table smaller
5284 return rv;
5287 nsresult
5288 nsCSSFrameConstructor::ConstructHTMLFrame(nsFrameConstructorState& aState,
5289 nsIContent* aContent,
5290 nsIFrame* aParentFrame,
5291 nsIAtom* aTag,
5292 PRInt32 aNameSpaceID,
5293 nsStyleContext* aStyleContext,
5294 nsFrameItems& aFrameItems,
5295 PRBool aHasPseudoParent)
5297 // Ignore the tag if it's not HTML content and if it doesn't extend (via XBL)
5298 // a valid HTML namespace. This check must match the one in
5299 // ShouldHaveFirstLineStyle.
5300 if (!aContent->IsNodeOfType(nsINode::eHTML) &&
5301 aNameSpaceID != kNameSpaceID_XHTML) {
5302 return NS_OK;
5305 PRBool frameHasBeenInitialized = PR_FALSE;
5306 nsIFrame* newFrame = nsnull; // the frame we construct
5307 PRBool addToHashTable = PR_TRUE;
5308 PRBool isFloatContainer = PR_FALSE;
5309 PRBool addedToFrameList = PR_FALSE;
5310 nsresult rv = NS_OK;
5312 PRBool triedFrame = PR_FALSE;
5314 // See if the element is absolute or fixed positioned
5315 const nsStyleDisplay* display = aStyleContext->GetStyleDisplay();
5317 // Create a frame based on the tag
5318 if (nsGkAtoms::img == aTag || nsGkAtoms::mozgeneratedcontentimage == aTag) {
5319 // Make sure to keep IsSpecialContent in synch with this code
5320 rv = CreateHTMLImageFrame(aContent, aStyleContext, NS_NewImageFrame,
5321 &newFrame);
5322 if (newFrame) {
5323 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5324 ProcessPseudoFrames(aState, aFrameItems);
5328 else if (nsGkAtoms::br == aTag) {
5329 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5330 ProcessPseudoFrames(aState, aFrameItems);
5332 newFrame = NS_NewBRFrame(mPresShell, aStyleContext);
5333 triedFrame = PR_TRUE;
5335 // BR frames don't go in the content->frame hash table: typically
5336 // there are many BR content objects and this would increase the size
5337 // of the hash table, and it's doubtful we need the mapping anyway
5338 addToHashTable = PR_FALSE;
5340 else if (nsGkAtoms::wbr == aTag) {
5341 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5342 ProcessPseudoFrames(aState, aFrameItems);
5344 newFrame = NS_NewWBRFrame(mPresShell, aStyleContext);
5345 triedFrame = PR_TRUE;
5347 else if (nsGkAtoms::input == aTag) {
5348 // Make sure to keep IsSpecialContent in synch with this code
5349 rv = CreateInputFrame(aState, aContent, aParentFrame,
5350 aTag, aStyleContext, &newFrame,
5351 display, frameHasBeenInitialized,
5352 addedToFrameList, aFrameItems,
5353 aHasPseudoParent);
5354 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty() &&
5355 newFrame && !addedToFrameList) {
5356 // We'll still be adding this new frame, and it's a replaced
5357 // element, so process pseudo-frames now.
5358 ProcessPseudoFrames(aState, aFrameItems);
5361 else if (nsGkAtoms::textarea == aTag) {
5362 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5363 ProcessPseudoFrames(aState, aFrameItems);
5365 newFrame = NS_NewTextControlFrame(mPresShell, aStyleContext);
5366 triedFrame = PR_TRUE;
5368 else if (nsGkAtoms::select == aTag) {
5369 if (!gUseXBLForms) {
5370 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5371 ProcessPseudoFrames(aState, aFrameItems);
5373 rv = ConstructSelectFrame(aState, aContent, aParentFrame,
5374 aTag, aStyleContext, newFrame,
5375 display, frameHasBeenInitialized,
5376 aFrameItems);
5377 if (newFrame) {
5378 NS_ASSERTION(nsPlaceholderFrame::GetRealFrameFor(aFrameItems.lastChild) ==
5379 newFrame,
5380 "Frame didn't get added to aFrameItems?");
5381 addedToFrameList = PR_TRUE;
5385 else if (nsGkAtoms::object == aTag ||
5386 nsGkAtoms::applet == aTag ||
5387 nsGkAtoms::embed == aTag) {
5388 // Make sure to keep IsSpecialContent in synch with this code
5389 if (!(aContent->IntrinsicState() &
5390 (NS_EVENT_STATE_BROKEN | NS_EVENT_STATE_USERDISABLED |
5391 NS_EVENT_STATE_SUPPRESSED))) {
5392 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5393 ProcessPseudoFrames(aState, aFrameItems);
5396 nsCOMPtr<nsIObjectLoadingContent> objContent(do_QueryInterface(aContent));
5397 NS_ASSERTION(objContent,
5398 "applet, embed and object must implement nsIObjectLoadingContent!");
5399 if (!objContent) {
5400 // XBL might trigger this...
5401 return NS_ERROR_UNEXPECTED;
5404 PRUint32 type;
5405 objContent->GetDisplayedType(&type);
5406 if (type == nsIObjectLoadingContent::TYPE_LOADING) {
5407 // Ideally, this should show the standby attribute
5408 // XXX Should we return something that is replaced, or make
5409 // nsFrame replaced but not its subclasses?
5410 newFrame = NS_NewEmptyFrame(mPresShell, aStyleContext);
5412 else if (type == nsIObjectLoadingContent::TYPE_PLUGIN)
5413 newFrame = NS_NewObjectFrame(mPresShell, aStyleContext);
5414 else if (type == nsIObjectLoadingContent::TYPE_IMAGE)
5415 newFrame = NS_NewImageFrame(mPresShell, aStyleContext);
5416 else if (type == nsIObjectLoadingContent::TYPE_DOCUMENT)
5417 newFrame = NS_NewSubDocumentFrame(mPresShell, aStyleContext);
5418 #ifdef DEBUG
5419 else
5420 NS_ERROR("Shouldn't get here if we're not broken and not "
5421 "suppressed and not blocked");
5422 #endif
5424 triedFrame = PR_TRUE;
5427 else if (nsGkAtoms::fieldset == aTag) {
5428 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5429 ProcessPseudoFrames(aState, aFrameItems);
5431 rv = ConstructFieldSetFrame(aState, aContent, aParentFrame,
5432 aTag, aStyleContext, newFrame,
5433 aFrameItems, display, frameHasBeenInitialized);
5434 NS_ASSERTION(nsPlaceholderFrame::GetRealFrameFor(aFrameItems.lastChild) ==
5435 newFrame,
5436 "Frame didn't get added to aFrameItems?");
5437 addedToFrameList = PR_TRUE;
5439 else if (nsGkAtoms::legend == aTag) {
5440 NS_ASSERTION(!display->IsAbsolutelyPositioned() && !display->IsFloating(),
5441 "Legends should not be positioned and should not float");
5443 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5444 ProcessPseudoFrames(aState, aFrameItems);
5446 newFrame = NS_NewLegendFrame(mPresShell, aStyleContext);
5447 triedFrame = PR_TRUE;
5449 isFloatContainer = PR_TRUE;
5451 else if (nsGkAtoms::frameset == aTag) {
5452 NS_ASSERTION(!display->IsAbsolutelyPositioned() && !display->IsFloating(),
5453 "Framesets should not be positioned and should not float");
5455 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5456 ProcessPseudoFrames(aState, aFrameItems);
5459 newFrame = NS_NewHTMLFramesetFrame(mPresShell, aStyleContext);
5460 triedFrame = PR_TRUE;
5462 else if (nsGkAtoms::iframe == aTag) {
5463 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5464 ProcessPseudoFrames(aState, aFrameItems);
5467 newFrame = NS_NewSubDocumentFrame(mPresShell, aStyleContext);
5468 triedFrame = PR_TRUE;
5470 if (newFrame) {
5471 // the nsSubDocumentFrame needs to know about its content parent during ::Init.
5472 // there is no reasonable way to get the value there.
5473 // so we store it as a frame property.
5474 nsCOMPtr<nsIAtom> contentParentAtom = do_GetAtom("contentParent");
5475 aState.mPresContext->PropertyTable()->
5476 SetProperty(newFrame, contentParentAtom,
5477 aParentFrame, nsnull, nsnull);
5480 else if (nsGkAtoms::spacer == aTag) {
5481 NS_ASSERTION(!display->IsAbsolutelyPositioned() && !display->IsFloating(),
5482 "Spacers should not be positioned and should not float");
5483 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5484 ProcessPseudoFrames(aState, aFrameItems);
5486 newFrame = NS_NewSpacerFrame(mPresShell, aStyleContext);
5487 triedFrame = PR_TRUE;
5489 else if (nsGkAtoms::button == aTag) {
5490 rv = ConstructButtonFrame(aState, aContent, aParentFrame,
5491 aTag, aStyleContext, &newFrame,
5492 display, aFrameItems, aHasPseudoParent);
5493 // the html4 button needs to act just like a
5494 // regular button except contain html content
5495 // so it must be replaced or html outside it will
5496 // draw into its borders. -EDV
5497 frameHasBeenInitialized = PR_TRUE;
5498 addedToFrameList = PR_TRUE;
5499 isFloatContainer = PR_TRUE;
5501 else if (nsGkAtoms::isindex == aTag) {
5502 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5503 ProcessPseudoFrames(aState, aFrameItems);
5505 newFrame = NS_NewIsIndexFrame(mPresShell, aStyleContext);
5506 triedFrame = PR_TRUE;
5508 else if (nsGkAtoms::canvas == aTag) {
5509 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5510 ProcessPseudoFrames(aState, aFrameItems);
5512 newFrame = NS_NewHTMLCanvasFrame(mPresShell, aStyleContext);
5513 triedFrame = PR_TRUE;
5515 #if defined(MOZ_MEDIA)
5516 else if (nsGkAtoms::video == aTag) {
5517 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
5518 ProcessPseudoFrames(aState, aFrameItems);
5520 newFrame = NS_NewHTMLVideoFrame(mPresShell, aStyleContext);
5521 triedFrame = PR_TRUE;
5523 #endif
5524 if (NS_UNLIKELY(triedFrame && !newFrame)) {
5525 return NS_ERROR_OUT_OF_MEMORY;
5527 else if (NS_FAILED(rv) || !newFrame) {
5528 return rv;
5531 // If we succeeded in creating a frame then initialize it, process its
5532 // children (if requested), and set the initial child list
5534 // Note: at this point we should construct kids for newFrame only if
5535 // it's not a leaf and hasn't been initialized yet.
5537 if (!frameHasBeenInitialized) {
5538 NS_ASSERTION(!addedToFrameList,
5539 "Frames that were already added to the frame list should be "
5540 "initialized by now!");
5541 nsIFrame* geometricParent = aState.GetGeometricParent(display,
5542 aParentFrame);
5544 rv = InitAndRestoreFrame(aState, aContent, geometricParent, nsnull, newFrame);
5545 NS_ASSERTION(NS_SUCCEEDED(rv), "InitAndRestoreFrame failed");
5546 // See if we need to create a view, e.g. the frame is absolutely
5547 // positioned
5548 nsHTMLContainerFrame::CreateViewForFrame(newFrame, aParentFrame, PR_FALSE);
5550 rv = aState.AddChild(newFrame, aFrameItems, aContent, aStyleContext,
5551 aParentFrame);
5552 if (NS_FAILED(rv)) {
5553 return rv;
5555 addedToFrameList = PR_TRUE;
5557 // Process the child content if requested
5558 nsFrameItems childItems;
5559 nsFrameConstructorSaveState absoluteSaveState;
5560 nsFrameConstructorSaveState floatSaveState;
5561 if (!newFrame->IsLeaf()) {
5562 if (display->IsPositioned()) {
5563 aState.PushAbsoluteContainingBlock(newFrame, absoluteSaveState);
5565 if (isFloatContainer) {
5566 PRBool haveFirstLetterStyle, haveFirstLineStyle;
5567 ShouldHaveSpecialBlockStyle(aContent, aStyleContext,
5568 &haveFirstLetterStyle,
5569 &haveFirstLineStyle);
5570 aState.PushFloatContainingBlock(newFrame, floatSaveState,
5571 PR_FALSE, PR_FALSE);
5574 // Process the child frames
5575 rv = ProcessChildren(aState, aContent, newFrame,
5576 PR_TRUE, childItems, PR_FALSE);
5579 // if there are any anonymous children create frames for them
5580 CreateAnonymousFrames(aTag, aState, aContent, newFrame,
5581 PR_FALSE, childItems);
5583 // Set the frame's initial child list
5584 if (childItems.childList) {
5585 newFrame->SetInitialChildList(nsnull, childItems.childList);
5589 if (!addedToFrameList) {
5590 // Gotta do it here. Note that things like absolutely positioned replaced
5591 // elements and the like will end up in this code. So use the AddChild
5592 // on the state.
5593 rv = aState.AddChild(newFrame, aFrameItems, aContent, aStyleContext,
5594 aParentFrame);
5595 if (NS_FAILED(rv)) {
5596 return rv;
5600 if (addToHashTable) {
5601 // Add a mapping from content object to primary frame. Note that for
5602 // floated and positioned frames this is the out-of-flow frame and not
5603 // the placeholder frame
5604 aState.mFrameManager->SetPrimaryFrameFor(aContent, newFrame);
5607 return rv;
5610 nsresult
5611 nsCSSFrameConstructor::CreateAnonymousFrames(nsIAtom* aTag,
5612 nsFrameConstructorState& aState,
5613 nsIContent* aParent,
5614 nsIFrame* aNewFrame,
5615 PRBool aAppendToExisting,
5616 nsFrameItems& aChildItems,
5617 PRBool aIsRoot)
5619 // See if we might have anonymous content
5620 // by looking at the tag rather than doing a QueryInterface on
5621 // the frame. Only these tags' frames can have anonymous content
5622 // through nsIAnonymousContentCreator. We do this check for
5623 // performance reasons. If we did a QueryInterface on every tag it
5624 // would be inefficient.
5626 // nsGenericElement::SetDocument ought to keep a list like this one,
5627 // but it can't because scroll frames get around this.
5628 if (!aIsRoot &&
5629 aTag != nsGkAtoms::input &&
5630 aTag != nsGkAtoms::textarea &&
5631 aTag != nsGkAtoms::combobox &&
5632 aTag != nsGkAtoms::isindex &&
5633 aTag != nsGkAtoms::scrollbar
5634 #ifdef MOZ_SVG
5635 && aTag != nsGkAtoms::use
5636 #endif
5637 #ifdef MOZ_MEDIA
5638 && aTag != nsGkAtoms::video
5639 #endif
5641 return NS_OK;
5643 return CreateAnonymousFrames(aState, aParent, mDocument, aNewFrame,
5644 aAppendToExisting, aChildItems);
5647 // after the node has been constructed and initialized create any
5648 // anonymous content a node needs.
5649 nsresult
5650 nsCSSFrameConstructor::CreateAnonymousFrames(nsFrameConstructorState& aState,
5651 nsIContent* aParent,
5652 nsIDocument* aDocument,
5653 nsIFrame* aParentFrame,
5654 PRBool aAppendToExisting,
5655 nsFrameItems& aChildItems)
5657 nsIAnonymousContentCreator* creator = nsnull;
5658 CallQueryInterface(aParentFrame, &creator);
5659 if (!creator)
5660 return NS_OK;
5662 nsresult rv;
5664 nsAutoTArray<nsIContent*, 4> newAnonymousItems;
5665 rv = creator->CreateAnonymousContent(newAnonymousItems);
5666 NS_ENSURE_SUCCESS(rv, rv);
5668 PRUint32 count = newAnonymousItems.Length();
5669 if (count == 0) {
5670 return NS_OK;
5673 // save the incoming pseudo frame state, so that we don't end up
5674 // with those pseudoframes in aChildItems
5675 nsPseudoFrames priorPseudoFrames;
5676 aState.mPseudoFrames.Reset(&priorPseudoFrames);
5678 for (PRUint32 i=0; i < count; i++) {
5679 // get our child's content and set its parent to our content
5680 nsIContent* content = newAnonymousItems[i];
5681 NS_ASSERTION(content, "null anonymous content?");
5683 #ifdef MOZ_SVG
5684 // least-surprise CSS binding until we do the SVG specified
5685 // cascading rules for <svg:use> - bug 265894
5686 if (!aParent ||
5687 !aParent->NodeInfo()->Equals(nsGkAtoms::use, kNameSpaceID_SVG))
5688 #endif
5690 content->SetNativeAnonymous();
5693 rv = content->BindToTree(aDocument, aParent, aParent, PR_TRUE);
5694 if (NS_FAILED(rv)) {
5695 content->UnbindFromTree();
5696 return rv;
5699 nsIFrame* newFrame = creator->CreateFrameFor(content);
5700 if (newFrame) {
5701 aChildItems.AddChild(newFrame);
5703 else {
5704 // create the frame and attach it to our frame
5705 ConstructFrame(aState, content, aParentFrame, aChildItems);
5709 creator->PostCreateFrames();
5711 // process the current pseudo frame state
5712 if (!aState.mPseudoFrames.IsEmpty()) {
5713 ProcessPseudoFrames(aState, aChildItems);
5716 // restore the incoming pseudo frame state
5717 aState.mPseudoFrames = priorPseudoFrames;
5719 return NS_OK;
5722 static
5723 PRBool IsXULDisplayType(const nsStyleDisplay* aDisplay)
5725 return (aDisplay->mDisplay == NS_STYLE_DISPLAY_INLINE_BOX ||
5726 aDisplay->mDisplay == NS_STYLE_DISPLAY_INLINE_GRID ||
5727 aDisplay->mDisplay == NS_STYLE_DISPLAY_INLINE_STACK ||
5728 aDisplay->mDisplay == NS_STYLE_DISPLAY_BOX ||
5729 aDisplay->mDisplay == NS_STYLE_DISPLAY_GRID ||
5730 aDisplay->mDisplay == NS_STYLE_DISPLAY_STACK ||
5731 aDisplay->mDisplay == NS_STYLE_DISPLAY_GRID_GROUP ||
5732 aDisplay->mDisplay == NS_STYLE_DISPLAY_GRID_LINE ||
5733 aDisplay->mDisplay == NS_STYLE_DISPLAY_DECK ||
5734 aDisplay->mDisplay == NS_STYLE_DISPLAY_POPUP ||
5735 aDisplay->mDisplay == NS_STYLE_DISPLAY_GROUPBOX
5739 nsresult
5740 nsCSSFrameConstructor::ConstructXULFrame(nsFrameConstructorState& aState,
5741 nsIContent* aContent,
5742 nsIFrame* aParentFrame,
5743 nsIAtom* aTag,
5744 PRInt32 aNameSpaceID,
5745 nsStyleContext* aStyleContext,
5746 nsFrameItems& aFrameItems,
5747 PRBool aXBLBaseTag,
5748 PRBool aHasPseudoParent,
5749 PRBool* aHaltProcessing)
5751 *aHaltProcessing = PR_FALSE;
5753 PRBool primaryFrameSet = PR_FALSE;
5754 nsresult rv = NS_OK;
5755 PRBool isPopup = PR_FALSE;
5756 PRBool frameHasBeenInitialized = PR_FALSE;
5758 // XXXbz somewhere here we should process pseudo frames if !aHasPseudoParent
5760 // this is the new frame that will be created
5761 nsIFrame* newFrame = nsnull;
5763 // this is the also the new frame that is created. But if a scroll frame is needed
5764 // the content will be mapped to the scrollframe and topFrame will point to it.
5765 // newFrame will still point to the child that we created like a "div" for example.
5766 nsIFrame* topFrame = nsnull;
5768 // Store aParentFrame away, since we plan to stomp on it later
5769 nsIFrame* origParentFrame = aParentFrame;
5771 NS_ASSERTION(aTag != nsnull, "null XUL tag");
5772 if (aTag == nsnull)
5773 return NS_OK;
5775 const nsStyleDisplay* display = aStyleContext->GetStyleDisplay();
5777 PRBool isXULNS = (aNameSpaceID == kNameSpaceID_XUL);
5778 PRBool isXULDisplay = IsXULDisplayType(display);
5780 // don't apply xul display types to tag based frames
5781 if (isXULDisplay && !isXULNS) {
5782 isXULDisplay = !IsSpecialContent(aContent, aTag, aNameSpaceID, aStyleContext);
5785 PRBool triedFrame = PR_FALSE;
5787 if (isXULNS || isXULDisplay) {
5788 PRBool mayBeScrollable = PR_FALSE;
5790 if (isXULNS) {
5791 triedFrame = PR_TRUE;
5793 // First try creating a frame based on the tag
5794 // Make sure to keep IsSpecialContent in synch with this code
5795 #ifdef MOZ_XUL
5796 // BUTTON CONSTRUCTION
5797 if (aTag == nsGkAtoms::button || aTag == nsGkAtoms::checkbox || aTag == nsGkAtoms::radio) {
5798 newFrame = NS_NewButtonBoxFrame(mPresShell, aStyleContext);
5800 // Boxes can scroll.
5801 mayBeScrollable = PR_TRUE;
5802 } // End of BUTTON CONSTRUCTION logic
5803 // AUTOREPEATBUTTON CONSTRUCTION
5804 else if (aTag == nsGkAtoms::autorepeatbutton) {
5805 newFrame = NS_NewAutoRepeatBoxFrame(mPresShell, aStyleContext);
5807 // Boxes can scroll.
5808 mayBeScrollable = PR_TRUE;
5809 } // End of AUTOREPEATBUTTON CONSTRUCTION logic
5811 // TITLEBAR CONSTRUCTION
5812 else if (aTag == nsGkAtoms::titlebar) {
5813 newFrame = NS_NewTitleBarFrame(mPresShell, aStyleContext);
5815 // Boxes can scroll.
5816 mayBeScrollable = PR_TRUE;
5817 } // End of TITLEBAR CONSTRUCTION logic
5819 // RESIZER CONSTRUCTION
5820 else if (aTag == nsGkAtoms::resizer) {
5821 newFrame = NS_NewResizerFrame(mPresShell, aStyleContext);
5823 // Boxes can scroll.
5824 mayBeScrollable = PR_TRUE;
5825 } // End of RESIZER CONSTRUCTION logic
5827 else if (aTag == nsGkAtoms::image) {
5828 newFrame = NS_NewImageBoxFrame(mPresShell, aStyleContext);
5830 else if (aTag == nsGkAtoms::spring ||
5831 aTag == nsGkAtoms::spacer) {
5832 newFrame = NS_NewLeafBoxFrame(mPresShell, aStyleContext);
5834 else if (aTag == nsGkAtoms::treechildren) {
5835 newFrame = NS_NewTreeBodyFrame(mPresShell, aStyleContext);
5837 else if (aTag == nsGkAtoms::treecol) {
5838 newFrame = NS_NewTreeColFrame(mPresShell, aStyleContext);
5840 // TEXT CONSTRUCTION
5841 else if (aTag == nsGkAtoms::text || aTag == nsGkAtoms::label ||
5842 aTag == nsGkAtoms::description) {
5843 if ((aTag == nsGkAtoms::label || aTag == nsGkAtoms::description) &&
5844 (! aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::value))) {
5845 // XXX we should probably be calling ConstructBlock here to handle
5846 // things like columns etc
5847 newFrame = NS_NewAreaFrame(mPresShell, aStyleContext,
5848 NS_BLOCK_SPACE_MGR | NS_BLOCK_MARGIN_ROOT);
5850 else {
5851 newFrame = NS_NewTextBoxFrame(mPresShell, aStyleContext);
5854 // End of TEXT CONSTRUCTION logic
5856 // Menu Construction
5857 else if (aTag == nsGkAtoms::menu ||
5858 aTag == nsGkAtoms::menuitem ||
5859 aTag == nsGkAtoms::menubutton) {
5860 // A derived class box frame
5861 // that has custom reflow to prevent menu children
5862 // from becoming part of the flow.
5863 newFrame = NS_NewMenuFrame(mPresShell, aStyleContext,
5864 (aTag != nsGkAtoms::menuitem));
5866 else if (aTag == nsGkAtoms::menubar) {
5867 #ifdef XP_MACOSX
5868 // On Mac OS X, we use the system menubar for any root chrome shell
5869 // XUL menubars.
5870 PRBool isRootChromeShell = PR_FALSE;
5871 nsCOMPtr<nsISupports> container = aState.mPresContext->GetContainer();
5872 if (container) {
5873 nsCOMPtr<nsIDocShellTreeItem> treeItem(do_QueryInterface(container));
5874 if (treeItem) {
5875 PRInt32 type;
5876 treeItem->GetItemType(&type);
5877 if (nsIDocShellTreeItem::typeChrome == type) {
5878 nsCOMPtr<nsIDocShellTreeItem> parent;
5879 treeItem->GetParent(getter_AddRefs(parent));
5880 isRootChromeShell = !parent;
5885 if (isRootChromeShell) {
5886 *aHaltProcessing = PR_TRUE;
5887 return NS_OK;
5889 #endif
5891 newFrame = NS_NewMenuBarFrame(mPresShell, aStyleContext);
5893 else if (aTag == nsGkAtoms::popupgroup &&
5894 aContent->IsRootOfNativeAnonymousSubtree()) {
5895 // This frame contains child popups
5896 newFrame = NS_NewPopupSetFrame(mPresShell, aStyleContext);
5898 else if (aTag == nsGkAtoms::iframe || aTag == nsGkAtoms::editor ||
5899 aTag == nsGkAtoms::browser) {
5900 newFrame = NS_NewSubDocumentFrame(mPresShell, aStyleContext);
5902 // PROGRESS METER CONSTRUCTION
5903 else if (aTag == nsGkAtoms::progressmeter) {
5904 newFrame = NS_NewProgressMeterFrame(mPresShell, aStyleContext);
5906 // End of PROGRESS METER CONSTRUCTION logic
5907 else
5908 #endif
5909 // SLIDER CONSTRUCTION
5910 if (aTag == nsGkAtoms::slider) {
5911 newFrame = NS_NewSliderFrame(mPresShell, aStyleContext);
5913 // End of SLIDER CONSTRUCTION logic
5915 // SCROLLBAR CONSTRUCTION
5916 else if (aTag == nsGkAtoms::scrollbar) {
5917 newFrame = NS_NewScrollbarFrame(mPresShell, aStyleContext);
5919 // End of SCROLLBAR CONSTRUCTION logic
5921 // SCROLLBUTTON CONSTRUCTION
5922 else if (aTag == nsGkAtoms::scrollbarbutton) {
5923 newFrame = NS_NewScrollbarButtonFrame(mPresShell, aStyleContext);
5925 // End of SCROLLBUTTON CONSTRUCTION logic
5927 #ifdef MOZ_XUL
5928 // SPLITTER CONSTRUCTION
5929 else if (aTag == nsGkAtoms::splitter) {
5930 newFrame = NS_NewSplitterFrame(mPresShell, aStyleContext);
5932 // End of SPLITTER CONSTRUCTION logic
5934 else {
5935 triedFrame = PR_FALSE;
5937 #endif
5940 // Display types for XUL start here
5941 // Make sure this is kept in sync with nsCSSProps::kDisplayKTable
5942 // First is BOX
5943 if (!newFrame && isXULDisplay) {
5944 triedFrame = PR_TRUE;
5946 if (display->mDisplay == NS_STYLE_DISPLAY_INLINE_BOX ||
5947 display->mDisplay == NS_STYLE_DISPLAY_BOX) {
5948 newFrame = NS_NewBoxFrame(mPresShell, aStyleContext, PR_FALSE, nsnull);
5950 // Boxes can scroll.
5951 mayBeScrollable = PR_TRUE;
5952 } // End of BOX CONSTRUCTION logic
5953 #ifdef MOZ_XUL
5954 // ------- Begin Grid ---------
5955 else if (display->mDisplay == NS_STYLE_DISPLAY_INLINE_GRID ||
5956 display->mDisplay == NS_STYLE_DISPLAY_GRID) {
5957 nsCOMPtr<nsIBoxLayout> layout;
5958 NS_NewGridLayout2(mPresShell, getter_AddRefs(layout));
5959 newFrame = NS_NewBoxFrame(mPresShell, aStyleContext, PR_FALSE, layout);
5961 // Boxes can scroll.
5962 mayBeScrollable = PR_TRUE;
5963 } //------- End Grid ------
5965 // ------- Begin Rows/Columns ---------
5966 else if (display->mDisplay == NS_STYLE_DISPLAY_GRID_GROUP) {
5967 nsCOMPtr<nsIBoxLayout> layout;
5969 if (aTag == nsGkAtoms::listboxbody) {
5970 NS_NewListBoxLayout(mPresShell, layout);
5971 newFrame = NS_NewListBoxBodyFrame(mPresShell, aStyleContext, PR_FALSE, layout);
5973 else
5975 NS_NewGridRowGroupLayout(mPresShell, getter_AddRefs(layout));
5976 newFrame = NS_NewGridRowGroupFrame(mPresShell, aStyleContext, PR_FALSE, layout);
5979 // Boxes can scroll.
5980 if (display->IsScrollableOverflow()) {
5981 // set the top to be the newly created scrollframe
5982 BuildScrollFrame(aState, aContent, aStyleContext, newFrame,
5983 aParentFrame, nsnull, topFrame, aStyleContext);
5985 // we have a scrollframe so the parent becomes the scroll frame.
5986 aParentFrame = newFrame->GetParent();
5988 primaryFrameSet = PR_TRUE;
5990 frameHasBeenInitialized = PR_TRUE;
5992 } //------- End Grid ------
5994 // ------- Begin Row/Column ---------
5995 else if (display->mDisplay == NS_STYLE_DISPLAY_GRID_LINE) {
5996 nsCOMPtr<nsIBoxLayout> layout;
5999 NS_NewGridRowLeafLayout(mPresShell, getter_AddRefs(layout));
6001 if (aTag == nsGkAtoms::listitem)
6002 newFrame = NS_NewListItemFrame(mPresShell, aStyleContext, PR_FALSE, layout);
6003 else
6004 newFrame = NS_NewGridRowLeafFrame(mPresShell, aStyleContext, PR_FALSE, layout);
6006 // Boxes can scroll.
6007 mayBeScrollable = PR_TRUE;
6008 } //------- End Grid ------
6009 // End of STACK CONSTRUCTION logic
6010 // DECK CONSTRUCTION
6011 else if (display->mDisplay == NS_STYLE_DISPLAY_DECK) {
6012 newFrame = NS_NewDeckFrame(mPresShell, aStyleContext);
6014 // End of DECK CONSTRUCTION logic
6015 else if (display->mDisplay == NS_STYLE_DISPLAY_GROUPBOX) {
6016 newFrame = NS_NewGroupBoxFrame(mPresShell, aStyleContext);
6018 // Boxes can scroll.
6019 mayBeScrollable = PR_TRUE;
6021 // STACK CONSTRUCTION
6022 else if (display->mDisplay == NS_STYLE_DISPLAY_STACK ||
6023 display->mDisplay == NS_STYLE_DISPLAY_INLINE_STACK) {
6024 newFrame = NS_NewStackFrame(mPresShell, aStyleContext);
6026 mayBeScrollable = PR_TRUE;
6028 else if (display->mDisplay == NS_STYLE_DISPLAY_POPUP) {
6029 // If a popup is inside a menu, then the menu understands the complex
6030 // rules/behavior governing the cascade of multiple menu popups and can handle
6031 // having the real popup frame placed under it as a child.
6032 // If, however, the parent is *not* a menu frame, then we need to create
6033 // a placeholder frame for the popup, and then we add the popup frame to the
6034 // root popup set (that manages all such "detached" popups).
6035 if (aParentFrame->GetType() != nsGkAtoms::menuFrame) {
6036 if (!aState.mPopupItems.containingBlock) {
6037 // Just don't create a frame for this popup; we can't do
6038 // anything with it, since there is no root popup set.
6039 *aHaltProcessing = PR_TRUE;
6040 NS_ASSERTION(!aState.mRootBox, "Popup containing block is missing");
6041 return NS_OK;
6044 #ifdef NS_DEBUG
6045 NS_ASSERTION(aState.mPopupItems.containingBlock->GetType() ==
6046 nsGkAtoms::popupSetFrame,
6047 "Popup containing block isn't a nsIPopupSetFrame");
6048 #endif
6049 isPopup = PR_TRUE;
6052 // This is its own frame that derives from box.
6053 newFrame = NS_NewMenuPopupFrame(mPresShell, aStyleContext);
6055 if (aTag == nsGkAtoms::tooltip) {
6056 if (aContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::_default,
6057 nsGkAtoms::_true, eIgnoreCase)) {
6058 // Tell the root box about the tooltip.
6059 if (aState.mRootBox)
6060 aState.mRootBox->SetDefaultTooltip(aContent);
6065 else {
6066 triedFrame = PR_FALSE;
6068 #endif // MOZ_XUL
6071 if (mayBeScrollable && display->IsScrollableOverflow()) {
6072 // set the top to be the newly created scrollframe
6073 BuildScrollFrame(aState, aContent, aStyleContext, newFrame,
6074 aParentFrame, nsnull, topFrame, aStyleContext);
6076 // we have a scrollframe so the parent becomes the scroll frame.
6077 // XXXldb Do we really want to do this? The one case where it
6078 // matters when |frameHasBeenInitialized| is true is one where
6079 // I think we'd be better off the other way around.
6080 aParentFrame = newFrame->GetParent();
6081 primaryFrameSet = PR_TRUE;
6082 frameHasBeenInitialized = PR_TRUE;
6086 if (NS_UNLIKELY(triedFrame && !newFrame))
6088 rv = NS_ERROR_OUT_OF_MEMORY;
6091 // If we succeeded in creating a frame then initialize it, process its
6092 // children (if requested), and set the initial child list
6093 if (NS_SUCCEEDED(rv) && newFrame != nsnull) {
6095 // if no top frame was created then the top is the new frame
6096 if (topFrame == nsnull)
6097 topFrame = newFrame;
6099 // xul does not support absolute positioning
6100 nsIFrame* geometricParent;
6101 #ifdef MOZ_XUL
6102 if (isPopup) {
6103 NS_ASSERTION(aState.mPopupItems.containingBlock, "How did we get here?");
6104 geometricParent = aState.mPopupItems.containingBlock;
6106 else
6107 #endif
6109 geometricParent = aParentFrame;
6113 nsIFrame* geometricParent = aState.GetGeometricParent(display, aParentFrame);
6115 // if the new frame was already initialized to initialize it again.
6116 if (!frameHasBeenInitialized) {
6118 rv = InitAndRestoreFrame(aState, aContent, geometricParent, nsnull, newFrame);
6120 if (NS_FAILED(rv)) {
6121 newFrame->Destroy();
6122 return rv;
6126 // if our parent is a block frame then do things the way html likes it
6127 // if not then we are in a box so do what boxes like. On example is boxes
6128 // do not support the absolute positioning of their children. While html blocks
6129 // that's why we call different things here.
6130 nsIAtom* frameType = geometricParent->GetType();
6131 if ((frameType == nsGkAtoms::blockFrame) ||
6132 (frameType == nsGkAtoms::areaFrame)) {
6134 // See if we need to create a view, e.g. the frame is absolutely positioned
6135 nsHTMLContainerFrame::CreateViewForFrame(newFrame, aParentFrame, PR_FALSE);
6138 } else {
6139 // we are in a box so do the box thing.
6140 nsBoxFrame::CreateViewForFrame(aState.mPresContext, newFrame,
6141 aStyleContext, PR_FALSE);
6147 // Add the new frame to our list of frame items. Note that we
6148 // don't support floating or positioning of XUL frames.
6149 rv = aState.AddChild(topFrame, aFrameItems, aContent, aStyleContext,
6150 origParentFrame, PR_FALSE, PR_FALSE, isPopup);
6151 if (NS_FAILED(rv)) {
6152 return rv;
6155 #ifdef MOZ_XUL
6156 if (aTag == nsGkAtoms::popupgroup &&
6157 aContent->IsRootOfNativeAnonymousSubtree()) {
6158 nsIRootBox* rootBox = nsIRootBox::GetRootBox(mPresShell);
6159 if (rootBox) {
6160 NS_ASSERTION(rootBox->GetPopupSetFrame() == newFrame,
6161 "Unexpected PopupSetFrame");
6162 aState.mPopupItems.containingBlock = rootBox->GetPopupSetFrame();
6165 #endif
6167 // If the new frame isn't a float containing block, then push a null
6168 // float containing block to disable floats. This is needed to disable
6169 // floats within XUL frames.
6170 nsFrameConstructorSaveState floatSaveState;
6171 PRBool isFloatContainingBlock =
6172 newFrame->GetContentInsertionFrame()->IsFloatContainingBlock();
6173 aState.PushFloatContainingBlock(isFloatContainingBlock ? newFrame : nsnull,
6174 floatSaveState, PR_FALSE, PR_FALSE);
6176 // Process the child content if requested
6177 nsFrameItems childItems;
6178 if (!newFrame->IsLeaf()) {
6179 // XXXbz don't we need calls to ShouldBuildChildFrames
6180 // elsewhere too? Why only for XUL?
6181 if (mDocument->BindingManager()->ShouldBuildChildFrames(aContent)) {
6182 rv = ProcessChildren(aState, aContent, newFrame, PR_FALSE,
6183 childItems, PR_FALSE);
6184 nsIContent *badKid;
6185 if (newFrame->IsBoxFrame() &&
6186 (badKid = AnyKidsNeedBlockParent(childItems.childList))) {
6187 nsAutoString parentTag, kidTag;
6188 aContent->Tag()->ToString(parentTag);
6189 badKid->Tag()->ToString(kidTag);
6190 const PRUnichar* params[] = { parentTag.get(), kidTag.get() };
6191 const char *message =
6192 (display->mDisplay == NS_STYLE_DISPLAY_INLINE_BOX)
6193 ? "NeededToWrapXULInlineBox" : "NeededToWrapXUL";
6194 nsContentUtils::ReportToConsole(nsContentUtils::eXUL_PROPERTIES,
6195 message,
6196 params, NS_ARRAY_LENGTH(params),
6197 mDocument->GetDocumentURI(),
6198 EmptyString(), 0, 0, // not useful
6199 nsIScriptError::warningFlag,
6200 "FrameConstructor");
6202 nsRefPtr<nsStyleContext> blockSC = mPresShell->StyleSet()->
6203 ResolvePseudoStyleFor(aContent,
6204 nsCSSAnonBoxes::mozXULAnonymousBlock,
6205 aStyleContext);
6206 nsIFrame *blockFrame = NS_NewBlockFrame(mPresShell, blockSC);
6207 // We might, in theory, want to set NS_BLOCK_SPACE_MGR and
6208 // NS_BLOCK_MARGIN_ROOT, but I think it's a bad idea given that
6209 // a real block placed here wouldn't get those set on it.
6211 InitAndRestoreFrame(aState, aContent, newFrame, nsnull,
6212 blockFrame, PR_FALSE);
6214 NS_ASSERTION(!blockFrame->HasView(), "need to do view reparenting");
6215 for (nsIFrame *f = childItems.childList; f; f = f->GetNextSibling()) {
6216 ReparentFrame(aState.mFrameManager, blockFrame, f);
6219 blockFrame->AppendFrames(nsnull, childItems.childList);
6220 childItems = nsFrameItems();
6221 childItems.AddChild(blockFrame);
6223 newFrame->AddStateBits(NS_STATE_BOX_WRAPS_KIDS_IN_BLOCK);
6228 // XXX These should go after the wrapper!
6229 CreateAnonymousFrames(aTag, aState, aContent, newFrame, PR_FALSE,
6230 childItems);
6232 // Set the frame's initial child list
6233 newFrame->SetInitialChildList(nsnull, childItems.childList);
6236 #ifdef MOZ_XUL
6237 // register tooltip support if needed
6238 if (aTag == nsGkAtoms::treechildren || // trees always need titletips
6239 aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::tooltiptext) ||
6240 aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::tooltip))
6242 nsIRootBox* rootBox = nsIRootBox::GetRootBox(mPresShell);
6243 if (rootBox)
6244 rootBox->AddTooltipSupport(aContent);
6246 #endif
6248 // addToHashTable:
6250 if (topFrame) {
6251 // the top frame is always what we map the content to. This is the frame that contains a pointer
6252 // to the content node.
6254 // Add a mapping from content object to primary frame. Note that for
6255 // floated and positioned frames this is the out-of-flow frame and not
6256 // the placeholder frame
6257 if (!primaryFrameSet)
6258 aState.mFrameManager->SetPrimaryFrameFor(aContent, topFrame);
6261 return rv;
6264 nsresult
6265 nsCSSFrameConstructor::AddLazyChildren(nsIContent* aContent,
6266 nsLazyFrameConstructionCallback* aCallback,
6267 void* aArg, PRBool aIsSynch)
6269 nsCOMPtr<nsIRunnable> event =
6270 new LazyGenerateChildrenEvent(aContent, mPresShell, aCallback, aArg);
6271 return aIsSynch ? event->Run() :
6272 NS_DispatchToCurrentThread(event);
6275 already_AddRefed<nsStyleContext>
6276 nsCSSFrameConstructor::BeginBuildingScrollFrame(nsFrameConstructorState& aState,
6277 nsIContent* aContent,
6278 nsStyleContext* aContentStyle,
6279 nsIFrame* aParentFrame,
6280 nsIFrame* aContentParentFrame,
6281 nsIAtom* aScrolledPseudo,
6282 PRBool aIsRoot,
6283 nsIFrame*& aNewFrame)
6285 nsIFrame* parentFrame = nsnull;
6286 nsIFrame* gfxScrollFrame = aNewFrame;
6288 nsFrameItems anonymousItems;
6290 nsRefPtr<nsStyleContext> contentStyle = aContentStyle;
6292 if (!gfxScrollFrame) {
6293 // Build a XULScrollFrame when the child is a box, otherwise an
6294 // HTMLScrollFrame
6295 if (IsXULDisplayType(aContentStyle->GetStyleDisplay())) {
6296 gfxScrollFrame = NS_NewXULScrollFrame(mPresShell, contentStyle, aIsRoot);
6297 } else {
6298 gfxScrollFrame = NS_NewHTMLScrollFrame(mPresShell, contentStyle, aIsRoot);
6301 InitAndRestoreFrame(aState, aContent, aParentFrame, nsnull, gfxScrollFrame);
6303 // Create a view
6304 nsHTMLContainerFrame::CreateViewForFrame(gfxScrollFrame,
6305 aContentParentFrame, PR_FALSE);
6308 // if there are any anonymous children for the scroll frame, create
6309 // frames for them.
6310 CreateAnonymousFrames(aState, aContent, mDocument, gfxScrollFrame,
6311 PR_FALSE, anonymousItems);
6313 parentFrame = gfxScrollFrame;
6314 aNewFrame = gfxScrollFrame;
6316 // we used the style that was passed in. So resolve another one.
6317 nsStyleSet *styleSet = mPresShell->StyleSet();
6318 nsStyleContext* aScrolledChildStyle = styleSet->ResolvePseudoStyleFor(aContent,
6319 aScrolledPseudo,
6320 contentStyle).get();
6322 if (gfxScrollFrame) {
6323 gfxScrollFrame->SetInitialChildList(nsnull, anonymousItems.childList);
6326 return aScrolledChildStyle;
6329 void
6330 nsCSSFrameConstructor::FinishBuildingScrollFrame(nsIFrame* aScrollFrame,
6331 nsIFrame* aScrolledFrame)
6333 aScrollFrame->AppendFrames(nsnull, aScrolledFrame);
6335 // force the scrolled frame to have a view. The view will be parented to
6336 // the correct anonymous inner view because the scrollframes override
6337 // nsIFrame::GetParentViewForChildFrame.
6338 nsHTMLContainerFrame::CreateViewForFrame(aScrolledFrame, nsnull, PR_TRUE);
6339 nsIView* view = aScrolledFrame->GetView();
6340 if (!view)
6341 return;
6346 * Called to wrap a gfx scrollframe around a frame. The hierarchy will look like this
6348 * ------- for gfx scrollbars ------
6351 * ScrollFrame
6354 * Frame (scrolled frame you passed in)
6357 *-----------------------------------
6358 * LEGEND:
6360 * ScrollFrame: This is a frame that manages gfx cross platform frame based scrollbars.
6362 * @param aContent the content node of the child to wrap.
6363 * @param aScrolledFrame The frame of the content to wrap. This should not be
6364 * Initialized. This method will initialize it with a scrolled pseudo
6365 * and no nsIContent. The content will be attached to the scrollframe
6366 * returned.
6367 * @param aContentStyle the style context that has already been resolved for the content being passed in.
6369 * @param aParentFrame The parent to attach the scroll frame to
6371 * @param aNewFrame The new scrollframe or gfx scrollframe that we create. It will contain the
6372 * scrolled frame you passed in. (returned)
6373 * If this is not null, we'll just use it
6374 * @param aScrolledContentStyle the style that was resolved for the scrolled frame. (returned)
6376 nsresult
6377 nsCSSFrameConstructor::BuildScrollFrame(nsFrameConstructorState& aState,
6378 nsIContent* aContent,
6379 nsStyleContext* aContentStyle,
6380 nsIFrame* aScrolledFrame,
6381 nsIFrame* aParentFrame,
6382 nsIFrame* aContentParentFrame,
6383 nsIFrame*& aNewFrame,
6384 nsStyleContext*& aScrolledContentStyle)
6386 nsRefPtr<nsStyleContext> scrolledContentStyle =
6387 BeginBuildingScrollFrame(aState, aContent, aContentStyle, aParentFrame,
6388 aContentParentFrame, nsCSSAnonBoxes::scrolledContent,
6389 PR_FALSE, aNewFrame);
6391 aScrolledFrame->SetStyleContextWithoutNotification(scrolledContentStyle);
6392 InitAndRestoreFrame(aState, aContent, aNewFrame, nsnull, aScrolledFrame);
6394 FinishBuildingScrollFrame(aNewFrame, aScrolledFrame);
6396 aScrolledContentStyle = scrolledContentStyle;
6398 // now set the primary frame to the ScrollFrame
6399 aState.mFrameManager->SetPrimaryFrameFor( aContent, aNewFrame );
6400 return NS_OK;
6404 nsresult
6405 nsCSSFrameConstructor::ConstructFrameByDisplayType(nsFrameConstructorState& aState,
6406 const nsStyleDisplay* aDisplay,
6407 nsIContent* aContent,
6408 PRInt32 aNameSpaceID,
6409 nsIAtom* aTag,
6410 nsIFrame* aParentFrame,
6411 nsStyleContext* aStyleContext,
6412 nsFrameItems& aFrameItems,
6413 PRBool aHasPseudoParent)
6415 PRBool primaryFrameSet = PR_FALSE;
6416 nsIFrame* newFrame = nsnull; // the frame we construct
6417 PRBool addToHashTable = PR_TRUE;
6418 PRBool addedToFrameList = PR_FALSE;
6419 nsresult rv = NS_OK;
6421 // The style system ensures that floated and positioned frames are
6422 // block-level.
6423 NS_ASSERTION(!(aDisplay->IsFloating() ||
6424 aDisplay->IsAbsolutelyPositioned()) ||
6425 aDisplay->IsBlockOutside(),
6426 "Style system did not apply CSS2.1 section 9.7 fixups");
6428 // If this is "body", try propagating its scroll style to the viewport
6429 // Note that we need to do this even if the body is NOT scrollable;
6430 // it might have dynamically changed from scrollable to not scrollable,
6431 // and that might need to be propagated.
6432 PRBool propagatedScrollToViewport = PR_FALSE;
6433 if (aContent->NodeInfo()->Equals(nsGkAtoms::body) &&
6434 aContent->IsNodeOfType(nsINode::eHTML)) {
6435 propagatedScrollToViewport =
6436 PropagateScrollToViewport() == aContent;
6439 // If the frame is a block-level frame and is scrollable, then wrap it
6440 // in a scroll frame.
6441 // XXX Ignore tables for the time being
6442 if (aDisplay->IsBlockInside() &&
6443 aDisplay->IsScrollableOverflow() &&
6444 !propagatedScrollToViewport) {
6446 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
6447 ProcessPseudoFrames(aState, aFrameItems);
6450 nsRefPtr<nsStyleContext> scrolledContentStyle
6451 = BeginBuildingScrollFrame(aState, aContent, aStyleContext,
6452 aState.GetGeometricParent(aDisplay, aParentFrame),
6453 aParentFrame,
6454 nsCSSAnonBoxes::scrolledContent,
6455 PR_FALSE, newFrame);
6457 // Initialize it
6458 // pass a temporary stylecontext, the correct one will be set later
6459 nsIFrame* scrolledFrame =
6460 NS_NewAreaFrame(mPresShell, aStyleContext,
6461 NS_BLOCK_SPACE_MGR | NS_BLOCK_MARGIN_ROOT);
6463 nsFrameItems blockItem;
6464 rv = ConstructBlock(aState,
6465 scrolledContentStyle->GetStyleDisplay(), aContent,
6466 newFrame, newFrame, scrolledContentStyle,
6467 &scrolledFrame, blockItem, aDisplay->IsPositioned());
6468 NS_ASSERTION(blockItem.childList == scrolledFrame,
6469 "Scrollframe's frameItems should be exactly the scrolled frame");
6470 FinishBuildingScrollFrame(newFrame, scrolledFrame);
6472 rv = aState.AddChild(newFrame, aFrameItems, aContent, aStyleContext,
6473 aParentFrame);
6474 if (NS_FAILED(rv)) {
6475 return rv;
6478 addedToFrameList = PR_TRUE;
6480 // See if the frame is absolute or fixed positioned
6481 else if (aDisplay->IsAbsolutelyPositioned() &&
6482 (NS_STYLE_DISPLAY_BLOCK == aDisplay->mDisplay ||
6483 NS_STYLE_DISPLAY_LIST_ITEM == aDisplay->mDisplay)) {
6485 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
6486 ProcessPseudoFrames(aState, aFrameItems);
6489 // Create a frame to wrap up the absolute positioned item
6490 // pass a temporary stylecontext, the correct one will be set later
6491 newFrame = NS_NewAbsoluteItemWrapperFrame(mPresShell, aStyleContext);
6493 rv = ConstructBlock(aState, aDisplay, aContent,
6494 aState.GetGeometricParent(aDisplay, aParentFrame), aParentFrame,
6495 aStyleContext, &newFrame, aFrameItems, PR_TRUE);
6496 if (NS_FAILED(rv)) {
6497 return rv;
6500 addedToFrameList = PR_TRUE;
6502 // See if the frame is floated and it's a block frame
6503 else if (aDisplay->IsFloating() &&
6504 (NS_STYLE_DISPLAY_BLOCK == aDisplay->mDisplay ||
6505 NS_STYLE_DISPLAY_LIST_ITEM == aDisplay->mDisplay)) {
6506 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
6507 ProcessPseudoFrames(aState, aFrameItems);
6509 // Create an area frame
6510 // pass a temporary stylecontext, the correct one will be set later
6511 newFrame = NS_NewFloatingItemWrapperFrame(mPresShell, aStyleContext);
6513 rv = ConstructBlock(aState, aDisplay, aContent,
6514 aState.GetGeometricParent(aDisplay, aParentFrame),
6515 aParentFrame, aStyleContext, &newFrame, aFrameItems,
6516 aDisplay->mPosition == NS_STYLE_POSITION_RELATIVE ||
6517 aDisplay->HasTransform());
6518 if (NS_FAILED(rv)) {
6519 return rv;
6522 addedToFrameList = PR_TRUE;
6524 // See if it's relatively positioned or transformed
6525 else if ((NS_STYLE_POSITION_RELATIVE == aDisplay->mPosition ||
6526 aDisplay->HasTransform()) &&
6527 (aDisplay->IsBlockInside() ||
6528 (NS_STYLE_DISPLAY_INLINE == aDisplay->mDisplay))) {
6529 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
6530 ProcessPseudoFrames(aState, aFrameItems);
6532 // Is it block-level or inline-level?
6533 if (aDisplay->IsBlockInside()) {
6534 // Create a wrapper frame. Only need space manager if it's inline-block
6535 PRUint32 flags = (aDisplay->mDisplay == NS_STYLE_DISPLAY_INLINE_BLOCK ?
6536 NS_BLOCK_SPACE_MGR | NS_BLOCK_MARGIN_ROOT : 0);
6537 newFrame = NS_NewRelativeItemWrapperFrame(mPresShell, aStyleContext,
6538 flags);
6539 // XXXbz should we be passing in a non-null aContentParentFrame?
6540 ConstructBlock(aState, aDisplay, aContent,
6541 aParentFrame, nsnull, aStyleContext, &newFrame,
6542 aFrameItems, PR_TRUE);
6543 addedToFrameList = PR_TRUE;
6544 } else {
6545 // Create a positioned inline frame
6546 newFrame = NS_NewPositionedInlineFrame(mPresShell, aStyleContext);
6547 // Note that we want to insert the inline after processing kids, since
6548 // processing of kids may split the inline.
6549 ConstructInline(aState, aDisplay, aContent,
6550 aParentFrame, aStyleContext, PR_TRUE, newFrame);
6553 // See if it's a block frame of some sort
6554 else if ((NS_STYLE_DISPLAY_BLOCK == aDisplay->mDisplay) ||
6555 (NS_STYLE_DISPLAY_LIST_ITEM == aDisplay->mDisplay) ||
6556 (NS_STYLE_DISPLAY_RUN_IN == aDisplay->mDisplay) ||
6557 (NS_STYLE_DISPLAY_COMPACT == aDisplay->mDisplay) ||
6558 (NS_STYLE_DISPLAY_INLINE_BLOCK == aDisplay->mDisplay)) {
6559 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
6560 ProcessPseudoFrames(aState, aFrameItems);
6562 PRUint32 flags = 0;
6563 if (NS_STYLE_DISPLAY_INLINE_BLOCK == aDisplay->mDisplay) {
6564 flags = NS_BLOCK_SPACE_MGR | NS_BLOCK_MARGIN_ROOT;
6566 // Create the block frame
6567 newFrame = NS_NewBlockFrame(mPresShell, aStyleContext, flags);
6568 if (newFrame) { // That worked so construct the block and its children
6569 // XXXbz should we be passing in a non-null aContentParentFrame?
6570 rv = ConstructBlock(aState, aDisplay, aContent,
6571 aParentFrame, nsnull, aStyleContext, &newFrame,
6572 aFrameItems, PR_FALSE);
6573 addedToFrameList = PR_TRUE;
6575 else {
6576 rv = NS_ERROR_OUT_OF_MEMORY;
6579 // See if it's an inline frame of some sort
6580 else if ((NS_STYLE_DISPLAY_INLINE == aDisplay->mDisplay) ||
6581 (NS_STYLE_DISPLAY_MARKER == aDisplay->mDisplay)) {
6582 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
6583 ProcessPseudoFrames(aState, aFrameItems);
6585 // Create the inline frame
6586 newFrame = NS_NewInlineFrame(mPresShell, aStyleContext);
6587 if (newFrame) { // That worked so construct the inline and its children
6588 // Note that we want to insert the inline after processing kids, since
6589 // processing of kids may split the inline.
6590 rv = ConstructInline(aState, aDisplay, aContent,
6591 aParentFrame, aStyleContext, PR_FALSE, newFrame);
6593 else {
6594 rv = NS_ERROR_OUT_OF_MEMORY;
6597 // To keep the hash table small don't add inline frames (they're
6598 // typically things like FONT and B), because we can quickly
6599 // find them if we need to
6600 addToHashTable = PR_FALSE;
6602 // otherwise let the display property influence the frame type to create
6603 else {
6604 // XXX This section now only handles table frames; should be
6605 // factored out probably
6607 // Use the 'display' property to choose a frame type
6608 switch (aDisplay->mDisplay) {
6609 case NS_STYLE_DISPLAY_TABLE:
6610 case NS_STYLE_DISPLAY_INLINE_TABLE:
6612 nsIFrame* innerTable;
6613 rv = ConstructTableFrame(aState, aContent,
6614 aParentFrame, aStyleContext,
6615 aNameSpaceID, PR_FALSE, aFrameItems, newFrame,
6616 innerTable);
6617 addedToFrameList = PR_TRUE;
6618 // Note: table construction function takes care of initializing
6619 // the frame, processing children, and setting the initial child
6620 // list
6621 break;
6624 // the next 5 cases are only relevant if the parent is not a table, ConstructTableFrame handles children
6625 case NS_STYLE_DISPLAY_TABLE_CAPTION:
6627 // aParentFrame may be an inner table frame rather than an outer frame
6628 // In this case we need to get the outer frame.
6629 nsIFrame* parentFrame = AdjustCaptionParentFrame(aParentFrame);
6630 rv = ConstructTableCaptionFrame(aState, aContent, parentFrame,
6631 aStyleContext, aNameSpaceID, aFrameItems,
6632 newFrame, aHasPseudoParent);
6633 if (NS_SUCCEEDED(rv) && !aHasPseudoParent) {
6634 aFrameItems.AddChild(newFrame);
6636 return rv;
6639 case NS_STYLE_DISPLAY_TABLE_ROW_GROUP:
6640 case NS_STYLE_DISPLAY_TABLE_HEADER_GROUP:
6641 case NS_STYLE_DISPLAY_TABLE_FOOTER_GROUP:
6642 rv = ConstructTableRowGroupFrame(aState, aContent, aParentFrame,
6643 aStyleContext, aNameSpaceID, PR_FALSE,
6644 aFrameItems, newFrame,
6645 aHasPseudoParent);
6646 if (NS_SUCCEEDED(rv) && !aHasPseudoParent) {
6647 aFrameItems.AddChild(newFrame);
6649 return rv;
6651 case NS_STYLE_DISPLAY_TABLE_COLUMN_GROUP:
6652 rv = ConstructTableColGroupFrame(aState, aContent, aParentFrame,
6653 aStyleContext, aNameSpaceID,
6654 PR_FALSE, aFrameItems, newFrame,
6655 aHasPseudoParent);
6656 if (NS_SUCCEEDED(rv) && !aHasPseudoParent) {
6657 aFrameItems.AddChild(newFrame);
6659 return rv;
6661 case NS_STYLE_DISPLAY_TABLE_COLUMN:
6662 rv = ConstructTableColFrame(aState, aContent, aParentFrame,
6663 aStyleContext, aNameSpaceID, PR_FALSE,
6664 aFrameItems, newFrame, aHasPseudoParent);
6665 if (NS_SUCCEEDED(rv) && !aHasPseudoParent) {
6666 aFrameItems.AddChild(newFrame);
6668 return rv;
6670 case NS_STYLE_DISPLAY_TABLE_ROW:
6671 rv = ConstructTableRowFrame(aState, aContent, aParentFrame,
6672 aStyleContext, aNameSpaceID, PR_FALSE,
6673 aFrameItems, newFrame, aHasPseudoParent);
6674 if (NS_SUCCEEDED(rv) && !aHasPseudoParent) {
6675 aFrameItems.AddChild(newFrame);
6677 return rv;
6679 case NS_STYLE_DISPLAY_TABLE_CELL:
6681 nsIFrame* innerTable;
6682 rv = ConstructTableCellFrame(aState, aContent, aParentFrame,
6683 aStyleContext, aNameSpaceID,
6684 PR_FALSE, aFrameItems, newFrame,
6685 innerTable, aHasPseudoParent);
6686 if (NS_SUCCEEDED(rv) && !aHasPseudoParent) {
6687 aFrameItems.AddChild(newFrame);
6689 return rv;
6692 default:
6693 NS_NOTREACHED("How did we get here?");
6694 break;
6698 if (!addedToFrameList) {
6699 // Gotta do it here
6700 NS_ASSERTION(!aDisplay->IsAbsolutelyPositioned() &&
6701 !aDisplay->IsFloating(),
6702 "Things that could be out-of-flow need to handle adding "
6703 "to the frame list themselves");
6705 rv = aState.AddChild(newFrame, aFrameItems, aContent, aStyleContext,
6706 aParentFrame);
6707 NS_ASSERTION(NS_SUCCEEDED(rv),
6708 "Cases where AddChild() can fail must handle it themselves");
6711 if (newFrame && addToHashTable) {
6712 // Add a mapping from content object to primary frame. Note that for
6713 // floated and positioned frames this is the out-of-flow frame and not
6714 // the placeholder frame
6715 if (!primaryFrameSet) {
6716 aState.mFrameManager->SetPrimaryFrameFor(aContent, newFrame);
6720 return rv;
6723 nsresult
6724 nsCSSFrameConstructor::InitAndRestoreFrame(const nsFrameConstructorState& aState,
6725 nsIContent* aContent,
6726 nsIFrame* aParentFrame,
6727 nsIFrame* aPrevInFlow,
6728 nsIFrame* aNewFrame,
6729 PRBool aAllowCounters)
6731 NS_PRECONDITION(mUpdateCount != 0,
6732 "Should be in an update while creating frames");
6734 nsresult rv = NS_OK;
6736 NS_ASSERTION(aNewFrame, "Null frame cannot be initialized");
6737 if (!aNewFrame)
6738 return NS_ERROR_NULL_POINTER;
6740 // Initialize the frame
6741 rv = aNewFrame->Init(aContent, aParentFrame, aPrevInFlow);
6742 aNewFrame->AddStateBits(aState.mAdditionalStateBits);
6744 if (aState.mFrameState && aState.mFrameManager) {
6745 // Restore frame state for just the newly created frame.
6746 aState.mFrameManager->RestoreFrameStateFor(aNewFrame, aState.mFrameState);
6749 if (aAllowCounters && !aPrevInFlow &&
6750 mCounterManager.AddCounterResetsAndIncrements(aNewFrame)) {
6751 CountersDirty();
6754 return rv;
6757 already_AddRefed<nsStyleContext>
6758 nsCSSFrameConstructor::ResolveStyleContext(nsIFrame* aParentFrame,
6759 nsIContent* aContent)
6761 nsStyleContext* parentStyleContext = nsnull;
6762 if (aContent->GetParent()) {
6763 aParentFrame = nsFrame::CorrectStyleParentFrame(aParentFrame, nsnull);
6765 if (aParentFrame) {
6766 // Resolve the style context based on the content object and the parent
6767 // style context
6768 parentStyleContext = aParentFrame->GetStyleContext();
6769 } else {
6770 // Perhaps aParentFrame is a canvasFrame and we're replicating
6771 // fixed-pos frames.
6772 // XXX should we create a way to tell ConstructFrame which style
6773 // context to use, and pass it the style context for the
6774 // previous page's fixed-pos frame?
6776 } else {
6777 // This has got to be a call from ConstructDocElementTableFrame.
6778 // Not sure how best to assert that here.
6781 nsStyleSet *styleSet = mPresShell->StyleSet();
6783 if (aContent->IsNodeOfType(nsINode::eELEMENT)) {
6784 return styleSet->ResolveStyleFor(aContent, parentStyleContext);
6785 } else {
6787 NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
6788 "shouldn't waste time creating style contexts for "
6789 "comments and processing instructions");
6791 return styleSet->ResolveStyleForNonElement(parentStyleContext);
6795 // MathML Mod - RBS
6796 #ifdef MOZ_MATHML
6797 nsresult
6798 nsCSSFrameConstructor::FlushAccumulatedBlock(nsFrameConstructorState& aState,
6799 nsIContent* aContent,
6800 nsIFrame* aParentFrame,
6801 nsFrameItems* aBlockItems,
6802 nsFrameItems* aNewItems)
6804 if (!aBlockItems->childList) {
6805 // Nothing to do
6806 return NS_OK;
6809 nsStyleContext* parentContext =
6810 nsFrame::CorrectStyleParentFrame(aParentFrame,
6811 nsCSSAnonBoxes::mozMathMLAnonymousBlock)->GetStyleContext();
6812 nsStyleSet *styleSet = mPresShell->StyleSet();
6813 nsRefPtr<nsStyleContext> blockContext;
6814 blockContext = styleSet->ResolvePseudoStyleFor(aContent,
6815 nsCSSAnonBoxes::mozMathMLAnonymousBlock,
6816 parentContext);
6818 // then, create a block frame that will wrap the child frames. Make it a
6819 // MathML frame so that Get(Absolute/Float)ContainingBlockFor know that this
6820 // is not a suitable block.
6821 nsIFrame* blockFrame = NS_NewMathMLmathBlockFrame(mPresShell, blockContext,
6822 NS_BLOCK_SPACE_MGR | NS_BLOCK_MARGIN_ROOT);
6823 if (NS_UNLIKELY(!blockFrame))
6824 return NS_ERROR_OUT_OF_MEMORY;
6826 InitAndRestoreFrame(aState, aContent, aParentFrame, nsnull, blockFrame);
6827 for (nsIFrame* f = aBlockItems->childList; f; f = f->GetNextSibling()) {
6828 ReparentFrame(aState.mFrameManager, blockFrame, f);
6830 // abs-pos and floats are disabled in MathML children so we don't have to
6831 // worry about messing up those.
6832 blockFrame->SetInitialChildList(nsnull, aBlockItems->childList);
6833 *aBlockItems = nsFrameItems();
6834 aNewItems->AddChild(blockFrame);
6835 return NS_OK;
6838 nsresult
6839 nsCSSFrameConstructor::ConstructMathMLFrame(nsFrameConstructorState& aState,
6840 nsIContent* aContent,
6841 nsIFrame* aParentFrame,
6842 nsIAtom* aTag,
6843 PRInt32 aNameSpaceID,
6844 nsStyleContext* aStyleContext,
6845 nsFrameItems& aFrameItems,
6846 PRBool aHasPseudoParent)
6848 // Make sure that we remain confined in the MathML world
6849 if (aNameSpaceID != kNameSpaceID_MathML)
6850 return NS_OK;
6852 nsresult rv = NS_OK;
6854 NS_ASSERTION(aTag != nsnull, "null MathML tag");
6855 if (aTag == nsnull)
6856 return NS_OK;
6858 // Initialize the new frame
6859 nsIFrame* newFrame = nsnull;
6861 // Make sure to keep IsSpecialContent in synch with this code
6862 const nsStyleDisplay* disp = aStyleContext->GetStyleDisplay();
6864 // Leverage IsSpecialContent to check if one of the |if aTag| below will
6865 // surely match (knowing that aNameSpaceID == kNameSpaceID_MathML here)
6866 if (IsSpecialContent(aContent, aTag, aNameSpaceID, aStyleContext)) {
6867 // process pending pseudo frames
6868 if (!aHasPseudoParent && !aState.mPseudoFrames.IsEmpty()) {
6869 ProcessPseudoFrames(aState, aFrameItems);
6873 if (aTag == nsGkAtoms::mi_ ||
6874 aTag == nsGkAtoms::mn_ ||
6875 aTag == nsGkAtoms::ms_ ||
6876 aTag == nsGkAtoms::mtext_)
6877 newFrame = NS_NewMathMLTokenFrame(mPresShell, aStyleContext);
6878 else if (aTag == nsGkAtoms::mo_)
6879 newFrame = NS_NewMathMLmoFrame(mPresShell, aStyleContext);
6880 else if (aTag == nsGkAtoms::mfrac_)
6881 newFrame = NS_NewMathMLmfracFrame(mPresShell, aStyleContext);
6882 else if (aTag == nsGkAtoms::msup_)
6883 newFrame = NS_NewMathMLmsupFrame(mPresShell, aStyleContext);
6884 else if (aTag == nsGkAtoms::msub_)
6885 newFrame = NS_NewMathMLmsubFrame(mPresShell, aStyleContext);
6886 else if (aTag == nsGkAtoms::msubsup_)
6887 newFrame = NS_NewMathMLmsubsupFrame(mPresShell, aStyleContext);
6888 else if (aTag == nsGkAtoms::munder_)
6889 newFrame = NS_NewMathMLmunderFrame(mPresShell, aStyleContext);
6890 else if (aTag == nsGkAtoms::mover_)
6891 newFrame = NS_NewMathMLmoverFrame(mPresShell, aStyleContext);
6892 else if (aTag == nsGkAtoms::munderover_)
6893 newFrame = NS_NewMathMLmunderoverFrame(mPresShell, aStyleContext);
6894 else if (aTag == nsGkAtoms::mphantom_)
6895 newFrame = NS_NewMathMLmphantomFrame(mPresShell, aStyleContext);
6896 else if (aTag == nsGkAtoms::mpadded_)
6897 newFrame = NS_NewMathMLmpaddedFrame(mPresShell, aStyleContext);
6898 else if (aTag == nsGkAtoms::mspace_ ||
6899 aTag == nsGkAtoms::none ||
6900 aTag == nsGkAtoms::mprescripts_)
6901 newFrame = NS_NewMathMLmspaceFrame(mPresShell, aStyleContext);
6902 else if (aTag == nsGkAtoms::mfenced_)
6903 newFrame = NS_NewMathMLmfencedFrame(mPresShell, aStyleContext);
6904 else if (aTag == nsGkAtoms::mmultiscripts_)
6905 newFrame = NS_NewMathMLmmultiscriptsFrame(mPresShell, aStyleContext);
6906 else if (aTag == nsGkAtoms::mstyle_)
6907 newFrame = NS_NewMathMLmstyleFrame(mPresShell, aStyleContext);
6908 else if (aTag == nsGkAtoms::msqrt_)
6909 newFrame = NS_NewMathMLmsqrtFrame(mPresShell, aStyleContext);
6910 else if (aTag == nsGkAtoms::mroot_)
6911 newFrame = NS_NewMathMLmrootFrame(mPresShell, aStyleContext);
6912 else if (aTag == nsGkAtoms::maction_)
6913 newFrame = NS_NewMathMLmactionFrame(mPresShell, aStyleContext);
6914 else if (aTag == nsGkAtoms::mrow_ ||
6915 aTag == nsGkAtoms::merror_)
6916 newFrame = NS_NewMathMLmrowFrame(mPresShell, aStyleContext);
6917 else if (aTag == nsGkAtoms::math) {
6918 // root <math> element
6919 const nsStyleDisplay* display = aStyleContext->GetStyleDisplay();
6920 PRBool isBlock = (NS_STYLE_DISPLAY_BLOCK == display->mDisplay);
6921 newFrame = NS_NewMathMLmathFrame(mPresShell, isBlock, aStyleContext);
6923 else {
6924 return NS_OK;
6927 // If we succeeded in creating a frame then initialize it, process its
6928 // children (if requested), and set the initial child list
6929 if (newFrame) {
6930 NS_ASSERTION(newFrame->IsFrameOfType(nsIFrame::eExcludesIgnorableWhitespace),
6931 "Ignorable whitespace should be excluded");
6933 // Only <math> elements can be floated or positioned. All other MathML
6934 // should be in-flow.
6935 PRBool isMath = aTag == nsGkAtoms::math;
6937 nsIFrame* geometricParent =
6938 isMath ? aState.GetGeometricParent(disp, aParentFrame) : aParentFrame;
6940 InitAndRestoreFrame(aState, aContent, geometricParent, nsnull, newFrame);
6942 // See if we need to create a view, e.g. the frame is absolutely positioned
6943 nsHTMLContainerFrame::CreateViewForFrame(newFrame, aParentFrame, PR_FALSE);
6945 rv = aState.AddChild(newFrame, aFrameItems, aContent, aStyleContext,
6946 aParentFrame, isMath, isMath);
6947 if (NS_FAILED(rv)) {
6948 return rv;
6951 // Push a null float containing block to disable floating within mathml
6952 nsFrameConstructorSaveState floatSaveState;
6953 aState.PushFloatContainingBlock(nsnull, floatSaveState, PR_FALSE,
6954 PR_FALSE);
6956 // Same for absolute positioning
6957 nsFrameConstructorSaveState absoluteSaveState;
6958 aState.PushAbsoluteContainingBlock(nsnull, absoluteSaveState);
6960 // MathML frames are inline frames, so just process their kids
6961 nsFrameItems childItems;
6962 if (!newFrame->IsLeaf()) {
6963 rv = ProcessChildren(aState, aContent, newFrame, PR_TRUE,
6964 childItems, PR_FALSE);
6967 CreateAnonymousFrames(aTag, aState, aContent, newFrame, PR_FALSE,
6968 childItems);
6970 // Wrap runs of inline children in a block
6971 if (NS_SUCCEEDED(rv)) {
6972 nsFrameItems newItems;
6973 nsFrameItems currentBlock;
6974 nsIFrame* f;
6975 while ((f = childItems.childList) != nsnull) {
6976 PRBool wrapFrame = IsInlineFrame(f) || IsFrameSpecial(f);
6977 if (!wrapFrame) {
6978 rv = FlushAccumulatedBlock(aState, aContent, newFrame, &currentBlock, &newItems);
6979 if (NS_FAILED(rv))
6980 break;
6983 childItems.RemoveChild(f, nsnull);
6984 if (wrapFrame) {
6985 currentBlock.AddChild(f);
6986 } else {
6987 newItems.AddChild(f);
6990 rv = FlushAccumulatedBlock(aState, aContent, newFrame, &currentBlock, &newItems);
6992 if (childItems.childList) {
6993 // an error must have occurred, delete unprocessed frames
6994 CleanupFrameReferences(aState.mFrameManager, childItems.childList);
6995 nsFrameList(childItems.childList).DestroyFrames();
6998 childItems = newItems;
7001 // Set the frame's initial child list
7002 newFrame->SetInitialChildList(nsnull, childItems.childList);
7004 return rv;
7006 else {
7007 return NS_ERROR_OUT_OF_MEMORY;
7010 #endif // MOZ_MATHML
7012 // SVG
7013 #ifdef MOZ_SVG
7014 nsresult
7015 nsCSSFrameConstructor::ConstructSVGFrame(nsFrameConstructorState& aState,
7016 nsIContent* aContent,
7017 nsIFrame* aParentFrame,
7018 nsIAtom* aTag,
7019 PRInt32 aNameSpaceID,
7020 nsStyleContext* aStyleContext,
7021 nsFrameItems& aFrameItems,
7022 PRBool aHasPseudoParent,
7023 PRBool* aHaltProcessing)
7025 NS_ASSERTION(aNameSpaceID == kNameSpaceID_SVG, "SVG frame constructed in wrong namespace");
7026 *aHaltProcessing = PR_FALSE;
7028 nsresult rv = NS_OK;
7029 PRBool forceView = PR_FALSE;
7030 PRBool isOuterSVGNode = PR_FALSE;
7031 const nsStyleDisplay* disp = aStyleContext->GetStyleDisplay();
7033 NS_ASSERTION(aTag != nsnull, "null SVG tag");
7034 if (aTag == nsnull)
7035 return NS_OK;
7037 // XXXbz somewhere here we should process pseudo frames if !aHasPseudoParent
7039 // Initialize the new frame
7040 nsIFrame* newFrame = nsnull;
7042 // Default to aParentFrame for the geometricParent; it's adjusted in
7043 // cases when we allow anything else.
7044 nsIFrame* geometricParent = aParentFrame;
7046 PRBool parentIsSVG = PR_FALSE;
7047 if (aParentFrame && aParentFrame->GetContent()) {
7048 PRInt32 parentNSID;
7049 nsIAtom* parentTag =
7050 mDocument->BindingManager()->ResolveTag(aParentFrame->GetContent(),
7051 &parentNSID);
7053 // It's not clear whether the SVG spec intends to allow any SVG
7054 // content within svg:foreignObject at all (SVG 1.1, section
7055 // 23.2), but if it does, it better be svg:svg. So given that
7056 // we're allowing it, treat it as a non-SVG parent.
7057 parentIsSVG = parentNSID == kNameSpaceID_SVG &&
7058 parentTag != nsGkAtoms::foreignObject;
7061 if ((aTag != nsGkAtoms::svg && !parentIsSVG) ||
7062 (aTag == nsGkAtoms::desc || aTag == nsGkAtoms::title)) {
7063 // Sections 5.1 and G.4 of SVG 1.1 say that SVG elements other than
7064 // svg:svg not contained within svg:svg are incorrect, although they
7065 // don't seem to specify error handling. Ignore them, since many of
7066 // our frame classes can't deal. It *may* be that the document
7067 // should at that point be considered in error according to F.2, but
7068 // it's hard to tell.
7070 // Style mutation can't change this situation, so don't bother
7071 // adding to the undisplayed content map.
7073 // We don't currently handle any UI for desc/title
7074 *aHaltProcessing = PR_TRUE;
7075 return NS_OK;
7078 // Reduce the number of frames we create unnecessarily. Note that this is not
7079 // where we select which frame in a <switch> to render! That happens in
7080 // nsSVGSwitchFrame::PaintSVG.
7081 if (!NS_SVG_PassesConditionalProcessingTests(aContent)) {
7082 // Note that just returning is probably not right. According
7083 // to the spec, <use> is allowed to use an element that fails its
7084 // conditional, but because we never actually create the frame when
7085 // a conditional fails and when we use GetReferencedFrame to find the
7086 // references, things don't work right.
7087 // XXX FIXME XXX
7088 *aHaltProcessing = PR_TRUE;
7089 return NS_OK;
7092 // Make sure to keep IsSpecialContent in synch with this code
7093 if (aTag == nsGkAtoms::svg) {
7094 if (!parentIsSVG) {
7095 // This is the outermost <svg> element.
7096 isOuterSVGNode = PR_TRUE;
7098 // Set the right geometricParent
7099 geometricParent = aState.GetGeometricParent(disp, aParentFrame);
7101 forceView = PR_TRUE;
7102 newFrame = NS_NewSVGOuterSVGFrame(mPresShell, aContent, aStyleContext);
7104 else {
7105 // This is an inner <svg> element
7106 newFrame = NS_NewSVGInnerSVGFrame(mPresShell, aContent, aStyleContext);
7109 else if (aTag == nsGkAtoms::g) {
7110 newFrame = NS_NewSVGGFrame(mPresShell, aContent, aStyleContext);
7112 else if (aTag == nsGkAtoms::svgSwitch) {
7113 newFrame = NS_NewSVGSwitchFrame(mPresShell, aContent, aStyleContext);
7115 else if (aTag == nsGkAtoms::polygon ||
7116 aTag == nsGkAtoms::polyline ||
7117 aTag == nsGkAtoms::circle ||
7118 aTag == nsGkAtoms::ellipse ||
7119 aTag == nsGkAtoms::line ||
7120 aTag == nsGkAtoms::rect ||
7121 aTag == nsGkAtoms::path)
7122 newFrame = NS_NewSVGPathGeometryFrame(mPresShell, aContent, aStyleContext);
7123 else if (aTag == nsGkAtoms::defs) {
7124 newFrame = NS_NewSVGContainerFrame(mPresShell, aContent, aStyleContext);
7126 else if (aTag == nsGkAtoms::foreignObject) {
7127 newFrame = NS_NewSVGForeignObjectFrame(mPresShell, aContent, aStyleContext);
7129 else if (aTag == nsGkAtoms::a) {
7130 newFrame = NS_NewSVGAFrame(mPresShell, aContent, aStyleContext);
7132 else if (aTag == nsGkAtoms::text) {
7133 nsIFrame *ancestorFrame = SVG_GetFirstNonAAncestorFrame(aParentFrame);
7134 if (ancestorFrame) {
7135 nsISVGTextContentMetrics* metrics;
7136 CallQueryInterface(ancestorFrame, &metrics);
7137 // Text cannot be nested
7138 if (!metrics)
7139 newFrame = NS_NewSVGTextFrame(mPresShell, aContent, aStyleContext);
7142 else if (aTag == nsGkAtoms::tspan) {
7143 nsIFrame *ancestorFrame = SVG_GetFirstNonAAncestorFrame(aParentFrame);
7144 if (ancestorFrame) {
7145 nsISVGTextContentMetrics* metrics;
7146 CallQueryInterface(ancestorFrame, &metrics);
7147 if (metrics)
7148 newFrame = NS_NewSVGTSpanFrame(mPresShell, aContent,
7149 ancestorFrame, aStyleContext);
7152 else if (aTag == nsGkAtoms::linearGradient) {
7153 newFrame = NS_NewSVGLinearGradientFrame(mPresShell, aContent, aStyleContext);
7155 else if (aTag == nsGkAtoms::radialGradient) {
7156 newFrame = NS_NewSVGRadialGradientFrame(mPresShell, aContent, aStyleContext);
7158 else if (aTag == nsGkAtoms::stop) {
7159 newFrame = NS_NewSVGStopFrame(mPresShell, aContent, aParentFrame, aStyleContext);
7161 else if (aTag == nsGkAtoms::use) {
7162 newFrame = NS_NewSVGUseFrame(mPresShell, aContent, aStyleContext);
7164 else if (aTag == nsGkAtoms::marker) {
7165 newFrame = NS_NewSVGMarkerFrame(mPresShell, aContent, aStyleContext);
7167 else if (aTag == nsGkAtoms::image) {
7168 newFrame = NS_NewSVGImageFrame(mPresShell, aContent, aStyleContext);
7170 else if (aTag == nsGkAtoms::clipPath) {
7171 newFrame = NS_NewSVGClipPathFrame(mPresShell, aContent, aStyleContext);
7173 else if (aTag == nsGkAtoms::textPath) {
7174 nsIFrame *ancestorFrame = SVG_GetFirstNonAAncestorFrame(aParentFrame);
7175 if (ancestorFrame &&
7176 ancestorFrame->GetType() == nsGkAtoms::svgTextFrame) {
7177 newFrame = NS_NewSVGTextPathFrame(mPresShell, aContent,
7178 ancestorFrame, aStyleContext);
7181 else if (aTag == nsGkAtoms::filter) {
7182 newFrame = NS_NewSVGFilterFrame(mPresShell, aContent, aStyleContext);
7184 else if (aTag == nsGkAtoms::pattern) {
7185 newFrame = NS_NewSVGPatternFrame(mPresShell, aContent, aStyleContext);
7187 else if (aTag == nsGkAtoms::mask) {
7188 newFrame = NS_NewSVGMaskFrame(mPresShell, aContent, aStyleContext);
7190 else if (aTag == nsGkAtoms::feDistantLight ||
7191 aTag == nsGkAtoms::fePointLight ||
7192 aTag == nsGkAtoms::feSpotLight ||
7193 aTag == nsGkAtoms::feBlend ||
7194 aTag == nsGkAtoms::feColorMatrix ||
7195 aTag == nsGkAtoms::feFuncR ||
7196 aTag == nsGkAtoms::feFuncG ||
7197 aTag == nsGkAtoms::feFuncB ||
7198 aTag == nsGkAtoms::feFuncA ||
7199 aTag == nsGkAtoms::feComposite ||
7200 aTag == nsGkAtoms::feConvolveMatrix ||
7201 aTag == nsGkAtoms::feDisplacementMap ||
7202 aTag == nsGkAtoms::feFlood ||
7203 aTag == nsGkAtoms::feGaussianBlur ||
7204 aTag == nsGkAtoms::feImage ||
7205 aTag == nsGkAtoms::feMergeNode ||
7206 aTag == nsGkAtoms::feMorphology ||
7207 aTag == nsGkAtoms::feOffset ||
7208 aTag == nsGkAtoms::feTile ||
7209 aTag == nsGkAtoms::feTurbulence) {
7210 // We don't really use the frame, just need it for the style
7211 // information, so create the simplest possible frame.
7212 newFrame = NS_NewSVGLeafFrame(mPresShell, aStyleContext);
7216 if (newFrame == nsnull) {
7217 // Either we have an unknown tag, or construction of a frame
7218 // failed. One reason why frame construction for a known tag might
7219 // have failed is that the content element doesn't implement all
7220 // interfaces required by the frame. This happens e.g. when using
7221 // 'extends' in xbl to extend an xbl binding from an svg
7222 // element. In that case, the bound content element will always be
7223 // a standard xml element, and not be of the right type.
7224 // The best we can do here is to create a generic svg container frame.
7225 // XXXldb This really isn't what the SVG spec says to do.
7226 #ifdef DEBUG
7227 // printf("Warning: Creating SVGGenericContainerFrame for tag <");
7228 // nsAutoString str;
7229 // aTag->ToString(str);
7230 // printf("%s>\n", NS_ConvertUTF16toUTF8(str).get());
7231 #endif
7232 newFrame = NS_NewSVGGenericContainerFrame(mPresShell, aContent, aStyleContext);
7234 // If we succeeded in creating a frame then initialize it, process its
7235 // children (if requested), and set the initial child list
7236 if (newFrame != nsnull) {
7237 InitAndRestoreFrame(aState, aContent, geometricParent, nsnull, newFrame);
7238 nsHTMLContainerFrame::CreateViewForFrame(newFrame, aParentFrame, forceView);
7240 rv = aState.AddChild(newFrame, aFrameItems, aContent, aStyleContext,
7241 aParentFrame, isOuterSVGNode, isOuterSVGNode);
7242 if (NS_FAILED(rv)) {
7243 return rv;
7246 nsFrameItems childItems;
7247 if (aTag == nsGkAtoms::foreignObject) {
7248 // Resolve pseudo style and create an inner block frame
7249 // XXX this breaks style inheritance
7250 nsRefPtr<nsStyleContext> innerPseudoStyle;
7251 innerPseudoStyle = mPresShell->StyleSet()->
7252 ResolvePseudoStyleFor(aContent,
7253 nsCSSAnonBoxes::mozSVGForeignContent, aStyleContext);
7255 nsIFrame* blockFrame = NS_NewBlockFrame(mPresShell, innerPseudoStyle,
7256 NS_BLOCK_SPACE_MGR |
7257 NS_BLOCK_MARGIN_ROOT);
7258 if (NS_UNLIKELY(!blockFrame))
7259 return NS_ERROR_OUT_OF_MEMORY;
7261 // Claim to be relatively positioned so that we end up being the
7262 // absolute containing block.
7263 nsFrameConstructorSaveState saveState;
7264 aState.PushFloatContainingBlock(nsnull, saveState, PR_FALSE, PR_FALSE);
7265 rv = ConstructBlock(aState, innerPseudoStyle->GetStyleDisplay(), aContent,
7266 newFrame, newFrame, innerPseudoStyle,
7267 &blockFrame, childItems, PR_TRUE);
7268 // Give the blockFrame a view so that GetOffsetTo works for descendants
7269 // of blockFrame with views...
7270 nsHTMLContainerFrame::CreateViewForFrame(blockFrame, nsnull, PR_TRUE);
7271 } else {
7272 // Process the child content if requested.
7273 if (!newFrame->IsLeaf()) {
7274 rv = ProcessChildren(aState, aContent, newFrame, PR_FALSE, childItems,
7275 PR_FALSE);
7277 CreateAnonymousFrames(aTag, aState, aContent, newFrame,
7278 PR_FALSE, childItems);
7281 // Set the frame's initial child list
7282 newFrame->SetInitialChildList(nsnull, childItems.childList);
7283 return rv;
7285 else {
7286 return NS_ERROR_FAILURE;
7289 #endif // MOZ_SVG
7291 // If page-break-before is set, this function constructs a page break frame,
7292 // EXCEPT for on these types of elements:
7293 // * row groups, rows, cells (these are handled internally by tables)
7294 // * fixed- and absolutely-positioned elements (currently, our positioning
7295 // code doesn't expect positioned frames to have nsPageBreakFrame siblings)
7297 // Returns true iff we should construct a page break frame after this element.
7298 PRBool
7299 nsCSSFrameConstructor::PageBreakBefore(nsFrameConstructorState& aState,
7300 nsIContent* aContent,
7301 nsIFrame* aParentFrame,
7302 nsStyleContext* aStyleContext,
7303 nsFrameItems& aFrameItems)
7305 const nsStyleDisplay* display = aStyleContext->GetStyleDisplay();
7307 if (NS_STYLE_DISPLAY_NONE != display->mDisplay &&
7308 NS_STYLE_POSITION_FIXED != display->mPosition &&
7309 NS_STYLE_POSITION_ABSOLUTE != display->mPosition &&
7310 (NS_STYLE_DISPLAY_TABLE == display->mDisplay ||
7311 !IsTableRelated(display->mDisplay, PR_TRUE))) {
7312 if (display->mBreakBefore) {
7313 ConstructPageBreakFrame(aState, aContent, aParentFrame, aStyleContext,
7314 aFrameItems);
7316 return display->mBreakAfter;
7318 return PR_FALSE;
7321 nsresult
7322 nsCSSFrameConstructor::ConstructPageBreakFrame(nsFrameConstructorState& aState,
7323 nsIContent* aContent,
7324 nsIFrame* aParentFrame,
7325 nsStyleContext* aStyleContext,
7326 nsFrameItems& aFrameItems)
7328 nsRefPtr<nsStyleContext> pseudoStyle;
7329 pseudoStyle = mPresShell->StyleSet()->ResolvePseudoStyleFor(nsnull,
7330 nsCSSAnonBoxes::pageBreak,
7331 aStyleContext);
7332 nsIFrame* pageBreakFrame = NS_NewPageBreakFrame(mPresShell, pseudoStyle);
7333 if (pageBreakFrame) {
7334 InitAndRestoreFrame(aState, aContent, aParentFrame, nsnull, pageBreakFrame);
7335 aFrameItems.AddChild(pageBreakFrame);
7337 return NS_OK;
7339 else {
7340 return NS_ERROR_OUT_OF_MEMORY;
7344 nsresult
7345 nsCSSFrameConstructor::ConstructFrame(nsFrameConstructorState& aState,
7346 nsIContent* aContent,
7347 nsIFrame* aParentFrame,
7348 nsFrameItems& aFrameItems)
7351 NS_PRECONDITION(nsnull != aParentFrame, "no parent frame");
7353 nsresult rv = NS_OK;
7355 // don't create a whitespace frame if aParent doesn't want it
7356 if (!NeedFrameFor(aParentFrame, aContent)) {
7357 return rv;
7360 // never create frames for comments or PIs
7361 if (aContent->IsNodeOfType(nsINode::eCOMMENT) ||
7362 aContent->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION))
7363 return rv;
7365 nsRefPtr<nsStyleContext> styleContext;
7366 styleContext = ResolveStyleContext(aParentFrame, aContent);
7368 PRBool pageBreakAfter = PR_FALSE;
7370 if (aState.mPresContext->IsPaginated()) {
7371 // See if there is a page break before, if so construct one. Also see if there is one after
7372 pageBreakAfter = PageBreakBefore(aState, aContent, aParentFrame,
7373 styleContext, aFrameItems);
7376 // construct the frame
7377 rv = ConstructFrameInternal(aState, aContent, aParentFrame,
7378 aContent->Tag(), aContent->GetNameSpaceID(),
7379 styleContext, aFrameItems, PR_FALSE);
7381 if (NS_SUCCEEDED(rv) && pageBreakAfter) {
7382 // Construct the page break after
7383 ConstructPageBreakFrame(aState, aContent, aParentFrame, styleContext,
7384 aFrameItems);
7387 return rv;
7391 nsresult
7392 nsCSSFrameConstructor::ConstructFrameInternal( nsFrameConstructorState& aState,
7393 nsIContent* aContent,
7394 nsIFrame* aParentFrame,
7395 nsIAtom* aTag,
7396 PRInt32 aNameSpaceID,
7397 nsStyleContext* aStyleContext,
7398 nsFrameItems& aFrameItems,
7399 PRBool aXBLBaseTag)
7401 // The following code allows the user to specify the base tag
7402 // of an element using XBL. XUL and HTML objects (like boxes, menus, etc.)
7403 // can then be extended arbitrarily.
7404 const nsStyleDisplay* display = aStyleContext->GetStyleDisplay();
7405 nsRefPtr<nsStyleContext> styleContext(aStyleContext);
7406 nsAutoEnqueueBinding binding(mDocument);
7407 if (!aXBLBaseTag)
7410 // Ensure that our XBL bindings are installed.
7411 if (display->mBinding) {
7412 // Get the XBL loader.
7413 nsresult rv;
7414 // Load the bindings.
7415 PRBool resolveStyle;
7417 nsIXBLService * xblService = GetXBLService();
7418 if (!xblService)
7419 return NS_ERROR_FAILURE;
7421 rv = xblService->LoadBindings(aContent, display->mBinding->mURI,
7422 display->mBinding->mOriginPrincipal,
7423 PR_FALSE, getter_AddRefs(binding.mBinding),
7424 &resolveStyle);
7425 if (NS_FAILED(rv))
7426 return NS_OK;
7428 if (resolveStyle) {
7429 styleContext = ResolveStyleContext(aParentFrame, aContent);
7430 display = styleContext->GetStyleDisplay();
7433 PRInt32 nameSpaceID;
7434 nsCOMPtr<nsIAtom> baseTag =
7435 mDocument->BindingManager()->ResolveTag(aContent, &nameSpaceID);
7437 if (baseTag != aTag || aNameSpaceID != nameSpaceID) {
7438 // Construct the frame using the XBL base tag.
7439 rv = ConstructFrameInternal(aState,
7440 aContent,
7441 aParentFrame,
7442 baseTag,
7443 nameSpaceID,
7444 styleContext,
7445 aFrameItems,
7446 PR_TRUE);
7447 return rv;
7452 // Pre-check for display "none" - if we find that, don't create
7453 // any frame at all
7454 if (NS_STYLE_DISPLAY_NONE == display->mDisplay) {
7455 aState.mFrameManager->SetUndisplayedContent(aContent, styleContext);
7456 return NS_OK;
7459 nsIFrame* adjParentFrame = aParentFrame;
7460 nsFrameItems* frameItems = &aFrameItems;
7461 PRBool pseudoParent = PR_FALSE;
7462 PRBool suppressFrame = PR_FALSE;
7463 nsFrameConstructorSaveState pseudoSaveState;
7464 nsresult rv = AdjustParentFrame(aState, aContent, adjParentFrame,
7465 aTag, aNameSpaceID, styleContext,
7466 frameItems, pseudoSaveState,
7467 suppressFrame, pseudoParent);
7468 if (NS_FAILED(rv) || suppressFrame) {
7469 return rv;
7472 if (aContent->IsNodeOfType(nsINode::eTEXT))
7473 return ConstructTextFrame(aState, aContent, adjParentFrame, styleContext,
7474 *frameItems, pseudoParent);
7476 #ifdef MOZ_SVG
7477 // Don't create frames for non-SVG children of SVG elements
7478 if (aNameSpaceID != kNameSpaceID_SVG &&
7479 aParentFrame &&
7480 aParentFrame->IsFrameOfType(nsIFrame::eSVG) &&
7481 !aParentFrame->IsFrameOfType(nsIFrame::eSVGForeignObject)
7483 return NS_OK;
7485 #endif
7487 // If the page contains markup that overrides text direction, and
7488 // does not contain any characters that would activate the Unicode
7489 // bidi algorithm, we need to call |SetBidiEnabled| on the pres
7490 // context before reflow starts. This requires us to resolve some
7491 // style information now. See bug 115921.
7493 if (styleContext->GetStyleVisibility()->mDirection ==
7494 NS_STYLE_DIRECTION_RTL)
7495 aState.mPresContext->SetBidiEnabled();
7497 // Start background loads during frame construction. This is just
7498 // a hint; the paint code will do the right thing in any case.
7500 styleContext->GetStyleBackground();
7503 nsIFrame* lastChild = frameItems->lastChild;
7505 // Handle specific frame types
7506 rv = ConstructHTMLFrame(aState, aContent, adjParentFrame, aTag, aNameSpaceID,
7507 styleContext, *frameItems, pseudoParent);
7509 // Failing to find a matching HTML frame, try creating a specialized
7510 // XUL frame. This is temporary, pending planned factoring of this
7511 // whole process into separate, pluggable steps.
7512 if (NS_SUCCEEDED(rv) &&
7513 (!frameItems->childList || lastChild == frameItems->lastChild)) {
7514 PRBool haltProcessing;
7516 rv = ConstructXULFrame(aState, aContent, adjParentFrame, aTag,
7517 aNameSpaceID, styleContext,
7518 *frameItems, aXBLBaseTag, pseudoParent,
7519 &haltProcessing);
7521 if (haltProcessing) {
7522 return rv;
7526 // MathML Mod - RBS
7527 #ifdef MOZ_MATHML
7528 if (NS_SUCCEEDED(rv) &&
7529 (!frameItems->childList || lastChild == frameItems->lastChild)) {
7530 rv = ConstructMathMLFrame(aState, aContent, adjParentFrame, aTag,
7531 aNameSpaceID, styleContext, *frameItems,
7532 pseudoParent);
7534 #endif
7536 // SVG
7537 #ifdef MOZ_SVG
7538 if (NS_SUCCEEDED(rv) &&
7539 (!frameItems->childList || lastChild == frameItems->lastChild) &&
7540 aNameSpaceID == kNameSpaceID_SVG &&
7541 NS_SVGEnabled()) {
7542 PRBool haltProcessing;
7543 rv = ConstructSVGFrame(aState, aContent, adjParentFrame, aTag,
7544 aNameSpaceID, styleContext,
7545 *frameItems, pseudoParent, &haltProcessing);
7546 if (haltProcessing) {
7547 return rv;
7550 #endif
7552 if (NS_SUCCEEDED(rv) &&
7553 (!frameItems->childList || lastChild == frameItems->lastChild)) {
7554 // When there is no explicit frame to create, assume it's a
7555 // container and let display style dictate the rest
7556 rv = ConstructFrameByDisplayType(aState, display, aContent, aNameSpaceID,
7557 aTag, adjParentFrame, styleContext,
7558 *frameItems, pseudoParent);
7561 return rv;
7565 inline PRBool
7566 IsRootBoxFrame(nsIFrame *aFrame)
7568 return (aFrame->GetType() == nsGkAtoms::rootFrame);
7571 nsresult
7572 nsCSSFrameConstructor::ReconstructDocElementHierarchy()
7574 AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
7575 return ReconstructDocElementHierarchyInternal();
7578 nsresult
7579 nsCSSFrameConstructor::ReconstructDocElementHierarchyInternal()
7581 #ifdef DEBUG
7582 if (gNoisyContentUpdates) {
7583 printf("nsCSSFrameConstructor::ReconstructDocElementHierarchy\n");
7585 #endif
7587 nsresult rv = NS_OK;
7589 // XXXbz is that null-check needed? Why?
7590 if (mDocument && mPresShell) {
7591 nsIContent *rootContent = mDocument->GetRootContent();
7593 if (rootContent) {
7594 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
7595 nsnull, nsnull, mTempFrameTreeState);
7597 // Before removing the frames associated with the content object, ask them to save their
7598 // state onto a temporary state object.
7599 CaptureStateFor(state.mFrameManager->GetRootFrame(), mTempFrameTreeState);
7601 // Get the frame that corresponds to the document element
7602 nsIFrame* docElementFrame =
7603 state.mFrameManager->GetPrimaryFrameFor(rootContent, -1);
7605 if (docElementFrame) {
7606 // Destroy out-of-flow frames that might not be in the frame subtree
7607 // rooted at docElementFrame
7608 ::DeletingFrameSubtree(state.mFrameManager, docElementFrame);
7611 // Remove any existing fixed items: they are always on the
7612 // FixedContainingBlock. Note that this has to be done before we call
7613 // ClearPlaceholderFrameMap(), since RemoveFixedItems uses the
7614 // placeholder frame map.
7615 rv = RemoveFixedItems(state, docElementFrame);
7617 if (NS_SUCCEEDED(rv)) {
7618 nsPlaceholderFrame* placeholderFrame = nsnull;
7619 if (docElementFrame &&
7620 (docElementFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW)) {
7621 // Get the placeholder frame now, before we tear down the
7622 // placeholder frame map
7623 placeholderFrame =
7624 state.mFrameManager->GetPlaceholderFrameFor(docElementFrame);
7625 NS_ASSERTION(placeholderFrame, "No placeholder for out-of-flow?");
7628 // Clear the hash tables that map from content to frame and out-of-flow
7629 // frame to placeholder frame
7630 state.mFrameManager->ClearPrimaryFrameMap();
7631 state.mFrameManager->ClearPlaceholderFrameMap();
7632 state.mFrameManager->ClearUndisplayedContentMap();
7634 if (docElementFrame) {
7635 // Take the docElementFrame, and remove it from its parent.
7636 // XXXbz So why can't we reuse ContentRemoved?
7638 // Notify self that we will destroy the entire frame tree, this blocks
7639 // RemoveMappingsForFrameSubtree() which would otherwise lead to a
7640 // crash since we cleared the placeholder map above (bug 398982).
7641 PRBool wasDestroyingFrameTree = mIsDestroyingFrameTree;
7642 WillDestroyFrameTree();
7644 rv = state.mFrameManager->RemoveFrame(docElementFrame->GetParent(),
7645 GetChildListNameFor(docElementFrame), docElementFrame);
7647 if (placeholderFrame) {
7648 // Remove the placeholder frame first (XXX second for now) (so
7649 // that it doesn't retain a dangling pointer to memory)
7650 rv |= state.mFrameManager->RemoveFrame(placeholderFrame->GetParent(),
7651 nsnull, placeholderFrame);
7654 mIsDestroyingFrameTree = wasDestroyingFrameTree;
7655 if (NS_FAILED(rv)) {
7656 return rv;
7660 mInitialContainingBlock = nsnull;
7661 mRootElementStyleFrame = nsnull;
7663 // Create the new document element hierarchy
7664 nsIFrame* newChild;
7665 rv = ConstructDocElementFrame(state, rootContent,
7666 mDocElementContainingBlock, &newChild);
7668 // newChild could be null even if |rv| is success, thanks to XBL.
7669 if (NS_SUCCEEDED(rv) && newChild) {
7670 rv = state.mFrameManager->InsertFrames(mDocElementContainingBlock,
7671 nsnull, nsnull, newChild);
7677 return rv;
7681 nsIFrame*
7682 nsCSSFrameConstructor::GetFrameFor(nsIContent* aContent)
7684 // Get the primary frame associated with the content
7685 nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
7687 if (!frame)
7688 return nsnull;
7690 nsIFrame* insertionFrame = frame->GetContentInsertionFrame();
7692 NS_ASSERTION(insertionFrame == frame || !frame->IsLeaf(),
7693 "The insertion frame is the primary frame or the primary frame isn't a leaf");
7695 return insertionFrame;
7698 nsIFrame*
7699 nsCSSFrameConstructor::GetAbsoluteContainingBlock(nsIFrame* aFrame)
7701 NS_PRECONDITION(nsnull != mInitialContainingBlock, "no initial containing block");
7703 // Starting with aFrame, look for a frame that is absolutely positioned or
7704 // relatively positioned
7705 nsIFrame* containingBlock = nsnull;
7706 for (nsIFrame* frame = aFrame; frame && !containingBlock;
7707 frame = frame->GetParent()) {
7708 if (frame->IsFrameOfType(nsIFrame::eMathML)) {
7709 // If it's mathml, bail out -- no absolute positioning out from inside
7710 // mathml frames. Note that we don't make this part of the loop
7711 // condition because of the mInitialContainingBlock stuff at the
7712 // end of this method...
7713 return nsnull;
7716 // Is it positioned?
7717 // If it's table-related then ignore it, because for the time
7718 // being table-related frames are not containers for absolutely
7719 // positioned child frames.
7720 const nsStyleDisplay* disp = frame->GetStyleDisplay();
7722 if (disp->IsPositioned() && !IsTableRelated(disp->mDisplay, PR_TRUE)) {
7723 // Find the outermost wrapped block under this frame
7724 for (nsIFrame* wrappedFrame = aFrame; wrappedFrame != frame->GetParent();
7725 wrappedFrame = wrappedFrame->GetParent()) {
7726 nsIAtom* frameType = wrappedFrame->GetType();
7727 if (nsGkAtoms::areaFrame == frameType ||
7728 nsGkAtoms::blockFrame == frameType ||
7729 nsGkAtoms::positionedInlineFrame == frameType) {
7730 containingBlock = wrappedFrame;
7731 } else if (nsGkAtoms::fieldSetFrame == frameType) {
7732 // If the positioned frame is a fieldset, use the area frame inside it.
7733 // We don't use GetContentInsertionFrame for fieldsets yet.
7734 containingBlock = GetFieldSetAreaFrame(wrappedFrame);
7738 #ifdef DEBUG
7739 if (!containingBlock)
7740 NS_WARNING("Positioned frame that does not handle positioned kids; looking further up the parent chain");
7741 #endif
7745 // If we found an absolutely positioned containing block, then use the
7746 // first-continuation.
7747 if (containingBlock)
7748 return AdjustAbsoluteContainingBlock(containingBlock);
7750 // If we didn't find it, then use the document element containing block
7751 return mHasRootAbsPosContainingBlock ? mDocElementContainingBlock : nsnull;
7754 nsIFrame*
7755 nsCSSFrameConstructor::GetFloatContainingBlock(nsIFrame* aFrame)
7757 NS_PRECONDITION(mInitialContainingBlock, "no initial containing block");
7759 // Starting with aFrame, look for a frame that is a float containing block.
7760 // IF we hit a mathml frame, bail out; we don't allow floating out of mathml
7761 // frames, because they don't seem to be able to deal.
7762 for (nsIFrame* containingBlock = aFrame;
7763 containingBlock && !containingBlock->IsFrameOfType(nsIFrame::eMathML) &&
7764 !containingBlock->IsBoxFrame();
7765 containingBlock = containingBlock->GetParent()) {
7766 if (containingBlock->IsFloatContainingBlock()) {
7767 return containingBlock;
7771 // If we didn't find a containing block, then there just isn't
7772 // one.... return null
7773 return nsnull;
7777 * This function will check whether aContainer has :after generated content.
7778 * If so, appending to it should actually insert. The return value is the
7779 * parent to use for newly-appended content. *aAfterFrame points to the :after
7780 * frame before which appended content should go, if there is one.
7782 static nsIFrame*
7783 AdjustAppendParentForAfterContent(nsPresContext* aPresContext,
7784 nsIContent* aContainer,
7785 nsIFrame* aParentFrame,
7786 nsIFrame** aAfterFrame)
7788 // See if the parent has an :after pseudo-element. Check for the presence
7789 // of style first, since nsLayoutUtils::GetAfterFrame is sorta expensive.
7790 nsStyleContext* parentStyle = aParentFrame->GetStyleContext();
7791 if (nsLayoutUtils::HasPseudoStyle(aContainer, parentStyle,
7792 nsCSSPseudoElements::after,
7793 aPresContext)) {
7794 nsIFrame* afterFrame = nsLayoutUtils::GetAfterFrame(aParentFrame);
7795 if (afterFrame) {
7796 *aAfterFrame = afterFrame;
7797 return afterFrame->GetParent();
7801 *aAfterFrame = nsnull;
7802 return aParentFrame;
7806 * This function is called by ContentAppended() and ContentInserted()
7807 * when appending flowed frames to a parent's principal child list. It
7808 * handles the case where the parent frame has :after pseudo-element
7809 * generated content.
7811 nsresult
7812 nsCSSFrameConstructor::AppendFrames(nsFrameConstructorState& aState,
7813 nsIContent* aContainer,
7814 nsIFrame* aParentFrame,
7815 nsFrameItems& aFrameList,
7816 nsIFrame* aAfterFrame)
7818 #ifdef DEBUG
7819 nsIFrame* debugAfterFrame;
7820 nsIFrame* debugNewParent =
7821 ::AdjustAppendParentForAfterContent(aState.mPresContext, aContainer,
7822 aParentFrame, &debugAfterFrame);
7823 NS_ASSERTION(debugNewParent == aParentFrame, "Incorrect parent");
7824 NS_ASSERTION(debugAfterFrame == aAfterFrame, "Incorrect after frame");
7825 #endif
7827 nsFrameManager* frameManager = aState.mFrameManager;
7828 if (aAfterFrame) {
7829 NS_ASSERTION(!IsFrameSpecial(aParentFrame) ||
7830 IsInlineFrame(aParentFrame) ||
7831 !IsInlineOutside(aAfterFrame),
7832 "Shouldn't have inline :after content on the block in an "
7833 "{ib} split");
7834 nsFrameList frames(aParentFrame->GetFirstChild(nsnull));
7836 // Insert the frames before the :after pseudo-element.
7837 return frameManager->InsertFrames(aParentFrame, nsnull,
7838 frames.GetPrevSiblingFor(aAfterFrame),
7839 aFrameList.childList);
7842 if (IsFrameSpecial(aParentFrame) &&
7843 !IsInlineFrame(aParentFrame) &&
7844 IsInlineOutside(aFrameList.lastChild)) {
7845 NS_ASSERTION(!aParentFrame->GetNextContinuation() ||
7846 !aParentFrame->GetNextContinuation()->GetFirstChild(nsnull),
7847 "Shouldn't happen");
7849 // We want to put some of the frames into the following inline frame.
7850 nsIFrame* lastBlock = FindLastBlock(aFrameList.childList);
7851 nsIFrame* firstTrailingInline;
7852 if (lastBlock) {
7853 firstTrailingInline = lastBlock->GetNextSibling();
7854 lastBlock->SetNextSibling(nsnull);
7855 aFrameList.lastChild = lastBlock;
7856 } else {
7857 firstTrailingInline = aFrameList.childList;
7858 aFrameList = nsFrameItems();
7861 NS_ASSERTION(firstTrailingInline, "How did that happen?");
7862 nsIFrame* parentFrame = aParentFrame;
7864 // As we go up the tree creating trailing inlines, we have to move floats
7865 // up to ancestor blocks. This means that at any given time we'll be
7866 // working with two frame constructor states, and aState is one of the two
7867 // only at the first step. Create some space to do this so we don't have
7868 // to allocate as we go.
7869 char stateBuf[2 * sizeof(nsFrameConstructorState)];
7870 nsFrameConstructorState* sourceState = &aState;
7871 nsFrameConstructorState* targetState =
7872 reinterpret_cast<nsFrameConstructorState*>(stateBuf);
7874 // Now we loop, because it might be the case that the parent of our special
7875 // block is another special block, and that we're at the very end of it,
7876 // and in that case if we create a new special inline we'll have to create
7877 // a parent for it too.
7878 do {
7879 NS_ASSERTION(IsFrameSpecial(parentFrame) && !IsInlineFrame(parentFrame),
7880 "Shouldn't be in this code");
7881 nsIFrame* inlineSibling = GetSpecialSibling(parentFrame);
7882 PRBool isPositioned = PR_FALSE;
7883 nsIContent* content = nsnull;
7884 nsStyleContext* styleContext = nsnull;
7885 if (!inlineSibling) {
7886 nsIFrame* firstInline =
7887 GetIBSplitSpecialPrevSiblingForAnonymousBlock(parentFrame);
7888 NS_ASSERTION(firstInline, "How did that happen?");
7890 content = firstInline->GetContent();
7891 styleContext = firstInline->GetStyleContext();
7892 isPositioned = (styleContext->GetStyleDisplay()->mPosition ==
7893 NS_STYLE_POSITION_RELATIVE);
7896 nsIFrame* stateParent =
7897 inlineSibling ? inlineSibling->GetParent() : parentFrame->GetParent();
7899 new (targetState)
7900 nsFrameConstructorState(mPresShell, mFixedContainingBlock,
7901 GetAbsoluteContainingBlock(stateParent),
7902 GetFloatContainingBlock(stateParent));
7903 nsIFrame* newInlineSibling =
7904 MoveFramesToEndOfIBSplit(*sourceState, inlineSibling,
7905 isPositioned, content,
7906 styleContext, firstTrailingInline,
7907 parentFrame, targetState);
7909 if (sourceState == &aState) {
7910 NS_ASSERTION(targetState ==
7911 reinterpret_cast<nsFrameConstructorState*>(stateBuf),
7912 "Bogus target state?");
7913 // Set sourceState to the value targetState should have next.
7914 sourceState = targetState + 1;
7915 } else {
7916 // Go ahead and process whatever insertions we didn't move out
7917 sourceState->~nsFrameConstructorState();
7920 // We're done with the source state. The target becomes the new source,
7921 // and we point the target pointer to the available memory.
7922 nsFrameConstructorState* temp = sourceState;
7923 sourceState = targetState;
7924 targetState = temp;;
7926 if (inlineSibling) {
7927 // we're all set -- we just moved things to a frame that was already
7928 // there.
7929 NS_ASSERTION(newInlineSibling == inlineSibling, "What happened?");
7930 break;
7933 SetFrameIsSpecial(parentFrame->GetFirstContinuation(), newInlineSibling);
7935 // We had to create a frame for this new inline sibling. Figure out
7936 // the right parentage for it.
7937 // XXXbz add a test for this?
7938 nsIFrame* newParentFrame = parentFrame->GetParent();
7939 NS_ASSERTION(!IsInlineFrame(newParentFrame),
7940 "The block in an {ib} split shouldn't be living inside "
7941 "an inline");
7942 if (!IsFrameSpecial(newParentFrame) ||
7943 newParentFrame->GetNextContinuation() ||
7944 parentFrame->GetNextSibling()) {
7945 // Just insert after parentFrame
7946 frameManager->InsertFrames(newParentFrame, nsnull, parentFrame,
7947 newInlineSibling);
7948 firstTrailingInline = nsnull;
7949 } else {
7950 // recurse up the tree
7951 parentFrame = newParentFrame;
7952 firstTrailingInline = newInlineSibling;
7954 } while (firstTrailingInline);
7956 // Process the float insertions on the last target state we had.
7957 sourceState->~nsFrameConstructorState();
7960 if (!aFrameList.childList) {
7961 // It all got eaten by the special inline
7962 return NS_OK;
7965 return frameManager->AppendFrames(aParentFrame, nsnull,
7966 aFrameList.childList);
7969 #define UNSET_DISPLAY 255
7971 nsIFrame*
7972 nsCSSFrameConstructor::FindPreviousAnonymousSibling(nsIContent* aContainer,
7973 nsIContent* aChild)
7975 nsCOMPtr<nsIDOMDocumentXBL> xblDoc(do_QueryInterface(mDocument));
7976 NS_ASSERTION(xblDoc, "null xblDoc for content element in FindNextAnonymousSibling");
7977 if (! xblDoc)
7978 return nsnull;
7980 // Grovel through the anonymous elements looking for aChild. We'll
7981 // start our search for a previous frame there.
7982 nsCOMPtr<nsIDOMNodeList> nodeList;
7983 nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(aContainer));
7984 xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList));
7986 if (! nodeList)
7987 return nsnull;
7989 PRUint32 length;
7990 nodeList->GetLength(&length);
7992 PRInt32 index;
7993 for (index = PRInt32(length) - 1; index >= 0; --index) {
7994 nsCOMPtr<nsIDOMNode> node;
7995 nodeList->Item(PRUint32(index), getter_AddRefs(node));
7997 nsCOMPtr<nsIContent> child = do_QueryInterface(node);
7998 if (child == aChild)
7999 break;
8002 // We want the node immediately before aChild. Keep going until we
8003 // run off the beginning of the nodeList, or we find a frame.
8004 PRUint8 childDisplay = UNSET_DISPLAY;
8005 while (--index >= 0) {
8006 nsCOMPtr<nsIDOMNode> node;
8007 nodeList->Item(PRUint32(index), getter_AddRefs(node));
8009 nsCOMPtr<nsIContent> child = do_QueryInterface(node);
8011 // Get its frame. If it doesn't have one, continue on to the
8012 // anonymous element that preceded it.
8013 nsIFrame* prevSibling = FindFrameForContentSibling(child, aChild,
8014 childDisplay, PR_TRUE);
8015 if (prevSibling) {
8016 // Found a previous sibling, we're done!
8017 return prevSibling;
8021 return nsnull;
8025 * Find the frame for the anonymous content immediately following
8026 * aChild.
8028 nsIFrame*
8029 nsCSSFrameConstructor::FindNextAnonymousSibling(nsIContent* aContainer,
8030 nsIContent* aChild)
8032 nsCOMPtr<nsIDOMDocumentXBL> xblDoc(do_QueryInterface(mDocument));
8033 NS_ASSERTION(xblDoc, "null xblDoc for content element in FindNextAnonymousSibling");
8034 if (! xblDoc)
8035 return nsnull;
8037 // Grovel through the anonymous elements looking for aChild
8038 nsCOMPtr<nsIDOMNodeList> nodeList;
8039 nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(aContainer));
8040 xblDoc->GetAnonymousNodes(elt, getter_AddRefs(nodeList));
8042 if (! nodeList)
8043 return nsnull;
8045 PRUint32 length;
8046 nodeList->GetLength(&length);
8048 PRInt32 index;
8049 for (index = 0; index < PRInt32(length); ++index) {
8050 nsCOMPtr<nsIDOMNode> node;
8051 nodeList->Item(PRUint32(index), getter_AddRefs(node));
8053 nsCOMPtr<nsIContent> child = do_QueryInterface(node);
8054 if (child == aChild)
8055 break;
8058 // We want the node immediately after aChild. Keep going until we
8059 // run off the end of the nodeList, or we find a next sibling.
8060 PRUint8 childDisplay = UNSET_DISPLAY;
8061 while (++index < PRInt32(length)) {
8062 nsCOMPtr<nsIDOMNode> node;
8063 nodeList->Item(PRUint32(index), getter_AddRefs(node));
8065 nsCOMPtr<nsIContent> child = do_QueryInterface(node);
8067 // Get its frame
8068 nsIFrame* nextSibling = FindFrameForContentSibling(child, aChild,
8069 childDisplay, PR_FALSE);
8070 if (nextSibling) {
8071 // Found a next sibling, we're done!
8072 return nextSibling;
8076 return nsnull;
8079 // This gets called to see if the frames corresponding to aSiblingDisplay and aDisplay
8080 // should be siblings in the frame tree. Although (1) rows and cols, (2) row groups
8081 // and col groups, (3) row groups and captions, (4) legends and content inside fieldsets, (5) popups and other kids of the menu
8082 // are siblings from a content perspective, they are not considered siblings in the
8083 // frame tree.
8084 PRBool
8085 nsCSSFrameConstructor::IsValidSibling(nsIFrame* aSibling,
8086 nsIContent* aContent,
8087 PRUint8& aDisplay)
8089 nsIFrame* parentFrame = aSibling->GetParent();
8090 nsIAtom* parentType = nsnull;
8091 nsIAtom* grandparentType = nsnull;
8092 if (parentFrame) {
8093 parentType = parentFrame->GetType();
8094 nsIFrame* grandparentFrame = parentFrame->GetParent();
8095 if (grandparentFrame) {
8096 grandparentType = grandparentFrame->GetType();
8100 PRUint8 siblingDisplay = aSibling->GetStyleDisplay()->mDisplay;
8101 if ((NS_STYLE_DISPLAY_TABLE_COLUMN_GROUP == siblingDisplay) ||
8102 (NS_STYLE_DISPLAY_TABLE_COLUMN == siblingDisplay) ||
8103 (NS_STYLE_DISPLAY_TABLE_CAPTION == siblingDisplay) ||
8104 (NS_STYLE_DISPLAY_TABLE_HEADER_GROUP == siblingDisplay) ||
8105 (NS_STYLE_DISPLAY_TABLE_ROW_GROUP == siblingDisplay) ||
8106 (NS_STYLE_DISPLAY_TABLE_FOOTER_GROUP == siblingDisplay) ||
8107 nsGkAtoms::menuFrame == parentType) {
8108 // if we haven't already, construct a style context to find the display type of aContent
8109 if (UNSET_DISPLAY == aDisplay) {
8110 nsRefPtr<nsStyleContext> styleContext;
8111 nsIFrame* styleParent;
8112 PRBool providerIsChild;
8113 if (NS_FAILED(aSibling->
8114 GetParentStyleContextFrame(aSibling->PresContext(),
8115 &styleParent,
8116 &providerIsChild)) ||
8117 !styleParent) {
8118 NS_NOTREACHED("Shouldn't happen");
8119 return PR_FALSE;
8121 styleContext = ResolveStyleContext(styleParent, aContent);
8122 if (!styleContext) return PR_FALSE;
8123 const nsStyleDisplay* display = styleContext->GetStyleDisplay();
8124 aDisplay = display->mDisplay;
8126 if (nsGkAtoms::menuFrame == parentType) {
8127 return
8128 (NS_STYLE_DISPLAY_POPUP == aDisplay) ==
8129 (NS_STYLE_DISPLAY_POPUP == siblingDisplay);
8131 switch (siblingDisplay) {
8132 case NS_STYLE_DISPLAY_TABLE_COLUMN_GROUP:
8133 return (NS_STYLE_DISPLAY_TABLE_COLUMN_GROUP == aDisplay);
8134 case NS_STYLE_DISPLAY_TABLE_COLUMN:
8135 return (NS_STYLE_DISPLAY_TABLE_COLUMN == aDisplay);
8136 case NS_STYLE_DISPLAY_TABLE_CAPTION:
8137 return (NS_STYLE_DISPLAY_TABLE_CAPTION == aDisplay);
8138 default: // all of the row group types
8139 return (NS_STYLE_DISPLAY_TABLE_HEADER_GROUP == aDisplay) ||
8140 (NS_STYLE_DISPLAY_TABLE_ROW_GROUP == aDisplay) ||
8141 (NS_STYLE_DISPLAY_TABLE_FOOTER_GROUP == aDisplay) ||
8142 (NS_STYLE_DISPLAY_TABLE_CAPTION == aDisplay);
8145 else if (nsGkAtoms::fieldSetFrame == parentType ||
8146 (nsGkAtoms::fieldSetFrame == grandparentType &&
8147 nsGkAtoms::areaFrame == parentType)) {
8148 // Legends can be sibling of legends but not of other content in the fieldset
8149 nsIAtom* sibType = aSibling->GetType();
8150 nsCOMPtr<nsIDOMHTMLLegendElement> legendContent(do_QueryInterface(aContent));
8152 if ((legendContent && (nsGkAtoms::legendFrame != sibType)) ||
8153 (!legendContent && (nsGkAtoms::legendFrame == sibType)))
8154 return PR_FALSE;
8157 return PR_TRUE;
8160 nsIFrame*
8161 nsCSSFrameConstructor::FindFrameForContentSibling(nsIContent* aContent,
8162 nsIContent* aTargetContent,
8163 PRUint8& aTargetContentDisplay,
8164 PRBool aPrevSibling)
8166 nsIFrame* sibling = mPresShell->GetPrimaryFrameFor(aContent);
8167 if (!sibling) {
8168 return nsnull;
8171 // If the frame is out-of-flow, GPFF() will have returned the
8172 // out-of-flow frame; we want the placeholder.
8173 if (sibling->GetStateBits() & NS_FRAME_OUT_OF_FLOW) {
8174 nsIFrame* placeholderFrame;
8175 mPresShell->GetPlaceholderFrameFor(sibling, &placeholderFrame);
8176 NS_ASSERTION(placeholderFrame, "no placeholder for out-of-flow frame");
8177 sibling = placeholderFrame;
8180 // The frame we have now should never be a continuation
8181 NS_ASSERTION(!sibling->GetPrevContinuation(), "How did that happen?");
8183 if (aPrevSibling) {
8184 // The frame may be a special frame (a split inline frame that
8185 // contains a block). Get the last part of that split.
8186 if (IsFrameSpecial(sibling)) {
8187 sibling = GetLastSpecialSibling(sibling);
8190 // The frame may have a continuation. If so, we want the last
8191 // non-overflow-container continuation as our previous sibling.
8192 sibling = sibling->GetTailContinuation();
8195 if (aTargetContent &&
8196 !IsValidSibling(sibling, aTargetContent, aTargetContentDisplay)) {
8197 sibling = nsnull;
8200 return sibling;
8204 * Find the ``rightmost'' frame for the content immediately preceding
8205 * aIndexInContainer, following continuations if necessary.
8207 nsIFrame*
8208 nsCSSFrameConstructor::FindPreviousSibling(nsIContent* aContainer,
8209 PRInt32 aIndexInContainer,
8210 nsIContent* aChild)
8212 NS_ASSERTION(aContainer, "null argument");
8214 ChildIterator first, iter;
8215 nsresult rv = ChildIterator::Init(aContainer, &first, &iter);
8216 NS_ENSURE_SUCCESS(rv, nsnull);
8217 iter.seek(aIndexInContainer);
8219 PRUint8 childDisplay = UNSET_DISPLAY;
8220 // Note: not all content objects are associated with a frame (e.g., if it's
8221 // `display: none') so keep looking until we find a previous frame
8222 while (iter-- != first) {
8223 nsIFrame* prevSibling =
8224 FindFrameForContentSibling(nsCOMPtr<nsIContent>(*iter), aChild,
8225 childDisplay, PR_TRUE);
8227 if (prevSibling) {
8228 #ifdef DEBUG
8229 nsIFrame* containerFrame = nsnull;
8230 containerFrame = mPresShell->GetPrimaryFrameFor(aContainer);
8231 NS_ASSERTION(prevSibling != containerFrame, "Previous Sibling is the Container's frame");
8232 #endif
8233 // Found a previous sibling, we're done!
8234 return prevSibling;
8238 return nsnull;
8242 * Find the frame for the content node immediately following
8243 * aIndexInContainer.
8245 nsIFrame*
8246 nsCSSFrameConstructor::FindNextSibling(nsIContent* aContainer,
8247 PRInt32 aIndexInContainer,
8248 nsIContent* aChild)
8250 ChildIterator iter, last;
8251 nsresult rv = ChildIterator::Init(aContainer, &iter, &last);
8252 NS_ENSURE_SUCCESS(rv, nsnull);
8253 iter.seek(aIndexInContainer);
8255 // Catch the case where someone tries to append
8256 if (iter == last)
8257 return nsnull;
8259 PRUint8 childDisplay = UNSET_DISPLAY;
8261 while (++iter != last) {
8262 nsIFrame* nextSibling =
8263 FindFrameForContentSibling(nsCOMPtr<nsIContent>(*iter), aChild,
8264 childDisplay, PR_FALSE);
8266 if (nextSibling) {
8267 // We found a next sibling, we're done!
8268 return nextSibling;
8272 return nsnull;
8275 inline PRBool
8276 ShouldIgnoreSelectChild(nsIContent* aContainer)
8278 // Ignore options and optgroups inside a select (size > 1)
8279 nsIAtom *containerTag = aContainer->Tag();
8281 if (containerTag == nsGkAtoms::optgroup ||
8282 containerTag == nsGkAtoms::select) {
8283 nsIContent* selectContent = aContainer;
8285 while (containerTag != nsGkAtoms::select) {
8286 selectContent = selectContent->GetParent();
8287 if (!selectContent) {
8288 break;
8290 containerTag = selectContent->Tag();
8293 nsCOMPtr<nsISelectElement> selectElement = do_QueryInterface(selectContent);
8294 if (selectElement) {
8295 nsAutoString selSize;
8296 aContainer->GetAttr(kNameSpaceID_None, nsGkAtoms::size, selSize);
8297 if (!selSize.IsEmpty()) {
8298 PRInt32 err;
8299 return (selSize.ToInteger(&err) > 1);
8304 return PR_FALSE;
8307 // For fieldsets, returns the area frame, if the child is not a legend.
8308 static nsIFrame*
8309 GetAdjustedParentFrame(nsIFrame* aParentFrame,
8310 nsIAtom* aParentFrameType,
8311 nsIContent* aParentContent,
8312 PRInt32 aChildIndex)
8314 NS_PRECONDITION(nsGkAtoms::tableOuterFrame != aParentFrameType,
8315 "Shouldn't be happening!");
8317 nsIContent *childContent = aParentContent->GetChildAt(aChildIndex);
8318 nsIFrame* newParent = nsnull;
8320 if (nsGkAtoms::fieldSetFrame == aParentFrameType) {
8321 // If the parent is a fieldSet, use the fieldSet's area frame as the
8322 // parent unless the new content is a legend.
8323 nsCOMPtr<nsIDOMHTMLLegendElement> legendContent(do_QueryInterface(childContent));
8324 if (!legendContent) {
8325 newParent = GetFieldSetAreaFrame(aParentFrame);
8328 return (newParent) ? newParent : aParentFrame;
8331 static void
8332 InvalidateCanvasIfNeeded(nsIFrame* aFrame);
8334 static PRBool
8335 IsSpecialFramesetChild(nsIContent* aContent)
8337 // IMPORTANT: This must match the conditions in nsHTMLFramesetFrame::Init.
8338 return aContent->IsNodeOfType(nsINode::eHTML) &&
8339 (aContent->Tag() == nsGkAtoms::frameset ||
8340 aContent->Tag() == nsGkAtoms::frame);
8343 nsresult
8344 nsCSSFrameConstructor::ContentAppended(nsIContent* aContainer,
8345 PRInt32 aNewIndexInContainer)
8347 AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
8348 NS_PRECONDITION(mUpdateCount != 0,
8349 "Should be in an update while creating frames");
8351 #ifdef DEBUG
8352 if (gNoisyContentUpdates) {
8353 printf("nsCSSFrameConstructor::ContentAppended container=%p index=%d\n",
8354 static_cast<void*>(aContainer), aNewIndexInContainer);
8355 if (gReallyNoisyContentUpdates && aContainer) {
8356 aContainer->List(stdout, 0);
8359 #endif
8361 #ifdef MOZ_XUL
8362 if (aContainer) {
8363 PRInt32 namespaceID;
8364 nsIAtom* tag =
8365 mDocument->BindingManager()->ResolveTag(aContainer, &namespaceID);
8367 // Just ignore tree tags, anyway we don't create any frames for them.
8368 if (tag == nsGkAtoms::treechildren ||
8369 tag == nsGkAtoms::treeitem ||
8370 tag == nsGkAtoms::treerow ||
8371 (namespaceID == kNameSpaceID_XUL && gUseXBLForms &&
8372 ShouldIgnoreSelectChild(aContainer)))
8373 return NS_OK;
8376 #endif // MOZ_XUL
8378 // Get the frame associated with the content
8379 nsIFrame* parentFrame = GetFrameFor(aContainer);
8380 if (! parentFrame)
8381 return NS_OK;
8383 // See if we have an XBL insertion point. If so, then that's our
8384 // real parent frame; if not, then the frame hasn't been built yet
8385 // and we just bail.
8387 nsIFrame* insertionPoint;
8388 PRBool multiple = PR_FALSE;
8389 GetInsertionPoint(parentFrame, nsnull, &insertionPoint, &multiple);
8390 if (! insertionPoint)
8391 return NS_OK; // Don't build the frames.
8393 PRBool hasInsertion = PR_FALSE;
8394 if (!multiple) {
8395 nsIDocument* document = nsnull;
8396 nsIContent *firstAppendedChild =
8397 aContainer->GetChildAt(aNewIndexInContainer);
8398 if (firstAppendedChild) {
8399 document = firstAppendedChild->GetDocument();
8401 if (document &&
8402 document->BindingManager()->GetInsertionParent(firstAppendedChild)) {
8403 hasInsertion = PR_TRUE;
8407 if (multiple || hasInsertion) {
8408 // We have an insertion point. There are some additional tests we need to do
8409 // in order to ensure that an append is a safe operation.
8410 PRUint32 childCount = 0;
8412 if (!multiple) {
8413 // We may need to make multiple ContentInserted calls instead. A
8414 // reasonable heuristic to employ (in order to maintain good performance)
8415 // is to find out if the insertion point's content node contains any
8416 // explicit children. If it does not, then it is highly likely that
8417 // an append is occurring. (Note it is not definite, and there are insane
8418 // cases we will not deal with by employing this heuristic, but it beats
8419 // always falling back to multiple ContentInserted calls).
8421 // In the multiple insertion point case, we know we're going to need to do
8422 // multiple ContentInserted calls anyway.
8423 childCount = insertionPoint->GetContent()->GetChildCount();
8426 if (multiple || childCount > 0) {
8427 // Now comes the fun part. For each appended child, we must obtain its
8428 // insertion point and find its exact position within that insertion point.
8429 // We then make a ContentInserted call with the correct computed index.
8430 nsIContent* insertionContent = insertionPoint->GetContent();
8432 PRUint32 containerCount = aContainer->GetChildCount();
8433 for (PRUint32 i = aNewIndexInContainer; i < containerCount; i++) {
8434 nsIContent *child = aContainer->GetChildAt(i);
8435 if (multiple) {
8436 // Filters are in effect, so the insertion point needs to be refetched for
8437 // each child.
8438 GetInsertionPoint(parentFrame, child, &insertionPoint);
8439 if (!insertionPoint) {
8440 // This content node doesn't have an insertion point, so we just
8441 // skip over it
8442 continue;
8444 insertionContent = insertionPoint->GetContent();
8447 // Construct an iterator to locate this child at its correct index.
8448 ChildIterator iter, last;
8449 for (ChildIterator::Init(insertionContent, &iter, &last);
8450 iter != last;
8451 ++iter) {
8452 LAYOUT_PHASE_TEMP_EXIT();
8453 nsIContent* item = nsCOMPtr<nsIContent>(*iter);
8454 if (item == child)
8455 // Call ContentInserted with this index.
8456 ContentInserted(aContainer, child,
8457 iter.position(), mTempFrameTreeState);
8458 LAYOUT_PHASE_TEMP_REENTER();
8462 return NS_OK;
8466 parentFrame = insertionPoint;
8468 if (parentFrame->GetType() == nsGkAtoms::frameSetFrame) {
8469 // Check whether we have any kids we care about.
8470 PRUint32 count = aContainer->GetChildCount();
8471 for (PRUint32 i = aNewIndexInContainer; i < count; ++i) {
8472 if (IsSpecialFramesetChild(aContainer->GetChildAt(i))) {
8473 // Just reframe the parent, since framesets are weird like that.
8474 return RecreateFramesForContent(parentFrame->GetContent());
8479 if (parentFrame->IsLeaf()) {
8480 // Nothing to do here; we shouldn't be constructing kids of leaves
8481 return NS_OK;
8484 #ifdef MOZ_MATHML
8485 if (parentFrame->IsFrameOfType(nsIFrame::eMathML))
8486 return RecreateFramesForContent(parentFrame->GetContent());
8487 #endif
8489 // If the frame we are manipulating is a ``special'' frame (that is, one
8490 // that's been created as a result of a block-in-inline situation) then we
8491 // need to append to the last special sibling, not to the frame itself.
8492 if (IsFrameSpecial(parentFrame)) {
8493 #ifdef DEBUG
8494 if (gNoisyContentUpdates) {
8495 printf("nsCSSFrameConstructor::ContentAppended: parentFrame=");
8496 nsFrame::ListTag(stdout, parentFrame);
8497 printf(" is special\n");
8499 #endif
8501 // Since we're appending, we'll walk to the last anonymous frame
8502 // that was created for the broken inline frame.
8503 parentFrame = GetLastSpecialSibling(parentFrame);
8506 // Get continuation that parents the last child
8507 parentFrame = nsLayoutUtils::GetLastContinuationWithChild(parentFrame);
8509 nsIAtom* frameType = parentFrame->GetType();
8510 // Deal with fieldsets
8511 parentFrame = ::GetAdjustedParentFrame(parentFrame, frameType,
8512 aContainer, aNewIndexInContainer);
8514 // Deal with possible :after generated content on the parent
8515 nsIFrame* parentAfterFrame;
8516 parentFrame =
8517 ::AdjustAppendParentForAfterContent(mPresShell->GetPresContext(),
8518 aContainer, parentFrame,
8519 &parentAfterFrame);
8521 // Create some new frames
8522 PRUint32 count;
8523 nsFrameItems frameItems;
8524 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
8525 GetAbsoluteContainingBlock(parentFrame),
8526 GetFloatContainingBlock(parentFrame));
8528 // See if the containing block has :first-letter style applied.
8529 PRBool haveFirstLetterStyle = PR_FALSE, haveFirstLineStyle = PR_FALSE;
8530 nsIFrame* containingBlock = state.mFloatedItems.containingBlock;
8531 if (containingBlock) {
8532 haveFirstLetterStyle = HasFirstLetterStyle(containingBlock);
8533 haveFirstLineStyle =
8534 ShouldHaveFirstLineStyle(containingBlock->GetContent(),
8535 containingBlock->GetStyleContext());
8538 if (haveFirstLetterStyle) {
8539 // Before we get going, remove the current letter frames
8540 RemoveLetterFrames(state.mPresContext, state.mPresShell,
8541 state.mFrameManager, containingBlock);
8544 // if the container is a table and a caption was appended, it needs to be put in
8545 // the outer table frame's additional child list.
8546 nsFrameItems captionItems;
8548 // The last frame that we added to the list.
8549 nsIFrame* oldNewFrame = nsnull;
8551 PRUint32 i;
8552 count = aContainer->GetChildCount();
8553 for (i = aNewIndexInContainer; i < count; i++) {
8554 nsIFrame* newFrame = nsnull;
8555 nsIContent *childContent = aContainer->GetChildAt(i);
8557 ConstructFrame(state, childContent, parentFrame, frameItems);
8558 newFrame = frameItems.lastChild;
8560 if (newFrame && newFrame != oldNewFrame) {
8561 InvalidateCanvasIfNeeded(newFrame);
8562 oldNewFrame = newFrame;
8566 if (nsGkAtoms::tableFrame == frameType) {
8567 // Pull out the captions. Note that we don't want to do that as we go,
8568 // because processing a single caption can add a whole bunch of things to
8569 // the frame items due to pseudoframe processing. So we'd have to pull
8570 // captions from a list anyway; might as well do that here.
8571 PullOutCaptionFrames(frameItems, captionItems);
8575 // process the current pseudo frame state
8576 if (!state.mPseudoFrames.IsEmpty()) {
8577 ProcessPseudoFrames(state, frameItems);
8580 if (haveFirstLineStyle && parentFrame == containingBlock) {
8581 // It's possible that some of the new frames go into a
8582 // first-line frame. Look at them and see...
8583 AppendFirstLineFrames(state, containingBlock->GetContent(),
8584 containingBlock, frameItems);
8587 nsresult result = NS_OK;
8589 // Notify the parent frame passing it the list of new frames
8590 if (NS_SUCCEEDED(result) &&
8591 (frameItems.childList || captionItems.childList)) {
8592 // Perform special check for diddling around with the frames in
8593 // a special inline frame.
8595 // If we're appending before :after content, then we're not really
8596 // appending, so let WipeContainingBlock know that.
8597 if (WipeContainingBlock(state, containingBlock, parentFrame, frameItems,
8598 !parentAfterFrame, nsnull)) {
8599 return NS_OK;
8602 // Append the flowed frames to the principal child list, tables need special treatment
8603 if (nsGkAtoms::tableFrame == frameType) {
8604 if (captionItems.childList) { // append the caption to the outer table
8605 nsIFrame* outerTable = parentFrame->GetParent();
8606 if (outerTable) {
8607 state.mFrameManager->AppendFrames(outerTable,
8608 nsGkAtoms::captionList,
8609 captionItems.childList);
8612 if (frameItems.childList) { // append children of the inner table
8613 AppendFrames(state, aContainer, parentFrame, frameItems,
8614 parentAfterFrame);
8617 else {
8618 AppendFrames(state, aContainer, parentFrame, frameItems,
8619 parentAfterFrame);
8623 // Recover first-letter frames
8624 if (haveFirstLetterStyle) {
8625 RecoverLetterFrames(state, containingBlock);
8628 #ifdef DEBUG
8629 if (gReallyNoisyContentUpdates) {
8630 nsIFrameDebug* fdbg = nsnull;
8631 CallQueryInterface(parentFrame, &fdbg);
8632 if (fdbg) {
8633 printf("nsCSSFrameConstructor::ContentAppended: resulting frame model:\n");
8634 fdbg->List(stdout, 0);
8637 #endif
8639 return NS_OK;
8642 #ifdef MOZ_XUL
8644 enum content_operation
8646 CONTENT_INSERTED,
8647 CONTENT_REMOVED
8650 // Helper function to lookup the listbox body frame and send a notification
8651 // for insertion or removal of content
8652 static
8653 PRBool NotifyListBoxBody(nsPresContext* aPresContext,
8654 nsIContent* aContainer,
8655 nsIContent* aChild,
8656 PRInt32 aIndexInContainer,
8657 nsIDocument* aDocument,
8658 nsIFrame* aChildFrame,
8659 PRBool aUseXBLForms,
8660 content_operation aOperation)
8662 if (!aContainer)
8663 return PR_FALSE;
8665 if (aContainer->IsNodeOfType(nsINode::eXUL) &&
8666 aChild->IsNodeOfType(nsINode::eXUL) &&
8667 aContainer->Tag() == nsGkAtoms::listbox &&
8668 aChild->Tag() == nsGkAtoms::listitem) {
8669 nsCOMPtr<nsIDOMXULElement> xulElement = do_QueryInterface(aContainer);
8670 nsCOMPtr<nsIBoxObject> boxObject;
8671 xulElement->GetBoxObject(getter_AddRefs(boxObject));
8672 nsCOMPtr<nsPIListBoxObject> listBoxObject = do_QueryInterface(boxObject);
8673 if (listBoxObject) {
8674 nsIListBoxObject* listboxBody = listBoxObject->GetListBoxBody(PR_FALSE);
8675 if (listboxBody) {
8676 nsListBoxBodyFrame *listBoxBodyFrame = static_cast<nsListBoxBodyFrame*>(listboxBody);
8677 if (aOperation == CONTENT_REMOVED) {
8678 // Except if we have an aChildFrame and its parent is not the right
8679 // thing, then we don't do this. Pseudo frames are so much fun....
8680 if (!aChildFrame || aChildFrame->GetParent() == listBoxBodyFrame) {
8681 listBoxBodyFrame->OnContentRemoved(aPresContext, aChildFrame,
8682 aIndexInContainer);
8683 return PR_TRUE;
8685 } else {
8686 listBoxBodyFrame->OnContentInserted(aPresContext, aChild);
8687 return PR_TRUE;
8693 PRInt32 namespaceID;
8694 aDocument->BindingManager()->ResolveTag(aContainer, &namespaceID);
8696 // XBL form control cruft... should that really be testing that the
8697 // namespace is XUL? Seems odd...
8698 if (aUseXBLForms && aContainer->GetParent() &&
8699 namespaceID == kNameSpaceID_XUL && ShouldIgnoreSelectChild(aContainer))
8700 return PR_TRUE;
8702 return PR_FALSE;
8704 #endif // MOZ_XUL
8706 nsresult
8707 nsCSSFrameConstructor::ContentInserted(nsIContent* aContainer,
8708 nsIContent* aChild,
8709 PRInt32 aIndexInContainer,
8710 nsILayoutHistoryState* aFrameState)
8712 AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
8713 NS_PRECONDITION(mUpdateCount != 0,
8714 "Should be in an update while creating frames");
8716 // XXXldb Do we need to re-resolve style to handle the CSS2 + combinator and
8717 // the :empty pseudo-class?
8718 #ifdef DEBUG
8719 if (gNoisyContentUpdates) {
8720 printf("nsCSSFrameConstructor::ContentInserted container=%p child=%p index=%d\n",
8721 static_cast<void*>(aContainer),
8722 static_cast<void*>(aChild),
8723 aIndexInContainer);
8724 if (gReallyNoisyContentUpdates) {
8725 (aContainer ? aContainer : aChild)->List(stdout, 0);
8728 #endif
8730 nsresult rv = NS_OK;
8732 #ifdef MOZ_XUL
8733 if (NotifyListBoxBody(mPresShell->GetPresContext(), aContainer, aChild,
8734 aIndexInContainer,
8735 mDocument, nsnull, gUseXBLForms, CONTENT_INSERTED))
8736 return NS_OK;
8737 #endif // MOZ_XUL
8739 // If we have a null parent, then this must be the document element
8740 // being inserted
8741 if (! aContainer) {
8742 nsIContent *docElement = mDocument->GetRootContent();
8744 if (aChild == docElement) {
8745 NS_PRECONDITION(nsnull == mInitialContainingBlock, "initial containing block already created");
8747 if (!mDocElementContainingBlock)
8748 return NS_OK; // We get into this situation when an XBL binding is asynchronously
8749 // applied to the root tag (e.g., <window> in XUL). It's ok. We can
8750 // just bail here because the root will really be built later during
8751 // InitialReflow.
8753 // Create frames for the document element and its child elements
8754 nsIFrame* docElementFrame;
8755 nsFrameConstructorState state(mPresShell, mFixedContainingBlock, nsnull,
8756 nsnull, aFrameState);
8757 rv = ConstructDocElementFrame(state,
8758 docElement,
8759 mDocElementContainingBlock,
8760 &docElementFrame);
8762 if (NS_SUCCEEDED(rv) && docElementFrame) {
8763 if (mDocElementContainingBlock->GetStateBits() & NS_FRAME_FIRST_REFLOW) {
8764 // Set the initial child list for the parent and wait on the initial
8765 // reflow.
8766 mDocElementContainingBlock->SetInitialChildList(nsnull,
8767 docElementFrame);
8768 } else {
8769 // Whoops, we've already received our initial reflow! Insert the doc.
8770 // element as a child so it reflows (note that containing block is
8771 // empty, so we can simply append).
8772 NS_ASSERTION(mDocElementContainingBlock->GetFirstChild(nsnull) == nsnull,
8773 "Unexpected child of document element containing block");
8774 mDocElementContainingBlock->AppendFrames(nsnull, docElementFrame);
8776 InvalidateCanvasIfNeeded(docElementFrame);
8777 #ifdef DEBUG
8778 if (gReallyNoisyContentUpdates) {
8779 nsIFrameDebug* fdbg = nsnull;
8780 CallQueryInterface(docElementFrame, &fdbg);
8781 if (fdbg) {
8782 printf("nsCSSFrameConstructor::ContentInserted: resulting frame model:\n");
8783 fdbg->List(stdout, 0);
8786 #endif
8790 // otherwise this is not a child of the root element, and we
8791 // won't let it have a frame.
8792 return NS_OK;
8795 // Otherwise, we've got parent content. Find its frame.
8796 nsIFrame* parentFrame = GetFrameFor(aContainer);
8797 if (! parentFrame)
8798 return NS_OK; // XXXwaterson will this break selects? (See ``Here
8799 // we have been notified...'' below.)
8801 // See if we have an XBL insertion point. If so, then that's our
8802 // real parent frame; if not, then the frame hasn't been built yet
8803 // and we just bail.
8804 nsIFrame* insertionPoint;
8805 GetInsertionPoint(parentFrame, aChild, &insertionPoint);
8806 if (! insertionPoint)
8807 return NS_OK; // Don't build the frames.
8809 parentFrame = insertionPoint;
8811 // Find the frame that precedes the insertion point. Walk backwards
8812 // from the parent frame to get the parent content, because if an
8813 // XBL insertion point is involved, we'll need to use _that_ to find
8814 // the preceding frame.
8815 nsIContent* container = parentFrame->GetContent();
8817 // XXX if the insertionPoint was different from the original
8818 // parentFrame, then aIndexInContainer is most likely completely
8819 // wrong. What we need to do here is remember the original index,
8820 // then as we insert, search the child list where we're about to put
8821 // the new frame to make sure that it appears after any siblings
8822 // with a lower index, and before any siblings with a higher
8823 // index. Same with FindNextSibling(), below.
8824 nsIFrame* prevSibling = (aIndexInContainer >= 0)
8825 ? FindPreviousSibling(container, aIndexInContainer, aChild)
8826 : FindPreviousAnonymousSibling(aContainer, aChild);
8828 PRBool isAppend = PR_FALSE;
8829 nsIFrame* appendAfterFrame; // This is only looked at when isAppend is true
8830 nsIFrame* nextSibling = nsnull;
8832 // If there is no previous sibling, then find the frame that follows
8833 if (! prevSibling) {
8834 nextSibling = (aIndexInContainer >= 0)
8835 ? FindNextSibling(container, aIndexInContainer, aChild)
8836 : FindNextAnonymousSibling(aContainer, aChild);
8839 // Now, find the geometric parent so that we can handle
8840 // continuations properly. Use the prev sibling if we have it;
8841 // otherwise use the next sibling.
8842 if (prevSibling) {
8843 parentFrame = prevSibling->GetParent()->GetContentInsertionFrame();
8845 else if (nextSibling) {
8846 parentFrame = nextSibling->GetParent()->GetContentInsertionFrame();
8848 else {
8849 // No previous or next sibling, so treat this like an appended frame.
8850 isAppend = PR_TRUE;
8851 // Deal with fieldsets
8852 parentFrame = ::GetAdjustedParentFrame(parentFrame, parentFrame->GetType(),
8853 aContainer, aIndexInContainer);
8854 parentFrame =
8855 ::AdjustAppendParentForAfterContent(mPresShell->GetPresContext(),
8856 aContainer, parentFrame,
8857 &appendAfterFrame);
8860 if (parentFrame->GetType() == nsGkAtoms::frameSetFrame &&
8861 IsSpecialFramesetChild(aChild)) {
8862 // Just reframe the parent, since framesets are weird like that.
8863 return RecreateFramesForContent(parentFrame->GetContent());
8866 // Don't construct kids of leaves
8867 if (parentFrame->IsLeaf()) {
8868 return NS_OK;
8871 #ifdef MOZ_MATHML
8872 if (parentFrame->IsFrameOfType(nsIFrame::eMathML))
8873 return RecreateFramesForContent(parentFrame->GetContent());
8874 #endif
8876 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
8877 GetAbsoluteContainingBlock(parentFrame),
8878 GetFloatContainingBlock(parentFrame),
8879 aFrameState);
8882 // Recover state for the containing block - we need to know if
8883 // it has :first-letter or :first-line style applied to it. The
8884 // reason we care is that the internal structure in these cases
8885 // is not the normal structure and requires custom updating
8886 // logic.
8887 nsIFrame* containingBlock = state.mFloatedItems.containingBlock;
8888 PRBool haveFirstLetterStyle = PR_FALSE;
8889 PRBool haveFirstLineStyle = PR_FALSE;
8891 // In order to shave off some cycles, we only dig up the
8892 // containing block haveFirst* flags if the parent frame where
8893 // the insertion/append is occurring is an inline or block
8894 // container. For other types of containers this isn't relevant.
8895 const nsStyleDisplay* parentDisplay = parentFrame->GetStyleDisplay();
8897 // Examine the parentFrame where the insertion is taking
8898 // place. If it's a certain kind of container then some special
8899 // processing is done.
8900 if ((NS_STYLE_DISPLAY_BLOCK == parentDisplay->mDisplay) ||
8901 (NS_STYLE_DISPLAY_LIST_ITEM == parentDisplay->mDisplay) ||
8902 (NS_STYLE_DISPLAY_INLINE == parentDisplay->mDisplay) ||
8903 (NS_STYLE_DISPLAY_INLINE_BLOCK == parentDisplay->mDisplay)) {
8904 // Recover the special style flags for the containing block
8905 if (containingBlock) {
8906 haveFirstLetterStyle = HasFirstLetterStyle(containingBlock);
8907 haveFirstLineStyle =
8908 ShouldHaveFirstLineStyle(containingBlock->GetContent(),
8909 containingBlock->GetStyleContext());
8912 if (haveFirstLetterStyle) {
8913 // Get the correct parentFrame and prevSibling - if a
8914 // letter-frame is present, use its parent.
8915 if (parentFrame->GetType() == nsGkAtoms::letterFrame) {
8916 parentFrame = parentFrame->GetParent();
8917 container = parentFrame->GetContent();
8920 // Remove the old letter frames before doing the insertion
8921 RemoveLetterFrames(state.mPresContext, mPresShell,
8922 state.mFrameManager,
8923 state.mFloatedItems.containingBlock);
8925 // Removing the letterframes messes around with the frame tree, removing
8926 // and creating frames. We need to reget our prevsibling.
8927 // See XXX comment the first time we do this in this method....
8928 prevSibling = (aIndexInContainer >= 0)
8929 ? FindPreviousSibling(container, aIndexInContainer, aChild)
8930 : FindPreviousAnonymousSibling(aContainer, aChild);
8932 // If there is no previous sibling, then find the frame that follows
8933 if (! prevSibling) {
8934 nextSibling = (aIndexInContainer >= 0)
8935 ? FindNextSibling(container, aIndexInContainer, aChild)
8936 : FindNextAnonymousSibling(aContainer, aChild);
8941 if (!prevSibling) {
8942 // We're inserting the new frame as the first child. See if the
8943 // parent has a :before pseudo-element
8944 nsIFrame* firstChild = parentFrame->GetFirstChild(nsnull);
8946 if (firstChild &&
8947 nsLayoutUtils::IsGeneratedContentFor(aContainer, firstChild,
8948 nsCSSPseudoElements::before)) {
8949 // Insert the new frames after the last continuation of the :before
8950 prevSibling = firstChild->GetTailContinuation();
8951 parentFrame = prevSibling->GetParent();
8952 // We perhaps could leave this true and take the AppendFrames path
8953 // below, but we'd have to update appendAfterFrame and it seems safer
8954 // to force all insert-after-:before cases to take these to take the
8955 // InsertFrames path
8956 isAppend = PR_FALSE;
8960 // if the container is a table and a caption will be appended, it needs to be
8961 // put in the outer table frame's additional child list.
8963 nsFrameItems frameItems, captionItems;
8965 ConstructFrame(state, aChild, parentFrame, frameItems);
8966 if (frameItems.childList) {
8967 InvalidateCanvasIfNeeded(frameItems.childList);
8969 if (nsGkAtoms::tableCaptionFrame == frameItems.childList->GetType()) {
8970 NS_ASSERTION(frameItems.childList == frameItems.lastChild ,
8971 "adding a non caption frame to the caption childlist?");
8972 captionItems.AddChild(frameItems.childList);
8973 frameItems = nsFrameItems();
8977 // process the current pseudo frame state
8978 if (!state.mPseudoFrames.IsEmpty())
8979 ProcessPseudoFrames(state, frameItems);
8981 // If the parent of our current prevSibling is different from the frame we'll
8982 // actually use as the parent, then the calculated insertion point is now
8983 // invalid and as it is unknown where to insert correctly we append instead
8984 // (bug 341858).
8985 if (prevSibling && frameItems.childList &&
8986 frameItems.childList->GetParent() != prevSibling->GetParent()) {
8987 prevSibling = nsnull;
8988 isAppend = PR_TRUE;
8989 parentFrame =
8990 ::AdjustAppendParentForAfterContent(mPresShell->GetPresContext(),
8991 aContainer,
8992 frameItems.childList->GetParent(),
8993 &appendAfterFrame);
8996 // Perform special check for diddling around with the frames in
8997 // a special inline frame.
8999 // If we're appending before :after content, then we're not really
9000 // appending, so let WipeContainingBlock know that.
9001 if (WipeContainingBlock(state, containingBlock, parentFrame, frameItems,
9002 isAppend && !appendAfterFrame, prevSibling))
9003 return NS_OK;
9005 if (haveFirstLineStyle && parentFrame == containingBlock) {
9006 // It's possible that the new frame goes into a first-line
9007 // frame. Look at it and see...
9008 if (isAppend) {
9009 // Use append logic when appending
9010 AppendFirstLineFrames(state, containingBlock->GetContent(),
9011 containingBlock, frameItems);
9013 else {
9014 // Use more complicated insert logic when inserting
9015 InsertFirstLineFrames(state, aContainer, containingBlock, &parentFrame,
9016 prevSibling, frameItems);
9020 nsIFrame* const newFrame = frameItems.childList;
9021 if (NS_SUCCEEDED(rv) && newFrame) {
9022 NS_ASSERTION(!captionItems.childList, "leaking caption frames");
9023 // Notify the parent frame
9024 if (isAppend) {
9025 AppendFrames(state, aContainer, parentFrame, frameItems,
9026 appendAfterFrame);
9027 } else {
9028 state.mFrameManager->InsertFrames(parentFrame,
9029 nsnull, prevSibling, newFrame);
9032 else {
9033 // we might have a caption treat it here
9034 nsIFrame* newCaptionFrame = captionItems.childList;
9035 if (NS_SUCCEEDED(rv) && newCaptionFrame) {
9036 nsIFrame* outerTableFrame;
9037 if (GetCaptionAdjustedParent(parentFrame, newCaptionFrame, &outerTableFrame)) {
9038 // If the parent of our current prevSibling is different from the frame
9039 // we'll actually use as the parent, then the calculated insertion
9040 // point is now invalid (bug 341382).
9041 if (prevSibling && prevSibling->GetParent() != outerTableFrame) {
9042 prevSibling = nsnull;
9044 // If the parent is not a outer table frame we will try to add frames
9045 // to a named child list that the parent does not honour and the frames
9046 // will get lost
9047 NS_ASSERTION(nsGkAtoms::tableOuterFrame == outerTableFrame->GetType(),
9048 "Pseudo frame construction failure, "
9049 "a caption can be only a child of a outer table frame");
9050 if (isAppend) {
9051 state.mFrameManager->AppendFrames(outerTableFrame,
9052 nsGkAtoms::captionList,
9053 newCaptionFrame);
9055 else {
9056 state.mFrameManager->InsertFrames(outerTableFrame,
9057 nsGkAtoms::captionList,
9058 prevSibling, newCaptionFrame);
9064 if (haveFirstLetterStyle) {
9065 // Recover the letter frames for the containing block when
9066 // it has first-letter style.
9067 RecoverLetterFrames(state, state.mFloatedItems.containingBlock);
9070 #ifdef DEBUG
9071 if (gReallyNoisyContentUpdates && parentFrame) {
9072 nsIFrameDebug* fdbg = nsnull;
9073 CallQueryInterface(parentFrame, &fdbg);
9074 if (fdbg) {
9075 printf("nsCSSFrameConstructor::ContentInserted: resulting frame model:\n");
9076 fdbg->List(stdout, 0);
9079 #endif
9081 return NS_OK;
9084 nsresult
9085 nsCSSFrameConstructor::ReinsertContent(nsIContent* aContainer,
9086 nsIContent* aChild)
9088 PRInt32 ix = aContainer->IndexOf(aChild);
9089 // XXX For now, do a brute force remove and insert.
9090 // XXXbz this probably doesn't work so well with anonymous content
9091 // XXXbz doesn't this need to do the state-saving stuff that
9092 // RecreateFramesForContent does?
9093 PRBool didReconstruct;
9094 nsresult res = ContentRemoved(aContainer, aChild, ix, &didReconstruct);
9096 if (NS_SUCCEEDED(res) && !didReconstruct) {
9097 // If ContentRemoved just reconstructed everything, there is no need to
9098 // reinsert the content here
9099 res = ContentInserted(aContainer, aChild, ix, nsnull);
9102 return res;
9106 * Called when a frame subtree is about to be deleted. Two important
9107 * things happen:
9109 * 1. For each frame in the subtree, we remove the mapping from the
9110 * content object to its frame
9112 * 2. For child frames that have been moved out of the flow, we enqueue
9113 * the out-of-flow frame for deletion *if* the out-of-flow frame's
9114 * geometric parent is not in |aRemovedFrame|'s hierarchy (e.g., an
9115 * absolutely positioned element that has been promoted to be a direct
9116 * descendant of an area frame).
9118 * Note: this function should only be called by DeletingFrameSubtree()
9120 * @param aRemovedFrame this is the frame that was removed from the
9121 * content model. As we recurse we need to remember this so we
9122 * can check if out-of-flow frames are a descendant of the frame
9123 * being removed
9124 * @param aFrame the local subtree that is being deleted. This is initially
9125 * the same as aRemovedFrame, but as we recurse down the tree
9126 * this changes
9128 static nsresult
9129 DoDeletingFrameSubtree(nsFrameManager* aFrameManager,
9130 nsVoidArray& aDestroyQueue,
9131 nsIFrame* aRemovedFrame,
9132 nsIFrame* aFrame)
9134 // Remove the mapping from the content object to its frame.
9135 nsIContent* content = aFrame->GetContent();
9136 if (content) {
9137 aFrameManager->RemoveAsPrimaryFrame(content, aFrame);
9138 aFrameManager->ClearAllUndisplayedContentIn(content);
9141 nsIAtom* childListName = nsnull;
9142 PRInt32 childListIndex = 0;
9144 do {
9145 // Walk aFrame's normal flow child frames looking for placeholder frames.
9146 nsIFrame* childFrame = aFrame->GetFirstChild(childListName);
9147 for (; childFrame; childFrame = childFrame->GetNextSibling()) {
9148 if (NS_LIKELY(nsGkAtoms::placeholderFrame != childFrame->GetType())) {
9149 DoDeletingFrameSubtree(aFrameManager, aDestroyQueue,
9150 aRemovedFrame, childFrame);
9152 } else {
9153 nsIFrame* outOfFlowFrame =
9154 nsPlaceholderFrame::GetRealFrameForPlaceholder(childFrame);
9156 // Remove the mapping from the out-of-flow frame to its placeholder.
9157 aFrameManager->UnregisterPlaceholderFrame((nsPlaceholderFrame*)childFrame);
9158 // Don't SetOutOfFlowFrame(nsnull) here because the float cache depends
9159 // on it when the float is removed later on, see bug 348688 comment 6.
9161 // Queue the out-of-flow frame to be destroyed only if aRemovedFrame is _not_
9162 // one of its ancestor frames or if it is a popup frame.
9163 // If aRemovedFrame is an ancestor of the out-of-flow frame, then
9164 // the out-of-flow frame will be destroyed by aRemovedFrame.
9165 if (outOfFlowFrame->GetStyleDisplay()->mDisplay == NS_STYLE_DISPLAY_POPUP ||
9166 !nsLayoutUtils::IsProperAncestorFrame(aRemovedFrame, outOfFlowFrame)) {
9167 NS_ASSERTION(aDestroyQueue.IndexOf(outOfFlowFrame) == kNotFound,
9168 "out-of-flow is already in the destroy queue");
9169 aDestroyQueue.AppendElement(outOfFlowFrame);
9170 // Recurse into the out-of-flow, it is now the aRemovedFrame.
9171 DoDeletingFrameSubtree(aFrameManager, aDestroyQueue,
9172 outOfFlowFrame, outOfFlowFrame);
9174 else {
9175 // Also recurse into the out-of-flow when it's a descendant of aRemovedFrame
9176 // since we don't walk those lists, see |childListName| increment below.
9177 DoDeletingFrameSubtree(aFrameManager, aDestroyQueue,
9178 aRemovedFrame, outOfFlowFrame);
9183 // Move to next child list but skip lists with frames we should have
9184 // a placeholder for.
9185 do {
9186 childListName = aFrame->GetAdditionalChildListName(childListIndex++);
9187 } while (IsOutOfFlowList(childListName));
9188 } while (childListName);
9190 return NS_OK;
9194 * Called when a frame is about to be deleted. Calls DoDeletingFrameSubtree()
9195 * for aFrame and each of its continuing frames
9197 static nsresult
9198 DeletingFrameSubtree(nsFrameManager* aFrameManager,
9199 nsIFrame* aFrame)
9201 NS_ENSURE_TRUE(aFrame, NS_OK); // XXXldb Remove this sometime in the future.
9203 // If there's no frame manager it's probably because the pres shell is
9204 // being destroyed.
9205 if (NS_UNLIKELY(!aFrameManager)) {
9206 return NS_OK;
9209 nsAutoVoidArray destroyQueue;
9211 // If it's a "special" block-in-inline frame, then we can't really deal.
9212 // That really shouldn't be happening.
9213 NS_ASSERTION(!IsFrameSpecial(aFrame),
9214 "DeletingFrameSubtree on a special frame. Prepare to crash.");
9216 do {
9217 DoDeletingFrameSubtree(aFrameManager, destroyQueue, aFrame, aFrame);
9219 // If it's split, then get the continuing frame. Note that we only do
9220 // this for the top-most frame being deleted. Don't do it if we're
9221 // recursing over a subtree, because those continuing frames should be
9222 // found as part of the walk over the top-most frame's continuing frames.
9223 // Walking them again will make this an N^2/2 algorithm.
9224 aFrame = aFrame->GetNextContinuation();
9225 } while (aFrame);
9227 // Now destroy any out-of-flow frames that have been enqueued for
9228 // destruction.
9229 for (PRInt32 i = destroyQueue.Count() - 1; i >= 0; --i) {
9230 nsIFrame* outOfFlowFrame = static_cast<nsIFrame*>(destroyQueue[i]);
9232 // Ask the out-of-flow's parent to delete the out-of-flow
9233 // frame from the right list.
9234 aFrameManager->RemoveFrame(outOfFlowFrame->GetParent(),
9235 GetChildListNameFor(outOfFlowFrame),
9236 outOfFlowFrame);
9239 return NS_OK;
9242 nsresult
9243 nsCSSFrameConstructor::RemoveMappingsForFrameSubtree(nsIFrame* aRemovedFrame)
9245 if (NS_UNLIKELY(mIsDestroyingFrameTree)) {
9246 // The frame tree might not be in a consistent state after
9247 // WillDestroyFrameTree() has been called. Most likely we're destroying
9248 // the pres shell which means the frame manager takes care of clearing all
9249 // mappings so there is no need to walk the frame tree here, bug 372576.
9250 return NS_OK;
9253 // Save the frame tree's state before deleting it
9254 CaptureStateFor(aRemovedFrame, mTempFrameTreeState);
9256 return ::DeletingFrameSubtree(mPresShell->FrameManager(), aRemovedFrame);
9259 static void UnregisterPlaceholderChain(nsFrameManager* frameManager,
9260 nsPlaceholderFrame* placeholderFrame)
9262 // Remove the mapping from the frame to its placeholder
9263 nsPlaceholderFrame* curFrame = placeholderFrame;
9264 do {
9265 frameManager->UnregisterPlaceholderFrame(curFrame);
9266 curFrame->SetOutOfFlowFrame(nsnull);
9267 curFrame = static_cast<nsPlaceholderFrame*>(curFrame->GetNextContinuation());
9268 } while (curFrame);
9271 nsresult
9272 nsCSSFrameConstructor::ContentRemoved(nsIContent* aContainer,
9273 nsIContent* aChild,
9274 PRInt32 aIndexInContainer,
9275 PRBool* aDidReconstruct)
9277 AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
9278 NS_PRECONDITION(mUpdateCount != 0,
9279 "Should be in an update while destroying frames");
9281 *aDidReconstruct = PR_FALSE;
9283 // XXXldb Do we need to re-resolve style to handle the CSS2 + combinator and
9284 // the :empty pseudo-class?
9286 #ifdef DEBUG
9287 if (gNoisyContentUpdates) {
9288 printf("nsCSSFrameConstructor::ContentRemoved container=%p child=%p index=%d\n",
9289 static_cast<void*>(aContainer),
9290 static_cast<void*>(aChild),
9291 aIndexInContainer);
9292 if (gReallyNoisyContentUpdates) {
9293 aContainer->List(stdout, 0);
9296 #endif
9298 nsFrameManager *frameManager = mPresShell->FrameManager();
9299 nsPresContext *presContext = mPresShell->GetPresContext();
9300 nsresult rv = NS_OK;
9302 // Find the child frame that maps the content
9303 nsIFrame* childFrame =
9304 mPresShell->FrameManager()->GetPrimaryFrameFor(aChild, aIndexInContainer);
9306 if (! childFrame) {
9307 frameManager->ClearUndisplayedContentIn(aChild, aContainer);
9310 #ifdef MOZ_XUL
9311 if (NotifyListBoxBody(presContext, aContainer, aChild, aIndexInContainer,
9312 mDocument, childFrame, gUseXBLForms, CONTENT_REMOVED))
9313 return NS_OK;
9315 #endif // MOZ_XUL
9317 if (childFrame) {
9318 InvalidateCanvasIfNeeded(childFrame);
9320 // If the frame we are manipulating is a special frame then do
9321 // something different instead of just inserting newly created
9322 // frames.
9323 // NOTE: if we are in ReinsertContent,
9324 // then do not reframe as we are already doing just that!
9325 if (MaybeRecreateContainerForIBSplitterFrame(childFrame, &rv)) {
9326 *aDidReconstruct = PR_TRUE;
9327 return rv;
9330 // Get the childFrame's parent frame
9331 nsIFrame* parentFrame = childFrame->GetParent();
9332 nsIAtom* parentType = parentFrame->GetType();
9334 if (parentType == nsGkAtoms::frameSetFrame &&
9335 IsSpecialFramesetChild(aChild)) {
9336 // Just reframe the parent, since framesets are weird like that.
9337 *aDidReconstruct = PR_TRUE;
9338 return RecreateFramesForContent(parentFrame->GetContent());
9341 #ifdef MOZ_MATHML
9342 // If we're a child of MathML, then we should reframe the MathML content.
9343 // If we're non-MathML, then we would be wrapped in a block so we need to
9344 // check our grandparent in that case.
9345 nsIFrame* possibleMathMLAncestor = parentType == nsGkAtoms::blockFrame ?
9346 parentFrame->GetParent() : parentFrame;
9347 if (possibleMathMLAncestor->IsFrameOfType(nsIFrame::eMathML)) {
9348 *aDidReconstruct = PR_TRUE;
9349 return RecreateFramesForContent(possibleMathMLAncestor->GetContent());
9351 #endif
9353 // Undo XUL wrapping if it's no longer needed.
9354 // (If we're in the XUL block-wrapping situation, parentFrame is the
9355 // wrapper frame.)
9356 nsIFrame* grandparentFrame = parentFrame->GetParent();
9357 if (grandparentFrame && grandparentFrame->IsBoxFrame() &&
9358 (grandparentFrame->GetStateBits() & NS_STATE_BOX_WRAPS_KIDS_IN_BLOCK) &&
9359 // check if this frame is the only one needing wrapping
9360 aChild == AnyKidsNeedBlockParent(parentFrame->GetFirstChild(nsnull)) &&
9361 !AnyKidsNeedBlockParent(childFrame->GetNextSibling())) {
9362 *aDidReconstruct = PR_TRUE;
9363 return RecreateFramesForContent(grandparentFrame->GetContent());
9366 // Examine the containing-block for the removed content and see if
9367 // :first-letter style applies.
9368 nsIFrame* containingBlock = GetFloatContainingBlock(parentFrame);
9369 PRBool haveFLS = containingBlock && HasFirstLetterStyle(containingBlock);
9370 if (haveFLS) {
9371 // Trap out to special routine that handles adjusting a blocks
9372 // frame tree when first-letter style is present.
9373 #ifdef NOISY_FIRST_LETTER
9374 printf("ContentRemoved: containingBlock=");
9375 nsFrame::ListTag(stdout, containingBlock);
9376 printf(" parentFrame=");
9377 nsFrame::ListTag(stdout, parentFrame);
9378 printf(" childFrame=");
9379 nsFrame::ListTag(stdout, childFrame);
9380 printf("\n");
9381 #endif
9383 // First update the containing blocks structure by removing the
9384 // existing letter frames. This makes the subsequent logic
9385 // simpler.
9386 RemoveLetterFrames(presContext, mPresShell, frameManager,
9387 containingBlock);
9389 // Recover childFrame and parentFrame
9390 childFrame = mPresShell->GetPrimaryFrameFor(aChild);
9391 if (!childFrame) {
9392 frameManager->ClearUndisplayedContentIn(aChild, aContainer);
9393 return NS_OK;
9395 parentFrame = childFrame->GetParent();
9397 #ifdef NOISY_FIRST_LETTER
9398 printf(" ==> revised parentFrame=");
9399 nsFrame::ListTag(stdout, parentFrame);
9400 printf(" childFrame=");
9401 nsFrame::ListTag(stdout, childFrame);
9402 printf("\n");
9403 #endif
9406 #ifdef DEBUG
9407 if (gReallyNoisyContentUpdates) {
9408 printf("nsCSSFrameConstructor::ContentRemoved: childFrame=");
9409 nsFrame::ListTag(stdout, childFrame);
9410 printf("\n");
9412 nsIFrameDebug* fdbg = nsnull;
9413 CallQueryInterface(parentFrame, &fdbg);
9414 if (fdbg)
9415 fdbg->List(stdout, 0);
9417 #endif
9419 // Walk the frame subtree deleting any out-of-flow frames, and
9420 // remove the mapping from content objects to frames
9421 ::DeletingFrameSubtree(frameManager, childFrame);
9423 // See if the child frame is an out-of-flow
9424 if (childFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW) {
9425 nsPlaceholderFrame* placeholderFrame =
9426 frameManager->GetPlaceholderFrameFor(childFrame);
9427 NS_ASSERTION(placeholderFrame, "No placeholder for out-of-flow?");
9429 UnregisterPlaceholderChain(frameManager, placeholderFrame);
9431 // Now we remove the out-of-flow frame
9432 // XXX has to be done first for now: for floats, the block's line list
9433 // contains an array of pointers to the placeholder - we have to
9434 // remove the float first (which gets rid of the lines
9435 // reference to the placeholder and float) and then remove the
9436 // placeholder
9437 rv = frameManager->RemoveFrame(parentFrame,
9438 GetChildListNameFor(childFrame),
9439 childFrame);
9441 // Remove the placeholder frame first (XXX second for now) (so
9442 // that it doesn't retain a dangling pointer to memory)
9443 nsIFrame* placeholderParent = placeholderFrame->GetParent();
9444 ::DeletingFrameSubtree(frameManager, placeholderFrame);
9445 rv |= frameManager->RemoveFrame(placeholderParent,
9446 nsnull, placeholderFrame);
9447 } else {
9448 // Notify the parent frame that it should delete the frame
9449 // check for a table caption which goes on an additional child list with a different parent
9450 nsIFrame* outerTableFrame;
9451 if (GetCaptionAdjustedParent(parentFrame, childFrame, &outerTableFrame)) {
9452 rv = frameManager->RemoveFrame(outerTableFrame,
9453 nsGkAtoms::captionList,
9454 childFrame);
9456 else {
9457 rv = frameManager->RemoveFrame(parentFrame, nsnull, childFrame);
9461 if (mInitialContainingBlock == childFrame) {
9462 mInitialContainingBlock = nsnull;
9463 mRootElementStyleFrame = nsnull;
9466 if (haveFLS && mInitialContainingBlock) {
9467 NS_ASSERTION(containingBlock == GetFloatContainingBlock(parentFrame),
9468 "What happened here?");
9469 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
9470 GetAbsoluteContainingBlock(parentFrame),
9471 containingBlock);
9472 RecoverLetterFrames(state, containingBlock);
9475 #ifdef DEBUG
9476 if (gReallyNoisyContentUpdates && parentFrame) {
9477 nsIFrameDebug* fdbg = nsnull;
9478 CallQueryInterface(parentFrame, &fdbg);
9479 if (fdbg) {
9480 printf("nsCSSFrameConstructor::ContentRemoved: resulting frame model:\n");
9481 fdbg->List(stdout, 0);
9484 #endif
9487 return rv;
9490 #ifdef DEBUG
9491 // To ensure that the functions below are only called within
9492 // |ApplyRenderingChangeToTree|.
9493 static PRBool gInApplyRenderingChangeToTree = PR_FALSE;
9494 #endif
9496 static void
9497 DoApplyRenderingChangeToTree(nsIFrame* aFrame,
9498 nsIViewManager* aViewManager,
9499 nsFrameManager* aFrameManager,
9500 nsChangeHint aChange);
9503 * @param aBoundsRect returns the bounds enclosing the areas covered by aFrame and its childre
9504 * This rect is relative to aFrame's parent
9506 static void
9507 UpdateViewsForTree(nsIFrame* aFrame, nsIViewManager* aViewManager,
9508 nsFrameManager* aFrameManager,
9509 nsChangeHint aChange)
9511 NS_PRECONDITION(gInApplyRenderingChangeToTree,
9512 "should only be called within ApplyRenderingChangeToTree");
9514 nsIView* view = aFrame->GetView();
9515 if (view) {
9516 if (aChange & nsChangeHint_SyncFrameView) {
9517 nsContainerFrame::SyncFrameViewProperties(aFrame->PresContext(),
9518 aFrame, nsnull, view);
9522 // now do children of frame
9523 PRInt32 listIndex = 0;
9524 nsIAtom* childList = nsnull;
9526 do {
9527 nsIFrame* child = aFrame->GetFirstChild(childList);
9528 while (child) {
9529 if (!(child->GetStateBits() & NS_FRAME_OUT_OF_FLOW)
9530 || (child->GetStateBits() & NS_FRAME_IS_OVERFLOW_CONTAINER)) {
9531 // only do frames that don't have placeholders
9532 if (nsGkAtoms::placeholderFrame == child->GetType()) { // placeholder
9533 // get out of flow frame and start over there
9534 nsIFrame* outOfFlowFrame =
9535 nsPlaceholderFrame::GetRealFrameForPlaceholder(child);
9537 DoApplyRenderingChangeToTree(outOfFlowFrame, aViewManager,
9538 aFrameManager, aChange);
9540 else { // regular frame
9541 UpdateViewsForTree(child, aViewManager, aFrameManager, aChange);
9544 child = child->GetNextSibling();
9546 childList = aFrame->GetAdditionalChildListName(listIndex++);
9547 } while (childList);
9550 static void
9551 DoApplyRenderingChangeToTree(nsIFrame* aFrame,
9552 nsIViewManager* aViewManager,
9553 nsFrameManager* aFrameManager,
9554 nsChangeHint aChange)
9556 NS_PRECONDITION(gInApplyRenderingChangeToTree,
9557 "should only be called within ApplyRenderingChangeToTree");
9559 for ( ; aFrame; aFrame = nsLayoutUtils::GetNextContinuationOrSpecialSibling(aFrame)) {
9560 // Get view if this frame has one and trigger an update. If the
9561 // frame doesn't have a view, find the nearest containing view
9562 // (adjusting r's coordinate system to reflect the nesting) and
9563 // update there.
9564 UpdateViewsForTree(aFrame, aViewManager, aFrameManager, aChange);
9566 // if frame has view, will already be invalidated
9567 if ((aChange & nsChangeHint_RepaintFrame) &&
9568 !aFrame->IsFrameOfType(nsIFrame::eSVG)) {
9569 aFrame->Invalidate(aFrame->GetOverflowRect());
9574 static void
9575 ApplyRenderingChangeToTree(nsPresContext* aPresContext,
9576 nsIFrame* aFrame,
9577 nsChangeHint aChange)
9579 nsIPresShell *shell = aPresContext->PresShell();
9580 PRBool isPaintingSuppressed = PR_FALSE;
9581 shell->IsPaintingSuppressed(&isPaintingSuppressed);
9582 if (isPaintingSuppressed) {
9583 // Don't allow synchronous rendering changes when painting is turned off.
9584 aChange = NS_SubtractHint(aChange, nsChangeHint_RepaintFrame);
9585 if (!aChange) {
9586 return;
9590 // If the frame's background is propagated to an ancestor, walk up to
9591 // that ancestor.
9592 const nsStyleBackground *bg;
9593 PRBool isCanvas;
9594 while (!nsCSSRendering::FindBackground(aPresContext, aFrame,
9595 &bg, &isCanvas)) {
9596 aFrame = aFrame->GetParent();
9597 NS_ASSERTION(aFrame, "root frame must paint");
9600 nsIViewManager* viewManager = aPresContext->GetViewManager();
9602 // Trigger rendering updates by damaging this frame and any
9603 // continuations of this frame.
9605 // XXX this needs to detect the need for a view due to an opacity change and deal with it...
9607 nsIViewManager::UpdateViewBatch batch(viewManager);
9609 #ifdef DEBUG
9610 gInApplyRenderingChangeToTree = PR_TRUE;
9611 #endif
9612 DoApplyRenderingChangeToTree(aFrame, viewManager, shell->FrameManager(),
9613 aChange);
9614 #ifdef DEBUG
9615 gInApplyRenderingChangeToTree = PR_FALSE;
9616 #endif
9618 batch.EndUpdateViewBatch(NS_VMREFRESH_NO_SYNC);
9622 * This method invalidates the canvas when frames are removed or added for a
9623 * node that might have its background propagated to the canvas, i.e., a
9624 * document root node or an HTML BODY which is a child of the root node.
9626 * @param aFrame a frame for a content node about to be removed or a frme that
9627 * was just created for a content node that was inserted.
9629 static void
9630 InvalidateCanvasIfNeeded(nsIFrame* aFrame)
9632 NS_ASSERTION(aFrame, "Must have frame!");
9634 // Note that for both in ContentRemoved and ContentInserted the content node
9635 // will still have the right parent pointer, so looking at that is ok.
9637 nsIContent* node = aFrame->GetContent();
9638 nsIContent* parent = node->GetParent();
9639 if (parent) {
9640 // Has a parent; might not be what we want
9641 nsIContent* grandParent = parent->GetParent();
9642 if (grandParent) {
9643 // Has a grandparent, so not what we want
9644 return;
9647 // Check whether it's an HTML body
9648 if (node->Tag() != nsGkAtoms::body ||
9649 !node->IsNodeOfType(nsINode::eHTML)) {
9650 return;
9654 // At this point the node has no parent or it's an HTML <body> child of the
9655 // root. We might not need to invalidate in this case (eg we might be in
9656 // XHTML or something), but chances are we want to. Play it safe. Find the
9657 // frame to invalidate and do it.
9658 nsIFrame *ancestor = aFrame;
9659 const nsStyleBackground *bg;
9660 PRBool isCanvas;
9661 nsPresContext* presContext = aFrame->PresContext();
9662 while (!nsCSSRendering::FindBackground(presContext, ancestor,
9663 &bg, &isCanvas)) {
9664 ancestor = ancestor->GetParent();
9665 NS_ASSERTION(ancestor, "canvas must paint");
9668 if (ancestor->GetType() == nsGkAtoms::canvasFrame) {
9669 // The canvas frame's dimensions are not meaningful; invalidate the
9670 // viewport instead.
9671 ancestor = ancestor->GetParent();
9674 if (ancestor != aFrame) {
9675 // Wrap this in a DEFERRED view update batch so we don't try to
9676 // flush out layout here
9678 nsIViewManager::UpdateViewBatch batch(presContext->GetViewManager());
9679 ApplyRenderingChangeToTree(presContext, ancestor,
9680 nsChangeHint_RepaintFrame);
9681 batch.EndUpdateViewBatch(NS_VMREFRESH_DEFERRED);
9685 nsresult
9686 nsCSSFrameConstructor::StyleChangeReflow(nsIFrame* aFrame)
9688 // If the frame hasn't even received an initial reflow, then don't
9689 // send it a style-change reflow!
9690 if (aFrame->GetStateBits() & NS_FRAME_FIRST_REFLOW)
9691 return NS_OK;
9693 #ifdef DEBUG
9694 if (gNoisyContentUpdates) {
9695 printf("nsCSSFrameConstructor::StyleChangeReflow: aFrame=");
9696 nsFrame::ListTag(stdout, aFrame);
9697 printf("\n");
9699 #endif
9701 // If the frame is part of a split block-in-inline hierarchy, then
9702 // target the style-change reflow at the first ``normal'' ancestor
9703 // so we're sure that the style change will propagate to any
9704 // anonymously created siblings.
9705 if (IsFrameSpecial(aFrame))
9706 aFrame = GetIBContainingBlockFor(aFrame);
9708 do {
9709 mPresShell->FrameNeedsReflow(aFrame, nsIPresShell::eStyleChange,
9710 NS_FRAME_IS_DIRTY);
9711 aFrame = aFrame->GetNextContinuation();
9712 } while (aFrame);
9714 return NS_OK;
9717 nsresult
9718 nsCSSFrameConstructor::CharacterDataChanged(nsIContent* aContent,
9719 PRBool aAppend)
9721 AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
9722 nsresult rv = NS_OK;
9724 // Find the child frame
9725 nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
9727 // Notify the first frame that maps the content. It will generate a reflow
9728 // command
9730 // It's possible the frame whose content changed isn't inserted into the
9731 // frame hierarchy yet, or that there is no frame that maps the content
9732 if (nsnull != frame) {
9733 #if 0
9734 NS_FRAME_LOG(NS_FRAME_TRACE_CALLS,
9735 ("nsCSSFrameConstructor::CharacterDataChanged: content=%p[%s] subcontent=%p frame=%p",
9736 aContent, ContentTag(aContent, 0),
9737 aSubContent, frame));
9738 #endif
9740 // Special check for text content that is a child of a letter frame. If
9741 // this happens, we should remove the letter frame, do whatever we're
9742 // planning to do with this notification, then put the letter frame back.
9743 // Note that this is basically what ReinsertContent ends up doing; the
9744 // reason we dont' want to call that here is that our text content could be
9745 // native anonymous, in which case ReinsertContent would completely barf on
9746 // it. And reinserting the non-anonymous ancestor would just lead us to
9747 // come back into this notification (e.g. if quotes or counters are
9748 // involved), leading to a loop.
9749 nsIFrame* block = GetFloatContainingBlock(frame);
9750 PRBool haveFirstLetterStyle = PR_FALSE;
9751 if (block) {
9752 // See if the block has first-letter style applied to it.
9753 haveFirstLetterStyle = HasFirstLetterStyle(block);
9754 if (haveFirstLetterStyle) {
9755 RemoveLetterFrames(mPresShell->GetPresContext(), mPresShell,
9756 mPresShell->FrameManager(), block);
9757 // Reget |frame|, since we might have killed it.
9758 // Do we really need to call CharacterDataChanged in this case, though?
9759 frame = mPresShell->GetPrimaryFrameFor(aContent);
9760 NS_ASSERTION(frame, "Should have frame here!");
9764 frame->CharacterDataChanged(mPresShell->GetPresContext(), aContent,
9765 aAppend);
9767 if (haveFirstLetterStyle) {
9768 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
9769 GetAbsoluteContainingBlock(frame),
9770 block, nsnull);
9771 RecoverLetterFrames(state, block);
9775 return rv;
9778 nsresult
9779 nsCSSFrameConstructor::ProcessRestyledFrames(nsStyleChangeList& aChangeList)
9781 PRInt32 count = aChangeList.Count();
9782 if (!count)
9783 return NS_OK;
9785 // Make sure to not rebuild quote or counter lists while we're
9786 // processing restyles
9787 BeginUpdate();
9789 nsPropertyTable *propTable = mPresShell->GetPresContext()->PropertyTable();
9791 // Mark frames so that we skip frames that die along the way, bug 123049.
9792 // A frame can be in the list multiple times with different hints. Further
9793 // optmization is possible if nsStyleChangeList::AppendChange could coalesce
9794 PRInt32 index = count;
9796 while (0 <= --index) {
9797 const nsStyleChangeData* changeData;
9798 aChangeList.ChangeAt(index, &changeData);
9799 if (changeData->mFrame) {
9800 propTable->SetProperty(changeData->mFrame,
9801 nsGkAtoms::changeListProperty,
9802 nsnull, nsnull, nsnull);
9806 index = count;
9807 while (0 <= --index) {
9808 nsIFrame* frame;
9809 nsIContent* content;
9810 nsChangeHint hint;
9811 aChangeList.ChangeAt(index, frame, content, hint);
9813 // skip any frame that has been destroyed due to a ripple effect
9814 if (frame) {
9815 nsresult res;
9817 propTable->GetProperty(frame, nsGkAtoms::changeListProperty, &res);
9819 if (NS_PROPTABLE_PROP_NOT_THERE == res)
9820 continue;
9823 if (hint & nsChangeHint_ReconstructFrame) {
9824 RecreateFramesForContent(content);
9825 } else {
9826 NS_ASSERTION(frame, "This shouldn't happen");
9827 #ifdef MOZ_SVG
9828 if (hint & nsChangeHint_UpdateEffects) {
9829 nsSVGUtils::UpdateEffects(frame);
9831 #endif
9832 if (hint & nsChangeHint_ReflowFrame) {
9833 StyleChangeReflow(frame);
9835 if (hint & (nsChangeHint_RepaintFrame | nsChangeHint_SyncFrameView)) {
9836 ApplyRenderingChangeToTree(mPresShell->GetPresContext(), frame, hint);
9838 if (hint & nsChangeHint_UpdateCursor) {
9839 nsIViewManager* viewMgr = mPresShell->GetViewManager();
9840 if (viewMgr)
9841 viewMgr->SynthesizeMouseMove(PR_FALSE);
9846 EndUpdate();
9848 // cleanup references and verify the style tree. Note that the latter needs
9849 // to happen once we've processed the whole list, since until then the tree
9850 // is not in fact in a consistent state.
9851 index = count;
9852 while (0 <= --index) {
9853 const nsStyleChangeData* changeData;
9854 aChangeList.ChangeAt(index, &changeData);
9855 if (changeData->mFrame) {
9856 propTable->DeleteProperty(changeData->mFrame,
9857 nsGkAtoms::changeListProperty);
9860 #ifdef DEBUG
9861 // reget frame from content since it may have been regenerated...
9862 if (changeData->mContent) {
9863 nsIFrame* frame = mPresShell->GetPrimaryFrameFor(changeData->mContent);
9864 if (frame) {
9865 mPresShell->FrameManager()->DebugVerifyStyleTree(frame);
9867 } else {
9868 NS_WARNING("Unable to test style tree integrity -- no content node");
9870 #endif
9873 aChangeList.Clear();
9874 return NS_OK;
9877 void
9878 nsCSSFrameConstructor::RestyleElement(nsIContent *aContent,
9879 nsIFrame *aPrimaryFrame,
9880 nsChangeHint aMinHint)
9882 NS_ASSERTION(aPrimaryFrame == mPresShell->GetPrimaryFrameFor(aContent),
9883 "frame/content mismatch");
9884 NS_ASSERTION(!aPrimaryFrame || aPrimaryFrame->GetContent() == aContent,
9885 "frame/content mismatch");
9887 if (aMinHint & nsChangeHint_ReconstructFrame) {
9888 RecreateFramesForContent(aContent);
9889 } else if (aPrimaryFrame) {
9890 nsStyleChangeList changeList;
9891 mPresShell->FrameManager()->
9892 ComputeStyleChangeFor(aPrimaryFrame, &changeList, aMinHint);
9893 ProcessRestyledFrames(changeList);
9894 } else {
9895 // no frames, reconstruct for content
9896 MaybeRecreateFramesForContent(aContent);
9900 void
9901 nsCSSFrameConstructor::RestyleLaterSiblings(nsIContent *aContent)
9903 nsIContent *parent = aContent->GetParent();
9904 if (!parent)
9905 return; // root element has no later siblings
9907 for (PRInt32 index = parent->IndexOf(aContent) + 1,
9908 index_end = parent->GetChildCount();
9909 index != index_end; ++index) {
9910 nsIContent *child = parent->GetChildAt(index);
9911 if (!child->IsNodeOfType(nsINode::eELEMENT))
9912 continue;
9914 nsIFrame* primaryFrame = mPresShell->GetPrimaryFrameFor(child);
9915 RestyleElement(child, primaryFrame, NS_STYLE_HINT_NONE);
9919 nsresult
9920 nsCSSFrameConstructor::ContentStatesChanged(nsIContent* aContent1,
9921 nsIContent* aContent2,
9922 PRInt32 aStateMask)
9924 DoContentStateChanged(aContent1, aStateMask);
9925 DoContentStateChanged(aContent2, aStateMask);
9926 return NS_OK;
9929 void
9930 nsCSSFrameConstructor::DoContentStateChanged(nsIContent* aContent,
9931 PRInt32 aStateMask)
9933 nsStyleSet *styleSet = mPresShell->StyleSet();
9934 nsPresContext *presContext = mPresShell->GetPresContext();
9935 NS_ASSERTION(styleSet, "couldn't get style set");
9937 if (aContent) {
9938 nsChangeHint hint = NS_STYLE_HINT_NONE;
9939 // Any change to a content state that affects which frames we construct
9940 // must lead to a frame reconstruct here if we already have a frame.
9941 // Note that we never decide through non-CSS means to not create frames
9942 // based on content states, so if we already don't have a frame we don't
9943 // need to force a reframe -- if it's needed, the HasStateDependentStyle
9944 // call will handle things.
9945 nsIFrame* primaryFrame = mPresShell->GetPrimaryFrameFor(aContent);
9946 if (primaryFrame) {
9947 // If it's generated content, ignore LOADING/etc state changes on it.
9948 if (!primaryFrame->IsGeneratedContentFrame() &&
9949 (aStateMask & (NS_EVENT_STATE_BROKEN | NS_EVENT_STATE_USERDISABLED |
9950 NS_EVENT_STATE_SUPPRESSED | NS_EVENT_STATE_LOADING))) {
9951 hint = nsChangeHint_ReconstructFrame;
9952 } else {
9953 PRUint8 app = primaryFrame->GetStyleDisplay()->mAppearance;
9954 if (app) {
9955 nsITheme *theme = presContext->GetTheme();
9956 if (theme && theme->ThemeSupportsWidget(presContext,
9957 primaryFrame, app)) {
9958 PRBool repaint = PR_FALSE;
9959 theme->WidgetStateChanged(primaryFrame, app, nsnull, &repaint);
9960 if (repaint) {
9961 NS_UpdateHint(hint, nsChangeHint_RepaintFrame);
9968 nsReStyleHint rshint =
9969 styleSet->HasStateDependentStyle(presContext, aContent, aStateMask);
9971 PostRestyleEvent(aContent, rshint, hint);
9975 nsresult
9976 nsCSSFrameConstructor::AttributeChanged(nsIContent* aContent,
9977 PRInt32 aNameSpaceID,
9978 nsIAtom* aAttribute,
9979 PRInt32 aModType,
9980 PRUint32 aStateMask)
9982 nsresult result = NS_OK;
9984 // Hold onto the PresShell to prevent ourselves from being destroyed.
9985 // XXXbz how, exactly, would this attribute change cause us to be
9986 // destroyed from inside this function?
9987 nsCOMPtr<nsIPresShell> shell = mPresShell;
9989 // Get the frame associated with the content which is the highest in the frame tree
9990 nsIFrame* primaryFrame = shell->GetPrimaryFrameFor(aContent);
9992 #if 0
9993 NS_FRAME_LOG(NS_FRAME_TRACE_CALLS,
9994 ("HTMLStyleSheet::AttributeChanged: content=%p[%s] frame=%p",
9995 aContent, ContentTag(aContent, 0), frame));
9996 #endif
9998 // the style tag has its own interpretation based on aHint
9999 nsChangeHint hint = aContent->GetAttributeChangeHint(aAttribute, aModType);
10001 PRBool reframe = (hint & nsChangeHint_ReconstructFrame) != 0;
10003 #ifdef MOZ_XUL
10004 // The following listbox widget trap prevents offscreen listbox widget
10005 // content from being removed and re-inserted (which is what would
10006 // happen otherwise).
10007 if (!primaryFrame && !reframe) {
10008 PRInt32 namespaceID;
10009 nsIAtom* tag =
10010 mDocument->BindingManager()->ResolveTag(aContent, &namespaceID);
10012 if (namespaceID == kNameSpaceID_XUL &&
10013 (tag == nsGkAtoms::listitem ||
10014 tag == nsGkAtoms::listcell))
10015 return NS_OK;
10018 if (aAttribute == nsGkAtoms::tooltiptext ||
10019 aAttribute == nsGkAtoms::tooltip)
10021 nsIRootBox* rootBox = nsIRootBox::GetRootBox(mPresShell);
10022 if (rootBox) {
10023 if (aModType == nsIDOMMutationEvent::REMOVAL)
10024 rootBox->RemoveTooltipSupport(aContent);
10025 if (aModType == nsIDOMMutationEvent::ADDITION)
10026 rootBox->AddTooltipSupport(aContent);
10030 #endif // MOZ_XUL
10032 if (primaryFrame) {
10033 // See if we have appearance information for a theme.
10034 const nsStyleDisplay* disp = primaryFrame->GetStyleDisplay();
10035 if (disp->mAppearance) {
10036 nsPresContext* presContext = mPresShell->GetPresContext();
10037 nsITheme *theme = presContext->GetTheme();
10038 if (theme && theme->ThemeSupportsWidget(presContext, primaryFrame, disp->mAppearance)) {
10039 PRBool repaint = PR_FALSE;
10040 theme->WidgetStateChanged(primaryFrame, disp->mAppearance, aAttribute, &repaint);
10041 if (repaint)
10042 NS_UpdateHint(hint, nsChangeHint_RepaintFrame);
10046 // let the frame deal with it now, so we don't have to deal later
10047 result = primaryFrame->AttributeChanged(aNameSpaceID, aAttribute,
10048 aModType);
10049 // XXXwaterson should probably check for special IB siblings
10050 // here, and propagate the AttributeChanged notification to
10051 // them, as well. Currently, inline frames don't do anything on
10052 // this notification, so it's not that big a deal.
10055 // See if we can optimize away the style re-resolution -- must be called after
10056 // the frame's AttributeChanged() in case it does something that affects the style
10057 nsFrameManager *frameManager = shell->FrameManager();
10058 nsReStyleHint rshint = frameManager->HasAttributeDependentStyle(aContent,
10059 aAttribute,
10060 aModType,
10061 aStateMask);
10063 PostRestyleEvent(aContent, rshint, hint);
10065 return result;
10068 void
10069 nsCSSFrameConstructor::BeginUpdate() {
10070 NS_SuppressFocusEvent();
10071 ++mUpdateCount;
10074 void
10075 nsCSSFrameConstructor::EndUpdate()
10077 if (mUpdateCount == 1) {
10078 // This is the end of our last update. Before we decrement
10079 // mUpdateCount, recalc quotes and counters as needed.
10081 RecalcQuotesAndCounters();
10082 NS_ASSERTION(mUpdateCount == 1, "Odd update count");
10084 NS_UnsuppressFocusEvent();
10085 --mUpdateCount;
10088 void
10089 nsCSSFrameConstructor::RecalcQuotesAndCounters()
10091 if (mQuotesDirty) {
10092 mQuotesDirty = PR_FALSE;
10093 mQuoteList.RecalcAll();
10096 if (mCountersDirty) {
10097 mCountersDirty = PR_FALSE;
10098 mCounterManager.RecalcAll();
10101 NS_ASSERTION(!mQuotesDirty, "Quotes updates will be lost");
10102 NS_ASSERTION(!mCountersDirty, "Counter updates will be lost");
10105 void
10106 nsCSSFrameConstructor::WillDestroyFrameTree()
10108 #if defined(DEBUG_dbaron_off)
10109 mCounterManager.Dump();
10110 #endif
10112 mIsDestroyingFrameTree = PR_TRUE;
10114 // Prevent frame tree destruction from being O(N^2)
10115 mQuoteList.Clear();
10116 mCounterManager.Clear();
10118 // Cancel all pending re-resolves
10119 mRestyleEvent.Revoke();
10122 //STATIC
10124 // XXXbz I'd really like this method to go away. Once we have inline-block and
10125 // I can just use that for sized broken images, that can happen, maybe.
10126 void nsCSSFrameConstructor::GetAlternateTextFor(nsIContent* aContent,
10127 nsIAtom* aTag, // content object's tag
10128 nsXPIDLString& aAltText)
10130 // The "alt" attribute specifies alternate text that is rendered
10131 // when the image can not be displayed
10133 // If there's no "alt" attribute, and aContent is an input
10134 // element, then use the value of the "value" attribute
10135 if (!aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::alt, aAltText) &&
10136 nsGkAtoms::input == aTag) {
10137 // If there's no "value" attribute either, then use the localized string
10138 // for "Submit" as the alternate text.
10139 if (!aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::value, aAltText)) {
10140 nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
10141 "Submit", aAltText);
10146 nsresult
10147 nsCSSFrameConstructor::CreateContinuingOuterTableFrame(nsIPresShell* aPresShell,
10148 nsPresContext* aPresContext,
10149 nsIFrame* aFrame,
10150 nsIFrame* aParentFrame,
10151 nsIContent* aContent,
10152 nsStyleContext* aStyleContext,
10153 nsIFrame** aContinuingFrame)
10155 nsIFrame* newFrame = NS_NewTableOuterFrame(aPresShell, aStyleContext);
10157 if (newFrame) {
10158 newFrame->Init(aContent, aParentFrame, aFrame);
10159 // XXXbz should we be passing in a non-null aContentParentFrame?
10160 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10162 // Create a continuing inner table frame, and if there's a caption then
10163 // replicate the caption
10164 nsFrameItems newChildFrames;
10166 nsIFrame* childFrame = aFrame->GetFirstChild(nsnull);
10167 if (childFrame) {
10168 nsIFrame* continuingTableFrame;
10169 nsresult rv = CreateContinuingFrame(aPresContext, childFrame, newFrame,
10170 &continuingTableFrame);
10171 if (NS_FAILED(rv)) {
10172 newFrame->Destroy();
10173 *aContinuingFrame = nsnull;
10174 return rv;
10176 newChildFrames.AddChild(continuingTableFrame);
10178 NS_ASSERTION(!childFrame->GetNextSibling(),"there can be only one inner table frame");
10181 // Set the outer table's initial child list
10182 newFrame->SetInitialChildList(nsnull, newChildFrames.childList);
10184 *aContinuingFrame = newFrame;
10185 return NS_OK;
10187 else {
10188 *aContinuingFrame = nsnull;
10189 return NS_ERROR_OUT_OF_MEMORY;
10193 nsresult
10194 nsCSSFrameConstructor::CreateContinuingTableFrame(nsIPresShell* aPresShell,
10195 nsPresContext* aPresContext,
10196 nsIFrame* aFrame,
10197 nsIFrame* aParentFrame,
10198 nsIContent* aContent,
10199 nsStyleContext* aStyleContext,
10200 nsIFrame** aContinuingFrame)
10202 nsIFrame* newFrame = NS_NewTableFrame(aPresShell, aStyleContext);
10204 if (newFrame) {
10205 newFrame->Init(aContent, aParentFrame, aFrame);
10206 // XXXbz should we be passing in a non-null aContentParentFrame?
10207 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10209 // Replicate any header/footer frames
10210 nsFrameItems childFrames;
10211 nsIFrame* childFrame = aFrame->GetFirstChild(nsnull);
10212 for ( ; childFrame; childFrame = childFrame->GetNextSibling()) {
10213 // See if it's a header/footer, possibly wrapped in a scroll frame.
10214 nsTableRowGroupFrame* rowGroupFrame =
10215 nsTableFrame::GetRowGroupFrame(childFrame);
10216 if (rowGroupFrame) {
10217 // If the row group was continued, then don't replicate it.
10218 nsIFrame* rgNextInFlow = rowGroupFrame->GetNextInFlow();
10219 if (rgNextInFlow) {
10220 rowGroupFrame->SetRepeatable(PR_FALSE);
10222 else if (rowGroupFrame->IsRepeatable()) {
10223 // Replicate the header/footer frame.
10224 nsTableRowGroupFrame* headerFooterFrame;
10225 nsFrameItems childItems;
10226 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
10227 GetAbsoluteContainingBlock(newFrame),
10228 nsnull);
10230 headerFooterFrame = static_cast<nsTableRowGroupFrame*>
10231 (NS_NewTableRowGroupFrame(aPresShell, rowGroupFrame->GetStyleContext()));
10232 nsIContent* headerFooter = rowGroupFrame->GetContent();
10233 headerFooterFrame->Init(headerFooter, newFrame, nsnull);
10234 ProcessChildren(state, headerFooter, headerFooterFrame,
10235 PR_TRUE, childItems, PR_FALSE);
10236 NS_ASSERTION(!state.mFloatedItems.childList, "unexpected floated element");
10237 headerFooterFrame->SetInitialChildList(nsnull, childItems.childList);
10238 headerFooterFrame->SetRepeatable(PR_TRUE);
10240 // Table specific initialization
10241 headerFooterFrame->InitRepeatedFrame(aPresContext, rowGroupFrame);
10243 // XXX Deal with absolute and fixed frames...
10244 childFrames.AddChild(headerFooterFrame);
10249 // Set the table frame's initial child list
10250 newFrame->SetInitialChildList(nsnull, childFrames.childList);
10252 *aContinuingFrame = newFrame;
10253 return NS_OK;
10255 else {
10256 *aContinuingFrame = nsnull;
10257 return NS_ERROR_OUT_OF_MEMORY;
10261 nsresult
10262 nsCSSFrameConstructor::CreateContinuingFrame(nsPresContext* aPresContext,
10263 nsIFrame* aFrame,
10264 nsIFrame* aParentFrame,
10265 nsIFrame** aContinuingFrame,
10266 PRBool aIsFluid)
10268 nsIPresShell* shell = aPresContext->PresShell();
10269 nsStyleContext* styleContext = aFrame->GetStyleContext();
10270 nsIFrame* newFrame = nsnull;
10271 nsresult rv = NS_OK;
10272 nsIFrame* nextContinuation = aFrame->GetNextContinuation();
10273 nsIFrame* nextInFlow = aFrame->GetNextInFlow();
10275 // Use the frame type to determine what type of frame to create
10276 nsIAtom* frameType = aFrame->GetType();
10277 nsIContent* content = aFrame->GetContent();
10279 NS_ASSERTION(aFrame->GetSplittableType() != NS_FRAME_NOT_SPLITTABLE,
10280 "why CreateContinuingFrame for a non-splittable frame?");
10282 if (nsGkAtoms::textFrame == frameType) {
10283 newFrame = NS_NewContinuingTextFrame(shell, styleContext);
10285 if (newFrame) {
10286 newFrame->Init(content, aParentFrame, aFrame);
10287 // XXXbz should we be passing in a non-null aContentParentFrame?
10288 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10291 } else if (nsGkAtoms::inlineFrame == frameType) {
10292 newFrame = NS_NewInlineFrame(shell, styleContext);
10294 if (newFrame) {
10295 newFrame->Init(content, aParentFrame, aFrame);
10296 // XXXbz should we be passing in a non-null aContentParentFrame?
10297 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10300 } else if (nsGkAtoms::blockFrame == frameType) {
10301 newFrame = NS_NewBlockFrame(shell, styleContext);
10303 if (newFrame) {
10304 newFrame->Init(content, aParentFrame, aFrame);
10305 // XXXbz should we be passing in a non-null aContentParentFrame?
10306 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10309 } else if (nsGkAtoms::areaFrame == frameType) {
10310 newFrame = NS_NewAreaFrame(shell, styleContext, 0);
10312 if (newFrame) {
10313 newFrame->Init(content, aParentFrame, aFrame);
10314 // XXXbz should we be passing in a non-null aContentParentFrame?
10315 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10318 } else if (nsGkAtoms::columnSetFrame == frameType) {
10319 newFrame = NS_NewColumnSetFrame(shell, styleContext, 0);
10321 if (newFrame) {
10322 newFrame->Init(content, aParentFrame, aFrame);
10323 // XXXbz should we be passing in a non-null aContentParentFrame?
10324 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10327 } else if (nsGkAtoms::positionedInlineFrame == frameType) {
10328 newFrame = NS_NewPositionedInlineFrame(shell, styleContext);
10330 if (newFrame) {
10331 newFrame->Init(content, aParentFrame, aFrame);
10332 // XXXbz should we be passing in a non-null aContentParentFrame?
10333 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10336 } else if (nsGkAtoms::pageFrame == frameType) {
10337 nsIFrame* canvasFrame;
10338 rv = ConstructPageFrame(shell, aPresContext, aParentFrame, aFrame,
10339 newFrame, canvasFrame);
10340 } else if (nsGkAtoms::tableOuterFrame == frameType) {
10341 rv = CreateContinuingOuterTableFrame(shell, aPresContext, aFrame, aParentFrame,
10342 content, styleContext, &newFrame);
10344 } else if (nsGkAtoms::tableFrame == frameType) {
10345 rv = CreateContinuingTableFrame(shell, aPresContext, aFrame, aParentFrame,
10346 content, styleContext, &newFrame);
10348 } else if (nsGkAtoms::tableRowGroupFrame == frameType) {
10349 newFrame = NS_NewTableRowGroupFrame(shell, styleContext);
10351 if (newFrame) {
10352 newFrame->Init(content, aParentFrame, aFrame);
10353 // XXXbz should we be passing in a non-null aContentParentFrame?
10354 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10357 } else if (nsGkAtoms::tableRowFrame == frameType) {
10358 newFrame = NS_NewTableRowFrame(shell, styleContext);
10360 if (newFrame) {
10361 newFrame->Init(content, aParentFrame, aFrame);
10362 // XXXbz should we be passing in a non-null aContentParentFrame?
10363 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10365 // Create a continuing frame for each table cell frame
10366 nsFrameItems newChildList;
10367 nsIFrame* cellFrame = aFrame->GetFirstChild(nsnull);
10368 while (cellFrame) {
10369 // See if it's a table cell frame
10370 if (IS_TABLE_CELL(cellFrame->GetType())) {
10371 nsIFrame* continuingCellFrame;
10372 rv = CreateContinuingFrame(aPresContext, cellFrame, newFrame,
10373 &continuingCellFrame);
10374 if (NS_FAILED(rv)) {
10375 nsFrameList tmp(newChildList.childList);
10376 tmp.DestroyFrames();
10377 newFrame->Destroy();
10378 *aContinuingFrame = nsnull;
10379 return NS_ERROR_OUT_OF_MEMORY;
10381 newChildList.AddChild(continuingCellFrame);
10383 cellFrame = cellFrame->GetNextSibling();
10386 // Set the table cell's initial child list
10387 newFrame->SetInitialChildList(nsnull, newChildList.childList);
10390 } else if (IS_TABLE_CELL(frameType)) {
10391 // Warning: If you change this and add a wrapper frame around table cell
10392 // frames, make sure Bug 368554 doesn't regress!
10393 // See IsInAutoWidthTableCellForQuirk() in nsImageFrame.cpp.
10394 newFrame = NS_NewTableCellFrame(shell, styleContext, IsBorderCollapse(aParentFrame));
10396 if (newFrame) {
10397 newFrame->Init(content, aParentFrame, aFrame);
10398 // XXXbz should we be passing in a non-null aContentParentFrame?
10399 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10401 // Create a continuing area frame
10402 nsIFrame* continuingAreaFrame;
10403 nsIFrame* areaFrame = aFrame->GetFirstChild(nsnull);
10404 rv = CreateContinuingFrame(aPresContext, areaFrame, newFrame,
10405 &continuingAreaFrame);
10406 if (NS_FAILED(rv)) {
10407 newFrame->Destroy();
10408 *aContinuingFrame = nsnull;
10409 return rv;
10412 // Set the table cell's initial child list
10413 newFrame->SetInitialChildList(nsnull, continuingAreaFrame);
10416 } else if (nsGkAtoms::lineFrame == frameType) {
10417 newFrame = NS_NewFirstLineFrame(shell, styleContext);
10419 if (newFrame) {
10420 newFrame->Init(content, aParentFrame, aFrame);
10421 // XXXbz should we be passing in a non-null aContentParentFrame?
10422 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10425 } else if (nsGkAtoms::letterFrame == frameType) {
10426 newFrame = NS_NewFirstLetterFrame(shell, styleContext);
10428 if (newFrame) {
10429 newFrame->Init(content, aParentFrame, aFrame);
10430 // XXXbz should we be passing in a non-null aContentParentFrame?
10431 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10434 } else if (nsGkAtoms::imageFrame == frameType) {
10435 newFrame = NS_NewImageFrame(shell, styleContext);
10437 if (newFrame) {
10438 newFrame->Init(content, aParentFrame, aFrame);
10440 } else if (nsGkAtoms::imageControlFrame == frameType) {
10441 newFrame = NS_NewImageControlFrame(shell, styleContext);
10443 if (newFrame) {
10444 newFrame->Init(content, aParentFrame, aFrame);
10446 } else if (nsGkAtoms::placeholderFrame == frameType) {
10447 // create a continuing out of flow frame
10448 nsIFrame* oofFrame = nsPlaceholderFrame::GetRealFrameForPlaceholder(aFrame);
10449 nsIFrame* oofContFrame;
10450 rv = CreateContinuingFrame(aPresContext, oofFrame, aParentFrame, &oofContFrame);
10451 if (NS_FAILED(rv)) {
10452 *aContinuingFrame = nsnull;
10453 return rv;
10455 // create a continuing placeholder frame
10456 rv = CreatePlaceholderFrameFor(shell, content, oofContFrame, styleContext,
10457 aParentFrame, aFrame, &newFrame);
10458 if (NS_FAILED(rv)) {
10459 oofContFrame->Destroy();
10460 *aContinuingFrame = nsnull;
10461 return rv;
10463 } else if (nsGkAtoms::fieldSetFrame == frameType) {
10464 newFrame = NS_NewFieldSetFrame(shell, styleContext);
10466 if (newFrame) {
10467 newFrame->Init(content, aParentFrame, aFrame);
10469 // XXXbz should we be passing in a non-null aContentParentFrame?
10470 nsHTMLContainerFrame::CreateViewForFrame(newFrame, nsnull, PR_FALSE);
10472 // Create a continuing area frame
10473 // XXXbz we really shouldn't have to do this by hand!
10474 nsIFrame* continuingAreaFrame;
10475 nsIFrame* areaFrame = GetFieldSetAreaFrame(aFrame);
10476 rv = CreateContinuingFrame(aPresContext, areaFrame, newFrame,
10477 &continuingAreaFrame);
10478 if (NS_FAILED(rv)) {
10479 newFrame->Destroy();
10480 *aContinuingFrame = nsnull;
10481 return rv;
10483 // Set the fieldset's initial child list
10484 newFrame->SetInitialChildList(nsnull, continuingAreaFrame);
10486 } else {
10487 NS_NOTREACHED("unexpected frame type");
10488 *aContinuingFrame = nsnull;
10489 return NS_ERROR_UNEXPECTED;
10492 *aContinuingFrame = newFrame;
10494 if (!newFrame) {
10495 return NS_ERROR_OUT_OF_MEMORY;
10498 // Init() set newFrame to be a fluid continuation of aFrame.
10499 // If we want a non-fluid continuation, we need to call SetPrevContinuation()
10500 // to reset NS_FRAME_IS_FLUID_CONTINUATION.
10501 if (!aIsFluid) {
10502 newFrame->SetPrevContinuation(aFrame);
10505 // A continuation of generated content is also generated content
10506 if (aFrame->GetStateBits() & NS_FRAME_GENERATED_CONTENT) {
10507 newFrame->AddStateBits(NS_FRAME_GENERATED_CONTENT);
10510 // A continuation of an out-of-flow is also an out-of-flow
10511 if (aFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW) {
10512 newFrame->AddStateBits(NS_FRAME_OUT_OF_FLOW);
10515 if (nextInFlow) {
10516 nextInFlow->SetPrevInFlow(newFrame);
10517 newFrame->SetNextInFlow(nextInFlow);
10518 } else if (nextContinuation) {
10519 nextContinuation->SetPrevContinuation(newFrame);
10520 newFrame->SetNextContinuation(nextContinuation);
10522 return NS_OK;
10525 nsresult
10526 nsCSSFrameConstructor::ReplicateFixedFrames(nsPageContentFrame* aParentFrame)
10528 // Now deal with fixed-pos things.... They should appear on all pages,
10529 // so we want to move over the placeholders when processing the child
10530 // of the pageContentFrame.
10532 nsIFrame* prevPageContentFrame = aParentFrame->GetPrevInFlow();
10533 if (!prevPageContentFrame) {
10534 return NS_OK;
10536 nsIFrame* canvasFrame = aParentFrame->GetFirstChild(nsnull);
10537 nsIFrame* prevCanvasFrame = prevPageContentFrame->GetFirstChild(nsnull);
10538 if (!canvasFrame || !prevCanvasFrame) {
10539 // document's root element frame missing
10540 return NS_ERROR_UNEXPECTED;
10543 nsFrameItems fixedPlaceholders;
10544 nsIFrame* firstFixed = prevPageContentFrame->GetFirstChild(nsGkAtoms::fixedList);
10545 if (!firstFixed) {
10546 return NS_OK;
10549 //XXXbz Should mInitialContainingBlock be docRootFrame? It probably doesn't matter.
10550 // Don't allow abs-pos descendants of the fixed content to escape the content.
10551 // This should not normally be possible (because fixed-pos elements should
10552 // be absolute containers) but fixed-pos tables currently aren't abs-pos
10553 // containers.
10554 nsFrameConstructorState state(mPresShell, aParentFrame,
10555 nsnull,
10556 mInitialContainingBlock);
10558 // Iterate across fixed frames and replicate each whose placeholder is a
10559 // descendant of aFrame. (We don't want to explicitly copy placeholders that
10560 // are within fixed frames, because that would cause duplicates on the new
10561 // page - bug 389619)
10562 for (nsIFrame* fixed = firstFixed; fixed; fixed = fixed->GetNextSibling()) {
10563 nsIFrame* prevPlaceholder = nsnull;
10564 mPresShell->GetPlaceholderFrameFor(fixed, &prevPlaceholder);
10565 if (prevPlaceholder &&
10566 nsLayoutUtils::IsProperAncestorFrame(prevCanvasFrame, prevPlaceholder)) {
10567 nsresult rv = ConstructFrame(state, fixed->GetContent(),
10568 canvasFrame, fixedPlaceholders);
10569 NS_ENSURE_SUCCESS(rv, rv);
10573 // Add the placeholders to our primary child list.
10574 // XXXbz this is a little screwed up, since the fixed frames will have
10575 // broken auto-positioning. Oh, well.
10576 NS_ASSERTION(!canvasFrame->GetFirstChild(nsnull),
10577 "leaking frames; doc root continuation must be empty");
10578 canvasFrame->SetInitialChildList(nsnull, fixedPlaceholders.childList);
10579 return NS_OK;
10582 static PRBool
10583 IsBindingAncestor(nsIContent* aContent, nsIContent* aBindingRoot)
10585 while (PR_TRUE) {
10586 // Native-anonymous content doesn't contain insertion points, so
10587 // we don't need to search through it.
10588 if (aContent->IsRootOfNativeAnonymousSubtree())
10589 return PR_FALSE;
10590 nsIContent* bindingParent = aContent->GetBindingParent();
10591 if (!bindingParent)
10592 return PR_FALSE;
10593 if (bindingParent == aBindingRoot)
10594 return PR_TRUE;
10595 aContent = bindingParent;
10599 // Helper function that searches the immediate child frames
10600 // (and their children if the frames are "special")
10601 // for a frame that maps the specified content object
10602 nsIFrame*
10603 nsCSSFrameConstructor::FindFrameWithContent(nsFrameManager* aFrameManager,
10604 nsIFrame* aParentFrame,
10605 nsIContent* aParentContent,
10606 nsIContent* aContent,
10607 nsFindFrameHint* aHint)
10610 #ifdef NOISY_FINDFRAME
10611 FFWC_totalCount++;
10612 printf("looking for content=%p, given aParentFrame %p parentContent %p, hint is %s\n",
10613 aContent, aParentFrame, aParentContent, aHint ? "set" : "NULL");
10614 #endif
10616 NS_ENSURE_TRUE(aParentFrame != nsnull, nsnull);
10618 do {
10619 // Search for the frame in each child list that aParentFrame supports
10620 nsIAtom* listName = nsnull;
10621 PRInt32 listIndex = 0;
10622 PRBool searchAgain;
10624 do {
10625 #ifdef NOISY_FINDFRAME
10626 FFWC_doLoop++;
10627 #endif
10628 nsIFrame* kidFrame = nsnull;
10630 searchAgain = PR_FALSE;
10632 // if we were given an hint, try to use it here to find a good
10633 // previous frame to start our search (|kidFrame|).
10634 if (aHint) {
10635 #ifdef NOISY_FINDFRAME
10636 printf(" hint frame is %p\n", aHint->mPrimaryFrameForPrevSibling);
10637 #endif
10638 // start with the primary frame for aContent's previous sibling
10639 kidFrame = aHint->mPrimaryFrameForPrevSibling;
10640 // But if it's out of flow, start from its placeholder.
10641 if (kidFrame && (kidFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW)) {
10642 kidFrame = aFrameManager->GetPlaceholderFrameFor(kidFrame);
10645 if (kidFrame) {
10646 // then use the next sibling frame as our starting point
10647 if (kidFrame->GetNextSibling()) {
10648 kidFrame = kidFrame->GetNextSibling();
10650 else {
10651 // The hint frame had no next sibling. Try the next-in-flow or
10652 // special sibling of the parent of the hint frame (or its
10653 // associated placeholder).
10654 nsIFrame *parentFrame = kidFrame->GetParent();
10655 kidFrame = nsnull;
10656 if (parentFrame) {
10657 parentFrame = nsLayoutUtils::GetNextContinuationOrSpecialSibling(parentFrame);
10659 if (parentFrame) {
10660 // Found it, continue the search with its first child.
10661 kidFrame = parentFrame->GetFirstChild(listName);
10662 // Leave |aParentFrame| as-is, since the only time we'll
10663 // reuse it is if the hint fails.
10666 #ifdef NOISY_FINDFRAME
10667 printf(" hint gives us kidFrame=%p with parent frame %p content %p\n",
10668 kidFrame, aParentFrame, aParentContent);
10669 #endif
10672 if (!kidFrame) { // we didn't have enough info to prune, start searching from the beginning
10673 kidFrame = aParentFrame->GetFirstChild(listName);
10675 while (kidFrame) {
10676 // See if the child frame points to the content object we're
10677 // looking for
10678 nsIContent* kidContent = kidFrame->GetContent();
10679 if (kidContent == aContent) {
10681 // We found a match. Return the out-of-flow if it's a placeholder
10682 return nsPlaceholderFrame::GetRealFrameFor(kidFrame);
10685 // only do this if there is content
10686 if (kidContent) {
10687 // We search the immediate children only, but if the child frame has
10688 // the same content pointer as its parent then we need to search its
10689 // child frames, too.
10690 // We also need to search if the child content is anonymous and scoped
10691 // to the parent content.
10692 // XXXldb What makes us continue the search once we're inside
10693 // the anonymous subtree?
10694 if (aParentContent == kidContent ||
10695 (aParentContent && IsBindingAncestor(kidContent, aParentContent)))
10697 #ifdef NOISY_FINDFRAME
10698 FFWC_recursions++;
10699 printf(" recursing with new parent set to kidframe=%p, parentContent=%p\n",
10700 kidFrame, aParentContent);
10701 #endif
10702 nsIFrame* matchingFrame =
10703 FindFrameWithContent(aFrameManager,
10704 nsPlaceholderFrame::GetRealFrameFor(kidFrame),
10705 aParentContent, aContent, nsnull);
10707 if (matchingFrame) {
10708 return matchingFrame;
10713 // Get the next sibling frame
10714 kidFrame = kidFrame->GetNextSibling();
10715 #ifdef NOISY_FINDFRAME
10716 FFWC_doSibling++;
10717 if (kidFrame) {
10718 printf(" searching sibling frame %p\n", kidFrame);
10720 #endif
10723 if (aHint) {
10724 // If we get here, and we had a hint, then we didn't find a frame.
10725 // The hint may have been a frame whose location in the frame tree
10726 // doesn't match the location of its corresponding element in the
10727 // DOM tree, e.g. a floated or absolutely positioned frame, or e.g.
10728 // a <col> frame, in which case we'd be off in the weeds looking
10729 // through something other than the primary frame list.
10730 // Reboot the search from scratch, without the hint, but using the
10731 // null child list again.
10732 aHint = nsnull;
10733 searchAgain = PR_TRUE;
10734 } else {
10735 do {
10736 listName = aParentFrame->GetAdditionalChildListName(listIndex++);
10737 } while (IsOutOfFlowList(listName));
10739 } while(listName || searchAgain);
10741 // We didn't find a matching frame. If aFrame has a next-in-flow,
10742 // then continue looking there
10743 aParentFrame = nsLayoutUtils::GetNextContinuationOrSpecialSibling(aParentFrame);
10744 #ifdef NOISY_FINDFRAME
10745 if (aParentFrame) {
10746 FFWC_nextInFlows++;
10747 printf(" searching NIF frame %p\n", aParentFrame);
10749 #endif
10750 } while (aParentFrame);
10752 // No matching frame
10753 return nsnull;
10756 // Request to find the primary frame associated with a given content object.
10757 // This is typically called by the pres shell when there is no mapping in
10758 // the pres shell hash table
10759 nsresult
10760 nsCSSFrameConstructor::FindPrimaryFrameFor(nsFrameManager* aFrameManager,
10761 nsIContent* aContent,
10762 nsIFrame** aFrame,
10763 nsFindFrameHint* aHint)
10765 NS_ASSERTION(aFrameManager && aContent && aFrame, "bad arg");
10767 *aFrame = nsnull; // initialize OUT parameter
10769 // We want to be able to quickly map from a content object to its frame,
10770 // but we also want to keep the hash table small. Therefore, many frames
10771 // are not added to the hash table when they're first created:
10772 // - text frames
10773 // - inline frames (often things like FONT and B)
10774 // - BR frames
10775 // - internal table frames (row-group, row, cell, col-group, col)
10777 // That means we need to need to search for the frame
10778 nsIFrame* parentFrame; // this pointer is used to iterate across all frames that map to parentContent
10780 // Get the frame that corresponds to the parent content object.
10781 // Note that this may recurse indirectly, because the pres shell will
10782 // call us back if there is no mapping in the hash table
10783 nsCOMPtr<nsIContent> parentContent = aContent->GetParent(); // Get this once
10784 if (parentContent) {
10785 parentFrame = aFrameManager->GetPrimaryFrameFor(parentContent, -1);
10786 while (parentFrame) {
10787 // Search the child frames for a match
10788 *aFrame = FindFrameWithContent(aFrameManager, parentFrame,
10789 parentContent, aContent, aHint);
10790 #ifdef NOISY_FINDFRAME
10791 printf("FindFrameWithContent returned %p\n", *aFrame);
10792 #endif
10794 #ifdef DEBUG
10795 // if we're given a hint and we were told to verify, then compare the resulting frame with
10796 // the frame we get by calling FindFrameWithContent *without* the hint.
10797 // Assert if they do not match
10798 // Note that this makes finding frames *slower* than it was before the fix.
10799 if (gVerifyFastFindFrame && aHint)
10801 #ifdef NOISY_FINDFRAME
10802 printf("VERIFYING...\n");
10803 #endif
10804 nsIFrame *verifyTestFrame =
10805 FindFrameWithContent(aFrameManager, parentFrame,
10806 parentContent, aContent, nsnull);
10807 #ifdef NOISY_FINDFRAME
10808 printf("VERIFY returned %p\n", verifyTestFrame);
10809 #endif
10810 NS_ASSERTION(verifyTestFrame == *aFrame, "hint shortcut found wrong frame");
10812 #endif
10813 // If we found a match, then add a mapping to the hash table so
10814 // next time this will be quick
10815 if (*aFrame) {
10816 aFrameManager->SetPrimaryFrameFor(aContent, *aFrame);
10817 break;
10819 else if (IsFrameSpecial(parentFrame)) {
10820 // If it's a "special" frame (that is, part of an inline
10821 // that's been split because it contained a block), we need to
10822 // follow the out-of-flow "special sibling" link, and search
10823 // *that* subtree as well.
10824 parentFrame = GetSpecialSibling(parentFrame);
10826 else {
10827 break;
10832 if (aHint && !*aFrame)
10833 { // if we had a hint, and we didn't get a frame, see if we should try the slow way
10834 if (aContent->IsNodeOfType(nsINode::eTEXT))
10836 #ifdef NOISY_FINDFRAME
10837 FFWC_slowSearchForText++;
10838 #endif
10839 // since we're passing in a null hint, we're guaranteed to only recurse once
10840 return FindPrimaryFrameFor(aFrameManager, aContent, aFrame, nsnull);
10844 #ifdef NOISY_FINDFRAME
10845 printf("%10s %10s %10s %10s %10s \n",
10846 "total", "doLoop", "doSibling", "recur", "nextIF", "slowSearch");
10847 printf("%10d %10d %10d %10d %10d \n",
10848 FFWC_totalCount, FFWC_doLoop, FFWC_doSibling, FFWC_recursions,
10849 FFWC_nextInFlows, FFWC_slowSearchForText);
10850 #endif
10852 return NS_OK;
10855 nsresult
10856 nsCSSFrameConstructor::GetInsertionPoint(nsIFrame* aParentFrame,
10857 nsIContent* aChildContent,
10858 nsIFrame** aInsertionPoint,
10859 PRBool* aMultiple)
10861 // Make the insertion point be the parent frame by default, in case
10862 // we have to bail early.
10863 *aInsertionPoint = aParentFrame;
10865 nsIContent* container = aParentFrame->GetContent();
10866 if (!container)
10867 return NS_OK;
10869 nsBindingManager *bindingManager = mDocument->BindingManager();
10871 nsIContent* insertionElement;
10872 if (aChildContent) {
10873 // We've got an explicit insertion child. Check to see if it's
10874 // anonymous.
10875 if (aChildContent->GetBindingParent() == container) {
10876 // This child content is anonymous. Don't use the insertion
10877 // point, since that's only for the explicit kids.
10878 return NS_OK;
10881 PRUint32 index;
10882 insertionElement = bindingManager->GetInsertionPoint(container,
10883 aChildContent,
10884 &index);
10886 else {
10887 PRBool multiple;
10888 PRUint32 index;
10889 insertionElement = bindingManager->GetSingleInsertionPoint(container,
10890 &index,
10891 &multiple);
10892 if (multiple && aMultiple)
10893 *aMultiple = multiple; // Record the fact that filters are in use.
10896 if (insertionElement) {
10897 nsIFrame* insertionPoint = mPresShell->GetPrimaryFrameFor(insertionElement);
10898 if (insertionPoint) {
10899 // Use the content insertion frame of the insertion point.
10900 insertionPoint = insertionPoint->GetContentInsertionFrame();
10901 if (insertionPoint && insertionPoint != aParentFrame)
10902 GetInsertionPoint(insertionPoint, aChildContent, aInsertionPoint, aMultiple);
10904 else {
10905 // There was no frame created yet for the insertion point.
10906 *aInsertionPoint = nsnull;
10910 // fieldsets have multiple insertion points. Note that we might
10911 // have to look at insertionElement here...
10912 if (aMultiple && !*aMultiple) {
10913 nsIContent* content = insertionElement ? insertionElement : container;
10914 if (content->IsNodeOfType(nsINode::eHTML) &&
10915 content->Tag() == nsGkAtoms::fieldset) {
10916 *aMultiple = PR_TRUE;
10920 return NS_OK;
10923 // Capture state for the frame tree rooted at the frame associated with the
10924 // content object, aContent
10925 nsresult
10926 nsCSSFrameConstructor::CaptureStateForFramesOf(nsIContent* aContent,
10927 nsILayoutHistoryState* aHistoryState)
10929 nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
10930 if (frame) {
10931 CaptureStateFor(frame, aHistoryState);
10933 return NS_OK;
10936 // Capture state for the frame tree rooted at aFrame.
10937 nsresult
10938 nsCSSFrameConstructor::CaptureStateFor(nsIFrame* aFrame,
10939 nsILayoutHistoryState* aHistoryState)
10941 if (aFrame && aHistoryState) {
10942 mPresShell->FrameManager()->CaptureFrameState(aFrame, aHistoryState);
10944 return NS_OK;
10947 nsresult
10948 nsCSSFrameConstructor::MaybeRecreateFramesForContent(nsIContent* aContent)
10950 nsresult result = NS_OK;
10951 nsFrameManager *frameManager = mPresShell->FrameManager();
10953 nsStyleContext *oldContext = frameManager->GetUndisplayedContent(aContent);
10954 if (oldContext) {
10955 // The parent has a frame, so try resolving a new context.
10956 nsRefPtr<nsStyleContext> newContext = mPresShell->StyleSet()->
10957 ResolveStyleFor(aContent, oldContext->GetParent());
10959 frameManager->ChangeUndisplayedContent(aContent, newContext);
10960 if (newContext->GetStyleDisplay()->mDisplay != NS_STYLE_DISPLAY_NONE) {
10961 result = RecreateFramesForContent(aContent);
10964 return result;
10967 PRBool
10968 nsCSSFrameConstructor::MaybeRecreateContainerForIBSplitterFrame(nsIFrame* aFrame,
10969 nsresult* aResult)
10971 NS_PRECONDITION(aFrame, "Must have a frame");
10972 NS_PRECONDITION(aFrame->GetParent(), "Frame shouldn't be root");
10973 NS_PRECONDITION(aResult, "Null out param?");
10974 NS_PRECONDITION(aFrame == aFrame->GetFirstContinuation(),
10975 "aFrame not the result of GetPrimaryFrameFor()?");
10977 if (IsFrameSpecial(aFrame)) {
10978 // The removal functions can't handle removal of an {ib} split directly; we
10979 // need to rebuild the containing block.
10980 #ifdef DEBUG
10981 if (gNoisyContentUpdates) {
10982 printf("nsCSSFrameConstructor::MaybeRecreateContainerForIBSplitterFrame: "
10983 "frame=");
10984 nsFrame::ListTag(stdout, aFrame);
10985 printf(" is special\n");
10987 #endif
10989 *aResult = ReframeContainingBlock(aFrame);
10990 return PR_TRUE;
10993 // We might still need to reconstruct things if the parent of aFrame is
10994 // special, since in that case the removal of aFrame might affect the
10995 // splitting of its parent.
10996 nsIFrame* parent = aFrame->GetParent();
10997 if (!IsFrameSpecial(parent)) {
10998 return PR_FALSE;
11001 // If aFrame is an inline, then it cannot possibly have caused the splitting.
11002 // If the frame is being reconstructed and being changed to a block, the
11003 // ContentInserted call will handle the containing block reframe. So in this
11004 // case, try to be conservative about whether we need to reframe. The only
11005 // case when it's needed is if the inline is the only child of the tail end
11006 // of an {ib} split, because the splitting code doesn't produce this tail end
11007 // if it would have no kids. If that ever changes, this code should change.
11008 if (IsInlineOutside(aFrame) &&
11010 // Not a kid of the third part of the IB split
11011 GetSpecialSibling(parent) || !IsInlineOutside(parent) ||
11012 // Or not the only child
11013 aFrame->GetTailContinuation()->GetNextSibling() ||
11014 aFrame != parent->GetFirstContinuation()->GetFirstChild(nsnull)
11015 )) {
11016 return PR_FALSE;
11019 #ifdef DEBUG
11020 if (gNoisyContentUpdates) {
11021 printf("nsCSSFrameConstructor::MaybeRecreateContainerForIBSplitterFrame: "
11022 "frame=");
11023 nsFrame::ListTag(stdout, parent);
11024 printf(" is special\n");
11026 #endif
11028 *aResult = ReframeContainingBlock(parent);
11029 return PR_TRUE;
11032 nsresult
11033 nsCSSFrameConstructor::RecreateFramesForContent(nsIContent* aContent)
11035 // If there is no document, we don't want to recreate frames for it. (You
11036 // shouldn't generally be giving this method content without a document
11037 // anyway).
11038 // Rebuilding the frame tree can have bad effects, especially if it's the
11039 // frame tree for chrome (see bug 157322).
11040 NS_ENSURE_TRUE(aContent->GetDocument(), NS_ERROR_FAILURE);
11042 // Is the frame `special'? If so, we need to reframe the containing
11043 // block *here*, rather than trying to remove and re-insert the
11044 // content (which would otherwise result in *two* nested reframe
11045 // containing block from ContentRemoved() and ContentInserted(),
11046 // below!). We'd really like to optimize away one of those
11047 // containing block reframes, hence the code here.
11049 nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
11050 if (frame && frame->IsFrameOfType(nsIFrame::eMathML)) {
11051 // Reframe the topmost MathML element to prevent exponential blowup
11052 // (see bug 397518)
11053 while (PR_TRUE) {
11054 nsIContent* parentContent = aContent->GetParent();
11055 nsIFrame* parentContentFrame = mPresShell->GetPrimaryFrameFor(parentContent);
11056 if (!parentContentFrame || !parentContentFrame->IsFrameOfType(nsIFrame::eMathML))
11057 break;
11058 aContent = parentContent;
11059 frame = parentContentFrame;
11063 nsresult rv = NS_OK;
11065 if (frame && MaybeRecreateContainerForIBSplitterFrame(frame, &rv)) {
11066 return rv;
11069 nsCOMPtr<nsIContent> container = aContent->GetParent();
11070 if (container) {
11071 // XXXbz what if this is anonymous content?
11072 PRInt32 indexInContainer = container->IndexOf(aContent);
11073 // Before removing the frames associated with the content object,
11074 // ask them to save their state onto a temporary state object.
11075 CaptureStateForFramesOf(aContent, mTempFrameTreeState);
11077 // Remove the frames associated with the content object on which
11078 // the attribute change occurred.
11079 PRBool didReconstruct;
11080 rv = ContentRemoved(container, aContent, indexInContainer, &didReconstruct);
11082 if (NS_SUCCEEDED(rv) && !didReconstruct) {
11083 // Now, recreate the frames associated with this content object. If
11084 // ContentRemoved triggered reconstruction, then we don't need to do this
11085 // because the frames will already have been built.
11086 rv = ContentInserted(container, aContent,
11087 indexInContainer, mTempFrameTreeState);
11089 } else {
11090 // The content is the root node, so just rebuild the world.
11091 ReconstructDocElementHierarchy();
11094 #ifdef ACCESSIBILITY
11095 if (mPresShell->IsAccessibilityActive()) {
11096 PRUint32 event;
11097 if (frame) {
11098 nsIFrame *newFrame = mPresShell->GetPrimaryFrameFor(aContent);
11099 event = newFrame ? PRUint32(nsIAccessibleEvent::EVENT_ASYNCH_SIGNIFICANT_CHANGE) :
11100 PRUint32(nsIAccessibleEvent::EVENT_ASYNCH_HIDE);
11102 else {
11103 event = nsIAccessibleEvent::EVENT_ASYNCH_SHOW;
11106 // A significant enough change occured that this part
11107 // of the accessible tree is no longer valid.
11108 nsCOMPtr<nsIAccessibilityService> accService =
11109 do_GetService("@mozilla.org/accessibilityService;1");
11110 if (accService) {
11111 accService->InvalidateSubtreeFor(mPresShell, aContent, event);
11114 #endif
11116 return rv;
11119 //////////////////////////////////////////////////////////////////////
11121 // Block frame construction code
11123 already_AddRefed<nsStyleContext>
11124 nsCSSFrameConstructor::GetFirstLetterStyle(nsIContent* aContent,
11125 nsStyleContext* aStyleContext)
11127 if (aContent) {
11128 return mPresShell->StyleSet()->
11129 ResolvePseudoStyleFor(aContent,
11130 nsCSSPseudoElements::firstLetter, aStyleContext);
11132 return nsnull;
11135 already_AddRefed<nsStyleContext>
11136 nsCSSFrameConstructor::GetFirstLineStyle(nsIContent* aContent,
11137 nsStyleContext* aStyleContext)
11139 if (aContent) {
11140 return mPresShell->StyleSet()->
11141 ResolvePseudoStyleFor(aContent,
11142 nsCSSPseudoElements::firstLine, aStyleContext);
11144 return nsnull;
11147 // Predicate to see if a given content (block element) has
11148 // first-letter style applied to it.
11149 PRBool
11150 nsCSSFrameConstructor::ShouldHaveFirstLetterStyle(nsIContent* aContent,
11151 nsStyleContext* aStyleContext)
11153 return nsLayoutUtils::HasPseudoStyle(aContent, aStyleContext,
11154 nsCSSPseudoElements::firstLetter,
11155 mPresShell->GetPresContext());
11158 PRBool
11159 nsCSSFrameConstructor::HasFirstLetterStyle(nsIFrame* aBlockFrame)
11161 NS_PRECONDITION(aBlockFrame, "Need a frame");
11162 NS_ASSERTION(nsLayoutUtils::GetAsBlock(aBlockFrame),
11163 "Not a block frame?");
11165 return (aBlockFrame->GetStateBits() & NS_BLOCK_HAS_FIRST_LETTER_STYLE) != 0;
11168 PRBool
11169 nsCSSFrameConstructor::ShouldHaveFirstLineStyle(nsIContent* aContent,
11170 nsStyleContext* aStyleContext)
11172 PRBool hasFirstLine =
11173 nsLayoutUtils::HasPseudoStyle(aContent, aStyleContext,
11174 nsCSSPseudoElements::firstLine,
11175 mPresShell->GetPresContext());
11176 if (hasFirstLine) {
11177 // But disable for fieldsets
11178 PRInt32 namespaceID;
11179 nsIAtom* tag = mDocument->BindingManager()->ResolveTag(aContent,
11180 &namespaceID);
11181 // This check must match the one in ConstructHTMLFrame.
11182 hasFirstLine = tag != nsGkAtoms::fieldset ||
11183 (namespaceID != kNameSpaceID_XHTML &&
11184 !aContent->IsNodeOfType(nsINode::eHTML));
11187 return hasFirstLine;
11190 void
11191 nsCSSFrameConstructor::ShouldHaveSpecialBlockStyle(nsIContent* aContent,
11192 nsStyleContext* aStyleContext,
11193 PRBool* aHaveFirstLetterStyle,
11194 PRBool* aHaveFirstLineStyle)
11196 *aHaveFirstLetterStyle =
11197 ShouldHaveFirstLetterStyle(aContent, aStyleContext);
11198 *aHaveFirstLineStyle =
11199 ShouldHaveFirstLineStyle(aContent, aStyleContext);
11203 * Request to process the child content elements and create frames.
11205 * @param aContent the content object whose child elements to process
11206 * @param aFrame the frame associated with aContent. This will be the
11207 * parent frame (both content and geometric) for the flowed
11208 * child frames
11210 nsresult
11211 nsCSSFrameConstructor::ProcessChildren(nsFrameConstructorState& aState,
11212 nsIContent* aContent,
11213 nsIFrame* aFrame,
11214 PRBool aCanHaveGeneratedContent,
11215 nsFrameItems& aFrameItems,
11216 PRBool aParentIsBlock)
11218 NS_PRECONDITION(!aFrame->IsLeaf(), "Bogus ProcessChildren caller!");
11219 // XXXbz ideally, this would do all the pushing of various
11220 // containing blocks as needed, so callers don't have to do it...
11221 nsresult rv = NS_OK;
11222 // :before/:after content should have the same style context parent
11223 // as normal kids.
11224 nsStyleContext* styleContext =
11225 nsFrame::CorrectStyleParentFrame(aFrame, nsnull)->GetStyleContext();
11227 // save the incoming pseudo frame state
11228 nsPseudoFrames priorPseudoFrames;
11229 aState.mPseudoFrames.Reset(&priorPseudoFrames);
11231 if (aCanHaveGeneratedContent) {
11232 // Probe for generated content before
11233 CreateGeneratedContentFrame(aState, aFrame, aContent,
11234 styleContext, nsCSSPseudoElements::before,
11235 aFrameItems);
11238 ChildIterator iter, last;
11239 for (ChildIterator::Init(aContent, &iter, &last);
11240 iter != last;
11241 ++iter) {
11242 rv = ConstructFrame(aState, nsCOMPtr<nsIContent>(*iter),
11243 aFrame, aFrameItems);
11244 if (NS_FAILED(rv))
11245 return rv;
11248 if (aCanHaveGeneratedContent) {
11249 // Probe for generated content after
11250 CreateGeneratedContentFrame(aState, aFrame, aContent,
11251 styleContext, nsCSSPseudoElements::after,
11252 aFrameItems);
11255 // process the current pseudo frame state
11256 if (!aState.mPseudoFrames.IsEmpty()) {
11257 ProcessPseudoFrames(aState, aFrameItems);
11260 // restore the incoming pseudo frame state
11261 aState.mPseudoFrames = priorPseudoFrames;
11263 if (aParentIsBlock) {
11264 if (aState.mFirstLetterStyle) {
11265 rv = WrapFramesInFirstLetterFrame(aState, aContent, aFrame, aFrameItems);
11267 if (aState.mFirstLineStyle) {
11268 rv = WrapFramesInFirstLineFrame(aState, aContent, aFrame, aFrameItems);
11272 return rv;
11275 //----------------------------------------------------------------------
11277 // Support for :first-line style
11279 // Special routine to handle placing a list of frames into a block
11280 // frame that has first-line style. The routine ensures that the first
11281 // collection of inline frames end up in a first-line frame.
11282 nsresult
11283 nsCSSFrameConstructor::WrapFramesInFirstLineFrame(
11284 nsFrameConstructorState& aState,
11285 nsIContent* aBlockContent,
11286 nsIFrame* aBlockFrame,
11287 nsFrameItems& aFrameItems)
11289 nsresult rv = NS_OK;
11291 // Find the first and last inline frame in aFrameItems
11292 nsIFrame* kid = aFrameItems.childList;
11293 nsIFrame* firstInlineFrame = nsnull;
11294 nsIFrame* lastInlineFrame = nsnull;
11295 while (kid) {
11296 if (IsInlineOutside(kid)) {
11297 if (!firstInlineFrame) firstInlineFrame = kid;
11298 lastInlineFrame = kid;
11300 else {
11301 break;
11303 kid = kid->GetNextSibling();
11306 // If we don't find any inline frames, then there is nothing to do
11307 if (!firstInlineFrame) {
11308 return rv;
11311 // Create line frame
11312 nsStyleContext* parentStyle =
11313 nsFrame::CorrectStyleParentFrame(aBlockFrame,
11314 nsCSSPseudoElements::firstLine)->
11315 GetStyleContext();
11316 nsRefPtr<nsStyleContext> firstLineStyle = GetFirstLineStyle(aBlockContent,
11317 parentStyle);
11319 nsIFrame* lineFrame = NS_NewFirstLineFrame(mPresShell, firstLineStyle);
11321 if (lineFrame) {
11322 // Initialize the line frame
11323 rv = InitAndRestoreFrame(aState, aBlockContent, aBlockFrame, nsnull,
11324 lineFrame);
11326 // Mangle the list of frames we are giving to the block: first
11327 // chop the list in two after lastInlineFrame
11328 nsIFrame* secondBlockFrame = lastInlineFrame->GetNextSibling();
11329 lastInlineFrame->SetNextSibling(nsnull);
11331 // The lineFrame will be the block's first child; the rest of the
11332 // frame list (after lastInlineFrame) will be the second and
11333 // subsequent children; join the list together and reset
11334 // aFrameItems appropriately.
11335 if (secondBlockFrame) {
11336 lineFrame->SetNextSibling(secondBlockFrame);
11338 if (aFrameItems.childList == lastInlineFrame) {
11339 // Just in case the block had exactly one inline child
11340 aFrameItems.lastChild = lineFrame;
11342 aFrameItems.childList = lineFrame;
11344 // Give the inline frames to the lineFrame <b>after</b> reparenting them
11345 kid = firstInlineFrame;
11346 NS_ASSERTION(lineFrame->GetStyleContext() == firstLineStyle,
11347 "Bogus style context on line frame");
11348 while (kid) {
11349 ReparentFrame(aState.mFrameManager, lineFrame, kid);
11350 kid = kid->GetNextSibling();
11352 lineFrame->SetInitialChildList(nsnull, firstInlineFrame);
11354 else {
11355 rv = NS_ERROR_OUT_OF_MEMORY;
11358 return rv;
11361 // Special routine to handle appending a new frame to a block frame's
11362 // child list. Takes care of placing the new frame into the right
11363 // place when first-line style is present.
11364 nsresult
11365 nsCSSFrameConstructor::AppendFirstLineFrames(
11366 nsFrameConstructorState& aState,
11367 nsIContent* aBlockContent,
11368 nsIFrame* aBlockFrame,
11369 nsFrameItems& aFrameItems)
11371 // It's possible that aBlockFrame needs to have a first-line frame
11372 // created because it doesn't currently have any children.
11373 nsIFrame* blockKid = aBlockFrame->GetFirstChild(nsnull);
11374 if (!blockKid) {
11375 return WrapFramesInFirstLineFrame(aState, aBlockContent,
11376 aBlockFrame, aFrameItems);
11379 // Examine the last block child - if it's a first-line frame then
11380 // appended frames need special treatment.
11381 nsresult rv = NS_OK;
11382 nsFrameList blockFrames(blockKid);
11383 nsIFrame* lastBlockKid = blockFrames.LastChild();
11384 if (lastBlockKid->GetType() != nsGkAtoms::lineFrame) {
11385 // No first-line frame at the end of the list, therefore there is
11386 // an interveening block between any first-line frame the frames
11387 // we are appending. Therefore, we don't need any special
11388 // treatment of the appended frames.
11389 return rv;
11391 nsIFrame* lineFrame = lastBlockKid;
11393 // Find the first and last inline frame in aFrameItems
11394 nsIFrame* kid = aFrameItems.childList;
11395 nsIFrame* firstInlineFrame = nsnull;
11396 nsIFrame* lastInlineFrame = nsnull;
11397 while (kid) {
11398 if (IsInlineOutside(kid)) {
11399 if (!firstInlineFrame) firstInlineFrame = kid;
11400 lastInlineFrame = kid;
11402 else {
11403 break;
11405 kid = kid->GetNextSibling();
11408 // If we don't find any inline frames, then there is nothing to do
11409 if (!firstInlineFrame) {
11410 return rv;
11413 // The inline frames get appended to the lineFrame. Make sure they
11414 // are reparented properly.
11415 nsIFrame* remainingFrames = lastInlineFrame->GetNextSibling();
11416 lastInlineFrame->SetNextSibling(nsnull);
11417 kid = firstInlineFrame;
11418 while (kid) {
11419 ReparentFrame(aState.mFrameManager, lineFrame, kid);
11420 kid = kid->GetNextSibling();
11422 aState.mFrameManager->AppendFrames(lineFrame, nsnull, firstInlineFrame);
11424 // The remaining frames get appended to the block frame
11425 if (remainingFrames) {
11426 aFrameItems.childList = remainingFrames;
11428 else {
11429 aFrameItems.childList = nsnull;
11430 aFrameItems.lastChild = nsnull;
11433 return rv;
11436 // Special routine to handle inserting a new frame into a block
11437 // frame's child list. Takes care of placing the new frame into the
11438 // right place when first-line style is present.
11439 nsresult
11440 nsCSSFrameConstructor::InsertFirstLineFrames(
11441 nsFrameConstructorState& aState,
11442 nsIContent* aContent,
11443 nsIFrame* aBlockFrame,
11444 nsIFrame** aParentFrame,
11445 nsIFrame* aPrevSibling,
11446 nsFrameItems& aFrameItems)
11448 nsresult rv = NS_OK;
11449 // XXXbz If you make this method actually do something, check to make sure
11450 // that the caller is passing what you expect. In particular, which content
11451 // is aContent?
11452 #if 0
11453 nsIFrame* parentFrame = *aParentFrame;
11454 nsIFrame* newFrame = aFrameItems.childList;
11455 PRBool isInline = IsInlineOutside(newFrame);
11457 if (!aPrevSibling) {
11458 // Insertion will become the first frame. Two cases: we either
11459 // already have a first-line frame or we don't.
11460 nsIFrame* firstBlockKid = aBlockFrame->GetFirstChild(nsnull);
11461 if (firstBlockKid->GetType() == nsGkAtoms::lineFrame) {
11462 // We already have a first-line frame
11463 nsIFrame* lineFrame = firstBlockKid;
11465 if (isInline) {
11466 // Easy case: the new inline frame will go into the lineFrame.
11467 ReparentFrame(aState.mFrameManager, lineFrame, newFrame);
11468 aState.mFrameManager->InsertFrames(lineFrame, nsnull, nsnull,
11469 newFrame);
11471 // Since the frame is going into the lineFrame, don't let it
11472 // go into the block too.
11473 aFrameItems.childList = nsnull;
11474 aFrameItems.lastChild = nsnull;
11476 else {
11477 // Harder case: We are about to insert a block level element
11478 // before the first-line frame.
11479 // XXX need a method to steal away frames from the line-frame
11482 else {
11483 // We do not have a first-line frame
11484 if (isInline) {
11485 // We now need a first-line frame to contain the inline frame.
11486 nsIFrame* lineFrame = NS_NewFirstLineFrame(firstLineStyle);
11487 if (!lineFrame) {
11488 rv = NS_ERROR_OUT_OF_MEMORY;
11491 if (NS_SUCCEEDED(rv)) {
11492 // Lookup first-line style context
11493 nsStyleContext* parentStyle =
11494 nsFrame::CorrectStyleParentFrame(aBlockFrame,
11495 nsCSSPseudoElements::firstLine)->
11496 GetStyleContext();
11497 nsRefPtr<nsStyleContext> firstLineStyle =
11498 GetFirstLineStyle(aContent, parentStyle);
11500 // Initialize the line frame
11501 rv = InitAndRestoreFrame(aState, aContent, aBlockFrame,
11502 nsnull, lineFrame);
11504 // Make sure the caller inserts the lineFrame into the
11505 // blocks list of children.
11506 aFrameItems.childList = lineFrame;
11507 aFrameItems.lastChild = lineFrame;
11509 // Give the inline frames to the lineFrame <b>after</b>
11510 // reparenting them
11511 NS_ASSERTION(lineFrame->GetStyleContext() == firstLineStyle,
11512 "Bogus style context on line frame");
11513 ReparentFrame(aPresContext, lineFrame, newFrame);
11514 lineFrame->SetInitialChildList(nsnull, newFrame);
11517 else {
11518 // Easy case: the regular insertion logic can insert the new
11519 // frame because it's a block frame.
11523 else {
11524 // Insertion will not be the first frame.
11525 nsIFrame* prevSiblingParent = aPrevSibling->GetParent();
11526 if (prevSiblingParent == aBlockFrame) {
11527 // Easy case: The prev-siblings parent is the block
11528 // frame. Therefore the prev-sibling is not currently in a
11529 // line-frame. Therefore the new frame which is going after it,
11530 // regardless of type, is not going into a line-frame.
11532 else {
11533 // If the prevSiblingParent is not the block-frame then it must
11534 // be a line-frame (if it were a letter-frame, that logic would
11535 // already have adjusted the prev-sibling to be the
11536 // letter-frame).
11537 if (isInline) {
11538 // Easy case: the insertion can go where the caller thinks it
11539 // should go (which is into prevSiblingParent).
11541 else {
11542 // Block elements don't end up in line-frames, therefore
11543 // change the insertion point to aBlockFrame. However, there
11544 // might be more inline elements following aPrevSibling that
11545 // need to be pulled out of the line-frame and become children
11546 // of the block.
11547 nsIFrame* nextSibling = aPrevSibling->GetNextSibling();
11548 nsIFrame* nextLineFrame = prevSiblingParent->GetNextInFlow();
11549 if (nextSibling || nextLineFrame) {
11550 // Oy. We have work to do. Create a list of the new frames
11551 // that are going into the block by stripping them away from
11552 // the line-frame(s).
11553 nsFrameList list(nextSibling);
11554 if (nextSibling) {
11555 nsLineFrame* lineFrame = (nsLineFrame*) prevSiblingParent;
11556 lineFrame->StealFramesFrom(nextSibling);
11559 nsLineFrame* nextLineFrame = (nsLineFrame*) lineFrame;
11560 for (;;) {
11561 nextLineFrame = nextLineFrame->GetNextInFlow();
11562 if (!nextLineFrame) {
11563 break;
11565 nsIFrame* kids = nextLineFrame->GetFirstChild(nsnull);
11568 else {
11569 // We got lucky: aPrevSibling was the last inline frame in
11570 // the line-frame.
11571 ReparentFrame(aState.mFrameManager, aBlockFrame, newFrame);
11572 aState.mFrameManager->InsertFrames(aBlockFrame, nsnull,
11573 prevSiblingParent, newFrame);
11574 aFrameItems.childList = nsnull;
11575 aFrameItems.lastChild = nsnull;
11581 #endif
11582 return rv;
11585 //----------------------------------------------------------------------
11587 // First-letter support
11589 // Determine how many characters in the text fragment apply to the
11590 // first letter
11591 static PRInt32
11592 FirstLetterCount(const nsTextFragment* aFragment)
11594 PRInt32 count = 0;
11595 PRInt32 firstLetterLength = 0;
11596 PRBool done = PR_FALSE;
11598 PRInt32 i, n = aFragment->GetLength();
11599 for (i = 0; i < n; i++) {
11600 PRUnichar ch = aFragment->CharAt(i);
11601 if (XP_IS_SPACE(ch)) {
11602 if (firstLetterLength) {
11603 done = PR_TRUE;
11604 break;
11606 count++;
11607 continue;
11609 // XXX I18n
11610 if ((ch == '\'') || (ch == '\"')) {
11611 if (firstLetterLength) {
11612 done = PR_TRUE;
11613 break;
11615 // keep looping
11616 firstLetterLength = 1;
11618 else {
11619 count++;
11620 done = PR_TRUE;
11621 break;
11625 return count;
11628 static PRBool
11629 NeedFirstLetterContinuation(nsIContent* aContent)
11631 NS_PRECONDITION(aContent, "null ptr");
11633 PRBool result = PR_FALSE;
11634 if (aContent) {
11635 const nsTextFragment* frag = aContent->GetText();
11636 if (frag) {
11637 PRInt32 flc = FirstLetterCount(frag);
11638 PRInt32 tl = frag->GetLength();
11639 if (flc < tl) {
11640 result = PR_TRUE;
11644 return result;
11647 static PRBool IsFirstLetterContent(nsIContent* aContent)
11649 return aContent->TextLength() &&
11650 !aContent->TextIsOnlyWhitespace();
11654 * Create a letter frame, only make it a floating frame.
11656 void
11657 nsCSSFrameConstructor::CreateFloatingLetterFrame(
11658 nsFrameConstructorState& aState,
11659 nsIFrame* aBlockFrame,
11660 nsIContent* aTextContent,
11661 nsIFrame* aTextFrame,
11662 nsIContent* aBlockContent,
11663 nsIFrame* aParentFrame,
11664 nsStyleContext* aStyleContext,
11665 nsFrameItems& aResult)
11667 // Create the first-letter-frame
11668 nsresult rv;
11669 nsIFrame* letterFrame;
11670 nsStyleSet *styleSet = mPresShell->StyleSet();
11672 letterFrame = NS_NewFirstLetterFrame(mPresShell, aStyleContext);
11673 // We don't want to use a text content for a non-text frame (because we want
11674 // its primary frame to be a text frame). So use its parent for the
11675 // first-letter.
11676 nsIContent* letterContent = aTextContent->GetParent();
11677 NS_ASSERTION(!letterContent->IsRootOfNativeAnonymousSubtree(),
11678 "Reframes of this letter frame will mess with the root of a "
11679 "native anonymous content subtree!");
11680 InitAndRestoreFrame(aState, letterContent,
11681 aState.GetGeometricParent(aStyleContext->GetStyleDisplay(),
11682 aParentFrame),
11683 nsnull, letterFrame);
11685 // Init the text frame to refer to the letter frame. Make sure we
11686 // get a proper style context for it (the one passed in is for the
11687 // letter frame and will have the float property set on it; the text
11688 // frame shouldn't have that set).
11689 nsRefPtr<nsStyleContext> textSC;
11690 textSC = styleSet->ResolveStyleForNonElement(aStyleContext);
11691 aTextFrame->SetStyleContextWithoutNotification(textSC);
11692 InitAndRestoreFrame(aState, aTextContent, letterFrame, nsnull, aTextFrame);
11694 // And then give the text frame to the letter frame
11695 letterFrame->SetInitialChildList(nsnull, aTextFrame);
11697 // See if we will need to continue the text frame (does it contain
11698 // more than just the first-letter text or not?) If it does, then we
11699 // create (in advance) a continuation frame for it.
11700 nsIFrame* nextTextFrame = nsnull;
11701 if (NeedFirstLetterContinuation(aTextContent)) {
11702 // Create continuation
11703 rv = CreateContinuingFrame(aState.mPresContext, aTextFrame, aParentFrame,
11704 &nextTextFrame);
11705 if (NS_FAILED(rv)) {
11706 letterFrame->Destroy();
11707 return;
11709 // Repair the continuations style context
11710 nsStyleContext* parentStyleContext = aStyleContext->GetParent();
11711 if (parentStyleContext) {
11712 nsRefPtr<nsStyleContext> newSC;
11713 newSC = styleSet->ResolveStyleForNonElement(parentStyleContext);
11714 if (newSC) {
11715 nextTextFrame->SetStyleContext(newSC);
11720 NS_ASSERTION(aResult.childList == nsnull,
11721 "aResult should be an empty nsFrameItems!");
11722 nsIFrame* insertAfter = nsnull;
11723 nsIFrame* f;
11724 // Put the new float before any of the floats in the block we're
11725 // doing first-letter for, that is, before any floats whose parent is aBlockFrame
11726 for (f = aState.mFloatedItems.childList; f; f = f->GetNextSibling()) {
11727 if (f->GetParent() == aBlockFrame)
11728 break;
11729 insertAfter = f;
11732 rv = aState.AddChild(letterFrame, aResult, letterContent, aStyleContext,
11733 aParentFrame, PR_FALSE, PR_TRUE, PR_FALSE, PR_TRUE,
11734 insertAfter);
11736 if (nextTextFrame) {
11737 if (NS_FAILED(rv)) {
11738 nextTextFrame->Destroy();
11739 } else {
11740 aResult.AddChild(nextTextFrame);
11746 * Create a new letter frame for aTextFrame. The letter frame will be
11747 * a child of aParentFrame.
11749 nsresult
11750 nsCSSFrameConstructor::CreateLetterFrame(nsFrameConstructorState& aState,
11751 nsIFrame* aBlockFrame,
11752 nsIContent* aTextContent,
11753 nsIFrame* aParentFrame,
11754 nsFrameItems& aResult)
11756 NS_PRECONDITION(aTextContent->IsNodeOfType(nsINode::eTEXT),
11757 "aTextContent isn't text");
11758 NS_ASSERTION(nsLayoutUtils::GetAsBlock(aBlockFrame),
11759 "Not a block frame?");
11761 // Get style context for the first-letter-frame
11762 nsStyleContext* parentStyleContext =
11763 nsFrame::CorrectStyleParentFrame(aParentFrame,
11764 nsCSSPseudoElements::firstLetter)->
11765 GetStyleContext();
11766 // Use content from containing block so that we can actually
11767 // find a matching style rule.
11768 nsIContent* blockContent =
11769 aState.mFloatedItems.containingBlock->GetContent();
11771 NS_ASSERTION(blockContent == aBlockFrame->GetContent(),
11772 "Unexpected block content");
11774 // Create first-letter style rule
11775 nsRefPtr<nsStyleContext> sc = GetFirstLetterStyle(blockContent,
11776 parentStyleContext);
11777 if (sc) {
11778 nsRefPtr<nsStyleContext> textSC;
11779 textSC = mPresShell->StyleSet()->ResolveStyleForNonElement(sc);
11781 // Create a new text frame (the original one will be discarded)
11782 // pass a temporary stylecontext, the correct one will be set later
11783 nsIFrame* textFrame = NS_NewTextFrame(mPresShell, textSC);
11785 // Create the right type of first-letter frame
11786 const nsStyleDisplay* display = sc->GetStyleDisplay();
11787 if (display->IsFloating()) {
11788 // Make a floating first-letter frame
11789 CreateFloatingLetterFrame(aState, aBlockFrame, aTextContent, textFrame,
11790 blockContent, aParentFrame,
11791 sc, aResult);
11793 else {
11794 // Make an inflow first-letter frame
11795 nsIFrame* letterFrame = NS_NewFirstLetterFrame(mPresShell, sc);
11797 if (letterFrame) {
11798 // Initialize the first-letter-frame. We don't want to use a text
11799 // content for a non-text frame (because we want its primary frame to
11800 // be a text frame). So use its parent for the first-letter.
11801 nsIContent* letterContent = aTextContent->GetParent();
11802 NS_ASSERTION(!letterContent->IsRootOfNativeAnonymousSubtree(),
11803 "Reframes of this letter frame will mess with the root "
11804 "of a native anonymous content subtree!");
11805 letterFrame->Init(letterContent, aParentFrame, nsnull);
11807 InitAndRestoreFrame(aState, aTextContent, letterFrame, nsnull,
11808 textFrame);
11810 letterFrame->SetInitialChildList(nsnull, textFrame);
11811 aResult.childList = aResult.lastChild = letterFrame;
11812 aBlockFrame->AddStateBits(NS_BLOCK_HAS_FIRST_LETTER_CHILD);
11817 return NS_OK;
11820 nsresult
11821 nsCSSFrameConstructor::WrapFramesInFirstLetterFrame(
11822 nsFrameConstructorState& aState,
11823 nsIContent* aBlockContent,
11824 nsIFrame* aBlockFrame,
11825 nsFrameItems& aBlockFrames)
11827 nsresult rv = NS_OK;
11829 aBlockFrame->AddStateBits(NS_BLOCK_HAS_FIRST_LETTER_STYLE);
11831 nsIFrame* parentFrame = nsnull;
11832 nsIFrame* textFrame = nsnull;
11833 nsIFrame* prevFrame = nsnull;
11834 nsFrameItems letterFrames;
11835 PRBool stopLooking = PR_FALSE;
11836 rv = WrapFramesInFirstLetterFrame(aState, aBlockFrame, aBlockFrame,
11837 aBlockFrames.childList,
11838 &parentFrame, &textFrame, &prevFrame,
11839 letterFrames, &stopLooking);
11840 if (NS_FAILED(rv)) {
11841 return rv;
11843 if (parentFrame) {
11844 if (parentFrame == aBlockFrame) {
11845 // Text textFrame out of the blocks frame list and substitute the
11846 // letter frame(s) instead.
11847 nsIFrame* nextSibling = textFrame->GetNextSibling();
11848 textFrame->SetNextSibling(nsnull);
11849 if (prevFrame) {
11850 prevFrame->SetNextSibling(letterFrames.childList);
11852 else {
11853 aBlockFrames.childList = letterFrames.childList;
11855 letterFrames.lastChild->SetNextSibling(nextSibling);
11857 // Destroy the old textFrame
11858 textFrame->Destroy();
11860 // Repair lastChild; the only time this needs to happen is when
11861 // the block had one child (the text frame).
11862 if (!nextSibling) {
11863 aBlockFrames.lastChild = letterFrames.lastChild;
11866 else {
11867 // Take the old textFrame out of the inline parents child list
11868 ::DeletingFrameSubtree(aState.mFrameManager, textFrame);
11869 parentFrame->RemoveFrame(nsnull, textFrame);
11871 // Insert in the letter frame(s)
11872 parentFrame->InsertFrames(nsnull, prevFrame, letterFrames.childList);
11876 return rv;
11879 nsresult
11880 nsCSSFrameConstructor::WrapFramesInFirstLetterFrame(
11881 nsFrameConstructorState& aState,
11882 nsIFrame* aBlockFrame,
11883 nsIFrame* aParentFrame,
11884 nsIFrame* aParentFrameList,
11885 nsIFrame** aModifiedParent,
11886 nsIFrame** aTextFrame,
11887 nsIFrame** aPrevFrame,
11888 nsFrameItems& aLetterFrames,
11889 PRBool* aStopLooking)
11891 nsresult rv = NS_OK;
11893 nsIFrame* prevFrame = nsnull;
11894 nsIFrame* frame = aParentFrameList;
11896 while (frame) {
11897 nsIFrame* nextFrame = frame->GetNextSibling();
11899 nsIAtom* frameType = frame->GetType();
11900 if (nsGkAtoms::textFrame == frameType) {
11901 // Wrap up first-letter content in a letter frame
11902 nsIContent* textContent = frame->GetContent();
11903 if (IsFirstLetterContent(textContent)) {
11904 // Create letter frame to wrap up the text
11905 rv = CreateLetterFrame(aState, aBlockFrame, textContent,
11906 aParentFrame, aLetterFrames);
11907 if (NS_FAILED(rv)) {
11908 return rv;
11911 // Provide adjustment information for parent
11912 *aModifiedParent = aParentFrame;
11913 *aTextFrame = frame;
11914 *aPrevFrame = prevFrame;
11915 *aStopLooking = PR_TRUE;
11916 return NS_OK;
11919 else if (IsInlineFrame(frame) && frameType != nsGkAtoms::brFrame) {
11920 nsIFrame* kids = frame->GetFirstChild(nsnull);
11921 WrapFramesInFirstLetterFrame(aState, aBlockFrame, frame, kids,
11922 aModifiedParent, aTextFrame,
11923 aPrevFrame, aLetterFrames, aStopLooking);
11924 if (*aStopLooking) {
11925 return NS_OK;
11928 else {
11929 // This will stop us looking to create more letter frames. For
11930 // example, maybe the frame-type is "letterFrame" or
11931 // "placeholderFrame". This keeps us from creating extra letter
11932 // frames, and also prevents us from creating letter frames when
11933 // the first real content child of a block is not text (e.g. an
11934 // image, hr, etc.)
11935 *aStopLooking = PR_TRUE;
11936 break;
11939 prevFrame = frame;
11940 frame = nextFrame;
11943 return rv;
11946 nsresult
11947 nsCSSFrameConstructor::RemoveFloatingFirstLetterFrames(
11948 nsPresContext* aPresContext,
11949 nsIPresShell* aPresShell,
11950 nsFrameManager* aFrameManager,
11951 nsIFrame* aBlockFrame,
11952 PRBool* aStopLooking)
11954 // First look for the float frame that is a letter frame
11955 nsIFrame* floatFrame = aBlockFrame->GetFirstChild(nsGkAtoms::floatList);
11956 while (floatFrame) {
11957 // See if we found a floating letter frame
11958 if (nsGkAtoms::letterFrame == floatFrame->GetType()) {
11959 break;
11961 floatFrame = floatFrame->GetNextSibling();
11963 if (!floatFrame) {
11964 // No such frame
11965 return NS_OK;
11968 // Take the text frame away from the letter frame (so it isn't
11969 // destroyed when we destroy the letter frame).
11970 nsIFrame* textFrame = floatFrame->GetFirstChild(nsnull);
11971 if (!textFrame) {
11972 return NS_OK;
11975 // Discover the placeholder frame for the letter frame
11976 nsIFrame* parentFrame;
11977 nsPlaceholderFrame* placeholderFrame =
11978 aFrameManager->GetPlaceholderFrameFor(floatFrame);
11980 if (!placeholderFrame) {
11981 // Somethings really wrong
11982 return NS_OK;
11984 parentFrame = placeholderFrame->GetParent();
11985 if (!parentFrame) {
11986 // Somethings really wrong
11987 return NS_OK;
11990 // Create a new text frame with the right style context that maps
11991 // all of the content that was previously part of the letter frame
11992 // (and probably continued elsewhere).
11993 nsStyleContext* parentSC = parentFrame->GetStyleContext();
11994 if (!parentSC) {
11995 return NS_OK;
11997 nsIContent* textContent = textFrame->GetContent();
11998 if (!textContent) {
11999 return NS_OK;
12001 nsRefPtr<nsStyleContext> newSC;
12002 newSC = aPresShell->StyleSet()->ResolveStyleForNonElement(parentSC);
12003 if (!newSC) {
12004 return NS_OK;
12006 nsIFrame* newTextFrame = NS_NewTextFrame(aPresShell, newSC);
12007 if (NS_UNLIKELY(!newTextFrame)) {
12008 return NS_ERROR_OUT_OF_MEMORY;;
12010 newTextFrame->Init(textContent, parentFrame, nsnull);
12012 // Destroy the old text frame's continuations (the old text frame
12013 // will be destroyed when its letter frame is destroyed).
12014 nsIFrame* nextTextFrame = textFrame->GetNextInFlow();
12015 if (nextTextFrame) {
12016 nsIFrame* nextTextParent = nextTextFrame->GetParent();
12017 if (nextTextParent) {
12018 nsSplittableFrame::BreakFromPrevFlow(nextTextFrame);
12019 ::DeletingFrameSubtree(aFrameManager, nextTextFrame);
12020 aFrameManager->RemoveFrame(nextTextParent, nsnull, nextTextFrame);
12024 // First find out where (in the content) the placeholder frames
12025 // text is and its previous sibling frame, if any. Note that:
12026 // 1) The placeholder had better be in the principal child list of
12027 // parentFrame.
12028 // 2) It's probably near the beginning (since we're a first-letter frame),
12029 // so just doing a linear search for the prevSibling is ok.
12030 // 3) Trying to use FindPreviousSibling will fail if the first-letter is in
12031 // anonymous content (eg generated content).
12032 nsFrameList siblingList(parentFrame->GetFirstChild(nsnull));
12033 NS_ASSERTION(siblingList.ContainsFrame(placeholderFrame),
12034 "Placeholder not in parent's principal child list?");
12035 nsIFrame* prevSibling = siblingList.GetPrevSiblingFor(placeholderFrame);
12037 // Now that everything is set...
12038 #ifdef NOISY_FIRST_LETTER
12039 printf("RemoveFloatingFirstLetterFrames: textContent=%p oldTextFrame=%p newTextFrame=%p\n",
12040 textContent.get(), textFrame, newTextFrame);
12041 #endif
12043 UnregisterPlaceholderChain(aFrameManager, placeholderFrame);
12045 // Remove the float frame
12046 ::DeletingFrameSubtree(aFrameManager, floatFrame);
12047 aFrameManager->RemoveFrame(aBlockFrame, nsGkAtoms::floatList,
12048 floatFrame);
12050 // Remove placeholder frame
12051 ::DeletingFrameSubtree(aFrameManager, placeholderFrame);
12052 aFrameManager->RemoveFrame(parentFrame, nsnull, placeholderFrame);
12054 // Insert text frame in its place
12055 aFrameManager->InsertFrames(parentFrame, nsnull,
12056 prevSibling, newTextFrame);
12058 return NS_OK;
12061 nsresult
12062 nsCSSFrameConstructor::RemoveFirstLetterFrames(nsPresContext* aPresContext,
12063 nsIPresShell* aPresShell,
12064 nsFrameManager* aFrameManager,
12065 nsIFrame* aFrame,
12066 PRBool* aStopLooking)
12068 nsIFrame* prevSibling = nsnull;
12069 nsIFrame* kid = aFrame->GetFirstChild(nsnull);
12071 while (kid) {
12072 if (nsGkAtoms::letterFrame == kid->GetType()) {
12073 // Bingo. Found it. First steal away the text frame.
12074 nsIFrame* textFrame = kid->GetFirstChild(nsnull);
12075 if (!textFrame) {
12076 break;
12079 // Create a new textframe
12080 nsStyleContext* parentSC = aFrame->GetStyleContext();
12081 if (!parentSC) {
12082 break;
12084 nsIContent* textContent = textFrame->GetContent();
12085 if (!textContent) {
12086 break;
12088 nsRefPtr<nsStyleContext> newSC;
12089 newSC = aPresShell->StyleSet()->ResolveStyleForNonElement(parentSC);
12090 if (!newSC) {
12091 break;
12093 textFrame = NS_NewTextFrame(aPresShell, newSC);
12094 textFrame->Init(textContent, aFrame, nsnull);
12096 // Next rip out the kid and replace it with the text frame
12097 ::DeletingFrameSubtree(aFrameManager, kid);
12098 aFrameManager->RemoveFrame(aFrame, nsnull, kid);
12100 // Insert text frame in its place
12101 aFrameManager->InsertFrames(aFrame, nsnull, prevSibling, textFrame);
12103 *aStopLooking = PR_TRUE;
12104 aFrame->RemoveStateBits(NS_BLOCK_HAS_FIRST_LETTER_CHILD);
12105 break;
12107 else if (IsInlineFrame(kid)) {
12108 // Look inside child inline frame for the letter frame
12109 RemoveFirstLetterFrames(aPresContext, aPresShell, aFrameManager, kid,
12110 aStopLooking);
12111 if (*aStopLooking) {
12112 break;
12115 prevSibling = kid;
12116 kid = kid->GetNextSibling();
12119 return NS_OK;
12122 nsresult
12123 nsCSSFrameConstructor::RemoveLetterFrames(nsPresContext* aPresContext,
12124 nsIPresShell* aPresShell,
12125 nsFrameManager* aFrameManager,
12126 nsIFrame* aBlockFrame)
12128 aBlockFrame = aBlockFrame->GetFirstContinuation();
12130 PRBool stopLooking = PR_FALSE;
12131 nsresult rv;
12132 do {
12133 rv = RemoveFloatingFirstLetterFrames(aPresContext, aPresShell,
12134 aFrameManager,
12135 aBlockFrame, &stopLooking);
12136 if (NS_SUCCEEDED(rv) && !stopLooking) {
12137 rv = RemoveFirstLetterFrames(aPresContext, aPresShell, aFrameManager,
12138 aBlockFrame, &stopLooking);
12140 if (stopLooking) {
12141 break;
12143 aBlockFrame = aBlockFrame->GetNextContinuation();
12144 } while (aBlockFrame);
12145 return rv;
12148 // Fixup the letter frame situation for the given block
12149 nsresult
12150 nsCSSFrameConstructor::RecoverLetterFrames(nsFrameConstructorState& aState,
12151 nsIFrame* aBlockFrame)
12153 aBlockFrame = aBlockFrame->GetFirstContinuation();
12155 nsIFrame* parentFrame = nsnull;
12156 nsIFrame* textFrame = nsnull;
12157 nsIFrame* prevFrame = nsnull;
12158 nsFrameItems letterFrames;
12159 PRBool stopLooking = PR_FALSE;
12160 nsresult rv;
12161 do {
12162 // XXX shouldn't this bit be set already (bug 408493), assert instead?
12163 aBlockFrame->AddStateBits(NS_BLOCK_HAS_FIRST_LETTER_STYLE);
12164 rv = WrapFramesInFirstLetterFrame(aState, aBlockFrame, aBlockFrame,
12165 aBlockFrame->GetFirstChild(nsnull),
12166 &parentFrame, &textFrame, &prevFrame,
12167 letterFrames, &stopLooking);
12168 if (NS_FAILED(rv)) {
12169 return rv;
12171 if (stopLooking) {
12172 break;
12174 aBlockFrame = aBlockFrame->GetNextContinuation();
12175 } while (aBlockFrame);
12177 if (parentFrame) {
12178 // Take the old textFrame out of the parents child list
12179 ::DeletingFrameSubtree(aState.mFrameManager, textFrame);
12180 parentFrame->RemoveFrame(nsnull, textFrame);
12182 // Insert in the letter frame(s)
12183 parentFrame->InsertFrames(nsnull, prevFrame, letterFrames.childList);
12185 return rv;
12188 //----------------------------------------------------------------------
12190 // listbox Widget Routines
12192 nsresult
12193 nsCSSFrameConstructor::CreateListBoxContent(nsPresContext* aPresContext,
12194 nsIFrame* aParentFrame,
12195 nsIFrame* aPrevFrame,
12196 nsIContent* aChild,
12197 nsIFrame** aNewFrame,
12198 PRBool aIsAppend,
12199 PRBool aIsScrollbar,
12200 nsILayoutHistoryState* aFrameState)
12202 #ifdef MOZ_XUL
12203 nsresult rv = NS_OK;
12205 // Construct a new frame
12206 if (nsnull != aParentFrame) {
12207 nsFrameItems frameItems;
12208 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
12209 GetAbsoluteContainingBlock(aParentFrame),
12210 GetFloatContainingBlock(aParentFrame),
12211 mTempFrameTreeState);
12213 nsRefPtr<nsStyleContext> styleContext;
12214 styleContext = ResolveStyleContext(aParentFrame, aChild);
12216 // Pre-check for display "none" - only if we find that, do we create
12217 // any frame at all
12218 const nsStyleDisplay* display = styleContext->GetStyleDisplay();
12220 if (NS_STYLE_DISPLAY_NONE == display->mDisplay) {
12221 *aNewFrame = nsnull;
12222 return NS_OK;
12225 BeginUpdate();
12227 rv = ConstructFrameInternal(state, aChild,
12228 aParentFrame, aChild->Tag(),
12229 aChild->GetNameSpaceID(),
12230 styleContext, frameItems, PR_FALSE);
12231 if (!state.mPseudoFrames.IsEmpty()) {
12232 ProcessPseudoFrames(state, frameItems);
12235 nsIFrame* newFrame = frameItems.childList;
12236 *aNewFrame = newFrame;
12238 if (NS_SUCCEEDED(rv) && (nsnull != newFrame)) {
12239 // Notify the parent frame
12240 if (aIsAppend)
12241 rv = ((nsListBoxBodyFrame*)aParentFrame)->ListBoxAppendFrames(newFrame);
12242 else
12243 rv = ((nsListBoxBodyFrame*)aParentFrame)->ListBoxInsertFrames(aPrevFrame, newFrame);
12246 EndUpdate();
12249 return rv;
12250 #else
12251 return NS_ERROR_FAILURE;
12252 #endif
12255 //----------------------------------------
12257 nsresult
12258 nsCSSFrameConstructor::ConstructBlock(nsFrameConstructorState& aState,
12259 const nsStyleDisplay* aDisplay,
12260 nsIContent* aContent,
12261 nsIFrame* aParentFrame,
12262 nsIFrame* aContentParentFrame,
12263 nsStyleContext* aStyleContext,
12264 nsIFrame** aNewFrame,
12265 nsFrameItems& aFrameItems,
12266 PRBool aAbsPosContainer)
12268 // Create column wrapper if necessary
12269 nsIFrame* blockFrame = *aNewFrame;
12270 nsIFrame* parent = aParentFrame;
12271 nsIFrame* contentParent = aContentParentFrame;
12272 nsRefPtr<nsStyleContext> blockStyle = aStyleContext;
12273 const nsStyleColumn* columns = aStyleContext->GetStyleColumn();
12275 if (columns->mColumnCount != NS_STYLE_COLUMN_COUNT_AUTO
12276 || columns->mColumnWidth.GetUnit() != eStyleUnit_Auto) {
12277 nsIFrame* columnSetFrame = nsnull;
12278 columnSetFrame = NS_NewColumnSetFrame(mPresShell, aStyleContext, 0);
12279 if (!columnSetFrame) {
12280 return NS_ERROR_OUT_OF_MEMORY;
12283 InitAndRestoreFrame(aState, aContent, aParentFrame, nsnull, columnSetFrame);
12284 // See if we need to create a view, e.g. the frame is absolutely positioned
12285 nsHTMLContainerFrame::CreateViewForFrame(columnSetFrame, aContentParentFrame,
12286 PR_FALSE);
12287 blockStyle = mPresShell->StyleSet()->
12288 ResolvePseudoStyleFor(aContent, nsCSSAnonBoxes::columnContent,
12289 aStyleContext);
12290 contentParent = columnSetFrame;
12291 parent = columnSetFrame;
12292 *aNewFrame = columnSetFrame;
12294 columnSetFrame->SetInitialChildList(nsnull, blockFrame);
12297 blockFrame->SetStyleContextWithoutNotification(blockStyle);
12298 InitAndRestoreFrame(aState, aContent, parent, nsnull, blockFrame);
12300 nsresult rv = aState.AddChild(*aNewFrame, aFrameItems, aContent,
12301 aStyleContext,
12302 aContentParentFrame ? aContentParentFrame :
12303 aParentFrame);
12304 if (NS_FAILED(rv)) {
12305 return rv;
12308 // See if we need to create a view, e.g. the frame is absolutely positioned
12309 nsHTMLContainerFrame::CreateViewForFrame(blockFrame, contentParent, PR_FALSE);
12311 // We should make the outer frame be the absolute containing block,
12312 // if one is required. We have to do this because absolute
12313 // positioning must be computed with respect to the CSS dimensions
12314 // of the element, which are the dimensions of the outer block. But
12315 // we can't really do that because only blocks can have absolute
12316 // children. So use the block and try to compensate with hacks
12317 // in nsBlockFrame::CalculateContainingBlockSizeForAbsolutes.
12318 nsFrameConstructorSaveState absoluteSaveState;
12319 if (aAbsPosContainer) {
12320 // NS_ASSERTION(aRelPos, "should have made area frame for this");
12321 aState.PushAbsoluteContainingBlock(blockFrame, absoluteSaveState);
12324 // See if the block has first-letter style applied to it...
12325 PRBool haveFirstLetterStyle, haveFirstLineStyle;
12326 ShouldHaveSpecialBlockStyle(aContent, aStyleContext,
12327 &haveFirstLetterStyle, &haveFirstLineStyle);
12329 // Process the child content
12330 nsFrameItems childItems;
12331 nsFrameConstructorSaveState floatSaveState;
12332 aState.PushFloatContainingBlock(blockFrame, floatSaveState,
12333 haveFirstLetterStyle,
12334 haveFirstLineStyle);
12335 rv = ProcessChildren(aState, aContent, blockFrame, PR_TRUE, childItems,
12336 PR_TRUE);
12338 CreateAnonymousFrames(aContent->Tag(), aState, aContent, blockFrame,
12339 PR_FALSE, childItems);
12341 // Set the frame's initial child list
12342 blockFrame->SetInitialChildList(nsnull, childItems.childList);
12344 return rv;
12347 static PRBool
12348 AreAllKidsInline(nsIFrame* aFrameList)
12350 nsIFrame* kid = aFrameList;
12351 while (kid) {
12352 if (!IsInlineOutside(kid)) {
12353 return PR_FALSE;
12355 kid = kid->GetNextSibling();
12357 return PR_TRUE;
12360 nsresult
12361 nsCSSFrameConstructor::ConstructInline(nsFrameConstructorState& aState,
12362 const nsStyleDisplay* aDisplay,
12363 nsIContent* aContent,
12364 nsIFrame* aParentFrame,
12365 nsStyleContext* aStyleContext,
12366 PRBool aIsPositioned,
12367 nsIFrame* aNewFrame)
12369 // Initialize the frame
12370 InitAndRestoreFrame(aState, aContent, aParentFrame, nsnull, aNewFrame);
12372 nsFrameConstructorSaveState absoluteSaveState; // definition cannot be inside next block
12373 // because the object's destructor is significant
12374 // this is part of the fix for bug 42372
12376 // Any inline frame might need a view (because of opacity, or fixed background)
12377 // XXXbz should we be passing in a non-null aContentParentFrame?
12378 nsHTMLContainerFrame::CreateViewForFrame(aNewFrame, nsnull, PR_FALSE);
12380 if (aIsPositioned) {
12381 // Relatively positioned frames becomes a container for child
12382 // frames that are positioned
12383 aState.PushAbsoluteContainingBlock(aNewFrame, absoluteSaveState);
12386 // Process the child content
12387 nsFrameItems childItems;
12388 PRBool kidsAllInline;
12389 nsresult rv = ProcessInlineChildren(aState, aContent, aNewFrame, PR_TRUE,
12390 childItems, &kidsAllInline);
12391 if (kidsAllInline) {
12392 // Set the inline frame's initial child list
12393 CreateAnonymousFrames(aContent->Tag(), aState, aContent, aNewFrame,
12394 PR_FALSE, childItems);
12396 aNewFrame->SetInitialChildList(nsnull, childItems.childList);
12397 return rv;
12400 // This inline frame contains several types of children. Therefore
12401 // this frame has to be chopped into several pieces. We will produce
12402 // as a result of this 3 lists of children. The first list contains
12403 // all of the inline children that precede the first block child
12404 // (and may be empty). The second list contains all of the block
12405 // children and any inlines that are between them (and must not be
12406 // empty, otherwise - why are we here?). The final list contains all
12407 // of the inline children that follow the final block child.
12409 // Find the first block child which defines list1 and list2
12410 nsIFrame* list1 = childItems.childList;
12411 nsIFrame* prevToFirstBlock;
12412 nsIFrame* list2 = FindFirstBlock(list1, &prevToFirstBlock);
12413 if (prevToFirstBlock) {
12414 prevToFirstBlock->SetNextSibling(nsnull);
12416 else {
12417 list1 = nsnull;
12420 // Find the last block child which defines the end of list2 and the
12421 // start of list3
12422 nsIFrame* afterFirstBlock = list2->GetNextSibling();
12423 nsIFrame* list3 = nsnull;
12424 nsIFrame* lastBlock = FindLastBlock(afterFirstBlock);
12425 if (!lastBlock) {
12426 lastBlock = list2;
12428 list3 = lastBlock->GetNextSibling();
12429 lastBlock->SetNextSibling(nsnull);
12431 // list1's frames belong to this inline frame so go ahead and take them
12432 aNewFrame->SetInitialChildList(nsnull, list1);
12434 // list2's frames belong to an anonymous block that we create right
12435 // now. The anonymous block will be the parent of the block children
12436 // of the inline.
12437 nsIAtom* blockStyle;
12438 nsRefPtr<nsStyleContext> blockSC;
12439 nsIFrame* blockFrame;
12440 if (aIsPositioned) {
12441 blockStyle = nsCSSAnonBoxes::mozAnonymousPositionedBlock;
12443 blockSC = mPresShell->StyleSet()->
12444 ResolvePseudoStyleFor(aContent, blockStyle, aStyleContext);
12446 blockFrame = NS_NewRelativeItemWrapperFrame(mPresShell, blockSC, 0);
12448 else {
12449 blockStyle = nsCSSAnonBoxes::mozAnonymousBlock;
12451 blockSC = mPresShell->StyleSet()->
12452 ResolvePseudoStyleFor(aContent, blockStyle, aStyleContext);
12454 blockFrame = NS_NewBlockFrame(mPresShell, blockSC);
12457 InitAndRestoreFrame(aState, aContent, aParentFrame, nsnull, blockFrame, PR_FALSE);
12459 // Any inline frame could have a view (e.g., opacity)
12460 // XXXbz should we be passing in a non-null aContentParentFrame?
12461 nsHTMLContainerFrame::CreateViewForFrame(blockFrame, nsnull, PR_FALSE);
12463 if (blockFrame->HasView() || aNewFrame->HasView()) {
12464 // Move list2's frames into the new view
12465 nsHTMLContainerFrame::ReparentFrameViewList(aState.mPresContext, list2,
12466 list2->GetParent(), blockFrame);
12469 blockFrame->SetInitialChildList(nsnull, list2);
12471 nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
12472 GetAbsoluteContainingBlock(blockFrame),
12473 GetFloatContainingBlock(blockFrame));
12475 // If we have an inline between two blocks all inside an inline and the inner
12476 // inline contains a float, the float will end up in the float list of the
12477 // parent block of the inline, but its parent pointer will be the anonymous
12478 // block we create... AdjustFloatParentPtrs() deals with this by moving the
12479 // float from the outer state |aState| to the inner |state|.
12480 MoveChildrenTo(state.mFrameManager, blockFrame, list2, nsnull, &state,
12481 &aState);
12483 // list3's frames belong to another inline frame
12484 nsIFrame* inlineFrame = nsnull;
12486 // If we ever start constructing a second inline in the split even when
12487 // list3 is null, the logic in MaybeRecreateContainerForIBSplitterFrame
12488 // needs to be adjusted. Also, if you're changing this code also change
12489 // AppendFrames().
12490 if (list3) {
12491 inlineFrame = MoveFramesToEndOfIBSplit(aState, nsnull,
12492 aIsPositioned, aContent,
12493 aStyleContext, list3,
12494 blockFrame, nsnull);
12498 // Mark the frames as special (note: marking for inlineFrame is handled by
12499 // MoveFramesToEndOfIBSplit). That way if any of the append/insert/remove
12500 // methods try to fiddle with the children, the containing block will be
12501 // reframed instead.
12502 SetFrameIsSpecial(aNewFrame, blockFrame);
12503 SetFrameIsSpecial(blockFrame, inlineFrame);
12504 MarkIBSpecialPrevSibling(blockFrame, aNewFrame);
12505 if (inlineFrame) {
12506 MarkIBSpecialPrevSibling(inlineFrame, blockFrame);
12509 #ifdef DEBUG
12510 if (gNoisyInlineConstruction) {
12511 nsIFrameDebug* frameDebug;
12513 printf("nsCSSFrameConstructor::ConstructInline:\n");
12514 if (NS_SUCCEEDED(CallQueryInterface(aNewFrame, &frameDebug))) {
12515 printf(" ==> leading inline frame:\n");
12516 frameDebug->List(stdout, 2);
12518 if (NS_SUCCEEDED(CallQueryInterface(blockFrame, &frameDebug))) {
12519 printf(" ==> block frame:\n");
12520 frameDebug->List(stdout, 2);
12522 if (inlineFrame &&
12523 NS_SUCCEEDED(CallQueryInterface(inlineFrame, &frameDebug))) {
12524 printf(" ==> trailing inline frame:\n");
12525 frameDebug->List(stdout, 2);
12528 #endif
12530 return rv;
12533 nsIFrame*
12534 nsCSSFrameConstructor::MoveFramesToEndOfIBSplit(nsFrameConstructorState& aState,
12535 nsIFrame* aExistingEndFrame,
12536 PRBool aIsPositioned,
12537 nsIContent* aContent,
12538 nsStyleContext* aStyleContext,
12539 nsIFrame* aFramesToMove,
12540 nsIFrame* aBlockPart,
12541 nsFrameConstructorState* aTargetState)
12543 NS_PRECONDITION(aFramesToMove, "Must have frames to move");
12544 NS_PRECONDITION(aBlockPart, "Must have a block part");
12546 nsIFrame* inlineFrame = aExistingEndFrame;
12547 if (!inlineFrame) {
12548 if (aIsPositioned) {
12549 inlineFrame = NS_NewPositionedInlineFrame(mPresShell, aStyleContext);
12551 else {
12552 inlineFrame = NS_NewInlineFrame(mPresShell, aStyleContext);
12555 InitAndRestoreFrame(aState, aContent, aBlockPart->GetParent(), nsnull,
12556 inlineFrame, PR_FALSE);
12558 // Any frame might need a view
12559 // XXXbz should we be passing in a non-null aContentParentFrame?
12560 nsHTMLContainerFrame::CreateViewForFrame(inlineFrame, nsnull, PR_FALSE);
12563 if (inlineFrame->HasView() || aFramesToMove->GetParent()->HasView()) {
12564 // Move list3's frames into the new view
12565 nsHTMLContainerFrame::ReparentFrameViewList(aState.mPresContext,
12566 aFramesToMove,
12567 aFramesToMove->GetParent(),
12568 inlineFrame);
12571 // Reparent (cheaply) the frames in list3
12572 nsIFrame* existingFirstChild = inlineFrame->GetFirstChild(nsnull);
12573 if (!existingFirstChild &&
12574 (inlineFrame->GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
12575 inlineFrame->SetInitialChildList(nsnull, aFramesToMove);
12576 } else {
12577 inlineFrame->InsertFrames(nsnull, nsnull, aFramesToMove);
12579 nsFrameConstructorState* startState = aTargetState ? &aState : nsnull;
12580 MoveChildrenTo(aState.mFrameManager, inlineFrame, aFramesToMove,
12581 existingFirstChild, aTargetState, startState);
12582 SetFrameIsSpecial(inlineFrame, nsnull);
12583 return inlineFrame;
12586 nsresult
12587 nsCSSFrameConstructor::ProcessInlineChildren(nsFrameConstructorState& aState,
12588 nsIContent* aContent,
12589 nsIFrame* aFrame,
12590 PRBool aCanHaveGeneratedContent,
12591 nsFrameItems& aFrameItems,
12592 PRBool* aKidsAllInline)
12594 nsresult rv = NS_OK;
12595 nsStyleContext* styleContext = nsnull;
12597 // save the pseudo frame state
12598 nsPseudoFrames prevPseudoFrames;
12599 aState.mPseudoFrames.Reset(&prevPseudoFrames);
12601 if (aCanHaveGeneratedContent) {
12602 // Probe for generated content before
12603 styleContext = aFrame->GetStyleContext();
12604 CreateGeneratedContentFrame(aState, aFrame, aContent,
12605 styleContext, nsCSSPseudoElements::before,
12606 aFrameItems);
12609 // Iterate the child content objects and construct frames
12610 PRBool allKidsInline = PR_TRUE;
12611 ChildIterator iter, last;
12612 for (ChildIterator::Init(aContent, &iter, &last);
12613 iter != last;
12614 ++iter) {
12615 // Construct a child frame
12616 nsIFrame* oldLastChild = aFrameItems.lastChild;
12617 rv = ConstructFrame(aState, nsCOMPtr<nsIContent>(*iter),
12618 aFrame, aFrameItems);
12620 if (NS_FAILED(rv)) {
12621 return rv;
12624 // Examine newly added children (we may have added more than one
12625 // child if the child was another inline frame that ends up
12626 // being carved in 3 pieces) to maintain the allKidsInline flag.
12627 if (allKidsInline) {
12628 nsIFrame* kid;
12629 if (oldLastChild) {
12630 kid = oldLastChild->GetNextSibling();
12632 else {
12633 kid = aFrameItems.childList;
12635 while (kid) {
12636 if (!IsInlineOutside(kid)) {
12637 allKidsInline = PR_FALSE;
12638 break;
12640 kid = kid->GetNextSibling();
12645 if (aCanHaveGeneratedContent) {
12646 // Probe for generated content after
12647 CreateGeneratedContentFrame(aState, aFrame, aContent,
12648 styleContext, nsCSSPseudoElements::after,
12649 aFrameItems);
12652 // process the current pseudo frame state
12653 if (!aState.mPseudoFrames.IsEmpty()) {
12654 ProcessPseudoFrames(aState, aFrameItems);
12655 // recompute allKidsInline to take into account new child frames
12656 // XXX we DON'T do this yet because anonymous table children should
12657 // be accepted as inline children, until we turn on inline-table.
12658 // See bug 297537.
12659 // allKidsInline = AreAllKidsInline(aFrameItems.childList);
12661 // restore the pseudo frame state
12662 aState.mPseudoFrames = prevPseudoFrames;
12664 *aKidsAllInline = allKidsInline;
12666 return rv;
12669 static void
12670 DestroyNewlyCreatedFrames(nsFrameConstructorState& aState,
12671 nsIFrame* aParentFrame,
12672 const nsFrameItems& aFrameList)
12674 // Ok, reverse tracks: wipe out the frames we just created
12675 nsFrameManager *frameManager = aState.mFrameManager;
12677 // Destroy the frames. As we do make sure any content to frame mappings
12678 // or entries in the undisplayed content map are removed
12679 frameManager->ClearAllUndisplayedContentIn(aParentFrame->GetContent());
12681 CleanupFrameReferences(frameManager, aFrameList.childList);
12682 if (aState.mAbsoluteItems.childList) {
12683 CleanupFrameReferences(frameManager, aState.mAbsoluteItems.childList);
12685 if (aState.mFixedItems.childList) {
12686 CleanupFrameReferences(frameManager, aState.mFixedItems.childList);
12688 if (aState.mFloatedItems.childList) {
12689 CleanupFrameReferences(frameManager, aState.mFloatedItems.childList);
12691 #ifdef MOZ_XUL
12692 if (aState.mPopupItems.childList) {
12693 CleanupFrameReferences(frameManager, aState.mPopupItems.childList);
12695 #endif
12696 nsFrameList tmp(aFrameList.childList);
12697 tmp.DestroyFrames();
12699 tmp.SetFrames(aState.mAbsoluteItems.childList);
12700 tmp.DestroyFrames();
12701 aState.mAbsoluteItems.childList = nsnull;
12703 tmp.SetFrames(aState.mFixedItems.childList);
12704 tmp.DestroyFrames();
12705 aState.mFixedItems.childList = nsnull;
12707 tmp.SetFrames(aState.mFloatedItems.childList);
12708 tmp.DestroyFrames();
12709 aState.mFloatedItems.childList = nsnull;
12711 #ifdef MOZ_XUL
12712 tmp.SetFrames(aState.mPopupItems.childList);
12713 tmp.DestroyFrames();
12714 aState.mPopupItems.childList = nsnull;
12715 #endif
12718 PRBool
12719 nsCSSFrameConstructor::WipeContainingBlock(nsFrameConstructorState& aState,
12720 nsIFrame* aContainingBlock,
12721 nsIFrame* aFrame,
12722 const nsFrameItems& aFrameList,
12723 PRBool aIsAppend,
12724 nsIFrame* aPrevSibling)
12726 if (!aFrameList.childList) {
12727 return PR_FALSE;
12730 // Before we go and append the frames, we must check for two
12731 // special situations.
12733 // Situation #1 is a XUL frame that contains frames that are required
12734 // to be wrapped in blocks.
12735 if (aFrame->IsBoxFrame() &&
12736 !(aFrame->GetStateBits() & NS_STATE_BOX_WRAPS_KIDS_IN_BLOCK) &&
12737 AnyKidsNeedBlockParent(aFrameList.childList)) {
12738 DestroyNewlyCreatedFrames(aState, aFrame, aFrameList);
12739 RecreateFramesForContent(aFrame->GetContent());
12740 return PR_TRUE;
12743 // Situation #2 is an inline frame that will now contain block
12744 // frames. This is a no-no and the frame construction logic knows
12745 // how to fix this. See defition of IsInlineFrame() for what "an
12746 // inline" is. Whether we have "a block" is tested for by
12747 // AreAllKidsInline.
12749 // We also need to check for an append of content ending in an
12750 // inline to the block in an {ib} split or an insert of content
12751 // starting with an inline to the start of that block. If that
12752 // happens, we also need to reframe, since that content needs to go
12753 // into the following or preceding inline in the split.
12755 if (IsInlineFrame(aFrame)) {
12756 // Nothing to do if all kids are inline
12757 if (AreAllKidsInline(aFrameList.childList)) {
12758 return PR_FALSE;
12760 } else if (!IsFrameSpecial(aFrame)) {
12761 return PR_FALSE;
12762 } else {
12763 // aFrame is the block in an {ib} split. Check that we're not
12764 // messing up either end of it.
12765 if (aIsAppend) {
12766 // Will be handled in AppendFrames(), unless we have floats that we can't
12767 // move out because there might be no float containing block to move them
12768 // into.
12769 if (!aState.mFloatedItems.childList) {
12770 return PR_FALSE;
12773 // Walk up until we get a float containing block that's not part of an
12774 // {ib} split, since otherwise we might have to ship floats out of it
12775 // too.
12776 nsIFrame* floatContainer = aFrame;
12777 do {
12778 floatContainer = GetFloatContainingBlock(
12779 GetIBSplitSpecialPrevSiblingForAnonymousBlock(floatContainer));
12780 if (!floatContainer) {
12781 break;
12783 if (!IsFrameSpecial(floatContainer)) {
12784 return PR_FALSE;
12786 } while (1);
12789 if (aPrevSibling && !aPrevSibling->GetNextSibling()) {
12790 // This is an append that won't go through AppendFrames. We can bail out
12791 // if the last frame we're appending is not inline
12792 if (!aFrameList.lastChild->GetStyleDisplay()->IsInlineOutside()) {
12793 return PR_FALSE;
12795 } else {
12796 // We can bail out if we're not inserting at the beginning or if
12797 // the first frame we're inserting is not inline.
12798 if (aPrevSibling ||
12799 !aFrameList.childList->GetStyleDisplay()->IsInlineOutside()) {
12800 return PR_FALSE;
12805 DestroyNewlyCreatedFrames(aState, aFrame, aFrameList);
12807 // If we don't have a containing block, start with aFrame and look for one.
12808 if (!aContainingBlock) {
12809 aContainingBlock = aFrame;
12812 // To find the right block to reframe, just walk up the tree until we find a
12813 // frame that is:
12814 // 1) Not part of an IB split (not special)
12815 // 2) Not a pseudo-frame
12816 // 3) Not an inline frame
12817 // We're guaranteed to find one, since nsStyleContext::ApplyStyleFixups
12818 // enforces that the root is display:none, display:table, or display:block.
12819 // Note that walking up "too far" is OK in terms of correctness, even if it
12820 // might be a little inefficient. This is why we walk out of all
12821 // pseudo-frames -- telling which ones are or are not OK to walk out of is
12822 // too hard (and I suspect that we do in fact need to walk out of all of
12823 // them).
12824 while (IsFrameSpecial(aContainingBlock) || IsInlineOutside(aContainingBlock) ||
12825 aContainingBlock->GetStyleContext()->GetPseudoType()) {
12826 aContainingBlock = aContainingBlock->GetParent();
12827 NS_ASSERTION(aContainingBlock,
12828 "Must have non-inline, non-special, non-pseudo frame as root "
12829 "(or child of root, for a table root)!");
12832 // Tell parent of the containing block to reformulate the
12833 // entire block. This is painful and definitely not optimal
12834 // but it will *always* get the right answer.
12836 nsIContent *blockContent = aContainingBlock->GetContent();
12837 nsCOMPtr<nsIContent> parentContainer = blockContent->GetParent();
12838 #ifdef DEBUG
12839 if (gNoisyContentUpdates) {
12840 printf("nsCSSFrameConstructor::WipeContainingBlock: blockContent=%p parentContainer=%p\n",
12841 static_cast<void*>(blockContent),
12842 static_cast<void*>(parentContainer));
12844 #endif
12845 if (parentContainer) {
12846 ReinsertContent(parentContainer, blockContent);
12848 else if (blockContent->GetCurrentDoc() == mDocument) {
12849 ReconstructDocElementHierarchyInternal();
12851 return PR_TRUE;
12854 nsresult
12855 nsCSSFrameConstructor::ReframeContainingBlock(nsIFrame* aFrame)
12858 #ifdef DEBUG
12859 // ReframeContainingBlock is a NASTY routine, it causes terrible performance problems
12860 // so I want to see when it is happening! Unfortunately, it is happening way to often because
12861 // so much content on the web causes 'special' block-in-inline frame situations and we handle them
12862 // very poorly
12863 if (gNoisyContentUpdates) {
12864 printf("nsCSSFrameConstructor::ReframeContainingBlock frame=%p\n",
12865 static_cast<void*>(aFrame));
12867 #endif
12869 PRBool isReflowing;
12870 mPresShell->IsReflowLocked(&isReflowing);
12871 if(isReflowing) {
12872 // don't ReframeContainingBlock, this will result in a crash
12873 // if we remove a tree that's in reflow - see bug 121368 for testcase
12874 NS_ASSERTION(0, "Atemptted to nsCSSFrameConstructor::ReframeContainingBlock during a Reflow!!!");
12875 return NS_OK;
12878 // Get the first "normal" ancestor of the target frame.
12879 nsIFrame* containingBlock = GetIBContainingBlockFor(aFrame);
12880 if (containingBlock) {
12881 // From here we look for the containing block in case the target
12882 // frame is already a block (which can happen when an inline frame
12883 // wraps some of its content in an anonymous block; see
12884 // ConstructInline)
12886 // NOTE: We used to get the FloatContainingBlock here, but it was often wrong.
12887 // GetIBContainingBlock works much better and provides the correct container in all cases
12888 // so GetFloatContainingBlock(aFrame) has been removed
12890 // And get the containingBlock's content
12891 nsCOMPtr<nsIContent> blockContent = containingBlock->GetContent();
12892 if (blockContent) {
12893 // Now find the containingBlock's content's parent
12894 nsCOMPtr<nsIContent> parentContainer = blockContent->GetParent();
12895 if (parentContainer) {
12896 #ifdef DEBUG
12897 if (gNoisyContentUpdates) {
12898 printf(" ==> blockContent=%p, parentContainer=%p\n",
12899 static_cast<void*>(blockContent),
12900 static_cast<void*>(parentContainer));
12902 #endif
12903 return ReinsertContent(parentContainer, blockContent);
12908 // If we get here, we're screwed!
12909 return ReconstructDocElementHierarchyInternal();
12912 nsresult
12913 nsCSSFrameConstructor::RemoveFixedItems(const nsFrameConstructorState& aState,
12914 nsIFrame *aRootElementFrame)
12916 nsresult rv=NS_OK;
12918 if (mFixedContainingBlock) {
12919 nsIFrame *fixedChild = nsnull;
12920 do {
12921 fixedChild = mFixedContainingBlock->GetFirstChild(nsGkAtoms::fixedList);
12922 if (fixedChild && fixedChild == aRootElementFrame) {
12923 // Skip the root element frame, if it happens to be fixed-positioned
12924 // It will be explicitly removed later in
12925 // ReconstructDocElementHierarchyInternal
12926 fixedChild = fixedChild->GetNextSibling();
12928 if (fixedChild) {
12929 // Remove the placeholder so it doesn't end up sitting about pointing
12930 // to the removed fixed frame.
12931 nsPlaceholderFrame *placeholderFrame =
12932 aState.mFrameManager->GetPlaceholderFrameFor(fixedChild);
12933 NS_ASSERTION(placeholderFrame, "no placeholder for fixed-pos frame");
12934 NS_ASSERTION(placeholderFrame->GetType() ==
12935 nsGkAtoms::placeholderFrame,
12936 "Wrong type");
12937 UnregisterPlaceholderChain(aState.mFrameManager, placeholderFrame);
12938 nsIFrame* placeholderParent = placeholderFrame->GetParent();
12939 ::DeletingFrameSubtree(aState.mFrameManager, placeholderFrame);
12940 rv = aState.mFrameManager->RemoveFrame(placeholderParent, nsnull,
12941 placeholderFrame);
12942 if (NS_FAILED(rv)) {
12943 NS_WARNING("Error removing placeholder for fixed frame in RemoveFixedItems");
12944 break;
12947 ::DeletingFrameSubtree(aState.mFrameManager, fixedChild);
12948 rv = aState.mFrameManager->RemoveFrame(mFixedContainingBlock,
12949 nsGkAtoms::fixedList,
12950 fixedChild);
12951 if (NS_FAILED(rv)) {
12952 NS_WARNING("Error removing frame from fixed containing block in RemoveFixedItems");
12953 break;
12956 } while(fixedChild);
12957 } else {
12958 NS_WARNING( "RemoveFixedItems called with no FixedContainingBlock data member set");
12960 return rv;
12963 void
12964 nsCSSFrameConstructor::RestyleForAppend(nsIContent* aContainer,
12965 PRInt32 aNewIndexInContainer)
12967 NS_ASSERTION(aContainer, "must have container for append");
12968 #ifdef DEBUG
12970 for (PRInt32 index = aNewIndexInContainer;; ++index) {
12971 nsIContent *content = aContainer->GetChildAt(index);
12972 if (!content) {
12973 NS_ASSERTION(index != aNewIndexInContainer, "yikes, nothing appended");
12974 break;
12976 NS_ASSERTION(!content->IsRootOfAnonymousSubtree(),
12977 "anonymous nodes should not be in child lists");
12980 #endif
12981 PRUint32 selectorFlags =
12982 aContainer->GetFlags() & (NODE_ALL_SELECTOR_FLAGS &
12983 ~NODE_HAS_SLOW_SELECTOR_NOAPPEND);
12984 if (selectorFlags == 0)
12985 return;
12987 if (selectorFlags & NODE_HAS_SLOW_SELECTOR) {
12988 PostRestyleEvent(aContainer, eReStyle_Self, NS_STYLE_HINT_NONE);
12989 // Restyling the container is the most we can do here, so we're done.
12990 return;
12993 if (selectorFlags & NODE_HAS_EMPTY_SELECTOR) {
12994 // see whether we need to restyle the container
12995 PRBool wasEmpty = PR_TRUE; // :empty or :-moz-only-whitespace
12996 for (PRInt32 index = 0; index < aNewIndexInContainer; ++index) {
12997 // We don't know whether we're testing :empty or :-moz-only-whitespace,
12998 // so be conservative and assume :-moz-only-whitespace (i.e., make
12999 // IsSignificantChild less likely to be true, and thus make us more
13000 // likely to restyle).
13001 if (nsStyleUtil::IsSignificantChild(aContainer->GetChildAt(index),
13002 PR_TRUE, PR_FALSE)) {
13003 wasEmpty = PR_FALSE;
13004 break;
13007 if (wasEmpty) {
13008 PostRestyleEvent(aContainer, eReStyle_Self, NS_STYLE_HINT_NONE);
13009 // Restyling the container is the most we can do here, so we're done.
13010 return;
13013 if (selectorFlags & NODE_HAS_EDGE_CHILD_SELECTOR) {
13014 // restyle the last element child before this node
13015 for (PRInt32 index = aNewIndexInContainer - 1; index >= 0; --index) {
13016 nsIContent *content = aContainer->GetChildAt(index);
13017 if (content->IsNodeOfType(nsINode::eELEMENT)) {
13018 PostRestyleEvent(content, eReStyle_Self, NS_STYLE_HINT_NONE);
13019 break;
13025 // Restyling for a ContentInserted or CharacterDataChanged notification.
13026 // This could be used for ContentRemoved as well if we got the
13027 // notification before the removal happened (and sometimes
13028 // CharacterDataChanged is more like a removal than an addition).
13029 // The comments are written and variables are named in terms of it being
13030 // a ContentInserted notification.
13031 void
13032 nsCSSFrameConstructor::RestyleForInsertOrChange(nsIContent* aContainer,
13033 nsIContent* aChild)
13035 NS_ASSERTION(!aChild->IsRootOfAnonymousSubtree(),
13036 "anonymous nodes should not be in child lists");
13037 PRUint32 selectorFlags =
13038 aContainer ? (aContainer->GetFlags() & NODE_ALL_SELECTOR_FLAGS) : 0;
13039 if (selectorFlags == 0)
13040 return;
13042 if (selectorFlags & (NODE_HAS_SLOW_SELECTOR |
13043 NODE_HAS_SLOW_SELECTOR_NOAPPEND)) {
13044 PostRestyleEvent(aContainer, eReStyle_Self, NS_STYLE_HINT_NONE);
13045 // Restyling the container is the most we can do here, so we're done.
13046 return;
13049 if (selectorFlags & NODE_HAS_EMPTY_SELECTOR) {
13050 // see whether we need to restyle the container
13051 PRBool wasEmpty = PR_TRUE; // :empty or :-moz-only-whitespace
13052 for (PRInt32 index = 0; ; ++index) {
13053 nsIContent *child = aContainer->GetChildAt(index);
13054 if (!child) // last child
13055 break;
13056 if (child == aChild)
13057 continue;
13058 // We don't know whether we're testing :empty or :-moz-only-whitespace,
13059 // so be conservative and assume :-moz-only-whitespace (i.e., make
13060 // IsSignificantChild less likely to be true, and thus make us more
13061 // likely to restyle).
13062 if (nsStyleUtil::IsSignificantChild(child, PR_TRUE, PR_FALSE)) {
13063 wasEmpty = PR_FALSE;
13064 break;
13067 if (wasEmpty) {
13068 PostRestyleEvent(aContainer, eReStyle_Self, NS_STYLE_HINT_NONE);
13069 // Restyling the container is the most we can do here, so we're done.
13070 return;
13074 if (selectorFlags & NODE_HAS_EDGE_CHILD_SELECTOR) {
13075 // restyle the previously-first element child if it is after this node
13076 PRBool passedChild = PR_FALSE;
13077 for (PRInt32 index = 0; ; ++index) {
13078 nsIContent *content = aContainer->GetChildAt(index);
13079 if (!content)
13080 break; // went through all children
13081 if (content == aChild) {
13082 passedChild = PR_TRUE;
13083 continue;
13085 if (content->IsNodeOfType(nsINode::eELEMENT)) {
13086 if (passedChild) {
13087 PostRestyleEvent(content, eReStyle_Self, NS_STYLE_HINT_NONE);
13089 break;
13092 // restyle the previously-last element child if it is before this node
13093 passedChild = PR_FALSE;
13094 for (PRInt32 index = aContainer->GetChildCount() - 1;
13095 index >= 0; --index) {
13096 nsIContent *content = aContainer->GetChildAt(index);
13097 if (content == aChild) {
13098 passedChild = PR_TRUE;
13099 continue;
13101 if (content->IsNodeOfType(nsINode::eELEMENT)) {
13102 if (passedChild) {
13103 PostRestyleEvent(content, eReStyle_Self, NS_STYLE_HINT_NONE);
13105 break;
13111 void
13112 nsCSSFrameConstructor::RestyleForRemove(nsIContent* aContainer,
13113 nsIContent* aOldChild,
13114 PRInt32 aIndexInContainer)
13116 NS_ASSERTION(!aOldChild->IsRootOfAnonymousSubtree(),
13117 "anonymous nodes should not be in child lists");
13118 PRUint32 selectorFlags =
13119 aContainer ? (aContainer->GetFlags() & NODE_ALL_SELECTOR_FLAGS) : 0;
13120 if (selectorFlags == 0)
13121 return;
13123 if (selectorFlags & (NODE_HAS_SLOW_SELECTOR |
13124 NODE_HAS_SLOW_SELECTOR_NOAPPEND)) {
13125 PostRestyleEvent(aContainer, eReStyle_Self, NS_STYLE_HINT_NONE);
13126 // Restyling the container is the most we can do here, so we're done.
13127 return;
13130 if (selectorFlags & NODE_HAS_EMPTY_SELECTOR) {
13131 // see whether we need to restyle the container
13132 PRBool isEmpty = PR_TRUE; // :empty or :-moz-only-whitespace
13133 for (PRInt32 index = 0; ; ++index) {
13134 nsIContent *child = aContainer->GetChildAt(index);
13135 if (!child) // last child
13136 break;
13137 // We don't know whether we're testing :empty or :-moz-only-whitespace,
13138 // so be conservative and assume :-moz-only-whitespace (i.e., make
13139 // IsSignificantChild less likely to be true, and thus make us more
13140 // likely to restyle).
13141 if (nsStyleUtil::IsSignificantChild(child, PR_TRUE, PR_FALSE)) {
13142 isEmpty = PR_FALSE;
13143 break;
13146 if (isEmpty) {
13147 PostRestyleEvent(aContainer, eReStyle_Self, NS_STYLE_HINT_NONE);
13148 // Restyling the container is the most we can do here, so we're done.
13149 return;
13153 if (selectorFlags & NODE_HAS_EDGE_CHILD_SELECTOR) {
13154 // restyle the previously-first element child if it is after aOldChild
13155 for (PRInt32 index = 0; ; ++index) {
13156 nsIContent *content = aContainer->GetChildAt(index);
13157 if (!content)
13158 break; // went through all children
13159 if (content->IsNodeOfType(nsINode::eELEMENT)) {
13160 if (index >= aIndexInContainer) {
13161 PostRestyleEvent(content, eReStyle_Self, NS_STYLE_HINT_NONE);
13163 break;
13166 // restyle the previously-last element child if it is before aOldChild
13167 for (PRInt32 index = aContainer->GetChildCount() - 1;
13168 index >= 0; --index) {
13169 nsIContent *content = aContainer->GetChildAt(index);
13170 if (content->IsNodeOfType(nsINode::eELEMENT)) {
13171 if (index < aIndexInContainer) {
13172 PostRestyleEvent(content, eReStyle_Self, NS_STYLE_HINT_NONE);
13174 break;
13181 PR_STATIC_CALLBACK(PLDHashOperator)
13182 CollectRestyles(nsISupports* aContent,
13183 nsCSSFrameConstructor::RestyleData& aData,
13184 void* aRestyleArrayPtr)
13186 nsCSSFrameConstructor::RestyleEnumerateData** restyleArrayPtr =
13187 static_cast<nsCSSFrameConstructor::RestyleEnumerateData**>
13188 (aRestyleArrayPtr);
13189 nsCSSFrameConstructor::RestyleEnumerateData* currentRestyle =
13190 *restyleArrayPtr;
13191 currentRestyle->mContent = static_cast<nsIContent*>(aContent);
13192 currentRestyle->mRestyleHint = aData.mRestyleHint;
13193 currentRestyle->mChangeHint = aData.mChangeHint;
13195 // Increment to the next slot in the array
13196 *restyleArrayPtr = currentRestyle + 1;
13198 return PL_DHASH_NEXT;
13201 void
13202 nsCSSFrameConstructor::ProcessOneRestyle(nsIContent* aContent,
13203 nsReStyleHint aRestyleHint,
13204 nsChangeHint aChangeHint)
13206 NS_PRECONDITION(aContent, "Must have content node");
13208 if (!aContent->IsInDoc() ||
13209 aContent->GetCurrentDoc() != mDocument) {
13210 // Content node has been removed from our document; nothing else
13211 // to do here
13212 return;
13215 nsIFrame* primaryFrame = mPresShell->GetPrimaryFrameFor(aContent);
13216 if (aRestyleHint & eReStyle_Self) {
13217 RestyleElement(aContent, primaryFrame, aChangeHint);
13218 } else if (aChangeHint &&
13219 (primaryFrame ||
13220 (aChangeHint & nsChangeHint_ReconstructFrame))) {
13221 // Don't need to recompute style; just apply the hint
13222 nsStyleChangeList changeList;
13223 changeList.AppendChange(primaryFrame, aContent, aChangeHint);
13224 ProcessRestyledFrames(changeList);
13227 if (aRestyleHint & eReStyle_LaterSiblings) {
13228 RestyleLaterSiblings(aContent);
13232 #define RESTYLE_ARRAY_STACKSIZE 128
13234 void
13235 nsCSSFrameConstructor::RebuildAllStyleData(nsChangeHint aExtraHint)
13237 NS_ASSERTION(!(aExtraHint & nsChangeHint_ReconstructFrame),
13238 "Should not reconstruct the root of the frame tree. "
13239 "Use ReconstructDocElementHierarchy instead.");
13241 mRebuildAllStyleData = PR_FALSE;
13243 if (!mPresShell || !mPresShell->GetRootFrame())
13244 return;
13246 // Tell the style set to get the old rule tree out of the way
13247 // so we can recalculate while maintaining rule tree immutability
13248 nsresult rv = mPresShell->StyleSet()->BeginReconstruct();
13249 if (NS_FAILED(rv))
13250 return;
13252 // Recalculate all of the style contexts for the document
13253 // Note that we can ignore the return value of ComputeStyleChangeFor
13254 // because we never need to reframe the root frame
13255 // XXX This could be made faster by not rerunning rule matching
13256 // (but note that nsPresShell::SetPreferenceStyleRules currently depends
13257 // on us re-running rule matching here
13258 nsStyleChangeList changeList;
13259 // XXX Does it matter that we're passing aExtraHint to the real root
13260 // frame and not the root node's primary frame?
13261 mPresShell->FrameManager()->ComputeStyleChangeFor(mPresShell->GetRootFrame(),
13262 &changeList, aExtraHint);
13263 // Process the required changes
13264 ProcessRestyledFrames(changeList);
13265 // Tell the style set it's safe to destroy the old rule tree. We
13266 // must do this after the ProcessRestyledFrames call in case the
13267 // change list has frame reconstructs in it (since frames to be
13268 // reconstructed will still have their old style context pointers
13269 // until they are destroyed).
13270 mPresShell->StyleSet()->EndReconstruct();
13273 void
13274 nsCSSFrameConstructor::ProcessPendingRestyles()
13276 PRUint32 count = mPendingRestyles.Count();
13277 if (!count) {
13278 // Nothing to do
13279 return;
13282 NS_PRECONDITION(mDocument, "No document? Pshaw!\n");
13284 // Use the stack if we can, otherwise fall back on heap-allocation.
13285 nsAutoTArray<RestyleEnumerateData, RESTYLE_ARRAY_STACKSIZE> restyleArr;
13286 RestyleEnumerateData* restylesToProcess = restyleArr.AppendElements(count);
13288 if (!restylesToProcess) {
13289 return;
13292 RestyleEnumerateData* lastRestyle = restylesToProcess;
13293 mPendingRestyles.Enumerate(CollectRestyles, &lastRestyle);
13295 NS_ASSERTION(lastRestyle - restylesToProcess == PRInt32(count),
13296 "Enumeration screwed up somehow");
13298 // Clear the hashtable so we don't end up trying to process a restyle we're
13299 // already processing, sending us into an infinite loop.
13300 mPendingRestyles.Clear();
13302 // Make sure to not rebuild quote or counter lists while we're
13303 // processing restyles
13304 BeginUpdate();
13306 for (RestyleEnumerateData* currentRestyle = restylesToProcess;
13307 currentRestyle != lastRestyle;
13308 ++currentRestyle) {
13309 ProcessOneRestyle(currentRestyle->mContent,
13310 currentRestyle->mRestyleHint,
13311 currentRestyle->mChangeHint);
13314 EndUpdate();
13316 #ifdef DEBUG
13317 mPresShell->VerifyStyleTree();
13318 #endif
13320 if (mRebuildAllStyleData) {
13321 // We probably wasted a lot of work up above, but this seems safest
13322 // and it should be rarely used.
13323 RebuildAllStyleData(nsChangeHint(0));
13327 void
13328 nsCSSFrameConstructor::PostRestyleEvent(nsIContent* aContent,
13329 nsReStyleHint aRestyleHint,
13330 nsChangeHint aMinChangeHint)
13332 if (NS_UNLIKELY(mIsDestroyingFrameTree)) {
13333 NS_NOTREACHED("PostRestyleEvent after the shell is destroyed (bug 279505)");
13334 return;
13337 if (aRestyleHint == 0 && !aMinChangeHint) {
13338 // Nothing to do here
13339 return;
13342 NS_ASSERTION(aContent->IsNodeOfType(nsINode::eELEMENT),
13343 "Shouldn't be trying to restyle non-elements directly");
13345 RestyleData existingData;
13346 existingData.mRestyleHint = nsReStyleHint(0);
13347 existingData.mChangeHint = NS_STYLE_HINT_NONE;
13349 mPendingRestyles.Get(aContent, &existingData);
13350 existingData.mRestyleHint =
13351 nsReStyleHint(existingData.mRestyleHint | aRestyleHint);
13352 NS_UpdateHint(existingData.mChangeHint, aMinChangeHint);
13354 mPendingRestyles.Put(aContent, existingData);
13356 if (!mRestyleEvent.IsPending()) {
13357 nsRefPtr<RestyleEvent> ev = new RestyleEvent(this);
13358 if (NS_FAILED(NS_DispatchToCurrentThread(ev))) {
13359 NS_WARNING("failed to dispatch restyle event");
13360 // XXXbz and what?
13361 } else {
13362 mRestyleEvent = ev;
13367 void
13368 nsCSSFrameConstructor::PostRebuildAllStyleDataEvent()
13370 mRebuildAllStyleData = PR_TRUE;
13371 // Get a restyle event posted if necessary
13372 mPresShell->ReconstructStyleDataInternal();
13375 NS_IMETHODIMP nsCSSFrameConstructor::RestyleEvent::Run()
13377 if (!mConstructor)
13378 return NS_OK; // event was revoked
13380 // Make sure that any restyles that happen from now on will go into
13381 // a new event.
13382 mConstructor->mRestyleEvent.Forget();
13384 return mConstructor->mPresShell->FlushPendingNotifications(Flush_Style);
13387 NS_IMETHODIMP
13388 nsCSSFrameConstructor::LazyGenerateChildrenEvent::Run()
13390 mPresShell->GetDocument()->FlushPendingNotifications(Flush_Layout);
13392 // this is hard-coded to handle only menu popup frames
13393 nsIFrame* frame = mPresShell->GetPrimaryFrameFor(mContent);
13394 if (frame && frame->GetType() == nsGkAtoms::menuPopupFrame) {
13395 #ifdef MOZ_XUL
13396 // it is possible that the frame is different than the one that requested
13397 // the lazy generation, but as long as it's a popup frame that hasn't
13398 // generated its children yet, that's OK.
13399 nsMenuPopupFrame* menuPopupFrame = static_cast<nsMenuPopupFrame *>(frame);
13400 if (menuPopupFrame->HasGeneratedChildren()) {
13401 if (mCallback)
13402 mCallback(mContent, frame, mArg);
13404 return NS_OK;
13407 // indicate that the children have been generated
13408 menuPopupFrame->SetGeneratedChildren();
13409 #endif
13411 nsCSSFrameConstructor* fc = mPresShell->FrameConstructor();
13412 fc->BeginUpdate();
13414 nsFrameItems childItems;
13415 nsFrameConstructorState state(mPresShell, nsnull, nsnull, nsnull);
13416 nsresult rv = fc->ProcessChildren(state, mContent, frame, PR_FALSE,
13417 childItems, PR_FALSE);
13418 if (NS_FAILED(rv))
13419 return rv;
13421 fc->CreateAnonymousFrames(mContent->Tag(), state, mContent, frame,
13422 PR_FALSE, childItems);
13423 frame->SetInitialChildList(nsnull, childItems.childList);
13425 fc->EndUpdate();
13427 if (mCallback)
13428 mCallback(mContent, frame, mArg);
13430 // call XBL constructors after the frames are created
13431 mPresShell->GetDocument()->BindingManager()->ProcessAttachedQueue();
13434 return NS_OK;