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 FrameSearchResult
PeekOffsetNoAmount(bool aForward
,
42 int32_t* aOffset
) override
;
43 FrameSearchResult
PeekOffsetCharacter(
44 bool aForward
, int32_t* aOffset
,
45 PeekOffsetCharacterOptions aOptions
=
46 PeekOffsetCharacterOptions()) override
;
47 FrameSearchResult
PeekOffsetWord(bool aForward
, bool aWordSelectEatSpace
,
48 bool aIsKeyboardSelect
, int32_t* aOffset
,
49 PeekWordState
* aState
,
50 bool aTrimSpaces
) override
;
52 void Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aMetrics
,
53 const ReflowInput
& aReflowInput
,
54 nsReflowStatus
& aStatus
) override
;
55 void AddInlineMinISize(gfxContext
* aRenderingContext
,
56 InlineMinISizeData
* aData
) override
;
57 void AddInlinePrefISize(gfxContext
* aRenderingContext
,
58 InlinePrefISizeData
* aData
) override
;
59 nscoord
GetMinISize(gfxContext
* aRenderingContext
) override
;
60 nscoord
GetPrefISize(gfxContext
* aRenderingContext
) override
;
62 Maybe
<nscoord
> GetNaturalBaselineBOffset(
63 WritingMode aWM
, BaselineSharingGroup aBaselineGroup
,
64 BaselineExportContext
) const override
;
66 bool IsFrameOfType(uint32_t aFlags
) const override
{
67 return nsIFrame::IsFrameOfType(
68 aFlags
& ~(nsIFrame::eReplaced
| nsIFrame::eLineParticipant
));
72 mozilla::a11y::AccType
AccessibleType() override
;
75 #ifdef DEBUG_FRAME_DUMP
76 nsresult
GetFrameName(nsAString
& aResult
) const override
{
77 return MakeFrameName(u
"BR"_ns
, aResult
);
82 BRFrame(ComputedStyle
* aStyle
, nsPresContext
* aPresContext
)
83 : nsIFrame(aStyle
, aPresContext
, kClassID
),
84 mAscent(NS_INTRINSIC_ISIZE_UNKNOWN
) {}
91 } // namespace mozilla
93 nsIFrame
* NS_NewBRFrame(mozilla::PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
94 return new (aPresShell
) BRFrame(aStyle
, aPresShell
->GetPresContext());
97 NS_IMPL_FRAMEARENA_HELPERS(BRFrame
)
99 BRFrame::~BRFrame() = default;
101 void BRFrame::Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aMetrics
,
102 const ReflowInput
& aReflowInput
, nsReflowStatus
& aStatus
) {
104 DO_GLOBAL_REFLOW_COUNT("BRFrame");
105 DISPLAY_REFLOW(aPresContext
, this, aReflowInput
, aMetrics
, aStatus
);
106 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
108 WritingMode wm
= aReflowInput
.GetWritingMode();
109 LogicalSize
finalSize(wm
);
110 finalSize
.BSize(wm
) = 0; // BR frames with block size 0 are ignored in quirks
111 // mode by nsLineLayout::VerticalAlignFrames .
112 // However, it's not always 0. See below.
113 finalSize
.ISize(wm
) = 0;
114 aMetrics
.SetBlockStartAscent(0);
116 // Only when the BR is operating in a line-layout situation will it
117 // behave like a BR. Additionally, we suppress breaks from BR inside
118 // of ruby frames. To determine if we're inside ruby, we have to rely
119 // on the *parent's* ShouldSuppressLineBreak() method, instead of our
120 // own, because we may have custom "display" value that makes our
121 // ShouldSuppressLineBreak() return false.
122 nsLineLayout
* ll
= aReflowInput
.mLineLayout
;
123 if (ll
&& !GetParent()->Style()->ShouldSuppressLineBreak()) {
124 // Note that the compatibility mode check excludes AlmostStandards
125 // mode, since this is the inline box model. See bug 161691.
126 if (ll
->LineIsEmpty() ||
127 aPresContext
->CompatibilityMode() == eCompatibility_FullStandards
) {
128 // The line is logically empty; any whitespace is trimmed away.
130 // If this frame is going to terminate the line we know
131 // that nothing else will go on the line. Therefore, in this
132 // case, we provide some height for the BR frame so that it
133 // creates some vertical whitespace. It's necessary to use the
134 // line-height rather than the font size because the
135 // quirks-mode fix that doesn't apply the block's min
136 // line-height makes this necessary to make BR cause a line
137 // of the full line-height
139 // We also do this in strict mode because BR should act like a
140 // normal inline frame. That line-height is used is important
141 // here for cases where the line-height is less than 1.
142 RefPtr
<nsFontMetrics
> fm
=
143 nsLayoutUtils::GetInflatedFontMetricsForFrame(this);
145 nscoord logicalHeight
= aReflowInput
.GetLineHeight();
146 finalSize
.BSize(wm
) = logicalHeight
;
147 aMetrics
.SetBlockStartAscent(nsLayoutUtils::GetCenteredFontBaseline(
148 fm
, logicalHeight
, wm
.IsLineInverted()));
150 aMetrics
.SetBlockStartAscent(aMetrics
.BSize(wm
) = 0);
153 // XXX temporary until I figure out a better solution; see the
154 // code in nsLineLayout::VerticalAlignFrames that zaps minY/maxY
155 // if the width is zero.
156 // XXX This also fixes bug 10036!
157 // Warning: nsTextControlFrame::CalculateSizeStandard depends on
158 // the following line, see bug 228752.
159 // The code below in AddInlinePrefISize also adds 1 appunit to width
160 finalSize
.ISize(wm
) = 1;
163 // Return our reflow status
164 aStatus
.SetInlineLineBreakAfter(aReflowInput
.mStyleDisplay
->mClear
);
165 ll
->SetLineEndsInBR(true);
168 aMetrics
.SetSize(wm
, finalSize
);
169 aMetrics
.SetOverflowAreasToDesiredBounds();
171 mAscent
= aMetrics
.BlockStartAscent();
175 void BRFrame::AddInlineMinISize(gfxContext
* aRenderingContext
,
176 nsIFrame::InlineMinISizeData
* aData
) {
177 if (!GetParent()->Style()->ShouldSuppressLineBreak()) {
183 void BRFrame::AddInlinePrefISize(gfxContext
* aRenderingContext
,
184 nsIFrame::InlinePrefISizeData
* aData
) {
185 if (!GetParent()->Style()->ShouldSuppressLineBreak()) {
186 // Match the 1 appunit width assigned in the Reflow method above
187 aData
->mCurrentLine
+= 1;
193 nscoord
BRFrame::GetMinISize(gfxContext
* aRenderingContext
) {
195 DISPLAY_MIN_INLINE_SIZE(this, result
);
200 nscoord
BRFrame::GetPrefISize(gfxContext
* aRenderingContext
) {
202 DISPLAY_PREF_INLINE_SIZE(this, result
);
206 Maybe
<nscoord
> BRFrame::GetNaturalBaselineBOffset(
207 WritingMode aWM
, BaselineSharingGroup aBaselineGroup
,
208 BaselineExportContext
) const {
209 if (aBaselineGroup
== BaselineSharingGroup::Last
) {
212 return Some(mAscent
);
215 nsIFrame::ContentOffsets
BRFrame::CalcContentOffsetsFromFramePoint(
216 const nsPoint
& aPoint
) {
217 ContentOffsets offsets
;
218 offsets
.content
= mContent
->GetParent();
219 if (offsets
.content
) {
220 offsets
.offset
= offsets
.content
->ComputeIndexOf_Deprecated(mContent
);
221 offsets
.secondaryOffset
= offsets
.offset
;
222 offsets
.associate
= CARET_ASSOCIATE_AFTER
;
227 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetNoAmount(bool aForward
,
229 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
230 int32_t startOffset
= *aOffset
;
231 // If we hit the end of a BR going backwards, go to its beginning and stay
233 if (!aForward
&& startOffset
!= 0) {
237 // Otherwise, stop if we hit the beginning, continue (forward) if we hit the
239 return (startOffset
== 0) ? FOUND
: CONTINUE
;
242 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetCharacter(
243 bool aForward
, int32_t* aOffset
, PeekOffsetCharacterOptions aOptions
) {
244 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
245 // Keep going. The actual line jumping will stop us.
249 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetWord(
250 bool aForward
, bool aWordSelectEatSpace
, bool aIsKeyboardSelect
,
251 int32_t* aOffset
, PeekWordState
* aState
, bool aTrimSpaces
) {
252 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
253 // Keep going. The actual line jumping will stop us.
258 a11y::AccType
BRFrame::AccessibleType() {
259 dom::HTMLBRElement
* brElement
= dom::HTMLBRElement::FromNode(mContent
);
260 if (brElement
->IsPaddingForEmptyEditor() ||
261 brElement
->IsPaddingForEmptyLastLine()) {
262 // This <br> is a "padding <br> element" used when there is no text or an
263 // empty last line in an editor.
264 return a11y::eNoType
;
267 return a11y::eHTMLBRType
;