1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
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 #include "TextOverflow.h"
10 // Please maintain alphabetical order below
11 #include "nsBlockFrame.h"
13 #include "nsContentUtils.h"
14 #include "nsCSSAnonBoxes.h"
15 #include "nsGfxScrollFrame.h"
16 #include "nsIScrollableFrame.h"
17 #include "nsLayoutUtils.h"
18 #include "nsPresContext.h"
20 #include "nsRenderingContext.h"
21 #include "nsTextFrame.h"
22 #include "nsIFrameInlines.h"
23 #include "mozilla/Util.h"
24 #include "mozilla/Likely.h"
29 class LazyReferenceRenderingContextGetterFromFrame MOZ_FINAL
:
30 public gfxFontGroup::LazyReferenceContextGetter
{
32 LazyReferenceRenderingContextGetterFromFrame(nsIFrame
* aFrame
)
34 virtual already_AddRefed
<gfxContext
> GetRefContext() MOZ_OVERRIDE
36 nsRefPtr
<nsRenderingContext
> rc
=
37 mFrame
->PresContext()->PresShell()->GetReferenceRenderingContext();
38 nsRefPtr
<gfxContext
> ctx
= rc
->ThebesContext();
46 GetEllipsisTextRun(nsIFrame
* aFrame
)
48 nsRefPtr
<nsFontMetrics
> fm
;
49 nsLayoutUtils::GetFontMetricsForFrame(aFrame
, getter_AddRefs(fm
),
50 nsLayoutUtils::FontSizeInflationFor(aFrame
));
51 LazyReferenceRenderingContextGetterFromFrame
lazyRefContextGetter(aFrame
);
52 return fm
->GetThebesFontGroup()->GetEllipsisTextRun(
53 aFrame
->PresContext()->AppUnitsPerDevPixel(), lazyRefContextGetter
);
57 GetSelfOrNearestBlock(nsIFrame
* aFrame
)
59 return nsLayoutUtils::GetAsBlock(aFrame
) ? aFrame
:
60 nsLayoutUtils::FindNearestBlockAncestor(aFrame
);
63 // Return true if the frame is an atomic inline-level element.
64 // It's not supposed to be called for block frames since we never
65 // process block descendants for text-overflow.
67 IsAtomicElement(nsIFrame
* aFrame
, const nsIAtom
* aFrameType
)
69 NS_PRECONDITION(!nsLayoutUtils::GetAsBlock(aFrame
) ||
70 !aFrame
->IsBlockOutside(),
71 "unexpected block frame");
72 NS_PRECONDITION(aFrameType
!= nsGkAtoms::placeholderFrame
,
73 "unexpected placeholder frame");
74 return !aFrame
->IsFrameOfType(nsIFrame::eLineParticipant
);
78 IsFullyClipped(nsTextFrame
* aFrame
, nscoord aLeft
, nscoord aRight
,
79 nscoord
* aSnappedLeft
, nscoord
* aSnappedRight
)
81 *aSnappedLeft
= aLeft
;
82 *aSnappedRight
= aRight
;
83 if (aLeft
<= 0 && aRight
<= 0) {
86 return !aFrame
->MeasureCharClippedText(aLeft
, aRight
,
87 aSnappedLeft
, aSnappedRight
);
91 IsHorizontalOverflowVisible(nsIFrame
* aFrame
)
93 NS_PRECONDITION(nsLayoutUtils::GetAsBlock(aFrame
) != nullptr,
94 "expected a block frame");
97 while (f
&& f
->StyleContext()->GetPseudo() &&
98 f
->GetType() != nsGkAtoms::scrollFrame
) {
101 return !f
|| f
->StyleDisplay()->mOverflowX
== NS_STYLE_OVERFLOW_VISIBLE
;
105 ClipMarker(const nsRect
& aContentArea
,
106 const nsRect
& aMarkerRect
,
107 DisplayListClipState::AutoSaveRestore
& aClipState
)
109 nscoord rightOverflow
= aMarkerRect
.XMost() - aContentArea
.XMost();
110 nsRect markerRect
= aMarkerRect
;
111 if (rightOverflow
> 0) {
112 // Marker overflows on the right side (content width < marker width).
113 markerRect
.width
-= rightOverflow
;
114 aClipState
.ClipContentDescendants(markerRect
);
116 nscoord leftOverflow
= aContentArea
.x
- aMarkerRect
.x
;
117 if (leftOverflow
> 0) {
118 // Marker overflows on the left side
119 markerRect
.width
-= leftOverflow
;
120 markerRect
.x
+= leftOverflow
;
121 aClipState
.ClipContentDescendants(markerRect
);
127 InflateLeft(nsRect
* aRect
, nscoord aDelta
)
129 nscoord xmost
= aRect
->XMost();
131 aRect
->width
= std::max(xmost
- aRect
->x
, 0);
135 InflateRight(nsRect
* aRect
, nscoord aDelta
)
137 aRect
->width
= std::max(aRect
->width
+ aDelta
, 0);
141 IsFrameDescendantOfAny(nsIFrame
* aChild
,
142 const TextOverflow::FrameHashtable
& aSetOfFrames
,
143 nsIFrame
* aCommonAncestor
)
145 for (nsIFrame
* f
= aChild
; f
&& f
!= aCommonAncestor
;
146 f
= nsLayoutUtils::GetCrossDocParentFrame(f
)) {
147 if (aSetOfFrames
.GetEntry(f
)) {
154 class nsDisplayTextOverflowMarker
: public nsDisplayItem
157 nsDisplayTextOverflowMarker(nsDisplayListBuilder
* aBuilder
, nsIFrame
* aFrame
,
158 const nsRect
& aRect
, nscoord aAscent
,
159 const nsStyleTextOverflowSide
* aStyle
,
161 : nsDisplayItem(aBuilder
, aFrame
), mRect(aRect
),
162 mStyle(aStyle
), mAscent(aAscent
), mIndex(aIndex
) {
163 MOZ_COUNT_CTOR(nsDisplayTextOverflowMarker
);
165 #ifdef NS_BUILD_REFCNT_LOGGING
166 virtual ~nsDisplayTextOverflowMarker() {
167 MOZ_COUNT_DTOR(nsDisplayTextOverflowMarker
);
170 virtual nsRect
GetBounds(nsDisplayListBuilder
* aBuilder
, bool* aSnap
) {
173 nsLayoutUtils::GetTextShadowRectsUnion(mRect
, mFrame
);
174 return mRect
.Union(shadowRect
);
176 virtual void Paint(nsDisplayListBuilder
* aBuilder
,
177 nsRenderingContext
* aCtx
);
179 virtual uint32_t GetPerFrameKey() {
180 return (mIndex
<< nsDisplayItem::TYPE_BITS
) | nsDisplayItem::GetPerFrameKey();
182 void PaintTextToContext(nsRenderingContext
* aCtx
,
183 nsPoint aOffsetFromRect
);
184 NS_DISPLAY_DECL_NAME("TextOverflow", TYPE_TEXT_OVERFLOW
)
186 nsRect mRect
; // in reference frame coordinates
187 const nsStyleTextOverflowSide
* mStyle
;
188 nscoord mAscent
; // baseline for the marker text in mRect
193 PaintTextShadowCallback(nsRenderingContext
* aCtx
,
194 nsPoint aShadowOffset
,
195 const nscolor
& aShadowColor
,
198 reinterpret_cast<nsDisplayTextOverflowMarker
*>(aData
)->
199 PaintTextToContext(aCtx
, aShadowOffset
);
203 nsDisplayTextOverflowMarker::Paint(nsDisplayListBuilder
* aBuilder
,
204 nsRenderingContext
* aCtx
)
206 nscolor foregroundColor
=
207 nsLayoutUtils::GetColor(mFrame
, eCSSProperty_color
);
209 // Paint the text-shadows for the overflow marker
210 nsLayoutUtils::PaintTextShadow(mFrame
, aCtx
, mRect
, mVisibleRect
,
211 foregroundColor
, PaintTextShadowCallback
,
213 aCtx
->SetColor(foregroundColor
);
214 PaintTextToContext(aCtx
, nsPoint(0, 0));
218 nsDisplayTextOverflowMarker::PaintTextToContext(nsRenderingContext
* aCtx
,
219 nsPoint aOffsetFromRect
)
221 gfxFloat y
= nsLayoutUtils::GetSnappedBaselineY(mFrame
, aCtx
->ThebesContext(),
223 nsPoint
baselinePt(mRect
.x
, NSToCoordFloor(y
));
224 nsPoint pt
= baselinePt
+ aOffsetFromRect
;
226 if (mStyle
->mType
== NS_STYLE_TEXT_OVERFLOW_ELLIPSIS
) {
227 gfxTextRun
* textRun
= GetEllipsisTextRun(mFrame
);
229 NS_ASSERTION(!textRun
->IsRightToLeft(),
230 "Ellipsis textruns should always be LTR!");
231 gfxPoint
gfxPt(pt
.x
, pt
.y
);
232 textRun
->Draw(aCtx
->ThebesContext(), gfxPt
, DrawMode::GLYPH_FILL
,
233 0, textRun
->GetLength(), nullptr, nullptr, nullptr);
236 nsRefPtr
<nsFontMetrics
> fm
;
237 nsLayoutUtils::GetFontMetricsForFrame(mFrame
, getter_AddRefs(fm
),
238 nsLayoutUtils::FontSizeInflationFor(mFrame
));
240 nsLayoutUtils::DrawString(mFrame
, aCtx
, mStyle
->mString
.get(),
241 mStyle
->mString
.Length(), pt
);
246 TextOverflow::Init(nsDisplayListBuilder
* aBuilder
,
247 nsIFrame
* aBlockFrame
)
250 mBlock
= aBlockFrame
;
251 mContentArea
= aBlockFrame
->GetContentRectRelativeToSelf();
252 mScrollableFrame
= nsLayoutUtils::GetScrollableFrameFor(aBlockFrame
);
253 uint8_t direction
= aBlockFrame
->StyleVisibility()->mDirection
;
254 mBlockIsRTL
= direction
== NS_STYLE_DIRECTION_RTL
;
255 mAdjustForPixelSnapping
= false;
257 if (!mScrollableFrame
) {
258 nsIAtom
* pseudoType
= aBlockFrame
->StyleContext()->GetPseudo();
259 if (pseudoType
== nsCSSAnonBoxes::mozXULAnonymousBlock
) {
261 nsLayoutUtils::GetScrollableFrameFor(aBlockFrame
->GetParent());
262 // nsXULScrollFrame::ClampAndSetBounds rounds to nearest pixels
263 // for RTL blocks (also for overflow:hidden), so we need to move
264 // the edges 1px outward in ExamineLineFrames to avoid triggering
265 // a text-overflow marker in this case.
266 mAdjustForPixelSnapping
= mBlockIsRTL
;
270 mCanHaveHorizontalScrollbar
= false;
271 if (mScrollableFrame
) {
272 mCanHaveHorizontalScrollbar
=
273 mScrollableFrame
->GetScrollbarStyles().mHorizontal
!= NS_STYLE_OVERFLOW_HIDDEN
;
274 if (!mAdjustForPixelSnapping
) {
275 // Scrolling to the end position can leave some text still overflowing due
276 // to pixel snapping behaviour in our scrolling code.
277 mAdjustForPixelSnapping
= mCanHaveHorizontalScrollbar
;
279 mContentArea
.MoveBy(mScrollableFrame
->GetScrollPosition());
280 nsIFrame
* scrollFrame
= do_QueryFrame(mScrollableFrame
);
281 scrollFrame
->AddStateBits(NS_SCROLLFRAME_INVALIDATE_CONTENTS_ON_SCROLL
);
283 const nsStyleTextReset
* style
= aBlockFrame
->StyleTextReset();
284 mLeft
.Init(style
->mTextOverflow
.GetLeft(direction
));
285 mRight
.Init(style
->mTextOverflow
.GetRight(direction
));
286 // The left/right marker string is setup in ExamineLineFrames when a line
287 // has overflow on that side.
290 /* static */ TextOverflow
*
291 TextOverflow::WillProcessLines(nsDisplayListBuilder
* aBuilder
,
292 nsIFrame
* aBlockFrame
)
294 if (!CanHaveTextOverflow(aBuilder
, aBlockFrame
)) {
297 nsAutoPtr
<TextOverflow
> textOverflow(new TextOverflow
);
298 textOverflow
->Init(aBuilder
, aBlockFrame
);
299 return textOverflow
.forget();
303 TextOverflow::ExamineFrameSubtree(nsIFrame
* aFrame
,
304 const nsRect
& aContentArea
,
305 const nsRect
& aInsideMarkersArea
,
306 FrameHashtable
* aFramesToHide
,
307 AlignmentEdges
* aAlignmentEdges
,
308 bool* aFoundVisibleTextOrAtomic
,
309 InnerClipEdges
* aClippedMarkerEdges
)
311 const nsIAtom
* frameType
= aFrame
->GetType();
312 if (frameType
== nsGkAtoms::brFrame
||
313 frameType
== nsGkAtoms::placeholderFrame
) {
316 const bool isAtomic
= IsAtomicElement(aFrame
, frameType
);
317 if (aFrame
->StyleVisibility()->IsVisible()) {
318 nsRect childRect
= aFrame
->GetScrollableOverflowRect() +
319 aFrame
->GetOffsetTo(mBlock
);
320 bool overflowLeft
= childRect
.x
< aContentArea
.x
;
321 bool overflowRight
= childRect
.XMost() > aContentArea
.XMost();
323 mLeft
.mHasOverflow
= true;
326 mRight
.mHasOverflow
= true;
328 if (isAtomic
&& ((mLeft
.mActive
&& overflowLeft
) ||
329 (mRight
.mActive
&& overflowRight
))) {
330 aFramesToHide
->PutEntry(aFrame
);
331 } else if (isAtomic
|| frameType
== nsGkAtoms::textFrame
) {
332 AnalyzeMarkerEdges(aFrame
, frameType
, aInsideMarkersArea
,
333 aFramesToHide
, aAlignmentEdges
,
334 aFoundVisibleTextOrAtomic
,
335 aClippedMarkerEdges
);
342 nsIFrame
* child
= aFrame
->GetFirstPrincipalChild();
344 ExamineFrameSubtree(child
, aContentArea
, aInsideMarkersArea
,
345 aFramesToHide
, aAlignmentEdges
,
346 aFoundVisibleTextOrAtomic
,
347 aClippedMarkerEdges
);
348 child
= child
->GetNextSibling();
353 TextOverflow::AnalyzeMarkerEdges(nsIFrame
* aFrame
,
354 const nsIAtom
* aFrameType
,
355 const nsRect
& aInsideMarkersArea
,
356 FrameHashtable
* aFramesToHide
,
357 AlignmentEdges
* aAlignmentEdges
,
358 bool* aFoundVisibleTextOrAtomic
,
359 InnerClipEdges
* aClippedMarkerEdges
)
361 nsRect
borderRect(aFrame
->GetOffsetTo(mBlock
), aFrame
->GetSize());
362 nscoord leftOverlap
=
363 std::max(aInsideMarkersArea
.x
- borderRect
.x
, 0);
364 nscoord rightOverlap
=
365 std::max(borderRect
.XMost() - aInsideMarkersArea
.XMost(), 0);
366 bool insideLeftEdge
= aInsideMarkersArea
.x
<= borderRect
.XMost();
367 bool insideRightEdge
= borderRect
.x
<= aInsideMarkersArea
.XMost();
369 if (leftOverlap
> 0) {
370 aClippedMarkerEdges
->AccumulateLeft(borderRect
);
371 if (!mLeft
.mActive
) {
375 if (rightOverlap
> 0) {
376 aClippedMarkerEdges
->AccumulateRight(borderRect
);
377 if (!mRight
.mActive
) {
382 if ((leftOverlap
> 0 && insideLeftEdge
) ||
383 (rightOverlap
> 0 && insideRightEdge
)) {
384 if (aFrameType
== nsGkAtoms::textFrame
) {
385 if (aInsideMarkersArea
.x
< aInsideMarkersArea
.XMost()) {
386 // a clipped text frame and there is some room between the markers
387 nscoord snappedLeft
, snappedRight
;
388 bool isFullyClipped
=
389 IsFullyClipped(static_cast<nsTextFrame
*>(aFrame
),
390 leftOverlap
, rightOverlap
, &snappedLeft
, &snappedRight
);
391 if (!isFullyClipped
) {
392 nsRect snappedRect
= borderRect
;
393 if (leftOverlap
> 0) {
394 snappedRect
.x
+= snappedLeft
;
395 snappedRect
.width
-= snappedLeft
;
397 if (rightOverlap
> 0) {
398 snappedRect
.width
-= snappedRight
;
400 aAlignmentEdges
->Accumulate(snappedRect
);
401 *aFoundVisibleTextOrAtomic
= true;
405 aFramesToHide
->PutEntry(aFrame
);
407 } else if (!insideLeftEdge
|| !insideRightEdge
) {
409 if (IsAtomicElement(aFrame
, aFrameType
)) {
410 aFramesToHide
->PutEntry(aFrame
);
414 aAlignmentEdges
->Accumulate(borderRect
);
415 *aFoundVisibleTextOrAtomic
= true;
420 TextOverflow::ExamineLineFrames(nsLineBox
* aLine
,
421 FrameHashtable
* aFramesToHide
,
422 AlignmentEdges
* aAlignmentEdges
)
424 // No ellipsing for 'clip' style.
425 bool suppressLeft
= mLeft
.mStyle
->mType
== NS_STYLE_TEXT_OVERFLOW_CLIP
;
426 bool suppressRight
= mRight
.mStyle
->mType
== NS_STYLE_TEXT_OVERFLOW_CLIP
;
427 if (mCanHaveHorizontalScrollbar
) {
428 nsPoint pos
= mScrollableFrame
->GetScrollPosition();
429 nsRect scrollRange
= mScrollableFrame
->GetScrollRange();
430 // No ellipsing when nothing to scroll to on that side (this includes
431 // overflow:auto that doesn't trigger a horizontal scrollbar).
432 if (pos
.x
<= scrollRange
.x
) {
435 if (pos
.x
>= scrollRange
.XMost()) {
436 suppressRight
= true;
440 nsRect contentArea
= mContentArea
;
441 const nscoord scrollAdjust
= mAdjustForPixelSnapping
?
442 mBlock
->PresContext()->AppUnitsPerDevPixel() : 0;
443 InflateLeft(&contentArea
, scrollAdjust
);
444 InflateRight(&contentArea
, scrollAdjust
);
445 nsRect lineRect
= aLine
->GetScrollableOverflowArea();
446 const bool leftOverflow
=
447 !suppressLeft
&& lineRect
.x
< contentArea
.x
;
448 const bool rightOverflow
=
449 !suppressRight
&& lineRect
.XMost() > contentArea
.XMost();
450 if (!leftOverflow
&& !rightOverflow
) {
451 // The line does not overflow on a side we should ellipsize.
456 bool retryEmptyLine
= true;
457 bool guessLeft
= leftOverflow
;
458 bool guessRight
= rightOverflow
;
459 mLeft
.mActive
= leftOverflow
;
460 mRight
.mActive
= rightOverflow
;
461 bool clippedLeftMarker
= false;
462 bool clippedRightMarker
= false;
464 // Setup marker strings as needed.
466 mLeft
.SetupString(mBlock
);
469 mRight
.SetupString(mBlock
);
472 // If there is insufficient space for both markers then keep the one on the
473 // end side per the block's 'direction'.
474 nscoord rightMarkerWidth
= mRight
.mActive
? mRight
.mWidth
: 0;
475 nscoord leftMarkerWidth
= mLeft
.mActive
? mLeft
.mWidth
: 0;
476 if (leftMarkerWidth
&& rightMarkerWidth
&&
477 leftMarkerWidth
+ rightMarkerWidth
> contentArea
.width
) {
479 rightMarkerWidth
= 0;
485 // Calculate the area between the potential markers aligned at the
487 nsRect insideMarkersArea
= mContentArea
;
489 InflateLeft(&insideMarkersArea
, -leftMarkerWidth
);
492 InflateRight(&insideMarkersArea
, -rightMarkerWidth
);
495 // Analyze the frames on aLine for the overflow situation at the content
496 // edges and at the edges of the area between the markers.
497 bool foundVisibleTextOrAtomic
= false;
498 int32_t n
= aLine
->GetChildCount();
499 nsIFrame
* child
= aLine
->mFirstChild
;
500 InnerClipEdges clippedMarkerEdges
;
501 for (; n
-- > 0; child
= child
->GetNextSibling()) {
502 ExamineFrameSubtree(child
, contentArea
, insideMarkersArea
,
503 aFramesToHide
, aAlignmentEdges
,
504 &foundVisibleTextOrAtomic
,
505 &clippedMarkerEdges
);
507 if (!foundVisibleTextOrAtomic
&& retryEmptyLine
) {
508 aAlignmentEdges
->mAssigned
= false;
509 aFramesToHide
->Clear();
511 if (mLeft
.IsNeeded() && mLeft
.mActive
&& !clippedLeftMarker
) {
512 if (clippedMarkerEdges
.mAssignedLeft
&&
513 clippedMarkerEdges
.mLeft
- mContentArea
.X() > 0) {
514 mLeft
.mWidth
= clippedMarkerEdges
.mLeft
- mContentArea
.X();
515 NS_ASSERTION(mLeft
.mWidth
< mLeft
.mIntrinsicWidth
,
516 "clipping a marker should make it strictly smaller");
517 clippedLeftMarker
= true;
519 mLeft
.mActive
= guessLeft
= false;
523 if (mRight
.IsNeeded() && mRight
.mActive
&& !clippedRightMarker
) {
524 if (clippedMarkerEdges
.mAssignedRight
&&
525 mContentArea
.XMost() - clippedMarkerEdges
.mRight
> 0) {
526 mRight
.mWidth
= mContentArea
.XMost() - clippedMarkerEdges
.mRight
;
527 NS_ASSERTION(mRight
.mWidth
< mRight
.mIntrinsicWidth
,
528 "clipping a marker should make it strictly smaller");
529 clippedRightMarker
= true;
531 mRight
.mActive
= guessRight
= false;
535 // The line simply has no visible content even without markers,
536 // so examine the line again without suppressing markers.
537 retryEmptyLine
= false;
538 mLeft
.mWidth
= mLeft
.mIntrinsicWidth
;
539 mLeft
.mActive
= guessLeft
= leftOverflow
;
540 mRight
.mWidth
= mRight
.mIntrinsicWidth
;
541 mRight
.mActive
= guessRight
= rightOverflow
;
544 if (guessLeft
== (mLeft
.mActive
&& mLeft
.IsNeeded()) &&
545 guessRight
== (mRight
.mActive
&& mRight
.IsNeeded())) {
548 guessLeft
= mLeft
.mActive
&& mLeft
.IsNeeded();
549 guessRight
= mRight
.mActive
&& mRight
.IsNeeded();
552 aFramesToHide
->Clear();
554 NS_ASSERTION(pass
== 0, "2nd pass should never guess wrong");
555 } while (++pass
!= 2);
556 if (!leftOverflow
|| !mLeft
.mActive
) {
559 if (!rightOverflow
|| !mRight
.mActive
) {
565 TextOverflow::ProcessLine(const nsDisplayListSet
& aLists
,
568 NS_ASSERTION(mLeft
.mStyle
->mType
!= NS_STYLE_TEXT_OVERFLOW_CLIP
||
569 mRight
.mStyle
->mType
!= NS_STYLE_TEXT_OVERFLOW_CLIP
,
570 "TextOverflow with 'clip' for both sides");
572 mLeft
.mActive
= mLeft
.mStyle
->mType
!= NS_STYLE_TEXT_OVERFLOW_CLIP
;
574 mRight
.mActive
= mRight
.mStyle
->mType
!= NS_STYLE_TEXT_OVERFLOW_CLIP
;
576 FrameHashtable
framesToHide(100);
577 AlignmentEdges alignmentEdges
;
578 ExamineLineFrames(aLine
, &framesToHide
, &alignmentEdges
);
579 bool needLeft
= mLeft
.IsNeeded();
580 bool needRight
= mRight
.IsNeeded();
581 if (!needLeft
&& !needRight
) {
584 NS_ASSERTION(mLeft
.mStyle
->mType
!= NS_STYLE_TEXT_OVERFLOW_CLIP
||
585 !needLeft
, "left marker for 'clip'");
586 NS_ASSERTION(mRight
.mStyle
->mType
!= NS_STYLE_TEXT_OVERFLOW_CLIP
||
587 !needRight
, "right marker for 'clip'");
589 // If there is insufficient space for both markers then keep the one on the
590 // end side per the block's 'direction'.
591 if (needLeft
&& needRight
&&
592 mLeft
.mWidth
+ mRight
.mWidth
> mContentArea
.width
) {
599 nsRect insideMarkersArea
= mContentArea
;
601 InflateLeft(&insideMarkersArea
, -mLeft
.mWidth
);
604 InflateRight(&insideMarkersArea
, -mRight
.mWidth
);
606 if (!mCanHaveHorizontalScrollbar
&& alignmentEdges
.mAssigned
) {
607 nsRect alignmentRect
= nsRect(alignmentEdges
.x
, insideMarkersArea
.y
,
608 alignmentEdges
.Width(), 1);
609 insideMarkersArea
.IntersectRect(insideMarkersArea
, alignmentRect
);
612 // Clip and remove display items as needed at the final marker edges.
613 nsDisplayList
* lists
[] = { aLists
.Content(), aLists
.PositionedDescendants() };
614 for (uint32_t i
= 0; i
< ArrayLength(lists
); ++i
) {
615 PruneDisplayListContents(lists
[i
], framesToHide
, insideMarkersArea
);
617 CreateMarkers(aLine
, needLeft
, needRight
, insideMarkersArea
);
621 TextOverflow::PruneDisplayListContents(nsDisplayList
* aList
,
622 const FrameHashtable
& aFramesToHide
,
623 const nsRect
& aInsideMarkersArea
)
627 while ((item
= aList
->RemoveBottom())) {
628 nsIFrame
* itemFrame
= item
->Frame();
629 if (IsFrameDescendantOfAny(itemFrame
, aFramesToHide
, mBlock
)) {
630 item
->~nsDisplayItem();
634 nsDisplayList
* wrapper
= item
->GetSameCoordinateSystemChildren();
636 if (!itemFrame
|| GetSelfOrNearestBlock(itemFrame
) == mBlock
) {
637 PruneDisplayListContents(wrapper
, aFramesToHide
, aInsideMarkersArea
);
641 nsCharClipDisplayItem
* charClip
= itemFrame
?
642 nsCharClipDisplayItem::CheckCast(item
) : nullptr;
643 if (charClip
&& GetSelfOrNearestBlock(itemFrame
) == mBlock
) {
644 nsRect rect
= itemFrame
->GetScrollableOverflowRect() +
645 itemFrame
->GetOffsetTo(mBlock
);
646 if (mLeft
.IsNeeded() && rect
.x
< aInsideMarkersArea
.x
) {
647 nscoord left
= aInsideMarkersArea
.x
- rect
.x
;
648 if (MOZ_UNLIKELY(left
< 0)) {
649 item
->~nsDisplayItem();
652 charClip
->mLeftEdge
= left
;
654 if (mRight
.IsNeeded() && rect
.XMost() > aInsideMarkersArea
.XMost()) {
655 nscoord right
= rect
.XMost() - aInsideMarkersArea
.XMost();
656 if (MOZ_UNLIKELY(right
< 0)) {
657 item
->~nsDisplayItem();
660 charClip
->mRightEdge
= right
;
664 saved
.AppendToTop(item
);
666 aList
->AppendToTop(&saved
);
670 TextOverflow::CanHaveTextOverflow(nsDisplayListBuilder
* aBuilder
,
671 nsIFrame
* aBlockFrame
)
673 const nsStyleTextReset
* style
= aBlockFrame
->StyleTextReset();
674 // Nothing to do for text-overflow:clip or if 'overflow-x:visible'
675 // or if we're just building items for event processing.
676 if ((style
->mTextOverflow
.mLeft
.mType
== NS_STYLE_TEXT_OVERFLOW_CLIP
&&
677 style
->mTextOverflow
.mRight
.mType
== NS_STYLE_TEXT_OVERFLOW_CLIP
) ||
678 IsHorizontalOverflowVisible(aBlockFrame
) ||
679 aBuilder
->IsForEventDelivery()) {
683 // Inhibit the markers if a descendant content owns the caret.
684 nsRefPtr
<nsCaret
> caret
= aBlockFrame
->PresContext()->PresShell()->GetCaret();
685 bool visible
= false;
686 if (caret
&& NS_SUCCEEDED(caret
->GetCaretVisible(&visible
)) && visible
) {
687 nsCOMPtr
<nsISelection
> domSelection
= caret
->GetCaretDOMSelection();
689 nsCOMPtr
<nsIDOMNode
> node
;
690 domSelection
->GetFocusNode(getter_AddRefs(node
));
691 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(node
);
692 if (content
&& nsContentUtils::ContentIsDescendantOf(content
,
693 aBlockFrame
->GetContent())) {
702 TextOverflow::CreateMarkers(const nsLineBox
* aLine
,
705 const nsRect
& aInsideMarkersArea
)
708 DisplayListClipState::AutoSaveRestore
clipState(mBuilder
);
710 nsRect markerRect
= nsRect(aInsideMarkersArea
.x
- mLeft
.mIntrinsicWidth
,
712 mLeft
.mIntrinsicWidth
, aLine
->mBounds
.height
);
713 markerRect
+= mBuilder
->ToReferenceFrame(mBlock
);
714 ClipMarker(mContentArea
+ mBuilder
->ToReferenceFrame(mBlock
),
715 markerRect
, clipState
);
716 nsDisplayItem
* marker
= new (mBuilder
)
717 nsDisplayTextOverflowMarker(mBuilder
, mBlock
, markerRect
,
718 aLine
->GetAscent(), mLeft
.mStyle
, 0);
719 mMarkerList
.AppendNewToTop(marker
);
723 DisplayListClipState::AutoSaveRestore
clipState(mBuilder
);
725 nsRect markerRect
= nsRect(aInsideMarkersArea
.XMost(),
727 mRight
.mIntrinsicWidth
, aLine
->mBounds
.height
);
728 markerRect
+= mBuilder
->ToReferenceFrame(mBlock
);
729 ClipMarker(mContentArea
+ mBuilder
->ToReferenceFrame(mBlock
),
730 markerRect
, clipState
);
731 nsDisplayItem
* marker
= new (mBuilder
)
732 nsDisplayTextOverflowMarker(mBuilder
, mBlock
, markerRect
,
733 aLine
->GetAscent(), mRight
.mStyle
, 1);
734 mMarkerList
.AppendNewToTop(marker
);
739 TextOverflow::Marker::SetupString(nsIFrame
* aFrame
)
745 if (mStyle
->mType
== NS_STYLE_TEXT_OVERFLOW_ELLIPSIS
) {
746 gfxTextRun
* textRun
= GetEllipsisTextRun(aFrame
);
748 mWidth
= textRun
->GetAdvanceWidth(0, textRun
->GetLength(), nullptr);
753 nsRefPtr
<nsRenderingContext
> rc
=
754 aFrame
->PresContext()->PresShell()->GetReferenceRenderingContext();
755 nsRefPtr
<nsFontMetrics
> fm
;
756 nsLayoutUtils::GetFontMetricsForFrame(aFrame
, getter_AddRefs(fm
),
757 nsLayoutUtils::FontSizeInflationFor(aFrame
));
759 mWidth
= nsLayoutUtils::GetStringWidth(aFrame
, rc
, mStyle
->mString
.get(),
760 mStyle
->mString
.Length());
762 mIntrinsicWidth
= mWidth
;
767 } // namespace mozilla