1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* rendering object for HTML <br> elements */
10 #include "nsPresContext.h"
11 #include "nsLineLayout.h"
12 #include "nsStyleConsts.h"
13 #include "nsGkAtoms.h"
14 #include "nsRenderingContext.h"
15 #include "nsLayoutUtils.h"
18 #include "nsIContent.h"
19 //END INCLUDES FOR SELECTION
21 using namespace mozilla
;
23 class BRFrame
: public nsFrame
{
25 NS_DECL_FRAMEARENA_HELPERS
27 friend nsIFrame
* NS_NewBRFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
);
29 virtual ContentOffsets
CalcContentOffsetsFromFramePoint(nsPoint aPoint
) MOZ_OVERRIDE
;
31 virtual FrameSearchResult
PeekOffsetNoAmount(bool aForward
, int32_t* aOffset
) MOZ_OVERRIDE
;
32 virtual FrameSearchResult
PeekOffsetCharacter(bool aForward
, int32_t* aOffset
,
33 bool aRespectClusters
= true) MOZ_OVERRIDE
;
34 virtual FrameSearchResult
PeekOffsetWord(bool aForward
, bool aWordSelectEatSpace
,
35 bool aIsKeyboardSelect
, int32_t* aOffset
,
36 PeekWordState
* aState
) MOZ_OVERRIDE
;
38 virtual void Reflow(nsPresContext
* aPresContext
,
39 nsHTMLReflowMetrics
& aDesiredSize
,
40 const nsHTMLReflowState
& aReflowState
,
41 nsReflowStatus
& aStatus
) MOZ_OVERRIDE
;
42 virtual void AddInlineMinISize(nsRenderingContext
*aRenderingContext
,
43 InlineMinISizeData
*aData
) MOZ_OVERRIDE
;
44 virtual void AddInlinePrefISize(nsRenderingContext
*aRenderingContext
,
45 InlinePrefISizeData
*aData
) MOZ_OVERRIDE
;
46 virtual nscoord
GetMinISize(nsRenderingContext
*aRenderingContext
) MOZ_OVERRIDE
;
47 virtual nscoord
GetPrefISize(nsRenderingContext
*aRenderingContext
) MOZ_OVERRIDE
;
48 virtual nsIAtom
* GetType() const MOZ_OVERRIDE
;
49 virtual nscoord
GetLogicalBaseline(mozilla::WritingMode aWritingMode
) const MOZ_OVERRIDE
;
51 virtual bool IsFrameOfType(uint32_t aFlags
) const MOZ_OVERRIDE
53 return nsFrame::IsFrameOfType(aFlags
& ~(nsIFrame::eReplaced
|
54 nsIFrame::eLineParticipant
));
58 virtual mozilla::a11y::AccType
AccessibleType() MOZ_OVERRIDE
;
62 explicit BRFrame(nsStyleContext
* aContext
) : nsFrame(aContext
) {}
69 NS_NewBRFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
71 return new (aPresShell
) BRFrame(aContext
);
74 NS_IMPL_FRAMEARENA_HELPERS(BRFrame
)
81 BRFrame::Reflow(nsPresContext
* aPresContext
,
82 nsHTMLReflowMetrics
& aMetrics
,
83 const nsHTMLReflowState
& aReflowState
,
84 nsReflowStatus
& aStatus
)
86 DO_GLOBAL_REFLOW_COUNT("BRFrame");
87 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aMetrics
, aStatus
);
88 WritingMode wm
= aReflowState
.GetWritingMode();
89 LogicalSize
finalSize(wm
);
90 finalSize
.BSize(wm
) = 0; // BR frames with block size 0 are ignored in quirks
91 // mode by nsLineLayout::VerticalAlignFrames .
92 // However, it's not always 0. See below.
93 finalSize
.ISize(wm
) = 0;
94 aMetrics
.SetBlockStartAscent(0);
96 // Only when the BR is operating in a line-layout situation will it
98 nsLineLayout
* ll
= aReflowState
.mLineLayout
;
100 // Note that the compatibility mode check excludes AlmostStandards
101 // mode, since this is the inline box model. See bug 161691.
102 if ( ll
->LineIsEmpty() ||
103 aPresContext
->CompatibilityMode() == eCompatibility_FullStandards
) {
104 // The line is logically empty; any whitespace is trimmed away.
106 // If this frame is going to terminate the line we know
107 // that nothing else will go on the line. Therefore, in this
108 // case, we provide some height for the BR frame so that it
109 // creates some vertical whitespace. It's necessary to use the
110 // line-height rather than the font size because the
111 // quirks-mode fix that doesn't apply the block's min
112 // line-height makes this necessary to make BR cause a line
113 // of the full line-height
115 // We also do this in strict mode because BR should act like a
116 // normal inline frame. That line-height is used is important
117 // here for cases where the line-height is less than 1.
118 nsRefPtr
<nsFontMetrics
> fm
;
119 nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm
),
120 nsLayoutUtils::FontSizeInflationFor(this));
121 aReflowState
.rendContext
->SetFont(fm
); // FIXME: maybe not needed?
123 nscoord logicalHeight
= aReflowState
.CalcLineHeight();
124 finalSize
.BSize(wm
) = logicalHeight
;
125 aMetrics
.SetBlockStartAscent(nsLayoutUtils::GetCenteredFontBaseline(fm
, logicalHeight
));
128 aMetrics
.SetBlockStartAscent(aMetrics
.BSize(wm
) = 0);
131 // XXX temporary until I figure out a better solution; see the
132 // code in nsLineLayout::VerticalAlignFrames that zaps minY/maxY
133 // if the width is zero.
134 // XXX This also fixes bug 10036!
135 // Warning: nsTextControlFrame::CalculateSizeStandard depends on
136 // the following line, see bug 228752.
137 finalSize
.ISize(wm
) = 1;
140 // Return our reflow status
141 uint32_t breakType
= aReflowState
.mStyleDisplay
->mBreakType
;
142 if (NS_STYLE_CLEAR_NONE
== breakType
) {
143 breakType
= NS_STYLE_CLEAR_LINE
;
146 aStatus
= NS_INLINE_BREAK
| NS_INLINE_BREAK_AFTER
|
147 NS_INLINE_MAKE_BREAK_TYPE(breakType
);
148 ll
->SetLineEndsInBR(true);
151 aStatus
= NS_FRAME_COMPLETE
;
154 aMetrics
.SetSize(wm
, finalSize
);
155 aMetrics
.SetOverflowAreasToDesiredBounds();
157 mAscent
= aMetrics
.BlockStartAscent();
159 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aMetrics
);
163 BRFrame::AddInlineMinISize(nsRenderingContext
*aRenderingContext
,
164 nsIFrame::InlineMinISizeData
*aData
)
166 aData
->ForceBreak(aRenderingContext
);
170 BRFrame::AddInlinePrefISize(nsRenderingContext
*aRenderingContext
,
171 nsIFrame::InlinePrefISizeData
*aData
)
173 aData
->ForceBreak(aRenderingContext
);
176 /* virtual */ nscoord
177 BRFrame::GetMinISize(nsRenderingContext
*aRenderingContext
)
180 DISPLAY_MIN_WIDTH(this, result
);
184 /* virtual */ nscoord
185 BRFrame::GetPrefISize(nsRenderingContext
*aRenderingContext
)
188 DISPLAY_PREF_WIDTH(this, result
);
193 BRFrame::GetType() const
195 return nsGkAtoms::brFrame
;
199 BRFrame::GetLogicalBaseline(mozilla::WritingMode aWritingMode
) const
204 nsIFrame::ContentOffsets
BRFrame::CalcContentOffsetsFromFramePoint(nsPoint aPoint
)
206 ContentOffsets offsets
;
207 offsets
.content
= mContent
->GetParent();
208 if (offsets
.content
) {
209 offsets
.offset
= offsets
.content
->IndexOf(mContent
);
210 offsets
.secondaryOffset
= offsets
.offset
;
211 offsets
.associate
= CARET_ASSOCIATE_AFTER
;
216 nsIFrame::FrameSearchResult
217 BRFrame::PeekOffsetNoAmount(bool aForward
, int32_t* aOffset
)
219 NS_ASSERTION (aOffset
&& *aOffset
<= 1, "aOffset out of range");
220 int32_t startOffset
= *aOffset
;
221 // If we hit the end of a BR going backwards, go to its beginning and stay there.
222 if (!aForward
&& startOffset
!= 0) {
226 // Otherwise, stop if we hit the beginning, continue (forward) if we hit the end.
227 return (startOffset
== 0) ? FOUND
: CONTINUE
;
230 nsIFrame::FrameSearchResult
231 BRFrame::PeekOffsetCharacter(bool aForward
, int32_t* aOffset
,
232 bool aRespectClusters
)
234 NS_ASSERTION (aOffset
&& *aOffset
<= 1, "aOffset out of range");
235 // Keep going. The actual line jumping will stop us.
239 nsIFrame::FrameSearchResult
240 BRFrame::PeekOffsetWord(bool aForward
, bool aWordSelectEatSpace
, bool aIsKeyboardSelect
,
241 int32_t* aOffset
, PeekWordState
* aState
)
243 NS_ASSERTION (aOffset
&& *aOffset
<= 1, "aOffset out of range");
244 // Keep going. The actual line jumping will stop us.
250 BRFrame::AccessibleType()
252 nsIContent
*parent
= mContent
->GetParent();
253 if (parent
&& parent
->IsRootOfNativeAnonymousSubtree() &&
254 parent
->GetChildCount() == 1) {
255 // This <br> is the only node in a text control, therefore it is the hacky
256 // "bogus node" used when there is no text in the control
257 return a11y::eNoType
;
260 return a11y::eHTMLBRType
;