1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* rendering object for HTML <br> elements */
9 #include "mozilla/PresShell.h"
10 #include "mozilla/dom/HTMLBRElement.h"
11 #include "gfxContext.h"
13 #include "nsContainerFrame.h"
14 #include "nsFontMetrics.h"
15 #include "nsHTMLParts.h"
17 #include "nsPresContext.h"
18 #include "nsLineLayout.h"
19 #include "nsStyleConsts.h"
20 #include "nsGkAtoms.h"
21 #include "nsLayoutUtils.h"
24 #include "nsIContent.h"
25 // END INCLUDES FOR SELECTION
27 using namespace mozilla
;
31 class BRFrame final
: public nsIFrame
{
33 NS_DECL_FRAMEARENA_HELPERS(BRFrame
)
35 friend nsIFrame
* ::NS_NewBRFrame(mozilla::PresShell
* aPresShell
,
36 ComputedStyle
* aStyle
);
38 ContentOffsets
CalcContentOffsetsFromFramePoint(
39 const nsPoint
& aPoint
) override
;
41 virtual FrameSearchResult
PeekOffsetNoAmount(bool aForward
,
42 int32_t* aOffset
) override
;
43 virtual FrameSearchResult
PeekOffsetCharacter(
44 bool aForward
, int32_t* aOffset
,
45 PeekOffsetCharacterOptions aOptions
=
46 PeekOffsetCharacterOptions()) override
;
47 virtual FrameSearchResult
PeekOffsetWord(
48 bool aForward
, bool aWordSelectEatSpace
, bool aIsKeyboardSelect
,
49 int32_t* aOffset
, PeekWordState
* aState
, bool aTrimSpaces
) override
;
51 virtual void Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aDesiredSize
,
52 const ReflowInput
& aReflowInput
,
53 nsReflowStatus
& aStatus
) override
;
54 virtual void AddInlineMinISize(gfxContext
* aRenderingContext
,
55 InlineMinISizeData
* aData
) override
;
56 virtual void AddInlinePrefISize(gfxContext
* aRenderingContext
,
57 InlinePrefISizeData
* aData
) override
;
58 virtual nscoord
GetMinISize(gfxContext
* aRenderingContext
) override
;
59 virtual nscoord
GetPrefISize(gfxContext
* aRenderingContext
) override
;
60 virtual nscoord
GetLogicalBaseline(
61 mozilla::WritingMode aWritingMode
) const override
;
63 virtual bool IsFrameOfType(uint32_t aFlags
) const override
{
64 return nsIFrame::IsFrameOfType(
65 aFlags
& ~(nsIFrame::eReplaced
| nsIFrame::eLineParticipant
));
69 virtual mozilla::a11y::AccType
AccessibleType() override
;
73 explicit BRFrame(ComputedStyle
* aStyle
, nsPresContext
* aPresContext
)
74 : nsIFrame(aStyle
, aPresContext
, kClassID
),
75 mAscent(NS_INTRINSIC_ISIZE_UNKNOWN
) {}
82 } // namespace mozilla
84 nsIFrame
* NS_NewBRFrame(mozilla::PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
85 return new (aPresShell
) BRFrame(aStyle
, aPresShell
->GetPresContext());
88 NS_IMPL_FRAMEARENA_HELPERS(BRFrame
)
90 BRFrame::~BRFrame() = default;
92 void BRFrame::Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aMetrics
,
93 const ReflowInput
& aReflowInput
, nsReflowStatus
& aStatus
) {
95 DO_GLOBAL_REFLOW_COUNT("BRFrame");
96 DISPLAY_REFLOW(aPresContext
, this, aReflowInput
, aMetrics
, aStatus
);
97 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
99 WritingMode wm
= aReflowInput
.GetWritingMode();
100 LogicalSize
finalSize(wm
);
101 finalSize
.BSize(wm
) = 0; // BR frames with block size 0 are ignored in quirks
102 // mode by nsLineLayout::VerticalAlignFrames .
103 // However, it's not always 0. See below.
104 finalSize
.ISize(wm
) = 0;
105 aMetrics
.SetBlockStartAscent(0);
107 // Only when the BR is operating in a line-layout situation will it
108 // behave like a BR. Additionally, we suppress breaks from BR inside
109 // of ruby frames. To determine if we're inside ruby, we have to rely
110 // on the *parent's* ShouldSuppressLineBreak() method, instead of our
111 // own, because we may have custom "display" value that makes our
112 // ShouldSuppressLineBreak() return false.
113 nsLineLayout
* ll
= aReflowInput
.mLineLayout
;
114 if (ll
&& !GetParent()->Style()->ShouldSuppressLineBreak()) {
115 // Note that the compatibility mode check excludes AlmostStandards
116 // mode, since this is the inline box model. See bug 161691.
117 if (ll
->LineIsEmpty() ||
118 aPresContext
->CompatibilityMode() == eCompatibility_FullStandards
) {
119 // The line is logically empty; any whitespace is trimmed away.
121 // If this frame is going to terminate the line we know
122 // that nothing else will go on the line. Therefore, in this
123 // case, we provide some height for the BR frame so that it
124 // creates some vertical whitespace. It's necessary to use the
125 // line-height rather than the font size because the
126 // quirks-mode fix that doesn't apply the block's min
127 // line-height makes this necessary to make BR cause a line
128 // of the full line-height
130 // We also do this in strict mode because BR should act like a
131 // normal inline frame. That line-height is used is important
132 // here for cases where the line-height is less than 1.
133 RefPtr
<nsFontMetrics
> fm
=
134 nsLayoutUtils::GetInflatedFontMetricsForFrame(this);
136 nscoord logicalHeight
= aReflowInput
.CalcLineHeight();
137 finalSize
.BSize(wm
) = logicalHeight
;
138 aMetrics
.SetBlockStartAscent(nsLayoutUtils::GetCenteredFontBaseline(
139 fm
, logicalHeight
, wm
.IsLineInverted()));
141 aMetrics
.SetBlockStartAscent(aMetrics
.BSize(wm
) = 0);
144 // XXX temporary until I figure out a better solution; see the
145 // code in nsLineLayout::VerticalAlignFrames that zaps minY/maxY
146 // if the width is zero.
147 // XXX This also fixes bug 10036!
148 // Warning: nsTextControlFrame::CalculateSizeStandard depends on
149 // the following line, see bug 228752.
150 // The code below in AddInlinePrefISize also adds 1 appunit to width
151 finalSize
.ISize(wm
) = 1;
154 // Return our reflow status
155 StyleClear breakType
= aReflowInput
.mStyleDisplay
->mBreakType
;
156 if (StyleClear::None
== breakType
) {
157 breakType
= StyleClear::Line
;
160 aStatus
.SetInlineLineBreakAfter(breakType
);
161 ll
->SetLineEndsInBR(true);
164 aMetrics
.SetSize(wm
, finalSize
);
165 aMetrics
.SetOverflowAreasToDesiredBounds();
167 mAscent
= aMetrics
.BlockStartAscent();
169 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowInput
, aMetrics
);
173 void BRFrame::AddInlineMinISize(gfxContext
* aRenderingContext
,
174 nsIFrame::InlineMinISizeData
* aData
) {
175 if (!GetParent()->Style()->ShouldSuppressLineBreak()) {
181 void BRFrame::AddInlinePrefISize(gfxContext
* aRenderingContext
,
182 nsIFrame::InlinePrefISizeData
* aData
) {
183 if (!GetParent()->Style()->ShouldSuppressLineBreak()) {
184 // Match the 1 appunit width assigned in the Reflow method above
185 aData
->mCurrentLine
+= 1;
191 nscoord
BRFrame::GetMinISize(gfxContext
* aRenderingContext
) {
193 DISPLAY_MIN_INLINE_SIZE(this, result
);
198 nscoord
BRFrame::GetPrefISize(gfxContext
* aRenderingContext
) {
200 DISPLAY_PREF_INLINE_SIZE(this, result
);
204 nscoord
BRFrame::GetLogicalBaseline(mozilla::WritingMode aWritingMode
) const {
208 nsIFrame::ContentOffsets
BRFrame::CalcContentOffsetsFromFramePoint(
209 const nsPoint
& aPoint
) {
210 ContentOffsets offsets
;
211 offsets
.content
= mContent
->GetParent();
212 if (offsets
.content
) {
213 offsets
.offset
= offsets
.content
->ComputeIndexOf(mContent
);
214 offsets
.secondaryOffset
= offsets
.offset
;
215 offsets
.associate
= CARET_ASSOCIATE_AFTER
;
220 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetNoAmount(bool aForward
,
222 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
223 int32_t startOffset
= *aOffset
;
224 // If we hit the end of a BR going backwards, go to its beginning and stay
226 if (!aForward
&& startOffset
!= 0) {
230 // Otherwise, stop if we hit the beginning, continue (forward) if we hit the
232 return (startOffset
== 0) ? FOUND
: CONTINUE
;
235 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetCharacter(
236 bool aForward
, int32_t* aOffset
, PeekOffsetCharacterOptions aOptions
) {
237 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
238 // Keep going. The actual line jumping will stop us.
242 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetWord(
243 bool aForward
, bool aWordSelectEatSpace
, bool aIsKeyboardSelect
,
244 int32_t* aOffset
, PeekWordState
* aState
, bool aTrimSpaces
) {
245 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
246 // Keep going. The actual line jumping will stop us.
251 a11y::AccType
BRFrame::AccessibleType() {
252 dom::HTMLBRElement
* brElement
= dom::HTMLBRElement::FromNode(mContent
);
253 if (brElement
->IsPaddingForEmptyEditor() ||
254 brElement
->IsPaddingForEmptyLastLine()) {
255 // This <br> is a "padding <br> element" used when there is no text or an
256 // empty last line in an editor.
257 return a11y::eNoType
;
260 return a11y::eHTMLBRType
;