Merge m-c to fx-team.
[gecko.git] / layout / generic / TextOverflow.cpp
blobddb43c15cccdacbb526e82da401141949ab779bf
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"
8 #include <algorithm>
10 // Please maintain alphabetical order below
11 #include "nsBlockFrame.h"
12 #include "nsCaret.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"
19 #include "nsRect.h"
20 #include "nsRenderingContext.h"
21 #include "nsTextFrame.h"
22 #include "nsIFrameInlines.h"
23 #include "mozilla/Util.h"
24 #include "mozilla/Likely.h"
26 namespace mozilla {
27 namespace css {
29 class LazyReferenceRenderingContextGetterFromFrame MOZ_FINAL :
30 public gfxFontGroup::LazyReferenceContextGetter {
31 public:
32 LazyReferenceRenderingContextGetterFromFrame(nsIFrame* aFrame)
33 : mFrame(aFrame) {}
34 virtual already_AddRefed<gfxContext> GetRefContext() MOZ_OVERRIDE
36 nsRefPtr<nsRenderingContext> rc =
37 mFrame->PresContext()->PresShell()->GetReferenceRenderingContext();
38 nsRefPtr<gfxContext> ctx = rc->ThebesContext();
39 return ctx.forget();
41 private:
42 nsIFrame* mFrame;
45 static gfxTextRun*
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);
56 static nsIFrame*
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.
66 static bool
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);
77 static bool
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) {
84 return false;
86 return !aFrame->MeasureCharClippedText(aLeft, aRight,
87 aSnappedLeft, aSnappedRight);
90 static bool
91 IsHorizontalOverflowVisible(nsIFrame* aFrame)
93 NS_PRECONDITION(nsLayoutUtils::GetAsBlock(aFrame) != nullptr,
94 "expected a block frame");
96 nsIFrame* f = aFrame;
97 while (f && f->StyleContext()->GetPseudo() &&
98 f->GetType() != nsGkAtoms::scrollFrame) {
99 f = f->GetParent();
101 return !f || f->StyleDisplay()->mOverflowX == NS_STYLE_OVERFLOW_VISIBLE;
104 static void
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);
115 } else {
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);
126 static void
127 InflateLeft(nsRect* aRect, nscoord aDelta)
129 nscoord xmost = aRect->XMost();
130 aRect->x -= aDelta;
131 aRect->width = std::max(xmost - aRect->x, 0);
134 static void
135 InflateRight(nsRect* aRect, nscoord aDelta)
137 aRect->width = std::max(aRect->width + aDelta, 0);
140 static bool
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)) {
148 return true;
151 return false;
154 class nsDisplayTextOverflowMarker : public nsDisplayItem
156 public:
157 nsDisplayTextOverflowMarker(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
158 const nsRect& aRect, nscoord aAscent,
159 const nsStyleTextOverflowSide* aStyle,
160 uint32_t aIndex)
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);
169 #endif
170 virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) {
171 *aSnap = false;
172 nsRect shadowRect =
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)
185 private:
186 nsRect mRect; // in reference frame coordinates
187 const nsStyleTextOverflowSide* mStyle;
188 nscoord mAscent; // baseline for the marker text in mRect
189 uint32_t mIndex;
192 static void
193 PaintTextShadowCallback(nsRenderingContext* aCtx,
194 nsPoint aShadowOffset,
195 const nscolor& aShadowColor,
196 void* aData)
198 reinterpret_cast<nsDisplayTextOverflowMarker*>(aData)->
199 PaintTextToContext(aCtx, aShadowOffset);
202 void
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,
212 (void*)this);
213 aCtx->SetColor(foregroundColor);
214 PaintTextToContext(aCtx, nsPoint(0, 0));
217 void
218 nsDisplayTextOverflowMarker::PaintTextToContext(nsRenderingContext* aCtx,
219 nsPoint aOffsetFromRect)
221 gfxFloat y = nsLayoutUtils::GetSnappedBaselineY(mFrame, aCtx->ThebesContext(),
222 mRect.y, mAscent);
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);
228 if (textRun) {
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);
235 } else {
236 nsRefPtr<nsFontMetrics> fm;
237 nsLayoutUtils::GetFontMetricsForFrame(mFrame, getter_AddRefs(fm),
238 nsLayoutUtils::FontSizeInflationFor(mFrame));
239 aCtx->SetFont(fm);
240 nsLayoutUtils::DrawString(mFrame, aCtx, mStyle->mString.get(),
241 mStyle->mString.Length(), pt);
245 void
246 TextOverflow::Init(nsDisplayListBuilder* aBuilder,
247 nsIFrame* aBlockFrame)
249 mBuilder = aBuilder;
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;
256 #ifdef MOZ_XUL
257 if (!mScrollableFrame) {
258 nsIAtom* pseudoType = aBlockFrame->StyleContext()->GetPseudo();
259 if (pseudoType == nsCSSAnonBoxes::mozXULAnonymousBlock) {
260 mScrollableFrame =
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;
269 #endif
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)) {
295 return nullptr;
297 nsAutoPtr<TextOverflow> textOverflow(new TextOverflow);
298 textOverflow->Init(aBuilder, aBlockFrame);
299 return textOverflow.forget();
302 void
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) {
314 return;
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();
322 if (overflowLeft) {
323 mLeft.mHasOverflow = true;
325 if (overflowRight) {
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);
338 if (isAtomic) {
339 return;
342 nsIFrame* child = aFrame->GetFirstPrincipalChild();
343 while (child) {
344 ExamineFrameSubtree(child, aContentArea, aInsideMarkersArea,
345 aFramesToHide, aAlignmentEdges,
346 aFoundVisibleTextOrAtomic,
347 aClippedMarkerEdges);
348 child = child->GetNextSibling();
352 void
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) {
372 leftOverlap = 0;
375 if (rightOverlap > 0) {
376 aClippedMarkerEdges->AccumulateRight(borderRect);
377 if (!mRight.mActive) {
378 rightOverlap = 0;
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;
404 } else {
405 aFramesToHide->PutEntry(aFrame);
407 } else if (!insideLeftEdge || !insideRightEdge) {
408 // frame is outside
409 if (IsAtomicElement(aFrame, aFrameType)) {
410 aFramesToHide->PutEntry(aFrame);
412 } else {
413 // frame is inside
414 aAlignmentEdges->Accumulate(borderRect);
415 *aFoundVisibleTextOrAtomic = true;
419 void
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) {
433 suppressLeft = true;
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.
452 return;
455 int pass = 0;
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;
463 do {
464 // Setup marker strings as needed.
465 if (guessLeft) {
466 mLeft.SetupString(mBlock);
468 if (guessRight) {
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) {
478 if (mBlockIsRTL) {
479 rightMarkerWidth = 0;
480 } else {
481 leftMarkerWidth = 0;
485 // Calculate the area between the potential markers aligned at the
486 // block's edge.
487 nsRect insideMarkersArea = mContentArea;
488 if (guessLeft) {
489 InflateLeft(&insideMarkersArea, -leftMarkerWidth);
491 if (guessRight) {
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();
510 pass = -1;
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;
518 } else {
519 mLeft.mActive = guessLeft = false;
521 continue;
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;
530 } else {
531 mRight.mActive = guessRight = false;
533 continue;
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;
542 continue;
544 if (guessLeft == (mLeft.mActive && mLeft.IsNeeded()) &&
545 guessRight == (mRight.mActive && mRight.IsNeeded())) {
546 break;
547 } else {
548 guessLeft = mLeft.mActive && mLeft.IsNeeded();
549 guessRight = mRight.mActive && mRight.IsNeeded();
550 mLeft.Reset();
551 mRight.Reset();
552 aFramesToHide->Clear();
554 NS_ASSERTION(pass == 0, "2nd pass should never guess wrong");
555 } while (++pass != 2);
556 if (!leftOverflow || !mLeft.mActive) {
557 mLeft.Reset();
559 if (!rightOverflow || !mRight.mActive) {
560 mRight.Reset();
564 void
565 TextOverflow::ProcessLine(const nsDisplayListSet& aLists,
566 nsLineBox* aLine)
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");
571 mLeft.Reset();
572 mLeft.mActive = mLeft.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP;
573 mRight.Reset();
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) {
582 return;
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) {
593 if (mBlockIsRTL) {
594 needRight = false;
595 } else {
596 needLeft = false;
599 nsRect insideMarkersArea = mContentArea;
600 if (needLeft) {
601 InflateLeft(&insideMarkersArea, -mLeft.mWidth);
603 if (needRight) {
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);
620 void
621 TextOverflow::PruneDisplayListContents(nsDisplayList* aList,
622 const FrameHashtable& aFramesToHide,
623 const nsRect& aInsideMarkersArea)
625 nsDisplayList saved;
626 nsDisplayItem* item;
627 while ((item = aList->RemoveBottom())) {
628 nsIFrame* itemFrame = item->Frame();
629 if (IsFrameDescendantOfAny(itemFrame, aFramesToHide, mBlock)) {
630 item->~nsDisplayItem();
631 continue;
634 nsDisplayList* wrapper = item->GetSameCoordinateSystemChildren();
635 if (wrapper) {
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();
650 continue;
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();
658 continue;
660 charClip->mRightEdge = right;
664 saved.AppendToTop(item);
666 aList->AppendToTop(&saved);
669 /* static */ bool
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()) {
680 return false;
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();
688 if (domSelection) {
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())) {
694 return false;
698 return true;
701 void
702 TextOverflow::CreateMarkers(const nsLineBox* aLine,
703 bool aCreateLeft,
704 bool aCreateRight,
705 const nsRect& aInsideMarkersArea)
707 if (aCreateLeft) {
708 DisplayListClipState::AutoSaveRestore clipState(mBuilder);
710 nsRect markerRect = nsRect(aInsideMarkersArea.x - mLeft.mIntrinsicWidth,
711 aLine->mBounds.y,
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);
722 if (aCreateRight) {
723 DisplayListClipState::AutoSaveRestore clipState(mBuilder);
725 nsRect markerRect = nsRect(aInsideMarkersArea.XMost(),
726 aLine->mBounds.y,
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);
738 void
739 TextOverflow::Marker::SetupString(nsIFrame* aFrame)
741 if (mInitialized) {
742 return;
745 if (mStyle->mType == NS_STYLE_TEXT_OVERFLOW_ELLIPSIS) {
746 gfxTextRun* textRun = GetEllipsisTextRun(aFrame);
747 if (textRun) {
748 mWidth = textRun->GetAdvanceWidth(0, textRun->GetLength(), nullptr);
749 } else {
750 mWidth = 0;
752 } else {
753 nsRefPtr<nsRenderingContext> rc =
754 aFrame->PresContext()->PresShell()->GetReferenceRenderingContext();
755 nsRefPtr<nsFontMetrics> fm;
756 nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm),
757 nsLayoutUtils::FontSizeInflationFor(aFrame));
758 rc->SetFont(fm);
759 mWidth = nsLayoutUtils::GetStringWidth(aFrame, rc, mStyle->mString.get(),
760 mStyle->mString.Length());
762 mIntrinsicWidth = mWidth;
763 mInitialized = true;
766 } // namespace css
767 } // namespace mozilla