Bug 1866566 - If there is change in over-all relevancy, update HiddenByContentVisibil...
[gecko.git] / layout / generic / nsIFrame.cpp
blob593536c85e1e74b74d1a559b0034dfd81119ae98
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* base class of all rendering objects */
9 #include "nsIFrame.h"
11 #include <stdarg.h>
12 #include <algorithm>
14 #include "gfx2DGlue.h"
15 #include "gfxUtils.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/ComputedStyle.h"
18 #include "mozilla/DebugOnly.h"
19 #include "mozilla/DisplayPortUtils.h"
20 #include "mozilla/EventForwards.h"
21 #include "mozilla/dom/CSSAnimation.h"
22 #include "mozilla/dom/CSSTransition.h"
23 #include "mozilla/dom/ContentVisibilityAutoStateChangeEvent.h"
24 #include "mozilla/dom/DocumentInlines.h"
25 #include "mozilla/dom/AncestorIterator.h"
26 #include "mozilla/dom/ElementInlines.h"
27 #include "mozilla/dom/ImageTracker.h"
28 #include "mozilla/dom/Selection.h"
29 #include "mozilla/gfx/2D.h"
30 #include "mozilla/gfx/PathHelpers.h"
31 #include "mozilla/IntegerRange.h"
32 #include "mozilla/intl/BidiEmbeddingLevel.h"
33 #include "mozilla/Maybe.h"
34 #include "mozilla/PresShell.h"
35 #include "mozilla/PresShellInlines.h"
36 #include "mozilla/ResultExtensions.h"
37 #include "mozilla/Sprintf.h"
38 #include "mozilla/StaticAnalysisFunctions.h"
39 #include "mozilla/StaticPrefs_layout.h"
40 #include "mozilla/StaticPrefs_print.h"
41 #include "mozilla/StaticPrefs_ui.h"
42 #include "mozilla/SVGMaskFrame.h"
43 #include "mozilla/SVGObserverUtils.h"
44 #include "mozilla/SVGTextFrame.h"
45 #include "mozilla/SVGIntegrationUtils.h"
46 #include "mozilla/SVGUtils.h"
47 #include "mozilla/TextControlElement.h"
48 #include "mozilla/ToString.h"
49 #include "mozilla/Try.h"
50 #include "mozilla/ViewportUtils.h"
52 #include "nsCOMPtr.h"
53 #include "nsFieldSetFrame.h"
54 #include "nsFlexContainerFrame.h"
55 #include "nsFocusManager.h"
56 #include "nsFrameList.h"
57 #include "nsPlaceholderFrame.h"
58 #include "nsIBaseWindow.h"
59 #include "nsIContent.h"
60 #include "nsIContentInlines.h"
61 #include "nsContentUtils.h"
62 #include "nsCSSFrameConstructor.h"
63 #include "nsCSSProps.h"
64 #include "nsCSSPseudoElements.h"
65 #include "nsCSSRendering.h"
66 #include "nsAtom.h"
67 #include "nsString.h"
68 #include "nsReadableUtils.h"
69 #include "nsTableWrapperFrame.h"
70 #include "nsView.h"
71 #include "nsViewManager.h"
72 #include "nsIScrollableFrame.h"
73 #include "nsPresContext.h"
74 #include "nsPresContextInlines.h"
75 #include "nsStyleConsts.h"
76 #include "mozilla/Logging.h"
77 #include "nsLayoutUtils.h"
78 #include "LayoutLogging.h"
79 #include "mozilla/RestyleManager.h"
80 #include "nsImageFrame.h"
81 #include "nsInlineFrame.h"
82 #include "nsFrameSelection.h"
83 #include "nsGkAtoms.h"
84 #include "nsGridContainerFrame.h"
85 #include "nsGfxScrollFrame.h"
86 #include "nsCSSAnonBoxes.h"
87 #include "nsCanvasFrame.h"
89 #include "nsFieldSetFrame.h"
90 #include "nsFrameTraversal.h"
91 #include "nsRange.h"
92 #include "nsITextControlFrame.h"
93 #include "nsNameSpaceManager.h"
94 #include "nsIPercentBSizeObserver.h"
95 #include "nsStyleStructInlines.h"
97 #include "nsBidiPresUtils.h"
98 #include "RubyUtils.h"
99 #include "TextOverflow.h"
100 #include "nsAnimationManager.h"
102 // For triple-click pref
103 #include "imgIRequest.h"
104 #include "nsError.h"
105 #include "nsContainerFrame.h"
106 #include "nsBlockFrame.h"
107 #include "nsDisplayList.h"
108 #include "nsChangeHint.h"
109 #include "nsSubDocumentFrame.h"
110 #include "RetainedDisplayListBuilder.h"
112 #include "gfxContext.h"
113 #include "nsAbsoluteContainingBlock.h"
114 #include "ScrollSnap.h"
115 #include "StickyScrollContainer.h"
116 #include "nsFontInflationData.h"
117 #include "nsRegion.h"
118 #include "nsIFrameInlines.h"
119 #include "nsStyleChangeList.h"
120 #include "nsWindowSizes.h"
122 #ifdef ACCESSIBILITY
123 # include "nsAccessibilityService.h"
124 #endif
126 #include "mozilla/AsyncEventDispatcher.h"
127 #include "mozilla/CSSClipPathInstance.h"
128 #include "mozilla/EffectCompositor.h"
129 #include "mozilla/EffectSet.h"
130 #include "mozilla/EventListenerManager.h"
131 #include "mozilla/EventStateManager.h"
132 #include "mozilla/Preferences.h"
133 #include "mozilla/LookAndFeel.h"
134 #include "mozilla/MouseEvents.h"
135 #include "mozilla/ServoStyleSet.h"
136 #include "mozilla/ServoStyleSetInlines.h"
137 #include "mozilla/css/ImageLoader.h"
138 #include "mozilla/dom/HTMLBodyElement.h"
139 #include "mozilla/dom/SVGPathData.h"
140 #include "mozilla/dom/TouchEvent.h"
141 #include "mozilla/gfx/Tools.h"
142 #include "mozilla/layers/WebRenderUserData.h"
143 #include "mozilla/layout/ScrollAnchorContainer.h"
144 #include "nsPrintfCString.h"
145 #include "ActiveLayerTracker.h"
147 #include "nsITheme.h"
149 using namespace mozilla;
150 using namespace mozilla::css;
151 using namespace mozilla::dom;
152 using namespace mozilla::gfx;
153 using namespace mozilla::layers;
154 using namespace mozilla::layout;
155 typedef nsAbsoluteContainingBlock::AbsPosReflowFlags AbsPosReflowFlags;
156 using nsStyleTransformMatrix::TransformReferenceBox;
158 nsIFrame* nsILineIterator::LineInfo::GetLastFrameOnLine() const {
159 if (!mNumFramesOnLine) {
160 return nullptr; // empty line, not illegal
162 MOZ_ASSERT(mFirstFrameOnLine);
163 nsIFrame* maybeLastFrame = mFirstFrameOnLine;
164 for ([[maybe_unused]] int32_t i : IntegerRange(mNumFramesOnLine - 1)) {
165 maybeLastFrame = maybeLastFrame->GetNextSibling();
166 if (NS_WARN_IF(!maybeLastFrame)) {
167 return nullptr;
170 return maybeLastFrame;
173 const mozilla::LayoutFrameType nsIFrame::sLayoutFrameTypes[kFrameClassCount] = {
174 #define FRAME_ID(class_, type_, ...) mozilla::LayoutFrameType::type_,
175 #define ABSTRACT_FRAME_ID(...)
176 #include "mozilla/FrameIdList.h"
177 #undef FRAME_ID
178 #undef ABSTRACT_FRAME_ID
181 const nsIFrame::ClassFlags nsIFrame::sLayoutFrameClassFlags[kFrameClassCount] =
183 #define FRAME_ID(class_, type_, flags_, ...) flags_,
184 #define ABSTRACT_FRAME_ID(...)
185 #include "mozilla/FrameIdList.h"
186 #undef FRAME_ID
187 #undef ABSTRACT_FRAME_ID
190 std::ostream& operator<<(std::ostream& aStream, const nsDirection& aDirection) {
191 return aStream << (aDirection == eDirNext ? "eDirNext" : "eDirPrevious");
194 struct nsContentAndOffset {
195 nsIContent* mContent = nullptr;
196 int32_t mOffset = 0;
199 // Some Misc #defines
200 #define SELECTION_DEBUG 0
201 #define FORCE_SELECTION_UPDATE 1
202 #define CALC_DEBUG 0
204 #include "nsILineIterator.h"
205 #include "prenv.h"
207 // Utility function to set a nsRect-valued property table entry on aFrame,
208 // reusing the existing storage if the property happens to be already set.
209 template <typename T>
210 static void SetOrUpdateRectValuedProperty(
211 nsIFrame* aFrame, FrameProperties::Descriptor<T> aProperty,
212 const nsRect& aNewValue) {
213 bool found;
214 nsRect* rectStorage = aFrame->GetProperty(aProperty, &found);
215 if (!found) {
216 rectStorage = new nsRect(aNewValue);
217 aFrame->AddProperty(aProperty, rectStorage);
218 } else {
219 *rectStorage = aNewValue;
223 FrameDestroyContext::~FrameDestroyContext() {
224 for (auto& content : mozilla::Reversed(mAnonymousContent)) {
225 mPresShell->NativeAnonymousContentRemoved(content);
226 content->UnbindFromTree();
230 // Formerly the nsIFrameDebug interface
232 std::ostream& operator<<(std::ostream& aStream, const nsReflowStatus& aStatus) {
233 char complete = 'Y';
234 if (aStatus.IsIncomplete()) {
235 complete = 'N';
236 } else if (aStatus.IsOverflowIncomplete()) {
237 complete = 'O';
240 char brk = 'N';
241 if (aStatus.IsInlineBreakBefore()) {
242 brk = 'B';
243 } else if (aStatus.IsInlineBreakAfter()) {
244 brk = 'A';
247 aStream << "["
248 << "Complete=" << complete << ","
249 << "NIF=" << (aStatus.NextInFlowNeedsReflow() ? 'Y' : 'N') << ","
250 << "Break=" << brk << ","
251 << "FirstLetter=" << (aStatus.FirstLetterComplete() ? 'Y' : 'N')
252 << "]";
253 return aStream;
256 #ifdef DEBUG
259 * Note: the log module is created during library initialization which
260 * means that you cannot perform logging before then.
262 mozilla::LazyLogModule nsIFrame::sFrameLogModule("frame");
264 #endif
266 NS_DECLARE_FRAME_PROPERTY_DELETABLE(AbsoluteContainingBlockProperty,
267 nsAbsoluteContainingBlock)
269 bool nsIFrame::HasAbsolutelyPositionedChildren() const {
270 return IsAbsoluteContainer() &&
271 GetAbsoluteContainingBlock()->HasAbsoluteFrames();
274 nsAbsoluteContainingBlock* nsIFrame::GetAbsoluteContainingBlock() const {
275 NS_ASSERTION(IsAbsoluteContainer(),
276 "The frame is not marked as an abspos container correctly");
277 nsAbsoluteContainingBlock* absCB =
278 GetProperty(AbsoluteContainingBlockProperty());
279 NS_ASSERTION(absCB,
280 "The frame is marked as an abspos container but doesn't have "
281 "the property");
282 return absCB;
285 void nsIFrame::MarkAsAbsoluteContainingBlock() {
286 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN));
287 NS_ASSERTION(!GetProperty(AbsoluteContainingBlockProperty()),
288 "Already has an abs-pos containing block property?");
289 NS_ASSERTION(!HasAnyStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN),
290 "Already has NS_FRAME_HAS_ABSPOS_CHILDREN state bit?");
291 AddStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN);
292 SetProperty(AbsoluteContainingBlockProperty(),
293 new nsAbsoluteContainingBlock(GetAbsoluteListID()));
296 void nsIFrame::MarkAsNotAbsoluteContainingBlock() {
297 NS_ASSERTION(!HasAbsolutelyPositionedChildren(), "Think of the children!");
298 NS_ASSERTION(GetProperty(AbsoluteContainingBlockProperty()),
299 "Should have an abs-pos containing block property");
300 NS_ASSERTION(HasAnyStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN),
301 "Should have NS_FRAME_HAS_ABSPOS_CHILDREN state bit");
302 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN));
303 RemoveStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN);
304 RemoveProperty(AbsoluteContainingBlockProperty());
307 bool nsIFrame::CheckAndClearPaintedState() {
308 bool result = HasAnyStateBits(NS_FRAME_PAINTED_THEBES);
309 RemoveStateBits(NS_FRAME_PAINTED_THEBES);
311 for (const auto& childList : ChildLists()) {
312 for (nsIFrame* child : childList.mList) {
313 if (child->CheckAndClearPaintedState()) {
314 result = true;
318 return result;
321 bool nsIFrame::CheckAndClearDisplayListState() {
322 bool result = BuiltDisplayList();
323 SetBuiltDisplayList(false);
325 for (const auto& childList : ChildLists()) {
326 for (nsIFrame* child : childList.mList) {
327 if (child->CheckAndClearDisplayListState()) {
328 result = true;
332 return result;
335 bool nsIFrame::IsVisibleConsideringAncestors(uint32_t aFlags) const {
336 if (!StyleVisibility()->IsVisible()) {
337 return false;
340 if (PresShell()->IsUnderHiddenEmbedderElement()) {
341 return false;
344 const nsIFrame* frame = this;
345 while (frame) {
346 nsView* view = frame->GetView();
347 if (view && view->GetVisibility() == ViewVisibility::Hide) {
348 return false;
351 if (frame->StyleUIReset()->mMozSubtreeHiddenOnlyVisually) {
352 return false;
355 // This method is used to determine if a frame is focusable, because it's
356 // called by nsIFrame::IsFocusable. `content-visibility: auto` should not
357 // force this frame to be unfocusable, so we only take into account
358 // `content-visibility: hidden` here.
359 if (this != frame &&
360 frame->HidesContent(IncludeContentVisibility::Hidden)) {
361 return false;
364 if (nsIFrame* parent = frame->GetParent()) {
365 frame = parent;
366 } else {
367 parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(frame);
368 if (!parent) break;
370 if ((aFlags & nsIFrame::VISIBILITY_CROSS_CHROME_CONTENT_BOUNDARY) == 0 &&
371 parent->PresContext()->IsChrome() &&
372 !frame->PresContext()->IsChrome()) {
373 break;
376 frame = parent;
380 return true;
383 void nsIFrame::FindCloserFrameForSelection(
384 const nsPoint& aPoint, FrameWithDistance* aCurrentBestFrame) {
385 if (nsLayoutUtils::PointIsCloserToRect(aPoint, mRect,
386 aCurrentBestFrame->mXDistance,
387 aCurrentBestFrame->mYDistance)) {
388 aCurrentBestFrame->mFrame = this;
392 void nsIFrame::ElementStateChanged(mozilla::dom::ElementState aStates) {}
394 void WeakFrame::Clear(mozilla::PresShell* aPresShell) {
395 if (aPresShell) {
396 aPresShell->RemoveWeakFrame(this);
398 mFrame = nullptr;
401 AutoWeakFrame::AutoWeakFrame(const WeakFrame& aOther)
402 : mPrev(nullptr), mFrame(nullptr) {
403 Init(aOther.GetFrame());
406 void AutoWeakFrame::Clear(mozilla::PresShell* aPresShell) {
407 if (aPresShell) {
408 aPresShell->RemoveAutoWeakFrame(this);
410 mFrame = nullptr;
411 mPrev = nullptr;
414 AutoWeakFrame::~AutoWeakFrame() {
415 Clear(mFrame ? mFrame->PresContext()->GetPresShell() : nullptr);
418 void AutoWeakFrame::Init(nsIFrame* aFrame) {
419 Clear(mFrame ? mFrame->PresContext()->GetPresShell() : nullptr);
420 mFrame = aFrame;
421 if (mFrame) {
422 mozilla::PresShell* presShell = mFrame->PresContext()->GetPresShell();
423 NS_WARNING_ASSERTION(presShell, "Null PresShell in AutoWeakFrame!");
424 if (presShell) {
425 presShell->AddAutoWeakFrame(this);
426 } else {
427 mFrame = nullptr;
432 void WeakFrame::Init(nsIFrame* aFrame) {
433 Clear(mFrame ? mFrame->PresContext()->GetPresShell() : nullptr);
434 mFrame = aFrame;
435 if (mFrame) {
436 mozilla::PresShell* presShell = mFrame->PresContext()->GetPresShell();
437 MOZ_ASSERT(presShell, "Null PresShell in WeakFrame!");
438 if (presShell) {
439 presShell->AddWeakFrame(this);
440 } else {
441 mFrame = nullptr;
446 nsIFrame* NS_NewEmptyFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
447 return new (aPresShell) nsIFrame(aStyle, aPresShell->GetPresContext());
450 nsIFrame::~nsIFrame() {
451 MOZ_COUNT_DTOR(nsIFrame);
453 MOZ_ASSERT(GetVisibility() != Visibility::ApproximatelyVisible,
454 "Visible nsFrame is being destroyed");
457 NS_IMPL_FRAMEARENA_HELPERS(nsIFrame)
459 // Dummy operator delete. Will never be called, but must be defined
460 // to satisfy some C++ ABIs.
461 void nsIFrame::operator delete(void*, size_t) {
462 MOZ_CRASH("nsIFrame::operator delete should never be called");
465 NS_QUERYFRAME_HEAD(nsIFrame)
466 NS_QUERYFRAME_ENTRY(nsIFrame)
467 NS_QUERYFRAME_TAIL_INHERITANCE_ROOT
469 /////////////////////////////////////////////////////////////////////////////
470 // nsIFrame
472 static bool IsFontSizeInflationContainer(nsIFrame* aFrame,
473 const nsStyleDisplay* aStyleDisplay) {
475 * Font size inflation is built around the idea that we're inflating
476 * the fonts for a pan-and-zoom UI so that when the user scales up a
477 * block or other container to fill the width of the device, the fonts
478 * will be readable. To do this, we need to pick what counts as a
479 * container.
481 * From a code perspective, the only hard requirement is that frames
482 * that are line participants (nsIFrame::IsLineParticipant) are never
483 * containers, since line layout assumes that the inflation is consistent
484 * within a line.
486 * This is not an imposition, since we obviously want a bunch of text
487 * (possibly with inline elements) flowing within a block to count the
488 * block (or higher) as its container.
490 * We also want form controls, including the text in the anonymous
491 * content inside of them, to match each other and the text next to
492 * them, so they and their anonymous content should also not be a
493 * container.
495 * However, because we can't reliably compute sizes across XUL during
496 * reflow, any XUL frame with a XUL parent is always a container.
498 * There are contexts where it would be nice if some blocks didn't
499 * count as a container, so that, for example, an indented quotation
500 * didn't end up with a smaller font size. However, it's hard to
501 * distinguish these situations where we really do want the indented
502 * thing to count as a container, so we don't try, and blocks are
503 * always containers.
506 // The root frame should always be an inflation container.
507 if (!aFrame->GetParent()) {
508 return true;
511 nsIContent* content = aFrame->GetContent();
512 if (content && content->IsInNativeAnonymousSubtree()) {
513 // Native anonymous content shouldn't be a font inflation root,
514 // except for the canvas custom content container.
515 nsCanvasFrame* canvas = aFrame->PresShell()->GetCanvasFrame();
516 return canvas && canvas->GetCustomContentContainer() == content;
519 LayoutFrameType frameType = aFrame->Type();
520 bool isInline =
521 aFrame->GetDisplay().IsInlineFlow() || RubyUtils::IsRubyBox(frameType) ||
522 (aStyleDisplay->IsFloatingStyle() &&
523 frameType == LayoutFrameType::Letter) ||
524 // Given multiple frames for the same node, only the
525 // outer one should be considered a container.
526 // (Important, e.g., for nsSelectsAreaFrame.)
527 (aFrame->GetParent()->GetContent() == content) ||
528 (content &&
529 // Form controls shouldn't become inflation containers.
530 (content->IsAnyOfHTMLElements(nsGkAtoms::option, nsGkAtoms::optgroup,
531 nsGkAtoms::select, nsGkAtoms::input,
532 nsGkAtoms::button, nsGkAtoms::textarea)));
533 NS_ASSERTION(!aFrame->IsLineParticipant() || isInline ||
534 // br frames and mathml frames report being line
535 // participants even when their position or display is
536 // set
537 aFrame->IsBrFrame() || aFrame->IsMathMLFrame(),
538 "line participants must not be containers");
539 return !isInline;
542 static void MaybeScheduleReflowSVGNonDisplayText(nsIFrame* aFrame) {
543 if (!aFrame->IsInSVGTextSubtree()) {
544 return;
547 // We need to ensure that any non-display SVGTextFrames get reflowed when a
548 // child text frame gets new style. Thus we need to schedule a reflow in
549 // |DidSetComputedStyle|. We also need to call it from |DestroyFrom|,
550 // because otherwise we won't get notified when style changes to
551 // "display:none".
552 SVGTextFrame* svgTextFrame = static_cast<SVGTextFrame*>(
553 nsLayoutUtils::GetClosestFrameOfType(aFrame, LayoutFrameType::SVGText));
554 nsIFrame* anonBlock = svgTextFrame->PrincipalChildList().FirstChild();
556 // Note that we must check NS_FRAME_FIRST_REFLOW on our SVGTextFrame's
557 // anonymous block frame rather than our aFrame, since NS_FRAME_FIRST_REFLOW
558 // may be set on us if we're a new frame that has been inserted after the
559 // document's first reflow. (In which case this DidSetComputedStyle call may
560 // be happening under frame construction under a Reflow() call.)
561 if (!anonBlock || anonBlock->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
562 return;
565 if (!svgTextFrame->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY) ||
566 svgTextFrame->HasAnyStateBits(NS_STATE_SVG_TEXT_IN_REFLOW)) {
567 return;
570 svgTextFrame->ScheduleReflowSVGNonDisplayText(
571 IntrinsicDirty::FrameAncestorsAndDescendants);
574 bool nsIFrame::IsPrimaryFrameOfRootOrBodyElement() const {
575 if (!IsPrimaryFrame()) {
576 return false;
578 nsIContent* content = GetContent();
579 Document* document = content->OwnerDoc();
580 return content == document->GetRootElement() ||
581 content == document->GetBodyElement();
584 bool nsIFrame::IsRenderedLegend() const {
585 if (auto* parent = GetParent(); parent && parent->IsFieldSetFrame()) {
586 return static_cast<nsFieldSetFrame*>(parent)->GetLegend() == this;
588 return false;
591 void nsIFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
592 nsIFrame* aPrevInFlow) {
593 MOZ_ASSERT(nsQueryFrame::FrameIID(mClass) == GetFrameId());
594 MOZ_ASSERT(!mContent, "Double-initing a frame?");
596 mContent = aContent;
597 mParent = aParent;
598 MOZ_DIAGNOSTIC_ASSERT(!mParent || PresShell() == mParent->PresShell());
600 if (aPrevInFlow) {
601 mWritingMode = aPrevInFlow->GetWritingMode();
603 // Copy some state bits from prev-in-flow (the bits that should apply
604 // throughout a continuation chain). The bits are sorted according to their
605 // order in nsFrameStateBits.h.
607 // clang-format off
608 AddStateBits(aPrevInFlow->GetStateBits() &
609 (NS_FRAME_GENERATED_CONTENT |
610 NS_FRAME_OUT_OF_FLOW |
611 NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN |
612 NS_FRAME_INDEPENDENT_SELECTION |
613 NS_FRAME_PART_OF_IBSPLIT |
614 NS_FRAME_MAY_BE_TRANSFORMED |
615 NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR));
616 // clang-format on
618 // Copy other bits in nsIFrame from prev-in-flow.
619 mHasColumnSpanSiblings = aPrevInFlow->HasColumnSpanSiblings();
620 } else {
621 PresContext()->ConstructedFrame();
624 if (GetParent()) {
625 if (MOZ_UNLIKELY(mContent == PresContext()->Document()->GetRootElement() &&
626 mContent == GetParent()->GetContent())) {
627 // Our content is the root element and we have the same content as our
628 // parent. That is, we are the internal anonymous frame of the root
629 // element. Copy the used mWritingMode from our parent because
630 // mDocElementContainingBlock gets its mWritingMode from <body>.
631 mWritingMode = GetParent()->GetWritingMode();
634 // Copy some state bits from our parent (the bits that should apply
635 // recursively throughout a subtree). The bits are sorted according to their
636 // order in nsFrameStateBits.h.
638 // clang-format off
639 AddStateBits(GetParent()->GetStateBits() &
640 (NS_FRAME_GENERATED_CONTENT |
641 NS_FRAME_INDEPENDENT_SELECTION |
642 NS_FRAME_IS_SVG_TEXT |
643 NS_FRAME_IN_POPUP |
644 NS_FRAME_IS_NONDISPLAY));
645 // clang-format on
647 if (HasAnyStateBits(NS_FRAME_IN_POPUP) && TrackingVisibility()) {
648 // Assume all frames in popups are visible.
649 IncApproximateVisibleCount();
652 if (aPrevInFlow) {
653 mMayHaveOpacityAnimation = aPrevInFlow->MayHaveOpacityAnimation();
654 mMayHaveTransformAnimation = aPrevInFlow->MayHaveTransformAnimation();
655 } else if (mContent) {
656 // It's fine to fetch the EffectSet for the style frame here because in the
657 // following code we take care of the case where animations may target
658 // a different frame.
659 EffectSet* effectSet = EffectSet::GetForStyleFrame(this);
660 if (effectSet) {
661 mMayHaveOpacityAnimation = effectSet->MayHaveOpacityAnimation();
663 if (effectSet->MayHaveTransformAnimation()) {
664 // If we are the inner table frame for display:table content, then
665 // transform animations should go on our parent frame (the table wrapper
666 // frame).
668 // We do this when initializing the child frame (table inner frame),
669 // because when initializng the table wrapper frame, we don't yet have
670 // access to its children so we can't tell if we have transform
671 // animations or not.
672 if (SupportsCSSTransforms()) {
673 mMayHaveTransformAnimation = true;
674 AddStateBits(NS_FRAME_MAY_BE_TRANSFORMED);
675 } else if (aParent && nsLayoutUtils::GetStyleFrame(aParent) == this) {
676 MOZ_ASSERT(
677 aParent->SupportsCSSTransforms(),
678 "Style frames that don't support transforms should have parents"
679 " that do");
680 aParent->mMayHaveTransformAnimation = true;
681 aParent->AddStateBits(NS_FRAME_MAY_BE_TRANSFORMED);
687 const nsStyleDisplay* disp = StyleDisplay();
688 if (disp->HasTransform(this)) {
689 // If 'transform' dynamically changes, RestyleManager takes care of
690 // updating this bit.
691 AddStateBits(NS_FRAME_MAY_BE_TRANSFORMED);
694 if (nsLayoutUtils::FontSizeInflationEnabled(PresContext()) ||
695 !GetParent()
696 #ifdef DEBUG
697 // We have assertions that check inflation invariants even when
698 // font size inflation is not enabled.
699 || true
700 #endif
702 if (IsFontSizeInflationContainer(this, disp)) {
703 AddStateBits(NS_FRAME_FONT_INFLATION_CONTAINER);
704 if (!GetParent() ||
705 // I'd use NS_FRAME_OUT_OF_FLOW, but it's not set yet.
706 disp->IsFloating(this) || disp->IsAbsolutelyPositioned(this) ||
707 GetParent()->IsFlexContainerFrame() ||
708 GetParent()->IsGridContainerFrame()) {
709 AddStateBits(NS_FRAME_FONT_INFLATION_FLOW_ROOT);
712 NS_ASSERTION(
713 GetParent() || HasAnyStateBits(NS_FRAME_FONT_INFLATION_CONTAINER),
714 "root frame should always be a container");
717 if (TrackingVisibility() && PresShell()->AssumeAllFramesVisible()) {
718 IncApproximateVisibleCount();
721 DidSetComputedStyle(nullptr);
723 // For a newly created frame, we need to update this frame's visibility state.
724 // Usually we update the state when the frame is restyled and has a
725 // VisibilityChange change hint but we don't generate any change hints for
726 // newly created frames.
727 // Note: We don't need to do this for placeholders since placeholders have
728 // different styles so that the styles don't have visibility:hidden even if
729 // the parent has visibility:hidden style. We also don't need to update the
730 // state when creating continuations because its visibility is the same as its
731 // prev-in-flow, and the animation code cares only primary frames.
732 if (!IsPlaceholderFrame() && !aPrevInFlow) {
733 UpdateVisibleDescendantsState();
736 if (!aPrevInFlow && HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
737 // We aren't going to get a reflow, so nothing else will call
738 // InvalidateRenderingObservers, we have to do it here.
739 SVGObserverUtils::InvalidateRenderingObservers(this);
743 void nsIFrame::InitPrimaryFrame() {
744 MOZ_ASSERT(IsPrimaryFrame());
745 const nsStyleDisplay* disp = StyleDisplay();
747 if (disp->mContainerType != StyleContainerType::Normal) {
748 PresContext()->RegisterContainerQueryFrame(this);
751 if (StyleDisplay()->ContentVisibility(*this) ==
752 StyleContentVisibility::Auto) {
753 PresShell()->RegisterContentVisibilityAutoFrame(this);
754 } else if (auto* element = Element::FromNodeOrNull(GetContent())) {
755 element->ClearContentRelevancy();
758 // TODO(mrobinson): Once bug 1765615 is fixed, this should be called on
759 // layout changes. In addition, when `content-visibility: auto` is implemented
760 // this should also be called when scrolling or focus causes content to be
761 // skipped or unskipped.
762 UpdateAnimationVisibility();
764 HandleLastRememberedSize();
767 void nsIFrame::Destroy(DestroyContext& aContext) {
768 NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
769 "destroy called on frame while scripts not blocked");
770 NS_ASSERTION(!GetNextSibling() && !GetPrevSibling(),
771 "Frames should be removed before destruction.");
772 MOZ_ASSERT(!HasAbsolutelyPositionedChildren());
773 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT),
774 "NS_FRAME_PART_OF_IBSPLIT set on non-nsContainerFrame?");
776 MaybeScheduleReflowSVGNonDisplayText(this);
778 SVGObserverUtils::InvalidateDirectRenderingObservers(this);
780 const auto* disp = StyleDisplay();
781 if (disp->mPosition == StylePositionProperty::Sticky) {
782 if (auto* ssc =
783 StickyScrollContainer::GetStickyScrollContainerForFrame(this)) {
784 ssc->RemoveFrame(this);
788 if (disp->mContainerType != StyleContainerType::Normal) {
789 PresContext()->UnregisterContainerQueryFrame(this);
792 nsPresContext* presContext = PresContext();
793 mozilla::PresShell* presShell = presContext->GetPresShell();
794 if (HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
795 if (nsPlaceholderFrame* placeholder = GetPlaceholderFrame()) {
796 placeholder->SetOutOfFlowFrame(nullptr);
800 if (IsPrimaryFrame()) {
801 // This needs to happen before we clear our Properties() table.
802 ActiveLayerTracker::TransferActivityToContent(this, mContent);
805 ScrollAnchorContainer* anchor = nullptr;
806 if (IsScrollAnchor(&anchor)) {
807 anchor->InvalidateAnchor();
810 if (HasCSSAnimations() || HasCSSTransitions() ||
811 // It's fine to look up the style frame here since if we're destroying the
812 // frames for display:table content we should be destroying both wrapper
813 // and inner frame.
814 EffectSet::GetForStyleFrame(this)) {
815 // If no new frame for this element is created by the end of the
816 // restyling process, stop animations and transitions for this frame
817 RestyleManager::AnimationsWithDestroyedFrame* adf =
818 presContext->RestyleManager()->GetAnimationsWithDestroyedFrame();
819 // AnimationsWithDestroyedFrame only lives during the restyling process.
820 if (adf) {
821 adf->Put(mContent, mComputedStyle);
825 // Disable visibility tracking. Note that we have to do this before we clear
826 // frame properties and lose track of whether we were previously visible.
827 // XXX(seth): It'd be ideal to assert that we're already marked nonvisible
828 // here, but it's unfortunately tricky to guarantee in the face of things like
829 // frame reconstruction induced by style changes.
830 DisableVisibilityTracking();
832 // Ensure that we're not in the approximately visible list anymore.
833 PresContext()->GetPresShell()->RemoveFrameFromApproximatelyVisibleList(this);
835 presShell->NotifyDestroyingFrame(this);
837 if (HasAnyStateBits(NS_FRAME_EXTERNAL_REFERENCE)) {
838 presShell->ClearFrameRefs(this);
841 nsView* view = GetView();
842 if (view) {
843 view->SetFrame(nullptr);
844 view->Destroy();
847 // Make sure that our deleted frame can't be returned from GetPrimaryFrame()
848 if (IsPrimaryFrame()) {
849 mContent->SetPrimaryFrame(nullptr);
851 // Pass the root of a generated content subtree (e.g. ::after/::before) to
852 // aPostDestroyData to unbind it after frame destruction is done.
853 if (HasAnyStateBits(NS_FRAME_GENERATED_CONTENT) &&
854 mContent->IsRootOfNativeAnonymousSubtree()) {
855 aContext.AddAnonymousContent(mContent.forget());
859 // Remove all properties attached to the frame, to ensure any property
860 // destructors that need the frame pointer are handled properly.
861 RemoveAllProperties();
863 // Must retrieve the object ID before calling destructors, so the
864 // vtable is still valid.
866 // Note to future tweakers: having the method that returns the
867 // object size call the destructor will not avoid an indirect call;
868 // the compiler cannot devirtualize the call to the destructor even
869 // if it's from a method defined in the same class.
871 nsQueryFrame::FrameIID id = GetFrameId();
872 this->~nsIFrame();
874 #ifdef DEBUG
876 nsIFrame* rootFrame = presShell->GetRootFrame();
877 MOZ_ASSERT(rootFrame);
878 if (this != rootFrame) {
879 auto* builder = nsLayoutUtils::GetRetainedDisplayListBuilder(rootFrame);
880 auto* data = builder ? builder->Data() : nullptr;
882 const bool inData =
883 data && (data->IsModified(this) || data->HasProps(this));
885 if (inData) {
886 DL_LOG(LogLevel::Warning, "Frame %p found in retained data", this);
889 MOZ_ASSERT(!inData, "Deleted frame in retained data!");
892 #endif
894 // Now that we're totally cleaned out, we need to add ourselves to
895 // the presshell's recycler.
896 presShell->FreeFrame(id, this);
899 std::pair<int32_t, int32_t> nsIFrame::GetOffsets() const {
900 return std::make_pair(0, 0);
903 static void CompareLayers(
904 const nsStyleImageLayers* aFirstLayers,
905 const nsStyleImageLayers* aSecondLayers,
906 const std::function<void(imgRequestProxy* aReq)>& aCallback) {
907 NS_FOR_VISIBLE_IMAGE_LAYERS_BACK_TO_FRONT(i, (*aFirstLayers)) {
908 const auto& image = aFirstLayers->mLayers[i].mImage;
909 if (!image.IsImageRequestType() || !image.IsResolved()) {
910 continue;
913 // aCallback is called when the style image in aFirstLayers is thought to
914 // be different with the corresponded one in aSecondLayers
915 if (!aSecondLayers || i >= aSecondLayers->mImageCount ||
916 (!aSecondLayers->mLayers[i].mImage.IsResolved() ||
917 image.GetImageRequest() !=
918 aSecondLayers->mLayers[i].mImage.GetImageRequest())) {
919 if (imgRequestProxy* req = image.GetImageRequest()) {
920 aCallback(req);
926 static void AddAndRemoveImageAssociations(
927 ImageLoader& aImageLoader, nsIFrame* aFrame,
928 const nsStyleImageLayers* aOldLayers,
929 const nsStyleImageLayers* aNewLayers) {
930 // If the old context had a background-image image, or mask-image image,
931 // and new context does not have the same image, clear the image load
932 // notifier (which keeps the image loading, if it still is) for the frame.
933 // We want to do this conservatively because some frames paint their
934 // backgrounds from some other frame's style data, and we don't want
935 // to clear those notifiers unless we have to. (They'll be reset
936 // when we paint, although we could miss a notification in that
937 // interval.)
938 if (aOldLayers && aFrame->HasImageRequest()) {
939 CompareLayers(aOldLayers, aNewLayers, [&](imgRequestProxy* aReq) {
940 aImageLoader.DisassociateRequestFromFrame(aReq, aFrame);
944 CompareLayers(aNewLayers, aOldLayers, [&](imgRequestProxy* aReq) {
945 aImageLoader.AssociateRequestToFrame(aReq, aFrame);
949 void nsIFrame::AddDisplayItem(nsDisplayItem* aItem) {
950 MOZ_DIAGNOSTIC_ASSERT(!mDisplayItems.Contains(aItem));
951 mDisplayItems.AppendElement(aItem);
952 #ifdef ACCESSIBILITY
953 if (nsAccessibilityService* accService = GetAccService()) {
954 accService->NotifyOfPossibleBoundsChange(PresShell(), mContent);
956 #endif
959 bool nsIFrame::RemoveDisplayItem(nsDisplayItem* aItem) {
960 return mDisplayItems.RemoveElement(aItem);
963 bool nsIFrame::HasDisplayItems() { return !mDisplayItems.IsEmpty(); }
965 bool nsIFrame::HasDisplayItem(nsDisplayItem* aItem) {
966 return mDisplayItems.Contains(aItem);
969 bool nsIFrame::HasDisplayItem(uint32_t aKey) {
970 for (nsDisplayItem* i : mDisplayItems) {
971 if (i->GetPerFrameKey() == aKey) {
972 return true;
975 return false;
978 template <typename Condition>
979 static void DiscardDisplayItems(nsIFrame* aFrame, Condition aCondition) {
980 for (nsDisplayItem* i : aFrame->DisplayItems()) {
981 // Only discard items that are invalidated by this frame, as we're only
982 // guaranteed to rebuild those items. Table background items are created by
983 // the relevant table part, but have the cell frame as the primary frame,
984 // and we don't want to remove them if this is the cell.
985 if (aCondition(i) && i->FrameForInvalidation() == aFrame) {
986 i->SetCantBeReused();
991 static void DiscardOldItems(nsIFrame* aFrame) {
992 DiscardDisplayItems(aFrame,
993 [](nsDisplayItem* aItem) { return aItem->IsOldItem(); });
996 void nsIFrame::RemoveDisplayItemDataForDeletion() {
997 // Destroying a WebRenderUserDataTable can cause destruction of other objects
998 // which can remove frame properties in their destructor. If we delete a frame
999 // property it runs the destructor of the stored object in the middle of
1000 // updating the frame property table, so if the destruction of that object
1001 // causes another update to the frame property table it would leave the frame
1002 // property table in an inconsistent state. So we remove it from the table and
1003 // then destroy it. (bug 1530657)
1004 WebRenderUserDataTable* userDataTable =
1005 TakeProperty(WebRenderUserDataProperty::Key());
1006 if (userDataTable) {
1007 for (const auto& data : userDataTable->Values()) {
1008 data->RemoveFromTable();
1010 delete userDataTable;
1013 if (!nsLayoutUtils::AreRetainedDisplayListsEnabled()) {
1014 // Retained display lists are disabled, no need to update
1015 // RetainedDisplayListData.
1016 return;
1019 auto* builder = nsLayoutUtils::GetRetainedDisplayListBuilder(this);
1020 if (!builder) {
1021 MOZ_ASSERT(DisplayItems().IsEmpty());
1022 MOZ_ASSERT(!IsFrameModified());
1023 return;
1026 for (nsDisplayItem* i : DisplayItems()) {
1027 if (i->GetDependentFrame() == this && !i->HasDeletedFrame()) {
1028 i->Frame()->MarkNeedsDisplayItemRebuild();
1030 i->RemoveFrame(this);
1033 DisplayItems().Clear();
1035 nsAutoString name;
1036 #ifdef DEBUG_FRAME_DUMP
1037 if (DL_LOG_TEST(LogLevel::Debug)) {
1038 GetFrameName(name);
1040 #endif
1041 DL_LOGV("Removing display item data for frame %p (%s)", this,
1042 NS_ConvertUTF16toUTF8(name).get());
1044 auto* data = builder->Data();
1045 if (MayHaveWillChangeBudget()) {
1046 // Keep the frame in list, so it can be removed from the will-change budget.
1047 data->Flags(this) = RetainedDisplayListData::FrameFlag::HadWillChange;
1048 } else {
1049 data->Remove(this);
1053 void nsIFrame::MarkNeedsDisplayItemRebuild() {
1054 if (!nsLayoutUtils::AreRetainedDisplayListsEnabled() || IsFrameModified() ||
1055 HasAnyStateBits(NS_FRAME_IN_POPUP)) {
1056 // Skip frames that are already marked modified.
1057 return;
1060 if (Type() == LayoutFrameType::Placeholder) {
1061 nsIFrame* oof = static_cast<nsPlaceholderFrame*>(this)->GetOutOfFlowFrame();
1062 if (oof) {
1063 oof->MarkNeedsDisplayItemRebuild();
1065 // Do not mark placeholder frames modified.
1066 return;
1069 #ifdef ACCESSIBILITY
1070 if (nsAccessibilityService* accService = GetAccService()) {
1071 accService->NotifyOfPossibleBoundsChange(PresShell(), mContent);
1073 #endif
1075 nsIFrame* rootFrame = PresShell()->GetRootFrame();
1077 if (rootFrame->IsFrameModified()) {
1078 // The whole frame tree is modified.
1079 return;
1082 auto* builder = nsLayoutUtils::GetRetainedDisplayListBuilder(this);
1083 if (!builder) {
1084 MOZ_ASSERT(DisplayItems().IsEmpty());
1085 return;
1088 RetainedDisplayListData* data = builder->Data();
1089 MOZ_ASSERT(data);
1091 if (data->AtModifiedFrameLimit()) {
1092 // This marks the whole frame tree modified.
1093 // See |RetainedDisplayListBuilder::ShouldBuildPartial()|.
1094 data->AddModifiedFrame(rootFrame);
1095 return;
1098 nsAutoString name;
1099 #ifdef DEBUG_FRAME_DUMP
1100 if (DL_LOG_TEST(LogLevel::Debug)) {
1101 GetFrameName(name);
1103 #endif
1105 DL_LOGV("RDL - Rebuilding display items for frame %p (%s)", this,
1106 NS_ConvertUTF16toUTF8(name).get());
1108 data->AddModifiedFrame(this);
1110 MOZ_ASSERT(
1111 PresContext()->LayoutPhaseCount(nsLayoutPhase::DisplayListBuilding) == 0);
1113 // Hopefully this is cheap, but we could use a frame state bit to note
1114 // the presence of dependencies to speed it up.
1115 for (nsDisplayItem* i : DisplayItems()) {
1116 if (i->HasDeletedFrame() || i->Frame() == this) {
1117 // Ignore the items with deleted frames, and the items with |this| as
1118 // the primary frame.
1119 continue;
1122 if (i->GetDependentFrame() == this) {
1123 // For items with |this| as a dependent frame, mark the primary frame
1124 // for rebuild.
1125 i->Frame()->MarkNeedsDisplayItemRebuild();
1130 // Subclass hook for style post processing
1131 /* virtual */
1132 void nsIFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
1133 #ifdef ACCESSIBILITY
1134 // Don't notify for reconstructed frames here, since the frame is still being
1135 // constructed at this point and so LocalAccessible::GetFrame() will return
1136 // null. Style changes for reconstructed frames are handled in
1137 // DocAccessible::PruneOrInsertSubtree.
1138 if (aOldComputedStyle) {
1139 if (nsAccessibilityService* accService = GetAccService()) {
1140 accService->NotifyOfComputedStyleChange(PresShell(), mContent);
1143 #endif
1145 MaybeScheduleReflowSVGNonDisplayText(this);
1147 Document* doc = PresContext()->Document();
1148 ImageLoader* loader = doc->StyleImageLoader();
1149 // Continuing text frame doesn't initialize its continuation pointer before
1150 // reaching here for the first time, so we have to exclude text frames. This
1151 // doesn't affect correctness because text can't match selectors.
1153 // FIXME(emilio): We should consider fixing that.
1155 // TODO(emilio): Can we avoid doing some / all of the image stuff when
1156 // isNonTextFirstContinuation is false? We should consider doing this just for
1157 // primary frames and pseudos, but the first-line reparenting code makes it
1158 // all bad, should get around to bug 1465474 eventually :(
1159 const bool isNonText = !IsTextFrame();
1160 if (isNonText) {
1161 mComputedStyle->StartImageLoads(*doc, aOldComputedStyle);
1164 const nsStyleImageLayers* oldLayers =
1165 aOldComputedStyle ? &aOldComputedStyle->StyleBackground()->mImage
1166 : nullptr;
1167 const nsStyleImageLayers* newLayers = &StyleBackground()->mImage;
1168 AddAndRemoveImageAssociations(*loader, this, oldLayers, newLayers);
1170 oldLayers =
1171 aOldComputedStyle ? &aOldComputedStyle->StyleSVGReset()->mMask : nullptr;
1172 newLayers = &StyleSVGReset()->mMask;
1173 AddAndRemoveImageAssociations(*loader, this, oldLayers, newLayers);
1175 const nsStyleDisplay* disp = StyleDisplay();
1176 bool handleStickyChange = false;
1177 if (aOldComputedStyle) {
1178 // Detect style changes that should trigger a scroll anchor adjustment
1179 // suppression.
1180 // https://drafts.csswg.org/css-scroll-anchoring/#suppression-triggers
1181 bool needAnchorSuppression = false;
1183 const nsStyleMargin* oldMargin = aOldComputedStyle->StyleMargin();
1184 if (oldMargin->mMargin != StyleMargin()->mMargin) {
1185 needAnchorSuppression = true;
1188 const nsStylePadding* oldPadding = aOldComputedStyle->StylePadding();
1189 if (oldPadding->mPadding != StylePadding()->mPadding) {
1190 SetHasPaddingChange(true);
1191 needAnchorSuppression = true;
1194 const nsStyleDisplay* oldDisp = aOldComputedStyle->StyleDisplay();
1195 if (oldDisp->mOverflowAnchor != disp->mOverflowAnchor) {
1196 if (auto* container = ScrollAnchorContainer::FindFor(this)) {
1197 container->InvalidateAnchor();
1199 if (nsIScrollableFrame* scrollableFrame = do_QueryFrame(this)) {
1200 scrollableFrame->Anchor()->InvalidateAnchor();
1204 if (mInScrollAnchorChain) {
1205 const nsStylePosition* pos = StylePosition();
1206 const nsStylePosition* oldPos = aOldComputedStyle->StylePosition();
1207 if (!needAnchorSuppression &&
1208 (oldPos->mOffset != pos->mOffset || oldPos->mWidth != pos->mWidth ||
1209 oldPos->mMinWidth != pos->mMinWidth ||
1210 oldPos->mMaxWidth != pos->mMaxWidth ||
1211 oldPos->mHeight != pos->mHeight ||
1212 oldPos->mMinHeight != pos->mMinHeight ||
1213 oldPos->mMaxHeight != pos->mMaxHeight ||
1214 oldDisp->mPosition != disp->mPosition ||
1215 oldDisp->mTransform != disp->mTransform)) {
1216 needAnchorSuppression = true;
1219 if (needAnchorSuppression &&
1220 StaticPrefs::layout_css_scroll_anchoring_suppressions_enabled()) {
1221 ScrollAnchorContainer::FindFor(this)->SuppressAdjustments();
1225 if (disp->mPosition != oldDisp->mPosition) {
1226 if (!disp->IsRelativelyOrStickyPositionedStyle() &&
1227 oldDisp->IsRelativelyOrStickyPositionedStyle()) {
1228 RemoveProperty(NormalPositionProperty());
1231 handleStickyChange = disp->mPosition == StylePositionProperty::Sticky ||
1232 oldDisp->mPosition == StylePositionProperty::Sticky;
1234 if (disp->mScrollSnapAlign != oldDisp->mScrollSnapAlign) {
1235 ScrollSnapUtils::PostPendingResnapFor(this);
1237 if (aOldComputedStyle->IsRootElementStyle() &&
1238 disp->mScrollSnapType != oldDisp->mScrollSnapType) {
1239 if (nsIScrollableFrame* scrollableFrame =
1240 PresShell()->GetRootScrollFrameAsScrollable()) {
1241 scrollableFrame->PostPendingResnap();
1244 if (StyleUIReset()->mMozSubtreeHiddenOnlyVisually &&
1245 !aOldComputedStyle->StyleUIReset()->mMozSubtreeHiddenOnlyVisually) {
1246 PresShell::ClearMouseCapture(this);
1248 } else { // !aOldComputedStyle
1249 handleStickyChange = disp->mPosition == StylePositionProperty::Sticky;
1252 if (handleStickyChange && !HasAnyStateBits(NS_FRAME_IS_NONDISPLAY) &&
1253 !GetPrevInFlow()) {
1254 // Note that we only add first continuations, but we really only
1255 // want to add first continuation-or-ib-split-siblings. But since we don't
1256 // yet know if we're a later part of a block-in-inline split, we'll just
1257 // add later members of a block-in-inline split here, and then
1258 // StickyScrollContainer will remove them later.
1259 if (auto* ssc =
1260 StickyScrollContainer::GetStickyScrollContainerForFrame(this)) {
1261 if (disp->mPosition == StylePositionProperty::Sticky) {
1262 ssc->AddFrame(this);
1263 } else {
1264 ssc->RemoveFrame(this);
1269 imgIRequest* oldBorderImage =
1270 aOldComputedStyle
1271 ? aOldComputedStyle->StyleBorder()->GetBorderImageRequest()
1272 : nullptr;
1273 imgIRequest* newBorderImage = StyleBorder()->GetBorderImageRequest();
1274 // FIXME (Bug 759996): The following is no longer true.
1275 // For border-images, we can't be as conservative (we need to set the
1276 // new loaders if there has been any change) since the CalcDifference
1277 // call depended on the result of GetComputedBorder() and that result
1278 // depends on whether the image has loaded, start the image load now
1279 // so that we'll get notified when it completes loading and can do a
1280 // restyle. Otherwise, the image might finish loading from the
1281 // network before we start listening to its notifications, and then
1282 // we'll never know that it's finished loading. Likewise, we want to
1283 // do this for freshly-created frames to prevent a similar race if the
1284 // image loads between reflow (which can depend on whether the image
1285 // is loaded) and paint. We also don't really care about any callers who try
1286 // to paint borders with a different style, because they won't have the
1287 // correct size for the border either.
1288 if (oldBorderImage != newBorderImage) {
1289 // stop and restart the image loading/notification
1290 if (oldBorderImage && HasImageRequest()) {
1291 loader->DisassociateRequestFromFrame(oldBorderImage, this);
1293 if (newBorderImage) {
1294 loader->AssociateRequestToFrame(newBorderImage, this);
1298 auto GetShapeImageRequest = [](const ComputedStyle* aStyle) -> imgIRequest* {
1299 if (!aStyle) {
1300 return nullptr;
1302 auto& shape = aStyle->StyleDisplay()->mShapeOutside;
1303 if (!shape.IsImage()) {
1304 return nullptr;
1306 return shape.AsImage().GetImageRequest();
1309 imgIRequest* oldShapeImage = GetShapeImageRequest(aOldComputedStyle);
1310 imgIRequest* newShapeImage = GetShapeImageRequest(Style());
1311 if (oldShapeImage != newShapeImage) {
1312 if (oldShapeImage && HasImageRequest()) {
1313 loader->DisassociateRequestFromFrame(oldShapeImage, this);
1315 if (newShapeImage) {
1316 loader->AssociateRequestToFrame(
1317 newShapeImage, this,
1318 ImageLoader::Flags::
1319 RequiresReflowOnFirstFrameCompleteAndLoadEventBlocking);
1323 // SVGObserverUtils::GetEffectProperties() asserts that we only invoke it with
1324 // the first continuation so we need to check that in advance.
1325 const bool isNonTextFirstContinuation = isNonText && !GetPrevContinuation();
1326 if (isNonTextFirstContinuation) {
1327 // Kick off loading of external SVG resources referenced from properties if
1328 // any. This currently includes filter, clip-path, and mask.
1329 SVGObserverUtils::InitiateResourceDocLoads(this);
1332 // If the page contains markup that overrides text direction, and
1333 // does not contain any characters that would activate the Unicode
1334 // bidi algorithm, we need to call |SetBidiEnabled| on the pres
1335 // context before reflow starts. See bug 115921.
1336 if (StyleVisibility()->mDirection == StyleDirection::Rtl) {
1337 PresContext()->SetBidiEnabled();
1340 // The following part is for caching offset-path:path(). We cache the
1341 // flatten gfx path, so we don't have to rebuild and re-flattern it at
1342 // each cycle if we have animations on offset-* with a fixed offset-path.
1343 const StyleOffsetPath* oldPath =
1344 aOldComputedStyle ? &aOldComputedStyle->StyleDisplay()->mOffsetPath
1345 : nullptr;
1346 const StyleOffsetPath& newPath = StyleDisplay()->mOffsetPath;
1347 if (!oldPath || *oldPath != newPath) {
1348 // FIXME: Bug 1837042. Cache all basic shapes.
1349 if (newPath.IsPath()) {
1350 RefPtr<gfx::PathBuilder> builder = MotionPathUtils::GetPathBuilder();
1351 RefPtr<gfx::Path> path =
1352 MotionPathUtils::BuildSVGPath(newPath.AsSVGPathData(), builder);
1353 if (path) {
1354 // The newPath could be path('') (i.e. empty path), so its gfx path
1355 // could be nullptr, and so we only set property for a non-empty path.
1356 SetProperty(nsIFrame::OffsetPathCache(), path.forget().take());
1357 } else {
1358 // May have an old cached path, so we have to delete it.
1359 RemoveProperty(nsIFrame::OffsetPathCache());
1361 } else if (oldPath) {
1362 RemoveProperty(nsIFrame::OffsetPathCache());
1366 if (IsPrimaryFrame()) {
1367 HandleLastRememberedSize();
1370 RemoveStateBits(NS_FRAME_SIMPLE_EVENT_REGIONS | NS_FRAME_SIMPLE_DISPLAYLIST);
1372 mMayHaveRoundedCorners = true;
1375 void nsIFrame::HandleLastRememberedSize() {
1376 MOZ_ASSERT(IsPrimaryFrame());
1377 // Storing a last remembered size requires contain-intrinsic-size, and using
1378 // a previously stored last remembered size requires content-visibility.
1379 if (!StaticPrefs::layout_css_contain_intrinsic_size_enabled() ||
1380 !StaticPrefs::layout_css_content_visibility_enabled()) {
1381 return;
1383 auto* element = Element::FromNodeOrNull(mContent);
1384 if (!element) {
1385 return;
1387 const WritingMode wm = GetWritingMode();
1388 const nsStylePosition* stylePos = StylePosition();
1389 bool canRememberBSize = stylePos->ContainIntrinsicBSize(wm).HasAuto();
1390 bool canRememberISize = stylePos->ContainIntrinsicISize(wm).HasAuto();
1391 if (!canRememberBSize) {
1392 element->RemoveLastRememberedBSize();
1394 if (!canRememberISize) {
1395 element->RemoveLastRememberedISize();
1397 if ((canRememberBSize || canRememberISize) && !HidesContent()) {
1398 bool isNonReplacedInline = IsLineParticipant() && !IsReplaced();
1399 if (!isNonReplacedInline) {
1400 PresContext()->Document()->ObserveForLastRememberedSize(*element);
1401 return;
1404 PresContext()->Document()->UnobserveForLastRememberedSize(*element);
1407 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
1408 void nsIFrame::AssertNewStyleIsSane(ComputedStyle& aNewStyle) {
1409 MOZ_DIAGNOSTIC_ASSERT(
1410 aNewStyle.GetPseudoType() == mComputedStyle->GetPseudoType() ||
1411 // ::first-line continuations are weird, this should probably be fixed via
1412 // bug 1465474.
1413 (mComputedStyle->GetPseudoType() == PseudoStyleType::firstLine &&
1414 aNewStyle.GetPseudoType() == PseudoStyleType::mozLineFrame) ||
1415 // ::first-letter continuations are broken, in particular floating ones,
1416 // see bug 1490281. The construction code tries to fix this up after the
1417 // fact, then restyling undoes it...
1418 (mComputedStyle->GetPseudoType() == PseudoStyleType::mozText &&
1419 aNewStyle.GetPseudoType() == PseudoStyleType::firstLetterContinuation) ||
1420 (mComputedStyle->GetPseudoType() ==
1421 PseudoStyleType::firstLetterContinuation &&
1422 aNewStyle.GetPseudoType() == PseudoStyleType::mozText));
1424 #endif
1426 void nsIFrame::ReparentFrameViewTo(nsViewManager* aViewManager,
1427 nsView* aNewParentView) {
1428 if (HasView()) {
1429 if (IsMenuPopupFrame()) {
1430 // This view must be parented by the root view, don't reparent it.
1431 return;
1433 nsView* view = GetView();
1434 aViewManager->RemoveChild(view);
1436 // The view will remember the Z-order and other attributes that have been
1437 // set on it.
1438 nsView* insertBefore =
1439 nsLayoutUtils::FindSiblingViewFor(aNewParentView, this);
1440 aViewManager->InsertChild(aNewParentView, view, insertBefore,
1441 insertBefore != nullptr);
1442 } else if (HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
1443 for (const auto& childList : ChildLists()) {
1444 // Iterate the child frames, and check each child frame to see if it has
1445 // a view
1446 for (nsIFrame* child : childList.mList) {
1447 child->ReparentFrameViewTo(aViewManager, aNewParentView);
1453 void nsIFrame::SyncFrameViewProperties(nsView* aView) {
1454 if (!aView) {
1455 aView = GetView();
1456 if (!aView) {
1457 return;
1461 nsViewManager* vm = aView->GetViewManager();
1463 // Make sure visibility is correct. This only affects nsSubDocumentFrame.
1464 if (!SupportsVisibilityHidden()) {
1465 // See if the view should be hidden or visible
1466 ComputedStyle* sc = Style();
1467 vm->SetViewVisibility(aView, sc->StyleVisibility()->IsVisible()
1468 ? ViewVisibility::Show
1469 : ViewVisibility::Hide);
1472 const auto zIndex = ZIndex();
1473 const bool autoZIndex = !zIndex;
1474 vm->SetViewZIndex(aView, autoZIndex, zIndex.valueOr(0));
1477 void nsIFrame::CreateView() {
1478 MOZ_ASSERT(!HasView());
1480 nsView* parentView = GetParent()->GetClosestView();
1481 MOZ_ASSERT(parentView, "no parent with view");
1483 nsViewManager* viewManager = parentView->GetViewManager();
1484 MOZ_ASSERT(viewManager, "null view manager");
1486 nsView* view = viewManager->CreateView(GetRect(), parentView);
1487 SyncFrameViewProperties(view);
1489 nsView* insertBefore = nsLayoutUtils::FindSiblingViewFor(parentView, this);
1490 // we insert this view 'above' the insertBefore view, unless insertBefore is
1491 // null, in which case we want to call with aAbove == false to insert at the
1492 // beginning in document order
1493 viewManager->InsertChild(parentView, view, insertBefore,
1494 insertBefore != nullptr);
1496 // REVIEW: Don't create a widget for fixed-pos elements anymore.
1497 // ComputeRepaintRegionForCopy will calculate the right area to repaint
1498 // when we scroll.
1499 // Reparent views on any child frames (or their descendants) to this
1500 // view. We can just call ReparentFrameViewTo on this frame because
1501 // we know this frame has no view, so it will crawl the children. Also,
1502 // we know that any descendants with views must have 'parentView' as their
1503 // parent view.
1504 ReparentFrameViewTo(viewManager, view);
1506 // Remember our view
1507 SetView(view);
1509 NS_FRAME_LOG(NS_FRAME_TRACE_CALLS,
1510 ("nsIFrame::CreateView: frame=%p view=%p", this, view));
1513 /* virtual */
1514 nsMargin nsIFrame::GetUsedMargin() const {
1515 nsMargin margin;
1516 if (((mState & NS_FRAME_FIRST_REFLOW) && !(mState & NS_FRAME_IN_REFLOW)) ||
1517 IsInSVGTextSubtree()) {
1518 return margin;
1521 if (nsMargin* m = GetProperty(UsedMarginProperty())) {
1522 margin = *m;
1523 } else if (!StyleMargin()->GetMargin(margin)) {
1524 // If we get here, our caller probably shouldn't be calling us...
1525 NS_ERROR(
1526 "Returning bogus 0-sized margin, because this margin "
1527 "depends on layout & isn't cached!");
1529 return margin;
1532 /* virtual */
1533 nsMargin nsIFrame::GetUsedBorder() const {
1534 if (((mState & NS_FRAME_FIRST_REFLOW) && !(mState & NS_FRAME_IN_REFLOW)) ||
1535 IsInSVGTextSubtree()) {
1536 return {};
1539 const nsStyleDisplay* disp = StyleDisplay();
1540 if (IsThemed(disp)) {
1541 // Theme methods don't use const-ness.
1542 auto* mutable_this = const_cast<nsIFrame*>(this);
1543 nsPresContext* pc = PresContext();
1544 LayoutDeviceIntMargin widgetBorder = pc->Theme()->GetWidgetBorder(
1545 pc->DeviceContext(), mutable_this, disp->EffectiveAppearance());
1546 return LayoutDevicePixel::ToAppUnits(widgetBorder,
1547 pc->AppUnitsPerDevPixel());
1550 return StyleBorder()->GetComputedBorder();
1553 /* virtual */
1554 nsMargin nsIFrame::GetUsedPadding() const {
1555 nsMargin padding;
1556 if (((mState & NS_FRAME_FIRST_REFLOW) && !(mState & NS_FRAME_IN_REFLOW)) ||
1557 IsInSVGTextSubtree()) {
1558 return padding;
1561 const nsStyleDisplay* disp = StyleDisplay();
1562 if (IsThemed(disp)) {
1563 // Theme methods don't use const-ness.
1564 nsIFrame* mutable_this = const_cast<nsIFrame*>(this);
1565 nsPresContext* pc = PresContext();
1566 LayoutDeviceIntMargin widgetPadding;
1567 if (pc->Theme()->GetWidgetPadding(pc->DeviceContext(), mutable_this,
1568 disp->EffectiveAppearance(),
1569 &widgetPadding)) {
1570 return LayoutDevicePixel::ToAppUnits(widgetPadding,
1571 pc->AppUnitsPerDevPixel());
1575 if (nsMargin* p = GetProperty(UsedPaddingProperty())) {
1576 padding = *p;
1577 } else if (!StylePadding()->GetPadding(padding)) {
1578 // If we get here, our caller probably shouldn't be calling us...
1579 NS_ERROR(
1580 "Returning bogus 0-sized padding, because this padding "
1581 "depends on layout & isn't cached!");
1583 return padding;
1586 nsIFrame::Sides nsIFrame::GetSkipSides() const {
1587 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
1588 StyleBoxDecorationBreak::Clone) &&
1589 !HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1590 return Sides();
1593 // Convert the logical skip sides to physical sides using the frame's
1594 // writing mode
1595 WritingMode writingMode = GetWritingMode();
1596 LogicalSides logicalSkip = GetLogicalSkipSides();
1597 Sides skip;
1599 if (logicalSkip.BStart()) {
1600 if (writingMode.IsVertical()) {
1601 skip |= writingMode.IsVerticalLR() ? SideBits::eLeft : SideBits::eRight;
1602 } else {
1603 skip |= SideBits::eTop;
1607 if (logicalSkip.BEnd()) {
1608 if (writingMode.IsVertical()) {
1609 skip |= writingMode.IsVerticalLR() ? SideBits::eRight : SideBits::eLeft;
1610 } else {
1611 skip |= SideBits::eBottom;
1615 if (logicalSkip.IStart()) {
1616 if (writingMode.IsVertical()) {
1617 skip |= SideBits::eTop;
1618 } else {
1619 skip |= writingMode.IsBidiLTR() ? SideBits::eLeft : SideBits::eRight;
1623 if (logicalSkip.IEnd()) {
1624 if (writingMode.IsVertical()) {
1625 skip |= SideBits::eBottom;
1626 } else {
1627 skip |= writingMode.IsBidiLTR() ? SideBits::eRight : SideBits::eLeft;
1630 return skip;
1633 nsRect nsIFrame::GetPaddingRectRelativeToSelf() const {
1634 nsMargin border = GetUsedBorder().ApplySkipSides(GetSkipSides());
1635 nsRect r(0, 0, mRect.width, mRect.height);
1636 r.Deflate(border);
1637 return r;
1640 nsRect nsIFrame::GetPaddingRect() const {
1641 return GetPaddingRectRelativeToSelf() + GetPosition();
1644 WritingMode nsIFrame::WritingModeForLine(WritingMode aSelfWM,
1645 nsIFrame* aSubFrame) const {
1646 MOZ_ASSERT(aSelfWM == GetWritingMode());
1647 WritingMode writingMode = aSelfWM;
1649 if (StyleTextReset()->mUnicodeBidi == StyleUnicodeBidi::Plaintext) {
1650 mozilla::intl::BidiEmbeddingLevel frameLevel =
1651 nsBidiPresUtils::GetFrameBaseLevel(aSubFrame);
1652 writingMode.SetDirectionFromBidiLevel(frameLevel);
1655 return writingMode;
1658 nsRect nsIFrame::GetMarginRect() const {
1659 return GetMarginRectRelativeToSelf() + GetPosition();
1662 nsRect nsIFrame::GetMarginRectRelativeToSelf() const {
1663 nsMargin m = GetUsedMargin().ApplySkipSides(GetSkipSides());
1664 nsRect r(0, 0, mRect.width, mRect.height);
1665 r.Inflate(m);
1666 return r;
1669 bool nsIFrame::IsTransformed() const {
1670 if (!HasAnyStateBits(NS_FRAME_MAY_BE_TRANSFORMED)) {
1671 MOZ_ASSERT(!IsCSSTransformed());
1672 MOZ_ASSERT(!IsSVGTransformed());
1673 return false;
1675 return IsCSSTransformed() || IsSVGTransformed();
1678 bool nsIFrame::IsCSSTransformed() const {
1679 return HasAnyStateBits(NS_FRAME_MAY_BE_TRANSFORMED) &&
1680 (StyleDisplay()->HasTransform(this) || HasAnimationOfTransform());
1683 bool nsIFrame::HasAnimationOfTransform() const {
1684 return IsPrimaryFrame() &&
1685 nsLayoutUtils::HasAnimationOfTransformAndMotionPath(this) &&
1686 SupportsCSSTransforms();
1689 bool nsIFrame::ChildrenHavePerspective(
1690 const nsStyleDisplay* aStyleDisplay) const {
1691 MOZ_ASSERT(aStyleDisplay == StyleDisplay());
1692 return aStyleDisplay->HasPerspective(this);
1695 bool nsIFrame::HasAnimationOfOpacity(EffectSet* aEffectSet) const {
1696 return ((nsLayoutUtils::IsPrimaryStyleFrame(this) ||
1697 nsLayoutUtils::FirstContinuationOrIBSplitSibling(this)
1698 ->IsPrimaryFrame()) &&
1699 nsLayoutUtils::HasAnimationOfPropertySet(
1700 this, nsCSSPropertyIDSet::OpacityProperties(), aEffectSet));
1703 bool nsIFrame::HasOpacityInternal(float aThreshold,
1704 const nsStyleDisplay* aStyleDisplay,
1705 const nsStyleEffects* aStyleEffects,
1706 EffectSet* aEffectSet) const {
1707 MOZ_ASSERT(0.0 <= aThreshold && aThreshold <= 1.0, "Invalid argument");
1708 if (aStyleEffects->mOpacity < aThreshold ||
1709 aStyleDisplay->mWillChange.bits & StyleWillChangeBits::OPACITY) {
1710 return true;
1713 if (!mMayHaveOpacityAnimation) {
1714 return false;
1717 return HasAnimationOfOpacity(aEffectSet);
1720 bool nsIFrame::IsSVGTransformed(gfx::Matrix* aOwnTransforms,
1721 gfx::Matrix* aFromParentTransforms) const {
1722 return false;
1725 bool nsIFrame::Extend3DContext(const nsStyleDisplay* aStyleDisplay,
1726 const nsStyleEffects* aStyleEffects,
1727 mozilla::EffectSet* aEffectSetForOpacity) const {
1728 if (!HasAnyStateBits(NS_FRAME_MAY_BE_TRANSFORMED)) {
1729 return false;
1731 const nsStyleDisplay* disp = StyleDisplayWithOptionalParam(aStyleDisplay);
1732 if (disp->mTransformStyle != StyleTransformStyle::Preserve3d ||
1733 !SupportsCSSTransforms()) {
1734 return false;
1737 // If we're all scroll frame, then all descendants will be clipped, so we
1738 // can't preserve 3d.
1739 if (IsScrollFrame()) {
1740 return false;
1743 const nsStyleEffects* effects = StyleEffectsWithOptionalParam(aStyleEffects);
1744 if (HasOpacity(disp, effects, aEffectSetForOpacity)) {
1745 return false;
1748 return ShouldApplyOverflowClipping(disp) == PhysicalAxes::None &&
1749 !GetClipPropClipRect(disp, effects, GetSize()) &&
1750 !SVGIntegrationUtils::UsingEffectsForFrame(this) &&
1751 !effects->HasMixBlendMode() &&
1752 disp->mIsolation != StyleIsolation::Isolate;
1755 bool nsIFrame::Combines3DTransformWithAncestors() const {
1756 // Check these first as they are faster then both calls below and are we are
1757 // likely to hit the early return (backface hidden is uncommon and
1758 // GetReferenceFrame is a hot caller of this which only calls this if
1759 // IsCSSTransformed is false).
1760 if (!IsCSSTransformed() && !BackfaceIsHidden()) {
1761 return false;
1763 nsIFrame* parent = GetClosestFlattenedTreeAncestorPrimaryFrame();
1764 return parent && parent->Extend3DContext();
1767 bool nsIFrame::In3DContextAndBackfaceIsHidden() const {
1768 // While both tests fail most of the time, test BackfaceIsHidden()
1769 // first since it's likely to fail faster.
1770 return BackfaceIsHidden() && Combines3DTransformWithAncestors();
1773 bool nsIFrame::HasPerspective() const {
1774 if (!IsCSSTransformed()) {
1775 return false;
1777 nsIFrame* parent = GetClosestFlattenedTreeAncestorPrimaryFrame();
1778 if (!parent) {
1779 return false;
1781 return parent->ChildrenHavePerspective();
1784 nsRect nsIFrame::GetContentRectRelativeToSelf() const {
1785 nsMargin bp = GetUsedBorderAndPadding().ApplySkipSides(GetSkipSides());
1786 nsRect r(0, 0, mRect.width, mRect.height);
1787 r.Deflate(bp);
1788 return r;
1791 nsRect nsIFrame::GetContentRect() const {
1792 return GetContentRectRelativeToSelf() + GetPosition();
1795 bool nsIFrame::ComputeBorderRadii(const BorderRadius& aBorderRadius,
1796 const nsSize& aFrameSize,
1797 const nsSize& aBorderArea, Sides aSkipSides,
1798 nscoord aRadii[8]) {
1799 // Percentages are relative to whichever side they're on.
1800 for (const auto i : mozilla::AllPhysicalHalfCorners()) {
1801 const LengthPercentage& c = aBorderRadius.Get(i);
1802 nscoord axis = HalfCornerIsX(i) ? aFrameSize.width : aFrameSize.height;
1803 aRadii[i] = std::max(0, c.Resolve(axis));
1806 if (aSkipSides.Top()) {
1807 aRadii[eCornerTopLeftX] = 0;
1808 aRadii[eCornerTopLeftY] = 0;
1809 aRadii[eCornerTopRightX] = 0;
1810 aRadii[eCornerTopRightY] = 0;
1813 if (aSkipSides.Right()) {
1814 aRadii[eCornerTopRightX] = 0;
1815 aRadii[eCornerTopRightY] = 0;
1816 aRadii[eCornerBottomRightX] = 0;
1817 aRadii[eCornerBottomRightY] = 0;
1820 if (aSkipSides.Bottom()) {
1821 aRadii[eCornerBottomRightX] = 0;
1822 aRadii[eCornerBottomRightY] = 0;
1823 aRadii[eCornerBottomLeftX] = 0;
1824 aRadii[eCornerBottomLeftY] = 0;
1827 if (aSkipSides.Left()) {
1828 aRadii[eCornerBottomLeftX] = 0;
1829 aRadii[eCornerBottomLeftY] = 0;
1830 aRadii[eCornerTopLeftX] = 0;
1831 aRadii[eCornerTopLeftY] = 0;
1834 // css3-background specifies this algorithm for reducing
1835 // corner radii when they are too big.
1836 bool haveRadius = false;
1837 double ratio = 1.0f;
1838 for (const auto side : mozilla::AllPhysicalSides()) {
1839 uint32_t hc1 = SideToHalfCorner(side, false, true);
1840 uint32_t hc2 = SideToHalfCorner(side, true, true);
1841 nscoord length =
1842 SideIsVertical(side) ? aBorderArea.height : aBorderArea.width;
1843 nscoord sum = aRadii[hc1] + aRadii[hc2];
1844 if (sum) {
1845 haveRadius = true;
1846 // avoid floating point division in the normal case
1847 if (length < sum) {
1848 ratio = std::min(ratio, double(length) / sum);
1852 if (ratio < 1.0) {
1853 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
1854 aRadii[corner] *= ratio;
1858 return haveRadius;
1861 void nsIFrame::AdjustBorderRadii(nscoord aRadii[8], const nsMargin& aOffsets) {
1862 auto AdjustOffset = [](const uint32_t aRadius, const nscoord aOffset) {
1863 // Implement the cubic formula to adjust offset when aOffset > 0 and
1864 // aRadius / aOffset < 1.
1865 // https://drafts.csswg.org/css-shapes/#valdef-shape-box-margin-box
1866 if (aOffset > 0) {
1867 const double ratio = aRadius / double(aOffset);
1868 if (ratio < 1.0) {
1869 return nscoord(aOffset * (1.0 + std::pow(ratio - 1, 3)));
1872 return aOffset;
1875 for (const auto side : mozilla::AllPhysicalSides()) {
1876 const nscoord offset = aOffsets.Side(side);
1877 const uint32_t hc1 = SideToHalfCorner(side, false, false);
1878 const uint32_t hc2 = SideToHalfCorner(side, true, false);
1879 if (aRadii[hc1] > 0) {
1880 const nscoord offset1 = AdjustOffset(aRadii[hc1], offset);
1881 aRadii[hc1] = std::max(0, aRadii[hc1] + offset1);
1883 if (aRadii[hc2] > 0) {
1884 const nscoord offset2 = AdjustOffset(aRadii[hc2], offset);
1885 aRadii[hc2] = std::max(0, aRadii[hc2] + offset2);
1890 static inline bool RadiiAreDefinitelyZero(const BorderRadius& aBorderRadius) {
1891 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
1892 if (!aBorderRadius.Get(corner).IsDefinitelyZero()) {
1893 return false;
1896 return true;
1899 /* virtual */
1900 bool nsIFrame::GetBorderRadii(const nsSize& aFrameSize,
1901 const nsSize& aBorderArea, Sides aSkipSides,
1902 nscoord aRadii[8]) const {
1903 if (!mMayHaveRoundedCorners) {
1904 memset(aRadii, 0, sizeof(nscoord) * 8);
1905 return false;
1908 if (IsThemed()) {
1909 // When we're themed, the native theme code draws the border and
1910 // background, and therefore it doesn't make sense to tell other
1911 // code that's interested in border-radius that we have any radii.
1913 // In an ideal world, we might have a way for the them to tell us an
1914 // border radius, but since we don't, we're better off assuming
1915 // zero.
1916 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
1917 aRadii[corner] = 0;
1919 return false;
1922 const auto& radii = StyleBorder()->mBorderRadius;
1923 const bool hasRadii =
1924 ComputeBorderRadii(radii, aFrameSize, aBorderArea, aSkipSides, aRadii);
1925 if (!hasRadii) {
1926 // TODO(emilio): Maybe we can just remove this bit and do the
1927 // IsDefinitelyZero check unconditionally. That should still avoid most of
1928 // the work, though maybe not the cache miss of going through the style and
1929 // the border struct.
1930 const_cast<nsIFrame*>(this)->mMayHaveRoundedCorners =
1931 !RadiiAreDefinitelyZero(radii);
1933 return hasRadii;
1936 bool nsIFrame::GetBorderRadii(nscoord aRadii[8]) const {
1937 nsSize sz = GetSize();
1938 return GetBorderRadii(sz, sz, GetSkipSides(), aRadii);
1941 bool nsIFrame::GetMarginBoxBorderRadii(nscoord aRadii[8]) const {
1942 return GetBoxBorderRadii(aRadii, GetUsedMargin());
1945 bool nsIFrame::GetPaddingBoxBorderRadii(nscoord aRadii[8]) const {
1946 return GetBoxBorderRadii(aRadii, -GetUsedBorder());
1949 bool nsIFrame::GetContentBoxBorderRadii(nscoord aRadii[8]) const {
1950 return GetBoxBorderRadii(aRadii, -GetUsedBorderAndPadding());
1953 bool nsIFrame::GetBoxBorderRadii(nscoord aRadii[8],
1954 const nsMargin& aOffsets) const {
1955 if (!GetBorderRadii(aRadii)) {
1956 return false;
1958 AdjustBorderRadii(aRadii, aOffsets);
1959 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
1960 if (aRadii[corner]) {
1961 return true;
1964 return false;
1967 bool nsIFrame::GetShapeBoxBorderRadii(nscoord aRadii[8]) const {
1968 using Tag = StyleShapeOutside::Tag;
1969 auto& shapeOutside = StyleDisplay()->mShapeOutside;
1970 auto box = StyleShapeBox::MarginBox;
1971 switch (shapeOutside.tag) {
1972 case Tag::Image:
1973 case Tag::None:
1974 return false;
1975 case Tag::Box:
1976 box = shapeOutside.AsBox();
1977 break;
1978 case Tag::Shape:
1979 box = shapeOutside.AsShape()._1;
1980 break;
1983 switch (box) {
1984 case StyleShapeBox::ContentBox:
1985 return GetContentBoxBorderRadii(aRadii);
1986 case StyleShapeBox::PaddingBox:
1987 return GetPaddingBoxBorderRadii(aRadii);
1988 case StyleShapeBox::BorderBox:
1989 return GetBorderRadii(aRadii);
1990 case StyleShapeBox::MarginBox:
1991 return GetMarginBoxBorderRadii(aRadii);
1992 default:
1993 MOZ_ASSERT_UNREACHABLE("Unexpected box value");
1994 return false;
1998 ComputedStyle* nsIFrame::GetAdditionalComputedStyle(int32_t aIndex) const {
1999 MOZ_ASSERT(aIndex >= 0, "invalid index number");
2000 return nullptr;
2003 void nsIFrame::SetAdditionalComputedStyle(int32_t aIndex,
2004 ComputedStyle* aComputedStyle) {
2005 MOZ_ASSERT(aIndex >= 0, "invalid index number");
2008 nscoord nsIFrame::SynthesizeFallbackBaseline(
2009 WritingMode aWM, BaselineSharingGroup aBaselineGroup) const {
2010 const auto margin = GetLogicalUsedMargin(aWM);
2011 NS_ASSERTION(!IsSubtreeDirty(), "frame must not be dirty");
2012 if (aWM.IsCentralBaseline()) {
2013 return (BSize(aWM) + GetLogicalUsedMargin(aWM).BEnd(aWM)) / 2;
2015 // Baseline for inverted line content is the top (block-start) margin edge,
2016 // as the frame is in effect "flipped" for alignment purposes.
2017 if (aWM.IsLineInverted()) {
2018 const auto marginStart = margin.BStart(aWM);
2019 return aBaselineGroup == BaselineSharingGroup::First
2020 ? -marginStart
2021 : BSize(aWM) + marginStart;
2023 // Otherwise, the bottom margin edge, per CSS2.1's definition of the
2024 // 'baseline' value of 'vertical-align'.
2025 const auto marginEnd = margin.BEnd(aWM);
2026 return aBaselineGroup == BaselineSharingGroup::First ? BSize(aWM) + marginEnd
2027 : -marginEnd;
2030 nscoord nsIFrame::GetLogicalBaseline(WritingMode aWM) const {
2031 return GetLogicalBaseline(aWM, GetDefaultBaselineSharingGroup(),
2032 BaselineExportContext::LineLayout);
2035 nscoord nsIFrame::GetLogicalBaseline(
2036 WritingMode aWM, BaselineSharingGroup aBaselineGroup,
2037 BaselineExportContext aExportContext) const {
2038 const auto result =
2039 GetNaturalBaselineBOffset(aWM, aBaselineGroup, aExportContext)
2040 .valueOrFrom([this, aWM, aBaselineGroup]() {
2041 return SynthesizeFallbackBaseline(aWM, aBaselineGroup);
2043 if (aBaselineGroup == BaselineSharingGroup::Last) {
2044 return BSize(aWM) - result;
2046 return result;
2049 const nsFrameList& nsIFrame::GetChildList(ChildListID aListID) const {
2050 if (IsAbsoluteContainer() && aListID == GetAbsoluteListID()) {
2051 return GetAbsoluteContainingBlock()->GetChildList();
2052 } else {
2053 return nsFrameList::EmptyList();
2057 void nsIFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
2058 if (IsAbsoluteContainer()) {
2059 const nsFrameList& absoluteList =
2060 GetAbsoluteContainingBlock()->GetChildList();
2061 absoluteList.AppendIfNonempty(aLists, GetAbsoluteListID());
2065 AutoTArray<nsIFrame::ChildList, 4> nsIFrame::CrossDocChildLists() {
2066 AutoTArray<ChildList, 4> childLists;
2067 nsSubDocumentFrame* subdocumentFrame = do_QueryFrame(this);
2068 if (subdocumentFrame) {
2069 // Descend into the subdocument
2070 nsIFrame* root = subdocumentFrame->GetSubdocumentRootFrame();
2071 if (root) {
2072 childLists.EmplaceBack(
2073 nsFrameList(root, nsLayoutUtils::GetLastSibling(root)),
2074 FrameChildListID::Principal);
2078 GetChildLists(&childLists);
2079 return childLists;
2082 nsIFrame::CaretBlockAxisMetrics nsIFrame::GetCaretBlockAxisMetrics(
2083 mozilla::WritingMode aWM, const nsFontMetrics& aFM) const {
2084 // Note(dshin): Ultimately, this does something highly similar (But still
2085 // different) to `nsLayoutUtils::GetFirstLinePosition`.
2086 const auto baseline = GetCaretBaseline();
2087 nscoord ascent = 0, descent = 0;
2088 ascent = aFM.MaxAscent();
2089 descent = aFM.MaxDescent();
2090 const nscoord height = ascent + descent;
2091 if (aWM.IsVertical() && aWM.IsLineInverted()) {
2092 return CaretBlockAxisMetrics{.mOffset = baseline - descent,
2093 .mExtent = height};
2095 return CaretBlockAxisMetrics{.mOffset = baseline - ascent, .mExtent = height};
2098 const nsAtom* nsIFrame::ComputePageValue() const {
2099 const nsAtom* value = nsGkAtoms::_empty;
2100 const nsIFrame* frame = this;
2101 // Find what CSS page name value this frame's subtree has, if any.
2102 // Starting with this frame, check if a page name other than auto is present,
2103 // and record it if so. Then, if the current frame is a container frame, find
2104 // the first non-placeholder child and repeat.
2105 // This will find the most deeply nested first in-flow child of this frame's
2106 // subtree, and return its page name (with auto resolved if applicable, and
2107 // subtrees with no page-names returning the empty atom rather than null).
2108 do {
2109 if (const nsAtom* maybePageName = frame->GetStylePageName()) {
2110 value = maybePageName;
2112 // Get the next frame to read from.
2113 const nsIFrame* firstNonPlaceholderFrame = nullptr;
2114 // If this is a container frame, inspect its in-flow children.
2115 if (const nsContainerFrame* containerFrame = do_QueryFrame(frame)) {
2116 for (const nsIFrame* childFrame : containerFrame->PrincipalChildList()) {
2117 if (!childFrame->IsPlaceholderFrame()) {
2118 firstNonPlaceholderFrame = childFrame;
2119 break;
2123 frame = firstNonPlaceholderFrame;
2124 } while (frame);
2125 return value;
2128 Visibility nsIFrame::GetVisibility() const {
2129 if (!HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED)) {
2130 return Visibility::Untracked;
2133 bool isSet = false;
2134 uint32_t visibleCount = GetProperty(VisibilityStateProperty(), &isSet);
2136 MOZ_ASSERT(isSet,
2137 "Should have a VisibilityStateProperty value "
2138 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2140 return visibleCount > 0 ? Visibility::ApproximatelyVisible
2141 : Visibility::ApproximatelyNonVisible;
2144 void nsIFrame::UpdateVisibilitySynchronously() {
2145 mozilla::PresShell* presShell = PresShell();
2146 if (!presShell) {
2147 return;
2150 if (presShell->AssumeAllFramesVisible()) {
2151 presShell->EnsureFrameInApproximatelyVisibleList(this);
2152 return;
2155 bool visible = StyleVisibility()->IsVisible();
2156 nsIFrame* f = GetParent();
2157 nsRect rect = GetRectRelativeToSelf();
2158 nsIFrame* rectFrame = this;
2159 while (f && visible) {
2160 nsIScrollableFrame* sf = do_QueryFrame(f);
2161 if (sf) {
2162 nsRect transformedRect =
2163 nsLayoutUtils::TransformFrameRectToAncestor(rectFrame, rect, f);
2164 if (!sf->IsRectNearlyVisible(transformedRect)) {
2165 visible = false;
2166 break;
2169 // In this code we're trying to synchronously update *approximate*
2170 // visibility. (In the future we may update precise visibility here as
2171 // well, which is why the method name does not contain 'approximate'.) The
2172 // IsRectNearlyVisible() check above tells us that the rect we're checking
2173 // is approximately visible within the scrollframe, but we still need to
2174 // ensure that, even if it was scrolled into view, it'd be visible when we
2175 // consider the rest of the document. To do that, we move transformedRect
2176 // to be contained in the scrollport as best we can (it might not fit) to
2177 // pretend that it was scrolled into view.
2178 rect = transformedRect.MoveInsideAndClamp(sf->GetScrollPortRect());
2179 rectFrame = f;
2181 nsIFrame* parent = f->GetParent();
2182 if (!parent) {
2183 parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(f);
2184 if (parent && parent->PresContext()->IsChrome()) {
2185 break;
2188 f = parent;
2191 if (visible) {
2192 presShell->EnsureFrameInApproximatelyVisibleList(this);
2193 } else {
2194 presShell->RemoveFrameFromApproximatelyVisibleList(this);
2198 void nsIFrame::EnableVisibilityTracking() {
2199 if (HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED)) {
2200 return; // Nothing to do.
2203 MOZ_ASSERT(!HasProperty(VisibilityStateProperty()),
2204 "Shouldn't have a VisibilityStateProperty value "
2205 "if NS_FRAME_VISIBILITY_IS_TRACKED is not set");
2207 // Add the state bit so we know to track visibility for this frame, and
2208 // initialize the frame property.
2209 AddStateBits(NS_FRAME_VISIBILITY_IS_TRACKED);
2210 SetProperty(VisibilityStateProperty(), 0);
2212 mozilla::PresShell* presShell = PresShell();
2213 if (!presShell) {
2214 return;
2217 // Schedule a visibility update. This method will virtually always be called
2218 // when layout has changed anyway, so it's very unlikely that any additional
2219 // visibility updates will be triggered by this, but this way we guarantee
2220 // that if this frame is currently visible we'll eventually find out.
2221 presShell->ScheduleApproximateFrameVisibilityUpdateSoon();
2224 void nsIFrame::DisableVisibilityTracking() {
2225 if (!HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED)) {
2226 return; // Nothing to do.
2229 bool isSet = false;
2230 uint32_t visibleCount = TakeProperty(VisibilityStateProperty(), &isSet);
2232 MOZ_ASSERT(isSet,
2233 "Should have a VisibilityStateProperty value "
2234 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2236 RemoveStateBits(NS_FRAME_VISIBILITY_IS_TRACKED);
2238 if (visibleCount == 0) {
2239 return; // We were nonvisible.
2242 // We were visible, so send an OnVisibilityChange() notification.
2243 OnVisibilityChange(Visibility::ApproximatelyNonVisible);
2246 void nsIFrame::DecApproximateVisibleCount(
2247 const Maybe<OnNonvisible>& aNonvisibleAction
2248 /* = Nothing() */) {
2249 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED));
2251 bool isSet = false;
2252 uint32_t visibleCount = GetProperty(VisibilityStateProperty(), &isSet);
2254 MOZ_ASSERT(isSet,
2255 "Should have a VisibilityStateProperty value "
2256 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2257 MOZ_ASSERT(visibleCount > 0,
2258 "Frame is already nonvisible and we're "
2259 "decrementing its visible count?");
2261 visibleCount--;
2262 SetProperty(VisibilityStateProperty(), visibleCount);
2263 if (visibleCount > 0) {
2264 return;
2267 // We just became nonvisible, so send an OnVisibilityChange() notification.
2268 OnVisibilityChange(Visibility::ApproximatelyNonVisible, aNonvisibleAction);
2271 void nsIFrame::IncApproximateVisibleCount() {
2272 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED));
2274 bool isSet = false;
2275 uint32_t visibleCount = GetProperty(VisibilityStateProperty(), &isSet);
2277 MOZ_ASSERT(isSet,
2278 "Should have a VisibilityStateProperty value "
2279 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2281 visibleCount++;
2282 SetProperty(VisibilityStateProperty(), visibleCount);
2283 if (visibleCount > 1) {
2284 return;
2287 // We just became visible, so send an OnVisibilityChange() notification.
2288 OnVisibilityChange(Visibility::ApproximatelyVisible);
2291 void nsIFrame::OnVisibilityChange(Visibility aNewVisibility,
2292 const Maybe<OnNonvisible>& aNonvisibleAction
2293 /* = Nothing() */) {
2294 // XXX(seth): In bug 1218990 we'll implement visibility tracking for CSS
2295 // images here.
2298 static nsIFrame* GetActiveSelectionFrame(nsPresContext* aPresContext,
2299 nsIFrame* aFrame) {
2300 nsIContent* capturingContent = PresShell::GetCapturingContent();
2301 if (capturingContent) {
2302 nsIFrame* activeFrame = aPresContext->GetPrimaryFrameFor(capturingContent);
2303 return activeFrame ? activeFrame : aFrame;
2306 return aFrame;
2309 int16_t nsIFrame::DetermineDisplaySelection() {
2310 int16_t selType = nsISelectionController::SELECTION_OFF;
2312 nsCOMPtr<nsISelectionController> selCon;
2313 nsresult result =
2314 GetSelectionController(PresContext(), getter_AddRefs(selCon));
2315 if (NS_SUCCEEDED(result) && selCon) {
2316 result = selCon->GetDisplaySelection(&selType);
2317 if (NS_SUCCEEDED(result) &&
2318 (selType != nsISelectionController::SELECTION_OFF)) {
2319 // Check whether style allows selection.
2320 if (!IsSelectable(nullptr)) {
2321 selType = nsISelectionController::SELECTION_OFF;
2325 return selType;
2328 static Element* FindElementAncestorForMozSelection(nsIContent* aContent) {
2329 NS_ENSURE_TRUE(aContent, nullptr);
2330 while (aContent && aContent->IsInNativeAnonymousSubtree()) {
2331 aContent = aContent->GetClosestNativeAnonymousSubtreeRootParentOrHost();
2333 NS_ASSERTION(aContent, "aContent isn't in non-anonymous tree?");
2334 return aContent ? aContent->GetAsElementOrParentElement() : nullptr;
2337 already_AddRefed<ComputedStyle> nsIFrame::ComputeSelectionStyle(
2338 int16_t aSelectionStatus) const {
2339 // Just bail out if not a selection-status that ::selection applies to.
2340 if (aSelectionStatus != nsISelectionController::SELECTION_ON &&
2341 aSelectionStatus != nsISelectionController::SELECTION_DISABLED) {
2342 return nullptr;
2344 Element* element = FindElementAncestorForMozSelection(GetContent());
2345 if (!element) {
2346 return nullptr;
2348 RefPtr<ComputedStyle> pseudoStyle =
2349 PresContext()->StyleSet()->ProbePseudoElementStyle(
2350 *element, PseudoStyleType::selection, nullptr, Style());
2351 if (!pseudoStyle) {
2352 return nullptr;
2354 // When in high-contrast mode, the style system ends up ignoring the color
2355 // declarations, which means that the ::selection style becomes the inherited
2356 // color, and default background. That's no good.
2357 // When force-color-adjust is set to none allow using the color styles,
2358 // as they will not be replaced.
2359 if (PresContext()->ForcingColors() &&
2360 pseudoStyle->StyleText()->mForcedColorAdjust !=
2361 StyleForcedColorAdjust::None) {
2362 return nullptr;
2364 return do_AddRef(pseudoStyle);
2367 already_AddRefed<ComputedStyle> nsIFrame::ComputeHighlightSelectionStyle(
2368 nsAtom* aHighlightName) {
2369 Element* element = FindElementAncestorForMozSelection(GetContent());
2370 if (!element) {
2371 return nullptr;
2373 return PresContext()->StyleSet()->ProbePseudoElementStyle(
2374 *element, PseudoStyleType::highlight, aHighlightName, Style());
2377 template <typename SizeOrMaxSize>
2378 static inline bool IsIntrinsicKeyword(const SizeOrMaxSize& aSize) {
2379 // All keywords other than auto/none/-moz-available depend on intrinsic sizes.
2380 return aSize.IsMaxContent() || aSize.IsMinContent() || aSize.IsFitContent() ||
2381 aSize.IsFitContentFunction();
2384 bool nsIFrame::CanBeDynamicReflowRoot() const {
2385 const auto& display = *StyleDisplay();
2386 if (IsLineParticipant() || display.mDisplay.IsRuby() ||
2387 display.IsInnerTableStyle() ||
2388 display.DisplayInside() == StyleDisplayInside::Table) {
2389 // We have a display type where 'width' and 'height' don't actually set the
2390 // width or height (i.e., the size depends on content).
2391 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_DYNAMIC_REFLOW_ROOT),
2392 "should not have dynamic reflow root bit");
2393 return false;
2396 // In general, frames that have contain:layout+size can be reflow roots.
2397 // (One exception: table-wrapper frames don't work well as reflow roots,
2398 // because their inner-table ReflowInput init path tries to reuse & deref
2399 // the wrapper's containing block's reflow input, which may be null if we
2400 // initiate reflow from the table-wrapper itself.)
2402 // Changes to `contain` force frame reconstructions, so we used to use
2403 // NS_FRAME_REFLOW_ROOT, this bit could be set for the whole lifetime of
2404 // this frame. But after the support of `content-visibility: auto` which
2405 // is with contain layout + size when it's not relevant to user, and only
2406 // with contain layout when it is relevant. The frame does not reconstruct
2407 // when the relevancy changes. So we use NS_FRAME_DYNAMIC_REFLOW_ROOT instead.
2409 // We place it above the pref check on purpose, to make sure it works for
2410 // containment even with the pref disabled.
2411 if (display.IsContainLayout() && GetContainSizeAxes().IsBoth()) {
2412 return true;
2415 if (!StaticPrefs::layout_dynamic_reflow_roots_enabled()) {
2416 return false;
2419 // We can't serve as a dynamic reflow root if our used 'width' and 'height'
2420 // might be influenced by content.
2422 // FIXME: For display:block, we should probably optimize inline-size: auto.
2423 // FIXME: Other flex and grid cases?
2424 const auto& pos = *StylePosition();
2425 const auto& width = pos.mWidth;
2426 const auto& height = pos.mHeight;
2427 if (!width.IsLengthPercentage() || width.HasPercent() ||
2428 !height.IsLengthPercentage() || height.HasPercent() ||
2429 IsIntrinsicKeyword(pos.mMinWidth) || IsIntrinsicKeyword(pos.mMaxWidth) ||
2430 IsIntrinsicKeyword(pos.mMinHeight) ||
2431 IsIntrinsicKeyword(pos.mMaxHeight) ||
2432 ((pos.mMinWidth.IsAuto() || pos.mMinHeight.IsAuto()) &&
2433 IsFlexOrGridItem())) {
2434 return false;
2437 // If our flex-basis is 'auto', it'll defer to 'width' (or 'height') which
2438 // we've already checked. Otherwise, it preempts them, so we need to
2439 // perform the same "could-this-value-be-influenced-by-content" checks that
2440 // we performed for 'width' and 'height' above.
2441 if (IsFlexItem()) {
2442 const auto& flexBasis = pos.mFlexBasis;
2443 if (!flexBasis.IsAuto()) {
2444 if (!flexBasis.IsSize() || !flexBasis.AsSize().IsLengthPercentage() ||
2445 flexBasis.AsSize().HasPercent()) {
2446 return false;
2451 if (!IsFixedPosContainingBlock()) {
2452 // We can't treat this frame as a reflow root, since dynamic changes
2453 // to absolutely-positioned frames inside of it require that we
2454 // reflow the placeholder before we reflow the absolutely positioned
2455 // frame.
2456 // FIXME: Alternatively, we could sort the reflow roots in
2457 // PresShell::ProcessReflowCommands by depth in the tree, from
2458 // deepest to least deep. However, for performance (FIXME) we
2459 // should really be sorting them in the opposite order!
2460 return false;
2463 // If we participate in a container's block reflow context, or margins
2464 // can collapse through us, we can't be a dynamic reflow root.
2465 if (IsBlockFrameOrSubclass() &&
2466 !HasAllStateBits(NS_BLOCK_FLOAT_MGR | NS_BLOCK_MARGIN_ROOT)) {
2467 return false;
2470 // Subgrids are never reflow roots, but 'contain:layout/paint' prevents
2471 // creating a subgrid in the first place.
2472 if (pos.mGridTemplateColumns.IsSubgrid() ||
2473 pos.mGridTemplateRows.IsSubgrid()) {
2474 // NOTE: we could check that 'display' of our parent's primary frame is
2475 // '[inline-]grid' here but that's probably not worth it in practice.
2476 if (!display.IsContainLayout() && !display.IsContainPaint()) {
2477 return false;
2481 // If we are split, we can't be a dynamic reflow root. Our reflow status may
2482 // change after reflow, and our parent is responsible to create or delete our
2483 // next-in-flow.
2484 if (GetPrevContinuation() || GetNextContinuation()) {
2485 return false;
2488 return true;
2491 /********************************************************
2492 * Refreshes each content's frame
2493 *********************************************************/
2495 void nsIFrame::DisplayOutlineUnconditional(nsDisplayListBuilder* aBuilder,
2496 const nsDisplayListSet& aLists) {
2497 // Per https://drafts.csswg.org/css-tables-3/#global-style-overrides:
2498 // "All css properties of table-column and table-column-group boxes are
2499 // ignored, except when explicitly specified by this specification."
2500 // CSS outlines fall into this category, so we skip them on these boxes.
2501 MOZ_ASSERT(!IsTableColGroupFrame() && !IsTableColFrame());
2502 const auto& outline = *StyleOutline();
2504 if (!outline.ShouldPaintOutline()) {
2505 return;
2508 // Outlines are painted by the table wrapper frame.
2509 if (IsTableFrame()) {
2510 return;
2513 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT) &&
2514 ScrollableOverflowRect().IsEmpty()) {
2515 // Skip parts of IB-splits with an empty overflow rect, see bug 434301.
2516 // We may still want to fix some of the overflow area calculations over in
2517 // that bug.
2518 return;
2521 // We don't display outline-style: auto on themed frames that have their own
2522 // focus indicators.
2523 if (outline.mOutlineStyle.IsAuto()) {
2524 auto* disp = StyleDisplay();
2525 if (IsThemed(disp) && PresContext()->Theme()->ThemeDrawsFocusForWidget(
2526 this, disp->EffectiveAppearance())) {
2527 return;
2531 aLists.Outlines()->AppendNewToTop<nsDisplayOutline>(aBuilder, this);
2534 void nsIFrame::DisplayOutline(nsDisplayListBuilder* aBuilder,
2535 const nsDisplayListSet& aLists) {
2536 if (!IsVisibleForPainting()) return;
2538 DisplayOutlineUnconditional(aBuilder, aLists);
2541 void nsIFrame::DisplayInsetBoxShadowUnconditional(
2542 nsDisplayListBuilder* aBuilder, nsDisplayList* aList) {
2543 // XXXbz should box-shadow for rows/rowgroups/columns/colgroups get painted
2544 // just because we're visible? Or should it depend on the cell visibility
2545 // when we're not the whole table?
2546 const auto* effects = StyleEffects();
2547 if (effects->HasBoxShadowWithInset(true)) {
2548 aList->AppendNewToTop<nsDisplayBoxShadowInner>(aBuilder, this);
2552 void nsIFrame::DisplayInsetBoxShadow(nsDisplayListBuilder* aBuilder,
2553 nsDisplayList* aList) {
2554 if (!IsVisibleForPainting()) return;
2556 DisplayInsetBoxShadowUnconditional(aBuilder, aList);
2559 void nsIFrame::DisplayOutsetBoxShadowUnconditional(
2560 nsDisplayListBuilder* aBuilder, nsDisplayList* aList) {
2561 // XXXbz should box-shadow for rows/rowgroups/columns/colgroups get painted
2562 // just because we're visible? Or should it depend on the cell visibility
2563 // when we're not the whole table?
2564 const auto* effects = StyleEffects();
2565 if (effects->HasBoxShadowWithInset(false)) {
2566 aList->AppendNewToTop<nsDisplayBoxShadowOuter>(aBuilder, this);
2570 void nsIFrame::DisplayOutsetBoxShadow(nsDisplayListBuilder* aBuilder,
2571 nsDisplayList* aList) {
2572 if (!IsVisibleForPainting()) return;
2574 DisplayOutsetBoxShadowUnconditional(aBuilder, aList);
2577 void nsIFrame::DisplayCaret(nsDisplayListBuilder* aBuilder,
2578 nsDisplayList* aList) {
2579 if (!IsVisibleForPainting()) return;
2581 aList->AppendNewToTop<nsDisplayCaret>(aBuilder, this);
2584 nscolor nsIFrame::GetCaretColorAt(int32_t aOffset) {
2585 return nsLayoutUtils::GetColor(this, &nsStyleUI::mCaretColor);
2588 auto nsIFrame::ComputeShouldPaintBackground() const -> ShouldPaintBackground {
2589 nsPresContext* pc = PresContext();
2590 ShouldPaintBackground settings{pc->GetBackgroundColorDraw(),
2591 pc->GetBackgroundImageDraw()};
2592 if (settings.mColor && settings.mImage) {
2593 return settings;
2596 if (StyleVisibility()->mPrintColorAdjust == StylePrintColorAdjust::Exact) {
2597 return {true, true};
2600 return settings;
2603 bool nsIFrame::DisplayBackgroundUnconditional(nsDisplayListBuilder* aBuilder,
2604 const nsDisplayListSet& aLists) {
2605 if (aBuilder->IsForEventDelivery() && !aBuilder->HitTestIsForVisibility()) {
2606 // For hit-testing, we generally just need a light-weight data structure
2607 // like nsDisplayEventReceiver. But if the hit-testing is for visibility,
2608 // then we need to know the opaque region in order to determine whether to
2609 // stop or not.
2610 aLists.BorderBackground()->AppendNewToTop<nsDisplayEventReceiver>(aBuilder,
2611 this);
2612 return false;
2615 const AppendedBackgroundType result =
2616 nsDisplayBackgroundImage::AppendBackgroundItemsToTop(
2617 aBuilder, this,
2618 GetRectRelativeToSelf() + aBuilder->ToReferenceFrame(this),
2619 aLists.BorderBackground());
2621 if (result == AppendedBackgroundType::None) {
2622 aBuilder->BuildCompositorHitTestInfoIfNeeded(this,
2623 aLists.BorderBackground());
2626 return result == AppendedBackgroundType::ThemedBackground;
2629 void nsIFrame::DisplayBorderBackgroundOutline(nsDisplayListBuilder* aBuilder,
2630 const nsDisplayListSet& aLists) {
2631 // The visibility check belongs here since child elements have the
2632 // opportunity to override the visibility property and display even if
2633 // their parent is hidden.
2634 if (!IsVisibleForPainting()) {
2635 return;
2638 DisplayOutsetBoxShadowUnconditional(aBuilder, aLists.BorderBackground());
2640 bool bgIsThemed = DisplayBackgroundUnconditional(aBuilder, aLists);
2641 DisplayInsetBoxShadowUnconditional(aBuilder, aLists.BorderBackground());
2643 // If there's a themed background, we should not create a border item.
2644 // It won't be rendered.
2645 // Don't paint borders for tables here, since they paint them in a different
2646 // order.
2647 if (!bgIsThemed && StyleBorder()->HasBorder() && !IsTableFrame()) {
2648 aLists.BorderBackground()->AppendNewToTop<nsDisplayBorder>(aBuilder, this);
2651 DisplayOutlineUnconditional(aBuilder, aLists);
2654 inline static bool IsSVGContentWithCSSClip(const nsIFrame* aFrame) {
2655 // The CSS spec says that the 'clip' property only applies to absolutely
2656 // positioned elements, whereas the SVG spec says that it applies to SVG
2657 // elements regardless of the value of the 'position' property. Here we obey
2658 // the CSS spec for outer-<svg> (since that's what we generally do), but
2659 // obey the SVG spec for other SVG elements to which 'clip' applies.
2660 return aFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT) &&
2661 aFrame->GetContent()->IsAnyOfSVGElements(nsGkAtoms::svg,
2662 nsGkAtoms::foreignObject);
2665 Maybe<nsRect> nsIFrame::GetClipPropClipRect(const nsStyleDisplay* aDisp,
2666 const nsStyleEffects* aEffects,
2667 const nsSize& aSize) const {
2668 if (aEffects->mClip.IsAuto() ||
2669 !(aDisp->IsAbsolutelyPositioned(this) || IsSVGContentWithCSSClip(this))) {
2670 return Nothing();
2673 auto& clipRect = aEffects->mClip.AsRect();
2674 nsRect rect = clipRect.ToLayoutRect();
2675 if (MOZ_LIKELY(StyleBorder()->mBoxDecorationBreak ==
2676 StyleBoxDecorationBreak::Slice)) {
2677 // The clip applies to the joined boxes so it's relative the first
2678 // continuation.
2679 nscoord y = 0;
2680 for (nsIFrame* f = GetPrevContinuation(); f; f = f->GetPrevContinuation()) {
2681 y += f->GetRect().height;
2683 rect.MoveBy(nsPoint(0, -y));
2686 if (clipRect.right.IsAuto()) {
2687 rect.width = aSize.width - rect.x;
2689 if (clipRect.bottom.IsAuto()) {
2690 rect.height = aSize.height - rect.y;
2692 return Some(rect);
2696 * If the CSS 'overflow' property applies to this frame, and is not
2697 * handled by constructing a dedicated nsHTML/XULScrollFrame, set up clipping
2698 * for that overflow in aBuilder->ClipState() to clip all containing-block
2699 * descendants.
2701 static void ApplyOverflowClipping(
2702 nsDisplayListBuilder* aBuilder, const nsIFrame* aFrame,
2703 nsIFrame::PhysicalAxes aClipAxes,
2704 DisplayListClipState::AutoClipMultiple& aClipState) {
2705 // Only 'clip' is handled here (and 'hidden' for table frames, and any
2706 // non-'visible' value for blocks in a paginated context).
2707 // We allow 'clip' to apply to any kind of frame. This is required by
2708 // comboboxes which make their display text (an inline frame) have clipping.
2709 MOZ_ASSERT(aClipAxes != nsIFrame::PhysicalAxes::None);
2710 MOZ_ASSERT(aFrame->ShouldApplyOverflowClipping(aFrame->StyleDisplay()) ==
2711 aClipAxes);
2713 nsRect clipRect;
2714 bool haveRadii = false;
2715 nscoord radii[8];
2716 auto* disp = aFrame->StyleDisplay();
2717 // Only deflate the padding if we clip to the content-box in that axis.
2718 auto wm = aFrame->GetWritingMode();
2719 bool cbH = (wm.IsVertical() ? disp->mOverflowClipBoxBlock
2720 : disp->mOverflowClipBoxInline) ==
2721 StyleOverflowClipBox::ContentBox;
2722 bool cbV = (wm.IsVertical() ? disp->mOverflowClipBoxInline
2723 : disp->mOverflowClipBoxBlock) ==
2724 StyleOverflowClipBox::ContentBox;
2726 nsMargin boxMargin = -aFrame->GetUsedPadding();
2727 if (!cbH) {
2728 boxMargin.left = boxMargin.right = nscoord(0);
2730 if (!cbV) {
2731 boxMargin.top = boxMargin.bottom = nscoord(0);
2734 auto clipMargin = aFrame->OverflowClipMargin(aClipAxes);
2736 boxMargin -= aFrame->GetUsedBorder();
2737 boxMargin += nsMargin(clipMargin.height, clipMargin.width, clipMargin.height,
2738 clipMargin.width);
2739 boxMargin.ApplySkipSides(aFrame->GetSkipSides());
2741 nsRect rect(nsPoint(0, 0), aFrame->GetSize());
2742 rect.Inflate(boxMargin);
2743 if (MOZ_UNLIKELY(!(aClipAxes & nsIFrame::PhysicalAxes::Horizontal))) {
2744 // NOTE(mats) We shouldn't be clipping at all in this dimension really,
2745 // but clipping in just one axis isn't supported by our GFX APIs so we
2746 // clip to our visual overflow rect instead.
2747 nsRect o = aFrame->InkOverflowRect();
2748 rect.x = o.x;
2749 rect.width = o.width;
2751 if (MOZ_UNLIKELY(!(aClipAxes & nsIFrame::PhysicalAxes::Vertical))) {
2752 // See the note above.
2753 nsRect o = aFrame->InkOverflowRect();
2754 rect.y = o.y;
2755 rect.height = o.height;
2757 clipRect = rect + aBuilder->ToReferenceFrame(aFrame);
2758 haveRadii = aFrame->GetBoxBorderRadii(radii, boxMargin);
2759 aClipState.ClipContainingBlockDescendantsExtra(clipRect,
2760 haveRadii ? radii : nullptr);
2763 nsSize nsIFrame::OverflowClipMargin(PhysicalAxes aClipAxes) const {
2764 nsSize result;
2765 if (aClipAxes == PhysicalAxes::None) {
2766 return result;
2768 const auto& margin = StyleMargin()->mOverflowClipMargin;
2769 if (margin.IsZero()) {
2770 return result;
2772 nscoord marginAu = margin.ToAppUnits();
2773 if (aClipAxes & PhysicalAxes::Horizontal) {
2774 result.width = marginAu;
2776 if (aClipAxes & PhysicalAxes::Vertical) {
2777 result.height = marginAu;
2779 return result;
2783 * Returns whether a display item that gets created with the builder's current
2784 * state will have a scrolled clip, i.e. a clip that is scrolled by a scroll
2785 * frame which does not move the item itself.
2787 static bool BuilderHasScrolledClip(nsDisplayListBuilder* aBuilder) {
2788 const DisplayItemClipChain* currentClip =
2789 aBuilder->ClipState().GetCurrentCombinedClipChain(aBuilder);
2790 if (!currentClip) {
2791 return false;
2794 const ActiveScrolledRoot* currentClipASR = currentClip->mASR;
2795 const ActiveScrolledRoot* currentASR = aBuilder->CurrentActiveScrolledRoot();
2796 return ActiveScrolledRoot::PickDescendant(currentClipASR, currentASR) !=
2797 currentASR;
2800 class AutoSaveRestoreContainsBlendMode {
2801 nsDisplayListBuilder& mBuilder;
2802 bool mSavedContainsBlendMode;
2804 public:
2805 explicit AutoSaveRestoreContainsBlendMode(nsDisplayListBuilder& aBuilder)
2806 : mBuilder(aBuilder),
2807 mSavedContainsBlendMode(aBuilder.ContainsBlendMode()) {}
2809 ~AutoSaveRestoreContainsBlendMode() {
2810 mBuilder.SetContainsBlendMode(mSavedContainsBlendMode);
2814 static bool IsFrameOrAncestorApzAware(nsIFrame* aFrame) {
2815 nsIContent* node = aFrame->GetContent();
2816 if (!node) {
2817 return false;
2820 do {
2821 if (node->IsNodeApzAware()) {
2822 return true;
2824 nsIContent* shadowRoot = node->GetShadowRoot();
2825 if (shadowRoot && shadowRoot->IsNodeApzAware()) {
2826 return true;
2829 // Even if the node owning aFrame doesn't have apz-aware event listeners
2830 // itself, its shadow root or display: contents ancestors (which have no
2831 // frames) might, so we need to account for them too.
2832 } while ((node = node->GetFlattenedTreeParent()) && node->IsElement() &&
2833 node->AsElement()->IsDisplayContents());
2835 return false;
2838 static void CheckForApzAwareEventHandlers(nsDisplayListBuilder* aBuilder,
2839 nsIFrame* aFrame) {
2840 if (aBuilder->GetAncestorHasApzAwareEventHandler()) {
2841 return;
2844 if (IsFrameOrAncestorApzAware(aFrame)) {
2845 aBuilder->SetAncestorHasApzAwareEventHandler(true);
2849 static void UpdateCurrentHitTestInfo(nsDisplayListBuilder* aBuilder,
2850 nsIFrame* aFrame) {
2851 if (!aBuilder->BuildCompositorHitTestInfo()) {
2852 // Compositor hit test info is not used.
2853 return;
2856 CheckForApzAwareEventHandlers(aBuilder, aFrame);
2858 const CompositorHitTestInfo info = aFrame->GetCompositorHitTestInfo(aBuilder);
2859 aBuilder->SetCompositorHitTestInfo(info);
2863 * True if aDescendant participates the context aAncestor participating.
2865 static bool FrameParticipatesIn3DContext(nsIFrame* aAncestor,
2866 nsIFrame* aDescendant) {
2867 MOZ_ASSERT(aAncestor != aDescendant);
2868 MOZ_ASSERT(aAncestor->GetContent() != aDescendant->GetContent());
2869 MOZ_ASSERT(aAncestor->Extend3DContext());
2871 nsIFrame* ancestor = aAncestor->FirstContinuation();
2872 MOZ_ASSERT(ancestor->IsPrimaryFrame());
2874 nsIFrame* frame;
2875 for (frame = aDescendant->GetClosestFlattenedTreeAncestorPrimaryFrame();
2876 frame && ancestor != frame;
2877 frame = frame->GetClosestFlattenedTreeAncestorPrimaryFrame()) {
2878 if (!frame->Extend3DContext()) {
2879 return false;
2883 MOZ_ASSERT(frame == ancestor);
2884 return true;
2887 static bool ItemParticipatesIn3DContext(nsIFrame* aAncestor,
2888 nsDisplayItem* aItem) {
2889 auto type = aItem->GetType();
2890 const bool isContainer = type == DisplayItemType::TYPE_WRAP_LIST ||
2891 type == DisplayItemType::TYPE_CONTAINER;
2893 if (isContainer && aItem->GetChildren()->Length() == 1) {
2894 // If the wraplist has only one child item, use the type of that item.
2895 type = aItem->GetChildren()->GetBottom()->GetType();
2898 if (type != DisplayItemType::TYPE_TRANSFORM &&
2899 type != DisplayItemType::TYPE_PERSPECTIVE) {
2900 return false;
2902 nsIFrame* transformFrame = aItem->Frame();
2903 if (aAncestor->GetContent() == transformFrame->GetContent()) {
2904 return true;
2906 return FrameParticipatesIn3DContext(aAncestor, transformFrame);
2909 static void WrapSeparatorTransform(nsDisplayListBuilder* aBuilder,
2910 nsIFrame* aFrame,
2911 nsDisplayList* aNonParticipants,
2912 nsDisplayList* aParticipants, int aIndex,
2913 nsDisplayItem** aSeparator) {
2914 if (aNonParticipants->IsEmpty()) {
2915 return;
2918 nsDisplayTransform* item = MakeDisplayItemWithIndex<nsDisplayTransform>(
2919 aBuilder, aFrame, aIndex, aNonParticipants, aBuilder->GetVisibleRect());
2921 if (*aSeparator == nullptr && item) {
2922 *aSeparator = item;
2925 aParticipants->AppendToTop(item);
2928 // Try to compute a clip rect to bound the contents of the mask item
2929 // that will be built for |aMaskedFrame|. If we're not able to compute
2930 // one, return an empty Maybe.
2931 // The returned clip rect, if there is one, is relative to |aMaskedFrame|.
2932 static Maybe<nsRect> ComputeClipForMaskItem(
2933 nsDisplayListBuilder* aBuilder, nsIFrame* aMaskedFrame,
2934 const SVGUtils::MaskUsage& aMaskUsage) {
2935 const nsStyleSVGReset* svgReset = aMaskedFrame->StyleSVGReset();
2937 nsPoint offsetToUserSpace =
2938 nsLayoutUtils::ComputeOffsetToUserSpace(aBuilder, aMaskedFrame);
2939 int32_t devPixelRatio = aMaskedFrame->PresContext()->AppUnitsPerDevPixel();
2940 gfxPoint devPixelOffsetToUserSpace =
2941 nsLayoutUtils::PointToGfxPoint(offsetToUserSpace, devPixelRatio);
2942 CSSToLayoutDeviceScale cssToDevScale =
2943 aMaskedFrame->PresContext()->CSSToDevPixelScale();
2945 nsPoint toReferenceFrame;
2946 aBuilder->FindReferenceFrameFor(aMaskedFrame, &toReferenceFrame);
2948 Maybe<gfxRect> combinedClip;
2949 if (aMaskUsage.ShouldApplyBasicShapeOrPath()) {
2950 Maybe<Rect> result =
2951 CSSClipPathInstance::GetBoundingRectForBasicShapeOrPathClip(
2952 aMaskedFrame, svgReset->mClipPath);
2953 if (result) {
2954 combinedClip = Some(ThebesRect(*result));
2956 } else if (aMaskUsage.ShouldApplyClipPath()) {
2957 gfxRect result = SVGUtils::GetBBox(
2958 aMaskedFrame,
2959 SVGUtils::eBBoxIncludeClipped | SVGUtils::eBBoxIncludeFill |
2960 SVGUtils::eBBoxIncludeMarkers | SVGUtils::eBBoxIncludeStroke |
2961 SVGUtils::eDoNotClipToBBoxOfContentInsideClipPath);
2962 combinedClip = Some(
2963 ThebesRect((CSSRect::FromUnknownRect(ToRect(result)) * cssToDevScale)
2964 .ToUnknownRect()));
2965 } else {
2966 // The code for this case is adapted from ComputeMaskGeometry().
2968 nsRect borderArea(toReferenceFrame, aMaskedFrame->GetSize());
2969 borderArea -= offsetToUserSpace;
2971 // Use an infinite dirty rect to pass into nsCSSRendering::
2972 // GetImageLayerClip() because we don't have an actual dirty rect to
2973 // pass in. This is fine because the only time GetImageLayerClip() will
2974 // not intersect the incoming dirty rect with something is in the "NoClip"
2975 // case, and we handle that specially.
2976 nsRect dirtyRect(nscoord_MIN / 2, nscoord_MIN / 2, nscoord_MAX,
2977 nscoord_MAX);
2979 nsIFrame* firstFrame =
2980 nsLayoutUtils::FirstContinuationOrIBSplitSibling(aMaskedFrame);
2981 nsTArray<SVGMaskFrame*> maskFrames;
2982 // XXX check return value?
2983 SVGObserverUtils::GetAndObserveMasks(firstFrame, &maskFrames);
2985 for (uint32_t i = 0; i < maskFrames.Length(); ++i) {
2986 gfxRect clipArea;
2987 if (maskFrames[i]) {
2988 clipArea = maskFrames[i]->GetMaskArea(aMaskedFrame);
2989 clipArea = ThebesRect(
2990 (CSSRect::FromUnknownRect(ToRect(clipArea)) * cssToDevScale)
2991 .ToUnknownRect());
2992 } else {
2993 const auto& layer = svgReset->mMask.mLayers[i];
2994 if (layer.mClip == StyleGeometryBox::NoClip) {
2995 return Nothing();
2998 nsCSSRendering::ImageLayerClipState clipState;
2999 nsCSSRendering::GetImageLayerClip(
3000 layer, aMaskedFrame, *aMaskedFrame->StyleBorder(), borderArea,
3001 dirtyRect, false /* aWillPaintBorder */, devPixelRatio, &clipState);
3002 clipArea = clipState.mDirtyRectInDevPx;
3004 combinedClip = UnionMaybeRects(combinedClip, Some(clipArea));
3007 if (combinedClip) {
3008 if (combinedClip->IsEmpty()) {
3009 // *clipForMask might be empty if all mask references are not resolvable
3010 // or the size of them are empty. We still need to create a transparent
3011 // mask before bug 1276834 fixed, so don't clip ctx by an empty rectangle
3012 // for for now.
3013 return Nothing();
3016 // Convert to user space.
3017 *combinedClip += devPixelOffsetToUserSpace;
3019 // Round the clip out. In FrameLayerBuilder we round clips to nearest
3020 // pixels, and if we have a really thin clip here, that can cause the
3021 // clip to become empty if we didn't round out here.
3022 // The rounding happens in coordinates that are relative to the reference
3023 // frame, which matches what FrameLayerBuilder does.
3024 combinedClip->RoundOut();
3026 // Convert to app units.
3027 nsRect result =
3028 nsLayoutUtils::RoundGfxRectToAppRect(*combinedClip, devPixelRatio);
3030 // The resulting clip is relative to the reference frame, but the caller
3031 // expects it to be relative to the masked frame, so adjust it.
3032 result -= toReferenceFrame;
3033 return Some(result);
3035 return Nothing();
3038 struct AutoCheckBuilder {
3039 explicit AutoCheckBuilder(nsDisplayListBuilder* aBuilder)
3040 : mBuilder(aBuilder) {
3041 aBuilder->Check();
3044 ~AutoCheckBuilder() { mBuilder->Check(); }
3046 nsDisplayListBuilder* mBuilder;
3050 * Tries to reuse a top-level stacking context item from the previous paint.
3051 * Returns true if an item was reused, otherwise false.
3053 bool TryToReuseStackingContextItem(nsDisplayListBuilder* aBuilder,
3054 nsDisplayList* aList, nsIFrame* aFrame) {
3055 if (!aBuilder->IsForPainting() || !aBuilder->IsPartialUpdate() ||
3056 aBuilder->InInvalidSubtree()) {
3057 return false;
3060 if (aFrame->IsFrameModified() || aFrame->HasModifiedDescendants()) {
3061 return false;
3064 auto& items = aFrame->DisplayItems();
3065 auto* res = std::find_if(
3066 items.begin(), items.end(),
3067 [](nsDisplayItem* aItem) { return aItem->IsPreProcessed(); });
3069 if (res == items.end()) {
3070 return false;
3073 nsDisplayItem* container = *res;
3074 MOZ_ASSERT(container->Frame() == aFrame);
3075 DL_LOGD("RDL - Found SC item %p (%s) (frame: %p)", container,
3076 container->Name(), container->Frame());
3078 aList->AppendToTop(container);
3079 aBuilder->ReuseDisplayItem(container);
3080 return true;
3083 void nsIFrame::BuildDisplayListForStackingContext(
3084 nsDisplayListBuilder* aBuilder, nsDisplayList* aList,
3085 bool* aCreatedContainerItem) {
3086 #ifdef DEBUG
3087 DL_LOGV("BuildDisplayListForStackingContext (%p) <", this);
3088 ScopeExit e(
3089 [this]() { DL_LOGV("> BuildDisplayListForStackingContext (%p)", this); });
3090 #endif
3092 AutoCheckBuilder check(aBuilder);
3094 if (aBuilder->IsReusingStackingContextItems() &&
3095 TryToReuseStackingContextItem(aBuilder, aList, this)) {
3096 if (aCreatedContainerItem) {
3097 *aCreatedContainerItem = true;
3099 return;
3102 if (HasAnyStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE)) {
3103 return;
3106 const auto& style = *Style();
3107 const nsStyleDisplay* disp = style.StyleDisplay();
3108 const nsStyleEffects* effects = style.StyleEffects();
3109 EffectSet* effectSetForOpacity =
3110 EffectSet::GetForFrame(this, nsCSSPropertyIDSet::OpacityProperties());
3111 // We can stop right away if this is a zero-opacity stacking context and
3112 // we're painting, and we're not animating opacity.
3113 bool needHitTestInfo = aBuilder->BuildCompositorHitTestInfo() &&
3114 Style()->PointerEvents() != StylePointerEvents::None;
3115 bool opacityItemForEventsOnly = false;
3116 if (effects->IsTransparent() && aBuilder->IsForPainting() &&
3117 !(disp->mWillChange.bits & StyleWillChangeBits::OPACITY) &&
3118 !nsLayoutUtils::HasAnimationOfPropertySet(
3119 this, nsCSSPropertyIDSet::OpacityProperties(), effectSetForOpacity)) {
3120 if (needHitTestInfo) {
3121 opacityItemForEventsOnly = true;
3122 } else {
3123 return;
3127 if (aBuilder->IsForPainting() && disp->mWillChange.bits) {
3128 aBuilder->AddToWillChangeBudget(this, GetSize());
3131 // For preserves3d, use the dirty rect already installed on the
3132 // builder, since aDirtyRect maybe distorted for transforms along
3133 // the chain.
3134 nsRect visibleRect = aBuilder->GetVisibleRect();
3135 nsRect dirtyRect = aBuilder->GetDirtyRect();
3137 // We build an opacity item if it's not going to be drawn by SVG content.
3138 // We could in principle skip creating an nsDisplayOpacity item if
3139 // nsDisplayOpacity::NeedsActiveLayer returns false and usingSVGEffects is
3140 // true (the nsDisplayFilter/nsDisplayMasksAndClipPaths could handle the
3141 // opacity). Since SVG has perf issues where we sometimes spend a lot of
3142 // time creating display list items that might be helpful. We'd need to
3143 // restore our mechanism to do that (changed in bug 1482403), and we'd
3144 // need to invalidate the frame if the value that would be return from
3145 // NeedsActiveLayer was to change, which we don't currently do.
3146 const bool useOpacity =
3147 HasVisualOpacity(disp, effects, effectSetForOpacity) &&
3148 !SVGUtils::CanOptimizeOpacity(this);
3150 const bool isTransformed = IsTransformed();
3151 const bool hasPerspective = isTransformed && HasPerspective();
3152 const bool extend3DContext =
3153 Extend3DContext(disp, effects, effectSetForOpacity);
3154 const bool combines3DTransformWithAncestors =
3155 (extend3DContext || isTransformed) && Combines3DTransformWithAncestors();
3157 Maybe<nsDisplayListBuilder::AutoPreserves3DContext> autoPreserves3DContext;
3158 if (extend3DContext && !combines3DTransformWithAncestors) {
3159 // Start a new preserves3d context to keep informations on
3160 // nsDisplayListBuilder.
3161 autoPreserves3DContext.emplace(aBuilder);
3162 // Save dirty rect on the builder to avoid being distorted for
3163 // multiple transforms along the chain.
3164 aBuilder->SavePreserves3DRect();
3166 // We rebuild everything within preserve-3d and don't try
3167 // to retain, so override the dirty rect now.
3168 if (aBuilder->IsRetainingDisplayList()) {
3169 dirtyRect = visibleRect;
3170 aBuilder->SetDisablePartialUpdates(true);
3174 const bool useBlendMode = effects->mMixBlendMode != StyleBlend::Normal;
3175 if (useBlendMode) {
3176 aBuilder->SetContainsBlendMode(true);
3179 // reset blend mode so we can keep track if this stacking context needs have
3180 // a nsDisplayBlendContainer. Set the blend mode back when the routine exits
3181 // so we keep track if the parent stacking context needs a container too.
3182 AutoSaveRestoreContainsBlendMode autoRestoreBlendMode(*aBuilder);
3183 aBuilder->SetContainsBlendMode(false);
3185 // NOTE: When changing this condition make sure to tweak nsGfxScrollFrame as
3186 // well.
3187 bool usingBackdropFilter = effects->HasBackdropFilters() &&
3188 IsVisibleForPainting() &&
3189 !style.IsRootElementStyle();
3191 nsRect visibleRectOutsideTransform = visibleRect;
3192 nsDisplayTransform::PrerenderInfo prerenderInfo;
3193 bool inTransform = aBuilder->IsInTransform();
3194 if (isTransformed) {
3195 prerenderInfo = nsDisplayTransform::ShouldPrerenderTransformedContent(
3196 aBuilder, this, &visibleRect);
3198 switch (prerenderInfo.mDecision) {
3199 case nsDisplayTransform::PrerenderDecision::Full:
3200 case nsDisplayTransform::PrerenderDecision::Partial:
3201 dirtyRect = visibleRect;
3202 break;
3203 case nsDisplayTransform::PrerenderDecision::No: {
3204 // If we didn't prerender an animated frame in a preserve-3d context,
3205 // then we want disable async animations for the rest of the preserve-3d
3206 // (especially ancestors).
3207 if ((extend3DContext || combines3DTransformWithAncestors) &&
3208 prerenderInfo.mHasAnimations) {
3209 aBuilder->SavePreserves3DAllowAsyncAnimation(false);
3212 const nsRect overflow = InkOverflowRectRelativeToSelf();
3213 if (overflow.IsEmpty() && !extend3DContext) {
3214 return;
3217 // If we're in preserve-3d then grab the dirty rect that was given to
3218 // the root and transform using the combined transform.
3219 if (combines3DTransformWithAncestors) {
3220 visibleRect = dirtyRect = aBuilder->GetPreserves3DRect();
3223 float appPerDev = PresContext()->AppUnitsPerDevPixel();
3224 auto transform = nsDisplayTransform::GetResultingTransformMatrix(
3225 this, nsPoint(), appPerDev,
3226 nsDisplayTransform::kTransformRectFlags);
3227 nsRect untransformedDirtyRect;
3228 if (nsDisplayTransform::UntransformRect(dirtyRect, overflow, transform,
3229 appPerDev,
3230 &untransformedDirtyRect)) {
3231 dirtyRect = untransformedDirtyRect;
3232 nsDisplayTransform::UntransformRect(visibleRect, overflow, transform,
3233 appPerDev, &visibleRect);
3234 } else {
3235 // This should only happen if the transform is singular, in which case
3236 // nothing is visible anyway
3237 dirtyRect.SetEmpty();
3238 visibleRect.SetEmpty();
3242 inTransform = true;
3243 } else if (IsFixedPosContainingBlock()) {
3244 // Restict the building area to the overflow rect for these frames, since
3245 // RetainedDisplayListBuilder uses it to know if the size of the stacking
3246 // context changed.
3247 visibleRect.IntersectRect(visibleRect, InkOverflowRect());
3248 dirtyRect.IntersectRect(dirtyRect, InkOverflowRect());
3251 bool hasOverrideDirtyRect = false;
3252 // If we're doing a partial build, we're not invalid and we're capable
3253 // of having an override building rect (stacking context and fixed pos
3254 // containing block), then we should assume we have one.
3255 // Either we have an explicit one, or nothing in our subtree changed and
3256 // we have an implicit empty rect.
3258 // These conditions should match |CanStoreDisplayListBuildingRect()| in
3259 // RetainedDisplayListBuilder.cpp
3260 if (!aBuilder->IsReusingStackingContextItems() &&
3261 aBuilder->IsPartialUpdate() && !aBuilder->InInvalidSubtree() &&
3262 !IsFrameModified() && IsFixedPosContainingBlock() &&
3263 !GetPrevContinuation() && !GetNextContinuation()) {
3264 dirtyRect = nsRect();
3265 if (HasOverrideDirtyRegion()) {
3266 nsDisplayListBuilder::DisplayListBuildingData* data =
3267 GetProperty(nsDisplayListBuilder::DisplayListBuildingRect());
3268 if (data) {
3269 dirtyRect = data->mDirtyRect.Intersect(visibleRect);
3270 hasOverrideDirtyRect = true;
3275 bool usingFilter = effects->HasFilters() && !style.IsRootElementStyle();
3276 SVGUtils::MaskUsage maskUsage = SVGUtils::DetermineMaskUsage(this, false);
3277 bool usingMask = maskUsage.UsingMaskOrClipPath();
3278 bool usingSVGEffects = usingFilter || usingMask;
3280 nsRect visibleRectOutsideSVGEffects = visibleRect;
3281 nsDisplayList hoistedScrollInfoItemsStorage(aBuilder);
3282 if (usingSVGEffects) {
3283 dirtyRect =
3284 SVGIntegrationUtils::GetRequiredSourceForInvalidArea(this, dirtyRect);
3285 visibleRect =
3286 SVGIntegrationUtils::GetRequiredSourceForInvalidArea(this, visibleRect);
3287 aBuilder->EnterSVGEffectsContents(this, &hoistedScrollInfoItemsStorage);
3290 bool useStickyPosition = disp->mPosition == StylePositionProperty::Sticky;
3292 bool useFixedPosition =
3293 disp->mPosition == StylePositionProperty::Fixed &&
3294 (DisplayPortUtils::IsFixedPosFrameInDisplayPort(this) ||
3295 BuilderHasScrolledClip(aBuilder));
3297 nsDisplayListBuilder::AutoBuildingDisplayList buildingDisplayList(
3298 aBuilder, this, visibleRect, dirtyRect, isTransformed);
3300 UpdateCurrentHitTestInfo(aBuilder, this);
3302 // Depending on the effects that are applied to this frame, we can create
3303 // multiple container display items and wrap them around our contents.
3304 // This enum lists all the potential container display items, in the order
3305 // outside to inside.
3306 enum class ContainerItemType : uint8_t {
3307 None = 0,
3308 OwnLayerIfNeeded,
3309 BlendMode,
3310 FixedPosition,
3311 OwnLayerForTransformWithRoundedClip,
3312 Perspective,
3313 Transform,
3314 SeparatorTransforms,
3315 Opacity,
3316 Filter,
3317 BlendContainer
3320 nsDisplayListBuilder::AutoContainerASRTracker contASRTracker(aBuilder);
3322 auto cssClip = GetClipPropClipRect(disp, effects, GetSize());
3323 auto ApplyClipProp = [&](DisplayListClipState::AutoSaveRestore& aClipState) {
3324 if (!cssClip) {
3325 return;
3327 nsPoint offset = aBuilder->GetCurrentFrameOffsetToReferenceFrame();
3328 aBuilder->IntersectDirtyRect(*cssClip);
3329 aBuilder->IntersectVisibleRect(*cssClip);
3330 aClipState.ClipContentDescendants(*cssClip + offset);
3333 // The CSS clip property is effectively inside the transform, but outside the
3334 // filters. So if we're not transformed we can apply it just here for
3335 // simplicity, instead of on each of the places that handle clipCapturedBy.
3336 DisplayListClipState::AutoSaveRestore untransformedCssClip(aBuilder);
3337 if (!isTransformed) {
3338 ApplyClipProp(untransformedCssClip);
3341 // If there is a current clip, then depending on the container items we
3342 // create, different things can happen to it. Some container items simply
3343 // propagate the clip to their children and aren't clipped themselves.
3344 // But other container items, especially those that establish a different
3345 // geometry for their contents (e.g. transforms), capture the clip on
3346 // themselves and unset the clip for their contents. If we create more than
3347 // one of those container items, the clip will be captured on the outermost
3348 // one and the inner container items will be unclipped.
3349 ContainerItemType clipCapturedBy = ContainerItemType::None;
3350 if (useFixedPosition) {
3351 clipCapturedBy = ContainerItemType::FixedPosition;
3352 } else if (isTransformed) {
3353 const DisplayItemClipChain* currentClip =
3354 aBuilder->ClipState().GetCurrentCombinedClipChain(aBuilder);
3355 if ((hasPerspective || extend3DContext) &&
3356 (currentClip && currentClip->HasRoundedCorners())) {
3357 // If we're creating an nsDisplayTransform item that is going to combine
3358 // its transform with its children (preserve-3d or perspective), then we
3359 // can't have an intermediate surface. Mask layers force an intermediate
3360 // surface, so if we're going to need both then create a separate
3361 // wrapping layer for the mask.
3362 clipCapturedBy = ContainerItemType::OwnLayerForTransformWithRoundedClip;
3363 } else if (hasPerspective) {
3364 clipCapturedBy = ContainerItemType::Perspective;
3365 } else {
3366 clipCapturedBy = ContainerItemType::Transform;
3368 } else if (usingFilter) {
3369 clipCapturedBy = ContainerItemType::Filter;
3372 DisplayListClipState::AutoSaveRestore clipState(aBuilder);
3373 if (clipCapturedBy != ContainerItemType::None) {
3374 clipState.Clear();
3377 DisplayListClipState::AutoSaveRestore transformedCssClip(aBuilder);
3378 if (isTransformed) {
3379 // FIXME(emilio, bug 1525159): In the case we have a both a transform _and_
3380 // filters, this clips the input to the filters as well, which is not
3381 // correct (clipping by the `clip` property is supposed to happen after
3382 // applying the filter effects, per [1].
3384 // This is not a regression though, since we used to do that anyway before
3385 // bug 1514384, and even without the transform we get it wrong.
3387 // [1]: https://drafts.fxtf.org/css-masking/#placement
3388 ApplyClipProp(transformedCssClip);
3391 nsDisplayListCollection set(aBuilder);
3392 Maybe<nsRect> clipForMask;
3394 DisplayListClipState::AutoSaveRestore nestedClipState(aBuilder);
3395 nsDisplayListBuilder::AutoInTransformSetter inTransformSetter(aBuilder,
3396 inTransform);
3397 nsDisplayListBuilder::AutoEnterFilter filterASRSetter(aBuilder,
3398 usingFilter);
3399 nsDisplayListBuilder::AutoInEventsOnly inEventsSetter(
3400 aBuilder, opacityItemForEventsOnly);
3402 // If we have a mask, compute a clip to bound the masked content.
3403 // This is necessary in case the content moves with an ancestor
3404 // ASR of the mask.
3405 // Don't do this if we also have a filter, because then the clip
3406 // would be applied before the filter, violating
3407 // https://www.w3.org/TR/filter-effects-1/#placement.
3408 // Filters are a containing block for fixed and absolute descendants,
3409 // so the masked content cannot move with an ancestor ASR.
3410 if (usingMask && !usingFilter) {
3411 clipForMask = ComputeClipForMaskItem(aBuilder, this, maskUsage);
3412 if (clipForMask) {
3413 aBuilder->IntersectDirtyRect(*clipForMask);
3414 aBuilder->IntersectVisibleRect(*clipForMask);
3415 nestedClipState.ClipContentDescendants(
3416 *clipForMask + aBuilder->GetCurrentFrameOffsetToReferenceFrame());
3420 // extend3DContext also guarantees that applyAbsPosClipping and
3421 // usingSVGEffects are false We only modify the preserve-3d rect if we are
3422 // the top of a preserve-3d heirarchy
3423 if (extend3DContext) {
3424 // Mark these first so MarkAbsoluteFramesForDisplayList knows if we are
3425 // going to be forced to descend into frames.
3426 aBuilder->MarkPreserve3DFramesForDisplayList(this);
3429 aBuilder->AdjustWindowDraggingRegion(this);
3431 MarkAbsoluteFramesForDisplayList(aBuilder);
3432 aBuilder->Check();
3433 BuildDisplayList(aBuilder, set);
3434 SetBuiltDisplayList(true);
3435 aBuilder->Check();
3436 aBuilder->DisplayCaret(this, set.Outlines());
3438 // Blend modes are a real pain for retained display lists. We build a blend
3439 // container item if the built list contains any blend mode items within
3440 // the current stacking context. This can change without an invalidation
3441 // to the stacking context frame, or the blend mode frame (e.g. by moving
3442 // an intermediate frame).
3443 // When we gain/remove a blend container item, we need to mark this frame
3444 // as invalid and have the full display list for merging to track
3445 // the change correctly.
3446 // It seems really hard to track this in advance, as the bookkeeping
3447 // required to note which stacking contexts have blend descendants
3448 // is complex and likely to be buggy.
3449 // Instead we're doing the sad thing, detecting it afterwards, and just
3450 // repeating display list building if it changed.
3451 // We have to repeat building for the entire display list (or at least
3452 // the outer stacking context), since we need to mark this frame as invalid
3453 // to remove any existing content that isn't wrapped in the blend container,
3454 // and then we need to build content infront/behind the blend container
3455 // to get correct positioning during merging.
3456 if (aBuilder->ContainsBlendMode() && aBuilder->IsRetainingDisplayList()) {
3457 if (aBuilder->IsPartialUpdate()) {
3458 aBuilder->SetPartialBuildFailed(true);
3459 } else {
3460 aBuilder->SetDisablePartialUpdates(true);
3465 if (aBuilder->IsBackgroundOnly()) {
3466 set.BlockBorderBackgrounds()->DeleteAll(aBuilder);
3467 set.Floats()->DeleteAll(aBuilder);
3468 set.Content()->DeleteAll(aBuilder);
3469 set.PositionedDescendants()->DeleteAll(aBuilder);
3470 set.Outlines()->DeleteAll(aBuilder);
3473 if (hasOverrideDirtyRect &&
3474 StaticPrefs::layout_display_list_show_rebuild_area()) {
3475 nsDisplaySolidColor* color = MakeDisplayItem<nsDisplaySolidColor>(
3476 aBuilder, this,
3477 dirtyRect + aBuilder->GetCurrentFrameOffsetToReferenceFrame(),
3478 NS_RGBA(255, 0, 0, 64), false);
3479 if (color) {
3480 color->SetOverrideZIndex(INT32_MAX);
3481 set.PositionedDescendants()->AppendToTop(color);
3485 nsIContent* content = GetContent();
3486 if (!content) {
3487 content = PresContext()->Document()->GetRootElement();
3490 nsDisplayList resultList(aBuilder);
3491 set.SerializeWithCorrectZOrder(&resultList, content);
3493 // Get the ASR to use for the container items that we create here.
3494 const ActiveScrolledRoot* containerItemASR = contASRTracker.GetContainerASR();
3496 bool createdContainer = false;
3498 // If adding both a nsDisplayBlendContainer and a nsDisplayBlendMode to the
3499 // same list, the nsDisplayBlendContainer should be added first. This only
3500 // happens when the element creating this stacking context has mix-blend-mode
3501 // and also contains a child which has mix-blend-mode.
3502 // The nsDisplayBlendContainer must be added to the list first, so it does not
3503 // isolate the containing element blending as well.
3504 if (aBuilder->ContainsBlendMode()) {
3505 resultList.AppendToTop(nsDisplayBlendContainer::CreateForMixBlendMode(
3506 aBuilder, this, &resultList, containerItemASR));
3507 createdContainer = true;
3510 if (usingBackdropFilter) {
3511 nsRect backdropRect =
3512 GetRectRelativeToSelf() + aBuilder->ToReferenceFrame(this);
3513 resultList.AppendNewToTop<nsDisplayBackdropFilters>(
3514 aBuilder, this, &resultList, backdropRect, this);
3515 createdContainer = true;
3518 // If there are any SVG effects, wrap the list up in an SVG effects item
3519 // (which also handles CSS group opacity). Note that we create an SVG effects
3520 // item even if resultList is empty, since a filter can produce graphical
3521 // output even if the element being filtered wouldn't otherwise do so.
3522 if (usingSVGEffects) {
3523 MOZ_ASSERT(usingFilter || usingMask,
3524 "Beside filter & mask/clip-path, what else effect do we have?");
3526 if (clipCapturedBy == ContainerItemType::Filter) {
3527 clipState.Restore();
3529 // Revert to the post-filter dirty rect.
3530 aBuilder->SetVisibleRect(visibleRectOutsideSVGEffects);
3532 // Skip all filter effects while generating glyph mask.
3533 if (usingFilter && !aBuilder->IsForGenerateGlyphMask()) {
3534 /* List now emptied, so add the new list to the top. */
3535 resultList.AppendNewToTop<nsDisplayFilters>(aBuilder, this, &resultList,
3536 this, usingBackdropFilter);
3537 createdContainer = true;
3540 if (usingMask) {
3541 // The mask should move with aBuilder->CurrentActiveScrolledRoot(), so
3542 // that's the ASR we prefer to use for the mask item. However, we can
3543 // only do this if the mask if clipped with respect to that ASR, because
3544 // an item always needs to have finite bounds with respect to its ASR.
3545 // If we weren't able to compute a clip for the mask, we fall back to
3546 // using containerItemASR, which is the lowest common ancestor clip of
3547 // the mask's contents. That's not entirely correct, but it satisfies
3548 // the base requirement of the ASR system (that items have finite bounds
3549 // wrt. their ASR).
3550 const ActiveScrolledRoot* maskASR =
3551 clipForMask.isSome() ? aBuilder->CurrentActiveScrolledRoot()
3552 : containerItemASR;
3553 /* List now emptied, so add the new list to the top. */
3554 resultList.AppendNewToTop<nsDisplayMasksAndClipPaths>(
3555 aBuilder, this, &resultList, maskASR, usingBackdropFilter);
3556 createdContainer = true;
3559 // TODO(miko): We could probably create a wraplist here and avoid creating
3560 // it later in |BuildDisplayListForChild()|.
3561 createdContainer = false;
3563 // Also add the hoisted scroll info items. We need those for APZ scrolling
3564 // because nsDisplayMasksAndClipPaths items can't build active layers.
3565 aBuilder->ExitSVGEffectsContents();
3566 resultList.AppendToTop(&hoistedScrollInfoItemsStorage);
3569 // If the list is non-empty and there is CSS group opacity without SVG
3570 // effects, wrap it up in an opacity item.
3571 if (useOpacity) {
3572 const bool needsActiveOpacityLayer =
3573 nsDisplayOpacity::NeedsActiveLayer(aBuilder, this);
3574 resultList.AppendNewToTop<nsDisplayOpacity>(
3575 aBuilder, this, &resultList, containerItemASR, opacityItemForEventsOnly,
3576 needsActiveOpacityLayer, usingBackdropFilter);
3577 createdContainer = true;
3580 // If we're going to apply a transformation and don't have preserve-3d set,
3581 // wrap everything in an nsDisplayTransform. If there's nothing in the list,
3582 // don't add anything.
3584 // For the preserve-3d case we want to individually wrap every child in the
3585 // list with a separate nsDisplayTransform instead. When the child is already
3586 // an nsDisplayTransform, we can skip this step, as the computed transform
3587 // will already include our own.
3589 // We also traverse into sublists created by nsDisplayWrapList, so that we
3590 // find all the correct children.
3591 if (isTransformed && extend3DContext) {
3592 // Install dummy nsDisplayTransform as a leaf containing
3593 // descendants not participating this 3D rendering context.
3594 nsDisplayList nonparticipants(aBuilder);
3595 nsDisplayList participants(aBuilder);
3596 int index = 1;
3598 nsDisplayItem* separator = nullptr;
3600 // TODO: This can be simplified: |participants| is just |resultList|.
3601 for (nsDisplayItem* item : resultList.TakeItems()) {
3602 if (ItemParticipatesIn3DContext(this, item) &&
3603 !item->GetClip().HasClip()) {
3604 // The frame of this item participates the same 3D context.
3605 WrapSeparatorTransform(aBuilder, this, &nonparticipants, &participants,
3606 index++, &separator);
3608 participants.AppendToTop(item);
3609 } else {
3610 // The frame of the item doesn't participate the current
3611 // context, or has no transform.
3613 // For items participating but not transformed, they are add
3614 // to nonparticipants to get a separator layer for handling
3615 // clips, if there is, on an intermediate surface.
3616 // \see ContainerLayer::DefaultComputeEffectiveTransforms().
3617 nonparticipants.AppendToTop(item);
3620 WrapSeparatorTransform(aBuilder, this, &nonparticipants, &participants,
3621 index++, &separator);
3623 if (separator) {
3624 createdContainer = true;
3627 resultList.AppendToTop(&participants);
3630 if (isTransformed) {
3631 transformedCssClip.Restore();
3632 if (clipCapturedBy == ContainerItemType::Transform) {
3633 // Restore clip state now so nsDisplayTransform is clipped properly.
3634 clipState.Restore();
3636 // Revert to the dirtyrect coming in from the parent, without our transform
3637 // taken into account.
3638 aBuilder->SetVisibleRect(visibleRectOutsideTransform);
3640 if (this != aBuilder->RootReferenceFrame()) {
3641 // Revert to the outer reference frame and offset because all display
3642 // items we create from now on are outside the transform.
3643 nsPoint toOuterReferenceFrame;
3644 const nsIFrame* outerReferenceFrame =
3645 aBuilder->FindReferenceFrameFor(GetParent(), &toOuterReferenceFrame);
3646 toOuterReferenceFrame += GetPosition();
3648 buildingDisplayList.SetReferenceFrameAndCurrentOffset(
3649 outerReferenceFrame, toOuterReferenceFrame);
3652 // We would like to block async animations for ancestors of ones not
3653 // prerendered in the preserve-3d tree. Now that we've finished processing
3654 // all descendants, update allowAsyncAnimation to take their prerender
3655 // state into account
3656 // FIXME: We don't block async animations for previous siblings because
3657 // their prerender decisions have been made. We may have to figure out a
3658 // better way to rollback their prerender decisions.
3659 // Alternatively we could not block animations for later siblings, and only
3660 // block them for ancestors of a blocked one.
3661 if ((extend3DContext || combines3DTransformWithAncestors) &&
3662 prerenderInfo.CanUseAsyncAnimations() &&
3663 !aBuilder->GetPreserves3DAllowAsyncAnimation()) {
3664 // aBuilder->GetPreserves3DAllowAsyncAnimation() means the inner or
3665 // previous silbing frames are allowed/disallowed for async animations.
3666 prerenderInfo.mDecision = nsDisplayTransform::PrerenderDecision::No;
3669 nsDisplayTransform* transformItem = MakeDisplayItem<nsDisplayTransform>(
3670 aBuilder, this, &resultList, visibleRect, prerenderInfo.mDecision);
3671 if (transformItem) {
3672 resultList.AppendToTop(transformItem);
3673 createdContainer = true;
3676 if (hasPerspective) {
3677 transformItem->MarkWithAssociatedPerspective();
3679 if (clipCapturedBy == ContainerItemType::Perspective) {
3680 clipState.Restore();
3682 resultList.AppendNewToTop<nsDisplayPerspective>(aBuilder, this,
3683 &resultList);
3684 createdContainer = true;
3688 if (clipCapturedBy ==
3689 ContainerItemType::OwnLayerForTransformWithRoundedClip) {
3690 clipState.Restore();
3691 resultList.AppendNewToTopWithIndex<nsDisplayOwnLayer>(
3692 aBuilder, this,
3693 /* aIndex = */ nsDisplayOwnLayer::OwnLayerForTransformWithRoundedClip,
3694 &resultList, aBuilder->CurrentActiveScrolledRoot(),
3695 nsDisplayOwnLayerFlags::None, ScrollbarData{},
3696 /* aForceActive = */ false, false);
3697 createdContainer = true;
3700 // If we have sticky positioning, wrap it in a sticky position item.
3701 if (useFixedPosition) {
3702 if (clipCapturedBy == ContainerItemType::FixedPosition) {
3703 clipState.Restore();
3705 // The ASR for the fixed item should be the ASR of our containing block,
3706 // which has been set as the builder's current ASR, unless this frame is
3707 // invisible and we hadn't saved display item data for it. In that case,
3708 // we need to take the containerItemASR since we might have fixed children.
3709 // For WebRender, we want to the know what |containerItemASR| is for the
3710 // case where the fixed-pos item is not a "real" fixed-pos item (e.g. it's
3711 // nested inside a scrolling transform), so we stash that on the display
3712 // item as well.
3713 const ActiveScrolledRoot* fixedASR = ActiveScrolledRoot::PickAncestor(
3714 containerItemASR, aBuilder->CurrentActiveScrolledRoot());
3715 resultList.AppendNewToTop<nsDisplayFixedPosition>(
3716 aBuilder, this, &resultList, fixedASR, containerItemASR);
3717 createdContainer = true;
3718 } else if (useStickyPosition) {
3719 // For position:sticky, the clip needs to be applied both to the sticky
3720 // container item and to the contents. The container item needs the clip
3721 // because a scrolled clip needs to move independently from the sticky
3722 // contents, and the contents need the clip so that they have finite
3723 // clipped bounds with respect to the container item's ASR. The latter is
3724 // a little tricky in the case where the sticky item has both fixed and
3725 // non-fixed descendants, because that means that the sticky container
3726 // item's ASR is the ASR of the fixed descendant.
3727 // For WebRender display list building, though, we still want to know the
3728 // the ASR that the sticky container item would normally have, so we stash
3729 // that on the display item as the "container ASR" (i.e. the normal ASR of
3730 // the container item, excluding the special behaviour induced by fixed
3731 // descendants).
3732 const ActiveScrolledRoot* stickyASR = ActiveScrolledRoot::PickAncestor(
3733 containerItemASR, aBuilder->CurrentActiveScrolledRoot());
3735 auto* stickyItem = MakeDisplayItem<nsDisplayStickyPosition>(
3736 aBuilder, this, &resultList, stickyASR,
3737 aBuilder->CurrentActiveScrolledRoot(),
3738 clipState.IsClippedToDisplayPort());
3740 bool shouldFlatten = true;
3742 StickyScrollContainer* stickyScrollContainer =
3743 StickyScrollContainer::GetStickyScrollContainerForFrame(this);
3744 if (stickyScrollContainer &&
3745 stickyScrollContainer->ScrollFrame()->IsMaybeAsynchronouslyScrolled()) {
3746 shouldFlatten = false;
3749 stickyItem->SetShouldFlatten(shouldFlatten);
3751 resultList.AppendToTop(stickyItem);
3752 createdContainer = true;
3754 // If the sticky element is inside a filter, annotate the scroll frame that
3755 // scrolls the filter as having out-of-flow content inside a filter (this
3756 // inhibits paint skipping).
3757 if (aBuilder->GetFilterASR() && aBuilder->GetFilterASR() == stickyASR) {
3758 aBuilder->GetFilterASR()
3759 ->mScrollableFrame->SetHasOutOfFlowContentInsideFilter();
3763 // If there's blending, wrap up the list in a blend-mode item. Note that
3764 // opacity can be applied before blending as the blend color is not affected
3765 // by foreground opacity (only background alpha).
3766 if (useBlendMode) {
3767 DisplayListClipState::AutoSaveRestore blendModeClipState(aBuilder);
3768 resultList.AppendNewToTop<nsDisplayBlendMode>(aBuilder, this, &resultList,
3769 effects->mMixBlendMode,
3770 containerItemASR, false);
3771 createdContainer = true;
3774 if (aBuilder->IsReusingStackingContextItems()) {
3775 if (resultList.IsEmpty()) {
3776 return;
3779 nsDisplayItem* container = resultList.GetBottom();
3780 if (resultList.Length() > 1 || container->Frame() != this) {
3781 container = MakeDisplayItem<nsDisplayContainer>(
3782 aBuilder, this, containerItemASR, &resultList);
3783 } else {
3784 MOZ_ASSERT(resultList.Length() == 1);
3785 resultList.Clear();
3788 // Mark the outermost display item as reusable. These display items and
3789 // their chidren can be reused during the next paint if no ancestor or
3790 // descendant frames have been modified.
3791 if (!container->IsReusedItem()) {
3792 container->SetReusable();
3794 aList->AppendToTop(container);
3795 createdContainer = true;
3796 } else {
3797 aList->AppendToTop(&resultList);
3800 if (aCreatedContainerItem) {
3801 *aCreatedContainerItem = createdContainer;
3805 static nsDisplayItem* WrapInWrapList(nsDisplayListBuilder* aBuilder,
3806 nsIFrame* aFrame, nsDisplayList* aList,
3807 const ActiveScrolledRoot* aContainerASR,
3808 bool aBuiltContainerItem = false) {
3809 nsDisplayItem* item = aList->GetBottom();
3810 if (!item) {
3811 return nullptr;
3814 // We need a wrap list if there are multiple items, or if the single
3815 // item has a different frame. This can change in a partial build depending
3816 // on which items we build, so we need to ensure that we don't transition
3817 // to/from a wrap list without invalidating correctly.
3818 bool needsWrapList =
3819 aList->Length() > 1 || item->Frame() != aFrame || item->GetChildren();
3821 // If we have an explicit container item (that can't change without an
3822 // invalidation) or we're doing a full build and don't need a wrap list, then
3823 // we can skip adding one.
3824 if (aBuiltContainerItem || (!aBuilder->IsPartialUpdate() && !needsWrapList)) {
3825 MOZ_ASSERT(aList->Length() == 1);
3826 aList->Clear();
3827 return item;
3830 // If we're doing a partial build and we didn't need a wrap list
3831 // previously then we can try to work from there.
3832 if (aBuilder->IsPartialUpdate() &&
3833 !aFrame->HasDisplayItem(uint32_t(DisplayItemType::TYPE_CONTAINER))) {
3834 // If we now need a wrap list, we must previously have had no display items
3835 // or a single one belonging to this frame. Mark the item itself as
3836 // discarded so that RetainedDisplayListBuilder uses the ones we just built.
3837 // We don't want to mark the frame as modified as that would invalidate
3838 // positioned descendants that might be outside of this list, and might not
3839 // have been rebuilt this time.
3840 if (needsWrapList) {
3841 DiscardOldItems(aFrame);
3842 } else {
3843 MOZ_ASSERT(aList->Length() == 1);
3844 aList->Clear();
3845 return item;
3849 // The last case we could try to handle is when we previously had a wrap list,
3850 // but no longer need it. Unfortunately we can't differentiate this case from
3851 // a partial build where other children exist but we just didn't build them
3852 // this time.
3853 // TODO:RetainedDisplayListBuilder's merge phase has the full list and
3854 // could strip them out.
3856 return MakeDisplayItem<nsDisplayContainer>(aBuilder, aFrame, aContainerASR,
3857 aList);
3861 * Check if a frame should be visited for building display list.
3863 static bool DescendIntoChild(nsDisplayListBuilder* aBuilder,
3864 const nsIFrame* aChild, const nsRect& aVisible,
3865 const nsRect& aDirty) {
3866 if (aChild->HasAnyStateBits(NS_FRAME_FORCE_DISPLAY_LIST_DESCEND_INTO)) {
3867 return true;
3870 // If the child is a scrollframe that we want to ignore, then we need
3871 // to descend into it because its scrolled child may intersect the dirty
3872 // area even if the scrollframe itself doesn't.
3873 if (aChild == aBuilder->GetIgnoreScrollFrame()) {
3874 return true;
3877 // There are cases where the "ignore scroll frame" on the builder is not set
3878 // correctly, and so we additionally want to catch cases where the child is
3879 // a root scrollframe and we are ignoring scrolling on the viewport.
3880 if (aChild == aBuilder->GetPresShellIgnoreScrollFrame()) {
3881 return true;
3884 nsRect overflow = aChild->InkOverflowRect();
3886 // On mobile, there may be a dynamic toolbar. The root content document's
3887 // root scroll frame's ink overflow rect does not include the toolbar
3888 // height, but if the toolbar is hidden, we still want to be able to target
3889 // content underneath the toolbar, so expand the overflow rect here to
3890 // allow display list building to descend into the scroll frame.
3891 if (aBuilder->IsForEventDelivery() &&
3892 aChild == aChild->PresShell()->GetRootScrollFrame() &&
3893 aChild->PresContext()->IsRootContentDocumentCrossProcess() &&
3894 aChild->PresContext()->HasDynamicToolbar()) {
3895 overflow.SizeTo(nsLayoutUtils::ExpandHeightForDynamicToolbar(
3896 aChild->PresContext(), overflow.Size()));
3899 if (aDirty.Intersects(overflow)) {
3900 return true;
3903 if (aChild->ForceDescendIntoIfVisible() && aVisible.Intersects(overflow)) {
3904 return true;
3907 if (aChild->IsTablePart()) {
3908 // Relative positioning and transforms can cause table parts to move, but we
3909 // will still paint the backgrounds for their ancestor parts under them at
3910 // their 'normal' position. That means that we must consider the overflow
3911 // rects at both positions.
3913 // We convert the overflow rect into the nsTableFrame's coordinate
3914 // space, applying the normal position offset at each step. Then we
3915 // compare that against the builder's cached dirty rect in table
3916 // coordinate space.
3917 const nsIFrame* f = aChild;
3918 nsRect normalPositionOverflowRelativeToTable = overflow;
3920 while (f->IsTablePart()) {
3921 normalPositionOverflowRelativeToTable += f->GetNormalPosition();
3922 f = f->GetParent();
3925 nsDisplayTableBackgroundSet* tableBGs = aBuilder->GetTableBackgroundSet();
3926 if (tableBGs && tableBGs->GetDirtyRect().Intersects(
3927 normalPositionOverflowRelativeToTable)) {
3928 return true;
3932 return false;
3935 void nsIFrame::BuildDisplayListForSimpleChild(nsDisplayListBuilder* aBuilder,
3936 nsIFrame* aChild,
3937 const nsDisplayListSet& aLists) {
3938 // This is the shortcut for frames been handled along the common
3939 // path, the most common one of THE COMMON CASE mentioned later.
3940 MOZ_ASSERT(aChild->Type() != LayoutFrameType::Placeholder);
3941 MOZ_ASSERT(!aBuilder->GetSelectedFramesOnly() &&
3942 !aBuilder->GetIncludeAllOutOfFlows(),
3943 "It should be held for painting to window");
3944 MOZ_ASSERT(aChild->HasAnyStateBits(NS_FRAME_SIMPLE_DISPLAYLIST));
3946 const nsPoint offset = aChild->GetOffsetTo(this);
3947 const nsRect visible = aBuilder->GetVisibleRect() - offset;
3948 const nsRect dirty = aBuilder->GetDirtyRect() - offset;
3950 if (!DescendIntoChild(aBuilder, aChild, visible, dirty)) {
3951 DL_LOGV("Skipped frame %p", aChild);
3952 return;
3955 // Child cannot be transformed since it is not a stacking context.
3956 nsDisplayListBuilder::AutoBuildingDisplayList buildingForChild(
3957 aBuilder, aChild, visible, dirty, false);
3959 UpdateCurrentHitTestInfo(aBuilder, aChild);
3961 aChild->MarkAbsoluteFramesForDisplayList(aBuilder);
3962 aBuilder->AdjustWindowDraggingRegion(aChild);
3963 aBuilder->Check();
3964 aChild->BuildDisplayList(aBuilder, aLists);
3965 aChild->SetBuiltDisplayList(true);
3966 aBuilder->Check();
3967 aBuilder->DisplayCaret(aChild, aLists.Outlines());
3970 static bool ShouldSkipFrame(nsDisplayListBuilder* aBuilder,
3971 const nsIFrame* aFrame) {
3972 // If painting is restricted to just the background of the top level frame,
3973 // then we have nothing to do here.
3974 if (aBuilder->IsBackgroundOnly()) {
3975 return true;
3977 if (aBuilder->IsForGenerateGlyphMask() &&
3978 (!aFrame->IsTextFrame() && aFrame->IsLeaf())) {
3979 return true;
3981 // The placeholder frame should have the same content as the OOF frame.
3982 if (aBuilder->GetSelectedFramesOnly() &&
3983 (aFrame->IsLeaf() && !aFrame->IsSelected())) {
3984 return true;
3986 static const nsFrameState skipFlags =
3987 (NS_FRAME_TOO_DEEP_IN_FRAME_TREE | NS_FRAME_IS_NONDISPLAY);
3988 if (aFrame->HasAnyStateBits(skipFlags)) {
3989 return true;
3991 return aFrame->StyleUIReset()->mMozSubtreeHiddenOnlyVisually;
3994 void nsIFrame::BuildDisplayListForChild(nsDisplayListBuilder* aBuilder,
3995 nsIFrame* aChild,
3996 const nsDisplayListSet& aLists,
3997 DisplayChildFlags aFlags) {
3998 AutoCheckBuilder check(aBuilder);
3999 #ifdef DEBUG
4000 DL_LOGV("BuildDisplayListForChild (%p) <", aChild);
4001 ScopeExit e(
4002 [aChild]() { DL_LOGV("> BuildDisplayListForChild (%p)", aChild); });
4003 #endif
4005 if (ShouldSkipFrame(aBuilder, aChild)) {
4006 return;
4009 if (HidesContent()) {
4010 return;
4013 // If we're generating a display list for printing, include Link items for
4014 // frames that correspond to HTML link elements so that we can have active
4015 // links in saved PDF output. Note that the state of "within a link" is
4016 // set on the display-list builder, such that all descendants of the link
4017 // element will generate display-list links.
4018 // TODO: we should be able to optimize this so as to avoid creating links
4019 // for the same destination that entirely overlap each other, which adds
4020 // nothing useful to the final PDF.
4021 Maybe<nsDisplayListBuilder::Linkifier> linkifier;
4022 if (StaticPrefs::print_save_as_pdf_links_enabled() &&
4023 aBuilder->IsForPrinting()) {
4024 linkifier.emplace(aBuilder, aChild, aLists.Content());
4025 linkifier->MaybeAppendLink(aBuilder, aChild);
4028 nsIFrame* child = aChild;
4029 auto* placeholder = child->IsPlaceholderFrame()
4030 ? static_cast<nsPlaceholderFrame*>(child)
4031 : nullptr;
4032 nsIFrame* childOrOutOfFlow =
4033 placeholder ? placeholder->GetOutOfFlowFrame() : child;
4035 nsIFrame* parent = childOrOutOfFlow->GetParent();
4036 const auto* parentDisplay = parent->StyleDisplay();
4037 const auto overflowClipAxes =
4038 parent->ShouldApplyOverflowClipping(parentDisplay);
4040 const bool isPaintingToWindow = aBuilder->IsPaintingToWindow();
4041 const bool doingShortcut =
4042 isPaintingToWindow &&
4043 child->HasAnyStateBits(NS_FRAME_SIMPLE_DISPLAYLIST) &&
4044 // Animations may change the stacking context state.
4045 // ShouldApplyOverflowClipping is affected by the parent style, which does
4046 // not invalidate the NS_FRAME_SIMPLE_DISPLAYLIST bit.
4047 !(overflowClipAxes != PhysicalAxes::None ||
4048 child->MayHaveTransformAnimation() || child->MayHaveOpacityAnimation());
4050 if (aBuilder->IsForPainting()) {
4051 aBuilder->ClearWillChangeBudgetStatus(child);
4054 if (StaticPrefs::layout_css_scroll_anchoring_highlight()) {
4055 if (child->FirstContinuation()->IsScrollAnchor()) {
4056 nsRect bounds = child->GetContentRectRelativeToSelf() +
4057 aBuilder->ToReferenceFrame(child);
4058 nsDisplaySolidColor* color = MakeDisplayItem<nsDisplaySolidColor>(
4059 aBuilder, child, bounds, NS_RGBA(255, 0, 255, 64));
4060 if (color) {
4061 color->SetOverrideZIndex(INT32_MAX);
4062 aLists.PositionedDescendants()->AppendToTop(color);
4067 if (doingShortcut) {
4068 BuildDisplayListForSimpleChild(aBuilder, child, aLists);
4069 return;
4072 // dirty rect in child-relative coordinates
4073 NS_ASSERTION(aBuilder->GetCurrentFrame() == this, "Wrong coord space!");
4074 const nsPoint offset = child->GetOffsetTo(this);
4075 nsRect visible = aBuilder->GetVisibleRect() - offset;
4076 nsRect dirty = aBuilder->GetDirtyRect() - offset;
4078 nsDisplayListBuilder::OutOfFlowDisplayData* savedOutOfFlowData = nullptr;
4079 if (placeholder) {
4080 if (placeholder->HasAnyStateBits(PLACEHOLDER_FOR_TOPLAYER)) {
4081 // If the out-of-flow frame is in the top layer, the viewport frame
4082 // will paint it. Skip it here. Note that, only out-of-flow frames
4083 // with this property should be skipped, because non-HTML elements
4084 // may stop their children from being out-of-flow. Those frames
4085 // should still be handled in the normal in-flow path.
4086 return;
4089 child = childOrOutOfFlow;
4090 if (aBuilder->IsForPainting()) {
4091 aBuilder->ClearWillChangeBudgetStatus(child);
4094 // If 'child' is a pushed float then it's owned by a block that's not an
4095 // ancestor of the placeholder, and it will be painted by that block and
4096 // should not be painted through the placeholder. Also recheck
4097 // NS_FRAME_TOO_DEEP_IN_FRAME_TREE and NS_FRAME_IS_NONDISPLAY.
4098 static const nsFrameState skipFlags =
4099 (NS_FRAME_IS_PUSHED_FLOAT | NS_FRAME_TOO_DEEP_IN_FRAME_TREE |
4100 NS_FRAME_IS_NONDISPLAY);
4101 if (child->HasAnyStateBits(skipFlags) || nsLayoutUtils::IsPopup(child)) {
4102 return;
4105 MOZ_ASSERT(child->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW));
4106 savedOutOfFlowData = nsDisplayListBuilder::GetOutOfFlowData(child);
4108 if (aBuilder->GetIncludeAllOutOfFlows()) {
4109 visible = child->InkOverflowRect();
4110 dirty = child->InkOverflowRect();
4111 } else if (savedOutOfFlowData) {
4112 visible =
4113 savedOutOfFlowData->GetVisibleRectForFrame(aBuilder, child, &dirty);
4114 } else {
4115 // The out-of-flow frame did not intersect the dirty area. We may still
4116 // need to traverse into it, since it may contain placeholders we need
4117 // to enter to reach other out-of-flow frames that are visible.
4118 visible.SetEmpty();
4119 dirty.SetEmpty();
4123 NS_ASSERTION(!child->IsPlaceholderFrame(),
4124 "Should have dealt with placeholders already");
4126 if (!DescendIntoChild(aBuilder, child, visible, dirty)) {
4127 DL_LOGV("Skipped frame %p", child);
4128 return;
4131 const bool isSVG = child->HasAnyStateBits(NS_FRAME_SVG_LAYOUT);
4133 // This flag is raised if the control flow strays off the common path.
4134 // The common path is the most common one of THE COMMON CASE mentioned later.
4135 bool awayFromCommonPath = !isPaintingToWindow;
4137 // true if this is a real or pseudo stacking context
4138 bool pseudoStackingContext =
4139 aFlags.contains(DisplayChildFlag::ForcePseudoStackingContext);
4141 if (!pseudoStackingContext && !isSVG &&
4142 aFlags.contains(DisplayChildFlag::Inline) &&
4143 !child->IsLineParticipant()) {
4144 // child is a non-inline frame in an inline context, i.e.,
4145 // it acts like inline-block or inline-table. Therefore it is a
4146 // pseudo-stacking-context.
4147 pseudoStackingContext = true;
4150 const nsStyleDisplay* ourDisp = StyleDisplay();
4151 // Don't paint our children if the theme object is a leaf.
4152 if (IsThemed(ourDisp) && !PresContext()->Theme()->WidgetIsContainer(
4153 ourDisp->EffectiveAppearance())) {
4154 return;
4157 // Since we're now sure that we're adding this frame to the display list
4158 // (which means we're painting it, modulo occlusion), mark it as visible
4159 // within the displayport.
4160 if (isPaintingToWindow && child->TrackingVisibility() &&
4161 child->IsVisibleForPainting()) {
4162 child->PresShell()->EnsureFrameInApproximatelyVisibleList(child);
4163 awayFromCommonPath = true;
4166 // Child is composited if it's transformed, partially transparent, or has
4167 // SVG effects or a blend mode..
4168 const nsStyleDisplay* disp = child->StyleDisplay();
4169 const nsStyleEffects* effects = child->StyleEffects();
4171 const bool isPositioned = disp->IsPositionedStyle();
4172 const bool isStackingContext =
4173 aFlags.contains(DisplayChildFlag::ForceStackingContext) ||
4174 child->IsStackingContext(disp, effects);
4176 if (pseudoStackingContext || isStackingContext || isPositioned ||
4177 placeholder || (!isSVG && disp->IsFloating(child)) ||
4178 (isSVG && effects->mClip.IsRect() && IsSVGContentWithCSSClip(child))) {
4179 pseudoStackingContext = true;
4180 awayFromCommonPath = true;
4183 NS_ASSERTION(!isStackingContext || pseudoStackingContext,
4184 "Stacking contexts must also be pseudo-stacking-contexts");
4186 nsDisplayListBuilder::AutoBuildingDisplayList buildingForChild(
4187 aBuilder, child, visible, dirty);
4189 UpdateCurrentHitTestInfo(aBuilder, child);
4191 DisplayListClipState::AutoClipMultiple clipState(aBuilder);
4192 nsDisplayListBuilder::AutoCurrentActiveScrolledRootSetter asrSetter(aBuilder);
4194 if (savedOutOfFlowData) {
4195 aBuilder->SetBuildingInvisibleItems(false);
4197 clipState.SetClipChainForContainingBlockDescendants(
4198 savedOutOfFlowData->mContainingBlockClipChain);
4199 asrSetter.SetCurrentActiveScrolledRoot(
4200 savedOutOfFlowData->mContainingBlockActiveScrolledRoot);
4201 asrSetter.SetCurrentScrollParentId(savedOutOfFlowData->mScrollParentId);
4202 MOZ_ASSERT(awayFromCommonPath,
4203 "It is impossible when savedOutOfFlowData is true");
4204 } else if (HasAnyStateBits(NS_FRAME_FORCE_DISPLAY_LIST_DESCEND_INTO) &&
4205 placeholder) {
4206 NS_ASSERTION(visible.IsEmpty(), "should have empty visible rect");
4207 // Every item we build from now until we descent into an out of flow that
4208 // does have saved out of flow data should be invisible. This state gets
4209 // restored when AutoBuildingDisplayList gets out of scope.
4210 aBuilder->SetBuildingInvisibleItems(true);
4212 // If we have nested out-of-flow frames and the outer one isn't visible
4213 // then we won't have stored clip data for it. We can just clear the clip
4214 // instead since we know we won't render anything, and the inner out-of-flow
4215 // frame will setup the correct clip for itself.
4216 clipState.SetClipChainForContainingBlockDescendants(nullptr);
4219 // Setup clipping for the parent's overflow:clip,
4220 // or overflow:hidden on elements that don't support scrolling (and therefore
4221 // don't create nsHTML/XULScrollFrame). This clipping needs to not clip
4222 // anything directly rendered by the parent, only the rendering of its
4223 // children.
4224 // Don't use overflowClip to restrict the dirty rect, since some of the
4225 // descendants may not be clipped by it. Even if we end up with unnecessary
4226 // display items, they'll be pruned during ComputeVisibility.
4228 // FIXME(emilio): Why can't we handle this more similarly to `clip` (on the
4229 // parent, rather than on the children)? Would ClipContentDescendants do what
4230 // we want?
4231 if (overflowClipAxes != PhysicalAxes::None) {
4232 ApplyOverflowClipping(aBuilder, parent, overflowClipAxes, clipState);
4233 awayFromCommonPath = true;
4236 nsDisplayList list(aBuilder);
4237 nsDisplayList extraPositionedDescendants(aBuilder);
4238 const ActiveScrolledRoot* wrapListASR;
4239 bool builtContainerItem = false;
4240 if (isStackingContext) {
4241 // True stacking context.
4242 // For stacking contexts, BuildDisplayListForStackingContext handles
4243 // clipping and MarkAbsoluteFramesForDisplayList.
4244 nsDisplayListBuilder::AutoContainerASRTracker contASRTracker(aBuilder);
4245 child->BuildDisplayListForStackingContext(aBuilder, &list,
4246 &builtContainerItem);
4247 wrapListASR = contASRTracker.GetContainerASR();
4248 if (!aBuilder->IsReusingStackingContextItems() &&
4249 aBuilder->GetCaretFrame() == child) {
4250 builtContainerItem = false;
4252 } else {
4253 Maybe<nsRect> clipPropClip =
4254 child->GetClipPropClipRect(disp, effects, child->GetSize());
4255 if (clipPropClip) {
4256 aBuilder->IntersectVisibleRect(*clipPropClip);
4257 aBuilder->IntersectDirtyRect(*clipPropClip);
4258 clipState.ClipContentDescendants(*clipPropClip +
4259 aBuilder->ToReferenceFrame(child));
4260 awayFromCommonPath = true;
4263 child->MarkAbsoluteFramesForDisplayList(aBuilder);
4264 child->SetBuiltDisplayList(true);
4266 // Some SVG frames might change opacity without invalidating the frame, so
4267 // exclude them from the fast-path.
4268 if (!awayFromCommonPath && !child->IsSVGFrame()) {
4269 // The shortcut is available for the child for next time.
4270 child->AddStateBits(NS_FRAME_SIMPLE_DISPLAYLIST);
4273 if (!pseudoStackingContext) {
4274 // THIS IS THE COMMON CASE.
4275 // Not a pseudo or real stacking context. Do the simple thing and
4276 // return early.
4277 aBuilder->AdjustWindowDraggingRegion(child);
4278 aBuilder->Check();
4279 child->BuildDisplayList(aBuilder, aLists);
4280 aBuilder->Check();
4281 aBuilder->DisplayCaret(child, aLists.Outlines());
4282 return;
4285 // A pseudo-stacking context (e.g., a positioned element with z-index auto).
4286 // We allow positioned descendants of the child to escape to our parent
4287 // stacking context's positioned descendant list, because they might be
4288 // z-index:non-auto
4289 nsDisplayListCollection pseudoStack(aBuilder);
4291 aBuilder->AdjustWindowDraggingRegion(child);
4292 nsDisplayListBuilder::AutoContainerASRTracker contASRTracker(aBuilder);
4293 aBuilder->Check();
4294 child->BuildDisplayList(aBuilder, pseudoStack);
4295 aBuilder->Check();
4296 if (aBuilder->DisplayCaret(child, pseudoStack.Outlines())) {
4297 builtContainerItem = false;
4299 wrapListASR = contASRTracker.GetContainerASR();
4301 list.AppendToTop(pseudoStack.BorderBackground());
4302 list.AppendToTop(pseudoStack.BlockBorderBackgrounds());
4303 list.AppendToTop(pseudoStack.Floats());
4304 list.AppendToTop(pseudoStack.Content());
4305 list.AppendToTop(pseudoStack.Outlines());
4306 extraPositionedDescendants.AppendToTop(pseudoStack.PositionedDescendants());
4309 buildingForChild.RestoreBuildingInvisibleItemsValue();
4311 if (!list.IsEmpty()) {
4312 if (isPositioned || isStackingContext) {
4313 // Genuine stacking contexts, and positioned pseudo-stacking-contexts,
4314 // go in this level.
4315 nsDisplayItem* item = WrapInWrapList(aBuilder, child, &list, wrapListASR,
4316 builtContainerItem);
4317 if (isSVG) {
4318 aLists.Content()->AppendToTop(item);
4319 } else {
4320 aLists.PositionedDescendants()->AppendToTop(item);
4322 } else if (!isSVG && disp->IsFloating(child)) {
4323 aLists.Floats()->AppendToTop(
4324 WrapInWrapList(aBuilder, child, &list, wrapListASR));
4325 } else {
4326 aLists.Content()->AppendToTop(&list);
4329 // We delay placing the positioned descendants of positioned frames to here,
4330 // because in the absence of z-index this is the correct order for them.
4331 // This doesn't affect correctness because the positioned descendants list
4332 // is sorted by z-order and content in BuildDisplayListForStackingContext,
4333 // but it means that sort routine needs to do less work.
4334 aLists.PositionedDescendants()->AppendToTop(&extraPositionedDescendants);
4337 void nsIFrame::MarkAbsoluteFramesForDisplayList(
4338 nsDisplayListBuilder* aBuilder) {
4339 if (IsAbsoluteContainer()) {
4340 aBuilder->MarkFramesForDisplayList(
4341 this, GetAbsoluteContainingBlock()->GetChildList());
4345 nsresult nsIFrame::GetContentForEvent(const WidgetEvent* aEvent,
4346 nsIContent** aContent) {
4347 nsIFrame* f = nsLayoutUtils::GetNonGeneratedAncestor(this);
4348 *aContent = f->GetContent();
4349 NS_IF_ADDREF(*aContent);
4350 return NS_OK;
4353 void nsIFrame::FireDOMEvent(const nsAString& aDOMEventName,
4354 nsIContent* aContent) {
4355 nsIContent* target = aContent ? aContent : GetContent();
4357 if (target) {
4358 RefPtr<AsyncEventDispatcher> asyncDispatcher = new AsyncEventDispatcher(
4359 target, aDOMEventName, CanBubble::eYes, ChromeOnlyDispatch::eNo);
4360 DebugOnly<nsresult> rv = asyncDispatcher->PostDOMEvent();
4361 NS_ASSERTION(NS_SUCCEEDED(rv), "AsyncEventDispatcher failed to dispatch");
4365 nsresult nsIFrame::HandleEvent(nsPresContext* aPresContext,
4366 WidgetGUIEvent* aEvent,
4367 nsEventStatus* aEventStatus) {
4368 if (aEvent->mMessage == eMouseMove) {
4369 // XXX If the second argument of HandleDrag() is WidgetMouseEvent,
4370 // the implementation becomes simpler.
4371 return HandleDrag(aPresContext, aEvent, aEventStatus);
4374 if ((aEvent->mClass == eMouseEventClass &&
4375 aEvent->AsMouseEvent()->mButton == MouseButton::ePrimary) ||
4376 aEvent->mClass == eTouchEventClass) {
4377 if (aEvent->mMessage == eMouseDown || aEvent->mMessage == eTouchStart) {
4378 HandlePress(aPresContext, aEvent, aEventStatus);
4379 } else if (aEvent->mMessage == eMouseUp || aEvent->mMessage == eTouchEnd) {
4380 HandleRelease(aPresContext, aEvent, aEventStatus);
4382 return NS_OK;
4385 // When secondary buttion is down, we need to move selection to make users
4386 // possible to paste something at click point quickly.
4387 // When middle button is down, we need to just move selection and focus at
4388 // the clicked point. Note that even if middle click paste is not enabled,
4389 // Chrome moves selection at middle mouse button down. So, we should follow
4390 // the behavior for the compatibility.
4391 if (aEvent->mMessage == eMouseDown) {
4392 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
4393 if (mouseEvent && (mouseEvent->mButton == MouseButton::eSecondary ||
4394 mouseEvent->mButton == MouseButton::eMiddle)) {
4395 if (*aEventStatus == nsEventStatus_eConsumeNoDefault) {
4396 return NS_OK;
4398 return MoveCaretToEventPoint(aPresContext, mouseEvent, aEventStatus);
4402 return NS_OK;
4405 nsresult nsIFrame::GetDataForTableSelection(
4406 const nsFrameSelection* aFrameSelection, mozilla::PresShell* aPresShell,
4407 WidgetMouseEvent* aMouseEvent, nsIContent** aParentContent,
4408 int32_t* aContentOffset, TableSelectionMode* aTarget) {
4409 if (!aFrameSelection || !aPresShell || !aMouseEvent || !aParentContent ||
4410 !aContentOffset || !aTarget)
4411 return NS_ERROR_NULL_POINTER;
4413 *aParentContent = nullptr;
4414 *aContentOffset = 0;
4415 *aTarget = TableSelectionMode::None;
4417 int16_t displaySelection = aPresShell->GetSelectionFlags();
4419 bool selectingTableCells = aFrameSelection->IsInTableSelectionMode();
4421 // DISPLAY_ALL means we're in an editor.
4422 // If already in cell selection mode,
4423 // continue selecting with mouse drag or end on mouse up,
4424 // or when using shift key to extend block of cells
4425 // (Mouse down does normal selection unless Ctrl/Cmd is pressed)
4426 bool doTableSelection =
4427 displaySelection == nsISelectionDisplay::DISPLAY_ALL &&
4428 selectingTableCells &&
4429 (aMouseEvent->mMessage == eMouseMove ||
4430 (aMouseEvent->mMessage == eMouseUp &&
4431 aMouseEvent->mButton == MouseButton::ePrimary) ||
4432 aMouseEvent->IsShift());
4434 if (!doTableSelection) {
4435 // In Browser, special 'table selection' key must be pressed for table
4436 // selection or when just Shift is pressed and we're already in table/cell
4437 // selection mode
4438 #ifdef XP_MACOSX
4439 doTableSelection = aMouseEvent->IsMeta() ||
4440 (aMouseEvent->IsShift() && selectingTableCells);
4441 #else
4442 doTableSelection = aMouseEvent->IsControl() ||
4443 (aMouseEvent->IsShift() && selectingTableCells);
4444 #endif
4446 if (!doTableSelection) return NS_OK;
4448 // Get the cell frame or table frame (or parent) of the current content node
4449 nsIFrame* frame = this;
4450 bool foundCell = false;
4451 bool foundTable = false;
4453 // Get the limiting node to stop parent frame search
4454 nsIContent* limiter = aFrameSelection->GetLimiter();
4456 // If our content node is an ancestor of the limiting node,
4457 // we should stop the search right now.
4458 if (limiter && limiter->IsInclusiveDescendantOf(GetContent())) return NS_OK;
4460 // We don't initiate row/col selection from here now,
4461 // but we may in future
4462 // bool selectColumn = false;
4463 // bool selectRow = false;
4465 while (frame) {
4466 // Check for a table cell by querying to a known CellFrame interface
4467 nsITableCellLayout* cellElement = do_QueryFrame(frame);
4468 if (cellElement) {
4469 foundCell = true;
4470 // TODO: If we want to use proximity to top or left border
4471 // for row and column selection, this is the place to do it
4472 break;
4473 } else {
4474 // If not a cell, check for table
4475 // This will happen when starting frame is the table or child of a table,
4476 // such as a row (we were inbetween cells or in table border)
4477 nsTableWrapperFrame* tableFrame = do_QueryFrame(frame);
4478 if (tableFrame) {
4479 foundTable = true;
4480 // TODO: How can we select row when along left table edge
4481 // or select column when along top edge?
4482 break;
4483 } else {
4484 frame = frame->GetParent();
4485 // Stop if we have hit the selection's limiting content node
4486 if (frame && frame->GetContent() == limiter) break;
4490 // We aren't in a cell or table
4491 if (!foundCell && !foundTable) return NS_OK;
4493 nsIContent* tableOrCellContent = frame->GetContent();
4494 if (!tableOrCellContent) return NS_ERROR_FAILURE;
4496 nsCOMPtr<nsIContent> parentContent = tableOrCellContent->GetParent();
4497 if (!parentContent) return NS_ERROR_FAILURE;
4499 const int32_t offset =
4500 parentContent->ComputeIndexOf_Deprecated(tableOrCellContent);
4501 // Not likely?
4502 if (offset < 0) {
4503 return NS_ERROR_FAILURE;
4506 // Everything is OK -- set the return values
4507 parentContent.forget(aParentContent);
4509 *aContentOffset = offset;
4511 #if 0
4512 if (selectRow)
4513 *aTarget = TableSelectionMode::Row;
4514 else if (selectColumn)
4515 *aTarget = TableSelectionMode::Column;
4516 else
4517 #endif
4518 if (foundCell) {
4519 *aTarget = TableSelectionMode::Cell;
4520 } else if (foundTable) {
4521 *aTarget = TableSelectionMode::Table;
4524 return NS_OK;
4527 static bool IsEditingHost(const nsIFrame* aFrame) {
4528 nsIContent* content = aFrame->GetContent();
4529 return content && content->IsEditingHost();
4532 static StyleUserSelect UsedUserSelect(const nsIFrame* aFrame) {
4533 if (aFrame->IsGeneratedContentFrame()) {
4534 return StyleUserSelect::None;
4537 // Per https://drafts.csswg.org/css-ui-4/#content-selection:
4539 // The used value is the same as the computed value, except:
4541 // 1 - on editable elements where the used value is always 'contain'
4542 // regardless of the computed value
4543 // 2 - when the computed value is auto, in which case the used value is one
4544 // of the other values...
4546 // See https://github.com/w3c/csswg-drafts/issues/3344 to see why we do this
4547 // at used-value time instead of at computed-value time.
4549 if (aFrame->IsTextInputFrame() || IsEditingHost(aFrame)) {
4550 // We don't implement 'contain' itself, but we make 'text' behave as
4551 // 'contain' for contenteditable and <input> / <textarea> elements anyway so
4552 // this is ok.
4553 return StyleUserSelect::Text;
4556 auto style = aFrame->Style()->UserSelect();
4557 if (style != StyleUserSelect::Auto) {
4558 return style;
4561 auto* parent = nsLayoutUtils::GetParentOrPlaceholderFor(aFrame);
4562 return parent ? UsedUserSelect(parent) : StyleUserSelect::Text;
4565 bool nsIFrame::IsSelectable(StyleUserSelect* aSelectStyle) const {
4566 auto style = UsedUserSelect(this);
4567 if (aSelectStyle) {
4568 *aSelectStyle = style;
4570 return style != StyleUserSelect::None;
4573 bool nsIFrame::ShouldHaveLineIfEmpty() const {
4574 if (Style()->IsPseudoOrAnonBox() &&
4575 Style()->GetPseudoType() != PseudoStyleType::scrolledContent) {
4576 return false;
4578 return IsEditingHost(this);
4582 * Handles the Mouse Press Event for the frame
4584 NS_IMETHODIMP
4585 nsIFrame::HandlePress(nsPresContext* aPresContext, WidgetGUIEvent* aEvent,
4586 nsEventStatus* aEventStatus) {
4587 NS_ENSURE_ARG_POINTER(aEventStatus);
4588 if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
4589 return NS_OK;
4592 NS_ENSURE_ARG_POINTER(aEvent);
4593 if (aEvent->mClass == eTouchEventClass) {
4594 return NS_OK;
4597 return MoveCaretToEventPoint(aPresContext, aEvent->AsMouseEvent(),
4598 aEventStatus);
4601 nsresult nsIFrame::MoveCaretToEventPoint(nsPresContext* aPresContext,
4602 WidgetMouseEvent* aMouseEvent,
4603 nsEventStatus* aEventStatus) {
4604 MOZ_ASSERT(aPresContext);
4605 MOZ_ASSERT(aMouseEvent);
4606 MOZ_ASSERT(aMouseEvent->mMessage == eMouseDown);
4607 MOZ_ASSERT(aEventStatus);
4608 MOZ_ASSERT(nsEventStatus_eConsumeNoDefault != *aEventStatus);
4610 mozilla::PresShell* presShell = aPresContext->GetPresShell();
4611 if (!presShell) {
4612 return NS_ERROR_FAILURE;
4615 // We often get out of sync state issues with mousedown events that
4616 // get interrupted by alerts/dialogs.
4617 // Check with the ESM to see if we should process this one
4618 if (!aPresContext->EventStateManager()->EventStatusOK(aMouseEvent)) {
4619 return NS_OK;
4622 const nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
4623 aMouseEvent, RelativeTo{this});
4625 // When not using `alt`, and clicking on a draggable, but non-editable
4626 // element, don't do anything, and let d&d handle the event.
4628 // See bug 48876, bug 388659 and bug 55921 for context here.
4630 // FIXME(emilio): The .Contains(pt) check looks a bit fishy. When would it be
4631 // false given we're the event target? If it is needed, why not checking the
4632 // actual draggable node rect instead?
4633 if (!aMouseEvent->IsAlt() && GetRectRelativeToSelf().Contains(pt)) {
4634 for (nsIContent* content = mContent; content;
4635 content = content->GetFlattenedTreeParent()) {
4636 if (nsContentUtils::ContentIsDraggable(content) &&
4637 !content->IsEditable()) {
4638 return NS_OK;
4643 // If we are in Navigator and the click is in a draggable node, we don't want
4644 // to start selection because we don't want to interfere with a potential
4645 // drag of said node and steal all its glory.
4646 const bool isEditor =
4647 presShell->GetSelectionFlags() == nsISelectionDisplay::DISPLAY_ALL;
4649 // Don't do something if it's middle button down event.
4650 const bool isPrimaryButtonDown =
4651 aMouseEvent->mButton == MouseButton::ePrimary;
4653 // check whether style allows selection
4654 // if not, don't tell selection the mouse event even occurred.
4655 StyleUserSelect selectStyle;
4656 // check for select: none
4657 if (!IsSelectable(&selectStyle)) {
4658 return NS_OK;
4661 if (isPrimaryButtonDown) {
4662 // If the mouse is dragged outside the nearest enclosing scrollable area
4663 // while making a selection, the area will be scrolled. To do this, capture
4664 // the mouse on the nearest scrollable frame. If there isn't a scrollable
4665 // frame, or something else is already capturing the mouse, there's no
4666 // reason to capture.
4667 if (!PresShell::GetCapturingContent()) {
4668 nsIScrollableFrame* scrollFrame =
4669 nsLayoutUtils::GetNearestScrollableFrame(
4670 this, nsLayoutUtils::SCROLLABLE_SAME_DOC |
4671 nsLayoutUtils::SCROLLABLE_INCLUDE_HIDDEN);
4672 if (scrollFrame) {
4673 nsIFrame* capturingFrame = do_QueryFrame(scrollFrame);
4674 PresShell::SetCapturingContent(capturingFrame->GetContent(),
4675 CaptureFlags::IgnoreAllowedState);
4680 // XXX This is screwy; it really should use the selection frame, not the
4681 // event frame
4682 const nsFrameSelection* frameselection =
4683 selectStyle == StyleUserSelect::Text ? GetConstFrameSelection()
4684 : presShell->ConstFrameSelection();
4686 if (!frameselection || frameselection->GetDisplaySelection() ==
4687 nsISelectionController::SELECTION_OFF) {
4688 return NS_OK; // nothing to do we cannot affect selection from here
4691 #ifdef XP_MACOSX
4692 // If Control key is pressed on macOS, it should be treated as right click.
4693 // So, don't change selection.
4694 if (aMouseEvent->IsControl()) {
4695 return NS_OK;
4697 const bool control = aMouseEvent->IsMeta();
4698 #else
4699 const bool control = aMouseEvent->IsControl();
4700 #endif
4702 RefPtr<nsFrameSelection> fc = const_cast<nsFrameSelection*>(frameselection);
4703 if (isPrimaryButtonDown && aMouseEvent->mClickCount > 1) {
4704 // These methods aren't const but can't actually delete anything,
4705 // so no need for AutoWeakFrame.
4706 fc->SetDragState(true);
4707 return HandleMultiplePress(aPresContext, aMouseEvent, aEventStatus,
4708 control);
4711 ContentOffsets offsets = GetContentOffsetsFromPoint(pt, SKIP_HIDDEN);
4713 if (!offsets.content) {
4714 return NS_ERROR_FAILURE;
4717 if (aMouseEvent->mButton == MouseButton::eSecondary &&
4718 !MovingCaretToEventPointAllowedIfSecondaryButtonEvent(
4719 *frameselection, *aMouseEvent, *offsets.content,
4720 // When we collapse selection in nsFrameSelection::TakeFocus,
4721 // we always collapse selection to the start offset. Therefore,
4722 // we can ignore the end offset here. E.g., when an <img> is clicked,
4723 // set the primary offset to after it, but the the secondary offset
4724 // may be before it, see OffsetsForSingleFrame for the detail.
4725 offsets.StartOffset())) {
4726 return NS_OK;
4729 if (aMouseEvent->mMessage == eMouseDown &&
4730 aMouseEvent->mButton == MouseButton::eMiddle &&
4731 !offsets.content->IsEditable()) {
4732 // However, some users don't like the Chrome compatible behavior of
4733 // middle mouse click. They want to keep selection after starting
4734 // autoscroll. However, the selection change is important for middle
4735 // mouse past. Therefore, we should allow users to take the traditional
4736 // behavior back by themselves unless middle click paste is enabled or
4737 // autoscrolling is disabled.
4738 if (!Preferences::GetBool("middlemouse.paste", false) &&
4739 Preferences::GetBool("general.autoScroll", false) &&
4740 Preferences::GetBool("general.autoscroll.prevent_to_collapse_selection_"
4741 "by_middle_mouse_down",
4742 false)) {
4743 return NS_OK;
4747 if (isPrimaryButtonDown) {
4748 // Let Ctrl/Cmd + left mouse down do table selection instead of drag
4749 // initiation.
4750 nsCOMPtr<nsIContent> parentContent;
4751 int32_t contentOffset;
4752 TableSelectionMode target;
4753 nsresult rv = GetDataForTableSelection(
4754 frameselection, presShell, aMouseEvent, getter_AddRefs(parentContent),
4755 &contentOffset, &target);
4756 if (NS_SUCCEEDED(rv) && parentContent) {
4757 fc->SetDragState(true);
4758 return fc->HandleTableSelection(parentContent, contentOffset, target,
4759 aMouseEvent);
4763 fc->SetDelayedCaretData(0);
4765 if (isPrimaryButtonDown) {
4766 // Check if any part of this frame is selected, and if the user clicked
4767 // inside the selected region, and if it's the left button. If so, we delay
4768 // starting a new selection since the user may be trying to drag the
4769 // selected region to some other app.
4771 if (GetContent() && GetContent()->IsMaybeSelected()) {
4772 bool inSelection = false;
4773 UniquePtr<SelectionDetails> details = frameselection->LookUpSelection(
4774 offsets.content, 0, offsets.EndOffset(), false);
4777 // If there are any details, check to see if the user clicked
4778 // within any selected region of the frame.
4781 for (SelectionDetails* curDetail = details.get(); curDetail;
4782 curDetail = curDetail->mNext.get()) {
4784 // If the user clicked inside a selection, then just
4785 // return without doing anything. We will handle placing
4786 // the caret later on when the mouse is released. We ignore
4787 // the spellcheck, find and url formatting selections.
4789 if (curDetail->mSelectionType != SelectionType::eSpellCheck &&
4790 curDetail->mSelectionType != SelectionType::eFind &&
4791 curDetail->mSelectionType != SelectionType::eURLSecondary &&
4792 curDetail->mSelectionType != SelectionType::eURLStrikeout &&
4793 curDetail->mSelectionType != SelectionType::eHighlight &&
4794 curDetail->mStart <= offsets.StartOffset() &&
4795 offsets.EndOffset() <= curDetail->mEnd) {
4796 inSelection = true;
4800 if (inSelection) {
4801 fc->SetDragState(false);
4802 fc->SetDelayedCaretData(aMouseEvent);
4803 return NS_OK;
4807 fc->SetDragState(true);
4810 // Do not touch any nsFrame members after this point without adding
4811 // weakFrame checks.
4812 const nsFrameSelection::FocusMode focusMode = [&]() {
4813 // If "Shift" and "Ctrl" are both pressed, "Shift" is given precedence. This
4814 // mimics the old behaviour.
4815 if (aMouseEvent->IsShift()) {
4816 // If clicked in a link when focused content is editable, we should
4817 // collapse selection in the link for compatibility with Blink.
4818 if (isEditor) {
4819 for (Element* element : mContent->InclusiveAncestorsOfType<Element>()) {
4820 if (element->IsLink()) {
4821 return nsFrameSelection::FocusMode::kCollapseToNewPoint;
4825 return nsFrameSelection::FocusMode::kExtendSelection;
4828 if (isPrimaryButtonDown && control) {
4829 return nsFrameSelection::FocusMode::kMultiRangeSelection;
4832 return nsFrameSelection::FocusMode::kCollapseToNewPoint;
4833 }();
4835 nsresult rv = fc->HandleClick(
4836 MOZ_KnownLive(offsets.content) /* bug 1636889 */, offsets.StartOffset(),
4837 offsets.EndOffset(), focusMode, offsets.associate);
4838 if (NS_FAILED(rv)) {
4839 return rv;
4842 // We don't handle mouse button up if it's middle button.
4843 if (isPrimaryButtonDown && offsets.offset != offsets.secondaryOffset) {
4844 fc->MaintainSelection();
4847 if (isPrimaryButtonDown && isEditor && !aMouseEvent->IsShift() &&
4848 (offsets.EndOffset() - offsets.StartOffset()) == 1) {
4849 // A single node is selected and we aren't extending an existing selection,
4850 // which means the user clicked directly on an object (either
4851 // `user-select: all` or a non-text node without children). Therefore,
4852 // disable selection extension during mouse moves.
4853 // XXX This is a bit hacky; shouldn't editor be able to deal with this?
4854 fc->SetDragState(false);
4857 return NS_OK;
4860 bool nsIFrame::MovingCaretToEventPointAllowedIfSecondaryButtonEvent(
4861 const nsFrameSelection& aFrameSelection,
4862 WidgetMouseEvent& aSecondaryButtonEvent,
4863 const nsIContent& aContentAtEventPoint, int32_t aOffsetAtEventPoint) const {
4864 MOZ_ASSERT(aSecondaryButtonEvent.mButton == MouseButton::eSecondary);
4866 if (NS_WARN_IF(aOffsetAtEventPoint < 0)) {
4867 return false;
4870 Selection* selection = aFrameSelection.GetSelection(SelectionType::eNormal);
4871 if (selection && !selection->IsCollapsed()) {
4872 // If right click in a selection range, we should not collapse selection.
4873 if (nsContentUtils::IsPointInSelection(
4874 *selection, aContentAtEventPoint,
4875 static_cast<uint32_t>(aOffsetAtEventPoint))) {
4876 return false;
4879 if (StaticPrefs::
4880 ui_mouse_right_click_collapse_selection_stop_if_non_collapsed_selection()) {
4881 // If currently selection is limited in an editing host, we should not
4882 // collapse selection if the clicked point is in the ancestor limiter.
4883 // Otherwise, this mouse click moves focus from the editing host to
4884 // different one or blur the editing host. In this case, we need to
4885 // update selection because keeping current selection in the editing
4886 // host looks like it's not blurred.
4887 // FIXME: If the active editing host is the document element, editor
4888 // does not set ancestor limiter properly. Fix it in the editor side.
4889 if (nsIContent* ancestorLimiter = selection->GetAncestorLimiter()) {
4890 MOZ_ASSERT(ancestorLimiter->IsEditable());
4891 return !aContentAtEventPoint.IsInclusiveDescendantOf(ancestorLimiter);
4893 // If currently selection is not limited in an editing host, we should
4894 // collapse selection only when this click moves focus to an editing
4895 // host because we need to update selection in this case.
4896 if (!aContentAtEventPoint.IsEditable()) {
4897 return false;
4902 return !StaticPrefs::
4903 ui_mouse_right_click_collapse_selection_stop_if_non_editable_node() ||
4904 // The user does not want to collapse selection into non-editable
4905 // content by a right button click.
4906 aContentAtEventPoint.IsEditable() ||
4907 // Treat clicking in a text control as always clicked on editable
4908 // content because we want a hack only for clicking in normal text
4909 // nodes which is outside any editing hosts.
4910 aContentAtEventPoint.IsTextControlElement() ||
4911 TextControlElement::FromNodeOrNull(
4912 aContentAtEventPoint.GetClosestNativeAnonymousSubtreeRoot());
4915 nsresult nsIFrame::SelectByTypeAtPoint(nsPresContext* aPresContext,
4916 const nsPoint& aPoint,
4917 nsSelectionAmount aBeginAmountType,
4918 nsSelectionAmount aEndAmountType,
4919 uint32_t aSelectFlags) {
4920 NS_ENSURE_ARG_POINTER(aPresContext);
4922 // No point in selecting if selection is turned off
4923 if (DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF) {
4924 return NS_OK;
4927 ContentOffsets offsets = GetContentOffsetsFromPoint(aPoint, SKIP_HIDDEN);
4928 if (!offsets.content) {
4929 return NS_ERROR_FAILURE;
4932 int32_t offset;
4933 nsIFrame* frame = nsFrameSelection::GetFrameForNodeOffset(
4934 offsets.content, offsets.offset, offsets.associate, &offset);
4935 if (!frame) {
4936 return NS_ERROR_FAILURE;
4938 return frame->PeekBackwardAndForward(aBeginAmountType, aEndAmountType, offset,
4939 aBeginAmountType != eSelectWord,
4940 aSelectFlags);
4944 * Multiple Mouse Press -- line or paragraph selection -- for the frame.
4945 * Wouldn't it be nice if this didn't have to be hardwired into Frame code?
4947 NS_IMETHODIMP
4948 nsIFrame::HandleMultiplePress(nsPresContext* aPresContext,
4949 WidgetGUIEvent* aEvent,
4950 nsEventStatus* aEventStatus, bool aControlHeld) {
4951 NS_ENSURE_ARG_POINTER(aEvent);
4952 NS_ENSURE_ARG_POINTER(aEventStatus);
4954 if (nsEventStatus_eConsumeNoDefault == *aEventStatus ||
4955 DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF) {
4956 return NS_OK;
4959 // Find out whether we're doing line or paragraph selection.
4960 // If browser.triple_click_selects_paragraph is true, triple-click selects
4961 // paragraph. Otherwise, triple-click selects line, and quadruple-click
4962 // selects paragraph (on platforms that support quadruple-click).
4963 nsSelectionAmount beginAmount, endAmount;
4964 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
4965 if (!mouseEvent) {
4966 return NS_OK;
4969 if (mouseEvent->mClickCount == 4) {
4970 beginAmount = endAmount = eSelectParagraph;
4971 } else if (mouseEvent->mClickCount == 3) {
4972 if (Preferences::GetBool("browser.triple_click_selects_paragraph")) {
4973 beginAmount = endAmount = eSelectParagraph;
4974 } else {
4975 beginAmount = eSelectBeginLine;
4976 endAmount = eSelectEndLine;
4978 } else if (mouseEvent->mClickCount == 2) {
4979 // We only want inline frames; PeekBackwardAndForward dislikes blocks
4980 beginAmount = endAmount = eSelectWord;
4981 } else {
4982 return NS_OK;
4985 nsPoint relPoint = nsLayoutUtils::GetEventCoordinatesRelativeTo(
4986 mouseEvent, RelativeTo{this});
4987 return SelectByTypeAtPoint(aPresContext, relPoint, beginAmount, endAmount,
4988 (aControlHeld ? SELECT_ACCUMULATE : 0));
4991 nsresult nsIFrame::PeekBackwardAndForward(nsSelectionAmount aAmountBack,
4992 nsSelectionAmount aAmountForward,
4993 int32_t aStartPos, bool aJumpLines,
4994 uint32_t aSelectFlags) {
4995 nsIFrame* baseFrame = this;
4996 int32_t baseOffset = aStartPos;
4997 nsresult rv;
4999 PeekOffsetOptions peekOffsetOptions{PeekOffsetOption::StopAtScroller};
5000 if (aJumpLines) {
5001 peekOffsetOptions += PeekOffsetOption::JumpLines;
5004 if (aAmountBack == eSelectWord) {
5005 // To avoid selecting the previous word when at start of word,
5006 // first move one character forward.
5007 PeekOffsetStruct pos(eSelectCharacter, eDirNext, aStartPos, nsPoint(0, 0),
5008 peekOffsetOptions);
5009 rv = PeekOffset(&pos);
5010 if (NS_SUCCEEDED(rv)) {
5011 baseFrame = pos.mResultFrame;
5012 baseOffset = pos.mContentOffset;
5016 // Search backward for a boundary.
5017 PeekOffsetStruct startpos(aAmountBack, eDirPrevious, baseOffset,
5018 nsPoint(0, 0), peekOffsetOptions);
5019 rv = baseFrame->PeekOffset(&startpos);
5020 if (NS_FAILED(rv)) {
5021 return rv;
5024 // If the backward search stayed within the same frame, search forward from
5025 // that position for the end boundary; but if it crossed out to a sibling or
5026 // ancestor, start from the original position.
5027 if (startpos.mResultFrame == baseFrame) {
5028 baseOffset = startpos.mContentOffset;
5029 } else {
5030 baseFrame = this;
5031 baseOffset = aStartPos;
5034 PeekOffsetStruct endpos(aAmountForward, eDirNext, baseOffset, nsPoint(0, 0),
5035 peekOffsetOptions);
5036 rv = baseFrame->PeekOffset(&endpos);
5037 if (NS_FAILED(rv)) {
5038 return rv;
5041 // Keep frameSelection alive.
5042 RefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
5044 const nsFrameSelection::FocusMode focusMode =
5045 (aSelectFlags & SELECT_ACCUMULATE)
5046 ? nsFrameSelection::FocusMode::kMultiRangeSelection
5047 : nsFrameSelection::FocusMode::kCollapseToNewPoint;
5048 rv = frameSelection->HandleClick(
5049 MOZ_KnownLive(startpos.mResultContent) /* bug 1636889 */,
5050 startpos.mContentOffset, startpos.mContentOffset, focusMode,
5051 CARET_ASSOCIATE_AFTER);
5052 if (NS_FAILED(rv)) {
5053 return rv;
5056 rv = frameSelection->HandleClick(
5057 MOZ_KnownLive(endpos.mResultContent) /* bug 1636889 */,
5058 endpos.mContentOffset, endpos.mContentOffset,
5059 nsFrameSelection::FocusMode::kExtendSelection, CARET_ASSOCIATE_BEFORE);
5060 if (NS_FAILED(rv)) {
5061 return rv;
5063 if (aAmountBack == eSelectWord) {
5064 frameSelection->SetIsDoubleClickSelection(true);
5067 // maintain selection
5068 return frameSelection->MaintainSelection(aAmountBack);
5071 NS_IMETHODIMP nsIFrame::HandleDrag(nsPresContext* aPresContext,
5072 WidgetGUIEvent* aEvent,
5073 nsEventStatus* aEventStatus) {
5074 MOZ_ASSERT(aEvent->mClass == eMouseEventClass,
5075 "HandleDrag can only handle mouse event");
5077 NS_ENSURE_ARG_POINTER(aEventStatus);
5079 RefPtr<nsFrameSelection> frameselection = GetFrameSelection();
5080 if (!frameselection) {
5081 return NS_OK;
5084 bool mouseDown = frameselection->GetDragState();
5085 if (!mouseDown) {
5086 return NS_OK;
5089 nsIFrame* scrollbar =
5090 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::Scrollbar);
5091 if (!scrollbar) {
5092 // XXX Do we really need to exclude non-selectable content here?
5093 // GetContentOffsetsFromPoint can handle it just fine, although some
5094 // other stuff might not like it.
5095 // NOTE: DetermineDisplaySelection() returns SELECTION_OFF for
5096 // non-selectable frames.
5097 if (DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF) {
5098 return NS_OK;
5102 frameselection->StopAutoScrollTimer();
5104 // Check if we are dragging in a table cell
5105 nsCOMPtr<nsIContent> parentContent;
5106 int32_t contentOffset;
5107 TableSelectionMode target;
5108 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
5109 mozilla::PresShell* presShell = aPresContext->PresShell();
5110 nsresult result;
5111 result = GetDataForTableSelection(frameselection, presShell, mouseEvent,
5112 getter_AddRefs(parentContent),
5113 &contentOffset, &target);
5115 AutoWeakFrame weakThis = this;
5116 if (NS_SUCCEEDED(result) && parentContent) {
5117 result = frameselection->HandleTableSelection(parentContent, contentOffset,
5118 target, mouseEvent);
5119 if (NS_WARN_IF(NS_FAILED(result))) {
5120 return result;
5122 } else {
5123 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mouseEvent,
5124 RelativeTo{this});
5125 frameselection->HandleDrag(this, pt);
5128 // The frameselection object notifies selection listeners synchronously above
5129 // which might have killed us.
5130 if (!weakThis.IsAlive()) {
5131 return NS_OK;
5134 // get the nearest scrollframe
5135 nsIScrollableFrame* scrollFrame = nsLayoutUtils::GetNearestScrollableFrame(
5136 this, nsLayoutUtils::SCROLLABLE_SAME_DOC |
5137 nsLayoutUtils::SCROLLABLE_INCLUDE_HIDDEN);
5139 if (scrollFrame) {
5140 nsIFrame* capturingFrame = scrollFrame->GetScrolledFrame();
5141 if (capturingFrame) {
5142 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
5143 mouseEvent, RelativeTo{capturingFrame});
5144 frameselection->StartAutoScrollTimer(capturingFrame, pt, 30);
5148 return NS_OK;
5152 * This static method handles part of the nsIFrame::HandleRelease in a way
5153 * which doesn't rely on the nsFrame object to stay alive.
5155 MOZ_CAN_RUN_SCRIPT_BOUNDARY static nsresult HandleFrameSelection(
5156 nsFrameSelection* aFrameSelection, nsIFrame::ContentOffsets& aOffsets,
5157 bool aHandleTableSel, int32_t aContentOffsetForTableSel,
5158 TableSelectionMode aTargetForTableSel,
5159 nsIContent* aParentContentForTableSel, WidgetGUIEvent* aEvent,
5160 const nsEventStatus* aEventStatus) {
5161 if (!aFrameSelection) {
5162 return NS_OK;
5165 nsresult rv = NS_OK;
5167 if (nsEventStatus_eConsumeNoDefault != *aEventStatus) {
5168 if (!aHandleTableSel) {
5169 if (!aOffsets.content || !aFrameSelection->HasDelayedCaretData()) {
5170 return NS_ERROR_FAILURE;
5173 // We are doing this to simulate what we would have done on HandlePress.
5174 // We didn't do it there to give the user an opportunity to drag
5175 // the text, but since they didn't drag, we want to place the
5176 // caret.
5177 // However, we'll use the mouse position from the release, since:
5178 // * it's easier
5179 // * that's the normal click position to use (although really, in
5180 // the normal case, small movements that don't count as a drag
5181 // can do selection)
5182 aFrameSelection->SetDragState(true);
5184 const nsFrameSelection::FocusMode focusMode =
5185 aFrameSelection->IsShiftDownInDelayedCaretData()
5186 ? nsFrameSelection::FocusMode::kExtendSelection
5187 : nsFrameSelection::FocusMode::kCollapseToNewPoint;
5188 rv = aFrameSelection->HandleClick(
5189 MOZ_KnownLive(aOffsets.content) /* bug 1636889 */,
5190 aOffsets.StartOffset(), aOffsets.EndOffset(), focusMode,
5191 aOffsets.associate);
5192 if (NS_FAILED(rv)) {
5193 return rv;
5195 } else if (aParentContentForTableSel) {
5196 aFrameSelection->SetDragState(false);
5197 rv = aFrameSelection->HandleTableSelection(
5198 aParentContentForTableSel, aContentOffsetForTableSel,
5199 aTargetForTableSel, aEvent->AsMouseEvent());
5200 if (NS_FAILED(rv)) {
5201 return rv;
5204 aFrameSelection->SetDelayedCaretData(0);
5207 aFrameSelection->SetDragState(false);
5208 aFrameSelection->StopAutoScrollTimer();
5210 return NS_OK;
5213 NS_IMETHODIMP nsIFrame::HandleRelease(nsPresContext* aPresContext,
5214 WidgetGUIEvent* aEvent,
5215 nsEventStatus* aEventStatus) {
5216 if (aEvent->mClass != eMouseEventClass) {
5217 return NS_OK;
5220 nsIFrame* activeFrame = GetActiveSelectionFrame(aPresContext, this);
5222 nsCOMPtr<nsIContent> captureContent = PresShell::GetCapturingContent();
5224 bool selectionOff =
5225 (DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF);
5227 RefPtr<nsFrameSelection> frameselection;
5228 ContentOffsets offsets;
5229 nsCOMPtr<nsIContent> parentContent;
5230 int32_t contentOffsetForTableSel = 0;
5231 TableSelectionMode targetForTableSel = TableSelectionMode::None;
5232 bool handleTableSelection = true;
5234 if (!selectionOff) {
5235 frameselection = GetFrameSelection();
5236 if (nsEventStatus_eConsumeNoDefault != *aEventStatus && frameselection) {
5237 // Check if the frameselection recorded the mouse going down.
5238 // If not, the user must have clicked in a part of the selection.
5239 // Place the caret before continuing!
5241 if (frameselection->MouseDownRecorded()) {
5242 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
5243 aEvent, RelativeTo{this});
5244 offsets = GetContentOffsetsFromPoint(pt, SKIP_HIDDEN);
5245 handleTableSelection = false;
5246 } else {
5247 GetDataForTableSelection(frameselection, PresShell(),
5248 aEvent->AsMouseEvent(),
5249 getter_AddRefs(parentContent),
5250 &contentOffsetForTableSel, &targetForTableSel);
5255 // We might be capturing in some other document and the event just happened to
5256 // trickle down here. Make sure that document's frame selection is notified.
5257 // Note, this may cause the current nsFrame object to be deleted, bug 336592.
5258 RefPtr<nsFrameSelection> frameSelection;
5259 if (activeFrame != this && activeFrame->DetermineDisplaySelection() !=
5260 nsISelectionController::SELECTION_OFF) {
5261 frameSelection = activeFrame->GetFrameSelection();
5264 // Also check the selection of the capturing content which might be in a
5265 // different document.
5266 if (!frameSelection && captureContent) {
5267 if (Document* doc = captureContent->GetComposedDoc()) {
5268 mozilla::PresShell* capturingPresShell = doc->GetPresShell();
5269 if (capturingPresShell &&
5270 capturingPresShell != PresContext()->GetPresShell()) {
5271 frameSelection = capturingPresShell->FrameSelection();
5276 if (frameSelection) {
5277 AutoWeakFrame wf(this);
5278 frameSelection->SetDragState(false);
5279 frameSelection->StopAutoScrollTimer();
5280 if (wf.IsAlive()) {
5281 nsIScrollableFrame* scrollFrame =
5282 nsLayoutUtils::GetNearestScrollableFrame(
5283 this, nsLayoutUtils::SCROLLABLE_SAME_DOC |
5284 nsLayoutUtils::SCROLLABLE_INCLUDE_HIDDEN);
5285 if (scrollFrame) {
5286 // Perform any additional scrolling needed to maintain CSS snap point
5287 // requirements when autoscrolling is over.
5288 scrollFrame->ScrollSnap();
5293 // Do not call any methods of the current object after this point!!!
5294 // The object is perhaps dead!
5296 return selectionOff ? NS_OK
5297 : HandleFrameSelection(
5298 frameselection, offsets, handleTableSelection,
5299 contentOffsetForTableSel, targetForTableSel,
5300 parentContent, aEvent, aEventStatus);
5303 struct MOZ_STACK_CLASS FrameContentRange {
5304 FrameContentRange(nsIContent* aContent, int32_t aStart, int32_t aEnd)
5305 : content(aContent), start(aStart), end(aEnd) {}
5306 nsCOMPtr<nsIContent> content;
5307 int32_t start;
5308 int32_t end;
5311 // Retrieve the content offsets of a frame
5312 static FrameContentRange GetRangeForFrame(const nsIFrame* aFrame) {
5313 nsIContent* content = aFrame->GetContent();
5314 if (!content) {
5315 NS_WARNING("Frame has no content");
5316 return FrameContentRange(nullptr, -1, -1);
5319 LayoutFrameType type = aFrame->Type();
5320 if (type == LayoutFrameType::Text) {
5321 auto [offset, offsetEnd] = aFrame->GetOffsets();
5322 return FrameContentRange(content, offset, offsetEnd);
5325 if (type == LayoutFrameType::Br) {
5326 nsIContent* parent = content->GetParent();
5327 const int32_t beginOffset = parent->ComputeIndexOf_Deprecated(content);
5328 return FrameContentRange(parent, beginOffset, beginOffset);
5331 while (content->IsRootOfNativeAnonymousSubtree()) {
5332 content = content->GetParent();
5335 nsIContent* parent = content->GetParent();
5336 if (aFrame->IsBlockOutside() || !parent) {
5337 return FrameContentRange(content, 0, content->GetChildCount());
5340 // TODO(emilio): Revise this in presence of Shadow DOM / display: contents,
5341 // it's likely that we don't want to just walk the light tree, and we need to
5342 // change the representation of FrameContentRange.
5343 const int32_t index = parent->ComputeIndexOf_Deprecated(content);
5344 MOZ_ASSERT(index >= 0);
5345 return FrameContentRange(parent, index, index + 1);
5348 // The FrameTarget represents the closest frame to a point that can be selected
5349 // The frame is the frame represented, frameEdge says whether one end of the
5350 // frame is the result (in which case different handling is needed), and
5351 // afterFrame says which end is represented if frameEdge is true
5352 struct FrameTarget {
5353 explicit operator bool() const { return !!frame; }
5355 nsIFrame* frame = nullptr;
5356 bool frameEdge = false;
5357 bool afterFrame = false;
5360 // See function implementation for information
5361 static FrameTarget GetSelectionClosestFrame(nsIFrame* aFrame,
5362 const nsPoint& aPoint,
5363 uint32_t aFlags);
5365 static bool SelfIsSelectable(nsIFrame* aFrame, uint32_t aFlags) {
5366 if ((aFlags & nsIFrame::SKIP_HIDDEN) &&
5367 !aFrame->StyleVisibility()->IsVisible()) {
5368 return false;
5370 return !aFrame->IsGeneratedContentFrame() &&
5371 aFrame->Style()->UserSelect() != StyleUserSelect::None;
5374 static bool SelectionDescendToKids(nsIFrame* aFrame) {
5375 // If we are only near (not directly over) then don't traverse
5376 // frames with independent selection (e.g. text and list controls, see bug
5377 // 268497). Note that this prevents any of the users of this method from
5378 // entering form controls.
5379 // XXX We might want some way to allow using the up-arrow to go into a form
5380 // control, but the focus didn't work right anyway; it'd probably be enough
5381 // if the left and right arrows could enter textboxes (which I don't believe
5382 // they can at the moment)
5383 if (aFrame->IsTextInputFrame() || aFrame->IsListControlFrame()) {
5384 MOZ_ASSERT(aFrame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION));
5385 return false;
5388 // Failure in this assertion means a new type of frame forms the root of an
5389 // NS_FRAME_INDEPENDENT_SELECTION subtree. In such case, the condition above
5390 // should be changed to handle it.
5391 MOZ_ASSERT_IF(
5392 aFrame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION),
5393 aFrame->GetParent()->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION));
5395 if (aFrame->IsGeneratedContentFrame()) {
5396 return false;
5399 auto style = aFrame->Style()->UserSelect();
5400 return style != StyleUserSelect::All && style != StyleUserSelect::None;
5403 static FrameTarget GetSelectionClosestFrameForChild(nsIFrame* aChild,
5404 const nsPoint& aPoint,
5405 uint32_t aFlags) {
5406 nsIFrame* parent = aChild->GetParent();
5407 if (SelectionDescendToKids(aChild)) {
5408 nsPoint pt = aPoint - aChild->GetOffsetTo(parent);
5409 return GetSelectionClosestFrame(aChild, pt, aFlags);
5411 return FrameTarget{aChild, false, false};
5414 // When the cursor needs to be at the beginning of a block, it shouldn't be
5415 // before the first child. A click on a block whose first child is a block
5416 // should put the cursor in the child. The cursor shouldn't be between the
5417 // blocks, because that's not where it's expected.
5418 // Note that this method is guaranteed to succeed.
5419 static FrameTarget DrillDownToSelectionFrame(nsIFrame* aFrame, bool aEndFrame,
5420 uint32_t aFlags) {
5421 if (SelectionDescendToKids(aFrame)) {
5422 nsIFrame* result = nullptr;
5423 nsIFrame* frame = aFrame->PrincipalChildList().FirstChild();
5424 if (!aEndFrame) {
5425 while (frame && (!SelfIsSelectable(frame, aFlags) || frame->IsEmpty()))
5426 frame = frame->GetNextSibling();
5427 if (frame) result = frame;
5428 } else {
5429 // Because the frame tree is singly linked, to find the last frame,
5430 // we have to iterate through all the frames
5431 // XXX I have a feeling this could be slow for long blocks, although
5432 // I can't find any slowdowns
5433 while (frame) {
5434 if (!frame->IsEmpty() && SelfIsSelectable(frame, aFlags))
5435 result = frame;
5436 frame = frame->GetNextSibling();
5439 if (result) return DrillDownToSelectionFrame(result, aEndFrame, aFlags);
5441 // If the current frame has no targetable children, target the current frame
5442 return FrameTarget{aFrame, true, aEndFrame};
5445 // This method finds the closest valid FrameTarget on a given line; if there is
5446 // no valid FrameTarget on the line, it returns a null FrameTarget
5447 static FrameTarget GetSelectionClosestFrameForLine(
5448 nsBlockFrame* aParent, nsBlockFrame::LineIterator aLine,
5449 const nsPoint& aPoint, uint32_t aFlags) {
5450 // Account for end of lines (any iterator from the block is valid)
5451 if (aLine == aParent->LinesEnd())
5452 return DrillDownToSelectionFrame(aParent, true, aFlags);
5453 nsIFrame* frame = aLine->mFirstChild;
5454 nsIFrame* closestFromIStart = nullptr;
5455 nsIFrame* closestFromIEnd = nullptr;
5456 nscoord closestIStart = aLine->IStart(), closestIEnd = aLine->IEnd();
5457 WritingMode wm = aLine->mWritingMode;
5458 LogicalPoint pt(wm, aPoint, aLine->mContainerSize);
5459 bool canSkipBr = false;
5460 bool lastFrameWasEditable = false;
5461 for (int32_t n = aLine->GetChildCount(); n;
5462 --n, frame = frame->GetNextSibling()) {
5463 // Skip brFrames. Can only skip if the line contains at least
5464 // one selectable and non-empty frame before. Also, avoid skipping brs if
5465 // the previous thing had a different editableness than us, since then we
5466 // may end up not being able to select after it if the br is the last thing
5467 // on the line.
5468 if (!SelfIsSelectable(frame, aFlags) || frame->IsEmpty() ||
5469 (canSkipBr && frame->IsBrFrame() &&
5470 lastFrameWasEditable == frame->GetContent()->IsEditable())) {
5471 continue;
5473 canSkipBr = true;
5474 lastFrameWasEditable =
5475 frame->GetContent() && frame->GetContent()->IsEditable();
5476 LogicalRect frameRect =
5477 LogicalRect(wm, frame->GetRect(), aLine->mContainerSize);
5478 if (pt.I(wm) >= frameRect.IStart(wm)) {
5479 if (pt.I(wm) < frameRect.IEnd(wm)) {
5480 return GetSelectionClosestFrameForChild(frame, aPoint, aFlags);
5482 if (frameRect.IEnd(wm) >= closestIStart) {
5483 closestFromIStart = frame;
5484 closestIStart = frameRect.IEnd(wm);
5486 } else {
5487 if (frameRect.IStart(wm) <= closestIEnd) {
5488 closestFromIEnd = frame;
5489 closestIEnd = frameRect.IStart(wm);
5493 if (!closestFromIStart && !closestFromIEnd) {
5494 // We should only get here if there are no selectable frames on a line
5495 // XXX Do we need more elaborate handling here?
5496 return FrameTarget();
5498 if (closestFromIStart &&
5499 (!closestFromIEnd ||
5500 (abs(pt.I(wm) - closestIStart) <= abs(pt.I(wm) - closestIEnd)))) {
5501 return GetSelectionClosestFrameForChild(closestFromIStart, aPoint, aFlags);
5503 return GetSelectionClosestFrameForChild(closestFromIEnd, aPoint, aFlags);
5506 // This method is for the special handling we do for block frames; they're
5507 // special because they represent paragraphs and because they are organized
5508 // into lines, which have bounds that are not stored elsewhere in the
5509 // frame tree. Returns a null FrameTarget for frames which are not
5510 // blocks or blocks with no lines except editable one.
5511 static FrameTarget GetSelectionClosestFrameForBlock(nsIFrame* aFrame,
5512 const nsPoint& aPoint,
5513 uint32_t aFlags) {
5514 nsBlockFrame* bf = do_QueryFrame(aFrame);
5515 if (!bf) {
5516 return FrameTarget();
5519 // This code searches for the correct line
5520 nsBlockFrame::LineIterator end = bf->LinesEnd();
5521 nsBlockFrame::LineIterator curLine = bf->LinesBegin();
5522 nsBlockFrame::LineIterator closestLine = end;
5524 if (curLine != end) {
5525 // Convert aPoint into a LogicalPoint in the writing-mode of this block
5526 WritingMode wm = curLine->mWritingMode;
5527 LogicalPoint pt(wm, aPoint, curLine->mContainerSize);
5528 do {
5529 // Check to see if our point lies within the line's block-direction bounds
5530 nscoord BCoord = pt.B(wm) - curLine->BStart();
5531 nscoord BSize = curLine->BSize();
5532 if (BCoord >= 0 && BCoord < BSize) {
5533 closestLine = curLine;
5534 break; // We found the line; stop looking
5536 if (BCoord < 0) break;
5537 ++curLine;
5538 } while (curLine != end);
5540 if (closestLine == end) {
5541 nsBlockFrame::LineIterator prevLine = curLine.prev();
5542 nsBlockFrame::LineIterator nextLine = curLine;
5543 // Avoid empty lines
5544 while (nextLine != end && nextLine->IsEmpty()) ++nextLine;
5545 while (prevLine != end && prevLine->IsEmpty()) --prevLine;
5547 // This hidden pref dictates whether a point above or below all lines
5548 // comes up with a line or the beginning or end of the frame; 0 on
5549 // Windows, 1 on other platforms by default at the writing of this code
5550 int32_t dragOutOfFrame =
5551 Preferences::GetInt("browser.drag_out_of_frame_style");
5553 if (prevLine == end) {
5554 if (dragOutOfFrame == 1 || nextLine == end)
5555 return DrillDownToSelectionFrame(aFrame, false, aFlags);
5556 closestLine = nextLine;
5557 } else if (nextLine == end) {
5558 if (dragOutOfFrame == 1)
5559 return DrillDownToSelectionFrame(aFrame, true, aFlags);
5560 closestLine = prevLine;
5561 } else { // Figure out which line is closer
5562 if (pt.B(wm) - prevLine->BEnd() < nextLine->BStart() - pt.B(wm))
5563 closestLine = prevLine;
5564 else
5565 closestLine = nextLine;
5570 do {
5571 if (auto target =
5572 GetSelectionClosestFrameForLine(bf, closestLine, aPoint, aFlags)) {
5573 return target;
5575 ++closestLine;
5576 } while (closestLine != end);
5578 // Fall back to just targeting the last targetable place
5579 return DrillDownToSelectionFrame(aFrame, true, aFlags);
5582 // Use frame edge for grid, flex, table, and non-editable images. Choose the
5583 // edge based on the point position past the frame rect. If past the middle,
5584 // caret should be at end, otherwise at start. This behavior matches Blink.
5586 // TODO(emilio): Can we use this code path for other replaced elements other
5587 // than images? Or even all other frames? We only get there when we didn't find
5588 // selectable children... At least one XUL test fails if we make this apply to
5589 // XUL labels. Also, editable images need _not_ to use the frame edge, see
5590 // below.
5591 static bool UseFrameEdge(nsIFrame* aFrame) {
5592 if (aFrame->IsFlexOrGridContainer() || aFrame->IsTableFrame()) {
5593 return true;
5595 const nsImageFrame* image = do_QueryFrame(aFrame);
5596 if (image && !aFrame->GetContent()->IsEditable()) {
5597 // Editable images are a special-case because editing relies on clicking on
5598 // an editable image selecting it, for it to show resizers.
5599 return true;
5601 return false;
5604 static FrameTarget LastResortFrameTargetForFrame(nsIFrame* aFrame,
5605 const nsPoint& aPoint) {
5606 if (!UseFrameEdge(aFrame)) {
5607 return {aFrame, false, false};
5609 const auto& rect = aFrame->GetRectRelativeToSelf();
5610 nscoord reference;
5611 nscoord middle;
5612 if (aFrame->GetWritingMode().IsVertical()) {
5613 reference = aPoint.y;
5614 middle = rect.Height() / 2;
5615 } else {
5616 reference = aPoint.x;
5617 middle = rect.Width() / 2;
5619 const bool afterFrame = reference > middle;
5620 return {aFrame, true, afterFrame};
5623 // GetSelectionClosestFrame is the helper function that calculates the closest
5624 // frame to the given point.
5625 // It doesn't completely account for offset styles, so needs to be used in
5626 // restricted environments.
5627 // Cannot handle overlapping frames correctly, so it should receive the output
5628 // of GetFrameForPoint
5629 // Guaranteed to return a valid FrameTarget.
5630 // aPoint is relative to aFrame.
5631 static FrameTarget GetSelectionClosestFrame(nsIFrame* aFrame,
5632 const nsPoint& aPoint,
5633 uint32_t aFlags) {
5634 // Handle blocks; if the frame isn't a block, the method fails
5635 if (auto target = GetSelectionClosestFrameForBlock(aFrame, aPoint, aFlags)) {
5636 return target;
5639 if (nsIFrame* kid = aFrame->PrincipalChildList().FirstChild()) {
5640 // Go through all the child frames to find the closest one
5641 nsIFrame::FrameWithDistance closest = {nullptr, nscoord_MAX, nscoord_MAX};
5642 for (; kid; kid = kid->GetNextSibling()) {
5643 if (!SelfIsSelectable(kid, aFlags) || kid->IsEmpty()) continue;
5645 kid->FindCloserFrameForSelection(aPoint, &closest);
5647 if (closest.mFrame) {
5648 if (closest.mFrame->IsInSVGTextSubtree())
5649 return FrameTarget{closest.mFrame, false, false};
5650 return GetSelectionClosestFrameForChild(closest.mFrame, aPoint, aFlags);
5654 return LastResortFrameTargetForFrame(aFrame, aPoint);
5657 static nsIFrame::ContentOffsets OffsetsForSingleFrame(nsIFrame* aFrame,
5658 const nsPoint& aPoint) {
5659 nsIFrame::ContentOffsets offsets;
5660 FrameContentRange range = GetRangeForFrame(aFrame);
5661 offsets.content = range.content;
5662 // If there are continuations (meaning it's not one rectangle), this is the
5663 // best this function can do
5664 if (aFrame->GetNextContinuation() || aFrame->GetPrevContinuation()) {
5665 offsets.offset = range.start;
5666 offsets.secondaryOffset = range.end;
5667 offsets.associate = CARET_ASSOCIATE_AFTER;
5668 return offsets;
5671 // Figure out whether the offsets should be over, after, or before the frame
5672 nsRect rect(nsPoint(0, 0), aFrame->GetSize());
5674 bool isBlock = !aFrame->StyleDisplay()->IsInlineFlow();
5675 bool isRtl = (aFrame->StyleVisibility()->mDirection == StyleDirection::Rtl);
5676 if ((isBlock && rect.y < aPoint.y) ||
5677 (!isBlock && ((isRtl && rect.x + rect.width / 2 > aPoint.x) ||
5678 (!isRtl && rect.x + rect.width / 2 < aPoint.x)))) {
5679 offsets.offset = range.end;
5680 if (rect.Contains(aPoint))
5681 offsets.secondaryOffset = range.start;
5682 else
5683 offsets.secondaryOffset = range.end;
5684 } else {
5685 offsets.offset = range.start;
5686 if (rect.Contains(aPoint))
5687 offsets.secondaryOffset = range.end;
5688 else
5689 offsets.secondaryOffset = range.start;
5691 offsets.associate = offsets.offset == range.start ? CARET_ASSOCIATE_AFTER
5692 : CARET_ASSOCIATE_BEFORE;
5693 return offsets;
5696 static nsIFrame* AdjustFrameForSelectionStyles(nsIFrame* aFrame) {
5697 nsIFrame* adjustedFrame = aFrame;
5698 for (nsIFrame* frame = aFrame; frame; frame = frame->GetParent()) {
5699 // These are the conditions that make all children not able to handle
5700 // a cursor.
5701 auto userSelect = frame->Style()->UserSelect();
5702 if (userSelect != StyleUserSelect::Auto &&
5703 userSelect != StyleUserSelect::All) {
5704 break;
5706 if (userSelect == StyleUserSelect::All ||
5707 frame->IsGeneratedContentFrame()) {
5708 adjustedFrame = frame;
5711 return adjustedFrame;
5714 nsIFrame::ContentOffsets nsIFrame::GetContentOffsetsFromPoint(
5715 const nsPoint& aPoint, uint32_t aFlags) {
5716 nsIFrame* adjustedFrame;
5717 if (aFlags & IGNORE_SELECTION_STYLE) {
5718 adjustedFrame = this;
5719 } else {
5720 // This section of code deals with special selection styles. Note that
5721 // -moz-all exists, even though it doesn't need to be explicitly handled.
5723 // The offset is forced not to end up in generated content; content offsets
5724 // cannot represent content outside of the document's content tree.
5726 adjustedFrame = AdjustFrameForSelectionStyles(this);
5728 // `user-select: all` needs special handling, because clicking on it should
5729 // lead to the whole frame being selected.
5730 if (adjustedFrame->Style()->UserSelect() == StyleUserSelect::All) {
5731 nsPoint adjustedPoint = aPoint + GetOffsetTo(adjustedFrame);
5732 return OffsetsForSingleFrame(adjustedFrame, adjustedPoint);
5735 // For other cases, try to find a closest frame starting from the parent of
5736 // the unselectable frame
5737 if (adjustedFrame != this) {
5738 adjustedFrame = adjustedFrame->GetParent();
5742 nsPoint adjustedPoint = aPoint + GetOffsetTo(adjustedFrame);
5744 FrameTarget closest =
5745 GetSelectionClosestFrame(adjustedFrame, adjustedPoint, aFlags);
5747 // If the correct offset is at one end of a frame, use offset-based
5748 // calculation method
5749 if (closest.frameEdge) {
5750 ContentOffsets offsets;
5751 FrameContentRange range = GetRangeForFrame(closest.frame);
5752 offsets.content = range.content;
5753 if (closest.afterFrame)
5754 offsets.offset = range.end;
5755 else
5756 offsets.offset = range.start;
5757 offsets.secondaryOffset = offsets.offset;
5758 offsets.associate = offsets.offset == range.start ? CARET_ASSOCIATE_AFTER
5759 : CARET_ASSOCIATE_BEFORE;
5760 return offsets;
5763 nsPoint pt;
5764 if (closest.frame != this) {
5765 if (closest.frame->IsInSVGTextSubtree()) {
5766 pt = nsLayoutUtils::TransformAncestorPointToFrame(
5767 RelativeTo{closest.frame}, aPoint, RelativeTo{this});
5768 } else {
5769 pt = aPoint - closest.frame->GetOffsetTo(this);
5771 } else {
5772 pt = aPoint;
5774 return closest.frame->CalcContentOffsetsFromFramePoint(pt);
5776 // XXX should I add some kind of offset standardization?
5777 // consider <b>xxxxx</b><i>zzzzz</i>; should any click between the last
5778 // x and first z put the cursor in the same logical position in addition
5779 // to the same visual position?
5782 nsIFrame::ContentOffsets nsIFrame::CalcContentOffsetsFromFramePoint(
5783 const nsPoint& aPoint) {
5784 return OffsetsForSingleFrame(this, aPoint);
5787 bool nsIFrame::AssociateImage(const StyleImage& aImage) {
5788 imgRequestProxy* req = aImage.GetImageRequest();
5789 if (!req) {
5790 return false;
5793 mozilla::css::ImageLoader* loader =
5794 PresContext()->Document()->StyleImageLoader();
5796 loader->AssociateRequestToFrame(req, this);
5797 return true;
5800 void nsIFrame::DisassociateImage(const StyleImage& aImage) {
5801 imgRequestProxy* req = aImage.GetImageRequest();
5802 if (!req) {
5803 return;
5806 mozilla::css::ImageLoader* loader =
5807 PresContext()->Document()->StyleImageLoader();
5809 loader->DisassociateRequestFromFrame(req, this);
5812 StyleImageRendering nsIFrame::UsedImageRendering() const {
5813 ComputedStyle* style;
5814 if (IsCanvasFrame()) {
5815 // XXXdholbert Maybe we should use FindCanvasBackground here (instead of
5816 // FindBackground), since we're inside an IsCanvasFrame check? Though then
5817 // we'd also have to copypaste or abstract-away the multi-part root-frame
5818 // lookup that the canvas-flavored API requires.
5819 style = nsCSSRendering::FindBackground(this);
5820 } else {
5821 style = Style();
5823 return style->StyleVisibility()->mImageRendering;
5826 // The touch-action CSS property applies to: all elements except: non-replaced
5827 // inline elements, table rows, row groups, table columns, and column groups.
5828 StyleTouchAction nsIFrame::UsedTouchAction() const {
5829 if (IsLineParticipant()) {
5830 return StyleTouchAction::AUTO;
5832 auto& disp = *StyleDisplay();
5833 if (disp.IsInternalTableStyleExceptCell()) {
5834 return StyleTouchAction::AUTO;
5836 return disp.mTouchAction;
5839 Maybe<nsIFrame::Cursor> nsIFrame::GetCursor(const nsPoint&) {
5840 StyleCursorKind kind = StyleUI()->Cursor().keyword;
5841 if (kind == StyleCursorKind::Auto) {
5842 // If this is editable, I-beam cursor is better for most elements.
5843 kind = (mContent && mContent->IsEditable()) ? StyleCursorKind::Text
5844 : StyleCursorKind::Default;
5846 if (kind == StyleCursorKind::Text && GetWritingMode().IsVertical()) {
5847 // Per CSS UI spec, UA may treat value 'text' as
5848 // 'vertical-text' for vertical text.
5849 kind = StyleCursorKind::VerticalText;
5852 return Some(Cursor{kind, AllowCustomCursorImage::Yes});
5855 // Resize and incremental reflow
5857 /* virtual */
5858 void nsIFrame::MarkIntrinsicISizesDirty() {
5859 // If we're a flex item, clear our flex-item-specific cached measurements
5860 // (which likely depended on our now-stale intrinsic isize).
5861 if (IsFlexItem()) {
5862 nsFlexContainerFrame::MarkCachedFlexMeasurementsDirty(this);
5865 if (HasAnyStateBits(NS_FRAME_FONT_INFLATION_FLOW_ROOT)) {
5866 nsFontInflationData::MarkFontInflationDataTextDirty(this);
5869 RemoveProperty(nsGridContainerFrame::CachedBAxisMeasurement::Prop());
5872 void nsIFrame::MarkSubtreeDirty() {
5873 if (HasAnyStateBits(NS_FRAME_IS_DIRTY)) {
5874 return;
5876 // Unconditionally mark given frame dirty.
5877 AddStateBits(NS_FRAME_IS_DIRTY);
5879 // Mark all descendants dirty, unless:
5880 // - Already dirty.
5881 // - TableColGroup
5882 // - XULBox
5883 AutoTArray<nsIFrame*, 32> stack;
5884 for (const auto& childLists : ChildLists()) {
5885 for (nsIFrame* kid : childLists.mList) {
5886 stack.AppendElement(kid);
5889 while (!stack.IsEmpty()) {
5890 nsIFrame* f = stack.PopLastElement();
5891 if (f->HasAnyStateBits(NS_FRAME_IS_DIRTY) || f->IsTableColGroupFrame()) {
5892 continue;
5895 f->AddStateBits(NS_FRAME_IS_DIRTY);
5897 for (const auto& childLists : f->ChildLists()) {
5898 for (nsIFrame* kid : childLists.mList) {
5899 stack.AppendElement(kid);
5905 /* virtual */
5906 nscoord nsIFrame::GetMinISize(gfxContext* aRenderingContext) {
5907 nscoord result = 0;
5908 DISPLAY_MIN_INLINE_SIZE(this, result);
5909 return result;
5912 /* virtual */
5913 nscoord nsIFrame::GetPrefISize(gfxContext* aRenderingContext) {
5914 nscoord result = 0;
5915 DISPLAY_PREF_INLINE_SIZE(this, result);
5916 return result;
5919 /* virtual */
5920 void nsIFrame::AddInlineMinISize(gfxContext* aRenderingContext,
5921 nsIFrame::InlineMinISizeData* aData) {
5922 nscoord isize = nsLayoutUtils::IntrinsicForContainer(
5923 aRenderingContext, this, IntrinsicISizeType::MinISize);
5924 aData->DefaultAddInlineMinISize(this, isize);
5927 /* virtual */
5928 void nsIFrame::AddInlinePrefISize(gfxContext* aRenderingContext,
5929 nsIFrame::InlinePrefISizeData* aData) {
5930 nscoord isize = nsLayoutUtils::IntrinsicForContainer(
5931 aRenderingContext, this, IntrinsicISizeType::PrefISize);
5932 aData->DefaultAddInlinePrefISize(isize);
5935 void nsIFrame::InlineMinISizeData::DefaultAddInlineMinISize(nsIFrame* aFrame,
5936 nscoord aISize,
5937 bool aAllowBreak) {
5938 auto parent = aFrame->GetParent();
5939 MOZ_ASSERT(parent, "Must have a parent if we get here!");
5940 const bool mayBreak = aAllowBreak && !aFrame->CanContinueTextRun() &&
5941 !parent->Style()->ShouldSuppressLineBreak() &&
5942 parent->StyleText()->WhiteSpaceCanWrap(parent);
5943 if (mayBreak) {
5944 OptionallyBreak();
5946 mTrailingWhitespace = 0;
5947 mSkipWhitespace = false;
5948 mCurrentLine += aISize;
5949 mAtStartOfLine = false;
5950 if (mayBreak) {
5951 OptionallyBreak();
5955 void nsIFrame::InlinePrefISizeData::DefaultAddInlinePrefISize(nscoord aISize) {
5956 mCurrentLine = NSCoordSaturatingAdd(mCurrentLine, aISize);
5957 mTrailingWhitespace = 0;
5958 mSkipWhitespace = false;
5959 mLineIsEmpty = false;
5962 void nsIFrame::InlineMinISizeData::ForceBreak() {
5963 mCurrentLine -= mTrailingWhitespace;
5964 mPrevLines = std::max(mPrevLines, mCurrentLine);
5965 mCurrentLine = mTrailingWhitespace = 0;
5967 for (uint32_t i = 0, i_end = mFloats.Length(); i != i_end; ++i) {
5968 nscoord float_min = mFloats[i].Width();
5969 if (float_min > mPrevLines) mPrevLines = float_min;
5971 mFloats.Clear();
5972 mSkipWhitespace = true;
5975 void nsIFrame::InlineMinISizeData::OptionallyBreak(nscoord aHyphenWidth) {
5976 // If we can fit more content into a smaller width by staying on this
5977 // line (because we're still at a negative offset due to negative
5978 // text-indent or negative margin), don't break. Otherwise, do the
5979 // same as ForceBreak. it doesn't really matter when we accumulate
5980 // floats.
5981 if (mCurrentLine + aHyphenWidth < 0 || mAtStartOfLine) return;
5982 mCurrentLine += aHyphenWidth;
5983 ForceBreak();
5986 void nsIFrame::InlinePrefISizeData::ForceBreak(StyleClear aClearType) {
5987 // If this force break is not clearing any float, we can leave all the
5988 // floats to the next force break.
5989 if (!mFloats.IsEmpty() && aClearType != StyleClear::None) {
5990 // preferred widths accumulated for floats that have already
5991 // been cleared past
5992 nscoord floats_done = 0,
5993 // preferred widths accumulated for floats that have not yet
5994 // been cleared past
5995 floats_cur_left = 0, floats_cur_right = 0;
5997 for (const FloatInfo& floatInfo : mFloats) {
5998 const nsStyleDisplay* floatDisp = floatInfo.Frame()->StyleDisplay();
5999 StyleClear clearType = floatDisp->mClear;
6000 if (clearType == StyleClear::Left || clearType == StyleClear::Right ||
6001 clearType == StyleClear::Both) {
6002 nscoord floats_cur =
6003 NSCoordSaturatingAdd(floats_cur_left, floats_cur_right);
6004 if (floats_cur > floats_done) {
6005 floats_done = floats_cur;
6007 if (clearType != StyleClear::Right) {
6008 floats_cur_left = 0;
6010 if (clearType != StyleClear::Left) {
6011 floats_cur_right = 0;
6015 StyleFloat floatStyle = floatDisp->mFloat;
6016 nscoord& floats_cur =
6017 floatStyle == StyleFloat::Left ? floats_cur_left : floats_cur_right;
6018 nscoord floatWidth = floatInfo.Width();
6019 // Negative-width floats don't change the available space so they
6020 // shouldn't change our intrinsic line width either.
6021 floats_cur = NSCoordSaturatingAdd(floats_cur, std::max(0, floatWidth));
6024 nscoord floats_cur =
6025 NSCoordSaturatingAdd(floats_cur_left, floats_cur_right);
6026 if (floats_cur > floats_done) floats_done = floats_cur;
6028 mCurrentLine = NSCoordSaturatingAdd(mCurrentLine, floats_done);
6030 if (aClearType == StyleClear::Both) {
6031 mFloats.Clear();
6032 } else {
6033 // If the break type does not clear all floats, it means there may
6034 // be some floats whose isize should contribute to the intrinsic
6035 // isize of the next line. The code here scans the current mFloats
6036 // and keeps floats which are not cleared by this break. Note that
6037 // floats may be cleared directly or indirectly. See below.
6038 nsTArray<FloatInfo> newFloats;
6039 MOZ_ASSERT(
6040 aClearType == StyleClear::Left || aClearType == StyleClear::Right,
6041 "Other values should have been handled in other branches");
6042 StyleFloat clearFloatType =
6043 aClearType == StyleClear::Left ? StyleFloat::Left : StyleFloat::Right;
6044 // Iterate the array in reverse so that we can stop when there are
6045 // no longer any floats we need to keep. See below.
6046 for (FloatInfo& floatInfo : Reversed(mFloats)) {
6047 const nsStyleDisplay* floatDisp = floatInfo.Frame()->StyleDisplay();
6048 if (floatDisp->mFloat != clearFloatType) {
6049 newFloats.AppendElement(floatInfo);
6050 } else {
6051 // This is a float on the side that this break directly clears
6052 // which means we're not keeping it in mFloats. However, if
6053 // this float clears floats on the opposite side (via a value
6054 // of either 'both' or one of 'left'/'right'), any remaining
6055 // (earlier) floats on that side would be indirectly cleared
6056 // as well. Thus, we should break out of this loop and stop
6057 // considering earlier floats to be kept in mFloats.
6058 StyleClear clearType = floatDisp->mClear;
6059 if (clearType != aClearType && clearType != StyleClear::None) {
6060 break;
6064 newFloats.Reverse();
6065 mFloats = std::move(newFloats);
6069 mCurrentLine =
6070 NSCoordSaturatingSubtract(mCurrentLine, mTrailingWhitespace, nscoord_MAX);
6071 mPrevLines = std::max(mPrevLines, mCurrentLine);
6072 mCurrentLine = mTrailingWhitespace = 0;
6073 mSkipWhitespace = true;
6074 mLineIsEmpty = true;
6077 static nscoord ResolveMargin(const LengthPercentageOrAuto& aStyle,
6078 nscoord aPercentageBasis) {
6079 if (aStyle.IsAuto()) {
6080 return nscoord(0);
6082 return nsLayoutUtils::ResolveToLength<false>(aStyle.AsLengthPercentage(),
6083 aPercentageBasis);
6086 static nscoord ResolvePadding(const LengthPercentage& aStyle,
6087 nscoord aPercentageBasis) {
6088 return nsLayoutUtils::ResolveToLength<true>(aStyle, aPercentageBasis);
6091 static nsIFrame::IntrinsicSizeOffsetData IntrinsicSizeOffsets(
6092 nsIFrame* aFrame, nscoord aPercentageBasis, bool aForISize) {
6093 nsIFrame::IntrinsicSizeOffsetData result;
6094 WritingMode wm = aFrame->GetWritingMode();
6095 const auto& margin = aFrame->StyleMargin()->mMargin;
6096 bool verticalAxis = aForISize == wm.IsVertical();
6097 if (verticalAxis) {
6098 result.margin += ResolveMargin(margin.Get(eSideTop), aPercentageBasis);
6099 result.margin += ResolveMargin(margin.Get(eSideBottom), aPercentageBasis);
6100 } else {
6101 result.margin += ResolveMargin(margin.Get(eSideLeft), aPercentageBasis);
6102 result.margin += ResolveMargin(margin.Get(eSideRight), aPercentageBasis);
6105 const auto& padding = aFrame->StylePadding()->mPadding;
6106 if (verticalAxis) {
6107 result.padding += ResolvePadding(padding.Get(eSideTop), aPercentageBasis);
6108 result.padding +=
6109 ResolvePadding(padding.Get(eSideBottom), aPercentageBasis);
6110 } else {
6111 result.padding += ResolvePadding(padding.Get(eSideLeft), aPercentageBasis);
6112 result.padding += ResolvePadding(padding.Get(eSideRight), aPercentageBasis);
6115 const nsStyleBorder* styleBorder = aFrame->StyleBorder();
6116 if (verticalAxis) {
6117 result.border += styleBorder->GetComputedBorderWidth(eSideTop);
6118 result.border += styleBorder->GetComputedBorderWidth(eSideBottom);
6119 } else {
6120 result.border += styleBorder->GetComputedBorderWidth(eSideLeft);
6121 result.border += styleBorder->GetComputedBorderWidth(eSideRight);
6124 const nsStyleDisplay* disp = aFrame->StyleDisplay();
6125 if (aFrame->IsThemed(disp)) {
6126 nsPresContext* presContext = aFrame->PresContext();
6128 LayoutDeviceIntMargin border = presContext->Theme()->GetWidgetBorder(
6129 presContext->DeviceContext(), aFrame, disp->EffectiveAppearance());
6130 result.border = presContext->DevPixelsToAppUnits(
6131 verticalAxis ? border.TopBottom() : border.LeftRight());
6133 LayoutDeviceIntMargin padding;
6134 if (presContext->Theme()->GetWidgetPadding(
6135 presContext->DeviceContext(), aFrame, disp->EffectiveAppearance(),
6136 &padding)) {
6137 result.padding = presContext->DevPixelsToAppUnits(
6138 verticalAxis ? padding.TopBottom() : padding.LeftRight());
6141 return result;
6144 /* virtual */ nsIFrame::IntrinsicSizeOffsetData nsIFrame::IntrinsicISizeOffsets(
6145 nscoord aPercentageBasis) {
6146 return IntrinsicSizeOffsets(this, aPercentageBasis, true);
6149 nsIFrame::IntrinsicSizeOffsetData nsIFrame::IntrinsicBSizeOffsets(
6150 nscoord aPercentageBasis) {
6151 return IntrinsicSizeOffsets(this, aPercentageBasis, false);
6154 /* virtual */
6155 IntrinsicSize nsIFrame::GetIntrinsicSize() {
6156 return IntrinsicSize(); // default is width/height set to eStyleUnit_None
6159 AspectRatio nsIFrame::GetAspectRatio() const {
6160 // Per spec, 'aspect-ratio' property applies to all elements except inline
6161 // boxes and internal ruby or table boxes.
6162 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio
6163 // For those frame types that don't support aspect-ratio, they must not have
6164 // the natural ratio, so this early return is fine.
6165 if (!SupportsAspectRatio()) {
6166 return AspectRatio();
6169 const StyleAspectRatio& aspectRatio = StylePosition()->mAspectRatio;
6170 // If aspect-ratio is zero or infinite, it's a degenerate ratio and behaves
6171 // as auto.
6172 // https://drafts.csswg.org/css-sizing-4/#valdef-aspect-ratio-ratio
6173 if (!aspectRatio.BehavesAsAuto()) {
6174 // Non-auto. Return the preferred aspect ratio from the aspect-ratio style.
6175 return aspectRatio.ratio.AsRatio().ToLayoutRatio(UseBoxSizing::Yes);
6178 // The rest of the cases are when aspect-ratio has 'auto'.
6179 if (auto intrinsicRatio = GetIntrinsicRatio()) {
6180 return intrinsicRatio;
6183 if (aspectRatio.HasRatio()) {
6184 // If it's a degenerate ratio, this returns 0. Just the same as the auto
6185 // case.
6186 return aspectRatio.ratio.AsRatio().ToLayoutRatio(UseBoxSizing::No);
6189 return AspectRatio();
6192 /* virtual */
6193 AspectRatio nsIFrame::GetIntrinsicRatio() const { return AspectRatio(); }
6195 static bool ShouldApplyAutomaticMinimumOnInlineAxis(
6196 WritingMode aWM, const nsStyleDisplay* aDisplay,
6197 const nsStylePosition* aPosition) {
6198 // Apply the automatic minimum size for aspect ratio:
6199 // Note: The replaced elements shouldn't be here, so we only check the scroll
6200 // container.
6201 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
6202 return !aDisplay->IsScrollableOverflow() && aPosition->MinISize(aWM).IsAuto();
6205 struct MinMaxSize {
6206 nscoord mMinSize = 0;
6207 nscoord mMaxSize = NS_UNCONSTRAINEDSIZE;
6209 nscoord ClampSizeToMinAndMax(nscoord aSize) const {
6210 return NS_CSS_MINMAX(aSize, mMinSize, mMaxSize);
6213 static MinMaxSize ComputeTransferredMinMaxInlineSize(
6214 const WritingMode aWM, const AspectRatio& aAspectRatio,
6215 const MinMaxSize& aMinMaxBSize, const LogicalSize& aBoxSizingAdjustment) {
6216 // Note: the spec mentions that
6217 // 1. This transferred minimum is capped by any definite preferred or maximum
6218 // size in the destination axis.
6219 // 2. This transferred maximum is floored by any definite preferred or minimum
6220 // size in the destination axis
6222 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio
6224 // The spec requires us to clamp these by the specified size (it calls it the
6225 // preferred size). However, we actually don't need to worry about that,
6226 // because we only use this if the inline size is indefinite.
6228 // We do not need to clamp the transferred minimum and maximum as long as we
6229 // always apply the transferred min/max size before the explicit min/max size,
6230 // the result will be identical.
6232 MinMaxSize transferredISize;
6234 if (aMinMaxBSize.mMinSize > 0) {
6235 transferredISize.mMinSize = aAspectRatio.ComputeRatioDependentSize(
6236 LogicalAxis::eLogicalAxisInline, aWM, aMinMaxBSize.mMinSize,
6237 aBoxSizingAdjustment);
6240 if (aMinMaxBSize.mMaxSize != NS_UNCONSTRAINEDSIZE) {
6241 transferredISize.mMaxSize = aAspectRatio.ComputeRatioDependentSize(
6242 LogicalAxis::eLogicalAxisInline, aWM, aMinMaxBSize.mMaxSize,
6243 aBoxSizingAdjustment);
6246 // Minimum size wins over maximum size.
6247 transferredISize.mMaxSize =
6248 std::max(transferredISize.mMinSize, transferredISize.mMaxSize);
6249 return transferredISize;
6252 /* virtual */
6253 nsIFrame::SizeComputationResult nsIFrame::ComputeSize(
6254 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
6255 nscoord aAvailableISize, const LogicalSize& aMargin,
6256 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
6257 ComputeSizeFlags aFlags) {
6258 MOZ_ASSERT(!GetIntrinsicRatio(),
6259 "Please override this method and call "
6260 "nsContainerFrame::ComputeSizeWithIntrinsicDimensions instead.");
6261 LogicalSize result =
6262 ComputeAutoSize(aRenderingContext, aWM, aCBSize, aAvailableISize, aMargin,
6263 aBorderPadding, aSizeOverrides, aFlags);
6264 const nsStylePosition* stylePos = StylePosition();
6265 const nsStyleDisplay* disp = StyleDisplay();
6266 auto aspectRatioUsage = AspectRatioUsage::None;
6268 const auto boxSizingAdjust = stylePos->mBoxSizing == StyleBoxSizing::Border
6269 ? aBorderPadding
6270 : LogicalSize(aWM);
6271 nscoord boxSizingToMarginEdgeISize = aMargin.ISize(aWM) +
6272 aBorderPadding.ISize(aWM) -
6273 boxSizingAdjust.ISize(aWM);
6275 const auto& styleISize = aSizeOverrides.mStyleISize
6276 ? *aSizeOverrides.mStyleISize
6277 : stylePos->ISize(aWM);
6278 const auto& styleBSize = aSizeOverrides.mStyleBSize
6279 ? *aSizeOverrides.mStyleBSize
6280 : stylePos->BSize(aWM);
6281 const auto& aspectRatio = aSizeOverrides.mAspectRatio
6282 ? *aSizeOverrides.mAspectRatio
6283 : GetAspectRatio();
6285 auto parentFrame = GetParent();
6286 auto alignCB = parentFrame;
6287 bool isGridItem = IsGridItem();
6288 if (parentFrame && parentFrame->IsTableWrapperFrame() && IsTableFrame()) {
6289 // An inner table frame is sized as a grid item if its table wrapper is,
6290 // because they actually have the same CB (the wrapper's CB).
6291 // @see ReflowInput::InitCBReflowInput
6292 auto tableWrapper = GetParent();
6293 auto grandParent = tableWrapper->GetParent();
6294 isGridItem = grandParent->IsGridContainerFrame() &&
6295 !tableWrapper->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW);
6296 if (isGridItem) {
6297 // When resolving justify/align-self below, we want to use the grid
6298 // container's justify/align-items value and WritingMode.
6299 alignCB = grandParent;
6302 const bool isFlexItem =
6303 IsFlexItem() && !parentFrame->HasAnyStateBits(
6304 NS_STATE_FLEX_IS_EMULATING_LEGACY_WEBKIT_BOX);
6305 // This variable only gets set (and used) if isFlexItem is true. It
6306 // indicates which axis (in this frame's own WM) corresponds to its
6307 // flex container's main axis.
6308 LogicalAxis flexMainAxis =
6309 eLogicalAxisInline; // (init to make valgrind happy)
6310 if (isFlexItem) {
6311 flexMainAxis = nsFlexContainerFrame::IsItemInlineAxisMainAxis(this)
6312 ? eLogicalAxisInline
6313 : eLogicalAxisBlock;
6316 const bool isOrthogonal = aWM.IsOrthogonalTo(alignCB->GetWritingMode());
6317 const bool isAutoISize = styleISize.IsAuto();
6318 const bool isAutoBSize =
6319 nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM));
6320 // Compute inline-axis size
6321 if (!isAutoISize) {
6322 auto iSizeResult = ComputeISizeValue(
6323 aRenderingContext, aWM, aCBSize, boxSizingAdjust,
6324 boxSizingToMarginEdgeISize, styleISize, aSizeOverrides, aFlags);
6325 result.ISize(aWM) = iSizeResult.mISize;
6326 aspectRatioUsage = iSizeResult.mAspectRatioUsage;
6327 } else if (MOZ_UNLIKELY(isGridItem) && !IsTrueOverflowContainer()) {
6328 // 'auto' inline-size for grid-level box - fill the CB for 'stretch' /
6329 // 'normal' and clamp it to the CB if requested:
6330 bool stretch = false;
6331 bool mayUseAspectRatio = aspectRatio && !isAutoBSize;
6332 if (!aFlags.contains(ComputeSizeFlag::ShrinkWrap) &&
6333 !StyleMargin()->HasInlineAxisAuto(aWM) &&
6334 !alignCB->IsMasonry(isOrthogonal ? eLogicalAxisBlock
6335 : eLogicalAxisInline)) {
6336 auto inlineAxisAlignment =
6337 isOrthogonal ? StylePosition()->UsedAlignSelf(alignCB->Style())._0
6338 : StylePosition()->UsedJustifySelf(alignCB->Style())._0;
6339 stretch = inlineAxisAlignment == StyleAlignFlags::STRETCH ||
6340 (inlineAxisAlignment == StyleAlignFlags::NORMAL &&
6341 !mayUseAspectRatio);
6344 // Apply the preferred aspect ratio for alignments other than *stretch* and
6345 // *normal without aspect ratio*.
6346 // The spec says all other values should size the items as fit-content, and
6347 // the intrinsic size should respect the preferred aspect ratio, so we also
6348 // apply aspect ratio for all other values.
6349 // https://drafts.csswg.org/css-grid/#grid-item-sizing
6350 if (!stretch && mayUseAspectRatio) {
6351 // Note: we don't need to handle aspect ratio for inline axis if both
6352 // width/height are auto. The default ratio-dependent axis is block axis
6353 // in this case, so we can simply get the block size from the non-auto
6354 // |styleBSize|.
6355 auto bSize = nsLayoutUtils::ComputeBSizeValue(
6356 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6357 styleBSize.AsLengthPercentage());
6358 result.ISize(aWM) = aspectRatio.ComputeRatioDependentSize(
6359 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
6360 aspectRatioUsage = AspectRatioUsage::ToComputeISize;
6363 if (stretch || aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
6364 auto iSizeToFillCB =
6365 std::max(nscoord(0), aCBSize.ISize(aWM) - aBorderPadding.ISize(aWM) -
6366 aMargin.ISize(aWM));
6367 if (stretch || result.ISize(aWM) > iSizeToFillCB) {
6368 result.ISize(aWM) = iSizeToFillCB;
6371 } else if (aspectRatio && !isAutoBSize) {
6372 auto bSize = nsLayoutUtils::ComputeBSizeValue(
6373 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6374 styleBSize.AsLengthPercentage());
6375 result.ISize(aWM) = aspectRatio.ComputeRatioDependentSize(
6376 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
6377 aspectRatioUsage = AspectRatioUsage::ToComputeISize;
6380 // Calculate and apply transferred min & max size contraints.
6381 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-size-transfers
6383 // Note: The basic principle is that sizing constraints transfer through the
6384 // aspect-ratio to the other side to preserve the aspect ratio to the extent
6385 // that they can without violating any sizes specified explicitly on that
6386 // affected axis.
6388 // FIXME: The spec words may not be correct, so we may have to update this
6389 // tentative solution once this spec issue gets resolved. Here, we clamp the
6390 // flex base size by the transferred min and max sizes, and don't include
6391 // the transferred min & max sizes into its used min & max sizes. So this
6392 // lets us match other browsers' current behaviors.
6393 // https://github.com/w3c/csswg-drafts/issues/6071
6395 // Note: This may make more sense if we clamp the flex base size in
6396 // FlexItem::ResolveFlexBaseSizeFromAspectRatio(). However, the result should
6397 // be identical. FlexItem::ResolveFlexBaseSizeFromAspectRatio() only handles
6398 // the case of the definite cross size, and the definite cross size is clamped
6399 // by the min & max cross sizes below in this function. This means its flex
6400 // base size has been clamped by the transferred min & max size already after
6401 // generating the flex items. So here we make the code more general for both
6402 // definite cross size and indefinite cross size.
6403 const bool isDefiniteISize = styleISize.IsLengthPercentage();
6404 const auto& minBSizeCoord = stylePos->MinBSize(aWM);
6405 const auto& maxBSizeCoord = stylePos->MaxBSize(aWM);
6406 const bool isAutoMinBSize =
6407 nsLayoutUtils::IsAutoBSize(minBSizeCoord, aCBSize.BSize(aWM));
6408 const bool isAutoMaxBSize =
6409 nsLayoutUtils::IsAutoBSize(maxBSizeCoord, aCBSize.BSize(aWM));
6410 if (aspectRatio && !isDefiniteISize) {
6411 const MinMaxSize minMaxBSize{
6412 isAutoMinBSize ? 0
6413 : nsLayoutUtils::ComputeBSizeValue(
6414 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6415 minBSizeCoord.AsLengthPercentage()),
6416 isAutoMaxBSize ? NS_UNCONSTRAINEDSIZE
6417 : nsLayoutUtils::ComputeBSizeValue(
6418 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6419 maxBSizeCoord.AsLengthPercentage())};
6420 MinMaxSize transferredMinMaxISize = ComputeTransferredMinMaxInlineSize(
6421 aWM, aspectRatio, minMaxBSize, boxSizingAdjust);
6423 result.ISize(aWM) =
6424 transferredMinMaxISize.ClampSizeToMinAndMax(result.ISize(aWM));
6427 // Flex items ignore their min & max sizing properties in their
6428 // flex container's main-axis. (Those properties get applied later in
6429 // the flexbox algorithm.)
6430 const bool isFlexItemInlineAxisMainAxis =
6431 isFlexItem && flexMainAxis == eLogicalAxisInline;
6432 const auto& maxISizeCoord = stylePos->MaxISize(aWM);
6433 nscoord maxISize = NS_UNCONSTRAINEDSIZE;
6434 if (!maxISizeCoord.IsNone() && !isFlexItemInlineAxisMainAxis) {
6435 maxISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
6436 boxSizingAdjust, boxSizingToMarginEdgeISize,
6437 maxISizeCoord, aSizeOverrides, aFlags)
6438 .mISize;
6439 result.ISize(aWM) = std::min(maxISize, result.ISize(aWM));
6442 const auto& minISizeCoord = stylePos->MinISize(aWM);
6443 nscoord minISize;
6444 if (!minISizeCoord.IsAuto() && !isFlexItemInlineAxisMainAxis) {
6445 minISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
6446 boxSizingAdjust, boxSizingToMarginEdgeISize,
6447 minISizeCoord, aSizeOverrides, aFlags)
6448 .mISize;
6449 } else if (MOZ_UNLIKELY(
6450 aFlags.contains(ComputeSizeFlag::IApplyAutoMinSize))) {
6451 // This implements "Implied Minimum Size of Grid Items".
6452 // https://drafts.csswg.org/css-grid/#min-size-auto
6453 minISize = std::min(maxISize, GetMinISize(aRenderingContext));
6454 if (styleISize.IsLengthPercentage()) {
6455 minISize = std::min(minISize, result.ISize(aWM));
6456 } else if (aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
6457 // "if the grid item spans only grid tracks that have a fixed max track
6458 // sizing function, its automatic minimum size in that dimension is
6459 // further clamped to less than or equal to the size necessary to fit
6460 // its margin box within the resulting grid area (flooring at zero)"
6461 // https://drafts.csswg.org/css-grid/#min-size-auto
6462 auto maxMinISize =
6463 std::max(nscoord(0), aCBSize.ISize(aWM) - aBorderPadding.ISize(aWM) -
6464 aMargin.ISize(aWM));
6465 minISize = std::min(minISize, maxMinISize);
6467 } else if (aspectRatioUsage == AspectRatioUsage::ToComputeISize &&
6468 ShouldApplyAutomaticMinimumOnInlineAxis(aWM, disp, stylePos)) {
6469 // This means we successfully applied aspect-ratio and now need to check
6470 // if we need to apply the implied minimum size:
6471 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
6472 MOZ_ASSERT(!HasReplacedSizing(),
6473 "aspect-ratio minimums should not apply to replaced elements");
6474 // The inline size computed by aspect-ratio shouldn't less than the content
6475 // size.
6476 minISize = GetMinISize(aRenderingContext);
6477 } else {
6478 // Treat "min-width: auto" as 0.
6479 // NOTE: Technically, "auto" is supposed to behave like "min-content" on
6480 // flex items. However, we don't need to worry about that here, because
6481 // flex items' min-sizes are intentionally ignored until the flex
6482 // container explicitly considers them during space distribution.
6483 minISize = 0;
6485 result.ISize(aWM) = std::max(minISize, result.ISize(aWM));
6487 // Compute block-axis size
6488 // (but not if we have auto bsize -- then, we'll just stick with the bsize
6489 // that we already calculated in the initial ComputeAutoSize() call. However,
6490 // if we have a valid preferred aspect ratio, we still have to compute the
6491 // block size because aspect ratio affects the intrinsic content size.)
6492 if (!isAutoBSize) {
6493 result.BSize(aWM) = nsLayoutUtils::ComputeBSizeValue(
6494 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6495 styleBSize.AsLengthPercentage());
6496 } else if (MOZ_UNLIKELY(isGridItem) && styleBSize.IsAuto() &&
6497 !aFlags.contains(ComputeSizeFlag::IsGridMeasuringReflow) &&
6498 !IsTrueOverflowContainer() &&
6499 !alignCB->IsMasonry(isOrthogonal ? eLogicalAxisInline
6500 : eLogicalAxisBlock)) {
6501 auto cbSize = aCBSize.BSize(aWM);
6502 if (cbSize != NS_UNCONSTRAINEDSIZE) {
6503 // 'auto' block-size for grid-level box - fill the CB for 'stretch' /
6504 // 'normal' and clamp it to the CB if requested:
6505 bool stretch = false;
6506 bool mayUseAspectRatio =
6507 aspectRatio && result.ISize(aWM) != NS_UNCONSTRAINEDSIZE;
6508 if (!StyleMargin()->HasBlockAxisAuto(aWM)) {
6509 auto blockAxisAlignment =
6510 isOrthogonal ? StylePosition()->UsedJustifySelf(alignCB->Style())._0
6511 : StylePosition()->UsedAlignSelf(alignCB->Style())._0;
6512 stretch = blockAxisAlignment == StyleAlignFlags::STRETCH ||
6513 (blockAxisAlignment == StyleAlignFlags::NORMAL &&
6514 !mayUseAspectRatio);
6517 // Apply the preferred aspect ratio for alignments other than *stretch*
6518 // and *normal without aspect ratio*.
6519 // The spec says all other values should size the items as fit-content,
6520 // and the intrinsic size should respect the preferred aspect ratio, so
6521 // we also apply aspect ratio for all other values.
6522 // https://drafts.csswg.org/css-grid/#grid-item-sizing
6523 if (!stretch && mayUseAspectRatio) {
6524 result.BSize(aWM) = aspectRatio.ComputeRatioDependentSize(
6525 LogicalAxis::eLogicalAxisBlock, aWM, result.ISize(aWM),
6526 boxSizingAdjust);
6527 MOZ_ASSERT(aspectRatioUsage == AspectRatioUsage::None);
6528 aspectRatioUsage = AspectRatioUsage::ToComputeBSize;
6531 if (stretch || aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize)) {
6532 auto bSizeToFillCB =
6533 std::max(nscoord(0),
6534 cbSize - aBorderPadding.BSize(aWM) - aMargin.BSize(aWM));
6535 if (stretch || (result.BSize(aWM) != NS_UNCONSTRAINEDSIZE &&
6536 result.BSize(aWM) > bSizeToFillCB)) {
6537 result.BSize(aWM) = bSizeToFillCB;
6541 } else if (aspectRatio) {
6542 // If both inline and block dimensions are auto, the block axis is the
6543 // ratio-dependent axis by default.
6544 // If we have a super large inline size, aspect-ratio should still be
6545 // applied (so aspectRatioUsage flag is set as expected). That's why we
6546 // apply aspect-ratio unconditionally for auto block size here.
6547 result.BSize(aWM) = aspectRatio.ComputeRatioDependentSize(
6548 LogicalAxis::eLogicalAxisBlock, aWM, result.ISize(aWM),
6549 boxSizingAdjust);
6550 MOZ_ASSERT(aspectRatioUsage == AspectRatioUsage::None);
6551 aspectRatioUsage = AspectRatioUsage::ToComputeBSize;
6554 if (result.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
6555 const bool isFlexItemBlockAxisMainAxis =
6556 isFlexItem && flexMainAxis == eLogicalAxisBlock;
6557 if (!isAutoMaxBSize && !isFlexItemBlockAxisMainAxis) {
6558 nscoord maxBSize = nsLayoutUtils::ComputeBSizeValue(
6559 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6560 maxBSizeCoord.AsLengthPercentage());
6561 result.BSize(aWM) = std::min(maxBSize, result.BSize(aWM));
6564 if (!isAutoMinBSize && !isFlexItemBlockAxisMainAxis) {
6565 nscoord minBSize = nsLayoutUtils::ComputeBSizeValue(
6566 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6567 minBSizeCoord.AsLengthPercentage());
6568 result.BSize(aWM) = std::max(minBSize, result.BSize(aWM));
6572 if (IsThemed(disp)) {
6573 nsPresContext* pc = PresContext();
6574 const LayoutDeviceIntSize widget = pc->Theme()->GetMinimumWidgetSize(
6575 pc, this, disp->EffectiveAppearance());
6577 // Convert themed widget's physical dimensions to logical coords
6578 LogicalSize size(aWM, LayoutDeviceIntSize::ToAppUnits(
6579 widget, pc->AppUnitsPerDevPixel()));
6581 // GetMinimumWidgetSize() returns border-box; we need content-box.
6582 size -= aBorderPadding;
6584 if (size.BSize(aWM) > result.BSize(aWM)) {
6585 result.BSize(aWM) = size.BSize(aWM);
6587 if (size.ISize(aWM) > result.ISize(aWM)) {
6588 result.ISize(aWM) = size.ISize(aWM);
6592 result.ISize(aWM) = std::max(0, result.ISize(aWM));
6593 result.BSize(aWM) = std::max(0, result.BSize(aWM));
6595 return {result, aspectRatioUsage};
6598 nsRect nsIFrame::ComputeTightBounds(DrawTarget* aDrawTarget) const {
6599 return InkOverflowRect();
6602 /* virtual */
6603 nsresult nsIFrame::GetPrefWidthTightBounds(gfxContext* aContext, nscoord* aX,
6604 nscoord* aXMost) {
6605 return NS_ERROR_NOT_IMPLEMENTED;
6608 /* virtual */
6609 LogicalSize nsIFrame::ComputeAutoSize(
6610 gfxContext* aRenderingContext, WritingMode aWM,
6611 const mozilla::LogicalSize& aCBSize, nscoord aAvailableISize,
6612 const mozilla::LogicalSize& aMargin,
6613 const mozilla::LogicalSize& aBorderPadding,
6614 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) {
6615 // Use basic shrink-wrapping as a default implementation.
6616 LogicalSize result(aWM, 0xdeadbeef, NS_UNCONSTRAINEDSIZE);
6618 // don't bother setting it if the result won't be used
6619 const auto& styleISize = aSizeOverrides.mStyleISize
6620 ? *aSizeOverrides.mStyleISize
6621 : StylePosition()->ISize(aWM);
6622 if (styleISize.IsAuto()) {
6623 nscoord availBased =
6624 aAvailableISize - aMargin.ISize(aWM) - aBorderPadding.ISize(aWM);
6625 result.ISize(aWM) = ShrinkISizeToFit(aRenderingContext, availBased, aFlags);
6627 return result;
6630 nscoord nsIFrame::ShrinkISizeToFit(gfxContext* aRenderingContext,
6631 nscoord aISizeInCB,
6632 ComputeSizeFlags aFlags) {
6633 // If we're a container for font size inflation, then shrink
6634 // wrapping inside of us should not apply font size inflation.
6635 AutoMaybeDisableFontInflation an(this);
6637 nscoord result;
6638 nscoord minISize = GetMinISize(aRenderingContext);
6639 if (minISize > aISizeInCB) {
6640 const bool clamp = aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize);
6641 result = MOZ_UNLIKELY(clamp) ? aISizeInCB : minISize;
6642 } else {
6643 nscoord prefISize = GetPrefISize(aRenderingContext);
6644 if (prefISize > aISizeInCB) {
6645 result = aISizeInCB;
6646 } else {
6647 result = prefISize;
6650 return result;
6653 Maybe<nscoord> nsIFrame::ComputeInlineSizeFromAspectRatio(
6654 WritingMode aWM, const LogicalSize& aCBSize,
6655 const LogicalSize& aContentEdgeToBoxSizing,
6656 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) const {
6657 // FIXME: Bug 1670151: Use GetAspectRatio() to cover replaced elements (and
6658 // then we can drop the check of eSupportsAspectRatio).
6659 const AspectRatio aspectRatio =
6660 aSizeOverrides.mAspectRatio
6661 ? *aSizeOverrides.mAspectRatio
6662 : StylePosition()->mAspectRatio.ToLayoutRatio();
6663 if (!SupportsAspectRatio() || !aspectRatio) {
6664 return Nothing();
6667 const StyleSize& styleBSize = aSizeOverrides.mStyleBSize
6668 ? *aSizeOverrides.mStyleBSize
6669 : StylePosition()->BSize(aWM);
6670 if (nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM))) {
6671 return Nothing();
6674 MOZ_ASSERT(styleBSize.IsLengthPercentage());
6675 nscoord bSize = nsLayoutUtils::ComputeBSizeValue(
6676 aCBSize.BSize(aWM), aContentEdgeToBoxSizing.BSize(aWM),
6677 styleBSize.AsLengthPercentage());
6678 return Some(aspectRatio.ComputeRatioDependentSize(
6679 LogicalAxis::eLogicalAxisInline, aWM, bSize, aContentEdgeToBoxSizing));
6682 nsIFrame::ISizeComputationResult nsIFrame::ComputeISizeValue(
6683 gfxContext* aRenderingContext, const WritingMode aWM,
6684 const LogicalSize& aContainingBlockSize,
6685 const LogicalSize& aContentEdgeToBoxSizing, nscoord aBoxSizingToMarginEdge,
6686 ExtremumLength aSize, Maybe<nscoord> aAvailableISizeOverride,
6687 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) {
6688 // If 'this' is a container for font size inflation, then shrink
6689 // wrapping inside of it should not apply font size inflation.
6690 AutoMaybeDisableFontInflation an(this);
6691 // If we have an aspect-ratio and a definite block size, we resolve the
6692 // min-content and max-content size by the aspect-ratio and the block size.
6693 // https://github.com/w3c/csswg-drafts/issues/5032
6694 Maybe<nscoord> intrinsicSizeFromAspectRatio =
6695 aSize == ExtremumLength::MozAvailable
6696 ? Nothing()
6697 : ComputeInlineSizeFromAspectRatio(aWM, aContainingBlockSize,
6698 aContentEdgeToBoxSizing,
6699 aSizeOverrides, aFlags);
6700 nscoord result;
6701 switch (aSize) {
6702 case ExtremumLength::MaxContent:
6703 result = intrinsicSizeFromAspectRatio ? *intrinsicSizeFromAspectRatio
6704 : GetPrefISize(aRenderingContext);
6705 NS_ASSERTION(result >= 0, "inline-size less than zero");
6706 return {result, intrinsicSizeFromAspectRatio
6707 ? AspectRatioUsage::ToComputeISize
6708 : AspectRatioUsage::None};
6709 case ExtremumLength::MinContent:
6710 result = intrinsicSizeFromAspectRatio ? *intrinsicSizeFromAspectRatio
6711 : GetMinISize(aRenderingContext);
6712 NS_ASSERTION(result >= 0, "inline-size less than zero");
6713 if (MOZ_UNLIKELY(
6714 aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize))) {
6715 auto available =
6716 aContainingBlockSize.ISize(aWM) -
6717 (aBoxSizingToMarginEdge + aContentEdgeToBoxSizing.ISize(aWM));
6718 result = std::min(available, result);
6720 return {result, intrinsicSizeFromAspectRatio
6721 ? AspectRatioUsage::ToComputeISize
6722 : AspectRatioUsage::None};
6723 case ExtremumLength::FitContentFunction:
6724 case ExtremumLength::FitContent: {
6725 nscoord pref = NS_UNCONSTRAINEDSIZE;
6726 nscoord min = 0;
6727 if (intrinsicSizeFromAspectRatio) {
6728 // The min-content and max-content size are identical and equal to the
6729 // size computed from the block size and the aspect ratio.
6730 pref = min = *intrinsicSizeFromAspectRatio;
6731 } else {
6732 pref = GetPrefISize(aRenderingContext);
6733 min = GetMinISize(aRenderingContext);
6736 nscoord fill = aAvailableISizeOverride
6737 ? *aAvailableISizeOverride
6738 : aContainingBlockSize.ISize(aWM) -
6739 (aBoxSizingToMarginEdge +
6740 aContentEdgeToBoxSizing.ISize(aWM));
6742 if (MOZ_UNLIKELY(
6743 aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize))) {
6744 min = std::min(min, fill);
6746 result = std::max(min, std::min(pref, fill));
6747 NS_ASSERTION(result >= 0, "inline-size less than zero");
6748 return {result};
6750 case ExtremumLength::MozAvailable:
6751 return {aContainingBlockSize.ISize(aWM) -
6752 (aBoxSizingToMarginEdge + aContentEdgeToBoxSizing.ISize(aWM))};
6754 MOZ_ASSERT_UNREACHABLE("Unknown extremum length?");
6755 return {};
6758 nscoord nsIFrame::ComputeISizeValue(const WritingMode aWM,
6759 const LogicalSize& aContainingBlockSize,
6760 const LogicalSize& aContentEdgeToBoxSizing,
6761 const LengthPercentage& aSize) {
6762 LAYOUT_WARN_IF_FALSE(
6763 aContainingBlockSize.ISize(aWM) != NS_UNCONSTRAINEDSIZE,
6764 "have unconstrained inline-size; this should only result from "
6765 "very large sizes, not attempts at intrinsic inline-size "
6766 "calculation");
6767 NS_ASSERTION(aContainingBlockSize.ISize(aWM) >= 0,
6768 "inline-size less than zero");
6770 nscoord result = aSize.Resolve(aContainingBlockSize.ISize(aWM));
6771 // The result of a calc() expression might be less than 0; we
6772 // should clamp at runtime (below). (Percentages and coords that
6773 // are less than 0 have already been dropped by the parser.)
6774 result -= aContentEdgeToBoxSizing.ISize(aWM);
6775 return std::max(0, result);
6778 void nsIFrame::DidReflow(nsPresContext* aPresContext,
6779 const ReflowInput* aReflowInput) {
6780 NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS, ("nsIFrame::DidReflow"));
6782 if (IsHiddenByContentVisibilityOfInFlowParentForLayout()) {
6783 RemoveStateBits(NS_FRAME_IN_REFLOW);
6784 return;
6787 SVGObserverUtils::InvalidateDirectRenderingObservers(
6788 this, SVGObserverUtils::INVALIDATE_REFLOW);
6790 RemoveStateBits(NS_FRAME_IN_REFLOW | NS_FRAME_FIRST_REFLOW |
6791 NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN);
6793 // Clear bits that were used in ReflowInput::InitResizeFlags (see
6794 // comment there for why we can't clear it there).
6795 SetHasBSizeChange(false);
6796 SetHasPaddingChange(false);
6798 // Notify the percent bsize observer if there is a percent bsize.
6799 // The observer may be able to initiate another reflow with a computed
6800 // bsize. This happens in the case where a table cell has no computed
6801 // bsize but can fabricate one when the cell bsize is known.
6802 if (aReflowInput && aReflowInput->mPercentBSizeObserver && !GetPrevInFlow()) {
6803 const auto& bsize =
6804 aReflowInput->mStylePosition->BSize(aReflowInput->GetWritingMode());
6805 if (bsize.HasPercent()) {
6806 aReflowInput->mPercentBSizeObserver->NotifyPercentBSize(*aReflowInput);
6810 aPresContext->ReflowedFrame();
6813 void nsIFrame::FinishReflowWithAbsoluteFrames(nsPresContext* aPresContext,
6814 ReflowOutput& aDesiredSize,
6815 const ReflowInput& aReflowInput,
6816 nsReflowStatus& aStatus,
6817 bool aConstrainBSize) {
6818 ReflowAbsoluteFrames(aPresContext, aDesiredSize, aReflowInput, aStatus,
6819 aConstrainBSize);
6821 FinishAndStoreOverflow(&aDesiredSize, aReflowInput.mStyleDisplay);
6824 void nsIFrame::ReflowAbsoluteFrames(nsPresContext* aPresContext,
6825 ReflowOutput& aDesiredSize,
6826 const ReflowInput& aReflowInput,
6827 nsReflowStatus& aStatus,
6828 bool aConstrainBSize) {
6829 if (HasAbsolutelyPositionedChildren()) {
6830 nsAbsoluteContainingBlock* absoluteContainer = GetAbsoluteContainingBlock();
6832 // Let the absolutely positioned container reflow any absolutely positioned
6833 // child frames that need to be reflowed
6835 // The containing block for the abs pos kids is formed by our padding edge.
6836 nsMargin usedBorder = GetUsedBorder();
6837 nscoord containingBlockWidth =
6838 std::max(0, aDesiredSize.Width() - usedBorder.LeftRight());
6839 nscoord containingBlockHeight =
6840 std::max(0, aDesiredSize.Height() - usedBorder.TopBottom());
6841 nsContainerFrame* container = do_QueryFrame(this);
6842 NS_ASSERTION(container,
6843 "Abs-pos children only supported on container frames for now");
6845 nsRect containingBlock(0, 0, containingBlockWidth, containingBlockHeight);
6846 AbsPosReflowFlags flags =
6847 AbsPosReflowFlags::CBWidthAndHeightChanged; // XXX could be optimized
6848 if (aConstrainBSize) {
6849 flags |= AbsPosReflowFlags::ConstrainHeight;
6851 absoluteContainer->Reflow(container, aPresContext, aReflowInput, aStatus,
6852 containingBlock, flags,
6853 &aDesiredSize.mOverflowAreas);
6857 /* virtual */
6858 bool nsIFrame::CanContinueTextRun() const {
6859 // By default, a frame will *not* allow a text run to be continued
6860 // through it.
6861 return false;
6864 void nsIFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize,
6865 const ReflowInput& aReflowInput,
6866 nsReflowStatus& aStatus) {
6867 MarkInReflow();
6868 DO_GLOBAL_REFLOW_COUNT("nsFrame");
6869 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
6870 aDesiredSize.ClearSize();
6873 bool nsIFrame::IsContentDisabled() const {
6874 // FIXME(emilio): Doing this via CSS means callers must ensure the style is up
6875 // to date, and they don't!
6876 if (StyleUI()->UserInput() == StyleUserInput::None) {
6877 return true;
6880 auto* element = nsGenericHTMLElement::FromNodeOrNull(GetContent());
6881 return element && element->IsDisabled();
6884 bool nsIFrame::IsContentRelevant() const {
6885 MOZ_ASSERT(StyleDisplay()->ContentVisibility(*this) ==
6886 StyleContentVisibility::Auto);
6888 auto* element = Element::FromNodeOrNull(GetContent());
6889 MOZ_ASSERT(element);
6891 Maybe<ContentRelevancy> relevancy = element->GetContentRelevancy();
6892 if (relevancy.isSome()) {
6893 return !relevancy->isEmpty();
6896 // If there is no relevancy set, then this frame still has not received had
6897 // the initial visibility callback call. In that case, only rely on whether
6898 // or not it is inside a top layer element which will never change for this
6899 // frame and allows proper rendering of the top layer.
6900 return IsDescendantOfTopLayerElement();
6903 bool nsIFrame::HidesContent(
6904 const EnumSet<IncludeContentVisibility>& aInclude) const {
6905 auto effectiveContentVisibility = StyleDisplay()->ContentVisibility(*this);
6906 if (aInclude.contains(IncludeContentVisibility::Hidden) &&
6907 effectiveContentVisibility == StyleContentVisibility::Hidden) {
6908 return true;
6911 if (aInclude.contains(IncludeContentVisibility::Auto) &&
6912 effectiveContentVisibility == StyleContentVisibility::Auto) {
6913 return !IsContentRelevant();
6916 return false;
6919 bool nsIFrame::HidesContentForLayout() const {
6920 return HidesContent() && !PresShell()->IsForcingLayoutForHiddenContent(this);
6923 bool nsIFrame::IsHiddenByContentVisibilityOfInFlowParentForLayout() const {
6924 const auto* parent = GetInFlowParent();
6925 // The anonymous children owned by parent are important for properly sizing
6926 // their parents.
6927 return parent && parent->HidesContentForLayout() &&
6928 !(parent->HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES) &&
6929 Style()->IsAnonBox());
6932 nsIFrame* nsIFrame::GetClosestContentVisibilityAncestor(
6933 const EnumSet<IncludeContentVisibility>& aInclude) const {
6934 if (!StaticPrefs::layout_css_content_visibility_enabled()) {
6935 return nullptr;
6938 auto* parent = GetInFlowParent();
6939 bool isAnonymousBlock = Style()->IsAnonBox() && parent &&
6940 parent->HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES);
6941 for (nsIFrame* cur = parent; cur; cur = cur->GetInFlowParent()) {
6942 if (!isAnonymousBlock && cur->HidesContent(aInclude)) {
6943 return cur;
6946 // Anonymous boxes are not hidden by the content-visibility of their first
6947 // non-anonymous ancestor, but can be hidden by ancestors further up the
6948 // tree.
6949 isAnonymousBlock = false;
6952 return nullptr;
6955 bool nsIFrame::IsHiddenByContentVisibilityOnAnyAncestor(
6956 const EnumSet<IncludeContentVisibility>& aInclude) const {
6957 return !!GetClosestContentVisibilityAncestor(aInclude);
6960 bool nsIFrame::HasSelectionInSubtree() {
6961 if (IsSelected()) {
6962 return true;
6965 RefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
6966 if (!frameSelection) {
6967 return false;
6970 const Selection* selection =
6971 frameSelection->GetSelection(SelectionType::eNormal);
6972 if (!selection) {
6973 return false;
6976 for (uint32_t i = 0; i < selection->RangeCount(); i++) {
6977 auto* range = selection->GetRangeAt(i);
6978 MOZ_ASSERT(range);
6980 const auto* commonAncestorNode =
6981 range->GetRegisteredClosestCommonInclusiveAncestor();
6982 if (commonAncestorNode &&
6983 commonAncestorNode->IsInclusiveDescendantOf(GetContent())) {
6984 return true;
6988 return false;
6991 bool nsIFrame::IsDescendantOfTopLayerElement() const {
6992 if (!GetContent()) {
6993 return false;
6996 nsTArray<dom::Element*> topLayer = PresContext()->Document()->GetTopLayer();
6997 for (auto* element : topLayer) {
6998 if (GetContent()->IsInclusiveFlatTreeDescendantOf(element)) {
6999 return true;
7003 return false;
7006 bool nsIFrame::UpdateIsRelevantContent(
7007 const ContentRelevancy& aRelevancyToUpdate) {
7008 MOZ_ASSERT(StyleDisplay()->ContentVisibility(*this) ==
7009 StyleContentVisibility::Auto);
7011 auto* element = Element::FromNodeOrNull(GetContent());
7012 MOZ_ASSERT(element);
7014 ContentRelevancy newRelevancy;
7015 Maybe<ContentRelevancy> oldRelevancy = element->GetContentRelevancy();
7016 if (oldRelevancy.isSome()) {
7017 newRelevancy = *oldRelevancy;
7020 auto setRelevancyValue = [&](ContentRelevancyReason reason, bool value) {
7021 if (value) {
7022 newRelevancy += reason;
7023 } else {
7024 newRelevancy -= reason;
7028 if (!oldRelevancy ||
7029 aRelevancyToUpdate.contains(ContentRelevancyReason::Visible)) {
7030 Maybe<bool> visible = element->GetVisibleForContentVisibility();
7031 if (visible.isSome()) {
7032 setRelevancyValue(ContentRelevancyReason::Visible, *visible);
7036 if (!oldRelevancy ||
7037 aRelevancyToUpdate.contains(ContentRelevancyReason::FocusInSubtree)) {
7038 setRelevancyValue(ContentRelevancyReason::FocusInSubtree,
7039 element->State().HasAtLeastOneOfStates(
7040 ElementState::FOCUS_WITHIN | ElementState::FOCUS));
7043 if (!oldRelevancy ||
7044 aRelevancyToUpdate.contains(ContentRelevancyReason::Selected)) {
7045 setRelevancyValue(ContentRelevancyReason::Selected,
7046 HasSelectionInSubtree());
7049 bool overallRelevancyChanged =
7050 !oldRelevancy || oldRelevancy->isEmpty() != newRelevancy.isEmpty();
7051 if (!oldRelevancy || *oldRelevancy != newRelevancy) {
7052 element->SetContentRelevancy(newRelevancy);
7055 if (!overallRelevancyChanged) {
7056 return false;
7059 HandleLastRememberedSize();
7060 PresShell()->FrameNeedsReflow(
7061 this, IntrinsicDirty::FrameAncestorsAndDescendants, NS_FRAME_IS_DIRTY);
7062 InvalidateFrame();
7064 ContentVisibilityAutoStateChangeEventInit init;
7065 init.mSkipped = newRelevancy.isEmpty();
7066 RefPtr<ContentVisibilityAutoStateChangeEvent> event =
7067 ContentVisibilityAutoStateChangeEvent::Constructor(
7068 element, u"contentvisibilityautostatechange"_ns, init);
7070 // Per
7071 // https://drafts.csswg.org/css-contain/#content-visibility-auto-state-changed
7072 // "This event is dispatched by posting a task at the time when the state
7073 // change occurs."
7074 RefPtr<AsyncEventDispatcher> asyncDispatcher =
7075 new AsyncEventDispatcher(element, event.forget());
7076 DebugOnly<nsresult> rv = asyncDispatcher->PostDOMEvent();
7077 NS_ASSERTION(NS_SUCCEEDED(rv), "AsyncEventDispatcher failed to dispatch");
7078 return true;
7081 nsresult nsIFrame::CharacterDataChanged(const CharacterDataChangeInfo&) {
7082 MOZ_ASSERT_UNREACHABLE("should only be called for text frames");
7083 return NS_OK;
7086 nsresult nsIFrame::AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute,
7087 int32_t aModType) {
7088 return NS_OK;
7091 // Flow member functions
7093 nsIFrame* nsIFrame::GetPrevContinuation() const { return nullptr; }
7095 void nsIFrame::SetPrevContinuation(nsIFrame* aPrevContinuation) {
7096 MOZ_ASSERT(false, "not splittable");
7099 nsIFrame* nsIFrame::GetNextContinuation() const { return nullptr; }
7101 void nsIFrame::SetNextContinuation(nsIFrame*) {
7102 MOZ_ASSERT(false, "not splittable");
7105 nsIFrame* nsIFrame::GetPrevInFlow() const { return nullptr; }
7107 void nsIFrame::SetPrevInFlow(nsIFrame* aPrevInFlow) {
7108 MOZ_ASSERT(false, "not splittable");
7111 nsIFrame* nsIFrame::GetNextInFlow() const { return nullptr; }
7113 void nsIFrame::SetNextInFlow(nsIFrame*) { MOZ_ASSERT(false, "not splittable"); }
7115 nsIFrame* nsIFrame::GetTailContinuation() {
7116 nsIFrame* frame = this;
7117 while (frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
7118 frame = frame->GetPrevContinuation();
7119 NS_ASSERTION(frame, "first continuation can't be overflow container");
7121 for (nsIFrame* next = frame->GetNextContinuation();
7122 next && !next->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
7123 next = frame->GetNextContinuation()) {
7124 frame = next;
7127 MOZ_ASSERT(frame, "illegal state in continuation chain.");
7128 return frame;
7131 // Associated view object
7132 void nsIFrame::SetView(nsView* aView) {
7133 if (aView) {
7134 aView->SetFrame(this);
7136 #ifdef DEBUG
7137 LayoutFrameType frameType = Type();
7138 NS_ASSERTION(frameType == LayoutFrameType::SubDocument ||
7139 frameType == LayoutFrameType::ListControl ||
7140 frameType == LayoutFrameType::Viewport ||
7141 frameType == LayoutFrameType::MenuPopup,
7142 "Only specific frame types can have an nsView");
7143 #endif
7145 // Store the view on the frame.
7146 SetViewInternal(aView);
7148 // Set the frame state bit that says the frame has a view
7149 AddStateBits(NS_FRAME_HAS_VIEW);
7151 // Let all of the ancestors know they have a descendant with a view.
7152 for (nsIFrame* f = GetParent();
7153 f && !f->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
7154 f = f->GetParent())
7155 f->AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
7156 } else {
7157 MOZ_ASSERT_UNREACHABLE("Destroying a view while the frame is alive?");
7158 RemoveStateBits(NS_FRAME_HAS_VIEW);
7159 SetViewInternal(nullptr);
7163 // Find the first geometric parent that has a view
7164 nsIFrame* nsIFrame::GetAncestorWithView() const {
7165 for (nsIFrame* f = GetParent(); nullptr != f; f = f->GetParent()) {
7166 if (f->HasView()) {
7167 return f;
7170 return nullptr;
7173 template <nsPoint (nsIFrame::*PositionGetter)() const>
7174 static nsPoint OffsetCalculator(const nsIFrame* aThis, const nsIFrame* aOther) {
7175 MOZ_ASSERT(aOther, "Must have frame for destination coordinate system!");
7177 NS_ASSERTION(aThis->PresContext() == aOther->PresContext(),
7178 "GetOffsetTo called on frames in different documents");
7180 nsPoint offset(0, 0);
7181 const nsIFrame* f;
7182 for (f = aThis; f != aOther && f; f = f->GetParent()) {
7183 offset += (f->*PositionGetter)();
7186 if (f != aOther) {
7187 // Looks like aOther wasn't an ancestor of |this|. So now we have
7188 // the root-frame-relative position of |this| in |offset|. Convert back
7189 // to the coordinates of aOther
7190 while (aOther) {
7191 offset -= (aOther->*PositionGetter)();
7192 aOther = aOther->GetParent();
7196 return offset;
7199 nsPoint nsIFrame::GetOffsetTo(const nsIFrame* aOther) const {
7200 return OffsetCalculator<&nsIFrame::GetPosition>(this, aOther);
7203 nsPoint nsIFrame::GetOffsetToIgnoringScrolling(const nsIFrame* aOther) const {
7204 return OffsetCalculator<&nsIFrame::GetPositionIgnoringScrolling>(this,
7205 aOther);
7208 nsPoint nsIFrame::GetOffsetToCrossDoc(const nsIFrame* aOther) const {
7209 return GetOffsetToCrossDoc(aOther, PresContext()->AppUnitsPerDevPixel());
7212 nsPoint nsIFrame::GetOffsetToCrossDoc(const nsIFrame* aOther,
7213 const int32_t aAPD) const {
7214 MOZ_ASSERT(aOther, "Must have frame for destination coordinate system!");
7215 NS_ASSERTION(PresContext()->GetRootPresContext() ==
7216 aOther->PresContext()->GetRootPresContext(),
7217 "trying to get the offset between frames in different document "
7218 "hierarchies?");
7219 if (PresContext()->GetRootPresContext() !=
7220 aOther->PresContext()->GetRootPresContext()) {
7221 // crash right away, we are almost certainly going to crash anyway.
7222 MOZ_CRASH(
7223 "trying to get the offset between frames in different "
7224 "document hierarchies?");
7227 const nsIFrame* root = nullptr;
7228 // offset will hold the final offset
7229 // docOffset holds the currently accumulated offset at the current APD, it
7230 // will be converted and added to offset when the current APD changes.
7231 nsPoint offset(0, 0), docOffset(0, 0);
7232 const nsIFrame* f = this;
7233 int32_t currAPD = PresContext()->AppUnitsPerDevPixel();
7234 while (f && f != aOther) {
7235 docOffset += f->GetPosition();
7236 nsIFrame* parent = f->GetParent();
7237 if (parent) {
7238 f = parent;
7239 } else {
7240 nsPoint newOffset(0, 0);
7241 root = f;
7242 f = nsLayoutUtils::GetCrossDocParentFrameInProcess(f, &newOffset);
7243 int32_t newAPD = f ? f->PresContext()->AppUnitsPerDevPixel() : 0;
7244 if (!f || newAPD != currAPD) {
7245 // Convert docOffset to the right APD and add it to offset.
7246 offset += docOffset.ScaleToOtherAppUnits(currAPD, aAPD);
7247 docOffset.x = docOffset.y = 0;
7249 currAPD = newAPD;
7250 docOffset += newOffset;
7253 if (f == aOther) {
7254 offset += docOffset.ScaleToOtherAppUnits(currAPD, aAPD);
7255 } else {
7256 // Looks like aOther wasn't an ancestor of |this|. So now we have
7257 // the root-document-relative position of |this| in |offset|. Subtract the
7258 // root-document-relative position of |aOther| from |offset|.
7259 // This call won't try to recurse again because root is an ancestor of
7260 // aOther.
7261 nsPoint negOffset = aOther->GetOffsetToCrossDoc(root, aAPD);
7262 offset -= negOffset;
7265 return offset;
7268 CSSIntRect nsIFrame::GetScreenRect() const {
7269 return CSSIntRect::FromAppUnitsToNearest(GetScreenRectInAppUnits());
7272 nsRect nsIFrame::GetScreenRectInAppUnits() const {
7273 nsPresContext* presContext = PresContext();
7274 nsIFrame* rootFrame = presContext->PresShell()->GetRootFrame();
7275 nsPoint rootScreenPos(0, 0);
7276 nsPoint rootFrameOffsetInParent(0, 0);
7277 nsIFrame* rootFrameParent = nsLayoutUtils::GetCrossDocParentFrameInProcess(
7278 rootFrame, &rootFrameOffsetInParent);
7279 if (rootFrameParent) {
7280 nsRect parentScreenRectAppUnits =
7281 rootFrameParent->GetScreenRectInAppUnits();
7282 nsPresContext* parentPresContext = rootFrameParent->PresContext();
7283 double parentScale = double(presContext->AppUnitsPerDevPixel()) /
7284 parentPresContext->AppUnitsPerDevPixel();
7285 nsPoint rootPt =
7286 parentScreenRectAppUnits.TopLeft() + rootFrameOffsetInParent;
7287 rootScreenPos.x = NS_round(parentScale * rootPt.x);
7288 rootScreenPos.y = NS_round(parentScale * rootPt.y);
7289 } else {
7290 nsCOMPtr<nsIWidget> rootWidget =
7291 presContext->PresShell()->GetViewManager()->GetRootWidget();
7292 if (rootWidget) {
7293 LayoutDeviceIntPoint rootDevPx = rootWidget->WidgetToScreenOffset();
7294 rootScreenPos.x = presContext->DevPixelsToAppUnits(rootDevPx.x);
7295 rootScreenPos.y = presContext->DevPixelsToAppUnits(rootDevPx.y);
7299 return nsRect(rootScreenPos + GetOffsetTo(rootFrame), GetSize());
7302 // Returns the offset from this frame to the closest geometric parent that
7303 // has a view. Also returns the containing view or null in case of error
7304 void nsIFrame::GetOffsetFromView(nsPoint& aOffset, nsView** aView) const {
7305 MOZ_ASSERT(nullptr != aView, "null OUT parameter pointer");
7306 nsIFrame* frame = const_cast<nsIFrame*>(this);
7308 *aView = nullptr;
7309 aOffset.MoveTo(0, 0);
7310 do {
7311 aOffset += frame->GetPosition();
7312 frame = frame->GetParent();
7313 } while (frame && !frame->HasView());
7315 if (frame) {
7316 *aView = frame->GetView();
7320 nsIWidget* nsIFrame::GetNearestWidget() const {
7321 return GetClosestView()->GetNearestWidget(nullptr);
7324 nsIWidget* nsIFrame::GetNearestWidget(nsPoint& aOffset) const {
7325 nsPoint offsetToView;
7326 nsPoint offsetToWidget;
7327 nsIWidget* widget =
7328 GetClosestView(&offsetToView)->GetNearestWidget(&offsetToWidget);
7329 aOffset = offsetToView + offsetToWidget;
7330 return widget;
7333 Matrix4x4Flagged nsIFrame::GetTransformMatrix(ViewportType aViewportType,
7334 RelativeTo aStopAtAncestor,
7335 nsIFrame** aOutAncestor,
7336 uint32_t aFlags) const {
7337 MOZ_ASSERT(aOutAncestor, "Need a place to put the ancestor!");
7339 /* If we're transformed, we want to hand back the combination
7340 * transform/translate matrix that will apply our current transform, then
7341 * shift us to our parent.
7343 const bool isTransformed = IsTransformed();
7344 const nsIFrame* zoomedContentRoot = nullptr;
7345 if (aStopAtAncestor.mViewportType == ViewportType::Visual) {
7346 zoomedContentRoot = ViewportUtils::IsZoomedContentRoot(this);
7347 if (zoomedContentRoot) {
7348 MOZ_ASSERT(aViewportType != ViewportType::Visual);
7352 if (isTransformed || zoomedContentRoot) {
7353 Matrix4x4 result;
7354 int32_t scaleFactor =
7355 ((aFlags & IN_CSS_UNITS) ? AppUnitsPerCSSPixel()
7356 : PresContext()->AppUnitsPerDevPixel());
7358 /* Compute the delta to the parent, which we need because we are converting
7359 * coordinates to our parent.
7361 if (isTransformed) {
7362 NS_ASSERTION(nsLayoutUtils::GetCrossDocParentFrameInProcess(this),
7363 "Cannot transform the viewport frame!");
7365 result = result * nsDisplayTransform::GetResultingTransformMatrix(
7366 this, nsPoint(0, 0), scaleFactor,
7367 nsDisplayTransform::INCLUDE_PERSPECTIVE |
7368 nsDisplayTransform::OFFSET_BY_ORIGIN);
7371 // The offset from a zoomed content root to its parent (e.g. from
7372 // a canvas frame to a scroll frame) is in layout coordinates, so
7373 // apply it before applying any layout-to-visual transform.
7374 *aOutAncestor = nsLayoutUtils::GetCrossDocParentFrameInProcess(this);
7375 nsPoint delta = GetOffsetToCrossDoc(*aOutAncestor);
7376 /* Combine the raw transform with a translation to our parent. */
7377 result.PostTranslate(NSAppUnitsToFloatPixels(delta.x, scaleFactor),
7378 NSAppUnitsToFloatPixels(delta.y, scaleFactor), 0.0f);
7380 if (zoomedContentRoot) {
7381 Matrix4x4 layoutToVisual;
7382 ScrollableLayerGuid::ViewID targetScrollId =
7383 nsLayoutUtils::FindOrCreateIDFor(zoomedContentRoot->GetContent());
7384 if (aFlags & nsIFrame::IN_CSS_UNITS) {
7385 layoutToVisual =
7386 ViewportUtils::GetVisualToLayoutTransform(targetScrollId)
7387 .Inverse()
7388 .ToUnknownMatrix();
7389 } else {
7390 layoutToVisual =
7391 ViewportUtils::GetVisualToLayoutTransform<LayoutDevicePixel>(
7392 targetScrollId)
7393 .Inverse()
7394 .ToUnknownMatrix();
7396 result = result * layoutToVisual;
7399 return result;
7402 *aOutAncestor = nsLayoutUtils::GetCrossDocParentFrameInProcess(this);
7404 /* Otherwise, we're not transformed. In that case, we'll walk up the frame
7405 * tree until we either hit the root frame or something that may be
7406 * transformed. We'll then change coordinates into that frame, since we're
7407 * guaranteed that nothing in-between can be transformed. First, however,
7408 * we have to check to see if we have a parent. If not, we'll set the
7409 * outparam to null (indicating that there's nothing left) and will hand back
7410 * the identity matrix.
7412 if (!*aOutAncestor) return Matrix4x4();
7414 /* Keep iterating while the frame can't possibly be transformed. */
7415 const nsIFrame* current = this;
7416 auto shouldStopAt = [](const nsIFrame* aCurrent, nsIFrame* aAncestor,
7417 uint32_t aFlags) {
7418 return aAncestor->IsTransformed() || nsLayoutUtils::IsPopup(aAncestor) ||
7419 ViewportUtils::IsZoomedContentRoot(aAncestor) ||
7420 ((aFlags & STOP_AT_STACKING_CONTEXT_AND_DISPLAY_PORT) &&
7421 (aAncestor->IsStackingContext() ||
7422 DisplayPortUtils::FrameHasDisplayPort(aAncestor, aCurrent)));
7424 while (*aOutAncestor != aStopAtAncestor.mFrame &&
7425 !shouldStopAt(current, *aOutAncestor, aFlags)) {
7426 /* If no parent, stop iterating. Otherwise, update the ancestor. */
7427 nsIFrame* parent =
7428 nsLayoutUtils::GetCrossDocParentFrameInProcess(*aOutAncestor);
7429 if (!parent) break;
7431 current = *aOutAncestor;
7432 *aOutAncestor = parent;
7435 NS_ASSERTION(*aOutAncestor, "Somehow ended up with a null ancestor...?");
7437 /* Translate from this frame to our ancestor, if it exists. That's the
7438 * entire transform, so we're done.
7440 nsPoint delta = GetOffsetToCrossDoc(*aOutAncestor);
7441 int32_t scaleFactor =
7442 ((aFlags & IN_CSS_UNITS) ? AppUnitsPerCSSPixel()
7443 : PresContext()->AppUnitsPerDevPixel());
7444 return Matrix4x4::Translation(NSAppUnitsToFloatPixels(delta.x, scaleFactor),
7445 NSAppUnitsToFloatPixels(delta.y, scaleFactor),
7446 0.0f);
7449 static void InvalidateRenderingObservers(nsIFrame* aDisplayRoot,
7450 nsIFrame* aFrame,
7451 bool aFrameChanged = true) {
7452 MOZ_ASSERT(aDisplayRoot == nsLayoutUtils::GetDisplayRootFrame(aFrame));
7453 SVGObserverUtils::InvalidateDirectRenderingObservers(aFrame);
7454 nsIFrame* parent = aFrame;
7455 while (parent != aDisplayRoot &&
7456 (parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(parent)) &&
7457 !parent->HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT)) {
7458 SVGObserverUtils::InvalidateDirectRenderingObservers(parent);
7461 if (!aFrameChanged) {
7462 return;
7465 aFrame->MarkNeedsDisplayItemRebuild();
7468 static void SchedulePaintInternal(
7469 nsIFrame* aDisplayRoot, nsIFrame* aFrame,
7470 nsIFrame::PaintType aType = nsIFrame::PAINT_DEFAULT) {
7471 MOZ_ASSERT(aDisplayRoot == nsLayoutUtils::GetDisplayRootFrame(aFrame));
7472 nsPresContext* pres = aDisplayRoot->PresContext()->GetRootPresContext();
7474 // No need to schedule a paint for an external document since they aren't
7475 // painted directly.
7476 if (!pres || (pres->Document() && pres->Document()->IsResourceDoc())) {
7477 return;
7479 if (!pres->GetContainerWeak()) {
7480 NS_WARNING("Shouldn't call SchedulePaint in a detached pres context");
7481 return;
7484 pres->PresShell()->ScheduleViewManagerFlush();
7486 if (aType == nsIFrame::PAINT_DEFAULT) {
7487 aDisplayRoot->AddStateBits(NS_FRAME_UPDATE_LAYER_TREE);
7491 static void InvalidateFrameInternal(nsIFrame* aFrame, bool aHasDisplayItem,
7492 bool aRebuildDisplayItems) {
7493 if (aHasDisplayItem) {
7494 aFrame->AddStateBits(NS_FRAME_NEEDS_PAINT);
7497 if (aRebuildDisplayItems) {
7498 aFrame->MarkNeedsDisplayItemRebuild();
7500 SVGObserverUtils::InvalidateDirectRenderingObservers(aFrame);
7501 bool needsSchedulePaint = false;
7502 if (nsLayoutUtils::IsPopup(aFrame)) {
7503 needsSchedulePaint = true;
7504 } else {
7505 nsIFrame* parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(aFrame);
7506 while (parent &&
7507 !parent->HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT)) {
7508 if (aHasDisplayItem && !parent->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
7509 parent->AddStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT);
7511 SVGObserverUtils::InvalidateDirectRenderingObservers(parent);
7513 // If we're inside a popup, then we need to make sure that we
7514 // call schedule paint so that the NS_FRAME_UPDATE_LAYER_TREE
7515 // flag gets added to the popup display root frame.
7516 if (nsLayoutUtils::IsPopup(parent)) {
7517 needsSchedulePaint = true;
7518 break;
7520 parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(parent);
7522 if (!parent) {
7523 needsSchedulePaint = true;
7526 if (!aHasDisplayItem) {
7527 return;
7529 if (needsSchedulePaint) {
7530 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(aFrame);
7531 SchedulePaintInternal(displayRoot, aFrame);
7533 if (aFrame->HasAnyStateBits(NS_FRAME_HAS_INVALID_RECT)) {
7534 aFrame->RemoveProperty(nsIFrame::InvalidationRect());
7535 aFrame->RemoveStateBits(NS_FRAME_HAS_INVALID_RECT);
7539 void nsIFrame::InvalidateFrameSubtree(bool aRebuildDisplayItems /* = true */) {
7540 InvalidateFrame(0, aRebuildDisplayItems);
7542 if (HasAnyStateBits(NS_FRAME_ALL_DESCENDANTS_NEED_PAINT)) {
7543 return;
7546 AddStateBits(NS_FRAME_ALL_DESCENDANTS_NEED_PAINT);
7548 for (const auto& childList : CrossDocChildLists()) {
7549 for (nsIFrame* child : childList.mList) {
7550 // Don't explicitly rebuild display items for our descendants,
7551 // since we should be marked and it implicitly includes all
7552 // descendants.
7553 child->InvalidateFrameSubtree(false);
7558 void nsIFrame::ClearInvalidationStateBits() {
7559 if (HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT)) {
7560 for (const auto& childList : CrossDocChildLists()) {
7561 for (nsIFrame* child : childList.mList) {
7562 child->ClearInvalidationStateBits();
7567 RemoveStateBits(NS_FRAME_NEEDS_PAINT | NS_FRAME_DESCENDANT_NEEDS_PAINT |
7568 NS_FRAME_ALL_DESCENDANTS_NEED_PAINT);
7571 bool HasRetainedDataFor(const nsIFrame* aFrame, uint32_t aDisplayItemKey) {
7572 if (RefPtr<WebRenderUserData> data =
7573 GetWebRenderUserData<WebRenderFallbackData>(aFrame,
7574 aDisplayItemKey)) {
7575 return true;
7578 return false;
7581 void nsIFrame::InvalidateFrame(uint32_t aDisplayItemKey,
7582 bool aRebuildDisplayItems /* = true */) {
7583 bool hasDisplayItem =
7584 !aDisplayItemKey || HasRetainedDataFor(this, aDisplayItemKey);
7585 InvalidateFrameInternal(this, hasDisplayItem, aRebuildDisplayItems);
7588 void nsIFrame::InvalidateFrameWithRect(const nsRect& aRect,
7589 uint32_t aDisplayItemKey,
7590 bool aRebuildDisplayItems /* = true */) {
7591 if (aRect.IsEmpty()) {
7592 return;
7594 bool hasDisplayItem =
7595 !aDisplayItemKey || HasRetainedDataFor(this, aDisplayItemKey);
7596 bool alreadyInvalid = false;
7597 if (!HasAnyStateBits(NS_FRAME_NEEDS_PAINT)) {
7598 InvalidateFrameInternal(this, hasDisplayItem, aRebuildDisplayItems);
7599 } else {
7600 alreadyInvalid = true;
7603 if (!hasDisplayItem) {
7604 return;
7607 nsRect* rect;
7608 if (HasAnyStateBits(NS_FRAME_HAS_INVALID_RECT)) {
7609 rect = GetProperty(InvalidationRect());
7610 MOZ_ASSERT(rect);
7611 } else {
7612 if (alreadyInvalid) {
7613 return;
7615 rect = new nsRect();
7616 AddProperty(InvalidationRect(), rect);
7617 AddStateBits(NS_FRAME_HAS_INVALID_RECT);
7620 *rect = rect->Union(aRect);
7623 /*static*/
7624 uint8_t nsIFrame::sLayerIsPrerenderedDataKey;
7626 bool nsIFrame::IsInvalid(nsRect& aRect) {
7627 if (!HasAnyStateBits(NS_FRAME_NEEDS_PAINT)) {
7628 return false;
7631 if (HasAnyStateBits(NS_FRAME_HAS_INVALID_RECT)) {
7632 nsRect* rect = GetProperty(InvalidationRect());
7633 NS_ASSERTION(
7634 rect, "Must have an invalid rect if NS_FRAME_HAS_INVALID_RECT is set!");
7635 aRect = *rect;
7636 } else {
7637 aRect.SetEmpty();
7639 return true;
7642 void nsIFrame::SchedulePaint(PaintType aType, bool aFrameChanged) {
7643 if (PresShell()->IsPaintingSuppressed()) {
7644 // We can't have any display items yet, and when we unsuppress we will
7645 // invalidate the root frame.
7646 return;
7648 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(this);
7649 InvalidateRenderingObservers(displayRoot, this, aFrameChanged);
7650 SchedulePaintInternal(displayRoot, this, aType);
7653 void nsIFrame::SchedulePaintWithoutInvalidatingObservers(PaintType aType) {
7654 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(this);
7655 SchedulePaintInternal(displayRoot, this, aType);
7658 void nsIFrame::InvalidateLayer(DisplayItemType aDisplayItemKey,
7659 const nsIntRect* aDamageRect,
7660 const nsRect* aFrameDamageRect,
7661 uint32_t aFlags /* = 0 */) {
7662 NS_ASSERTION(aDisplayItemKey > DisplayItemType::TYPE_ZERO, "Need a key");
7664 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(this);
7665 InvalidateRenderingObservers(displayRoot, this, false);
7667 // Check if frame supports WebRender's async update
7668 if ((aFlags & UPDATE_IS_ASYNC) &&
7669 WebRenderUserData::SupportsAsyncUpdate(this)) {
7670 // WebRender does not use layer, then return nullptr.
7671 return;
7674 if (aFrameDamageRect && aFrameDamageRect->IsEmpty()) {
7675 return;
7678 // In the bug 930056, dialer app startup but not shown on the
7679 // screen because sometimes we don't have any retainned data
7680 // for remote type displayitem and thus Repaint event is not
7681 // triggered. So, always invalidate in this case.
7682 DisplayItemType displayItemKey = aDisplayItemKey;
7683 if (aDisplayItemKey == DisplayItemType::TYPE_REMOTE) {
7684 displayItemKey = DisplayItemType::TYPE_ZERO;
7687 if (aFrameDamageRect) {
7688 InvalidateFrameWithRect(*aFrameDamageRect,
7689 static_cast<uint32_t>(displayItemKey));
7690 } else {
7691 InvalidateFrame(static_cast<uint32_t>(displayItemKey));
7695 static nsRect ComputeEffectsRect(nsIFrame* aFrame, const nsRect& aOverflowRect,
7696 const nsSize& aNewSize) {
7697 nsRect r = aOverflowRect;
7699 if (aFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
7700 // For SVG frames, we only need to account for filters.
7701 // TODO: We could also take account of clipPath and mask to reduce the
7702 // ink overflow, but that's not essential.
7703 if (aFrame->StyleEffects()->HasFilters()) {
7704 SetOrUpdateRectValuedProperty(aFrame, nsIFrame::PreEffectsBBoxProperty(),
7706 r = SVGUtils::GetPostFilterInkOverflowRect(aFrame, aOverflowRect);
7708 return r;
7711 // box-shadow
7712 r.UnionRect(r, nsLayoutUtils::GetBoxShadowRectForFrame(aFrame, aNewSize));
7714 // border-image-outset.
7715 // We need to include border-image-outset because it can cause the
7716 // border image to be drawn beyond the border box.
7718 // (1) It's important we not check whether there's a border-image
7719 // since the style hint for a change in border image doesn't cause
7720 // reflow, and that's probably more important than optimizing the
7721 // overflow areas for the silly case of border-image-outset without
7722 // border-image
7723 // (2) It's important that we not check whether the border-image
7724 // is actually loaded, since that would require us to reflow when
7725 // the image loads.
7726 const nsStyleBorder* styleBorder = aFrame->StyleBorder();
7727 nsMargin outsetMargin = styleBorder->GetImageOutset();
7729 if (outsetMargin != nsMargin(0, 0, 0, 0)) {
7730 nsRect outsetRect(nsPoint(0, 0), aNewSize);
7731 outsetRect.Inflate(outsetMargin);
7732 r.UnionRect(r, outsetRect);
7735 // Note that we don't remove the outlineInnerRect if a frame loses outline
7736 // style. That would require an extra property lookup for every frame,
7737 // or a new frame state bit to track whether a property had been stored,
7738 // or something like that. It's not worth doing that here. At most it's
7739 // only one heap-allocated rect per frame and it will be cleaned up when
7740 // the frame dies.
7742 if (SVGIntegrationUtils::UsingOverflowAffectingEffects(aFrame)) {
7743 SetOrUpdateRectValuedProperty(aFrame, nsIFrame::PreEffectsBBoxProperty(),
7745 r = SVGIntegrationUtils::ComputePostEffectsInkOverflowRect(aFrame, r);
7748 return r;
7751 void nsIFrame::SetPosition(const nsPoint& aPt) {
7752 if (mRect.TopLeft() == aPt) {
7753 return;
7755 mRect.MoveTo(aPt);
7756 MarkNeedsDisplayItemRebuild();
7759 void nsIFrame::MovePositionBy(const nsPoint& aTranslation) {
7760 nsPoint position = GetNormalPosition() + aTranslation;
7762 const nsMargin* computedOffsets = nullptr;
7763 if (IsRelativelyOrStickyPositioned()) {
7764 computedOffsets = GetProperty(nsIFrame::ComputedOffsetProperty());
7766 ReflowInput::ApplyRelativePositioning(
7767 this, computedOffsets ? *computedOffsets : nsMargin(), &position);
7768 SetPosition(position);
7771 nsRect nsIFrame::GetNormalRect() const {
7772 // It might be faster to first check
7773 // StyleDisplay()->IsRelativelyPositionedStyle().
7774 bool hasProperty;
7775 nsPoint normalPosition = GetProperty(NormalPositionProperty(), &hasProperty);
7776 if (hasProperty) {
7777 return nsRect(normalPosition, GetSize());
7779 return GetRect();
7782 nsRect nsIFrame::GetBoundingClientRect() {
7783 return nsLayoutUtils::GetAllInFlowRectsUnion(
7784 this, nsLayoutUtils::GetContainingBlockForClientRect(this),
7785 nsLayoutUtils::RECTS_ACCOUNT_FOR_TRANSFORMS);
7788 nsPoint nsIFrame::GetPositionIgnoringScrolling() const {
7789 return GetParent() ? GetParent()->GetPositionOfChildIgnoringScrolling(this)
7790 : GetPosition();
7793 nsRect nsIFrame::GetOverflowRect(OverflowType aType) const {
7794 // Note that in some cases the overflow area might not have been
7795 // updated (yet) to reflect any outline set on the frame or the area
7796 // of child frames. That's OK because any reflow that updates these
7797 // areas will invalidate the appropriate area, so any (mis)uses of
7798 // this method will be fixed up.
7800 if (mOverflow.mType == OverflowStorageType::Large) {
7801 // there is an overflow rect, and it's not stored as deltas but as
7802 // a separately-allocated rect
7803 return GetOverflowAreasProperty()->Overflow(aType);
7806 if (aType == OverflowType::Ink &&
7807 mOverflow.mType != OverflowStorageType::None) {
7808 return InkOverflowFromDeltas();
7811 return GetRectRelativeToSelf();
7814 OverflowAreas nsIFrame::GetOverflowAreas() const {
7815 if (mOverflow.mType == OverflowStorageType::Large) {
7816 // there is an overflow rect, and it's not stored as deltas but as
7817 // a separately-allocated rect
7818 return *GetOverflowAreasProperty();
7821 return OverflowAreas(InkOverflowFromDeltas(),
7822 nsRect(nsPoint(0, 0), GetSize()));
7825 OverflowAreas nsIFrame::GetOverflowAreasRelativeToSelf() const {
7826 if (IsTransformed()) {
7827 if (OverflowAreas* preTransformOverflows =
7828 GetProperty(PreTransformOverflowAreasProperty())) {
7829 return *preTransformOverflows;
7832 return GetOverflowAreas();
7835 OverflowAreas nsIFrame::GetOverflowAreasRelativeToParent() const {
7836 return GetOverflowAreas() + GetPosition();
7839 OverflowAreas nsIFrame::GetActualAndNormalOverflowAreasRelativeToParent()
7840 const {
7841 if (MOZ_LIKELY(!IsRelativelyOrStickyPositioned())) {
7842 return GetOverflowAreasRelativeToParent();
7845 const OverflowAreas overflows = GetOverflowAreas();
7846 OverflowAreas actualAndNormalOverflows = overflows + GetPosition();
7847 actualAndNormalOverflows.UnionWith(overflows + GetNormalPosition());
7848 return actualAndNormalOverflows;
7851 nsRect nsIFrame::ScrollableOverflowRectRelativeToParent() const {
7852 return ScrollableOverflowRect() + GetPosition();
7855 nsRect nsIFrame::InkOverflowRectRelativeToParent() const {
7856 return InkOverflowRect() + GetPosition();
7859 nsRect nsIFrame::ScrollableOverflowRectRelativeToSelf() const {
7860 if (IsTransformed()) {
7861 if (OverflowAreas* preTransformOverflows =
7862 GetProperty(PreTransformOverflowAreasProperty())) {
7863 return preTransformOverflows->ScrollableOverflow();
7866 return ScrollableOverflowRect();
7869 nsRect nsIFrame::InkOverflowRectRelativeToSelf() const {
7870 if (IsTransformed()) {
7871 if (OverflowAreas* preTransformOverflows =
7872 GetProperty(PreTransformOverflowAreasProperty())) {
7873 return preTransformOverflows->InkOverflow();
7876 return InkOverflowRect();
7879 nsRect nsIFrame::PreEffectsInkOverflowRect() const {
7880 nsRect* r = GetProperty(nsIFrame::PreEffectsBBoxProperty());
7881 return r ? *r : InkOverflowRectRelativeToSelf();
7884 bool nsIFrame::UpdateOverflow() {
7885 MOZ_ASSERT(FrameMaintainsOverflow(),
7886 "Non-display SVG do not maintain ink overflow rects");
7888 nsRect rect(nsPoint(0, 0), GetSize());
7889 OverflowAreas overflowAreas(rect, rect);
7891 if (!ComputeCustomOverflow(overflowAreas)) {
7892 // If updating overflow wasn't supported by this frame, then it should
7893 // have scheduled any necessary reflows. We can return false to say nothing
7894 // changed, and wait for reflow to correct it.
7895 return false;
7898 UnionChildOverflow(overflowAreas);
7900 if (FinishAndStoreOverflow(overflowAreas, GetSize())) {
7901 if (nsView* view = GetView()) {
7902 // Make sure the frame's view is properly sized.
7903 nsViewManager* vm = view->GetViewManager();
7904 vm->ResizeView(view, overflowAreas.InkOverflow(), true);
7907 return true;
7910 // Frames that combine their 3d transform with their ancestors
7911 // only compute a pre-transform overflow rect, and then contribute
7912 // to the normal overflow rect of the preserve-3d root. Always return
7913 // true here so that we propagate changes up to the root for final
7914 // calculation.
7915 return Combines3DTransformWithAncestors();
7918 /* virtual */
7919 bool nsIFrame::ComputeCustomOverflow(OverflowAreas& aOverflowAreas) {
7920 return true;
7923 bool nsIFrame::DoesClipChildrenInBothAxes() const {
7924 nsIScrollableFrame* sf = do_QueryFrame(this);
7925 const nsStyleDisplay* display = StyleDisplay();
7926 return sf || (display->mOverflowX == StyleOverflow::Clip &&
7927 display->mOverflowY == StyleOverflow::Clip);
7930 /* virtual */
7931 void nsIFrame::UnionChildOverflow(OverflowAreas& aOverflowAreas) {
7932 if (!DoesClipChildrenInBothAxes()) {
7933 nsLayoutUtils::UnionChildOverflow(this, aOverflowAreas);
7937 // Return true if this form control element's preferred size property (but not
7938 // percentage max size property) contains a percentage value that should be
7939 // resolved against zero when calculating its min-content contribution in the
7940 // corresponding axis.
7942 // For proper replaced elements, the percentage value in both their max size
7943 // property or preferred size property should be resolved against zero. This is
7944 // handled in IsPercentageResolvedAgainstZero().
7945 inline static bool FormControlShrinksForPercentSize(const nsIFrame* aFrame) {
7946 if (!aFrame->IsReplaced()) {
7947 // Quick test to reject most frames.
7948 return false;
7951 LayoutFrameType fType = aFrame->Type();
7952 if (fType == LayoutFrameType::Meter || fType == LayoutFrameType::Progress ||
7953 fType == LayoutFrameType::Range) {
7954 // progress, meter and range do have this shrinking behavior
7955 // FIXME: Maybe these should be nsIFormControlFrame?
7956 return true;
7959 if (!static_cast<nsIFormControlFrame*>(do_QueryFrame(aFrame))) {
7960 // Not a form control. This includes fieldsets, which do not
7961 // shrink.
7962 return false;
7965 if (fType == LayoutFrameType::GfxButtonControl ||
7966 fType == LayoutFrameType::HTMLButtonControl) {
7967 // Buttons don't have this shrinking behavior. (Note that color
7968 // inputs do, even though they inherit from button, so we can't use
7969 // do_QueryFrame here.)
7970 return false;
7973 return true;
7976 bool nsIFrame::IsPercentageResolvedAgainstZero(
7977 const StyleSize& aStyleSize, const StyleMaxSize& aStyleMaxSize) const {
7978 const bool sizeHasPercent = aStyleSize.HasPercent();
7979 return ((sizeHasPercent || aStyleMaxSize.HasPercent()) &&
7980 HasReplacedSizing()) ||
7981 (sizeHasPercent && FormControlShrinksForPercentSize(this));
7984 // Summary of the Cyclic-Percentage Intrinsic Size Contribution Rules:
7986 // Element Type | Replaced | Non-replaced
7987 // Contribution Type | min-content max-content | min-content max-content
7988 // ---------------------------------------------------------------------------
7989 // min size | zero zero | zero zero
7990 // max & preferred size | zero initial | initial initial
7992 // https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
7993 bool nsIFrame::IsPercentageResolvedAgainstZero(const LengthPercentage& aSize,
7994 SizeProperty aProperty) const {
7995 // Early return to avoid calling the virtual function, IsFrameOfType().
7996 if (aProperty == SizeProperty::MinSize) {
7997 return true;
8000 const bool hasPercentOnReplaced = aSize.HasPercent() && HasReplacedSizing();
8001 if (aProperty == SizeProperty::MaxSize) {
8002 return hasPercentOnReplaced;
8005 MOZ_ASSERT(aProperty == SizeProperty::Size);
8006 return hasPercentOnReplaced ||
8007 (aSize.HasPercent() && FormControlShrinksForPercentSize(this));
8010 bool nsIFrame::IsBlockWrapper() const {
8011 auto pseudoType = Style()->GetPseudoType();
8012 return pseudoType == PseudoStyleType::mozBlockInsideInlineWrapper ||
8013 pseudoType == PseudoStyleType::buttonContent ||
8014 pseudoType == PseudoStyleType::cellContent ||
8015 pseudoType == PseudoStyleType::columnSpanWrapper;
8018 bool nsIFrame::IsBlockFrameOrSubclass() const {
8019 const nsBlockFrame* thisAsBlock = do_QueryFrame(this);
8020 return !!thisAsBlock;
8023 bool nsIFrame::IsImageFrameOrSubclass() const {
8024 const nsImageFrame* asImage = do_QueryFrame(this);
8025 return !!asImage;
8028 bool nsIFrame::IsSubgrid() const {
8029 return IsGridContainerFrame() &&
8030 static_cast<const nsGridContainerFrame*>(this)->IsSubgrid();
8033 static nsIFrame* GetNearestBlockContainer(nsIFrame* frame) {
8034 while (!frame->IsBlockContainer()) {
8035 frame = frame->GetParent();
8036 NS_ASSERTION(
8037 frame,
8038 "How come we got to the root frame without seeing a containing block?");
8040 return frame;
8043 bool nsIFrame::IsBlockContainer() const {
8044 // The block wrappers we use to wrap blocks inside inlines aren't
8045 // described in the CSS spec. We need to make them not be containing
8046 // blocks.
8047 // Since the parent of such a block is either a normal block or
8048 // another such pseudo, this shouldn't cause anything bad to happen.
8049 // Also the anonymous blocks inside table cells are not containing blocks.
8051 // If we ever start skipping table row groups from being containing blocks,
8052 // you need to remove the StickyScrollContainer hack referencing bug 1421660.
8053 return !IsLineParticipant() && !IsBlockWrapper() && !IsSubgrid() &&
8054 // Table rows are not containing blocks either
8055 !IsTableRowFrame();
8058 nsIFrame* nsIFrame::GetContainingBlock(
8059 uint32_t aFlags, const nsStyleDisplay* aStyleDisplay) const {
8060 MOZ_ASSERT(aStyleDisplay == StyleDisplay());
8062 // Keep this in sync with MightBeContainingBlockFor in ReflowInput.cpp.
8064 if (!GetParent()) {
8065 return nullptr;
8067 // MathML frames might have absolute positioning style, but they would
8068 // still be in-flow. So we have to check to make sure that the frame
8069 // is really out-of-flow too.
8070 nsIFrame* f;
8071 if (IsAbsolutelyPositioned(aStyleDisplay)) {
8072 f = GetParent(); // the parent is always the containing block
8073 } else {
8074 f = GetNearestBlockContainer(GetParent());
8077 if (aFlags & SKIP_SCROLLED_FRAME && f &&
8078 f->Style()->GetPseudoType() == PseudoStyleType::scrolledContent) {
8079 f = f->GetParent();
8081 return f;
8084 #ifdef DEBUG_FRAME_DUMP
8086 Maybe<uint32_t> nsIFrame::ContentIndexInContainer(const nsIFrame* aFrame) {
8087 if (nsIContent* content = aFrame->GetContent()) {
8088 return content->ComputeIndexInParentContent();
8090 return Nothing();
8093 nsAutoCString nsIFrame::ListTag() const {
8094 nsAutoString tmp;
8095 GetFrameName(tmp);
8097 nsAutoCString tag;
8098 tag += NS_ConvertUTF16toUTF8(tmp);
8099 tag += nsPrintfCString("@%p", static_cast<const void*>(this));
8100 return tag;
8103 std::string nsIFrame::ConvertToString(const LogicalRect& aRect,
8104 const WritingMode aWM, ListFlags aFlags) {
8105 if (aFlags.contains(ListFlag::DisplayInCSSPixels)) {
8106 // Abuse CSSRect to store all LogicalRect's dimensions in CSS pixels.
8107 return ToString(mozilla::CSSRect(CSSPixel::FromAppUnits(aRect.IStart(aWM)),
8108 CSSPixel::FromAppUnits(aRect.BStart(aWM)),
8109 CSSPixel::FromAppUnits(aRect.ISize(aWM)),
8110 CSSPixel::FromAppUnits(aRect.BSize(aWM))));
8112 return ToString(aRect);
8115 std::string nsIFrame::ConvertToString(const LogicalSize& aSize,
8116 const WritingMode aWM, ListFlags aFlags) {
8117 if (aFlags.contains(ListFlag::DisplayInCSSPixels)) {
8118 // Abuse CSSSize to store all LogicalSize's dimensions in CSS pixels.
8119 return ToString(CSSSize(CSSPixel::FromAppUnits(aSize.ISize(aWM)),
8120 CSSPixel::FromAppUnits(aSize.BSize(aWM))));
8122 return ToString(aSize);
8125 // Debugging
8126 void nsIFrame::ListGeneric(nsACString& aTo, const char* aPrefix,
8127 ListFlags aFlags) const {
8128 aTo += aPrefix;
8129 aTo += ListTag();
8130 if (HasView()) {
8131 aTo += nsPrintfCString(" [view=%p]", static_cast<void*>(GetView()));
8133 if (GetParent()) {
8134 aTo += nsPrintfCString(" parent=%p", static_cast<void*>(GetParent()));
8136 if (GetNextSibling()) {
8137 aTo += nsPrintfCString(" next=%p", static_cast<void*>(GetNextSibling()));
8139 if (GetPrevContinuation()) {
8140 bool fluid = GetPrevInFlow() == GetPrevContinuation();
8141 aTo += nsPrintfCString(" prev-%s=%p", fluid ? "in-flow" : "continuation",
8142 static_cast<void*>(GetPrevContinuation()));
8144 if (GetNextContinuation()) {
8145 bool fluid = GetNextInFlow() == GetNextContinuation();
8146 aTo += nsPrintfCString(" next-%s=%p", fluid ? "in-flow" : "continuation",
8147 static_cast<void*>(GetNextContinuation()));
8149 if (const nsAtom* const autoPageValue =
8150 GetProperty(AutoPageValueProperty())) {
8151 aTo += " AutoPage=";
8152 aTo += nsAtomCString(autoPageValue);
8154 if (const nsIFrame::PageValues* const pageValues =
8155 GetProperty(PageValuesProperty())) {
8156 aTo += " PageValues={";
8157 if (pageValues->mStartPageValue) {
8158 aTo += nsAtomCString(pageValues->mStartPageValue);
8159 } else {
8160 aTo += "<null>";
8162 aTo += ", ";
8163 if (pageValues->mEndPageValue) {
8164 aTo += nsAtomCString(pageValues->mEndPageValue);
8165 } else {
8166 aTo += "<null>";
8168 aTo += "}";
8170 void* IBsibling = GetProperty(IBSplitSibling());
8171 if (IBsibling) {
8172 aTo += nsPrintfCString(" IBSplitSibling=%p", IBsibling);
8174 void* IBprevsibling = GetProperty(IBSplitPrevSibling());
8175 if (IBprevsibling) {
8176 aTo += nsPrintfCString(" IBSplitPrevSibling=%p", IBprevsibling);
8178 if (nsLayoutUtils::FontSizeInflationEnabled(PresContext())) {
8179 if (HasAnyStateBits(NS_FRAME_FONT_INFLATION_FLOW_ROOT)) {
8180 aTo += nsPrintfCString(" FFR");
8181 if (nsFontInflationData* data =
8182 nsFontInflationData::FindFontInflationDataFor(this)) {
8183 aTo += nsPrintfCString(
8184 ",enabled=%s,UIS=%s", data->InflationEnabled() ? "yes" : "no",
8185 ConvertToString(data->UsableISize(), aFlags).c_str());
8188 if (HasAnyStateBits(NS_FRAME_FONT_INFLATION_CONTAINER)) {
8189 aTo += nsPrintfCString(" FIC");
8191 aTo += nsPrintfCString(" FI=%f", nsLayoutUtils::FontSizeInflationFor(this));
8193 aTo += nsPrintfCString(" %s", ConvertToString(mRect, aFlags).c_str());
8195 mozilla::WritingMode wm = GetWritingMode();
8196 if (wm.IsVertical() || wm.IsBidiRTL()) {
8197 aTo +=
8198 nsPrintfCString(" wm=%s logical-size=(%s)", ToString(wm).c_str(),
8199 ConvertToString(GetLogicalSize(), wm, aFlags).c_str());
8202 nsIFrame* parent = GetParent();
8203 if (parent) {
8204 WritingMode pWM = parent->GetWritingMode();
8205 if (pWM.IsVertical() || pWM.IsBidiRTL()) {
8206 nsSize containerSize = parent->mRect.Size();
8207 LogicalRect lr(pWM, mRect, containerSize);
8208 aTo += nsPrintfCString(" parent-wm=%s cs=(%s) logical-rect=%s",
8209 ToString(pWM).c_str(),
8210 ConvertToString(containerSize, aFlags).c_str(),
8211 ConvertToString(lr, pWM, aFlags).c_str());
8214 nsIFrame* f = const_cast<nsIFrame*>(this);
8215 if (f->HasOverflowAreas()) {
8216 nsRect io = f->InkOverflowRect();
8217 if (!io.IsEqualEdges(mRect)) {
8218 aTo += nsPrintfCString(" ink-overflow=%s",
8219 ConvertToString(io, aFlags).c_str());
8221 nsRect so = f->ScrollableOverflowRect();
8222 if (!so.IsEqualEdges(mRect)) {
8223 aTo += nsPrintfCString(" scr-overflow=%s",
8224 ConvertToString(so, aFlags).c_str());
8227 if (OverflowAreas* preTransformOverflows =
8228 f->GetProperty(PreTransformOverflowAreasProperty())) {
8229 nsRect io = preTransformOverflows->InkOverflow();
8230 if (!io.IsEqualEdges(mRect) &&
8231 (!f->HasOverflowAreas() || !io.IsEqualEdges(f->InkOverflowRect()))) {
8232 aTo += nsPrintfCString(" pre-transform-ink-overflow=%s",
8233 ConvertToString(io, aFlags).c_str());
8235 nsRect so = preTransformOverflows->ScrollableOverflow();
8236 if (!so.IsEqualEdges(mRect) &&
8237 (!f->HasOverflowAreas() ||
8238 !so.IsEqualEdges(f->ScrollableOverflowRect()))) {
8239 aTo += nsPrintfCString(" pre-transform-scr-overflow=%s",
8240 ConvertToString(so, aFlags).c_str());
8243 bool hasNormalPosition;
8244 nsPoint normalPosition = GetNormalPosition(&hasNormalPosition);
8245 if (hasNormalPosition) {
8246 aTo += nsPrintfCString(" normal-position=%s",
8247 ConvertToString(normalPosition, aFlags).c_str());
8249 if (HasProperty(BidiDataProperty())) {
8250 FrameBidiData bidi = GetBidiData();
8251 aTo += nsPrintfCString(" bidi(%d,%d,%d)", bidi.baseLevel.Value(),
8252 bidi.embeddingLevel.Value(),
8253 bidi.precedingControl.Value());
8255 if (IsTransformed()) {
8256 aTo += nsPrintfCString(" transformed");
8258 if (ChildrenHavePerspective()) {
8259 aTo += nsPrintfCString(" perspective");
8261 if (Extend3DContext()) {
8262 aTo += nsPrintfCString(" extend-3d");
8264 if (Combines3DTransformWithAncestors()) {
8265 aTo += nsPrintfCString(" combines-3d-transform-with-ancestors");
8267 if (mContent) {
8268 aTo += nsPrintfCString(" [content=%p]", static_cast<void*>(mContent));
8270 aTo += nsPrintfCString(" [cs=%p", static_cast<void*>(mComputedStyle));
8271 if (mComputedStyle) {
8272 auto pseudoType = mComputedStyle->GetPseudoType();
8273 aTo += ToString(pseudoType).c_str();
8275 aTo += "]";
8277 auto contentVisibility = StyleDisplay()->ContentVisibility(*this);
8278 if (contentVisibility != StyleContentVisibility::Visible) {
8279 aTo += nsPrintfCString(" [content-visibility=");
8280 if (contentVisibility == StyleContentVisibility::Auto) {
8281 aTo += "auto, "_ns;
8282 } else if (contentVisibility == StyleContentVisibility::Hidden) {
8283 aTo += "hiden, "_ns;
8286 if (HidesContent()) {
8287 aTo += "HidesContent=hidden"_ns;
8288 } else {
8289 aTo += "HidesContent=visibile"_ns;
8291 aTo += "]";
8294 if (IsFrameModified()) {
8295 aTo += nsPrintfCString(" modified");
8298 if (HasModifiedDescendants()) {
8299 aTo += nsPrintfCString(" has-modified-descendants");
8303 void nsIFrame::List(FILE* out, const char* aPrefix, ListFlags aFlags) const {
8304 nsCString str;
8305 ListGeneric(str, aPrefix, aFlags);
8306 fprintf_stderr(out, "%s\n", str.get());
8309 void nsIFrame::ListTextRuns(FILE* out) const {
8310 nsTHashSet<const void*> seen;
8311 ListTextRuns(out, seen);
8314 void nsIFrame::ListTextRuns(FILE* out, nsTHashSet<const void*>& aSeen) const {
8315 for (const auto& childList : ChildLists()) {
8316 for (const nsIFrame* kid : childList.mList) {
8317 kid->ListTextRuns(out, aSeen);
8322 void nsIFrame::ListMatchedRules(FILE* out, const char* aPrefix) const {
8323 nsTArray<const StyleLockedStyleRule*> rawRuleList;
8324 Servo_ComputedValues_GetStyleRuleList(mComputedStyle, &rawRuleList);
8325 for (const StyleLockedStyleRule* rawRule : rawRuleList) {
8326 nsAutoCString ruleText;
8327 Servo_StyleRule_GetCssText(rawRule, &ruleText);
8328 fprintf_stderr(out, "%s%s\n", aPrefix, ruleText.get());
8332 void nsIFrame::ListWithMatchedRules(FILE* out, const char* aPrefix) const {
8333 fprintf_stderr(out, "%s%s\n", aPrefix, ListTag().get());
8335 nsCString rulePrefix;
8336 rulePrefix += aPrefix;
8337 rulePrefix += " ";
8338 ListMatchedRules(out, rulePrefix.get());
8341 nsresult nsIFrame::GetFrameName(nsAString& aResult) const {
8342 return MakeFrameName(u"Frame"_ns, aResult);
8345 nsresult nsIFrame::MakeFrameName(const nsAString& aType,
8346 nsAString& aResult) const {
8347 aResult = aType;
8348 if (mContent && !mContent->IsText()) {
8349 nsAutoString buf;
8350 mContent->NodeInfo()->NameAtom()->ToString(buf);
8351 if (nsAtom* id = mContent->GetID()) {
8352 buf.AppendLiteral(" id=");
8353 buf.Append(nsDependentAtomString(id));
8355 if (IsSubDocumentFrame()) {
8356 nsAutoString src;
8357 mContent->AsElement()->GetAttr(nsGkAtoms::src, src);
8358 buf.AppendLiteral(" src=");
8359 buf.Append(src);
8361 aResult.Append('(');
8362 aResult.Append(buf);
8363 aResult.Append(')');
8365 aResult.Append('(');
8366 Maybe<uint32_t> index = ContentIndexInContainer(this);
8367 if (index.isSome()) {
8368 aResult.AppendInt(*index);
8369 } else {
8370 aResult.AppendInt(-1);
8372 aResult.Append(')');
8373 return NS_OK;
8376 void nsIFrame::DumpFrameTree() const {
8377 PresShell()->GetRootFrame()->List(stderr);
8380 void nsIFrame::DumpFrameTreeInCSSPixels() const {
8381 PresShell()->GetRootFrame()->List(stderr, "", ListFlag::DisplayInCSSPixels);
8384 void nsIFrame::DumpFrameTreeLimited() const { List(stderr); }
8385 void nsIFrame::DumpFrameTreeLimitedInCSSPixels() const {
8386 List(stderr, "", ListFlag::DisplayInCSSPixels);
8389 #endif
8391 bool nsIFrame::IsVisibleForPainting() const {
8392 return StyleVisibility()->IsVisible();
8395 bool nsIFrame::IsVisibleOrCollapsedForPainting() const {
8396 return StyleVisibility()->IsVisibleOrCollapsed();
8399 /* virtual */
8400 bool nsIFrame::IsEmpty() {
8401 return IsHiddenByContentVisibilityOfInFlowParentForLayout();
8404 bool nsIFrame::CachedIsEmpty() {
8405 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_DIRTY) ||
8406 IsHiddenByContentVisibilityOfInFlowParentForLayout(),
8407 "Must only be called on reflowed lines or those hidden by "
8408 "content-visibility.");
8409 return IsEmpty();
8412 /* virtual */
8413 bool nsIFrame::IsSelfEmpty() {
8414 return IsHiddenByContentVisibilityOfInFlowParentForLayout();
8417 nsresult nsIFrame::GetSelectionController(nsPresContext* aPresContext,
8418 nsISelectionController** aSelCon) {
8419 if (!aPresContext || !aSelCon) return NS_ERROR_INVALID_ARG;
8421 nsIFrame* frame = this;
8422 while (frame && frame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION)) {
8423 nsITextControlFrame* tcf = do_QueryFrame(frame);
8424 if (tcf) {
8425 return tcf->GetOwnedSelectionController(aSelCon);
8427 frame = frame->GetParent();
8430 *aSelCon = do_AddRef(aPresContext->PresShell()).take();
8431 return NS_OK;
8434 already_AddRefed<nsFrameSelection> nsIFrame::GetFrameSelection() {
8435 RefPtr<nsFrameSelection> fs =
8436 const_cast<nsFrameSelection*>(GetConstFrameSelection());
8437 return fs.forget();
8440 const nsFrameSelection* nsIFrame::GetConstFrameSelection() const {
8441 nsIFrame* frame = const_cast<nsIFrame*>(this);
8442 while (frame && frame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION)) {
8443 nsITextControlFrame* tcf = do_QueryFrame(frame);
8444 if (tcf) {
8445 return tcf->GetOwnedFrameSelection();
8447 frame = frame->GetParent();
8450 return PresShell()->ConstFrameSelection();
8453 bool nsIFrame::IsFrameSelected() const {
8454 NS_ASSERTION(!GetContent() || GetContent()->IsMaybeSelected(),
8455 "use the public IsSelected() instead");
8456 return GetContent()->IsSelected(0, GetContent()->GetChildCount());
8459 nsresult nsIFrame::GetPointFromOffset(int32_t inOffset, nsPoint* outPoint) {
8460 MOZ_ASSERT(outPoint != nullptr, "Null parameter");
8461 nsRect contentRect = GetContentRectRelativeToSelf();
8462 nsPoint pt = contentRect.TopLeft();
8463 if (mContent) {
8464 nsIContent* newContent = mContent->GetParent();
8465 if (newContent) {
8466 const int32_t newOffset = newContent->ComputeIndexOf_Deprecated(mContent);
8468 // Find the direction of the frame from the EmbeddingLevelProperty,
8469 // which is the resolved bidi level set in
8470 // nsBidiPresUtils::ResolveParagraph (odd levels = right-to-left).
8471 // If the embedding level isn't set, just use the CSS direction
8472 // property.
8473 bool hasBidiData;
8474 FrameBidiData bidiData = GetProperty(BidiDataProperty(), &hasBidiData);
8475 bool isRTL = hasBidiData
8476 ? bidiData.embeddingLevel.IsRTL()
8477 : StyleVisibility()->mDirection == StyleDirection::Rtl;
8478 if ((!isRTL && inOffset > newOffset) ||
8479 (isRTL && inOffset <= newOffset)) {
8480 pt = contentRect.TopRight();
8484 *outPoint = pt;
8485 return NS_OK;
8488 nsresult nsIFrame::GetCharacterRectsInRange(int32_t aInOffset, int32_t aLength,
8489 nsTArray<nsRect>& aOutRect) {
8490 /* no text */
8491 return NS_ERROR_FAILURE;
8494 nsresult nsIFrame::GetChildFrameContainingOffset(int32_t inContentOffset,
8495 bool inHint,
8496 int32_t* outFrameContentOffset,
8497 nsIFrame** outChildFrame) {
8498 MOZ_ASSERT(outChildFrame && outFrameContentOffset, "Null parameter");
8499 *outFrameContentOffset = (int32_t)inHint;
8500 // the best frame to reflect any given offset would be a visible frame if
8501 // possible i.e. we are looking for a valid frame to place the blinking caret
8502 nsRect rect = GetRect();
8503 if (!rect.width || !rect.height) {
8504 // if we have a 0 width or height then lets look for another frame that
8505 // possibly has the same content. If we have no frames in flow then just
8506 // let us return 'this' frame
8507 nsIFrame* nextFlow = GetNextInFlow();
8508 if (nextFlow)
8509 return nextFlow->GetChildFrameContainingOffset(
8510 inContentOffset, inHint, outFrameContentOffset, outChildFrame);
8512 *outChildFrame = this;
8513 return NS_OK;
8517 // What I've pieced together about this routine:
8518 // Starting with a block frame (from which a line frame can be gotten)
8519 // and a line number, drill down and get the first/last selectable
8520 // frame on that line, depending on aPos->mDirection.
8521 // aOutSideLimit != 0 means ignore aLineStart, instead work from
8522 // the end (if > 0) or beginning (if < 0).
8524 static nsresult GetNextPrevLineFromBlockFrame(PeekOffsetStruct* aPos,
8525 nsIFrame* aBlockFrame,
8526 int32_t aLineStart,
8527 int8_t aOutSideLimit) {
8528 MOZ_ASSERT(aPos);
8529 MOZ_ASSERT(aBlockFrame);
8531 nsPresContext* pc = aBlockFrame->PresContext();
8533 // magic numbers: aLineStart will be -1 for end of block, 0 will be start of
8534 // block.
8536 aPos->mResultFrame = nullptr;
8537 aPos->mResultContent = nullptr;
8538 aPos->mAttach = aPos->mDirection == eDirNext ? CARET_ASSOCIATE_AFTER
8539 : CARET_ASSOCIATE_BEFORE;
8541 AutoAssertNoDomMutations guard;
8542 nsILineIterator* it = aBlockFrame->GetLineIterator();
8543 if (!it) {
8544 return NS_ERROR_FAILURE;
8546 int32_t searchingLine = aLineStart;
8547 int32_t countLines = it->GetNumLines();
8548 if (aOutSideLimit > 0) { // start at end
8549 searchingLine = countLines;
8550 } else if (aOutSideLimit < 0) { // start at beginning
8551 searchingLine = -1; //"next" will be 0
8552 } else if ((aPos->mDirection == eDirPrevious && searchingLine == 0) ||
8553 (aPos->mDirection == eDirNext &&
8554 searchingLine >= (countLines - 1))) {
8555 // Not found.
8556 return NS_ERROR_FAILURE;
8558 nsIFrame* resultFrame = nullptr;
8559 nsIFrame* farStoppingFrame = nullptr; // we keep searching until we find a
8560 // "this" frame then we go to next line
8561 nsIFrame* nearStoppingFrame = nullptr; // if we are backing up from edge,
8562 // stop here
8563 nsIFrame* firstFrame;
8564 nsIFrame* lastFrame;
8565 bool isBeforeFirstFrame, isAfterLastFrame;
8566 bool found = false;
8568 nsresult result = NS_OK;
8569 while (!found) {
8570 if (aPos->mDirection == eDirPrevious)
8571 searchingLine--;
8572 else
8573 searchingLine++;
8574 if ((aPos->mDirection == eDirPrevious && searchingLine < 0) ||
8575 (aPos->mDirection == eDirNext && searchingLine >= countLines)) {
8576 // we need to jump to new block frame.
8577 return NS_ERROR_FAILURE;
8579 auto line = it->GetLine(searchingLine).unwrap();
8580 if (!line.mNumFramesOnLine) {
8581 continue;
8583 lastFrame = firstFrame = line.mFirstFrameOnLine;
8584 for (int32_t lineFrameCount = line.mNumFramesOnLine; lineFrameCount > 1;
8585 lineFrameCount--) {
8586 lastFrame = lastFrame->GetNextSibling();
8587 if (!lastFrame) {
8588 NS_ERROR("GetLine promised more frames than could be found");
8589 return NS_ERROR_FAILURE;
8592 nsIFrame::GetLastLeaf(&lastFrame);
8594 if (aPos->mDirection == eDirNext) {
8595 nearStoppingFrame = firstFrame;
8596 farStoppingFrame = lastFrame;
8597 } else {
8598 nearStoppingFrame = lastFrame;
8599 farStoppingFrame = firstFrame;
8601 nsPoint offset;
8602 nsView* view; // used for call of get offset from view
8603 aBlockFrame->GetOffsetFromView(offset, &view);
8604 nsPoint newDesiredPos =
8605 aPos->mDesiredCaretPos -
8606 offset; // get desired position into blockframe coords
8607 result = it->FindFrameAt(searchingLine, newDesiredPos, &resultFrame,
8608 &isBeforeFirstFrame, &isAfterLastFrame);
8609 if (NS_FAILED(result)) {
8610 continue;
8613 if (resultFrame) {
8614 // check to see if this is ANOTHER blockframe inside the other one if so
8615 // then call into its lines
8616 if (resultFrame->CanProvideLineIterator()) {
8617 aPos->mResultFrame = resultFrame;
8618 return NS_OK;
8620 // resultFrame is not a block frame
8621 result = NS_ERROR_FAILURE;
8623 nsCOMPtr<nsIFrameEnumerator> frameTraversal;
8624 result = NS_NewFrameTraversal(
8625 getter_AddRefs(frameTraversal), pc, resultFrame, ePostOrder,
8626 false, // aVisual
8627 aPos->mOptions.contains(PeekOffsetOption::StopAtScroller),
8628 false, // aFollowOOFs
8629 false // aSkipPopupChecks
8631 if (NS_FAILED(result)) {
8632 return result;
8635 auto FoundValidFrame = [aPos](const nsIFrame::ContentOffsets& aOffsets,
8636 const nsIFrame* aFrame) {
8637 if (!aOffsets.content) {
8638 return false;
8640 if (!aFrame->IsSelectable(nullptr)) {
8641 return false;
8643 if (aPos->mOptions.contains(PeekOffsetOption::ForceEditableRegion) &&
8644 !aOffsets.content->IsEditable()) {
8645 return false;
8647 return true;
8650 nsIFrame* storeOldResultFrame = resultFrame;
8651 while (!found) {
8652 nsPoint point;
8653 nsRect tempRect = resultFrame->GetRect();
8654 nsPoint offset;
8655 nsView* view; // used for call of get offset from view
8656 resultFrame->GetOffsetFromView(offset, &view);
8657 if (!view) {
8658 return NS_ERROR_FAILURE;
8660 if (resultFrame->GetWritingMode().IsVertical()) {
8661 point.y = aPos->mDesiredCaretPos.y;
8662 point.x = tempRect.width + offset.x;
8663 } else {
8664 point.y = tempRect.height + offset.y;
8665 point.x = aPos->mDesiredCaretPos.x;
8668 if (!resultFrame->HasView()) {
8669 nsView* view;
8670 nsPoint offset;
8671 resultFrame->GetOffsetFromView(offset, &view);
8672 nsIFrame::ContentOffsets offsets =
8673 resultFrame->GetContentOffsetsFromPoint(point - offset);
8674 aPos->mResultContent = offsets.content;
8675 aPos->mContentOffset = offsets.offset;
8676 aPos->mAttach = offsets.associate;
8677 if (FoundValidFrame(offsets, resultFrame)) {
8678 found = true;
8679 break;
8683 if (aPos->mDirection == eDirPrevious &&
8684 resultFrame == farStoppingFrame) {
8685 break;
8687 if (aPos->mDirection == eDirNext && resultFrame == nearStoppingFrame) {
8688 break;
8690 // always try previous on THAT line if that fails go the other way
8691 resultFrame = frameTraversal->Traverse(/* aForward = */ false);
8692 if (!resultFrame) {
8693 return NS_ERROR_FAILURE;
8697 if (!found) {
8698 resultFrame = storeOldResultFrame;
8700 result = NS_NewFrameTraversal(
8701 getter_AddRefs(frameTraversal), pc, resultFrame, eLeaf,
8702 false, // aVisual
8703 aPos->mOptions.contains(PeekOffsetOption::StopAtScroller),
8704 false, // aFollowOOFs
8705 false // aSkipPopupChecks
8708 while (!found) {
8709 nsPoint point = aPos->mDesiredCaretPos;
8710 nsView* view;
8711 nsPoint offset;
8712 resultFrame->GetOffsetFromView(offset, &view);
8713 nsIFrame::ContentOffsets offsets =
8714 resultFrame->GetContentOffsetsFromPoint(point - offset);
8715 aPos->mResultContent = offsets.content;
8716 aPos->mContentOffset = offsets.offset;
8717 aPos->mAttach = offsets.associate;
8718 if (FoundValidFrame(offsets, resultFrame)) {
8719 found = true;
8720 if (resultFrame == farStoppingFrame)
8721 aPos->mAttach = CARET_ASSOCIATE_BEFORE;
8722 else
8723 aPos->mAttach = CARET_ASSOCIATE_AFTER;
8724 break;
8726 if (aPos->mDirection == eDirPrevious &&
8727 (resultFrame == nearStoppingFrame))
8728 break;
8729 if (aPos->mDirection == eDirNext && (resultFrame == farStoppingFrame))
8730 break;
8731 // previous didnt work now we try "next"
8732 nsIFrame* tempFrame = frameTraversal->Traverse(/* aForward = */ true);
8733 if (!tempFrame) break;
8734 resultFrame = tempFrame;
8736 aPos->mResultFrame = resultFrame;
8737 } else {
8738 // we need to jump to new block frame.
8739 aPos->mAmount = eSelectLine;
8740 aPos->mStartOffset = 0;
8741 aPos->mAttach = aPos->mDirection == eDirNext ? CARET_ASSOCIATE_BEFORE
8742 : CARET_ASSOCIATE_AFTER;
8743 if (aPos->mDirection == eDirPrevious)
8744 aPos->mStartOffset = -1; // start from end
8745 return aBlockFrame->PeekOffset(aPos);
8748 return NS_OK;
8751 nsIFrame::CaretPosition nsIFrame::GetExtremeCaretPosition(bool aStart) {
8752 CaretPosition result;
8754 FrameTarget targetFrame = DrillDownToSelectionFrame(this, !aStart, 0);
8755 FrameContentRange range = GetRangeForFrame(targetFrame.frame);
8756 result.mResultContent = range.content;
8757 result.mContentOffset = aStart ? range.start : range.end;
8758 return result;
8761 // If this is a preformatted text frame, see if it ends with a newline
8762 static nsContentAndOffset FindLineBreakInText(nsIFrame* aFrame,
8763 nsDirection aDirection) {
8764 nsContentAndOffset result;
8766 if (aFrame->IsGeneratedContentFrame() ||
8767 !aFrame->HasSignificantTerminalNewline()) {
8768 return result;
8771 int32_t endOffset = aFrame->GetOffsets().second;
8772 result.mContent = aFrame->GetContent();
8773 result.mOffset = endOffset - (aDirection == eDirPrevious ? 0 : 1);
8774 return result;
8777 // Find the first (or last) descendant of the given frame
8778 // which is either a block-level frame or a BRFrame, or some other kind of break
8779 // which stops the line.
8780 static nsContentAndOffset FindLineBreakingFrame(nsIFrame* aFrame,
8781 nsDirection aDirection) {
8782 nsContentAndOffset result;
8784 if (aFrame->IsGeneratedContentFrame()) {
8785 return result;
8788 // Treat form controls as inline leaves
8789 // XXX we really need a way to determine whether a frame is inline-level
8790 if (static_cast<nsIFormControlFrame*>(do_QueryFrame(aFrame))) {
8791 return result;
8794 // Check the frame itself
8795 // Fall through block-in-inline split frames because their mContent is
8796 // the content of the inline frames they were created from. The
8797 // first/last child of such frames is the real block frame we're
8798 // looking for.
8799 if ((aFrame->IsBlockOutside() &&
8800 !aFrame->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) ||
8801 aFrame->IsBrFrame()) {
8802 nsIContent* content = aFrame->GetContent();
8803 result.mContent = content->GetParent();
8804 // In some cases (bug 310589, bug 370174) we end up here with a null
8805 // content. This probably shouldn't ever happen, but since it sometimes
8806 // does, we want to avoid crashing here.
8807 NS_ASSERTION(result.mContent, "Unexpected orphan content");
8808 if (result.mContent) {
8809 result.mOffset = result.mContent->ComputeIndexOf_Deprecated(content) +
8810 (aDirection == eDirPrevious ? 1 : 0);
8812 return result;
8815 result = FindLineBreakInText(aFrame, aDirection);
8816 if (result.mContent) {
8817 return result;
8820 // Iterate over children and call ourselves recursively
8821 if (aDirection == eDirPrevious) {
8822 nsIFrame* child = aFrame->PrincipalChildList().LastChild();
8823 while (child && !result.mContent) {
8824 result = FindLineBreakingFrame(child, aDirection);
8825 child = child->GetPrevSibling();
8827 } else { // eDirNext
8828 nsIFrame* child = aFrame->PrincipalChildList().FirstChild();
8829 while (child && !result.mContent) {
8830 result = FindLineBreakingFrame(child, aDirection);
8831 child = child->GetNextSibling();
8834 return result;
8837 nsresult nsIFrame::PeekOffsetForParagraph(PeekOffsetStruct* aPos) {
8838 nsIFrame* frame = this;
8839 nsContentAndOffset blockFrameOrBR;
8840 blockFrameOrBR.mContent = nullptr;
8841 bool reachedLimit = frame->IsBlockOutside() || IsEditingHost(frame);
8843 auto traverse = [&aPos](nsIFrame* current) {
8844 return aPos->mDirection == eDirPrevious ? current->GetPrevSibling()
8845 : current->GetNextSibling();
8848 // Go through containing frames until reaching a block frame.
8849 // In each step, search the previous (or next) siblings for the closest
8850 // "stop frame" (a block frame or a BRFrame).
8851 // If found, set it to be the selection boundary and abort.
8852 while (!reachedLimit) {
8853 nsIFrame* parent = frame->GetParent();
8854 // Treat a frame associated with the root content as if it were a block
8855 // frame.
8856 if (!frame->mContent || !frame->mContent->GetParent()) {
8857 reachedLimit = true;
8858 break;
8861 if (aPos->mDirection == eDirNext) {
8862 // Try to find our own line-break before looking at our siblings.
8863 blockFrameOrBR = FindLineBreakInText(frame, eDirNext);
8866 nsIFrame* sibling = traverse(frame);
8867 while (sibling && !blockFrameOrBR.mContent) {
8868 blockFrameOrBR = FindLineBreakingFrame(sibling, aPos->mDirection);
8869 sibling = traverse(sibling);
8871 if (blockFrameOrBR.mContent) {
8872 aPos->mResultContent = blockFrameOrBR.mContent;
8873 aPos->mContentOffset = blockFrameOrBR.mOffset;
8874 break;
8876 frame = parent;
8877 reachedLimit = frame && (frame->IsBlockOutside() || IsEditingHost(frame));
8880 if (reachedLimit) { // no "stop frame" found
8881 aPos->mResultContent = frame->GetContent();
8882 if (aPos->mDirection == eDirPrevious) {
8883 aPos->mContentOffset = 0;
8884 } else if (aPos->mResultContent) {
8885 aPos->mContentOffset = aPos->mResultContent->GetChildCount();
8888 return NS_OK;
8891 // Determine movement direction relative to frame
8892 static bool IsMovingInFrameDirection(const nsIFrame* frame,
8893 nsDirection aDirection, bool aVisual) {
8894 bool isReverseDirection =
8895 aVisual && nsBidiPresUtils::IsReversedDirectionFrame(frame);
8896 return aDirection == (isReverseDirection ? eDirPrevious : eDirNext);
8899 // Determines "are we looking for a boundary between whitespace and
8900 // non-whitespace (in the direction we're moving in)". It is true when moving
8901 // forward and looking for a beginning of a word, or when moving backwards and
8902 // looking for an end of a word.
8903 static bool ShouldWordSelectionEatSpace(const PeekOffsetStruct& aPos) {
8904 if (aPos.mWordMovementType != eDefaultBehavior) {
8905 // aPos->mWordMovementType possible values:
8906 // eEndWord: eat the space if we're moving backwards
8907 // eStartWord: eat the space if we're moving forwards
8908 return (aPos.mWordMovementType == eEndWord) ==
8909 (aPos.mDirection == eDirPrevious);
8911 // Use the hidden preference which is based on operating system
8912 // behavior. This pref only affects whether moving forward by word
8913 // should go to the end of this word or start of the next word. When
8914 // going backwards, the start of the word is always used, on every
8915 // operating system.
8916 return aPos.mDirection == eDirNext &&
8917 StaticPrefs::layout_word_select_eat_space_to_next_word();
8920 enum class OffsetIsAtLineEdge : bool { No, Yes };
8922 static void SetPeekResultFromFrame(PeekOffsetStruct& aPos, nsIFrame* aFrame,
8923 int32_t aOffset,
8924 OffsetIsAtLineEdge aAtLineEdge) {
8925 FrameContentRange range = GetRangeForFrame(aFrame);
8926 aPos.mResultFrame = aFrame;
8927 aPos.mResultContent = range.content;
8928 // Output offset is relative to content, not frame
8929 aPos.mContentOffset =
8930 aOffset < 0 ? range.end + aOffset + 1 : range.start + aOffset;
8931 if (aAtLineEdge == OffsetIsAtLineEdge::Yes) {
8932 aPos.mAttach = aPos.mContentOffset == range.start ? CARET_ASSOCIATE_AFTER
8933 : CARET_ASSOCIATE_BEFORE;
8937 void nsIFrame::SelectablePeekReport::TransferTo(PeekOffsetStruct& aPos) const {
8938 return SetPeekResultFromFrame(aPos, mFrame, mOffset, OffsetIsAtLineEdge::No);
8941 nsIFrame::SelectablePeekReport::SelectablePeekReport(
8942 const mozilla::GenericErrorResult<nsresult>&& aErr) {
8943 MOZ_ASSERT(NS_FAILED(aErr.operator nsresult()));
8944 // Return an empty report
8947 nsresult nsIFrame::PeekOffsetForCharacter(PeekOffsetStruct* aPos,
8948 int32_t aOffset) {
8949 SelectablePeekReport current{this, aOffset};
8951 nsIFrame::FrameSearchResult peekSearchState = CONTINUE;
8953 while (peekSearchState != FOUND) {
8954 const bool movingInFrameDirection = IsMovingInFrameDirection(
8955 current.mFrame, aPos->mDirection,
8956 aPos->mOptions.contains(PeekOffsetOption::Visual));
8958 if (current.mJumpedLine) {
8959 // If we jumped lines, it's as if we found a character, but we still need
8960 // to eat non-renderable content on the new line.
8961 peekSearchState = current.PeekOffsetNoAmount(movingInFrameDirection);
8962 } else {
8963 PeekOffsetCharacterOptions options;
8964 options.mRespectClusters = aPos->mAmount == eSelectCluster;
8965 peekSearchState =
8966 current.PeekOffsetCharacter(movingInFrameDirection, options);
8969 current.mMovedOverNonSelectableText |=
8970 peekSearchState == CONTINUE_UNSELECTABLE;
8972 if (peekSearchState != FOUND) {
8973 SelectablePeekReport next = current.mFrame->GetFrameFromDirection(*aPos);
8974 if (next.Failed()) {
8975 return NS_ERROR_FAILURE;
8977 next.mJumpedLine |= current.mJumpedLine;
8978 next.mMovedOverNonSelectableText |= current.mMovedOverNonSelectableText;
8979 next.mHasSelectableFrame |= current.mHasSelectableFrame;
8980 current = next;
8983 // Found frame, but because we moved over non selectable text we want
8984 // the offset to be at the frame edge. Note that if we are extending the
8985 // selection, this doesn't matter.
8986 if (peekSearchState == FOUND && current.mMovedOverNonSelectableText &&
8987 (!aPos->mOptions.contains(PeekOffsetOption::Extend) ||
8988 current.mHasSelectableFrame)) {
8989 auto [start, end] = current.mFrame->GetOffsets();
8990 current.mOffset = aPos->mDirection == eDirNext ? 0 : end - start;
8994 // Set outputs
8995 current.TransferTo(*aPos);
8996 // If we're dealing with a text frame and moving backward positions us at
8997 // the end of that line, decrease the offset by one to make sure that
8998 // we're placed before the linefeed character on the previous line.
8999 if (current.mOffset < 0 && current.mJumpedLine &&
9000 aPos->mDirection == eDirPrevious &&
9001 current.mFrame->HasSignificantTerminalNewline() &&
9002 !current.mIgnoredBrFrame) {
9003 --aPos->mContentOffset;
9005 return NS_OK;
9008 nsresult nsIFrame::PeekOffsetForWord(PeekOffsetStruct* aPos, int32_t aOffset) {
9009 SelectablePeekReport current{this, aOffset};
9010 bool shouldStopAtHardBreak =
9011 aPos->mWordMovementType == eDefaultBehavior &&
9012 StaticPrefs::layout_word_select_eat_space_to_next_word();
9013 bool wordSelectEatSpace = ShouldWordSelectionEatSpace(*aPos);
9015 PeekWordState state;
9016 while (true) {
9017 bool movingInFrameDirection = IsMovingInFrameDirection(
9018 current.mFrame, aPos->mDirection,
9019 aPos->mOptions.contains(PeekOffsetOption::Visual));
9021 FrameSearchResult searchResult = current.mFrame->PeekOffsetWord(
9022 movingInFrameDirection, wordSelectEatSpace,
9023 aPos->mOptions.contains(PeekOffsetOption::IsKeyboardSelect),
9024 &current.mOffset, &state,
9025 !aPos->mOptions.contains(PeekOffsetOption::PreserveSpaces));
9026 if (searchResult == FOUND) {
9027 break;
9030 SelectablePeekReport next = [&]() {
9031 PeekOffsetOptions options = aPos->mOptions;
9032 if (state.mSawInlineCharacter) {
9033 // If we've already found a character, we don't want to stop at
9034 // placeholder frame boundary if there is in the word.
9035 options += PeekOffsetOption::StopAtPlaceholder;
9037 return current.mFrame->GetFrameFromDirection(aPos->mDirection, options);
9038 }();
9039 if (next.Failed()) {
9040 // If we've crossed the line boundary, check to make sure that we
9041 // have not consumed a trailing newline as whitespace if it's
9042 // significant.
9043 if (next.mJumpedLine && wordSelectEatSpace &&
9044 current.mFrame->HasSignificantTerminalNewline() &&
9045 current.mFrame->StyleText()->mWhiteSpace !=
9046 StyleWhiteSpace::PreLine) {
9047 current.mOffset -= 1;
9049 break;
9052 if ((next.mJumpedLine || next.mFoundPlaceholder) && !wordSelectEatSpace &&
9053 state.mSawBeforeType) {
9054 // We can't jump lines if we're looking for whitespace following
9055 // non-whitespace, and we already encountered non-whitespace.
9056 break;
9059 if (shouldStopAtHardBreak && next.mJumpedHardBreak) {
9061 * Prev, always: Jump and stop right there
9062 * Next, saw inline: just stop
9063 * Next, no inline: Jump and consume whitespaces
9065 if (aPos->mDirection == eDirPrevious) {
9066 // Try moving to the previous line if exists
9067 current.TransferTo(*aPos);
9068 current.mFrame->PeekOffsetForCharacter(aPos, current.mOffset);
9069 return NS_OK;
9071 if (state.mSawInlineCharacter || current.mJumpedHardBreak) {
9072 if (current.mFrame->HasSignificantTerminalNewline()) {
9073 current.mOffset -= 1;
9075 current.TransferTo(*aPos);
9076 return NS_OK;
9078 // Mark the state as whitespace and continue
9079 state.Update(false, true);
9082 if (next.mJumpedLine) {
9083 state.mContext.Truncate();
9085 current = next;
9086 // Jumping a line is equivalent to encountering whitespace
9087 // This affects only when it already met an actual character
9088 if (wordSelectEatSpace && next.mJumpedLine) {
9089 state.SetSawBeforeType();
9093 // Set outputs
9094 current.TransferTo(*aPos);
9095 return NS_OK;
9098 static nsIFrame* GetFirstSelectableDescendantWithLineIterator(
9099 nsIFrame* aParentFrame, bool aForceEditableRegion) {
9100 auto FoundValidFrame = [aForceEditableRegion](const nsIFrame* aFrame) {
9101 if (!aFrame->IsSelectable(nullptr)) {
9102 return false;
9104 if (aForceEditableRegion && !aFrame->GetContent()->IsEditable()) {
9105 return false;
9107 return true;
9110 for (nsIFrame* child : aParentFrame->PrincipalChildList()) {
9111 // some children may not be selectable, e.g. :before / :after pseudoelements
9112 // content with user-select: none, or contenteditable="false"
9113 // we need to skip them
9114 if (child->CanProvideLineIterator() && FoundValidFrame(child)) {
9115 return child;
9117 if (nsIFrame* nested = GetFirstSelectableDescendantWithLineIterator(
9118 child, aForceEditableRegion)) {
9119 return nested;
9122 return nullptr;
9125 nsresult nsIFrame::PeekOffsetForLine(PeekOffsetStruct* aPos) {
9126 nsIFrame* blockFrame = this;
9127 nsresult result = NS_ERROR_FAILURE;
9129 // outer loop
9130 // moving to a next block when no more blocks are available in a subtree
9131 AutoAssertNoDomMutations guard;
9132 while (NS_FAILED(result)) {
9133 auto [newBlock, lineFrame] = blockFrame->GetContainingBlockForLine(
9134 aPos->mOptions.contains(PeekOffsetOption::StopAtScroller));
9135 if (!newBlock) {
9136 return NS_ERROR_FAILURE;
9138 blockFrame = newBlock;
9139 nsILineIterator* iter = blockFrame->GetLineIterator();
9140 int32_t thisLine = iter->FindLineContaining(lineFrame);
9141 if (NS_WARN_IF(thisLine < 0)) {
9142 return NS_ERROR_FAILURE;
9145 int8_t edgeCase = 0; // no edge case. This should look at thisLine
9147 // this part will find a frame or a block frame. If it's a block frame
9148 // it will "drill down" to find a viable frame or it will return an
9149 // error.
9150 nsIFrame* lastFrame = this;
9152 // inner loop - crawling the frames within a specific block subtree
9153 while (true) {
9154 result =
9155 GetNextPrevLineFromBlockFrame(aPos, blockFrame, thisLine, edgeCase);
9156 // we came back to same spot! keep going
9157 if (NS_SUCCEEDED(result) &&
9158 (!aPos->mResultFrame || aPos->mResultFrame == lastFrame)) {
9159 aPos->mResultFrame = nullptr;
9160 lastFrame = nullptr;
9161 if (aPos->mDirection == eDirPrevious) {
9162 thisLine--;
9163 } else {
9164 thisLine++;
9166 continue;
9169 if (NS_FAILED(result)) {
9170 break;
9173 lastFrame = aPos->mResultFrame; // set last frame
9174 /* SPECIAL CHECK FOR NAVIGATION INTO TABLES
9175 * when we hit a frame which doesn't have line iterator, we need to
9176 * drill down and find a child with the line iterator to prevent the
9177 * crawling process to prematurely finish. Note that this is only sound if
9178 * we're guaranteed to not have multiple children implementing
9179 * LineIterator.
9181 * So far known cases are:
9182 * 1) table wrapper (drill down into table row group)
9183 * 2) table cell (drill down into its only anon child)
9185 const bool shouldDrillIntoChildren =
9186 aPos->mResultFrame->IsTableWrapperFrame() ||
9187 aPos->mResultFrame->IsTableCellFrame();
9189 if (shouldDrillIntoChildren) {
9190 nsIFrame* child = GetFirstSelectableDescendantWithLineIterator(
9191 aPos->mResultFrame,
9192 aPos->mOptions.contains(PeekOffsetOption::ForceEditableRegion));
9193 if (child) {
9194 aPos->mResultFrame = child;
9198 if (!aPos->mResultFrame->CanProvideLineIterator()) {
9199 // no more selectable content at this level
9200 break;
9203 if (aPos->mResultFrame == blockFrame) {
9204 // Make sure block element is not the same as the one we had before.
9205 break;
9208 // we've struck another block element with selectable content!
9209 if (aPos->mDirection == eDirPrevious) {
9210 edgeCase = 1; // far edge, search from end backwards
9211 } else {
9212 edgeCase = -1; // near edge search from beginning onwards
9214 thisLine = 0; // this line means nothing now.
9215 // everything else means something so keep looking "inside" the
9216 // block
9217 blockFrame = aPos->mResultFrame;
9220 return result;
9223 nsresult nsIFrame::PeekOffsetForLineEdge(PeekOffsetStruct* aPos) {
9224 // Adjusted so that the caret can't get confused when content changes
9225 nsIFrame* frame = AdjustFrameForSelectionStyles(this);
9226 Element* editingHost = frame->GetContent()->GetEditingHost();
9228 auto [blockFrame, lineFrame] = frame->GetContainingBlockForLine(
9229 aPos->mOptions.contains(PeekOffsetOption::StopAtScroller));
9230 if (!blockFrame) {
9231 return NS_ERROR_FAILURE;
9233 AutoAssertNoDomMutations guard;
9234 nsILineIterator* it = blockFrame->GetLineIterator();
9235 int32_t thisLine = it->FindLineContaining(lineFrame);
9236 if (thisLine < 0) {
9237 return NS_ERROR_FAILURE;
9240 nsIFrame* baseFrame = nullptr;
9241 bool endOfLine = eSelectEndLine == aPos->mAmount;
9243 if (aPos->mOptions.contains(PeekOffsetOption::Visual) &&
9244 PresContext()->BidiEnabled()) {
9245 nsIFrame* firstFrame;
9246 bool isReordered;
9247 nsIFrame* lastFrame;
9248 MOZ_TRY(
9249 it->CheckLineOrder(thisLine, &isReordered, &firstFrame, &lastFrame));
9250 baseFrame = endOfLine ? lastFrame : firstFrame;
9251 } else {
9252 auto line = it->GetLine(thisLine).unwrap();
9254 nsIFrame* frame = line.mFirstFrameOnLine;
9255 bool lastFrameWasEditable = false;
9256 for (int32_t count = line.mNumFramesOnLine; count;
9257 --count, frame = frame->GetNextSibling()) {
9258 if (frame->IsGeneratedContentFrame()) {
9259 continue;
9261 // When jumping to the end of the line with the "end" key,
9262 // try to skip over brFrames
9263 if (endOfLine && line.mNumFramesOnLine > 1 && frame->IsBrFrame() &&
9264 lastFrameWasEditable == frame->GetContent()->IsEditable()) {
9265 continue;
9267 lastFrameWasEditable =
9268 frame->GetContent() && frame->GetContent()->IsEditable();
9269 baseFrame = frame;
9270 if (!endOfLine) {
9271 break;
9275 if (!baseFrame) {
9276 return NS_ERROR_FAILURE;
9278 // Make sure we are not leaving our inline editing host if exists
9279 if (editingHost) {
9280 if (nsIFrame* frame = editingHost->GetPrimaryFrame()) {
9281 if (frame->IsInlineOutside() &&
9282 !editingHost->Contains(baseFrame->GetContent())) {
9283 baseFrame = frame;
9284 if (endOfLine) {
9285 baseFrame = baseFrame->LastContinuation();
9290 FrameTarget targetFrame = DrillDownToSelectionFrame(baseFrame, endOfLine, 0);
9291 SetPeekResultFromFrame(*aPos, targetFrame.frame, endOfLine ? -1 : 0,
9292 OffsetIsAtLineEdge::Yes);
9293 if (endOfLine && targetFrame.frame->HasSignificantTerminalNewline()) {
9294 // Do not position the caret after the terminating newline if we're
9295 // trying to move to the end of line (see bug 596506)
9296 --aPos->mContentOffset;
9298 if (!aPos->mResultContent) {
9299 return NS_ERROR_FAILURE;
9301 return NS_OK;
9304 nsresult nsIFrame::PeekOffset(PeekOffsetStruct* aPos) {
9305 MOZ_ASSERT(aPos);
9307 if (NS_WARN_IF(HasAnyStateBits(NS_FRAME_IS_DIRTY))) {
9308 // FIXME(Bug 1654362): <caption> currently can remain dirty.
9309 return NS_ERROR_UNEXPECTED;
9312 // Translate content offset to be relative to frame
9313 int32_t offset = aPos->mStartOffset - GetRangeForFrame(this).start;
9315 switch (aPos->mAmount) {
9316 case eSelectCharacter:
9317 case eSelectCluster:
9318 return PeekOffsetForCharacter(aPos, offset);
9319 case eSelectWordNoSpace:
9320 // eSelectWordNoSpace means that we should not be eating any whitespace
9321 // when moving to the adjacent word. This means that we should set aPos->
9322 // mWordMovementType to eEndWord if we're moving forwards, and to
9323 // eStartWord if we're moving backwards.
9324 if (aPos->mDirection == eDirPrevious) {
9325 aPos->mWordMovementType = eStartWord;
9326 } else {
9327 aPos->mWordMovementType = eEndWord;
9329 // Intentionally fall through the eSelectWord case.
9330 [[fallthrough]];
9331 case eSelectWord:
9332 return PeekOffsetForWord(aPos, offset);
9333 case eSelectLine:
9334 return PeekOffsetForLine(aPos);
9335 case eSelectBeginLine:
9336 case eSelectEndLine:
9337 return PeekOffsetForLineEdge(aPos);
9338 case eSelectParagraph:
9339 return PeekOffsetForParagraph(aPos);
9340 default: {
9341 NS_ASSERTION(false, "Invalid amount");
9342 return NS_ERROR_FAILURE;
9347 nsIFrame::FrameSearchResult nsIFrame::PeekOffsetNoAmount(bool aForward,
9348 int32_t* aOffset) {
9349 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
9350 // Sure, we can stop right here.
9351 return FOUND;
9354 nsIFrame::FrameSearchResult nsIFrame::PeekOffsetCharacter(
9355 bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
9356 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
9357 int32_t startOffset = *aOffset;
9358 // A negative offset means "end of frame", which in our case means offset 1.
9359 if (startOffset < 0) startOffset = 1;
9360 if (aForward == (startOffset == 0)) {
9361 // We're before the frame and moving forward, or after it and moving
9362 // backwards: skip to the other side and we're done.
9363 *aOffset = 1 - startOffset;
9364 return FOUND;
9366 return CONTINUE;
9369 nsIFrame::FrameSearchResult nsIFrame::PeekOffsetWord(
9370 bool aForward, bool aWordSelectEatSpace, bool aIsKeyboardSelect,
9371 int32_t* aOffset, PeekWordState* aState, bool /*aTrimSpaces*/) {
9372 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
9373 int32_t startOffset = *aOffset;
9374 // This isn't text, so truncate the context
9375 aState->mContext.Truncate();
9376 if (startOffset < 0) startOffset = 1;
9377 if (aForward == (startOffset == 0)) {
9378 // We're before the frame and moving forward, or after it and moving
9379 // backwards. If we're looking for non-whitespace, we found it (without
9380 // skipping this frame).
9381 if (!aState->mAtStart) {
9382 if (aState->mLastCharWasPunctuation) {
9383 // We're not punctuation, so this is a punctuation boundary.
9384 if (BreakWordBetweenPunctuation(aState, aForward, false, false,
9385 aIsKeyboardSelect))
9386 return FOUND;
9387 } else {
9388 // This is not a punctuation boundary.
9389 if (aWordSelectEatSpace && aState->mSawBeforeType) return FOUND;
9392 // Otherwise skip to the other side and note that we encountered
9393 // non-whitespace.
9394 *aOffset = 1 - startOffset;
9395 aState->Update(false, // not punctuation
9396 false // not whitespace
9398 if (!aWordSelectEatSpace) aState->SetSawBeforeType();
9400 return CONTINUE;
9403 // static
9404 bool nsIFrame::BreakWordBetweenPunctuation(const PeekWordState* aState,
9405 bool aForward, bool aPunctAfter,
9406 bool aWhitespaceAfter,
9407 bool aIsKeyboardSelect) {
9408 NS_ASSERTION(aPunctAfter != aState->mLastCharWasPunctuation,
9409 "Call this only at punctuation boundaries");
9410 if (aState->mLastCharWasWhitespace) {
9411 // We always stop between whitespace and punctuation
9412 return true;
9414 if (!StaticPrefs::layout_word_select_stop_at_punctuation()) {
9415 // When this pref is false, we never stop at a punctuation boundary unless
9416 // it's followed by whitespace (in the relevant direction).
9417 return aWhitespaceAfter;
9419 if (!aIsKeyboardSelect) {
9420 // mouse caret movement (e.g. word selection) always stops at every
9421 // punctuation boundary
9422 return true;
9424 bool afterPunct = aForward ? aState->mLastCharWasPunctuation : aPunctAfter;
9425 if (!afterPunct) {
9426 // keyboard caret movement only stops after punctuation (in content order)
9427 return false;
9429 // Stop only if we've seen some non-punctuation since the last whitespace;
9430 // don't stop after punctuation that follows whitespace.
9431 return aState->mSeenNonPunctuationSinceWhitespace;
9434 std::pair<nsIFrame*, nsIFrame*> nsIFrame::GetContainingBlockForLine(
9435 bool aLockScroll) const {
9436 const nsIFrame* parentFrame = this;
9437 const nsIFrame* frame;
9438 while (parentFrame) {
9439 frame = parentFrame;
9440 if (frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
9441 // if we are searching for a frame that is not in flow we will not find
9442 // it. we must instead look for its placeholder
9443 if (frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
9444 // abspos continuations don't have placeholders, get the fif
9445 frame = frame->FirstInFlow();
9447 frame = frame->GetPlaceholderFrame();
9448 if (!frame) {
9449 return std::pair(nullptr, nullptr);
9452 parentFrame = frame->GetParent();
9453 if (parentFrame) {
9454 if (aLockScroll && parentFrame->IsScrollFrame()) {
9455 return std::pair(nullptr, nullptr);
9457 if (parentFrame->CanProvideLineIterator()) {
9458 return std::pair(const_cast<nsIFrame*>(parentFrame),
9459 const_cast<nsIFrame*>(frame));
9463 return std::pair(nullptr, nullptr);
9466 Result<bool, nsresult> nsIFrame::IsVisuallyAtLineEdge(
9467 nsILineIterator* aLineIterator, int32_t aLine, nsDirection aDirection) {
9468 auto line = aLineIterator->GetLine(aLine).unwrap();
9470 const bool lineIsRTL = aLineIterator->IsLineIteratorFlowRTL();
9472 nsIFrame *firstFrame = nullptr, *lastFrame = nullptr;
9473 bool isReordered = false;
9474 MOZ_TRY(aLineIterator->CheckLineOrder(aLine, &isReordered, &firstFrame,
9475 &lastFrame));
9476 if (!firstFrame || !lastFrame) {
9477 return true; // XXX: Why true? We check whether `this` is at the edge...
9480 nsIFrame* leftmostFrame = lineIsRTL ? lastFrame : firstFrame;
9481 nsIFrame* rightmostFrame = lineIsRTL ? firstFrame : lastFrame;
9482 auto FrameIsRTL = [](nsIFrame* aFrame) {
9483 return nsBidiPresUtils::FrameDirection(aFrame) ==
9484 mozilla::intl::BidiDirection::RTL;
9486 if (!lineIsRTL == (aDirection == eDirPrevious)) {
9487 nsIFrame* maybeLeftmostFrame = leftmostFrame;
9488 for ([[maybe_unused]] int32_t i : IntegerRange(line.mNumFramesOnLine)) {
9489 if (maybeLeftmostFrame == this) {
9490 return true;
9492 // If left edge of the line starts with placeholder frames, we can ignore
9493 // them and should keep checking the following frames.
9494 if (!maybeLeftmostFrame->IsPlaceholderFrame()) {
9495 if ((FrameIsRTL(maybeLeftmostFrame) == lineIsRTL) ==
9496 (aDirection == eDirPrevious)) {
9497 nsIFrame::GetFirstLeaf(&maybeLeftmostFrame);
9498 } else {
9499 nsIFrame::GetLastLeaf(&maybeLeftmostFrame);
9501 return maybeLeftmostFrame == this;
9503 maybeLeftmostFrame = nsBidiPresUtils::GetFrameToRightOf(
9504 maybeLeftmostFrame, line.mFirstFrameOnLine, line.mNumFramesOnLine);
9505 if (!maybeLeftmostFrame) {
9506 return false;
9509 return false;
9512 nsIFrame* maybeRightmostFrame = rightmostFrame;
9513 for ([[maybe_unused]] int32_t i : IntegerRange(line.mNumFramesOnLine)) {
9514 if (maybeRightmostFrame == this) {
9515 return true;
9517 // If the line ends with placehlder frames, we can ignore them and should
9518 // keep checking the preceding frames.
9519 if (!maybeRightmostFrame->IsPlaceholderFrame()) {
9520 if ((FrameIsRTL(maybeRightmostFrame) == lineIsRTL) ==
9521 (aDirection == eDirPrevious)) {
9522 nsIFrame::GetFirstLeaf(&maybeRightmostFrame);
9523 } else {
9524 nsIFrame::GetLastLeaf(&maybeRightmostFrame);
9526 return maybeRightmostFrame == this;
9528 maybeRightmostFrame = nsBidiPresUtils::GetFrameToLeftOf(
9529 maybeRightmostFrame, line.mFirstFrameOnLine, line.mNumFramesOnLine);
9530 if (!maybeRightmostFrame) {
9531 return false;
9534 return false;
9537 Result<bool, nsresult> nsIFrame::IsLogicallyAtLineEdge(
9538 nsILineIterator* aLineIterator, int32_t aLine, nsDirection aDirection) {
9539 auto line = aLineIterator->GetLine(aLine).unwrap();
9540 if (!line.mNumFramesOnLine) {
9541 return false;
9543 MOZ_ASSERT(line.mFirstFrameOnLine);
9545 if (aDirection == eDirPrevious) {
9546 nsIFrame* maybeFirstFrame = line.mFirstFrameOnLine;
9547 for ([[maybe_unused]] int32_t i : IntegerRange(line.mNumFramesOnLine)) {
9548 if (maybeFirstFrame == this) {
9549 return true;
9551 // If the line starts with placeholder frames, we can ignore them and
9552 // should keep checking the following frames.
9553 if (!maybeFirstFrame->IsPlaceholderFrame()) {
9554 nsIFrame::GetFirstLeaf(&maybeFirstFrame);
9555 return maybeFirstFrame == this;
9557 maybeFirstFrame = maybeFirstFrame->GetNextSibling();
9558 if (!maybeFirstFrame) {
9559 return false;
9562 return false;
9565 // eDirNext
9566 nsIFrame* maybeLastFrame = line.GetLastFrameOnLine();
9567 for ([[maybe_unused]] int32_t i : IntegerRange(line.mNumFramesOnLine)) {
9568 if (maybeLastFrame == this) {
9569 return true;
9571 // If the line ends with placehlder frames, we can ignore them and should
9572 // keep checking the preceding frames.
9573 if (!maybeLastFrame->IsPlaceholderFrame()) {
9574 nsIFrame::GetLastLeaf(&maybeLastFrame);
9575 return maybeLastFrame == this;
9577 maybeLastFrame = maybeLastFrame->GetPrevSibling();
9579 return false;
9582 nsIFrame::SelectablePeekReport nsIFrame::GetFrameFromDirection(
9583 nsDirection aDirection, const PeekOffsetOptions& aOptions) {
9584 SelectablePeekReport result;
9586 nsPresContext* presContext = PresContext();
9587 const bool needsVisualTraversal =
9588 aOptions.contains(PeekOffsetOption::Visual) && presContext->BidiEnabled();
9589 const bool followOofs =
9590 !aOptions.contains(PeekOffsetOption::StopAtPlaceholder);
9591 nsCOMPtr<nsIFrameEnumerator> frameTraversal;
9592 MOZ_TRY(NS_NewFrameTraversal(
9593 getter_AddRefs(frameTraversal), presContext, this, eLeaf,
9594 needsVisualTraversal, aOptions.contains(PeekOffsetOption::StopAtScroller),
9595 followOofs,
9596 false // aSkipPopupChecks
9599 // Find the prev/next selectable frame
9600 bool selectable = false;
9601 nsIFrame* traversedFrame = this;
9602 AutoAssertNoDomMutations guard;
9603 const nsIContent* const nativeAnonymousSubtreeContent =
9604 GetClosestNativeAnonymousSubtreeRoot();
9605 while (!selectable) {
9606 auto [blockFrame, lineFrame] = traversedFrame->GetContainingBlockForLine(
9607 aOptions.contains(PeekOffsetOption::StopAtScroller));
9608 if (!blockFrame) {
9609 return result;
9612 nsILineIterator* it = blockFrame->GetLineIterator();
9613 int32_t thisLine = it->FindLineContaining(lineFrame);
9614 if (thisLine < 0) {
9615 return result;
9618 bool atLineEdge;
9619 MOZ_TRY_VAR(
9620 atLineEdge,
9621 needsVisualTraversal
9622 ? traversedFrame->IsVisuallyAtLineEdge(it, thisLine, aDirection)
9623 : traversedFrame->IsLogicallyAtLineEdge(it, thisLine, aDirection));
9624 if (atLineEdge) {
9625 result.mJumpedLine = true;
9626 if (!aOptions.contains(PeekOffsetOption::JumpLines)) {
9627 return result; // we are done. cannot jump lines
9629 int32_t lineToCheckWrap =
9630 aDirection == eDirPrevious ? thisLine - 1 : thisLine;
9631 if (lineToCheckWrap < 0 ||
9632 !it->GetLine(lineToCheckWrap).unwrap().mIsWrapped) {
9633 result.mJumpedHardBreak = true;
9637 traversedFrame = frameTraversal->Traverse(aDirection == eDirNext);
9638 if (!traversedFrame) {
9639 return result;
9642 if (aOptions.contains(PeekOffsetOption::StopAtPlaceholder) &&
9643 traversedFrame->IsPlaceholderFrame()) {
9644 // XXX If the placeholder frame does not have meaningful content, the user
9645 // may want to select as a word around the out-of-flow cotent. However,
9646 // non-text frame resets context in nsIFrame::PeekOffsetWord(). Therefore,
9647 // next text frame considers the new word starts from its edge. So, it's
9648 // not enough to implement such behavior with adding a check here whether
9649 // the real frame may change the word with its contents if it were not
9650 // out-of-flow.
9651 result.mFoundPlaceholder = true;
9652 return result;
9655 auto IsSelectable =
9656 [aOptions, nativeAnonymousSubtreeContent](const nsIFrame* aFrame) {
9657 if (!aFrame->IsSelectable(nullptr)) {
9658 return false;
9660 // If the new frame is in a native anonymous subtree, we should treat
9661 // it as not selectable unless the frame and found frame are in same
9662 // subtree.
9663 if (aFrame->GetClosestNativeAnonymousSubtreeRoot() !=
9664 nativeAnonymousSubtreeContent) {
9665 return false;
9667 return !aOptions.contains(PeekOffsetOption::ForceEditableRegion) ||
9668 aFrame->GetContent()->IsEditable();
9671 // Skip br frames, but only if we can select something before hitting the
9672 // end of the line or a non-selectable region.
9673 if (atLineEdge && aDirection == eDirPrevious &&
9674 traversedFrame->IsBrFrame()) {
9675 for (nsIFrame* current = traversedFrame->GetPrevSibling(); current;
9676 current = current->GetPrevSibling()) {
9677 if (!current->IsBlockOutside() && IsSelectable(current)) {
9678 if (!current->IsBrFrame()) {
9679 result.mIgnoredBrFrame = true;
9681 break;
9684 if (result.mIgnoredBrFrame) {
9685 continue;
9689 selectable = IsSelectable(traversedFrame);
9690 if (!selectable) {
9691 if (traversedFrame->IsSelectable(nullptr)) {
9692 result.mHasSelectableFrame = true;
9694 result.mMovedOverNonSelectableText = true;
9696 } // while (!selectable)
9698 result.mOffset = (aDirection == eDirNext) ? 0 : -1;
9700 if (aOptions.contains(PeekOffsetOption::Visual) &&
9701 nsBidiPresUtils::IsReversedDirectionFrame(traversedFrame)) {
9702 // The new frame is reverse-direction, go to the other end
9703 result.mOffset = -1 - result.mOffset;
9705 result.mFrame = traversedFrame;
9706 return result;
9709 nsIFrame::SelectablePeekReport nsIFrame::GetFrameFromDirection(
9710 const PeekOffsetStruct& aPos) {
9711 return GetFrameFromDirection(aPos.mDirection, aPos.mOptions);
9714 nsView* nsIFrame::GetClosestView(nsPoint* aOffset) const {
9715 nsPoint offset(0, 0);
9716 for (const nsIFrame* f = this; f; f = f->GetParent()) {
9717 if (f->HasView()) {
9718 if (aOffset) *aOffset = offset;
9719 return f->GetView();
9721 offset += f->GetPosition();
9724 MOZ_ASSERT_UNREACHABLE("No view on any parent? How did that happen?");
9725 return nullptr;
9728 /* virtual */
9729 void nsIFrame::ChildIsDirty(nsIFrame* aChild) {
9730 MOZ_ASSERT_UNREACHABLE(
9731 "should never be called on a frame that doesn't "
9732 "inherit from nsContainerFrame");
9735 #ifdef ACCESSIBILITY
9736 a11y::AccType nsIFrame::AccessibleType() {
9737 if (IsTableCaption() && !GetRect().IsEmpty()) {
9738 return a11y::eHTMLCaptionType;
9740 return a11y::eNoType;
9742 #endif
9744 bool nsIFrame::ClearOverflowRects() {
9745 if (mOverflow.mType == OverflowStorageType::None) {
9746 return false;
9748 if (mOverflow.mType == OverflowStorageType::Large) {
9749 RemoveProperty(OverflowAreasProperty());
9751 mOverflow.mType = OverflowStorageType::None;
9752 return true;
9755 bool nsIFrame::SetOverflowAreas(const OverflowAreas& aOverflowAreas) {
9756 if (mOverflow.mType == OverflowStorageType::Large) {
9757 OverflowAreas* overflow = GetOverflowAreasProperty();
9758 bool changed = *overflow != aOverflowAreas;
9759 *overflow = aOverflowAreas;
9761 // Don't bother with converting to the deltas form if we already
9762 // have a property.
9763 return changed;
9766 const nsRect& vis = aOverflowAreas.InkOverflow();
9767 uint32_t l = -vis.x, // left edge: positive delta is leftwards
9768 t = -vis.y, // top: positive is upwards
9769 r = vis.XMost() - mRect.width, // right: positive is rightwards
9770 b = vis.YMost() - mRect.height; // bottom: positive is downwards
9771 if (aOverflowAreas.ScrollableOverflow().IsEqualEdges(
9772 nsRect(nsPoint(0, 0), GetSize())) &&
9773 l <= InkOverflowDeltas::kMax && t <= InkOverflowDeltas::kMax &&
9774 r <= InkOverflowDeltas::kMax && b <= InkOverflowDeltas::kMax &&
9775 // we have to check these against zero because we *never* want to
9776 // set a frame as having no overflow in this function. This is
9777 // because FinishAndStoreOverflow calls this function prior to
9778 // SetRect based on whether the overflow areas match aNewSize.
9779 // In the case where the overflow areas exactly match mRect but
9780 // do not match aNewSize, we need to store overflow in a property
9781 // so that our eventual SetRect/SetSize will know that it has to
9782 // reset our overflow areas.
9783 (l | t | r | b) != 0) {
9784 InkOverflowDeltas oldDeltas = mOverflow.mInkOverflowDeltas;
9785 // It's a "small" overflow area so we store the deltas for each edge
9786 // directly in the frame, rather than allocating a separate rect.
9787 // If they're all zero, that's fine; we're setting things to
9788 // no-overflow.
9789 mOverflow.mInkOverflowDeltas.mLeft = l;
9790 mOverflow.mInkOverflowDeltas.mTop = t;
9791 mOverflow.mInkOverflowDeltas.mRight = r;
9792 mOverflow.mInkOverflowDeltas.mBottom = b;
9793 // There was no scrollable overflow before, and there isn't now.
9794 return oldDeltas != mOverflow.mInkOverflowDeltas;
9795 } else {
9796 bool changed =
9797 !aOverflowAreas.ScrollableOverflow().IsEqualEdges(
9798 nsRect(nsPoint(0, 0), GetSize())) ||
9799 !aOverflowAreas.InkOverflow().IsEqualEdges(InkOverflowFromDeltas());
9801 // it's a large overflow area that we need to store as a property
9802 mOverflow.mType = OverflowStorageType::Large;
9803 AddProperty(OverflowAreasProperty(), new OverflowAreas(aOverflowAreas));
9804 return changed;
9808 enum class ApplyTransform : bool { No, Yes };
9811 * Compute the outline inner rect (so without outline-width and outline-offset)
9812 * of aFrame, maybe iterating over its descendants, in aFrame's coordinate space
9813 * or its post-transform coordinate space (depending on aApplyTransform).
9815 static nsRect ComputeOutlineInnerRect(
9816 nsIFrame* aFrame, ApplyTransform aApplyTransform, bool& aOutValid,
9817 const nsSize* aSizeOverride = nullptr,
9818 const OverflowAreas* aOverflowOverride = nullptr) {
9819 const nsRect bounds(nsPoint(0, 0),
9820 aSizeOverride ? *aSizeOverride : aFrame->GetSize());
9822 // The SVG container frames besides SVGTextFrame do not maintain
9823 // an accurate mRect. It will make the outline be larger than
9824 // we expect, we need to make them narrow to their children's outline.
9825 // aOutValid is set to false if the returned nsRect is not valid
9826 // and should not be included in the outline rectangle.
9827 aOutValid = !aFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT) ||
9828 !aFrame->IsSVGContainerFrame() || aFrame->IsSVGTextFrame();
9830 nsRect u;
9832 if (!aFrame->FrameMaintainsOverflow()) {
9833 return u;
9836 // Start from our border-box, transformed. See comment below about
9837 // transform of children.
9838 bool doTransform =
9839 aApplyTransform == ApplyTransform::Yes && aFrame->IsTransformed();
9840 TransformReferenceBox boundsRefBox(nullptr, bounds);
9841 if (doTransform) {
9842 u = nsDisplayTransform::TransformRect(bounds, aFrame, boundsRefBox);
9843 } else {
9844 u = bounds;
9847 if (aOutValid && !StaticPrefs::layout_outline_include_overflow()) {
9848 return u;
9851 // Only iterate through the children if the overflow areas suggest
9852 // that we might need to, and if the frame doesn't clip its overflow
9853 // anyway.
9854 if (aOverflowOverride) {
9855 if (!doTransform && bounds.IsEqualEdges(aOverflowOverride->InkOverflow()) &&
9856 bounds.IsEqualEdges(aOverflowOverride->ScrollableOverflow())) {
9857 return u;
9859 } else {
9860 if (!doTransform && bounds.IsEqualEdges(aFrame->InkOverflowRect()) &&
9861 bounds.IsEqualEdges(aFrame->ScrollableOverflowRect())) {
9862 return u;
9865 const nsStyleDisplay* disp = aFrame->StyleDisplay();
9866 LayoutFrameType fType = aFrame->Type();
9867 if (fType == LayoutFrameType::Scroll ||
9868 fType == LayoutFrameType::ListControl ||
9869 fType == LayoutFrameType::SVGOuterSVG) {
9870 return u;
9873 auto overflowClipAxes = aFrame->ShouldApplyOverflowClipping(disp);
9874 auto overflowClipMargin = aFrame->OverflowClipMargin(overflowClipAxes);
9875 if (overflowClipAxes == nsIFrame::PhysicalAxes::Both &&
9876 overflowClipMargin == nsSize()) {
9877 return u;
9880 const nsStyleEffects* effects = aFrame->StyleEffects();
9881 Maybe<nsRect> clipPropClipRect =
9882 aFrame->GetClipPropClipRect(disp, effects, bounds.Size());
9884 // Iterate over all children except pop-up, absolutely-positioned,
9885 // float, and overflow ones.
9886 const FrameChildListIDs skip = {
9887 FrameChildListID::Popup, FrameChildListID::Absolute,
9888 FrameChildListID::Fixed, FrameChildListID::Float,
9889 FrameChildListID::Overflow};
9890 for (const auto& [list, listID] : aFrame->ChildLists()) {
9891 if (skip.contains(listID)) {
9892 continue;
9895 for (nsIFrame* child : list) {
9896 if (child->IsPlaceholderFrame()) {
9897 continue;
9900 // Note that passing ApplyTransform::Yes when
9901 // child->Combines3DTransformWithAncestors() returns true is incorrect if
9902 // our aApplyTransform is No... but the opposite would be as well.
9903 // This is because elements within a preserve-3d scene are always
9904 // transformed up to the top of the scene. This means we don't have a
9905 // mechanism for getting a transform up to an intermediate point within
9906 // the scene. We choose to over-transform rather than under-transform
9907 // because this is consistent with other overflow areas.
9908 bool validRect = true;
9909 nsRect childRect =
9910 ComputeOutlineInnerRect(child, ApplyTransform::Yes, validRect) +
9911 child->GetPosition();
9913 if (!validRect) {
9914 continue;
9917 if (clipPropClipRect) {
9918 // Intersect with the clip before transforming.
9919 childRect.IntersectRect(childRect, *clipPropClipRect);
9922 // Note that we transform each child separately according to
9923 // aFrame's transform, and then union, which gives a different
9924 // (smaller) result from unioning and then transforming the
9925 // union. This doesn't match the way we handle overflow areas
9926 // with 2-D transforms, though it does match the way we handle
9927 // overflow areas in preserve-3d 3-D scenes.
9928 if (doTransform && !child->Combines3DTransformWithAncestors()) {
9929 childRect =
9930 nsDisplayTransform::TransformRect(childRect, aFrame, boundsRefBox);
9933 // If a SVGContainer has a non-SVGContainer child, we assign
9934 // its child's outline to this SVGContainer directly.
9935 if (!aOutValid && validRect) {
9936 u = childRect;
9937 aOutValid = true;
9938 } else {
9939 u = u.UnionEdges(childRect);
9944 if (overflowClipAxes != nsIFrame::PhysicalAxes::None) {
9945 OverflowAreas::ApplyOverflowClippingOnRect(u, bounds, overflowClipAxes,
9946 overflowClipMargin);
9948 return u;
9951 static void ComputeAndIncludeOutlineArea(nsIFrame* aFrame,
9952 OverflowAreas& aOverflowAreas,
9953 const nsSize& aNewSize) {
9954 const nsStyleOutline* outline = aFrame->StyleOutline();
9955 if (!outline->ShouldPaintOutline()) {
9956 return;
9959 // When the outline property is set on a :-moz-block-inside-inline-wrapper
9960 // pseudo-element, it inherited that outline from the inline that was broken
9961 // because it contained a block. In that case, we don't want a really wide
9962 // outline if the block inside the inline is narrow, so union the actual
9963 // contents of the anonymous blocks.
9964 nsIFrame* frameForArea = aFrame;
9965 do {
9966 PseudoStyleType pseudoType = frameForArea->Style()->GetPseudoType();
9967 if (pseudoType != PseudoStyleType::mozBlockInsideInlineWrapper) break;
9968 // If we're done, we really want it and all its later siblings.
9969 frameForArea = frameForArea->PrincipalChildList().FirstChild();
9970 NS_ASSERTION(frameForArea, "anonymous block with no children?");
9971 } while (frameForArea);
9973 // Find the union of the border boxes of all descendants, or in
9974 // the block-in-inline case, all descendants we care about.
9976 // Note that the interesting perspective-related cases are taken
9977 // care of by the code that handles those issues for overflow
9978 // calling FinishAndStoreOverflow again, which in turn calls this
9979 // function again. We still need to deal with preserve-3d a bit.
9980 nsRect innerRect;
9981 bool validRect = false;
9982 if (frameForArea == aFrame) {
9983 innerRect = ComputeOutlineInnerRect(aFrame, ApplyTransform::No, validRect,
9984 &aNewSize, &aOverflowAreas);
9985 } else {
9986 for (; frameForArea; frameForArea = frameForArea->GetNextSibling()) {
9987 nsRect r =
9988 ComputeOutlineInnerRect(frameForArea, ApplyTransform::Yes, validRect);
9990 // Adjust for offsets transforms up to aFrame's pre-transform
9991 // (i.e., normal) coordinate space; see comments in
9992 // UnionBorderBoxes for some of the subtlety here.
9993 for (nsIFrame *f = frameForArea, *parent = f->GetParent();
9994 /* see middle of loop */; f = parent, parent = f->GetParent()) {
9995 r += f->GetPosition();
9996 if (parent == aFrame) {
9997 break;
9999 if (parent->IsTransformed() && !f->Combines3DTransformWithAncestors()) {
10000 TransformReferenceBox refBox(parent);
10001 r = nsDisplayTransform::TransformRect(r, parent, refBox);
10005 innerRect.UnionRect(innerRect, r);
10009 // Keep this code in sync with nsDisplayOutline::GetInnerRect.
10010 if (innerRect == aFrame->GetRectRelativeToSelf()) {
10011 aFrame->RemoveProperty(nsIFrame::OutlineInnerRectProperty());
10012 } else {
10013 SetOrUpdateRectValuedProperty(aFrame, nsIFrame::OutlineInnerRectProperty(),
10014 innerRect);
10017 nsRect outerRect(innerRect);
10018 outerRect.Inflate(outline->EffectiveOffsetFor(outerRect));
10020 if (outline->mOutlineStyle.IsAuto()) {
10021 nsPresContext* pc = aFrame->PresContext();
10023 pc->Theme()->GetWidgetOverflow(pc->DeviceContext(), aFrame,
10024 StyleAppearance::FocusOutline, &outerRect);
10025 } else {
10026 const nscoord width = outline->GetOutlineWidth();
10027 outerRect.Inflate(width);
10030 nsRect& vo = aOverflowAreas.InkOverflow();
10031 vo = vo.UnionEdges(innerRect.Union(outerRect));
10034 bool nsIFrame::FinishAndStoreOverflow(OverflowAreas& aOverflowAreas,
10035 nsSize aNewSize, nsSize* aOldSize,
10036 const nsStyleDisplay* aStyleDisplay) {
10037 MOZ_ASSERT(FrameMaintainsOverflow(),
10038 "Don't call - overflow rects not maintained on these SVG frames");
10040 const nsStyleDisplay* disp = StyleDisplayWithOptionalParam(aStyleDisplay);
10041 bool hasTransform = IsTransformed();
10043 nsRect bounds(nsPoint(0, 0), aNewSize);
10044 // Store the passed in overflow area if we are a preserve-3d frame or we have
10045 // a transform, and it's not just the frame bounds.
10046 if (hasTransform || Combines3DTransformWithAncestors()) {
10047 if (!aOverflowAreas.InkOverflow().IsEqualEdges(bounds) ||
10048 !aOverflowAreas.ScrollableOverflow().IsEqualEdges(bounds)) {
10049 OverflowAreas* initial = GetProperty(nsIFrame::InitialOverflowProperty());
10050 if (!initial) {
10051 AddProperty(nsIFrame::InitialOverflowProperty(),
10052 new OverflowAreas(aOverflowAreas));
10053 } else if (initial != &aOverflowAreas) {
10054 *initial = aOverflowAreas;
10056 } else {
10057 RemoveProperty(nsIFrame::InitialOverflowProperty());
10059 #ifdef DEBUG
10060 SetProperty(nsIFrame::DebugInitialOverflowPropertyApplied(), true);
10061 #endif
10062 } else {
10063 #ifdef DEBUG
10064 RemoveProperty(nsIFrame::DebugInitialOverflowPropertyApplied());
10065 #endif
10068 nsSize oldSize = mRect.Size();
10069 bool sizeChanged = ((aOldSize ? *aOldSize : oldSize) != aNewSize);
10071 // Our frame size may not have been computed and set yet, but code under
10072 // functions such as ComputeEffectsRect (which we're about to call) use the
10073 // values that are stored in our frame rect to compute their results. We
10074 // need the results from those functions to be based on the frame size that
10075 // we *will* have, so we temporarily set our frame size here before calling
10076 // those functions.
10078 // XXX Someone should document here why we revert the frame size before we
10079 // return rather than just leaving it set.
10081 // We pass false here to avoid invalidating display items for this temporary
10082 // change. We sometimes reflow frames multiple times, with the final size
10083 // being the same as the initial. The single call to SetSize after reflow is
10084 // done will take care of invalidating display items if the size has actually
10085 // changed.
10086 SetSize(aNewSize, false);
10088 const auto overflowClipAxes = ShouldApplyOverflowClipping(disp);
10090 if (ChildrenHavePerspective(disp) && sizeChanged) {
10091 RecomputePerspectiveChildrenOverflow(this);
10093 if (overflowClipAxes != PhysicalAxes::Both) {
10094 aOverflowAreas.SetAllTo(bounds);
10095 DebugOnly<bool> ok = ComputeCustomOverflow(aOverflowAreas);
10097 // ComputeCustomOverflow() should not return false, when
10098 // FrameMaintainsOverflow() returns true.
10099 MOZ_ASSERT(ok, "FrameMaintainsOverflow() != ComputeCustomOverflow()");
10101 UnionChildOverflow(aOverflowAreas);
10105 // This is now called FinishAndStoreOverflow() instead of
10106 // StoreOverflow() because frame-generic ways of adding overflow
10107 // can happen here, e.g. CSS2 outline and native theme.
10108 // If the overflow area width or height is nscoord_MAX, then a
10109 // saturating union may have encounted an overflow, so the overflow may not
10110 // contain the frame border-box. Don't warn in that case.
10111 // Don't warn for SVG either, since SVG doesn't need the overflow area
10112 // to contain the frame bounds.
10113 for (const auto otype : AllOverflowTypes()) {
10114 DebugOnly<nsRect*> r = &aOverflowAreas.Overflow(otype);
10115 NS_ASSERTION(aNewSize.width == 0 || aNewSize.height == 0 ||
10116 r->width == nscoord_MAX || r->height == nscoord_MAX ||
10117 HasAnyStateBits(NS_FRAME_SVG_LAYOUT) ||
10118 r->Contains(nsRect(nsPoint(0, 0), aNewSize)),
10119 "Computed overflow area must contain frame bounds");
10122 // Overflow area must always include the frame's top-left and bottom-right,
10123 // even if the frame rect is empty (so we can scroll to those positions).
10124 const bool shouldIncludeBounds = [&] {
10125 if (aNewSize.width == 0 && IsInlineFrame()) {
10126 // Pending a real fix for bug 426879, don't do this for inline frames with
10127 // zero width.
10128 return false;
10130 if (HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
10131 // Do not do this for SVG either, since it will usually massively increase
10132 // the area unnecessarily (except for SVG that applies clipping, since
10133 // that's the pre-existing behavior, and breaks pre-rendering otherwise).
10134 // FIXME(bug 1770704): This check most likely wants to be removed or check
10135 // for specific frame types at least.
10136 return overflowClipAxes != PhysicalAxes::None;
10138 return true;
10139 }();
10141 if (shouldIncludeBounds) {
10142 for (const auto otype : AllOverflowTypes()) {
10143 nsRect& o = aOverflowAreas.Overflow(otype);
10144 o = o.UnionEdges(bounds);
10148 // If we clip our children, clear accumulated overflow area in the affected
10149 // dimension(s). The children are actually clipped to the padding-box, but
10150 // since the overflow area should include the entire border-box, just set it
10151 // to the border-box size here.
10152 if (overflowClipAxes != PhysicalAxes::None) {
10153 aOverflowAreas.ApplyClipping(bounds, overflowClipAxes,
10154 OverflowClipMargin(overflowClipAxes));
10157 ComputeAndIncludeOutlineArea(this, aOverflowAreas, aNewSize);
10159 // Nothing in here should affect scrollable overflow.
10160 aOverflowAreas.InkOverflow() =
10161 ComputeEffectsRect(this, aOverflowAreas.InkOverflow(), aNewSize);
10163 // Absolute position clipping
10164 const nsStyleEffects* effects = StyleEffects();
10165 Maybe<nsRect> clipPropClipRect = GetClipPropClipRect(disp, effects, aNewSize);
10166 if (clipPropClipRect) {
10167 for (const auto otype : AllOverflowTypes()) {
10168 nsRect& o = aOverflowAreas.Overflow(otype);
10169 o.IntersectRect(o, *clipPropClipRect);
10173 /* If we're transformed, transform the overflow rect by the current
10174 * transformation. */
10175 if (hasTransform) {
10176 SetProperty(nsIFrame::PreTransformOverflowAreasProperty(),
10177 new OverflowAreas(aOverflowAreas));
10179 if (Combines3DTransformWithAncestors()) {
10180 /* If we're a preserve-3d leaf frame, then our pre-transform overflow
10181 * should be correct. Our post-transform overflow is empty though, because
10182 * we only contribute to the overflow area of the preserve-3d root frame.
10183 * If we're an intermediate frame then the pre-transform overflow should
10184 * contain all our non-preserve-3d children, which is what we want. Again
10185 * we have no post-transform overflow.
10187 aOverflowAreas.SetAllTo(nsRect());
10188 } else {
10189 TransformReferenceBox refBox(this);
10190 for (const auto otype : AllOverflowTypes()) {
10191 nsRect& o = aOverflowAreas.Overflow(otype);
10192 o = nsDisplayTransform::TransformRect(o, this, refBox);
10195 /* If we're the root of the 3d context, then we want to include the
10196 * overflow areas of all the participants. This won't have happened yet as
10197 * the code above set their overflow area to empty. Manually collect these
10198 * overflow areas now.
10200 if (Extend3DContext(disp, effects)) {
10201 ComputePreserve3DChildrenOverflow(aOverflowAreas);
10204 } else {
10205 RemoveProperty(nsIFrame::PreTransformOverflowAreasProperty());
10208 /* Revert the size change in case some caller is depending on this. */
10209 SetSize(oldSize, false);
10211 bool anyOverflowChanged;
10212 if (aOverflowAreas != OverflowAreas(bounds, bounds)) {
10213 anyOverflowChanged = SetOverflowAreas(aOverflowAreas);
10214 } else {
10215 anyOverflowChanged = ClearOverflowRects();
10218 if (anyOverflowChanged) {
10219 SVGObserverUtils::InvalidateDirectRenderingObservers(this);
10220 if (nsBlockFrame* block = do_QueryFrame(this)) {
10221 // NOTE(emilio): we need to use BeforeReflow::Yes, because we want to
10222 // invalidate in cases where we _used_ to have an overflow marker and no
10223 // longer do.
10224 if (TextOverflow::CanHaveOverflowMarkers(
10225 block, TextOverflow::BeforeReflow::Yes)) {
10226 DiscardDisplayItems(this, [](nsDisplayItem* aItem) {
10227 return aItem->GetType() == DisplayItemType::TYPE_TEXT_OVERFLOW;
10229 SchedulePaint(PAINT_DEFAULT);
10233 return anyOverflowChanged;
10236 void nsIFrame::RecomputePerspectiveChildrenOverflow(
10237 const nsIFrame* aStartFrame) {
10238 for (const auto& childList : ChildLists()) {
10239 for (nsIFrame* child : childList.mList) {
10240 if (!child->FrameMaintainsOverflow()) {
10241 continue; // frame does not maintain overflow rects
10243 if (child->HasPerspective()) {
10244 OverflowAreas* overflow =
10245 child->GetProperty(nsIFrame::InitialOverflowProperty());
10246 nsRect bounds(nsPoint(0, 0), child->GetSize());
10247 if (overflow) {
10248 OverflowAreas overflowCopy = *overflow;
10249 child->FinishAndStoreOverflow(overflowCopy, bounds.Size());
10250 } else {
10251 OverflowAreas boundsOverflow;
10252 boundsOverflow.SetAllTo(bounds);
10253 child->FinishAndStoreOverflow(boundsOverflow, bounds.Size());
10255 } else if (child->GetContent() == aStartFrame->GetContent() ||
10256 child->GetClosestFlattenedTreeAncestorPrimaryFrame() ==
10257 aStartFrame) {
10258 // If a frame is using perspective, then the size used to compute
10259 // perspective-origin is the size of the frame belonging to its parent
10260 // style. We must find any descendant frames using our size
10261 // (by recursing into frames that have the same containing block)
10262 // to update their overflow rects too.
10263 child->RecomputePerspectiveChildrenOverflow(aStartFrame);
10269 void nsIFrame::ComputePreserve3DChildrenOverflow(
10270 OverflowAreas& aOverflowAreas) {
10271 // Find all descendants that participate in the 3d context, and include their
10272 // overflow. These descendants have an empty overflow, so won't have been
10273 // included in the normal overflow calculation. Any children that don't
10274 // participate have normal overflow, so will have been included already.
10276 nsRect childVisual;
10277 nsRect childScrollable;
10278 for (const auto& childList : ChildLists()) {
10279 for (nsIFrame* child : childList.mList) {
10280 // If this child participates in the 3d context, then take the
10281 // pre-transform region (which contains all descendants that aren't
10282 // participating in the 3d context) and transform it into the 3d context
10283 // root coordinate space.
10284 if (child->Combines3DTransformWithAncestors()) {
10285 OverflowAreas childOverflow = child->GetOverflowAreasRelativeToSelf();
10286 TransformReferenceBox refBox(child);
10287 for (const auto otype : AllOverflowTypes()) {
10288 nsRect& o = childOverflow.Overflow(otype);
10289 o = nsDisplayTransform::TransformRect(o, child, refBox);
10292 aOverflowAreas.UnionWith(childOverflow);
10294 // If this child also extends the 3d context, then recurse into it
10295 // looking for more participants.
10296 if (child->Extend3DContext()) {
10297 child->ComputePreserve3DChildrenOverflow(aOverflowAreas);
10304 bool nsIFrame::ZIndexApplies() const {
10305 return StyleDisplay()->IsPositionedStyle() || IsFlexOrGridItem() ||
10306 IsMenuPopupFrame();
10309 Maybe<int32_t> nsIFrame::ZIndex() const {
10310 if (!ZIndexApplies()) {
10311 return Nothing();
10313 const auto& zIndex = StylePosition()->mZIndex;
10314 if (zIndex.IsAuto()) {
10315 return Nothing();
10317 return Some(zIndex.AsInteger());
10320 bool nsIFrame::IsScrollAnchor(ScrollAnchorContainer** aOutContainer) {
10321 if (!mInScrollAnchorChain) {
10322 return false;
10325 nsIFrame* f = this;
10327 // FIXME(emilio, bug 1629280): We should find a non-null anchor if we have the
10328 // flag set, but bug 1629280 makes it so that we cannot really assert it /
10329 // make this just a `while (true)`, and uncomment the below assertion.
10330 while (auto* container = ScrollAnchorContainer::FindFor(f)) {
10331 // MOZ_ASSERT(f->IsInScrollAnchorChain());
10332 if (nsIFrame* anchor = container->AnchorNode()) {
10333 if (anchor != this) {
10334 return false;
10336 if (aOutContainer) {
10337 *aOutContainer = container;
10339 return true;
10342 f = container->Frame();
10345 return false;
10348 bool nsIFrame::IsInScrollAnchorChain() const { return mInScrollAnchorChain; }
10350 void nsIFrame::SetInScrollAnchorChain(bool aInChain) {
10351 mInScrollAnchorChain = aInChain;
10354 uint32_t nsIFrame::GetDepthInFrameTree() const {
10355 uint32_t result = 0;
10356 for (nsContainerFrame* ancestor = GetParent(); ancestor;
10357 ancestor = ancestor->GetParent()) {
10358 result++;
10360 return result;
10364 * This function takes a frame that is part of a block-in-inline split,
10365 * and _if_ that frame is an anonymous block created by an ib split it
10366 * returns the block's preceding inline. This is needed because the
10367 * split inline's style is the parent of the anonymous block's style.
10369 * If aFrame is not an anonymous block, null is returned.
10371 static nsIFrame* GetIBSplitSiblingForAnonymousBlock(const nsIFrame* aFrame) {
10372 MOZ_ASSERT(aFrame, "Must have a non-null frame!");
10373 NS_ASSERTION(aFrame->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT),
10374 "GetIBSplitSibling should only be called on ib-split frames");
10376 if (aFrame->Style()->GetPseudoType() !=
10377 PseudoStyleType::mozBlockInsideInlineWrapper) {
10378 // it's not an anonymous block
10379 return nullptr;
10382 // Find the first continuation of the frame. (Ugh. This ends up
10383 // being O(N^2) when it is called O(N) times.)
10384 aFrame = aFrame->FirstContinuation();
10387 * Now look up the nsGkAtoms::IBSplitPrevSibling
10388 * property.
10390 nsIFrame* ibSplitSibling =
10391 aFrame->GetProperty(nsIFrame::IBSplitPrevSibling());
10392 NS_ASSERTION(ibSplitSibling, "Broken frame tree?");
10393 return ibSplitSibling;
10397 * Get the parent, corrected for the mangled frame tree resulting from
10398 * having a block within an inline. The result only differs from the
10399 * result of |GetParent| when |GetParent| returns an anonymous block
10400 * that was created for an element that was 'display: inline' because
10401 * that element contained a block.
10403 * Also skip anonymous scrolled-content parents; inherit directly from the
10404 * outer scroll frame.
10406 * Also skip NAC parents if the child frame is NAC.
10408 static nsIFrame* GetCorrectedParent(const nsIFrame* aFrame) {
10409 nsIFrame* parent = aFrame->GetParent();
10410 if (!parent) {
10411 return nullptr;
10414 // For a table caption we want the _inner_ table frame (unless it's anonymous)
10415 // as the style parent.
10416 if (aFrame->IsTableCaption()) {
10417 nsIFrame* innerTable = parent->PrincipalChildList().FirstChild();
10418 if (!innerTable->Style()->IsAnonBox()) {
10419 return innerTable;
10423 // Table wrappers are always anon boxes; if we're in here for an outer
10424 // table, that actually means its the _inner_ table that wants to
10425 // know its parent. So get the pseudo of the inner in that case.
10426 auto pseudo = aFrame->Style()->GetPseudoType();
10427 if (pseudo == PseudoStyleType::tableWrapper) {
10428 pseudo =
10429 aFrame->PrincipalChildList().FirstChild()->Style()->GetPseudoType();
10432 // Prevent a NAC pseudo-element from inheriting from its NAC parent, and
10433 // inherit from the NAC generator element instead.
10434 if (pseudo != PseudoStyleType::NotPseudo) {
10435 MOZ_ASSERT(aFrame->GetContent());
10436 Element* element = Element::FromNode(aFrame->GetContent());
10437 // Make sure to avoid doing the fixup for non-element-backed pseudos like
10438 // ::first-line and such.
10439 if (element && !element->IsRootOfNativeAnonymousSubtree() &&
10440 element->GetPseudoElementType() == aFrame->Style()->GetPseudoType()) {
10441 while (parent->GetContent() &&
10442 !parent->GetContent()->IsRootOfNativeAnonymousSubtree()) {
10443 parent = parent->GetInFlowParent();
10445 parent = parent->GetInFlowParent();
10449 return nsIFrame::CorrectStyleParentFrame(parent, pseudo);
10452 /* static */
10453 nsIFrame* nsIFrame::CorrectStyleParentFrame(nsIFrame* aProspectiveParent,
10454 PseudoStyleType aChildPseudo) {
10455 MOZ_ASSERT(aProspectiveParent, "Must have a prospective parent");
10457 if (aChildPseudo != PseudoStyleType::NotPseudo) {
10458 // Non-inheriting anon boxes have no style parent frame at all.
10459 if (PseudoStyle::IsNonInheritingAnonBox(aChildPseudo)) {
10460 return nullptr;
10463 // Other anon boxes are parented to their actual parent already, except
10464 // for non-elements. Those should not be treated as an anon box.
10465 if (PseudoStyle::IsAnonBox(aChildPseudo) &&
10466 !nsCSSAnonBoxes::IsNonElement(aChildPseudo)) {
10467 NS_ASSERTION(aChildPseudo != PseudoStyleType::mozBlockInsideInlineWrapper,
10468 "Should have dealt with kids that have "
10469 "NS_FRAME_PART_OF_IBSPLIT elsewhere");
10470 return aProspectiveParent;
10474 // Otherwise, walk up out of all anon boxes. For placeholder frames, walk out
10475 // of all pseudo-elements as well. Otherwise ReparentComputedStyle could
10476 // cause style data to be out of sync with the frame tree.
10477 nsIFrame* parent = aProspectiveParent;
10478 do {
10479 if (parent->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
10480 nsIFrame* sibling = GetIBSplitSiblingForAnonymousBlock(parent);
10482 if (sibling) {
10483 // |parent| was a block in an {ib} split; use the inline as
10484 // |the style parent.
10485 parent = sibling;
10489 if (!parent->Style()->IsPseudoOrAnonBox()) {
10490 return parent;
10493 if (!parent->Style()->IsAnonBox() && aChildPseudo != PseudoStyleType::MAX) {
10494 // nsPlaceholderFrame passes in PseudoStyleType::MAX for
10495 // aChildPseudo (even though that's not a valid pseudo-type) just to
10496 // trigger this behavior of walking up to the nearest non-pseudo
10497 // ancestor.
10498 return parent;
10501 parent = parent->GetInFlowParent();
10502 } while (parent);
10504 if (aProspectiveParent->Style()->GetPseudoType() ==
10505 PseudoStyleType::viewportScroll) {
10506 // aProspectiveParent is the scrollframe for a viewport
10507 // and the kids are the anonymous scrollbars
10508 return aProspectiveParent;
10511 // We can get here if the root element is absolutely positioned.
10512 // We can't test for this very accurately, but it can only happen
10513 // when the prospective parent is a canvas frame.
10514 NS_ASSERTION(aProspectiveParent->IsCanvasFrame(),
10515 "Should have found a parent before this");
10516 return nullptr;
10519 ComputedStyle* nsIFrame::DoGetParentComputedStyle(
10520 nsIFrame** aProviderFrame) const {
10521 *aProviderFrame = nullptr;
10523 // Handle display:contents and the root frame, when there's no parent frame
10524 // to inherit from.
10525 if (MOZ_LIKELY(mContent)) {
10526 Element* parentElement = mContent->GetFlattenedTreeParentElement();
10527 if (MOZ_LIKELY(parentElement)) {
10528 auto pseudo = Style()->GetPseudoType();
10529 if (pseudo == PseudoStyleType::NotPseudo || !mContent->IsElement() ||
10530 (!PseudoStyle::IsAnonBox(pseudo) &&
10531 // Ensure that we don't return the display:contents style
10532 // of the parent content for pseudos that have the same content
10533 // as their primary frame (like -moz-list-bullets do):
10534 IsPrimaryFrame()) ||
10535 /* if next is true then it's really a request for the table frame's
10536 parent context, see nsTable[Outer]Frame::GetParentComputedStyle. */
10537 pseudo == PseudoStyleType::tableWrapper) {
10538 // In some edge cases involving display: contents, we may end up here
10539 // for something that's pending to be reframed. In this case we return
10540 // the wrong style from here (because we've already lost track of it!),
10541 // but it's not a big deal as we're going to be reframed anyway.
10542 if (MOZ_LIKELY(parentElement->HasServoData()) &&
10543 Servo_Element_IsDisplayContents(parentElement)) {
10544 RefPtr<ComputedStyle> style =
10545 ServoStyleSet::ResolveServoStyle(*parentElement);
10546 // NOTE(emilio): we return a weak reference because the element also
10547 // holds the style context alive. This is a bit silly (we could've
10548 // returned a weak ref directly), but it's probably not worth
10549 // optimizing, given this function has just one caller which is rare,
10550 // and this path is rare itself.
10551 return style;
10554 } else {
10555 if (Style()->GetPseudoType() == PseudoStyleType::NotPseudo) {
10556 // We're a frame for the root. We have no style parent.
10557 return nullptr;
10562 if (!HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
10564 * If this frame is an anonymous block created when an inline with a block
10565 * inside it got split, then the parent style is on its preceding inline. We
10566 * can get to it using GetIBSplitSiblingForAnonymousBlock.
10568 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
10569 nsIFrame* ibSplitSibling = GetIBSplitSiblingForAnonymousBlock(this);
10570 if (ibSplitSibling) {
10571 return (*aProviderFrame = ibSplitSibling)->Style();
10575 // If this frame is one of the blocks that split an inline, we must
10576 // return the "special" inline parent, i.e., the parent that this
10577 // frame would have if we didn't mangle the frame structure.
10578 *aProviderFrame = GetCorrectedParent(this);
10579 return *aProviderFrame ? (*aProviderFrame)->Style() : nullptr;
10582 // We're an out-of-flow frame. For out-of-flow frames, we must
10583 // resolve underneath the placeholder's parent. The placeholder is
10584 // reached from the first-in-flow.
10585 nsPlaceholderFrame* placeholder = FirstInFlow()->GetPlaceholderFrame();
10586 if (!placeholder) {
10587 MOZ_ASSERT_UNREACHABLE("no placeholder frame for out-of-flow frame");
10588 *aProviderFrame = GetCorrectedParent(this);
10589 return *aProviderFrame ? (*aProviderFrame)->Style() : nullptr;
10591 return placeholder->GetParentComputedStyleForOutOfFlow(aProviderFrame);
10594 void nsIFrame::GetLastLeaf(nsIFrame** aFrame) {
10595 if (!aFrame || !*aFrame) return;
10596 nsIFrame* child = *aFrame;
10597 // if we are a block frame then go for the last line of 'this'
10598 while (1) {
10599 child = child->PrincipalChildList().FirstChild();
10600 if (!child) return; // nothing to do
10601 nsIFrame* siblingFrame;
10602 nsIContent* content;
10603 // ignore anonymous elements, e.g. mozTableAdd* mozTableRemove*
10604 // see bug 278197 comment #12 #13 for details
10605 while ((siblingFrame = child->GetNextSibling()) &&
10606 (content = siblingFrame->GetContent()) &&
10607 !content->IsRootOfNativeAnonymousSubtree())
10608 child = siblingFrame;
10609 *aFrame = child;
10613 void nsIFrame::GetFirstLeaf(nsIFrame** aFrame) {
10614 if (!aFrame || !*aFrame) return;
10615 nsIFrame* child = *aFrame;
10616 while (1) {
10617 child = child->PrincipalChildList().FirstChild();
10618 if (!child) return; // nothing to do
10619 *aFrame = child;
10623 bool nsIFrame::IsFocusableDueToScrollFrame() {
10624 if (!IsScrollFrame()) {
10625 if (nsFieldSetFrame* fieldset = do_QueryFrame(this)) {
10626 // TODO: Do we have similar special-cases like this where we can have
10627 // anonymous scrollable boxes hanging off a primary frame?
10628 if (nsIFrame* inner = fieldset->GetInner()) {
10629 return inner->IsFocusableDueToScrollFrame();
10632 return false;
10634 if (!mContent->IsHTMLElement()) {
10635 return false;
10637 if (mContent->IsRootOfNativeAnonymousSubtree()) {
10638 return false;
10640 if (!mContent->GetParent()) {
10641 return false;
10643 if (mContent->AsElement()->HasAttr(nsGkAtoms::tabindex)) {
10644 return false;
10646 // Elements with scrollable view are focusable with script & tabbable
10647 // Otherwise you couldn't scroll them with keyboard, which is an accessibility
10648 // issue (e.g. Section 508 rules) However, we don't make them to be focusable
10649 // with the mouse, because the extra focus outlines are considered
10650 // unnecessarily ugly. When clicked on, the selection position within the
10651 // element will be enough to make them keyboard scrollable.
10652 nsIScrollableFrame* scrollFrame = do_QueryFrame(this);
10653 if (!scrollFrame) {
10654 return false;
10656 if (scrollFrame->IsForTextControlWithNoScrollbars()) {
10657 return false;
10659 if (scrollFrame->GetScrollStyles().IsHiddenInBothDirections()) {
10660 return false;
10662 if (scrollFrame->GetScrollRange().IsEqualEdges(nsRect(0, 0, 0, 0))) {
10663 return false;
10665 return true;
10668 nsIFrame::Focusable nsIFrame::IsFocusable(bool aWithMouse,
10669 bool aCheckVisibility) {
10670 // cannot focus content in print preview mode. Only the root can be focused,
10671 // but that's handled elsewhere.
10672 if (PresContext()->Type() == nsPresContext::eContext_PrintPreview) {
10673 return {};
10676 if (!mContent || !mContent->IsElement()) {
10677 return {};
10680 if (aCheckVisibility && !IsVisibleConsideringAncestors()) {
10681 return {};
10684 const nsStyleUI& ui = *StyleUI();
10685 if (ui.IsInert()) {
10686 return {};
10689 PseudoStyleType pseudo = Style()->GetPseudoType();
10690 if (pseudo == PseudoStyleType::anonymousItem) {
10691 return {};
10694 int32_t tabIndex = -1;
10695 if (ui.UserFocus() != StyleUserFocus::Ignore &&
10696 ui.UserFocus() != StyleUserFocus::None) {
10697 // Pass in default tabindex of -1 for nonfocusable and 0 for focusable
10698 tabIndex = 0;
10701 if (mContent->IsFocusable(&tabIndex, aWithMouse)) {
10702 // If the content is focusable, then we're done.
10703 return {true, tabIndex};
10706 // If we're focusing with the mouse we never focus scroll areas.
10707 if (!aWithMouse && IsFocusableDueToScrollFrame()) {
10708 return {true, 0};
10711 return {false, tabIndex};
10715 * @return true if this text frame ends with a newline character which is
10716 * treated as preformatted. It should return false if this is not a text frame.
10718 bool nsIFrame::HasSignificantTerminalNewline() const { return false; }
10720 static StyleVerticalAlignKeyword ConvertSVGDominantBaselineToVerticalAlign(
10721 StyleDominantBaseline aDominantBaseline) {
10722 // Most of these are approximate mappings.
10723 switch (aDominantBaseline) {
10724 case StyleDominantBaseline::Hanging:
10725 case StyleDominantBaseline::TextBeforeEdge:
10726 return StyleVerticalAlignKeyword::TextTop;
10727 case StyleDominantBaseline::TextAfterEdge:
10728 case StyleDominantBaseline::Ideographic:
10729 return StyleVerticalAlignKeyword::TextBottom;
10730 case StyleDominantBaseline::Central:
10731 case StyleDominantBaseline::Middle:
10732 case StyleDominantBaseline::Mathematical:
10733 return StyleVerticalAlignKeyword::Middle;
10734 case StyleDominantBaseline::Auto:
10735 case StyleDominantBaseline::Alphabetic:
10736 return StyleVerticalAlignKeyword::Baseline;
10737 default:
10738 MOZ_ASSERT_UNREACHABLE("unexpected aDominantBaseline value");
10739 return StyleVerticalAlignKeyword::Baseline;
10743 Maybe<StyleVerticalAlignKeyword> nsIFrame::VerticalAlignEnum() const {
10744 if (IsInSVGTextSubtree()) {
10745 StyleDominantBaseline dominantBaseline = StyleSVG()->mDominantBaseline;
10746 return Some(ConvertSVGDominantBaselineToVerticalAlign(dominantBaseline));
10749 const auto& verticalAlign = StyleDisplay()->mVerticalAlign;
10750 if (verticalAlign.IsKeyword()) {
10751 return Some(verticalAlign.AsKeyword());
10754 return Nothing();
10757 void nsIFrame::UpdateStyleOfChildAnonBox(nsIFrame* aChildFrame,
10758 ServoRestyleState& aRestyleState) {
10759 #ifdef DEBUG
10760 nsIFrame* parent = aChildFrame->GetInFlowParent();
10761 if (aChildFrame->IsTableFrame()) {
10762 parent = parent->GetParent();
10764 if (parent->IsLineFrame()) {
10765 parent = parent->GetParent();
10767 MOZ_ASSERT(nsLayoutUtils::FirstContinuationOrIBSplitSibling(parent) == this,
10768 "This should only be used for children!");
10769 #endif // DEBUG
10770 MOZ_ASSERT(!GetContent() || !aChildFrame->GetContent() ||
10771 aChildFrame->GetContent() == GetContent(),
10772 "What content node is it a frame for?");
10773 MOZ_ASSERT(!aChildFrame->GetPrevContinuation(),
10774 "Only first continuations should end up here");
10776 // We could force the caller to pass in the pseudo, since some callers know it
10777 // statically... But this API is a bit nicer.
10778 auto pseudo = aChildFrame->Style()->GetPseudoType();
10779 MOZ_ASSERT(PseudoStyle::IsAnonBox(pseudo), "Child is not an anon box?");
10780 MOZ_ASSERT(!PseudoStyle::IsNonInheritingAnonBox(pseudo),
10781 "Why did the caller bother calling us?");
10783 // Anon boxes inherit from their parent; that's us.
10784 RefPtr<ComputedStyle> newContext =
10785 aRestyleState.StyleSet().ResolveInheritingAnonymousBoxStyle(pseudo,
10786 Style());
10788 nsChangeHint childHint =
10789 UpdateStyleOfOwnedChildFrame(aChildFrame, newContext, aRestyleState);
10791 // Now that we've updated the style on aChildFrame, check whether it itself
10792 // has anon boxes to deal with.
10793 ServoRestyleState childrenState(*aChildFrame, aRestyleState, childHint,
10794 ServoRestyleState::Type::InFlow);
10795 aChildFrame->UpdateStyleOfOwnedAnonBoxes(childrenState);
10797 // Assuming anon boxes don't have ::backdrop associated with them... if that
10798 // ever changes, we'd need to handle that here, like we do in
10799 // RestyleManager::ProcessPostTraversal
10801 // We do need to handle block pseudo-elements here, though. Especially list
10802 // bullets.
10803 if (nsBlockFrame* block = do_QueryFrame(aChildFrame)) {
10804 block->UpdatePseudoElementStyles(childrenState);
10808 /* static */
10809 nsChangeHint nsIFrame::UpdateStyleOfOwnedChildFrame(
10810 nsIFrame* aChildFrame, ComputedStyle* aNewComputedStyle,
10811 ServoRestyleState& aRestyleState,
10812 const Maybe<ComputedStyle*>& aContinuationComputedStyle) {
10813 MOZ_ASSERT(!aChildFrame->GetAdditionalComputedStyle(0),
10814 "We don't handle additional styles here");
10816 // Figure out whether we have an actual change. It's important that we do
10817 // this, for several reasons:
10819 // 1) Even if all the child's changes are due to properties it inherits from
10820 // us, it's possible that no one ever asked us for those style structs and
10821 // hence changes to them aren't reflected in the changes handled at all.
10823 // 2) Content can change stylesheets that change the styles of pseudos, and
10824 // extensions can add/remove stylesheets that change the styles of
10825 // anonymous boxes directly.
10826 uint32_t equalStructs; // Not used, actually.
10827 nsChangeHint childHint = aChildFrame->Style()->CalcStyleDifference(
10828 *aNewComputedStyle, &equalStructs);
10830 // If aChildFrame is out of flow, then aRestyleState's "changes handled by the
10831 // parent" doesn't apply to it, because it may have some other parent in the
10832 // frame tree.
10833 if (!aChildFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
10834 childHint = NS_RemoveSubsumedHints(
10835 childHint, aRestyleState.ChangesHandledFor(aChildFrame));
10837 if (childHint) {
10838 if (childHint & nsChangeHint_ReconstructFrame) {
10839 // If we generate a reconstruct here, remove any non-reconstruct hints we
10840 // may have already generated for this content.
10841 aRestyleState.ChangeList().PopChangesForContent(
10842 aChildFrame->GetContent());
10844 aRestyleState.ChangeList().AppendChange(
10845 aChildFrame, aChildFrame->GetContent(), childHint);
10848 aChildFrame->SetComputedStyle(aNewComputedStyle);
10849 ComputedStyle* continuationStyle = aContinuationComputedStyle
10850 ? *aContinuationComputedStyle
10851 : aNewComputedStyle;
10852 for (nsIFrame* kid = aChildFrame->GetNextContinuation(); kid;
10853 kid = kid->GetNextContinuation()) {
10854 MOZ_ASSERT(!kid->GetAdditionalComputedStyle(0));
10855 kid->SetComputedStyle(continuationStyle);
10858 return childHint;
10861 /* static */
10862 void nsIFrame::AddInPopupStateBitToDescendants(nsIFrame* aFrame) {
10863 if (!aFrame->HasAnyStateBits(NS_FRAME_IN_POPUP) &&
10864 aFrame->TrackingVisibility()) {
10865 // Assume all frames in popups are visible.
10866 aFrame->IncApproximateVisibleCount();
10869 aFrame->AddStateBits(NS_FRAME_IN_POPUP);
10871 for (const auto& childList : aFrame->CrossDocChildLists()) {
10872 for (nsIFrame* child : childList.mList) {
10873 AddInPopupStateBitToDescendants(child);
10878 /* static */
10879 void nsIFrame::RemoveInPopupStateBitFromDescendants(nsIFrame* aFrame) {
10880 if (!aFrame->HasAnyStateBits(NS_FRAME_IN_POPUP) ||
10881 nsLayoutUtils::IsPopup(aFrame)) {
10882 return;
10885 aFrame->RemoveStateBits(NS_FRAME_IN_POPUP);
10887 if (aFrame->TrackingVisibility()) {
10888 // We assume all frames in popups are visible, so this decrement balances
10889 // out the increment in AddInPopupStateBitToDescendants above.
10890 aFrame->DecApproximateVisibleCount();
10892 for (const auto& childList : aFrame->CrossDocChildLists()) {
10893 for (nsIFrame* child : childList.mList) {
10894 RemoveInPopupStateBitFromDescendants(child);
10899 void nsIFrame::SetParent(nsContainerFrame* aParent) {
10900 // If our parent is a wrapper anon box, our new parent should be too. We
10901 // _can_ change parent if our parent is a wrapper anon box, because some
10902 // wrapper anon boxes can have continuations.
10903 MOZ_ASSERT_IF(ParentIsWrapperAnonBox(),
10904 aParent->Style()->IsInheritingAnonBox());
10906 // Note that the current mParent may already be destroyed at this point.
10907 mParent = aParent;
10908 MOZ_DIAGNOSTIC_ASSERT(!mParent || PresShell() == mParent->PresShell());
10910 if (HasAnyStateBits(NS_FRAME_HAS_VIEW | NS_FRAME_HAS_CHILD_WITH_VIEW)) {
10911 for (nsIFrame* f = aParent;
10912 f && !f->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
10913 f = f->GetParent()) {
10914 f->AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
10918 if (HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
10919 for (nsIFrame* f = aParent; f; f = f->GetParent()) {
10920 if (f->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
10921 break;
10923 f->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE);
10927 if (HasAnyStateBits(NS_FRAME_DESCENDANT_INTRINSIC_ISIZE_DEPENDS_ON_BSIZE)) {
10928 for (nsIFrame* f = aParent; f; f = f->GetParent()) {
10929 if (f->HasAnyStateBits(
10930 NS_FRAME_DESCENDANT_INTRINSIC_ISIZE_DEPENDS_ON_BSIZE)) {
10931 break;
10933 f->AddStateBits(NS_FRAME_DESCENDANT_INTRINSIC_ISIZE_DEPENDS_ON_BSIZE);
10937 if (HasInvalidFrameInSubtree()) {
10938 for (nsIFrame* f = aParent;
10939 f && !f->HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT |
10940 NS_FRAME_IS_NONDISPLAY);
10941 f = nsLayoutUtils::GetCrossDocParentFrameInProcess(f)) {
10942 f->AddStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT);
10946 if (aParent->HasAnyStateBits(NS_FRAME_IN_POPUP)) {
10947 AddInPopupStateBitToDescendants(this);
10948 } else {
10949 RemoveInPopupStateBitFromDescendants(this);
10952 // If our new parent only has invalid children, then we just invalidate
10953 // ourselves too. This is probably faster than clearing the flag all
10954 // the way up the frame tree.
10955 if (aParent->HasAnyStateBits(NS_FRAME_ALL_DESCENDANTS_NEED_PAINT)) {
10956 InvalidateFrame();
10957 } else {
10958 SchedulePaint();
10962 bool nsIFrame::IsStackingContext(const nsStyleDisplay* aStyleDisplay,
10963 const nsStyleEffects* aStyleEffects) {
10964 // Properties that influence the output of this function should be handled in
10965 // change_bits_for_longhand as well.
10966 if (HasOpacity(aStyleDisplay, aStyleEffects, nullptr)) {
10967 return true;
10969 if (IsTransformed()) {
10970 return true;
10972 auto willChange = aStyleDisplay->mWillChange.bits;
10973 if (aStyleDisplay->IsContainPaint() || aStyleDisplay->IsContainLayout() ||
10974 willChange & StyleWillChangeBits::CONTAIN) {
10975 if (SupportsContainLayoutAndPaint()) {
10976 return true;
10979 // strictly speaking, 'perspective' doesn't require visual atomicity,
10980 // but the spec says it acts like the rest of these
10981 if (aStyleDisplay->HasPerspectiveStyle() ||
10982 willChange & StyleWillChangeBits::PERSPECTIVE) {
10983 if (SupportsCSSTransforms()) {
10984 return true;
10987 if (!StylePosition()->mZIndex.IsAuto() ||
10988 willChange & StyleWillChangeBits::Z_INDEX) {
10989 if (ZIndexApplies()) {
10990 return true;
10993 return aStyleEffects->mMixBlendMode != StyleBlend::Normal ||
10994 SVGIntegrationUtils::UsingEffectsForFrame(this) ||
10995 aStyleDisplay->IsPositionForcingStackingContext() ||
10996 aStyleDisplay->mIsolation != StyleIsolation::Auto ||
10997 willChange & StyleWillChangeBits::STACKING_CONTEXT_UNCONDITIONAL;
11000 bool nsIFrame::IsStackingContext() {
11001 return IsStackingContext(StyleDisplay(), StyleEffects());
11004 static bool IsFrameScrolledOutOfView(const nsIFrame* aTarget,
11005 const nsRect& aTargetRect,
11006 const nsIFrame* aParent) {
11007 // The ancestor frame we are checking if it clips out aTargetRect relative to
11008 // aTarget.
11009 nsIFrame* clipParent = nullptr;
11011 // find the first scrollable frame or root frame if we are in a fixed pos
11012 // subtree
11013 for (nsIFrame* f = const_cast<nsIFrame*>(aParent); f;
11014 f = nsLayoutUtils::GetCrossDocParentFrameInProcess(f)) {
11015 nsIScrollableFrame* scrollableFrame = do_QueryFrame(f);
11016 if (scrollableFrame) {
11017 clipParent = f;
11018 break;
11020 if (f->StyleDisplay()->mPosition == StylePositionProperty::Fixed &&
11021 nsLayoutUtils::IsReallyFixedPos(f)) {
11022 clipParent = f->GetParent();
11023 break;
11027 if (!clipParent) {
11028 // Even if we couldn't find the nearest scrollable frame, it might mean we
11029 // are in an out-of-process iframe, try to see if |aTarget| frame is
11030 // scrolled out of view in an scrollable frame in a cross-process ancestor
11031 // document.
11032 return nsLayoutUtils::FrameIsScrolledOutOfViewInCrossProcess(aTarget);
11035 nsRect clipRect = clipParent->InkOverflowRectRelativeToSelf();
11036 // We consider that the target is scrolled out if the scrollable (or root)
11037 // frame is empty.
11038 if (clipRect.IsEmpty()) {
11039 return true;
11042 nsRect transformedRect = nsLayoutUtils::TransformFrameRectToAncestor(
11043 aTarget, aTargetRect, clipParent);
11045 if (transformedRect.IsEmpty()) {
11046 // If the transformed rect is empty it represents a line or a point that we
11047 // should check is outside the the scrollable rect.
11048 if (transformedRect.x > clipRect.XMost() ||
11049 transformedRect.y > clipRect.YMost() ||
11050 clipRect.x > transformedRect.XMost() ||
11051 clipRect.y > transformedRect.YMost()) {
11052 return true;
11054 } else if (!transformedRect.Intersects(clipRect)) {
11055 return true;
11058 nsIFrame* parent = clipParent->GetParent();
11059 if (!parent) {
11060 return false;
11063 return IsFrameScrolledOutOfView(aTarget, aTargetRect, parent);
11066 bool nsIFrame::IsScrolledOutOfView() const {
11067 nsRect rect = InkOverflowRectRelativeToSelf();
11068 return IsFrameScrolledOutOfView(this, rect, this);
11071 gfx::Matrix nsIFrame::ComputeWidgetTransform() const {
11072 const nsStyleUIReset* uiReset = StyleUIReset();
11073 if (uiReset->mMozWindowTransform.IsNone()) {
11074 return gfx::Matrix();
11077 TransformReferenceBox refBox(nullptr, nsRect(nsPoint(), GetSize()));
11079 nsPresContext* presContext = PresContext();
11080 int32_t appUnitsPerDevPixel = presContext->AppUnitsPerDevPixel();
11081 gfx::Matrix4x4 matrix = nsStyleTransformMatrix::ReadTransforms(
11082 uiReset->mMozWindowTransform, refBox, float(appUnitsPerDevPixel));
11084 // Apply the -moz-window-transform-origin translation to the matrix.
11085 const StyleTransformOrigin& origin = uiReset->mWindowTransformOrigin;
11086 Point transformOrigin = nsStyleTransformMatrix::Convert2DPosition(
11087 origin.horizontal, origin.vertical, refBox, appUnitsPerDevPixel);
11088 matrix.ChangeBasis(Point3D(transformOrigin.x, transformOrigin.y, 0));
11090 gfx::Matrix result2d;
11091 if (!matrix.CanDraw2D(&result2d)) {
11092 // FIXME: It would be preferable to reject non-2D transforms at parse time.
11093 NS_WARNING(
11094 "-moz-window-transform does not describe a 2D transform, "
11095 "but only 2d transforms are supported");
11096 return gfx::Matrix();
11099 return result2d;
11102 void nsIFrame::DoUpdateStyleOfOwnedAnonBoxes(ServoRestyleState& aRestyleState) {
11103 // As a special case, we check for {ib}-split block frames here, rather
11104 // than have an nsInlineFrame::AppendDirectlyOwnedAnonBoxes implementation
11105 // that returns them.
11107 // (If we did handle them in AppendDirectlyOwnedAnonBoxes, we would have to
11108 // return *all* of the in-flow {ib}-split block frames, not just the first
11109 // one. For restyling, we really just need the first in flow, and the other
11110 // user of the AppendOwnedAnonBoxes API, AllChildIterator, doesn't need to
11111 // know about them at all, since these block frames never create NAC. So we
11112 // avoid any unncessary hashtable lookups for the {ib}-split frames by calling
11113 // UpdateStyleOfOwnedAnonBoxesForIBSplit directly here.)
11114 if (IsInlineFrame()) {
11115 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
11116 static_cast<nsInlineFrame*>(this)->UpdateStyleOfOwnedAnonBoxesForIBSplit(
11117 aRestyleState);
11119 return;
11122 AutoTArray<OwnedAnonBox, 4> frames;
11123 AppendDirectlyOwnedAnonBoxes(frames);
11124 for (OwnedAnonBox& box : frames) {
11125 if (box.mUpdateStyleFn) {
11126 box.mUpdateStyleFn(this, box.mAnonBoxFrame, aRestyleState);
11127 } else {
11128 UpdateStyleOfChildAnonBox(box.mAnonBoxFrame, aRestyleState);
11133 /* virtual */
11134 void nsIFrame::AppendDirectlyOwnedAnonBoxes(nsTArray<OwnedAnonBox>& aResult) {
11135 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES));
11136 MOZ_ASSERT(false, "Why did this get called?");
11139 void nsIFrame::DoAppendOwnedAnonBoxes(nsTArray<OwnedAnonBox>& aResult) {
11140 size_t i = aResult.Length();
11141 AppendDirectlyOwnedAnonBoxes(aResult);
11143 // After appending the directly owned anonymous boxes of this frame to
11144 // aResult above, we need to check each of them to see if they own
11145 // any anonymous boxes themselves. Note that we keep progressing
11146 // through aResult, looking for additional entries in aResult from these
11147 // subsequent AppendDirectlyOwnedAnonBoxes calls. (Thus we can't
11148 // use a ranged for loop here.)
11150 while (i < aResult.Length()) {
11151 nsIFrame* f = aResult[i].mAnonBoxFrame;
11152 if (f->HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES)) {
11153 f->AppendDirectlyOwnedAnonBoxes(aResult);
11155 ++i;
11159 nsIFrame::CaretPosition::CaretPosition() : mContentOffset(0) {}
11161 nsIFrame::CaretPosition::~CaretPosition() = default;
11163 bool nsIFrame::HasCSSAnimations() {
11164 auto* collection = AnimationCollection<CSSAnimation>::Get(this);
11165 return collection && !collection->mAnimations.IsEmpty();
11168 bool nsIFrame::HasCSSTransitions() {
11169 auto* collection = AnimationCollection<CSSTransition>::Get(this);
11170 return collection && !collection->mAnimations.IsEmpty();
11173 void nsIFrame::AddSizeOfExcludingThisForTree(nsWindowSizes& aSizes) const {
11174 aSizes.mLayoutFramePropertiesSize +=
11175 mProperties.SizeOfExcludingThis(aSizes.mState.mMallocSizeOf);
11177 // We don't do this for Gecko because this stuff is stored in the nsPresArena
11178 // and so measured elsewhere.
11179 if (!aSizes.mState.HaveSeenPtr(mComputedStyle)) {
11180 mComputedStyle->AddSizeOfIncludingThis(aSizes,
11181 &aSizes.mLayoutComputedValuesNonDom);
11184 // And our additional styles.
11185 int32_t index = 0;
11186 while (auto* extra = GetAdditionalComputedStyle(index++)) {
11187 if (!aSizes.mState.HaveSeenPtr(extra)) {
11188 extra->AddSizeOfIncludingThis(aSizes,
11189 &aSizes.mLayoutComputedValuesNonDom);
11193 for (const auto& childList : ChildLists()) {
11194 for (const nsIFrame* f : childList.mList) {
11195 f->AddSizeOfExcludingThisForTree(aSizes);
11200 nsRect nsIFrame::GetCompositorHitTestArea(nsDisplayListBuilder* aBuilder) {
11201 nsRect area;
11203 nsIScrollableFrame* scrollFrame = nsLayoutUtils::GetScrollableFrameFor(this);
11204 if (scrollFrame) {
11205 // If the frame is content of a scrollframe, then we need to pick up the
11206 // area corresponding to the overflow rect as well. Otherwise the parts of
11207 // the overflow that are not occupied by descendants get skipped and the
11208 // APZ code sends touch events to the content underneath instead.
11209 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1127773#c15.
11210 area = ScrollableOverflowRect();
11211 } else {
11212 area = GetRectRelativeToSelf();
11215 if (!area.IsEmpty()) {
11216 return area + aBuilder->ToReferenceFrame(this);
11219 return area;
11222 CompositorHitTestInfo nsIFrame::GetCompositorHitTestInfo(
11223 nsDisplayListBuilder* aBuilder) {
11224 CompositorHitTestInfo result = CompositorHitTestInvisibleToHit;
11226 if (aBuilder->IsInsidePointerEventsNoneDoc()) {
11227 // Somewhere up the parent document chain is a subdocument with pointer-
11228 // events:none set on it.
11229 return result;
11231 if (!GetParent()) {
11232 MOZ_ASSERT(IsViewportFrame());
11233 // Viewport frames are never event targets, other frames, like canvas
11234 // frames, are the event targets for any regions viewport frames may cover.
11235 return result;
11237 if (Style()->PointerEvents() == StylePointerEvents::None) {
11238 return result;
11240 if (!StyleVisibility()->IsVisible()) {
11241 return result;
11244 // Anything that didn't match the above conditions is visible to hit-testing.
11245 result = CompositorHitTestFlags::eVisibleToHitTest;
11246 SVGUtils::MaskUsage maskUsage = SVGUtils::DetermineMaskUsage(this, false);
11247 if (maskUsage.UsingMaskOrClipPath()) {
11248 // If WebRender is enabled, simple clip-paths can be converted into WR
11249 // clips that WR knows how to hit-test against, so we don't need to mark
11250 // it as an irregular area.
11251 if (!maskUsage.IsSimpleClipShape()) {
11252 result += CompositorHitTestFlags::eIrregularArea;
11256 if (aBuilder->IsBuildingNonLayerizedScrollbar()) {
11257 // Scrollbars may be painted into a layer below the actual layer they will
11258 // scroll, and therefore wheel events may be dispatched to the outer frame
11259 // instead of the intended scrollframe. To address this, we force a d-t-c
11260 // region on scrollbar frames that won't be placed in their own layer. See
11261 // bug 1213324 for details.
11262 result += CompositorHitTestFlags::eInactiveScrollframe;
11263 } else if (aBuilder->GetAncestorHasApzAwareEventHandler()) {
11264 result += CompositorHitTestFlags::eApzAwareListeners;
11265 } else if (IsRangeFrame()) {
11266 // Range frames handle touch events directly without having a touch listener
11267 // so we need to let APZ know that this area cares about events.
11268 result += CompositorHitTestFlags::eApzAwareListeners;
11271 if (aBuilder->IsTouchEventPrefEnabledDoc()) {
11272 // Inherit the touch-action flags from the parent, if there is one. We do
11273 // this because of how the touch-action on a frame combines the touch-action
11274 // from ancestor DOM elements. Refer to the documentation in
11275 // TouchActionHelper.cpp for details; this code is meant to be equivalent to
11276 // that code, but woven into the top-down recursive display list building
11277 // process.
11278 CompositorHitTestInfo inheritedTouchAction =
11279 aBuilder->GetCompositorHitTestInfo() & CompositorHitTestTouchActionMask;
11281 nsIFrame* touchActionFrame = this;
11282 if (nsIScrollableFrame* scrollFrame =
11283 nsLayoutUtils::GetScrollableFrameFor(this)) {
11284 ScrollStyles ss = scrollFrame->GetScrollStyles();
11285 if (ss.mVertical != StyleOverflow::Hidden ||
11286 ss.mHorizontal != StyleOverflow::Hidden) {
11287 touchActionFrame = do_QueryFrame(scrollFrame);
11288 // On scrollframes, stop inheriting the pan-x and pan-y flags; instead,
11289 // reset them back to zero to allow panning on the scrollframe unless we
11290 // encounter an element that disables it that's inside the scrollframe.
11291 // This is equivalent to the |considerPanning| variable in
11292 // TouchActionHelper.cpp, but for a top-down traversal.
11293 CompositorHitTestInfo panMask(
11294 CompositorHitTestFlags::eTouchActionPanXDisabled,
11295 CompositorHitTestFlags::eTouchActionPanYDisabled);
11296 inheritedTouchAction -= panMask;
11300 result += inheritedTouchAction;
11302 const StyleTouchAction touchAction = touchActionFrame->UsedTouchAction();
11303 // The CSS allows the syntax auto | none | [pan-x || pan-y] | manipulation
11304 // so we can eliminate some combinations of things.
11305 if (touchAction == StyleTouchAction::AUTO) {
11306 // nothing to do
11307 } else if (touchAction & StyleTouchAction::MANIPULATION) {
11308 result += CompositorHitTestFlags::eTouchActionAnimatingZoomDisabled;
11309 } else {
11310 // This path handles the cases none | [pan-x || pan-y || pinch-zoom] so
11311 // double-tap is disabled in here.
11312 if (!(touchAction & StyleTouchAction::PINCH_ZOOM)) {
11313 result += CompositorHitTestFlags::eTouchActionPinchZoomDisabled;
11316 result += CompositorHitTestFlags::eTouchActionAnimatingZoomDisabled;
11318 if (!(touchAction & StyleTouchAction::PAN_X)) {
11319 result += CompositorHitTestFlags::eTouchActionPanXDisabled;
11321 if (!(touchAction & StyleTouchAction::PAN_Y)) {
11322 result += CompositorHitTestFlags::eTouchActionPanYDisabled;
11324 if (touchAction & StyleTouchAction::NONE) {
11325 // all the touch-action disabling flags will already have been set above
11326 MOZ_ASSERT(result.contains(CompositorHitTestTouchActionMask));
11331 const Maybe<ScrollDirection> scrollDirection =
11332 aBuilder->GetCurrentScrollbarDirection();
11333 if (scrollDirection.isSome()) {
11334 if (GetContent()->IsXULElement(nsGkAtoms::thumb)) {
11335 const bool thumbGetsLayer = aBuilder->GetCurrentScrollbarTarget() !=
11336 layers::ScrollableLayerGuid::NULL_SCROLL_ID;
11337 if (thumbGetsLayer) {
11338 result += CompositorHitTestFlags::eScrollbarThumb;
11339 } else {
11340 result += CompositorHitTestFlags::eInactiveScrollframe;
11344 if (*scrollDirection == ScrollDirection::eVertical) {
11345 result += CompositorHitTestFlags::eScrollbarVertical;
11348 // includes the ScrollbarFrame, SliderFrame, anything else that
11349 // might be inside the xul:scrollbar
11350 result += CompositorHitTestFlags::eScrollbar;
11353 return result;
11356 // Returns true if we can guarantee there is no visible descendants.
11357 static bool HasNoVisibleDescendants(const nsIFrame* aFrame) {
11358 for (const auto& childList : aFrame->ChildLists()) {
11359 for (nsIFrame* f : childList.mList) {
11360 if (nsPlaceholderFrame::GetRealFrameFor(f)
11361 ->IsVisibleOrMayHaveVisibleDescendants()) {
11362 return false;
11366 return true;
11369 void nsIFrame::UpdateVisibleDescendantsState() {
11370 if (StyleVisibility()->IsVisible()) {
11371 // Notify invisible ancestors that a visible descendant exists now.
11372 nsIFrame* ancestor;
11373 for (ancestor = GetInFlowParent();
11374 ancestor && !ancestor->StyleVisibility()->IsVisible();
11375 ancestor = ancestor->GetInFlowParent()) {
11376 ancestor->mAllDescendantsAreInvisible = false;
11378 } else {
11379 mAllDescendantsAreInvisible = HasNoVisibleDescendants(this);
11383 void nsIFrame::UpdateAnimationVisibility() {
11384 auto* animationCollection = AnimationCollection<CSSAnimation>::Get(this);
11385 auto* transitionCollection = AnimationCollection<CSSTransition>::Get(this);
11387 if ((!animationCollection || animationCollection->mAnimations.IsEmpty()) &&
11388 (!transitionCollection || transitionCollection->mAnimations.IsEmpty())) {
11389 return;
11392 bool hidden = IsHiddenByContentVisibilityOnAnyAncestor();
11393 if (animationCollection) {
11394 for (auto& animation : animationCollection->mAnimations) {
11395 animation->SetHiddenByContentVisibility(hidden);
11399 if (transitionCollection) {
11400 for (auto& transition : transitionCollection->mAnimations) {
11401 transition->SetHiddenByContentVisibility(hidden);
11406 nsIFrame::PhysicalAxes nsIFrame::ShouldApplyOverflowClipping(
11407 const nsStyleDisplay* aDisp) const {
11408 MOZ_ASSERT(aDisp == StyleDisplay(), "Wrong display struct");
11410 // 'contain:paint', which we handle as 'overflow:clip' here. Except for
11411 // scrollframes we don't need contain:paint to add any clipping, because
11412 // the scrollable frame will already clip overflowing content, and because
11413 // 'contain:paint' should prevent all means of escaping that clipping
11414 // (e.g. because it forms a fixed-pos containing block).
11415 if (aDisp->IsContainPaint() && !IsScrollFrame() &&
11416 SupportsContainLayoutAndPaint()) {
11417 return PhysicalAxes::Both;
11420 // and overflow:hidden that we should interpret as clip
11421 if (aDisp->mOverflowX == StyleOverflow::Hidden &&
11422 aDisp->mOverflowY == StyleOverflow::Hidden) {
11423 // REVIEW: these are the frame types that set up clipping.
11424 LayoutFrameType type = Type();
11425 switch (type) {
11426 case LayoutFrameType::Table:
11427 case LayoutFrameType::TableCell:
11428 case LayoutFrameType::SVGOuterSVG:
11429 case LayoutFrameType::SVGInnerSVG:
11430 case LayoutFrameType::SVGSymbol:
11431 case LayoutFrameType::SVGForeignObject:
11432 return PhysicalAxes::Both;
11433 default:
11434 if (IsReplacedWithBlock()) {
11435 if (type == mozilla::LayoutFrameType::TextInput) {
11436 // It has an anonymous scroll frame that handles any overflow.
11437 return PhysicalAxes::None;
11439 return PhysicalAxes::Both;
11444 // clip overflow:clip, except for nsListControlFrame which is
11445 // an nsHTMLScrollFrame sub-class.
11446 if (MOZ_UNLIKELY((aDisp->mOverflowX == mozilla::StyleOverflow::Clip ||
11447 aDisp->mOverflowY == mozilla::StyleOverflow::Clip) &&
11448 !IsListControlFrame())) {
11449 // FIXME: we could use GetViewportScrollStylesOverrideElement() here instead
11450 // if that worked correctly in a print context. (see bug 1654667)
11451 const auto* element = Element::FromNodeOrNull(GetContent());
11452 if (!element ||
11453 !PresContext()->ElementWouldPropagateScrollStyles(*element)) {
11454 uint8_t axes = uint8_t(PhysicalAxes::None);
11455 if (aDisp->mOverflowX == mozilla::StyleOverflow::Clip) {
11456 axes |= uint8_t(PhysicalAxes::Horizontal);
11458 if (aDisp->mOverflowY == mozilla::StyleOverflow::Clip) {
11459 axes |= uint8_t(PhysicalAxes::Vertical);
11461 return PhysicalAxes(axes);
11465 if (HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
11466 return PhysicalAxes::None;
11469 // If we're paginated and a block, and have NS_BLOCK_CLIP_PAGINATED_OVERFLOW
11470 // set, then we want to clip our overflow.
11471 bool clip = HasAnyStateBits(NS_BLOCK_CLIP_PAGINATED_OVERFLOW) &&
11472 PresContext()->IsPaginated() && IsBlockFrame();
11473 return clip ? PhysicalAxes::Both : PhysicalAxes::None;
11476 #ifdef DEBUG
11477 static void GetTagName(nsIFrame* aFrame, nsIContent* aContent, int aResultSize,
11478 char* aResult) {
11479 if (aContent) {
11480 snprintf(aResult, aResultSize, "%s@%p",
11481 nsAtomCString(aContent->NodeInfo()->NameAtom()).get(), aFrame);
11482 } else {
11483 snprintf(aResult, aResultSize, "@%p", aFrame);
11487 void nsIFrame::Trace(const char* aMethod, bool aEnter) {
11488 if (NS_FRAME_LOG_TEST(sFrameLogModule, NS_FRAME_TRACE_CALLS)) {
11489 char tagbuf[40];
11490 GetTagName(this, mContent, sizeof(tagbuf), tagbuf);
11491 printf_stderr("%s: %s %s", tagbuf, aEnter ? "enter" : "exit", aMethod);
11495 void nsIFrame::Trace(const char* aMethod, bool aEnter,
11496 const nsReflowStatus& aStatus) {
11497 if (NS_FRAME_LOG_TEST(sFrameLogModule, NS_FRAME_TRACE_CALLS)) {
11498 char tagbuf[40];
11499 GetTagName(this, mContent, sizeof(tagbuf), tagbuf);
11500 printf_stderr("%s: %s %s, status=%scomplete%s", tagbuf,
11501 aEnter ? "enter" : "exit", aMethod,
11502 aStatus.IsIncomplete() ? "not" : "",
11503 (aStatus.NextInFlowNeedsReflow()) ? "+reflow" : "");
11507 void nsIFrame::TraceMsg(const char* aFormatString, ...) {
11508 if (NS_FRAME_LOG_TEST(sFrameLogModule, NS_FRAME_TRACE_CALLS)) {
11509 // Format arguments into a buffer
11510 char argbuf[200];
11511 va_list ap;
11512 va_start(ap, aFormatString);
11513 VsprintfLiteral(argbuf, aFormatString, ap);
11514 va_end(ap);
11516 char tagbuf[40];
11517 GetTagName(this, mContent, sizeof(tagbuf), tagbuf);
11518 printf_stderr("%s: %s", tagbuf, argbuf);
11522 void nsIFrame::VerifyDirtyBitSet(const nsFrameList& aFrameList) {
11523 for (nsIFrame* f : aFrameList) {
11524 NS_ASSERTION(f->HasAnyStateBits(NS_FRAME_IS_DIRTY), "dirty bit not set");
11528 // Start Display Reflow
11529 DR_cookie::DR_cookie(nsPresContext* aPresContext, nsIFrame* aFrame,
11530 const ReflowInput& aReflowInput, ReflowOutput& aMetrics,
11531 nsReflowStatus& aStatus)
11532 : mPresContext(aPresContext),
11533 mFrame(aFrame),
11534 mReflowInput(aReflowInput),
11535 mMetrics(aMetrics),
11536 mStatus(aStatus) {
11537 MOZ_COUNT_CTOR(DR_cookie);
11538 mValue = nsIFrame::DisplayReflowEnter(aPresContext, mFrame, mReflowInput);
11541 DR_cookie::~DR_cookie() {
11542 MOZ_COUNT_DTOR(DR_cookie);
11543 nsIFrame::DisplayReflowExit(mPresContext, mFrame, mMetrics, mStatus, mValue);
11546 DR_layout_cookie::DR_layout_cookie(nsIFrame* aFrame) : mFrame(aFrame) {
11547 MOZ_COUNT_CTOR(DR_layout_cookie);
11548 mValue = nsIFrame::DisplayLayoutEnter(mFrame);
11551 DR_layout_cookie::~DR_layout_cookie() {
11552 MOZ_COUNT_DTOR(DR_layout_cookie);
11553 nsIFrame::DisplayLayoutExit(mFrame, mValue);
11556 DR_intrinsic_inline_size_cookie::DR_intrinsic_inline_size_cookie(
11557 nsIFrame* aFrame, const char* aType, nscoord& aResult)
11558 : mFrame(aFrame), mType(aType), mResult(aResult) {
11559 MOZ_COUNT_CTOR(DR_intrinsic_inline_size_cookie);
11560 mValue = nsIFrame::DisplayIntrinsicISizeEnter(mFrame, mType);
11563 DR_intrinsic_inline_size_cookie::~DR_intrinsic_inline_size_cookie() {
11564 MOZ_COUNT_DTOR(DR_intrinsic_inline_size_cookie);
11565 nsIFrame::DisplayIntrinsicISizeExit(mFrame, mType, mResult, mValue);
11568 DR_intrinsic_size_cookie::DR_intrinsic_size_cookie(nsIFrame* aFrame,
11569 const char* aType,
11570 nsSize& aResult)
11571 : mFrame(aFrame), mType(aType), mResult(aResult) {
11572 MOZ_COUNT_CTOR(DR_intrinsic_size_cookie);
11573 mValue = nsIFrame::DisplayIntrinsicSizeEnter(mFrame, mType);
11576 DR_intrinsic_size_cookie::~DR_intrinsic_size_cookie() {
11577 MOZ_COUNT_DTOR(DR_intrinsic_size_cookie);
11578 nsIFrame::DisplayIntrinsicSizeExit(mFrame, mType, mResult, mValue);
11581 DR_init_constraints_cookie::DR_init_constraints_cookie(
11582 nsIFrame* aFrame, ReflowInput* aState, nscoord aCBWidth, nscoord aCBHeight,
11583 const mozilla::Maybe<mozilla::LogicalMargin> aBorder,
11584 const mozilla::Maybe<mozilla::LogicalMargin> aPadding)
11585 : mFrame(aFrame), mState(aState) {
11586 MOZ_COUNT_CTOR(DR_init_constraints_cookie);
11587 nsMargin border;
11588 if (aBorder) {
11589 border = aBorder->GetPhysicalMargin(aFrame->GetWritingMode());
11591 nsMargin padding;
11592 if (aPadding) {
11593 padding = aPadding->GetPhysicalMargin(aFrame->GetWritingMode());
11595 mValue = ReflowInput::DisplayInitConstraintsEnter(
11596 mFrame, mState, aCBWidth, aCBHeight, aBorder ? &border : nullptr,
11597 aPadding ? &padding : nullptr);
11600 DR_init_constraints_cookie::~DR_init_constraints_cookie() {
11601 MOZ_COUNT_DTOR(DR_init_constraints_cookie);
11602 ReflowInput::DisplayInitConstraintsExit(mFrame, mState, mValue);
11605 DR_init_offsets_cookie::DR_init_offsets_cookie(
11606 nsIFrame* aFrame, SizeComputationInput* aState, nscoord aPercentBasis,
11607 WritingMode aCBWritingMode,
11608 const mozilla::Maybe<mozilla::LogicalMargin> aBorder,
11609 const mozilla::Maybe<mozilla::LogicalMargin> aPadding)
11610 : mFrame(aFrame), mState(aState) {
11611 MOZ_COUNT_CTOR(DR_init_offsets_cookie);
11612 nsMargin border;
11613 if (aBorder) {
11614 border = aBorder->GetPhysicalMargin(aFrame->GetWritingMode());
11616 nsMargin padding;
11617 if (aPadding) {
11618 padding = aPadding->GetPhysicalMargin(aFrame->GetWritingMode());
11620 mValue = SizeComputationInput::DisplayInitOffsetsEnter(
11621 mFrame, mState, aPercentBasis, aCBWritingMode,
11622 aBorder ? &border : nullptr, aPadding ? &padding : nullptr);
11625 DR_init_offsets_cookie::~DR_init_offsets_cookie() {
11626 MOZ_COUNT_DTOR(DR_init_offsets_cookie);
11627 SizeComputationInput::DisplayInitOffsetsExit(mFrame, mState, mValue);
11630 struct DR_Rule;
11632 struct DR_FrameTypeInfo {
11633 DR_FrameTypeInfo(LayoutFrameType aFrameType, const char* aFrameNameAbbrev,
11634 const char* aFrameName);
11635 ~DR_FrameTypeInfo();
11637 LayoutFrameType mType;
11638 char mNameAbbrev[16];
11639 char mName[32];
11640 nsTArray<DR_Rule*> mRules;
11642 private:
11643 DR_FrameTypeInfo& operator=(const DR_FrameTypeInfo&) = delete;
11646 struct DR_FrameTreeNode;
11647 struct DR_Rule;
11649 struct DR_State {
11650 DR_State();
11651 ~DR_State();
11652 void Init();
11653 void AddFrameTypeInfo(LayoutFrameType aFrameType,
11654 const char* aFrameNameAbbrev, const char* aFrameName);
11655 DR_FrameTypeInfo* GetFrameTypeInfo(LayoutFrameType aFrameType);
11656 DR_FrameTypeInfo* GetFrameTypeInfo(char* aFrameName);
11657 void InitFrameTypeTable();
11658 DR_FrameTreeNode* CreateTreeNode(nsIFrame* aFrame,
11659 const ReflowInput* aReflowInput);
11660 void FindMatchingRule(DR_FrameTreeNode& aNode);
11661 bool RuleMatches(DR_Rule& aRule, DR_FrameTreeNode& aNode);
11662 bool GetToken(FILE* aFile, char* aBuf, size_t aBufSize);
11663 DR_Rule* ParseRule(FILE* aFile);
11664 void ParseRulesFile();
11665 void AddRule(nsTArray<DR_Rule*>& aRules, DR_Rule& aRule);
11666 bool IsWhiteSpace(int c);
11667 bool GetNumber(char* aBuf, int32_t& aNumber);
11668 void PrettyUC(nscoord aSize, char* aBuf, int aBufSize);
11669 void PrintMargin(const char* tag, const nsMargin* aMargin);
11670 void DisplayFrameTypeInfo(nsIFrame* aFrame, int32_t aIndent);
11671 void DeleteTreeNode(DR_FrameTreeNode& aNode);
11673 bool mInited;
11674 bool mActive;
11675 int32_t mCount;
11676 int32_t mAssert;
11677 int32_t mIndent;
11678 bool mIndentUndisplayedFrames;
11679 bool mDisplayPixelErrors;
11680 nsTArray<DR_Rule*> mWildRules;
11681 nsTArray<DR_FrameTypeInfo> mFrameTypeTable;
11682 // reflow specific state
11683 nsTArray<DR_FrameTreeNode*> mFrameTreeLeaves;
11686 static DR_State* DR_state; // the one and only DR_State
11688 struct DR_RulePart {
11689 explicit DR_RulePart(LayoutFrameType aFrameType)
11690 : mFrameType(aFrameType), mNext(0) {}
11692 void Destroy();
11694 LayoutFrameType mFrameType;
11695 DR_RulePart* mNext;
11698 void DR_RulePart::Destroy() {
11699 if (mNext) {
11700 mNext->Destroy();
11702 delete this;
11705 struct DR_Rule {
11706 DR_Rule() : mLength(0), mTarget(nullptr), mDisplay(false) {
11707 MOZ_COUNT_CTOR(DR_Rule);
11709 ~DR_Rule() {
11710 if (mTarget) mTarget->Destroy();
11711 MOZ_COUNT_DTOR(DR_Rule);
11713 void AddPart(LayoutFrameType aFrameType);
11715 uint32_t mLength;
11716 DR_RulePart* mTarget;
11717 bool mDisplay;
11720 void DR_Rule::AddPart(LayoutFrameType aFrameType) {
11721 DR_RulePart* newPart = new DR_RulePart(aFrameType);
11722 newPart->mNext = mTarget;
11723 mTarget = newPart;
11724 mLength++;
11727 DR_FrameTypeInfo::~DR_FrameTypeInfo() {
11728 int32_t numElements;
11729 numElements = mRules.Length();
11730 for (int32_t i = numElements - 1; i >= 0; i--) {
11731 delete mRules.ElementAt(i);
11735 DR_FrameTypeInfo::DR_FrameTypeInfo(LayoutFrameType aFrameType,
11736 const char* aFrameNameAbbrev,
11737 const char* aFrameName) {
11738 mType = aFrameType;
11739 PL_strncpyz(mNameAbbrev, aFrameNameAbbrev, sizeof(mNameAbbrev));
11740 PL_strncpyz(mName, aFrameName, sizeof(mName));
11743 struct DR_FrameTreeNode {
11744 DR_FrameTreeNode(nsIFrame* aFrame, DR_FrameTreeNode* aParent)
11745 : mFrame(aFrame), mParent(aParent), mDisplay(0), mIndent(0) {
11746 MOZ_COUNT_CTOR(DR_FrameTreeNode);
11749 MOZ_COUNTED_DTOR(DR_FrameTreeNode)
11751 nsIFrame* mFrame;
11752 DR_FrameTreeNode* mParent;
11753 bool mDisplay;
11754 uint32_t mIndent;
11757 // DR_State implementation
11759 DR_State::DR_State()
11760 : mInited(false),
11761 mActive(false),
11762 mCount(0),
11763 mAssert(-1),
11764 mIndent(0),
11765 mIndentUndisplayedFrames(false),
11766 mDisplayPixelErrors(false) {
11767 MOZ_COUNT_CTOR(DR_State);
11770 void DR_State::Init() {
11771 char* env = PR_GetEnv("GECKO_DISPLAY_REFLOW_ASSERT");
11772 int32_t num;
11773 if (env) {
11774 if (GetNumber(env, num))
11775 mAssert = num;
11776 else
11777 printf("GECKO_DISPLAY_REFLOW_ASSERT - invalid value = %s", env);
11780 env = PR_GetEnv("GECKO_DISPLAY_REFLOW_INDENT_START");
11781 if (env) {
11782 if (GetNumber(env, num))
11783 mIndent = num;
11784 else
11785 printf("GECKO_DISPLAY_REFLOW_INDENT_START - invalid value = %s", env);
11788 env = PR_GetEnv("GECKO_DISPLAY_REFLOW_INDENT_UNDISPLAYED_FRAMES");
11789 if (env) {
11790 if (GetNumber(env, num))
11791 mIndentUndisplayedFrames = num;
11792 else
11793 printf(
11794 "GECKO_DISPLAY_REFLOW_INDENT_UNDISPLAYED_FRAMES - invalid value = %s",
11795 env);
11798 env = PR_GetEnv("GECKO_DISPLAY_REFLOW_FLAG_PIXEL_ERRORS");
11799 if (env) {
11800 if (GetNumber(env, num))
11801 mDisplayPixelErrors = num;
11802 else
11803 printf("GECKO_DISPLAY_REFLOW_FLAG_PIXEL_ERRORS - invalid value = %s",
11804 env);
11807 InitFrameTypeTable();
11808 ParseRulesFile();
11809 mInited = true;
11812 DR_State::~DR_State() {
11813 MOZ_COUNT_DTOR(DR_State);
11814 int32_t numElements, i;
11815 numElements = mWildRules.Length();
11816 for (i = numElements - 1; i >= 0; i--) {
11817 delete mWildRules.ElementAt(i);
11819 numElements = mFrameTreeLeaves.Length();
11820 for (i = numElements - 1; i >= 0; i--) {
11821 delete mFrameTreeLeaves.ElementAt(i);
11825 bool DR_State::GetNumber(char* aBuf, int32_t& aNumber) {
11826 if (sscanf(aBuf, "%d", &aNumber) > 0)
11827 return true;
11828 else
11829 return false;
11832 bool DR_State::IsWhiteSpace(int c) {
11833 return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
11836 bool DR_State::GetToken(FILE* aFile, char* aBuf, size_t aBufSize) {
11837 bool haveToken = false;
11838 aBuf[0] = 0;
11839 // get the 1st non whitespace char
11840 int c = -1;
11841 for (c = getc(aFile); (c > 0) && IsWhiteSpace(c); c = getc(aFile)) {
11844 if (c > 0) {
11845 haveToken = true;
11846 aBuf[0] = c;
11847 // get everything up to the next whitespace char
11848 size_t cX;
11849 for (cX = 1; cX + 1 < aBufSize; cX++) {
11850 c = getc(aFile);
11851 if (c < 0) { // EOF
11852 ungetc(' ', aFile);
11853 break;
11854 } else {
11855 if (IsWhiteSpace(c)) {
11856 break;
11857 } else {
11858 aBuf[cX] = c;
11862 aBuf[cX] = 0;
11864 return haveToken;
11867 DR_Rule* DR_State::ParseRule(FILE* aFile) {
11868 char buf[128];
11869 int32_t doDisplay;
11870 DR_Rule* rule = nullptr;
11871 while (GetToken(aFile, buf, sizeof(buf))) {
11872 if (GetNumber(buf, doDisplay)) {
11873 if (rule) {
11874 rule->mDisplay = !!doDisplay;
11875 break;
11876 } else {
11877 printf("unexpected token - %s \n", buf);
11879 } else {
11880 if (!rule) {
11881 rule = new DR_Rule;
11883 if (strcmp(buf, "*") == 0) {
11884 rule->AddPart(LayoutFrameType::None);
11885 } else {
11886 DR_FrameTypeInfo* info = GetFrameTypeInfo(buf);
11887 if (info) {
11888 rule->AddPart(info->mType);
11889 } else {
11890 printf("invalid frame type - %s \n", buf);
11895 return rule;
11898 void DR_State::AddRule(nsTArray<DR_Rule*>& aRules, DR_Rule& aRule) {
11899 int32_t numRules = aRules.Length();
11900 for (int32_t ruleX = 0; ruleX < numRules; ruleX++) {
11901 DR_Rule* rule = aRules.ElementAt(ruleX);
11902 NS_ASSERTION(rule, "program error");
11903 if (aRule.mLength > rule->mLength) {
11904 aRules.InsertElementAt(ruleX, &aRule);
11905 return;
11908 aRules.AppendElement(&aRule);
11911 static Maybe<bool> ShouldLogReflow(const char* processes) {
11912 switch (processes[0]) {
11913 case 'A':
11914 case 'a':
11915 return Some(true);
11916 case 'P':
11917 case 'p':
11918 return Some(XRE_IsParentProcess());
11919 case 'C':
11920 case 'c':
11921 return Some(XRE_IsContentProcess());
11922 default:
11923 return Nothing{};
11927 void DR_State::ParseRulesFile() {
11928 char* processes = PR_GetEnv("GECKO_DISPLAY_REFLOW_PROCESSES");
11929 if (processes) {
11930 Maybe<bool> enableLog = ShouldLogReflow(processes);
11931 if (enableLog.isNothing()) {
11932 MOZ_CRASH("GECKO_DISPLAY_REFLOW_PROCESSES: [a]ll [p]arent [c]ontent");
11933 } else if (enableLog.value()) {
11934 DR_Rule* rule = new DR_Rule;
11935 rule->AddPart(LayoutFrameType::None);
11936 rule->mDisplay = true;
11937 AddRule(mWildRules, *rule);
11938 mActive = true;
11940 return;
11943 char* path = PR_GetEnv("GECKO_DISPLAY_REFLOW_RULES_FILE");
11944 if (path) {
11945 FILE* inFile = fopen(path, "r");
11946 if (!inFile) {
11947 MOZ_CRASH(
11948 "Failed to open the specified rules file; Try `--setpref "
11949 "security.sandbox.content.level=2` if the sandbox is at cause");
11951 for (DR_Rule* rule = ParseRule(inFile); rule; rule = ParseRule(inFile)) {
11952 if (rule->mTarget) {
11953 LayoutFrameType fType = rule->mTarget->mFrameType;
11954 if (fType != LayoutFrameType::None) {
11955 DR_FrameTypeInfo* info = GetFrameTypeInfo(fType);
11956 AddRule(info->mRules, *rule);
11957 } else {
11958 AddRule(mWildRules, *rule);
11960 mActive = true;
11964 fclose(inFile);
11968 void DR_State::AddFrameTypeInfo(LayoutFrameType aFrameType,
11969 const char* aFrameNameAbbrev,
11970 const char* aFrameName) {
11971 mFrameTypeTable.EmplaceBack(aFrameType, aFrameNameAbbrev, aFrameName);
11974 DR_FrameTypeInfo* DR_State::GetFrameTypeInfo(LayoutFrameType aFrameType) {
11975 int32_t numEntries = mFrameTypeTable.Length();
11976 NS_ASSERTION(numEntries != 0, "empty FrameTypeTable");
11977 for (int32_t i = 0; i < numEntries; i++) {
11978 DR_FrameTypeInfo& info = mFrameTypeTable.ElementAt(i);
11979 if (info.mType == aFrameType) {
11980 return &info;
11983 return &mFrameTypeTable.ElementAt(numEntries -
11984 1); // return unknown frame type
11987 DR_FrameTypeInfo* DR_State::GetFrameTypeInfo(char* aFrameName) {
11988 int32_t numEntries = mFrameTypeTable.Length();
11989 NS_ASSERTION(numEntries != 0, "empty FrameTypeTable");
11990 for (int32_t i = 0; i < numEntries; i++) {
11991 DR_FrameTypeInfo& info = mFrameTypeTable.ElementAt(i);
11992 if ((strcmp(aFrameName, info.mName) == 0) ||
11993 (strcmp(aFrameName, info.mNameAbbrev) == 0)) {
11994 return &info;
11997 return &mFrameTypeTable.ElementAt(numEntries -
11998 1); // return unknown frame type
12001 void DR_State::InitFrameTypeTable() {
12002 AddFrameTypeInfo(LayoutFrameType::Block, "block", "block");
12003 AddFrameTypeInfo(LayoutFrameType::Br, "br", "br");
12004 AddFrameTypeInfo(LayoutFrameType::ColorControl, "color", "colorControl");
12005 AddFrameTypeInfo(LayoutFrameType::GfxButtonControl, "button",
12006 "gfxButtonControl");
12007 AddFrameTypeInfo(LayoutFrameType::HTMLButtonControl, "HTMLbutton",
12008 "HTMLButtonControl");
12009 AddFrameTypeInfo(LayoutFrameType::HTMLCanvas, "HTMLCanvas", "HTMLCanvas");
12010 AddFrameTypeInfo(LayoutFrameType::SubDocument, "subdoc", "subDocument");
12011 AddFrameTypeInfo(LayoutFrameType::Image, "img", "image");
12012 AddFrameTypeInfo(LayoutFrameType::Inline, "inline", "inline");
12013 AddFrameTypeInfo(LayoutFrameType::Letter, "letter", "letter");
12014 AddFrameTypeInfo(LayoutFrameType::Line, "line", "line");
12015 AddFrameTypeInfo(LayoutFrameType::ListControl, "select", "select");
12016 AddFrameTypeInfo(LayoutFrameType::Page, "page", "page");
12017 AddFrameTypeInfo(LayoutFrameType::Placeholder, "place", "placeholder");
12018 AddFrameTypeInfo(LayoutFrameType::Canvas, "canvas", "canvas");
12019 AddFrameTypeInfo(LayoutFrameType::Scroll, "scroll", "scroll");
12020 AddFrameTypeInfo(LayoutFrameType::TableCell, "cell", "tableCell");
12021 AddFrameTypeInfo(LayoutFrameType::TableCol, "col", "tableCol");
12022 AddFrameTypeInfo(LayoutFrameType::TableColGroup, "colG", "tableColGroup");
12023 AddFrameTypeInfo(LayoutFrameType::Table, "tbl", "table");
12024 AddFrameTypeInfo(LayoutFrameType::TableWrapper, "tblW", "tableWrapper");
12025 AddFrameTypeInfo(LayoutFrameType::TableRowGroup, "rowG", "tableRowGroup");
12026 AddFrameTypeInfo(LayoutFrameType::TableRow, "row", "tableRow");
12027 AddFrameTypeInfo(LayoutFrameType::TextInput, "textCtl", "textInput");
12028 AddFrameTypeInfo(LayoutFrameType::Text, "text", "text");
12029 AddFrameTypeInfo(LayoutFrameType::Viewport, "VP", "viewport");
12030 AddFrameTypeInfo(LayoutFrameType::Slider, "Slider", "Slider");
12031 AddFrameTypeInfo(LayoutFrameType::None, "unknown", "unknown");
12034 void DR_State::DisplayFrameTypeInfo(nsIFrame* aFrame, int32_t aIndent) {
12035 DR_FrameTypeInfo* frameTypeInfo = GetFrameTypeInfo(aFrame->Type());
12036 if (frameTypeInfo) {
12037 for (int32_t i = 0; i < aIndent; i++) {
12038 printf(" ");
12040 if (!strcmp(frameTypeInfo->mNameAbbrev, "unknown")) {
12041 if (aFrame) {
12042 nsAutoString name;
12043 aFrame->GetFrameName(name);
12044 printf("%s %p ", NS_LossyConvertUTF16toASCII(name).get(),
12045 (void*)aFrame);
12046 } else {
12047 printf("%s %p ", frameTypeInfo->mNameAbbrev, (void*)aFrame);
12049 } else {
12050 printf("%s %p ", frameTypeInfo->mNameAbbrev, (void*)aFrame);
12055 bool DR_State::RuleMatches(DR_Rule& aRule, DR_FrameTreeNode& aNode) {
12056 NS_ASSERTION(aRule.mTarget, "program error");
12058 DR_RulePart* rulePart;
12059 DR_FrameTreeNode* parentNode;
12060 for (rulePart = aRule.mTarget->mNext, parentNode = aNode.mParent;
12061 rulePart && parentNode;
12062 rulePart = rulePart->mNext, parentNode = parentNode->mParent) {
12063 if (rulePart->mFrameType != LayoutFrameType::None) {
12064 if (parentNode->mFrame) {
12065 if (rulePart->mFrameType != parentNode->mFrame->Type()) {
12066 return false;
12068 } else
12069 NS_ASSERTION(false, "program error");
12071 // else wild card match
12073 return true;
12076 void DR_State::FindMatchingRule(DR_FrameTreeNode& aNode) {
12077 if (!aNode.mFrame) {
12078 NS_ASSERTION(false, "invalid DR_FrameTreeNode \n");
12079 return;
12082 bool matchingRule = false;
12084 DR_FrameTypeInfo* info = GetFrameTypeInfo(aNode.mFrame->Type());
12085 NS_ASSERTION(info, "program error");
12086 int32_t numRules = info->mRules.Length();
12087 for (int32_t ruleX = 0; ruleX < numRules; ruleX++) {
12088 DR_Rule* rule = info->mRules.ElementAt(ruleX);
12089 if (rule && RuleMatches(*rule, aNode)) {
12090 aNode.mDisplay = rule->mDisplay;
12091 matchingRule = true;
12092 break;
12095 if (!matchingRule) {
12096 int32_t numWildRules = mWildRules.Length();
12097 for (int32_t ruleX = 0; ruleX < numWildRules; ruleX++) {
12098 DR_Rule* rule = mWildRules.ElementAt(ruleX);
12099 if (rule && RuleMatches(*rule, aNode)) {
12100 aNode.mDisplay = rule->mDisplay;
12101 break;
12107 DR_FrameTreeNode* DR_State::CreateTreeNode(nsIFrame* aFrame,
12108 const ReflowInput* aReflowInput) {
12109 // find the frame of the parent reflow input (usually just the parent of
12110 // aFrame)
12111 nsIFrame* parentFrame;
12112 if (aReflowInput) {
12113 const ReflowInput* parentRI = aReflowInput->mParentReflowInput;
12114 parentFrame = (parentRI) ? parentRI->mFrame : nullptr;
12115 } else {
12116 parentFrame = aFrame->GetParent();
12119 // find the parent tree node leaf
12120 DR_FrameTreeNode* parentNode = nullptr;
12122 DR_FrameTreeNode* lastLeaf = nullptr;
12123 if (mFrameTreeLeaves.Length())
12124 lastLeaf = mFrameTreeLeaves.ElementAt(mFrameTreeLeaves.Length() - 1);
12125 if (lastLeaf) {
12126 for (parentNode = lastLeaf;
12127 parentNode && (parentNode->mFrame != parentFrame);
12128 parentNode = parentNode->mParent) {
12131 DR_FrameTreeNode* newNode = new DR_FrameTreeNode(aFrame, parentNode);
12132 FindMatchingRule(*newNode);
12134 newNode->mIndent = mIndent;
12135 if (newNode->mDisplay || mIndentUndisplayedFrames) {
12136 ++mIndent;
12139 if (lastLeaf && (lastLeaf == parentNode)) {
12140 mFrameTreeLeaves.RemoveLastElement();
12142 mFrameTreeLeaves.AppendElement(newNode);
12143 mCount++;
12145 return newNode;
12148 void DR_State::PrettyUC(nscoord aSize, char* aBuf, int aBufSize) {
12149 if (NS_UNCONSTRAINEDSIZE == aSize) {
12150 strcpy(aBuf, "UC");
12151 } else {
12152 if ((nscoord)0xdeadbeefU == aSize) {
12153 strcpy(aBuf, "deadbeef");
12154 } else {
12155 snprintf(aBuf, aBufSize, "%d", aSize);
12160 void DR_State::PrintMargin(const char* tag, const nsMargin* aMargin) {
12161 if (aMargin) {
12162 char t[16], r[16], b[16], l[16];
12163 PrettyUC(aMargin->top, t, 16);
12164 PrettyUC(aMargin->right, r, 16);
12165 PrettyUC(aMargin->bottom, b, 16);
12166 PrettyUC(aMargin->left, l, 16);
12167 printf(" %s=%s,%s,%s,%s", tag, t, r, b, l);
12168 } else {
12169 // use %p here for consistency with other null-pointer printouts
12170 printf(" %s=%p", tag, (void*)aMargin);
12174 void DR_State::DeleteTreeNode(DR_FrameTreeNode& aNode) {
12175 mFrameTreeLeaves.RemoveElement(&aNode);
12176 int32_t numLeaves = mFrameTreeLeaves.Length();
12177 if ((0 == numLeaves) ||
12178 (aNode.mParent != mFrameTreeLeaves.ElementAt(numLeaves - 1))) {
12179 mFrameTreeLeaves.AppendElement(aNode.mParent);
12182 if (aNode.mDisplay || mIndentUndisplayedFrames) {
12183 --mIndent;
12185 // delete the tree node
12186 delete &aNode;
12189 static void CheckPixelError(nscoord aSize, int32_t aPixelToTwips) {
12190 if (NS_UNCONSTRAINEDSIZE != aSize) {
12191 if ((aSize % aPixelToTwips) > 0) {
12192 printf("VALUE %d is not a whole pixel \n", aSize);
12197 static void DisplayReflowEnterPrint(nsPresContext* aPresContext,
12198 nsIFrame* aFrame,
12199 const ReflowInput& aReflowInput,
12200 DR_FrameTreeNode& aTreeNode,
12201 bool aChanged) {
12202 if (aTreeNode.mDisplay) {
12203 DR_state->DisplayFrameTypeInfo(aFrame, aTreeNode.mIndent);
12205 char width[16];
12206 char height[16];
12208 DR_state->PrettyUC(aReflowInput.AvailableWidth(), width, 16);
12209 DR_state->PrettyUC(aReflowInput.AvailableHeight(), height, 16);
12210 printf("Reflow a=%s,%s ", width, height);
12212 DR_state->PrettyUC(aReflowInput.ComputedWidth(), width, 16);
12213 DR_state->PrettyUC(aReflowInput.ComputedHeight(), height, 16);
12214 printf("c=%s,%s ", width, height);
12216 if (aFrame->HasAnyStateBits(NS_FRAME_IS_DIRTY)) printf("dirty ");
12218 if (aFrame->HasAnyStateBits(NS_FRAME_HAS_DIRTY_CHILDREN))
12219 printf("dirty-children ");
12221 if (aReflowInput.mFlags.mSpecialBSizeReflow) printf("special-bsize ");
12223 if (aReflowInput.IsHResize()) printf("h-resize ");
12225 if (aReflowInput.IsVResize()) printf("v-resize ");
12227 nsIFrame* inFlow = aFrame->GetPrevInFlow();
12228 if (inFlow) {
12229 printf("pif=%p ", (void*)inFlow);
12231 inFlow = aFrame->GetNextInFlow();
12232 if (inFlow) {
12233 printf("nif=%p ", (void*)inFlow);
12235 if (aChanged)
12236 printf("CHANGED \n");
12237 else
12238 printf("cnt=%d \n", DR_state->mCount);
12239 if (DR_state->mDisplayPixelErrors) {
12240 int32_t d2a = aPresContext->AppUnitsPerDevPixel();
12241 CheckPixelError(aReflowInput.AvailableWidth(), d2a);
12242 CheckPixelError(aReflowInput.AvailableHeight(), d2a);
12243 CheckPixelError(aReflowInput.ComputedWidth(), d2a);
12244 CheckPixelError(aReflowInput.ComputedHeight(), d2a);
12249 void* nsIFrame::DisplayReflowEnter(nsPresContext* aPresContext,
12250 nsIFrame* aFrame,
12251 const ReflowInput& aReflowInput) {
12252 if (!DR_state->mInited) DR_state->Init();
12253 if (!DR_state->mActive) return nullptr;
12255 NS_ASSERTION(aFrame, "invalid call");
12257 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, &aReflowInput);
12258 if (treeNode) {
12259 DisplayReflowEnterPrint(aPresContext, aFrame, aReflowInput, *treeNode,
12260 false);
12262 return treeNode;
12265 void* nsIFrame::DisplayLayoutEnter(nsIFrame* aFrame) {
12266 if (!DR_state->mInited) DR_state->Init();
12267 if (!DR_state->mActive) return nullptr;
12269 NS_ASSERTION(aFrame, "invalid call");
12271 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12272 if (treeNode && treeNode->mDisplay) {
12273 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12274 printf("XULLayout\n");
12276 return treeNode;
12279 void* nsIFrame::DisplayIntrinsicISizeEnter(nsIFrame* aFrame,
12280 const char* aType) {
12281 if (!DR_state->mInited) DR_state->Init();
12282 if (!DR_state->mActive) return nullptr;
12284 NS_ASSERTION(aFrame, "invalid call");
12286 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12287 if (treeNode && treeNode->mDisplay) {
12288 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12289 printf("Get%sISize\n", aType);
12291 return treeNode;
12294 void* nsIFrame::DisplayIntrinsicSizeEnter(nsIFrame* aFrame, const char* aType) {
12295 if (!DR_state->mInited) DR_state->Init();
12296 if (!DR_state->mActive) return nullptr;
12298 NS_ASSERTION(aFrame, "invalid call");
12300 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12301 if (treeNode && treeNode->mDisplay) {
12302 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12303 printf("Get%sSize\n", aType);
12305 return treeNode;
12308 void nsIFrame::DisplayReflowExit(nsPresContext* aPresContext, nsIFrame* aFrame,
12309 ReflowOutput& aMetrics,
12310 const nsReflowStatus& aStatus,
12311 void* aFrameTreeNode) {
12312 if (!DR_state->mActive) return;
12314 NS_ASSERTION(aFrame, "DisplayReflowExit - invalid call");
12315 if (!aFrameTreeNode) return;
12317 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12318 if (treeNode->mDisplay) {
12319 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12321 char width[16];
12322 char height[16];
12323 char x[16];
12324 char y[16];
12325 DR_state->PrettyUC(aMetrics.Width(), width, 16);
12326 DR_state->PrettyUC(aMetrics.Height(), height, 16);
12327 printf("Reflow d=%s,%s", width, height);
12329 if (!aStatus.IsEmpty()) {
12330 printf(" status=%s", ToString(aStatus).c_str());
12332 if (aFrame->HasOverflowAreas()) {
12333 DR_state->PrettyUC(aMetrics.InkOverflow().x, x, 16);
12334 DR_state->PrettyUC(aMetrics.InkOverflow().y, y, 16);
12335 DR_state->PrettyUC(aMetrics.InkOverflow().width, width, 16);
12336 DR_state->PrettyUC(aMetrics.InkOverflow().height, height, 16);
12337 printf(" vis-o=(%s,%s) %s x %s", x, y, width, height);
12339 nsRect storedOverflow = aFrame->InkOverflowRect();
12340 DR_state->PrettyUC(storedOverflow.x, x, 16);
12341 DR_state->PrettyUC(storedOverflow.y, y, 16);
12342 DR_state->PrettyUC(storedOverflow.width, width, 16);
12343 DR_state->PrettyUC(storedOverflow.height, height, 16);
12344 printf(" vis-sto=(%s,%s) %s x %s", x, y, width, height);
12346 DR_state->PrettyUC(aMetrics.ScrollableOverflow().x, x, 16);
12347 DR_state->PrettyUC(aMetrics.ScrollableOverflow().y, y, 16);
12348 DR_state->PrettyUC(aMetrics.ScrollableOverflow().width, width, 16);
12349 DR_state->PrettyUC(aMetrics.ScrollableOverflow().height, height, 16);
12350 printf(" scr-o=(%s,%s) %s x %s", x, y, width, height);
12352 storedOverflow = aFrame->ScrollableOverflowRect();
12353 DR_state->PrettyUC(storedOverflow.x, x, 16);
12354 DR_state->PrettyUC(storedOverflow.y, y, 16);
12355 DR_state->PrettyUC(storedOverflow.width, width, 16);
12356 DR_state->PrettyUC(storedOverflow.height, height, 16);
12357 printf(" scr-sto=(%s,%s) %s x %s", x, y, width, height);
12359 printf("\n");
12360 if (DR_state->mDisplayPixelErrors) {
12361 int32_t d2a = aPresContext->AppUnitsPerDevPixel();
12362 CheckPixelError(aMetrics.Width(), d2a);
12363 CheckPixelError(aMetrics.Height(), d2a);
12366 DR_state->DeleteTreeNode(*treeNode);
12369 void nsIFrame::DisplayLayoutExit(nsIFrame* aFrame, void* aFrameTreeNode) {
12370 if (!DR_state->mActive) return;
12372 NS_ASSERTION(aFrame, "non-null frame required");
12373 if (!aFrameTreeNode) return;
12375 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12376 if (treeNode->mDisplay) {
12377 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12378 nsRect rect = aFrame->GetRect();
12379 printf("XULLayout=%d,%d,%d,%d\n", rect.x, rect.y, rect.width, rect.height);
12381 DR_state->DeleteTreeNode(*treeNode);
12384 void nsIFrame::DisplayIntrinsicISizeExit(nsIFrame* aFrame, const char* aType,
12385 nscoord aResult,
12386 void* aFrameTreeNode) {
12387 if (!DR_state->mActive) return;
12389 NS_ASSERTION(aFrame, "non-null frame required");
12390 if (!aFrameTreeNode) return;
12392 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12393 if (treeNode->mDisplay) {
12394 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12395 char iSize[16];
12396 DR_state->PrettyUC(aResult, iSize, 16);
12397 printf("Get%sISize=%s\n", aType, iSize);
12399 DR_state->DeleteTreeNode(*treeNode);
12402 void nsIFrame::DisplayIntrinsicSizeExit(nsIFrame* aFrame, const char* aType,
12403 nsSize aResult, void* aFrameTreeNode) {
12404 if (!DR_state->mActive) return;
12406 NS_ASSERTION(aFrame, "non-null frame required");
12407 if (!aFrameTreeNode) return;
12409 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12410 if (treeNode->mDisplay) {
12411 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12413 char width[16];
12414 char height[16];
12415 DR_state->PrettyUC(aResult.width, width, 16);
12416 DR_state->PrettyUC(aResult.height, height, 16);
12417 printf("Get%sSize=%s,%s\n", aType, width, height);
12419 DR_state->DeleteTreeNode(*treeNode);
12422 /* static */
12423 void nsIFrame::DisplayReflowStartup() { DR_state = new DR_State(); }
12425 /* static */
12426 void nsIFrame::DisplayReflowShutdown() {
12427 delete DR_state;
12428 DR_state = nullptr;
12431 void DR_cookie::Change() const {
12432 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)mValue;
12433 if (treeNode && treeNode->mDisplay) {
12434 DisplayReflowEnterPrint(mPresContext, mFrame, mReflowInput, *treeNode,
12435 true);
12439 /* static */
12440 void* ReflowInput::DisplayInitConstraintsEnter(nsIFrame* aFrame,
12441 ReflowInput* aState,
12442 nscoord aContainingBlockWidth,
12443 nscoord aContainingBlockHeight,
12444 const nsMargin* aBorder,
12445 const nsMargin* aPadding) {
12446 MOZ_ASSERT(aFrame, "non-null frame required");
12447 MOZ_ASSERT(aState, "non-null state required");
12449 if (!DR_state->mInited) DR_state->Init();
12450 if (!DR_state->mActive) return nullptr;
12452 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, aState);
12453 if (treeNode && treeNode->mDisplay) {
12454 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12456 printf("InitConstraints parent=%p", (void*)aState->mParentReflowInput);
12458 char width[16];
12459 char height[16];
12461 DR_state->PrettyUC(aContainingBlockWidth, width, 16);
12462 DR_state->PrettyUC(aContainingBlockHeight, height, 16);
12463 printf(" cb=%s,%s", width, height);
12465 DR_state->PrettyUC(aState->AvailableWidth(), width, 16);
12466 DR_state->PrettyUC(aState->AvailableHeight(), height, 16);
12467 printf(" as=%s,%s", width, height);
12469 DR_state->PrintMargin("b", aBorder);
12470 DR_state->PrintMargin("p", aPadding);
12471 putchar('\n');
12473 return treeNode;
12476 /* static */
12477 void ReflowInput::DisplayInitConstraintsExit(nsIFrame* aFrame,
12478 ReflowInput* aState,
12479 void* aValue) {
12480 MOZ_ASSERT(aFrame, "non-null frame required");
12481 MOZ_ASSERT(aState, "non-null state required");
12483 if (!DR_state->mActive) return;
12484 if (!aValue) return;
12486 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aValue;
12487 if (treeNode->mDisplay) {
12488 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12489 char cmiw[16], cw[16], cmxw[16], cmih[16], ch[16], cmxh[16];
12490 DR_state->PrettyUC(aState->ComputedMinWidth(), cmiw, 16);
12491 DR_state->PrettyUC(aState->ComputedWidth(), cw, 16);
12492 DR_state->PrettyUC(aState->ComputedMaxWidth(), cmxw, 16);
12493 DR_state->PrettyUC(aState->ComputedMinHeight(), cmih, 16);
12494 DR_state->PrettyUC(aState->ComputedHeight(), ch, 16);
12495 DR_state->PrettyUC(aState->ComputedMaxHeight(), cmxh, 16);
12496 printf("InitConstraints= cw=(%s <= %s <= %s) ch=(%s <= %s <= %s)", cmiw, cw,
12497 cmxw, cmih, ch, cmxh);
12498 const nsMargin m = aState->ComputedPhysicalOffsets();
12499 DR_state->PrintMargin("co", &m);
12500 putchar('\n');
12502 DR_state->DeleteTreeNode(*treeNode);
12505 /* static */
12506 void* SizeComputationInput::DisplayInitOffsetsEnter(
12507 nsIFrame* aFrame, SizeComputationInput* aState, nscoord aPercentBasis,
12508 WritingMode aCBWritingMode, const nsMargin* aBorder,
12509 const nsMargin* aPadding) {
12510 MOZ_ASSERT(aFrame, "non-null frame required");
12511 MOZ_ASSERT(aState, "non-null state required");
12513 if (!DR_state->mInited) DR_state->Init();
12514 if (!DR_state->mActive) return nullptr;
12516 // aState is not necessarily a ReflowInput
12517 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12518 if (treeNode && treeNode->mDisplay) {
12519 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12521 char pctBasisStr[16];
12522 DR_state->PrettyUC(aPercentBasis, pctBasisStr, 16);
12523 printf("InitOffsets pct_basis=%s", pctBasisStr);
12525 DR_state->PrintMargin("b", aBorder);
12526 DR_state->PrintMargin("p", aPadding);
12527 putchar('\n');
12529 return treeNode;
12532 /* static */
12533 void SizeComputationInput::DisplayInitOffsetsExit(nsIFrame* aFrame,
12534 SizeComputationInput* aState,
12535 void* aValue) {
12536 MOZ_ASSERT(aFrame, "non-null frame required");
12537 MOZ_ASSERT(aState, "non-null state required");
12539 if (!DR_state->mActive) return;
12540 if (!aValue) return;
12542 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aValue;
12543 if (treeNode->mDisplay) {
12544 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12545 printf("InitOffsets=");
12546 const auto m = aState->ComputedPhysicalMargin();
12547 DR_state->PrintMargin("m", &m);
12548 const auto p = aState->ComputedPhysicalPadding();
12549 DR_state->PrintMargin("p", &p);
12550 const auto bp = aState->ComputedPhysicalBorderPadding();
12551 DR_state->PrintMargin("b+p", &bp);
12552 putchar('\n');
12554 DR_state->DeleteTreeNode(*treeNode);
12557 // End Display Reflow
12559 // Validation of SideIsVertical.
12560 # define CASE(side, result) \
12561 static_assert(SideIsVertical(side) == result, "SideIsVertical is wrong")
12562 CASE(eSideTop, false);
12563 CASE(eSideRight, true);
12564 CASE(eSideBottom, false);
12565 CASE(eSideLeft, true);
12566 # undef CASE
12568 // Validation of HalfCornerIsX.
12569 # define CASE(corner, result) \
12570 static_assert(HalfCornerIsX(corner) == result, "HalfCornerIsX is wrong")
12571 CASE(eCornerTopLeftX, true);
12572 CASE(eCornerTopLeftY, false);
12573 CASE(eCornerTopRightX, true);
12574 CASE(eCornerTopRightY, false);
12575 CASE(eCornerBottomRightX, true);
12576 CASE(eCornerBottomRightY, false);
12577 CASE(eCornerBottomLeftX, true);
12578 CASE(eCornerBottomLeftY, false);
12579 # undef CASE
12581 // Validation of HalfToFullCorner.
12582 # define CASE(corner, result) \
12583 static_assert(HalfToFullCorner(corner) == result, \
12584 "HalfToFullCorner is " \
12585 "wrong")
12586 CASE(eCornerTopLeftX, eCornerTopLeft);
12587 CASE(eCornerTopLeftY, eCornerTopLeft);
12588 CASE(eCornerTopRightX, eCornerTopRight);
12589 CASE(eCornerTopRightY, eCornerTopRight);
12590 CASE(eCornerBottomRightX, eCornerBottomRight);
12591 CASE(eCornerBottomRightY, eCornerBottomRight);
12592 CASE(eCornerBottomLeftX, eCornerBottomLeft);
12593 CASE(eCornerBottomLeftY, eCornerBottomLeft);
12594 # undef CASE
12596 // Validation of FullToHalfCorner.
12597 # define CASE(corner, vert, result) \
12598 static_assert(FullToHalfCorner(corner, vert) == result, \
12599 "FullToHalfCorner is wrong")
12600 CASE(eCornerTopLeft, false, eCornerTopLeftX);
12601 CASE(eCornerTopLeft, true, eCornerTopLeftY);
12602 CASE(eCornerTopRight, false, eCornerTopRightX);
12603 CASE(eCornerTopRight, true, eCornerTopRightY);
12604 CASE(eCornerBottomRight, false, eCornerBottomRightX);
12605 CASE(eCornerBottomRight, true, eCornerBottomRightY);
12606 CASE(eCornerBottomLeft, false, eCornerBottomLeftX);
12607 CASE(eCornerBottomLeft, true, eCornerBottomLeftY);
12608 # undef CASE
12610 // Validation of SideToFullCorner.
12611 # define CASE(side, second, result) \
12612 static_assert(SideToFullCorner(side, second) == result, \
12613 "SideToFullCorner is wrong")
12614 CASE(eSideTop, false, eCornerTopLeft);
12615 CASE(eSideTop, true, eCornerTopRight);
12617 CASE(eSideRight, false, eCornerTopRight);
12618 CASE(eSideRight, true, eCornerBottomRight);
12620 CASE(eSideBottom, false, eCornerBottomRight);
12621 CASE(eSideBottom, true, eCornerBottomLeft);
12623 CASE(eSideLeft, false, eCornerBottomLeft);
12624 CASE(eSideLeft, true, eCornerTopLeft);
12625 # undef CASE
12627 // Validation of SideToHalfCorner.
12628 # define CASE(side, second, parallel, result) \
12629 static_assert(SideToHalfCorner(side, second, parallel) == result, \
12630 "SideToHalfCorner is wrong")
12631 CASE(eSideTop, false, true, eCornerTopLeftX);
12632 CASE(eSideTop, false, false, eCornerTopLeftY);
12633 CASE(eSideTop, true, true, eCornerTopRightX);
12634 CASE(eSideTop, true, false, eCornerTopRightY);
12636 CASE(eSideRight, false, false, eCornerTopRightX);
12637 CASE(eSideRight, false, true, eCornerTopRightY);
12638 CASE(eSideRight, true, false, eCornerBottomRightX);
12639 CASE(eSideRight, true, true, eCornerBottomRightY);
12641 CASE(eSideBottom, false, true, eCornerBottomRightX);
12642 CASE(eSideBottom, false, false, eCornerBottomRightY);
12643 CASE(eSideBottom, true, true, eCornerBottomLeftX);
12644 CASE(eSideBottom, true, false, eCornerBottomLeftY);
12646 CASE(eSideLeft, false, false, eCornerBottomLeftX);
12647 CASE(eSideLeft, false, true, eCornerBottomLeftY);
12648 CASE(eSideLeft, true, false, eCornerTopLeftX);
12649 CASE(eSideLeft, true, true, eCornerTopLeftY);
12650 # undef CASE
12652 #endif