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/CaretAssociationHint.h"
10 #include "mozilla/PresShell.h"
11 #include "mozilla/dom/HTMLBRElement.h"
12 #include "gfxContext.h"
14 #include "nsContainerFrame.h"
15 #include "nsFontMetrics.h"
16 #include "nsHTMLParts.h"
18 #include "nsPresContext.h"
19 #include "nsLineLayout.h"
20 #include "nsStyleConsts.h"
21 #include "nsGkAtoms.h"
22 #include "nsLayoutUtils.h"
25 #include "nsIContent.h"
26 // END INCLUDES FOR SELECTION
28 using namespace mozilla
;
32 class BRFrame final
: public nsIFrame
{
34 NS_DECL_FRAMEARENA_HELPERS(BRFrame
)
36 friend nsIFrame
* ::NS_NewBRFrame(mozilla::PresShell
* aPresShell
,
37 ComputedStyle
* aStyle
);
39 ContentOffsets
CalcContentOffsetsFromFramePoint(
40 const nsPoint
& aPoint
) override
;
42 FrameSearchResult
PeekOffsetNoAmount(bool aForward
,
43 int32_t* aOffset
) override
;
44 FrameSearchResult
PeekOffsetCharacter(
45 bool aForward
, int32_t* aOffset
,
46 PeekOffsetCharacterOptions aOptions
=
47 PeekOffsetCharacterOptions()) override
;
48 FrameSearchResult
PeekOffsetWord(bool aForward
, bool aWordSelectEatSpace
,
49 bool aIsKeyboardSelect
, int32_t* aOffset
,
50 PeekWordState
* aState
,
51 bool aTrimSpaces
) override
;
53 void Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aMetrics
,
54 const ReflowInput
& aReflowInput
,
55 nsReflowStatus
& aStatus
) override
;
56 void AddInlineMinISize(gfxContext
* aRenderingContext
,
57 InlineMinISizeData
* aData
) override
;
58 void AddInlinePrefISize(gfxContext
* aRenderingContext
,
59 InlinePrefISizeData
* aData
) override
;
60 nscoord
GetMinISize(gfxContext
* aRenderingContext
) override
;
61 nscoord
GetPrefISize(gfxContext
* aRenderingContext
) override
;
63 Maybe
<nscoord
> GetNaturalBaselineBOffset(
64 WritingMode aWM
, BaselineSharingGroup aBaselineGroup
,
65 BaselineExportContext
) const override
;
68 mozilla::a11y::AccType
AccessibleType() override
;
71 #ifdef DEBUG_FRAME_DUMP
72 nsresult
GetFrameName(nsAString
& aResult
) const override
{
73 return MakeFrameName(u
"BR"_ns
, aResult
);
78 BRFrame(ComputedStyle
* aStyle
, nsPresContext
* aPresContext
)
79 : nsIFrame(aStyle
, aPresContext
, kClassID
),
80 mAscent(NS_INTRINSIC_ISIZE_UNKNOWN
) {}
87 } // namespace mozilla
89 nsIFrame
* NS_NewBRFrame(mozilla::PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
90 return new (aPresShell
) BRFrame(aStyle
, aPresShell
->GetPresContext());
93 NS_IMPL_FRAMEARENA_HELPERS(BRFrame
)
95 BRFrame::~BRFrame() = default;
97 void BRFrame::Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aMetrics
,
98 const ReflowInput
& aReflowInput
, nsReflowStatus
& aStatus
) {
100 DO_GLOBAL_REFLOW_COUNT("BRFrame");
101 DISPLAY_REFLOW(aPresContext
, this, aReflowInput
, aMetrics
, aStatus
);
102 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
104 WritingMode wm
= aReflowInput
.GetWritingMode();
105 LogicalSize
finalSize(wm
);
106 finalSize
.BSize(wm
) = 0; // BR frames with block size 0 are ignored in quirks
107 // mode by nsLineLayout::VerticalAlignFrames .
108 // However, it's not always 0. See below.
109 finalSize
.ISize(wm
) = 0;
110 aMetrics
.SetBlockStartAscent(0);
112 // Only when the BR is operating in a line-layout situation will it
113 // behave like a BR. Additionally, we suppress breaks from BR inside
114 // of ruby frames. To determine if we're inside ruby, we have to rely
115 // on the *parent's* ShouldSuppressLineBreak() method, instead of our
116 // own, because we may have custom "display" value that makes our
117 // ShouldSuppressLineBreak() return false.
118 nsLineLayout
* ll
= aReflowInput
.mLineLayout
;
119 if (ll
&& !GetParent()->Style()->ShouldSuppressLineBreak()) {
120 // Note that the compatibility mode check excludes AlmostStandards
121 // mode, since this is the inline box model. See bug 161691.
122 if (ll
->LineIsEmpty() ||
123 aPresContext
->CompatibilityMode() == eCompatibility_FullStandards
) {
124 // The line is logically empty; any whitespace is trimmed away.
126 // If this frame is going to terminate the line we know
127 // that nothing else will go on the line. Therefore, in this
128 // case, we provide some height for the BR frame so that it
129 // creates some vertical whitespace. It's necessary to use the
130 // line-height rather than the font size because the
131 // quirks-mode fix that doesn't apply the block's min
132 // line-height makes this necessary to make BR cause a line
133 // of the full line-height
135 // We also do this in strict mode because BR should act like a
136 // normal inline frame. That line-height is used is important
137 // here for cases where the line-height is less than 1.
138 RefPtr
<nsFontMetrics
> fm
=
139 nsLayoutUtils::GetInflatedFontMetricsForFrame(this);
141 nscoord logicalHeight
= aReflowInput
.GetLineHeight();
142 finalSize
.BSize(wm
) = logicalHeight
;
143 aMetrics
.SetBlockStartAscent(nsLayoutUtils::GetCenteredFontBaseline(
144 fm
, logicalHeight
, wm
.IsLineInverted()));
146 aMetrics
.SetBlockStartAscent(aMetrics
.BSize(wm
) = 0);
149 // XXX temporary until I figure out a better solution; see the
150 // code in nsLineLayout::VerticalAlignFrames that zaps minY/maxY
151 // if the width is zero.
152 // XXX This also fixes bug 10036!
153 // Warning: nsTextControlFrame::CalculateSizeStandard depends on
154 // the following line, see bug 228752.
155 // The code below in AddInlinePrefISize also adds 1 appunit to width
156 finalSize
.ISize(wm
) = 1;
159 // Return our reflow status
160 aStatus
.SetInlineLineBreakAfter(aReflowInput
.mStyleDisplay
->mClear
);
161 ll
->SetLineEndsInBR(true);
164 aMetrics
.SetSize(wm
, finalSize
);
165 aMetrics
.SetOverflowAreasToDesiredBounds();
167 mAscent
= aMetrics
.BlockStartAscent();
171 void BRFrame::AddInlineMinISize(gfxContext
* aRenderingContext
,
172 nsIFrame::InlineMinISizeData
* aData
) {
173 if (!GetParent()->Style()->ShouldSuppressLineBreak()) {
179 void BRFrame::AddInlinePrefISize(gfxContext
* aRenderingContext
,
180 nsIFrame::InlinePrefISizeData
* aData
) {
181 if (!GetParent()->Style()->ShouldSuppressLineBreak()) {
182 // Match the 1 appunit width assigned in the Reflow method above
183 aData
->mCurrentLine
+= 1;
189 nscoord
BRFrame::GetMinISize(gfxContext
* aRenderingContext
) {
191 DISPLAY_MIN_INLINE_SIZE(this, result
);
196 nscoord
BRFrame::GetPrefISize(gfxContext
* aRenderingContext
) {
198 DISPLAY_PREF_INLINE_SIZE(this, result
);
202 Maybe
<nscoord
> BRFrame::GetNaturalBaselineBOffset(
203 WritingMode aWM
, BaselineSharingGroup aBaselineGroup
,
204 BaselineExportContext
) const {
205 if (aBaselineGroup
== BaselineSharingGroup::Last
) {
208 return Some(mAscent
);
211 nsIFrame::ContentOffsets
BRFrame::CalcContentOffsetsFromFramePoint(
212 const nsPoint
& aPoint
) {
213 ContentOffsets offsets
;
214 offsets
.content
= mContent
->GetParent();
215 if (offsets
.content
) {
216 offsets
.offset
= offsets
.content
->ComputeIndexOf_Deprecated(mContent
);
217 offsets
.secondaryOffset
= offsets
.offset
;
218 offsets
.associate
= CaretAssociationHint::After
;
223 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetNoAmount(bool aForward
,
225 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
226 int32_t startOffset
= *aOffset
;
227 // If we hit the end of a BR going backwards, go to its beginning and stay
229 if (!aForward
&& startOffset
!= 0) {
233 // Otherwise, stop if we hit the beginning, continue (forward) if we hit the
235 return (startOffset
== 0) ? FOUND
: CONTINUE
;
238 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetCharacter(
239 bool aForward
, int32_t* aOffset
, PeekOffsetCharacterOptions aOptions
) {
240 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
241 // Keep going. The actual line jumping will stop us.
245 nsIFrame::FrameSearchResult
BRFrame::PeekOffsetWord(
246 bool aForward
, bool aWordSelectEatSpace
, bool aIsKeyboardSelect
,
247 int32_t* aOffset
, PeekWordState
* aState
, bool aTrimSpaces
) {
248 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
249 // Keep going. The actual line jumping will stop us.
254 a11y::AccType
BRFrame::AccessibleType() {
255 dom::HTMLBRElement
* brElement
= dom::HTMLBRElement::FromNode(mContent
);
256 if (brElement
->IsPaddingForEmptyEditor() ||
257 brElement
->IsPaddingForEmptyLastLine()) {
258 // This <br> is a "padding <br> element" used when there is no text or an
259 // empty last line in an editor.
260 return a11y::eNoType
;
263 return a11y::eHTMLBRType
;