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/. */
6 
7 /* rendering object for HTML <br> elements */
8 
9 #include "mozilla/PresShell.h"
10 #include "mozilla/dom/HTMLBRElement.h"
11 #include "gfxContext.h"
12 #include "nsCOMPtr.h"
13 #include "nsContainerFrame.h"
14 #include "nsFontMetrics.h"
15 #include "nsHTMLParts.h"
16 #include "nsIFrame.h"
17 #include "nsPresContext.h"
18 #include "nsLineLayout.h"
19 #include "nsStyleConsts.h"
20 #include "nsGkAtoms.h"
21 #include "nsLayoutUtils.h"
22 
23 // FOR SELECTION
24 #include "nsIContent.h"
25 // END INCLUDES FOR SELECTION
26 
27 using namespace mozilla;
28 
29 namespace mozilla {
30 
31 class BRFrame final : public nsIFrame {
32  public:
33   NS_DECL_FRAMEARENA_HELPERS(BRFrame)
34 
35   friend nsIFrame* ::NS_NewBRFrame(mozilla::PresShell* aPresShell,
36                                    ComputedStyle* aStyle);
37 
38   ContentOffsets CalcContentOffsetsFromFramePoint(
39       const nsPoint& aPoint) override;
40 
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;
50 
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;
62 
IsFrameOfType(uint32_t aFlags) const63   virtual bool IsFrameOfType(uint32_t aFlags) const override {
64     return nsIFrame::IsFrameOfType(
65         aFlags & ~(nsIFrame::eReplaced | nsIFrame::eLineParticipant));
66   }
67 
68 #ifdef ACCESSIBILITY
69   virtual mozilla::a11y::AccType AccessibleType() override;
70 #endif
71 
72  protected:
BRFrame(ComputedStyle * aStyle,nsPresContext * aPresContext)73   explicit BRFrame(ComputedStyle* aStyle, nsPresContext* aPresContext)
74       : nsIFrame(aStyle, aPresContext, kClassID),
75         mAscent(NS_INTRINSIC_ISIZE_UNKNOWN) {}
76 
77   virtual ~BRFrame();
78 
79   nscoord mAscent;
80 };
81 
82 }  // namespace mozilla
83 
NS_NewBRFrame(mozilla::PresShell * aPresShell,ComputedStyle * aStyle)84 nsIFrame* NS_NewBRFrame(mozilla::PresShell* aPresShell, ComputedStyle* aStyle) {
85   return new (aPresShell) BRFrame(aStyle, aPresShell->GetPresContext());
86 }
87 
88 NS_IMPL_FRAMEARENA_HELPERS(BRFrame)
89 
90 BRFrame::~BRFrame() = default;
91 
Reflow(nsPresContext * aPresContext,ReflowOutput & aMetrics,const ReflowInput & aReflowInput,nsReflowStatus & aStatus)92 void BRFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aMetrics,
93                      const ReflowInput& aReflowInput, nsReflowStatus& aStatus) {
94   MarkInReflow();
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!");
98 
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);
106 
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.
120       //
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
129 
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);
135       if (fm) {
136         nscoord logicalHeight = aReflowInput.GetLineHeight();
137         finalSize.BSize(wm) = logicalHeight;
138         aMetrics.SetBlockStartAscent(nsLayoutUtils::GetCenteredFontBaseline(
139             fm, logicalHeight, wm.IsLineInverted()));
140       } else {
141         aMetrics.SetBlockStartAscent(aMetrics.BSize(wm) = 0);
142       }
143 
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;
152     }
153 
154     // Return our reflow status
155     StyleClear breakType = aReflowInput.mStyleDisplay->mBreakType;
156     if (StyleClear::None == breakType) {
157       breakType = StyleClear::Line;
158     }
159 
160     aStatus.SetInlineLineBreakAfter(breakType);
161     ll->SetLineEndsInBR(true);
162   }
163 
164   aMetrics.SetSize(wm, finalSize);
165   aMetrics.SetOverflowAreasToDesiredBounds();
166 
167   mAscent = aMetrics.BlockStartAscent();
168 
169   NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aMetrics);
170 }
171 
172 /* virtual */
AddInlineMinISize(gfxContext * aRenderingContext,nsIFrame::InlineMinISizeData * aData)173 void BRFrame::AddInlineMinISize(gfxContext* aRenderingContext,
174                                 nsIFrame::InlineMinISizeData* aData) {
175   if (!GetParent()->Style()->ShouldSuppressLineBreak()) {
176     aData->ForceBreak();
177   }
178 }
179 
180 /* virtual */
AddInlinePrefISize(gfxContext * aRenderingContext,nsIFrame::InlinePrefISizeData * aData)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;
186     aData->ForceBreak();
187   }
188 }
189 
190 /* virtual */
GetMinISize(gfxContext * aRenderingContext)191 nscoord BRFrame::GetMinISize(gfxContext* aRenderingContext) {
192   nscoord result = 0;
193   DISPLAY_MIN_INLINE_SIZE(this, result);
194   return result;
195 }
196 
197 /* virtual */
GetPrefISize(gfxContext * aRenderingContext)198 nscoord BRFrame::GetPrefISize(gfxContext* aRenderingContext) {
199   nscoord result = 0;
200   DISPLAY_PREF_INLINE_SIZE(this, result);
201   return result;
202 }
203 
GetLogicalBaseline(mozilla::WritingMode aWritingMode) const204 nscoord BRFrame::GetLogicalBaseline(mozilla::WritingMode aWritingMode) const {
205   return mAscent;
206 }
207 
CalcContentOffsetsFromFramePoint(const nsPoint & aPoint)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_Deprecated(mContent);
214     offsets.secondaryOffset = offsets.offset;
215     offsets.associate = CARET_ASSOCIATE_AFTER;
216   }
217   return offsets;
218 }
219 
PeekOffsetNoAmount(bool aForward,int32_t * aOffset)220 nsIFrame::FrameSearchResult BRFrame::PeekOffsetNoAmount(bool aForward,
221                                                         int32_t* aOffset) {
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
225   // there.
226   if (!aForward && startOffset != 0) {
227     *aOffset = 0;
228     return FOUND;
229   }
230   // Otherwise, stop if we hit the beginning, continue (forward) if we hit the
231   // end.
232   return (startOffset == 0) ? FOUND : CONTINUE;
233 }
234 
PeekOffsetCharacter(bool aForward,int32_t * aOffset,PeekOffsetCharacterOptions aOptions)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.
239   return CONTINUE;
240 }
241 
PeekOffsetWord(bool aForward,bool aWordSelectEatSpace,bool aIsKeyboardSelect,int32_t * aOffset,PeekWordState * aState,bool aTrimSpaces)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.
247   return CONTINUE;
248 }
249 
250 #ifdef ACCESSIBILITY
AccessibleType()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;
258   }
259 
260   return a11y::eHTMLBRType;
261 }
262 #endif
263