Files
palemoon27/layout/generic/TextOverflow.cpp
T
roytam1 8a0a002cf2 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1148708: Add missing 'override' annotations in DocAccessibleChild.h. rs=ehsan (d606358545)
- Bug 1210408 - make nsMaiInterfaceAction work with proxies, r=tbsaunde (f7c819c6ae)
- Bug 1210407 - teach nsMaiInterfaceTable to use proxies, r=tbsaunde (4ca4f10b5f)
- bug 1185157 make sure we don't send an event to a destroyed ipc document r=billm (23acf53f75)
- bug 1214864 - make SetCarretOffset() async r=davidb (e3079e9b2d)
- missing of Bug 1139972 - IPC Proxy for charAt, r=tbsaunde (e9593ed752)
- bug 1191598 - Pass MOZ_CURRENT_PROJECT in environment when running post-build automation steps for universal mac builds. r=gps (fc342c6ced)
- Bug 1164596 - Add mach android-emulator command; r=ahal (afeb9b27d1)
- Bug 1223149 - Add basic usage documentation for mach build; r=glandium (bfb802d175)
- Bug 1182301 - Improve 'mach build' notifications. r=gps (2c65a122d1)
- Bug 1184696 - Add clobber targets to |mach clobber|; Ability to clobber compiled python files, r=gps (35d8be292e)
- Bug 1117958 - Allow any debugging options to the run or gtest mach subcommands to automatically enable debugging. r=gps (32f986af4b)
- Bug 1180081 - Properly rebuild gtest/libxul before running gtests. r=gps (80db9a3d49)
- Bug 1171647, part 1 - Define a new function to convert the mode to a string. r=njn (61ad16f5ba)
- Bug 1171647, part 2 - Remove redundant assertion for dark matter mode. r=njn (b5ac9519f3)
- Bug 1058178, part 1 - Implement DMD heap scanning mode. r=njn (60e1079536)
- Bug 1058178, part 2 - Implement address clamping analysis for DMD scan logs. r=njn (45c0326b93)
- Bug 1102388 - Fix DMD static constructor ordering dependency. r=mccr8 (59b87897a1)
- Bug 1128705 - Don't redefine PAGE_SIZE in DMD if it's already defined. r=erahm (49216348ee)
- Bug 1179042 - Add a script for analyzing memory blocks using a heap scan DMD log. r=njn DONTBUILD (1c08d2d66e)
- Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat (1c999d139b)
- Bug 1158772 - fix non-idiomatic memset call in nsDeque.cpp; r=erahm (de6b555245)
- Bug 1199400 - Part 1: Use CheckedInt when growing nsDeque capacity. r=froydnj (dfdf6814a3)
- Bug 1199400 - Part 0: Remove unused nsDequeIterator. r=froydnj (38d69d7f47)
- Bug 1199400 - Part 2: Add tests for possible nsDeque corner cases. r=froydnj (931baff195)
- Bug 1201997 - Part 1 - Converted compiled test to gtest for nsDeque class. Added tests to test untested methods. r=froydn (e893916651)
- Bug 1201997 - Part 2 - Removing unused methods from the nsDeque class. r=froydn (41595a90ac)
- Bug 1201997 - Part 3 - Make internally used methods private. r=froydn (8cd3afd96f)
- Bug 1201997 - Part 4 - Change size and offset variables to size_t.r=froydn (73eabc8d60)
- Bug 1215140 P1 Add an nsIConsoleReportCollector interface to support navigation channel logging. r=bz (8a41535e2b)
- Bug 1215140 P2 Make HttpBaseChannel implement nsIConsoleReportCollector. r=bz (75fca301f2)
- Bug 1197679 - If nsUnknownDecoder is involved in e10s DivertToParent can break. r=jduell (5d94a12504)
- Bug 1178991 - smartptr for http converter r=hurley (8e7fbc8443)
- bug 366559 - patch 2, fix nsHTTPCompressConv indentation r=bagder (ba762da587)
- bug 366559 - patch 3, fix nsHTTPCompressConv bracing style r=bagder (54195ab451)
- bits of  bug 366559 - patch 7, content-encoding brotli for http (f0b4051022)
- Bug 1205112 - Make PushEvent.data nullable. r=mt,smaug (775db32856)
- Bug 1193414 - SharedWorkers thread should be kept alive also when the SharedWorker object is CCed, r=khuey (b77ea8125c)
- Bug 1206520: Add about:config prefs to enable throwing on asm.js validation failures; r=bz (c42126665d)
- Bug 1193414 - Telemetry for SharedWorker spawning. r=bkelly (77984b7bcc)
- Bug 1205676 - Enable WPT service-worker/unregister-then-register-new-script.https.html in e10s, r=nsm (ec24939cf6)
- Bug 1193133 - Throw when calling postMessage from a Service Worker dom object with no global. r=bkelly (526dcacfab)
- Bug 1181871 P1 Only enforce Cache Context shared data destruction on target thread after init. r=ehsan (cdbf3ed3a8)
- Bug 1181871 P2 Fix ServiceWorkerManager usage of stack-based ErrorResult. r=ehsan (c449195d90)
- minor cleanup and missing bit of 1198230 (02f459db05)
- Bug 1143717 - Implement the ServiceWorkerMessageEvent interface. r=baku (027b3465f2)
- fix misspatch (708eee4e84)
- Bug 1188545 - Disentangle service workers from shared workers and refactor event dispatching code into a separate class. r=nsm,mrbkap (fb5b5341c9)
- Bug 1205228 - Change PackagedAppVerifier to notify the verification result asynchronously. r=valentin. (9edda0fa00)
- Bug 1178518 - Packaged App Utils. r=valentin (f60f3b7a93)
- Bug 1213150 - Part 1: Add a nsContentUtils::IsNonSubresourceRequest helper; r=jdm (b509cc3cc9)
- Bug 1213150 - Part 2: Rework ShouldPrepareForIntercept() in terms of subresource requests; r=jdm (2e92fe8780)
- Bug 1213150 - Part 3: Remove nsIInterceptedChannel.isNavigation; r=jdm (becf1cc12f)
- Bug 1213150 follow-up: fix build bustage (8d73d6ca73)
- Bug 1198394 - Part 1: Allow interception of HSTS upgraded connections in non-e10s mode; r=mcmanus (f504c5be08)
- Bug 1198394 - Part 2: Add a test for interception of HSTS upgraded connections; r=jdm (054e984eef)
- Bug 1187011 - Don't allow response body with null body status. r=bkelly (b1860741d1)
- missing bit of 1140788 (29d319712e)
- Bug 1213436 - Reject core dumps with node IDs that don't fit in an IEEE 754 double; r=sfink (3c1f6fdda0)
- Bug 1211006 - Add Debugger.Source.prototype.canonicalId; r=ejpbruel (eef7b79fce)
- Bug 1199218 - Implement JS::ubi::Node::size for js::LazyScript referents; r=sfink (098a48d240)
- Bug 1220031 - Add JS::ubi::Node::scriptFilename; r=sfink (6b824ae680)
- Bug 1143575. Remove unused MediaQueue::Empty. r=cpearce (de737f3433)
- Bug 1209933 - Make sure all parent runtime pointers are the topmost parent, r=billm. (fe824d967d)
- Bug 1197012 - Fix ThrowTypeError in Notification. r=mccr8 (0b1a097526)
- Bug 1197893 - Check the number of arguments for ThrowTypeError() and ThrowRangeError() at compile time. r=peterv (d98c7d78a0)
- Bug 1142083 - Add test for IDN Unicode domain redirect. r=mcmanus (0c8961fe17)
- Bug 1187159 - Add mochitest for loading packaged apps (iframe+fetch+mozapp) r=jduell (ce90ea561b)
- Bug 1186290 - Notify TabParent to switch process when loading a signed package. r=honzab, r=kanru. (c58a14554a)
- fix (15e2df75eb)
- Bug 1206124 P1 Fix "same-origin" CORS credentials in FetchDriver. r=ehsan (fae1bb6ab3)
- Bug 1206124 P2 Test fetch() with credentials and redirects. r=ehsan (ffc6254112)
- Bug 1211751: Remove nsIChannelEventSink-forwarding from EventSource and FetchDriver. It's never needed. r=smaug (adafe5737a)
- Bug 1212433 Fail fetch() calls that require preflight and also redirect. r=sicking a=abillings (c0d6742b9e)
- Bug 1193128 - Fix base64 decoding when fetching data URIs. r=baku (80bafa291a)
- Bug 1195167 part 1: Let necko handle all protocols. r=bkelly (bb932b0ada)
- Bug 1195167 part 2: Remove redundant aCORSFlag argument and instead use mCORSFlagEverSet. r=bkelly (beadafcad0)
- Bug 1195167 part 3: Remove more scheme-specific handling from FetchDriver. r=bkelly (d00b38db9e)
- Bug 1195167 part 4: Remove FetchDriver::BasicFetch since it is empty. r=bkelly (c5ed097267)
- Bug 1210413 P2 Test CORS credentials on cross-origin redirects. r=sicking a=dveditz (b4eeb8aac0)
- Bug 1210413 P1 Propagate new channel load flags from child to parent on redirect. r=jduell a=dveditz (8b329af4fa)
- Bug 1195167 part 5: Make FetchDriver use AsyncOpen2. r=bkelly (cc217c4cc1)
- Bug 1195167 part 6: Some code simplification since necko handles fetch recursion. r=bkelly (f3b6da2262)
- Bug 1195167: Followup to fix test which I forgot to change (81e7439a2e)
- Bug 1215746: Remove RequestMode::Cors_with_forced_preflight. r=bkelly (0336e812b6)
- Bug 1211000: Move CORS preflight logic from nsCORSListenerProxy to nsCORSPreflightListener. r=ehsan (bf2f71cf22)
- missing bit of Bug 1211443 - Drop scheduled update if decoder initialization isn't done yet. r=jya (f6bc074e33)
- Bug 1182571: Fix nsILoadInfo->GetContentPolicyType API to be less ambigious. Audit and fix all users of it. r=ckerschb (5af6fa7442)
- fix (e40c8e7625)
- Bug 1173811 - Part 1: Propagate the response URL to intercepted channels when necessary (non-e10s). r=mayhemer,bkelly (26f4f13c28)
- Bug 1173811 - Part 2: Propagate the response URL to intercepted channels when necessary (e10s). r=mayhemer,bkelly (a603fe1df2)
- Bug 1154309 - Add New Resource Timing Fields r=bz,hurley (1d14eb6bef)
- Bug 1175685 - add OriginAttribute to LoadInfo. r=jonas, r=ckerschb, r=michal (a5d18bb637)
- Bug 1175685 - add OriginAttribute to LoadInfo. r=jonas, r=ckerschb, r=michal (fb07d2c8aa)
- Bug 1212904 P1 Add a LoadTainting enumeration. r=jduell (a1db8a3e99)
- Bug 1212904 P2 Add LoadTainting information to nsILoadInfo. r=jduell (2482e5e334)
- Bug 1221151 - use [infallible] in nsILoadInfo.idl instead of manual %{C++ blocks; r=jduell (aae73129b6)
- Bug 1045891 - CSP 2 child-src implementation r=ckerschb (792920aeb9)
- Bug 1219931 - CSP: Don't allow removing a policy (r=sicking) (9daaab4186)
- Bug 1208661 - Dump client-side layer textures. r=BenWa (1f2d17d515)
2022-10-17 11:08:37 +08:00

822 lines
30 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "TextOverflow.h"
#include <algorithm>
// Please maintain alphabetical order below
#include "nsBlockFrame.h"
#include "nsCaret.h"
#include "nsContentUtils.h"
#include "nsCSSAnonBoxes.h"
#include "nsFontMetrics.h"
#include "nsGfxScrollFrame.h"
#include "nsIScrollableFrame.h"
#include "nsLayoutUtils.h"
#include "nsPresContext.h"
#include "nsRect.h"
#include "nsRenderingContext.h"
#include "nsTextFrame.h"
#include "nsIFrameInlines.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Likely.h"
#include "nsISelection.h"
namespace mozilla {
namespace css {
class LazyReferenceRenderingContextGetterFromFrame final :
public gfxFontGroup::LazyReferenceContextGetter {
public:
explicit LazyReferenceRenderingContextGetterFromFrame(nsIFrame* aFrame)
: mFrame(aFrame) {}
virtual already_AddRefed<gfxContext> GetRefContext() override
{
return mFrame->PresContext()->PresShell()->CreateReferenceRenderingContext();
}
private:
nsIFrame* mFrame;
};
static gfxTextRun*
GetEllipsisTextRun(nsIFrame* aFrame)
{
RefPtr<nsFontMetrics> fm;
nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm),
nsLayoutUtils::FontSizeInflationFor(aFrame));
LazyReferenceRenderingContextGetterFromFrame lazyRefContextGetter(aFrame);
return fm->GetThebesFontGroup()->GetEllipsisTextRun(
aFrame->PresContext()->AppUnitsPerDevPixel(),
nsLayoutUtils::GetTextRunOrientFlagsForStyle(aFrame->StyleContext()),
lazyRefContextGetter);
}
static nsIFrame*
GetSelfOrNearestBlock(nsIFrame* aFrame)
{
return nsLayoutUtils::GetAsBlock(aFrame) ? aFrame :
nsLayoutUtils::FindNearestBlockAncestor(aFrame);
}
// Return true if the frame is an atomic inline-level element.
// It's not supposed to be called for block frames since we never
// process block descendants for text-overflow.
static bool
IsAtomicElement(nsIFrame* aFrame, const nsIAtom* aFrameType)
{
NS_PRECONDITION(!nsLayoutUtils::GetAsBlock(aFrame) ||
!aFrame->IsBlockOutside(),
"unexpected block frame");
NS_PRECONDITION(aFrameType != nsGkAtoms::placeholderFrame,
"unexpected placeholder frame");
return !aFrame->IsFrameOfType(nsIFrame::eLineParticipant);
}
static bool
IsFullyClipped(nsTextFrame* aFrame, nscoord aLeft, nscoord aRight,
nscoord* aSnappedLeft, nscoord* aSnappedRight)
{
*aSnappedLeft = aLeft;
*aSnappedRight = aRight;
if (aLeft <= 0 && aRight <= 0) {
return false;
}
return !aFrame->MeasureCharClippedText(aLeft, aRight,
aSnappedLeft, aSnappedRight);
}
static bool
IsInlineAxisOverflowVisible(nsIFrame* aFrame)
{
NS_PRECONDITION(nsLayoutUtils::GetAsBlock(aFrame) != nullptr,
"expected a block frame");
nsIFrame* f = aFrame;
while (f && f->StyleContext()->GetPseudo() &&
f->GetType() != nsGkAtoms::scrollFrame) {
f = f->GetParent();
}
if (!f) {
return true;
}
auto overflow = aFrame->GetWritingMode().IsVertical() ?
f->StyleDisplay()->mOverflowY : f->StyleDisplay()->mOverflowX;
return overflow == NS_STYLE_OVERFLOW_VISIBLE;
}
static void
ClipMarker(const nsRect& aContentArea,
const nsRect& aMarkerRect,
DisplayListClipState::AutoSaveRestore& aClipState)
{
nscoord rightOverflow = aMarkerRect.XMost() - aContentArea.XMost();
nsRect markerRect = aMarkerRect;
if (rightOverflow > 0) {
// Marker overflows on the right side (content width < marker width).
markerRect.width -= rightOverflow;
aClipState.ClipContentDescendants(markerRect);
} else {
nscoord leftOverflow = aContentArea.x - aMarkerRect.x;
if (leftOverflow > 0) {
// Marker overflows on the left side
markerRect.width -= leftOverflow;
markerRect.x += leftOverflow;
aClipState.ClipContentDescendants(markerRect);
}
}
}
static void
InflateIStart(WritingMode aWM, LogicalRect* aRect, nscoord aDelta)
{
nscoord iend = aRect->IEnd(aWM);
aRect->IStart(aWM) -= aDelta;
aRect->ISize(aWM) = std::max(iend - aRect->IStart(aWM), 0);
}
static void
InflateIEnd(WritingMode aWM, LogicalRect* aRect, nscoord aDelta)
{
aRect->ISize(aWM) = std::max(aRect->ISize(aWM) + aDelta, 0);
}
static bool
IsFrameDescendantOfAny(nsIFrame* aChild,
const TextOverflow::FrameHashtable& aSetOfFrames,
nsIFrame* aCommonAncestor)
{
for (nsIFrame* f = aChild; f && f != aCommonAncestor;
f = nsLayoutUtils::GetCrossDocParentFrame(f)) {
if (aSetOfFrames.GetEntry(f)) {
return true;
}
}
return false;
}
class nsDisplayTextOverflowMarker : public nsDisplayItem
{
public:
nsDisplayTextOverflowMarker(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
const nsRect& aRect, nscoord aAscent,
const nsStyleTextOverflowSide* aStyle,
uint32_t aIndex)
: nsDisplayItem(aBuilder, aFrame), mRect(aRect),
mStyle(aStyle), mAscent(aAscent), mIndex(aIndex) {
MOZ_COUNT_CTOR(nsDisplayTextOverflowMarker);
}
#ifdef NS_BUILD_REFCNT_LOGGING
virtual ~nsDisplayTextOverflowMarker() {
MOZ_COUNT_DTOR(nsDisplayTextOverflowMarker);
}
#endif
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
bool* aSnap) override {
*aSnap = false;
nsRect shadowRect =
nsLayoutUtils::GetTextShadowRectsUnion(mRect, mFrame);
return mRect.Union(shadowRect);
}
virtual void Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx) override;
virtual uint32_t GetPerFrameKey() override {
return (mIndex << nsDisplayItem::TYPE_BITS) | nsDisplayItem::GetPerFrameKey();
}
void PaintTextToContext(nsRenderingContext* aCtx,
nsPoint aOffsetFromRect);
NS_DISPLAY_DECL_NAME("TextOverflow", TYPE_TEXT_OVERFLOW)
private:
nsRect mRect; // in reference frame coordinates
const nsStyleTextOverflowSide* mStyle;
nscoord mAscent; // baseline for the marker text in mRect
uint32_t mIndex;
};
static void
PaintTextShadowCallback(nsRenderingContext* aCtx,
nsPoint aShadowOffset,
const nscolor& aShadowColor,
void* aData)
{
reinterpret_cast<nsDisplayTextOverflowMarker*>(aData)->
PaintTextToContext(aCtx, aShadowOffset);
}
void
nsDisplayTextOverflowMarker::Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx)
{
nscolor foregroundColor =
nsLayoutUtils::GetColor(mFrame, eCSSProperty_color);
// Paint the text-shadows for the overflow marker
nsLayoutUtils::PaintTextShadow(mFrame, aCtx, mRect, mVisibleRect,
foregroundColor, PaintTextShadowCallback,
(void*)this);
aCtx->ThebesContext()->SetColor(gfx::Color::FromABGR(foregroundColor));
PaintTextToContext(aCtx, nsPoint(0, 0));
}
void
nsDisplayTextOverflowMarker::PaintTextToContext(nsRenderingContext* aCtx,
nsPoint aOffsetFromRect)
{
WritingMode wm = mFrame->GetWritingMode();
nsPoint pt(mRect.x, mRect.y);
if (wm.IsVertical()) {
if (wm.IsVerticalLR()) {
pt.x = NSToCoordFloor(nsLayoutUtils::GetSnappedBaselineX(
mFrame, aCtx->ThebesContext(), pt.x, mAscent));
} else {
pt.x = NSToCoordFloor(nsLayoutUtils::GetSnappedBaselineX(
mFrame, aCtx->ThebesContext(), pt.x + mRect.width, -mAscent));
}
} else {
pt.y = NSToCoordFloor(nsLayoutUtils::GetSnappedBaselineY(
mFrame, aCtx->ThebesContext(), pt.y, mAscent));
}
pt += aOffsetFromRect;
if (mStyle->mType == NS_STYLE_TEXT_OVERFLOW_ELLIPSIS) {
gfxTextRun* textRun = GetEllipsisTextRun(mFrame);
if (textRun) {
NS_ASSERTION(!textRun->IsRightToLeft(),
"Ellipsis textruns should always be LTR!");
gfxPoint gfxPt(pt.x, pt.y);
textRun->Draw(aCtx->ThebesContext(), gfxPt, DrawMode::GLYPH_FILL,
0, textRun->GetLength(), nullptr, nullptr, nullptr);
}
} else {
RefPtr<nsFontMetrics> fm;
nsLayoutUtils::GetFontMetricsForFrame(mFrame, getter_AddRefs(fm),
nsLayoutUtils::FontSizeInflationFor(mFrame));
nsLayoutUtils::DrawString(mFrame, *fm, aCtx, mStyle->mString.get(),
mStyle->mString.Length(), pt);
}
}
TextOverflow::TextOverflow(nsDisplayListBuilder* aBuilder,
nsIFrame* aBlockFrame)
: mContentArea(aBlockFrame->GetWritingMode(),
aBlockFrame->GetContentRectRelativeToSelf(),
aBlockFrame->GetSize())
, mBuilder(aBuilder)
, mBlock(aBlockFrame)
, mScrollableFrame(nsLayoutUtils::GetScrollableFrameFor(aBlockFrame))
, mBlockSize(aBlockFrame->GetSize())
, mBlockWM(aBlockFrame->GetWritingMode())
, mAdjustForPixelSnapping(false)
{
#ifdef MOZ_XUL
if (!mScrollableFrame) {
nsIAtom* pseudoType = aBlockFrame->StyleContext()->GetPseudo();
if (pseudoType == nsCSSAnonBoxes::mozXULAnonymousBlock) {
mScrollableFrame =
nsLayoutUtils::GetScrollableFrameFor(aBlockFrame->GetParent());
// nsXULScrollFrame::ClampAndSetBounds rounds to nearest pixels
// for RTL blocks (also for overflow:hidden), so we need to move
// the edges 1px outward in ExamineLineFrames to avoid triggering
// a text-overflow marker in this case.
mAdjustForPixelSnapping = !mBlockWM.IsBidiLTR();
}
}
#endif
mCanHaveInlineAxisScrollbar = false;
if (mScrollableFrame) {
auto scrollbarStyle = mBlockWM.IsVertical() ?
mScrollableFrame->GetScrollbarStyles().mVertical :
mScrollableFrame->GetScrollbarStyles().mHorizontal;
mCanHaveInlineAxisScrollbar = scrollbarStyle != NS_STYLE_OVERFLOW_HIDDEN;
if (!mAdjustForPixelSnapping) {
// Scrolling to the end position can leave some text still overflowing due
// to pixel snapping behaviour in our scrolling code.
mAdjustForPixelSnapping = mCanHaveInlineAxisScrollbar;
}
// Use a null containerSize to convert a vector from logical to physical.
const nsSize nullContainerSize;
mContentArea.MoveBy(mBlockWM,
LogicalPoint(mBlockWM,
mScrollableFrame->GetScrollPosition(),
nullContainerSize));
nsIFrame* scrollFrame = do_QueryFrame(mScrollableFrame);
scrollFrame->AddStateBits(NS_SCROLLFRAME_INVALIDATE_CONTENTS_ON_SCROLL);
}
uint8_t direction = aBlockFrame->StyleVisibility()->mDirection;
const nsStyleTextReset* style = aBlockFrame->StyleTextReset();
if (mBlockWM.IsBidiLTR()) {
mIStart.Init(style->mTextOverflow.GetLeft(direction));
mIEnd.Init(style->mTextOverflow.GetRight(direction));
} else {
mIStart.Init(style->mTextOverflow.GetRight(direction));
mIEnd.Init(style->mTextOverflow.GetLeft(direction));
}
// The left/right marker string is setup in ExamineLineFrames when a line
// has overflow on that side.
}
/* static */ TextOverflow*
TextOverflow::WillProcessLines(nsDisplayListBuilder* aBuilder,
nsIFrame* aBlockFrame)
{
if (!CanHaveTextOverflow(aBuilder, aBlockFrame)) {
return nullptr;
}
nsIScrollableFrame* scrollableFrame = nsLayoutUtils::GetScrollableFrameFor(aBlockFrame);
if (scrollableFrame && scrollableFrame->IsTransformingByAPZ()) {
// If the APZ is actively scrolling this, don't bother with markers.
return nullptr;
}
return new TextOverflow(aBuilder, aBlockFrame);
}
void
TextOverflow::ExamineFrameSubtree(nsIFrame* aFrame,
const LogicalRect& aContentArea,
const LogicalRect& aInsideMarkersArea,
FrameHashtable* aFramesToHide,
AlignmentEdges* aAlignmentEdges,
bool* aFoundVisibleTextOrAtomic,
InnerClipEdges* aClippedMarkerEdges)
{
const nsIAtom* frameType = aFrame->GetType();
if (frameType == nsGkAtoms::brFrame ||
frameType == nsGkAtoms::placeholderFrame) {
return;
}
const bool isAtomic = IsAtomicElement(aFrame, frameType);
if (aFrame->StyleVisibility()->IsVisible()) {
LogicalRect childRect =
GetLogicalScrollableOverflowRectRelativeToBlock(aFrame);
bool overflowIStart =
childRect.IStart(mBlockWM) < aContentArea.IStart(mBlockWM);
bool overflowIEnd =
childRect.IEnd(mBlockWM) > aContentArea.IEnd(mBlockWM);
if (overflowIStart) {
mIStart.mHasOverflow = true;
}
if (overflowIEnd) {
mIEnd.mHasOverflow = true;
}
if (isAtomic && ((mIStart.mActive && overflowIStart) ||
(mIEnd.mActive && overflowIEnd))) {
aFramesToHide->PutEntry(aFrame);
} else if (isAtomic || frameType == nsGkAtoms::textFrame) {
AnalyzeMarkerEdges(aFrame, frameType, aInsideMarkersArea,
aFramesToHide, aAlignmentEdges,
aFoundVisibleTextOrAtomic,
aClippedMarkerEdges);
}
}
if (isAtomic) {
return;
}
nsIFrame* child = aFrame->GetFirstPrincipalChild();
while (child) {
ExamineFrameSubtree(child, aContentArea, aInsideMarkersArea,
aFramesToHide, aAlignmentEdges,
aFoundVisibleTextOrAtomic,
aClippedMarkerEdges);
child = child->GetNextSibling();
}
}
void
TextOverflow::AnalyzeMarkerEdges(nsIFrame* aFrame,
const nsIAtom* aFrameType,
const LogicalRect& aInsideMarkersArea,
FrameHashtable* aFramesToHide,
AlignmentEdges* aAlignmentEdges,
bool* aFoundVisibleTextOrAtomic,
InnerClipEdges* aClippedMarkerEdges)
{
LogicalRect borderRect(mBlockWM,
nsRect(aFrame->GetOffsetTo(mBlock),
aFrame->GetSize()),
mBlockSize);
nscoord istartOverlap = std::max(
aInsideMarkersArea.IStart(mBlockWM) - borderRect.IStart(mBlockWM), 0);
nscoord iendOverlap = std::max(
borderRect.IEnd(mBlockWM) - aInsideMarkersArea.IEnd(mBlockWM), 0);
bool insideIStartEdge =
aInsideMarkersArea.IStart(mBlockWM) <= borderRect.IEnd(mBlockWM);
bool insideIEndEdge =
borderRect.IStart(mBlockWM) <= aInsideMarkersArea.IEnd(mBlockWM);
if (istartOverlap > 0) {
aClippedMarkerEdges->AccumulateIStart(mBlockWM, borderRect);
if (!mIStart.mActive) {
istartOverlap = 0;
}
}
if (iendOverlap > 0) {
aClippedMarkerEdges->AccumulateIEnd(mBlockWM, borderRect);
if (!mIEnd.mActive) {
iendOverlap = 0;
}
}
if ((istartOverlap > 0 && insideIStartEdge) ||
(iendOverlap > 0 && insideIEndEdge)) {
if (aFrameType == nsGkAtoms::textFrame) {
if (aInsideMarkersArea.IStart(mBlockWM) <
aInsideMarkersArea.IEnd(mBlockWM)) {
// a clipped text frame and there is some room between the markers
nscoord snappedIStart, snappedIEnd;
auto textFrame = static_cast<nsTextFrame*>(aFrame);
bool isFullyClipped = mBlockWM.IsBidiLTR() ?
IsFullyClipped(textFrame, istartOverlap, iendOverlap,
&snappedIStart, &snappedIEnd) :
IsFullyClipped(textFrame, iendOverlap, istartOverlap,
&snappedIEnd, &snappedIStart);
if (!isFullyClipped) {
LogicalRect snappedRect = borderRect;
if (istartOverlap > 0) {
snappedRect.IStart(mBlockWM) += snappedIStart;
snappedRect.ISize(mBlockWM) -= snappedIStart;
}
if (iendOverlap > 0) {
snappedRect.ISize(mBlockWM) -= snappedIEnd;
}
aAlignmentEdges->Accumulate(mBlockWM, snappedRect);
*aFoundVisibleTextOrAtomic = true;
}
}
} else {
aFramesToHide->PutEntry(aFrame);
}
} else if (!insideIStartEdge || !insideIEndEdge) {
// frame is outside
if (IsAtomicElement(aFrame, aFrameType)) {
aFramesToHide->PutEntry(aFrame);
}
} else {
// frame is inside
aAlignmentEdges->Accumulate(mBlockWM, borderRect);
*aFoundVisibleTextOrAtomic = true;
}
}
void
TextOverflow::ExamineLineFrames(nsLineBox* aLine,
FrameHashtable* aFramesToHide,
AlignmentEdges* aAlignmentEdges)
{
// No ellipsing for 'clip' style.
bool suppressIStart = mIStart.mStyle->mType == NS_STYLE_TEXT_OVERFLOW_CLIP;
bool suppressIEnd = mIEnd.mStyle->mType == NS_STYLE_TEXT_OVERFLOW_CLIP;
if (mCanHaveInlineAxisScrollbar) {
LogicalPoint pos(mBlockWM, mScrollableFrame->GetScrollPosition(),
mBlockSize);
LogicalRect scrollRange(mBlockWM, mScrollableFrame->GetScrollRange(),
mBlockSize);
// No ellipsing when nothing to scroll to on that side (this includes
// overflow:auto that doesn't trigger a horizontal scrollbar).
if (pos.I(mBlockWM) <= scrollRange.IStart(mBlockWM)) {
suppressIStart = true;
}
if (pos.I(mBlockWM) >= scrollRange.IEnd(mBlockWM)) {
suppressIEnd = true;
}
}
LogicalRect contentArea = mContentArea;
const nscoord scrollAdjust = mAdjustForPixelSnapping ?
mBlock->PresContext()->AppUnitsPerDevPixel() : 0;
InflateIStart(mBlockWM, &contentArea, scrollAdjust);
InflateIEnd(mBlockWM, &contentArea, scrollAdjust);
LogicalRect lineRect(mBlockWM, aLine->GetScrollableOverflowArea(),
mBlockSize);
const bool istartOverflow =
!suppressIStart && lineRect.IStart(mBlockWM) < contentArea.IStart(mBlockWM);
const bool iendOverflow =
!suppressIEnd && lineRect.IEnd(mBlockWM) > contentArea.IEnd(mBlockWM);
if (!istartOverflow && !iendOverflow) {
// The line does not overflow on a side we should ellipsize.
return;
}
int pass = 0;
bool retryEmptyLine = true;
bool guessIStart = istartOverflow;
bool guessIEnd = iendOverflow;
mIStart.mActive = istartOverflow;
mIEnd.mActive = iendOverflow;
bool clippedIStartMarker = false;
bool clippedIEndMarker = false;
do {
// Setup marker strings as needed.
if (guessIStart) {
mIStart.SetupString(mBlock);
}
if (guessIEnd) {
mIEnd.SetupString(mBlock);
}
// If there is insufficient space for both markers then keep the one on the
// end side per the block's 'direction'.
nscoord istartMarkerISize = mIStart.mActive ? mIStart.mISize : 0;
nscoord iendMarkerISize = mIEnd.mActive ? mIEnd.mISize : 0;
if (istartMarkerISize && iendMarkerISize &&
istartMarkerISize + iendMarkerISize > contentArea.ISize(mBlockWM)) {
istartMarkerISize = 0;
}
// Calculate the area between the potential markers aligned at the
// block's edge.
LogicalRect insideMarkersArea = mContentArea;
if (guessIStart) {
InflateIStart(mBlockWM, &insideMarkersArea, -istartMarkerISize);
}
if (guessIEnd) {
InflateIEnd(mBlockWM, &insideMarkersArea, -iendMarkerISize);
}
// Analyze the frames on aLine for the overflow situation at the content
// edges and at the edges of the area between the markers.
bool foundVisibleTextOrAtomic = false;
int32_t n = aLine->GetChildCount();
nsIFrame* child = aLine->mFirstChild;
InnerClipEdges clippedMarkerEdges;
for (; n-- > 0; child = child->GetNextSibling()) {
ExamineFrameSubtree(child, contentArea, insideMarkersArea,
aFramesToHide, aAlignmentEdges,
&foundVisibleTextOrAtomic,
&clippedMarkerEdges);
}
if (!foundVisibleTextOrAtomic && retryEmptyLine) {
aAlignmentEdges->mAssigned = false;
aFramesToHide->Clear();
pass = -1;
if (mIStart.IsNeeded() && mIStart.mActive && !clippedIStartMarker) {
if (clippedMarkerEdges.mAssignedIStart &&
clippedMarkerEdges.mIStart > mContentArea.IStart(mBlockWM)) {
mIStart.mISize =
clippedMarkerEdges.mIStart - mContentArea.IStart(mBlockWM);
NS_ASSERTION(mIStart.mISize < mIStart.mIntrinsicISize,
"clipping a marker should make it strictly smaller");
clippedIStartMarker = true;
} else {
mIStart.mActive = guessIStart = false;
}
continue;
}
if (mIEnd.IsNeeded() && mIEnd.mActive && !clippedIEndMarker) {
if (clippedMarkerEdges.mAssignedIEnd &&
mContentArea.IEnd(mBlockWM) > clippedMarkerEdges.mIEnd) {
mIEnd.mISize = mContentArea.IEnd(mBlockWM) - clippedMarkerEdges.mIEnd;
NS_ASSERTION(mIEnd.mISize < mIEnd.mIntrinsicISize,
"clipping a marker should make it strictly smaller");
clippedIEndMarker = true;
} else {
mIEnd.mActive = guessIEnd = false;
}
continue;
}
// The line simply has no visible content even without markers,
// so examine the line again without suppressing markers.
retryEmptyLine = false;
mIStart.mISize = mIStart.mIntrinsicISize;
mIStart.mActive = guessIStart = istartOverflow;
mIEnd.mISize = mIEnd.mIntrinsicISize;
mIEnd.mActive = guessIEnd = iendOverflow;
continue;
}
if (guessIStart == (mIStart.mActive && mIStart.IsNeeded()) &&
guessIEnd == (mIEnd.mActive && mIEnd.IsNeeded())) {
break;
} else {
guessIStart = mIStart.mActive && mIStart.IsNeeded();
guessIEnd = mIEnd.mActive && mIEnd.IsNeeded();
mIStart.Reset();
mIEnd.Reset();
aFramesToHide->Clear();
}
NS_ASSERTION(pass == 0, "2nd pass should never guess wrong");
} while (++pass != 2);
if (!istartOverflow || !mIStart.mActive) {
mIStart.Reset();
}
if (!iendOverflow || !mIEnd.mActive) {
mIEnd.Reset();
}
}
void
TextOverflow::ProcessLine(const nsDisplayListSet& aLists,
nsLineBox* aLine)
{
NS_ASSERTION(mIStart.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP ||
mIEnd.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP,
"TextOverflow with 'clip' for both sides");
mIStart.Reset();
mIStart.mActive = mIStart.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP;
mIEnd.Reset();
mIEnd.mActive = mIEnd.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP;
FrameHashtable framesToHide(64);
AlignmentEdges alignmentEdges;
ExamineLineFrames(aLine, &framesToHide, &alignmentEdges);
bool needIStart = mIStart.IsNeeded();
bool needIEnd = mIEnd.IsNeeded();
if (!needIStart && !needIEnd) {
return;
}
NS_ASSERTION(mIStart.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP ||
!needIStart, "left marker for 'clip'");
NS_ASSERTION(mIEnd.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP ||
!needIEnd, "right marker for 'clip'");
// If there is insufficient space for both markers then keep the one on the
// end side per the block's 'direction'.
if (needIStart && needIEnd &&
mIStart.mISize + mIEnd.mISize > mContentArea.ISize(mBlockWM)) {
needIStart = false;
}
LogicalRect insideMarkersArea = mContentArea;
if (needIStart) {
InflateIStart(mBlockWM, &insideMarkersArea, -mIStart.mISize);
}
if (needIEnd) {
InflateIEnd(mBlockWM, &insideMarkersArea, -mIEnd.mISize);
}
if (!mCanHaveInlineAxisScrollbar && alignmentEdges.mAssigned) {
LogicalRect alignmentRect(mBlockWM, alignmentEdges.mIStart,
insideMarkersArea.BStart(mBlockWM),
alignmentEdges.ISize(), 1);
insideMarkersArea.IntersectRect(insideMarkersArea, alignmentRect);
}
// Clip and remove display items as needed at the final marker edges.
nsDisplayList* lists[] = { aLists.Content(), aLists.PositionedDescendants() };
for (uint32_t i = 0; i < ArrayLength(lists); ++i) {
PruneDisplayListContents(lists[i], framesToHide, insideMarkersArea);
}
CreateMarkers(aLine, needIStart, needIEnd, insideMarkersArea);
}
void
TextOverflow::PruneDisplayListContents(nsDisplayList* aList,
const FrameHashtable& aFramesToHide,
const LogicalRect& aInsideMarkersArea)
{
nsDisplayList saved;
nsDisplayItem* item;
while ((item = aList->RemoveBottom())) {
nsIFrame* itemFrame = item->Frame();
if (IsFrameDescendantOfAny(itemFrame, aFramesToHide, mBlock)) {
item->~nsDisplayItem();
continue;
}
nsDisplayList* wrapper = item->GetSameCoordinateSystemChildren();
if (wrapper) {
if (!itemFrame || GetSelfOrNearestBlock(itemFrame) == mBlock) {
PruneDisplayListContents(wrapper, aFramesToHide, aInsideMarkersArea);
}
}
nsCharClipDisplayItem* charClip = itemFrame ?
nsCharClipDisplayItem::CheckCast(item) : nullptr;
if (charClip && GetSelfOrNearestBlock(itemFrame) == mBlock) {
LogicalRect rect =
GetLogicalScrollableOverflowRectRelativeToBlock(itemFrame);
if (mIStart.IsNeeded()) {
nscoord istart =
aInsideMarkersArea.IStart(mBlockWM) - rect.IStart(mBlockWM);
if (istart > 0) {
(mBlockWM.IsBidiLTR() ?
charClip->mVisIStartEdge : charClip->mVisIEndEdge) = istart;
}
}
if (mIEnd.IsNeeded()) {
nscoord iend = rect.IEnd(mBlockWM) - aInsideMarkersArea.IEnd(mBlockWM);
if (iend > 0) {
(mBlockWM.IsBidiLTR() ?
charClip->mVisIEndEdge : charClip->mVisIStartEdge) = iend;
}
}
}
saved.AppendToTop(item);
}
aList->AppendToTop(&saved);
}
/* static */ bool
TextOverflow::HasClippedOverflow(nsIFrame* aBlockFrame)
{
const nsStyleTextReset* style = aBlockFrame->StyleTextReset();
return style->mTextOverflow.mLeft.mType == NS_STYLE_TEXT_OVERFLOW_CLIP &&
style->mTextOverflow.mRight.mType == NS_STYLE_TEXT_OVERFLOW_CLIP;
}
/* static */ bool
TextOverflow::CanHaveTextOverflow(nsDisplayListBuilder* aBuilder,
nsIFrame* aBlockFrame)
{
// Nothing to do for text-overflow:clip or if 'overflow-x/y:visible' or if
// we're just building items for event processing or image visibility.
if (HasClippedOverflow(aBlockFrame) ||
IsInlineAxisOverflowVisible(aBlockFrame) ||
aBuilder->IsForEventDelivery() || aBuilder->IsForImageVisibility()) {
return false;
}
// Skip ComboboxControlFrame because it would clip the drop-down arrow.
// Its anon block inherits 'text-overflow' and does what is expected.
if (aBlockFrame->GetType() == nsGkAtoms::comboboxControlFrame) {
return false;
}
// Inhibit the markers if a descendant content owns the caret.
RefPtr<nsCaret> caret = aBlockFrame->PresContext()->PresShell()->GetCaret();
if (caret && caret->IsVisible()) {
nsCOMPtr<nsISelection> domSelection = caret->GetSelection();
if (domSelection) {
nsCOMPtr<nsIDOMNode> node;
domSelection->GetFocusNode(getter_AddRefs(node));
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
if (content && nsContentUtils::ContentIsDescendantOf(content,
aBlockFrame->GetContent())) {
return false;
}
}
}
return true;
}
void
TextOverflow::CreateMarkers(const nsLineBox* aLine,
bool aCreateIStart, bool aCreateIEnd,
const mozilla::LogicalRect& aInsideMarkersArea)
{
if (aCreateIStart) {
DisplayListClipState::AutoSaveRestore clipState(mBuilder);
LogicalRect markerLogicalRect(
mBlockWM, aInsideMarkersArea.IStart(mBlockWM) - mIStart.mIntrinsicISize,
aLine->BStart(), mIStart.mIntrinsicISize, aLine->BSize());
nsPoint offset = mBuilder->ToReferenceFrame(mBlock);
nsRect markerRect =
markerLogicalRect.GetPhysicalRect(mBlockWM, mBlockSize) + offset;
ClipMarker(mContentArea.GetPhysicalRect(mBlockWM, mBlockSize) + offset,
markerRect, clipState);
nsDisplayItem* marker = new (mBuilder)
nsDisplayTextOverflowMarker(mBuilder, mBlock, markerRect,
aLine->GetLogicalAscent(), mIStart.mStyle, 0);
mMarkerList.AppendNewToTop(marker);
}
if (aCreateIEnd) {
DisplayListClipState::AutoSaveRestore clipState(mBuilder);
LogicalRect markerLogicalRect(
mBlockWM, aInsideMarkersArea.IEnd(mBlockWM), aLine->BStart(),
mIEnd.mIntrinsicISize, aLine->BSize());
nsPoint offset = mBuilder->ToReferenceFrame(mBlock);
nsRect markerRect =
markerLogicalRect.GetPhysicalRect(mBlockWM, mBlockSize) + offset;
ClipMarker(mContentArea.GetPhysicalRect(mBlockWM, mBlockSize) + offset,
markerRect, clipState);
nsDisplayItem* marker = new (mBuilder)
nsDisplayTextOverflowMarker(mBuilder, mBlock, markerRect,
aLine->GetLogicalAscent(), mIEnd.mStyle, 1);
mMarkerList.AppendNewToTop(marker);
}
}
void
TextOverflow::Marker::SetupString(nsIFrame* aFrame)
{
if (mInitialized) {
return;
}
if (mStyle->mType == NS_STYLE_TEXT_OVERFLOW_ELLIPSIS) {
gfxTextRun* textRun = GetEllipsisTextRun(aFrame);
if (textRun) {
mISize = textRun->GetAdvanceWidth(0, textRun->GetLength(), nullptr);
} else {
mISize = 0;
}
} else {
nsRenderingContext rc(
aFrame->PresContext()->PresShell()->CreateReferenceRenderingContext());
RefPtr<nsFontMetrics> fm;
nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm),
nsLayoutUtils::FontSizeInflationFor(aFrame));
mISize = nsLayoutUtils::AppUnitWidthOfStringBidi(mStyle->mString, aFrame,
*fm, rc);
}
mIntrinsicISize = mISize;
mInitialized = true;
}
} // namespace css
} // namespace mozilla