Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / generic / nsIFrame.cpp
blob0b276cc7d9c4e958e2459973961a8b0eb8ad51aa
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/intl/BidiEmbeddingLevel.h"
32 #include "mozilla/Maybe.h"
33 #include "mozilla/PresShell.h"
34 #include "mozilla/PresShellInlines.h"
35 #include "mozilla/ResultExtensions.h"
36 #include "mozilla/Sprintf.h"
37 #include "mozilla/StaticAnalysisFunctions.h"
38 #include "mozilla/StaticPrefs_layout.h"
39 #include "mozilla/StaticPrefs_print.h"
40 #include "mozilla/StaticPrefs_ui.h"
41 #include "mozilla/SVGMaskFrame.h"
42 #include "mozilla/SVGObserverUtils.h"
43 #include "mozilla/SVGTextFrame.h"
44 #include "mozilla/SVGIntegrationUtils.h"
45 #include "mozilla/SVGUtils.h"
46 #include "mozilla/TextControlElement.h"
47 #include "mozilla/ToString.h"
48 #include "mozilla/Try.h"
49 #include "mozilla/ViewportUtils.h"
51 #include "nsCOMPtr.h"
52 #include "nsFieldSetFrame.h"
53 #include "nsFlexContainerFrame.h"
54 #include "nsFocusManager.h"
55 #include "nsFrameList.h"
56 #include "nsPlaceholderFrame.h"
57 #include "nsIBaseWindow.h"
58 #include "nsIContent.h"
59 #include "nsIContentInlines.h"
60 #include "nsContentUtils.h"
61 #include "nsCSSFrameConstructor.h"
62 #include "nsCSSProps.h"
63 #include "nsCSSPseudoElements.h"
64 #include "nsCSSRendering.h"
65 #include "nsAtom.h"
66 #include "nsString.h"
67 #include "nsReadableUtils.h"
68 #include "nsTableWrapperFrame.h"
69 #include "nsView.h"
70 #include "nsViewManager.h"
71 #include "nsIScrollableFrame.h"
72 #include "nsPresContext.h"
73 #include "nsPresContextInlines.h"
74 #include "nsStyleConsts.h"
75 #include "mozilla/Logging.h"
76 #include "nsLayoutUtils.h"
77 #include "LayoutLogging.h"
78 #include "mozilla/RestyleManager.h"
79 #include "nsImageFrame.h"
80 #include "nsInlineFrame.h"
81 #include "nsFrameSelection.h"
82 #include "nsGkAtoms.h"
83 #include "nsGridContainerFrame.h"
84 #include "nsGfxScrollFrame.h"
85 #include "nsCSSAnonBoxes.h"
86 #include "nsCanvasFrame.h"
88 #include "nsFieldSetFrame.h"
89 #include "nsFrameTraversal.h"
90 #include "nsRange.h"
91 #include "nsITextControlFrame.h"
92 #include "nsNameSpaceManager.h"
93 #include "nsIPercentBSizeObserver.h"
94 #include "nsStyleStructInlines.h"
96 #include "nsBidiPresUtils.h"
97 #include "RubyUtils.h"
98 #include "TextOverflow.h"
99 #include "nsAnimationManager.h"
101 // For triple-click pref
102 #include "imgIRequest.h"
103 #include "nsError.h"
104 #include "nsContainerFrame.h"
105 #include "nsBlockFrame.h"
106 #include "nsDisplayList.h"
107 #include "nsChangeHint.h"
108 #include "nsSubDocumentFrame.h"
109 #include "RetainedDisplayListBuilder.h"
111 #include "gfxContext.h"
112 #include "nsAbsoluteContainingBlock.h"
113 #include "ScrollSnap.h"
114 #include "StickyScrollContainer.h"
115 #include "nsFontInflationData.h"
116 #include "nsRegion.h"
117 #include "nsIFrameInlines.h"
118 #include "nsStyleChangeList.h"
119 #include "nsWindowSizes.h"
121 #ifdef ACCESSIBILITY
122 # include "nsAccessibilityService.h"
123 #endif
125 #include "mozilla/AsyncEventDispatcher.h"
126 #include "mozilla/CSSClipPathInstance.h"
127 #include "mozilla/EffectCompositor.h"
128 #include "mozilla/EffectSet.h"
129 #include "mozilla/EventListenerManager.h"
130 #include "mozilla/EventStateManager.h"
131 #include "mozilla/Preferences.h"
132 #include "mozilla/LookAndFeel.h"
133 #include "mozilla/MouseEvents.h"
134 #include "mozilla/ServoStyleSet.h"
135 #include "mozilla/ServoStyleSetInlines.h"
136 #include "mozilla/css/ImageLoader.h"
137 #include "mozilla/dom/HTMLBodyElement.h"
138 #include "mozilla/dom/SVGPathData.h"
139 #include "mozilla/dom/TouchEvent.h"
140 #include "mozilla/gfx/Tools.h"
141 #include "mozilla/layers/WebRenderUserData.h"
142 #include "mozilla/layout/ScrollAnchorContainer.h"
143 #include "nsPrintfCString.h"
144 #include "ActiveLayerTracker.h"
146 #include "nsITheme.h"
148 using namespace mozilla;
149 using namespace mozilla::css;
150 using namespace mozilla::dom;
151 using namespace mozilla::gfx;
152 using namespace mozilla::layers;
153 using namespace mozilla::layout;
154 typedef nsAbsoluteContainingBlock::AbsPosReflowFlags AbsPosReflowFlags;
155 using nsStyleTransformMatrix::TransformReferenceBox;
157 const mozilla::LayoutFrameType nsIFrame::sLayoutFrameTypes[
158 #define FRAME_ID(...) 1 +
159 #define ABSTRACT_FRAME_ID(...)
160 #include "mozilla/FrameIdList.h"
161 #undef FRAME_ID
162 #undef ABSTRACT_FRAME_ID
163 0] = {
164 #define FRAME_ID(class_, type_, ...) mozilla::LayoutFrameType::type_,
165 #define ABSTRACT_FRAME_ID(...)
166 #include "mozilla/FrameIdList.h"
167 #undef FRAME_ID
168 #undef ABSTRACT_FRAME_ID
171 const nsIFrame::FrameClassBits nsIFrame::sFrameClassBits[
172 #define FRAME_ID(...) 1 +
173 #define ABSTRACT_FRAME_ID(...)
174 #include "mozilla/FrameIdList.h"
175 #undef FRAME_ID
176 #undef ABSTRACT_FRAME_ID
177 0] = {
178 #define Leaf eFrameClassBitsLeaf
179 #define NotLeaf eFrameClassBitsNone
180 #define DynamicLeaf eFrameClassBitsDynamicLeaf
181 #define FRAME_ID(class_, type_, leaf_, ...) leaf_,
182 #define ABSTRACT_FRAME_ID(...)
183 #include "mozilla/FrameIdList.h"
184 #undef Leaf
185 #undef NotLeaf
186 #undef DynamicLeaf
187 #undef FRAME_ID
188 #undef ABSTRACT_FRAME_ID
191 std::ostream& operator<<(std::ostream& aStream, const nsDirection& aDirection) {
192 return aStream << (aDirection == eDirNext ? "eDirNext" : "eDirPrevious");
195 struct nsContentAndOffset {
196 nsIContent* mContent = nullptr;
197 int32_t mOffset = 0;
200 // Some Misc #defines
201 #define SELECTION_DEBUG 0
202 #define FORCE_SELECTION_UPDATE 1
203 #define CALC_DEBUG 0
205 #include "nsILineIterator.h"
206 #include "prenv.h"
208 // Utility function to set a nsRect-valued property table entry on aFrame,
209 // reusing the existing storage if the property happens to be already set.
210 template <typename T>
211 static void SetOrUpdateRectValuedProperty(
212 nsIFrame* aFrame, FrameProperties::Descriptor<T> aProperty,
213 const nsRect& aNewValue) {
214 bool found;
215 nsRect* rectStorage = aFrame->GetProperty(aProperty, &found);
216 if (!found) {
217 rectStorage = new nsRect(aNewValue);
218 aFrame->AddProperty(aProperty, rectStorage);
219 } else {
220 *rectStorage = aNewValue;
224 FrameDestroyContext::~FrameDestroyContext() {
225 for (auto& content : mozilla::Reversed(mAnonymousContent)) {
226 mPresShell->NativeAnonymousContentRemoved(content);
227 content->UnbindFromTree();
231 // Formerly the nsIFrameDebug interface
233 std::ostream& operator<<(std::ostream& aStream, const nsReflowStatus& aStatus) {
234 char complete = 'Y';
235 if (aStatus.IsIncomplete()) {
236 complete = 'N';
237 } else if (aStatus.IsOverflowIncomplete()) {
238 complete = 'O';
241 char brk = 'N';
242 if (aStatus.IsInlineBreakBefore()) {
243 brk = 'B';
244 } else if (aStatus.IsInlineBreakAfter()) {
245 brk = 'A';
248 aStream << "["
249 << "Complete=" << complete << ","
250 << "NIF=" << (aStatus.NextInFlowNeedsReflow() ? 'Y' : 'N') << ","
251 << "Break=" << brk << ","
252 << "FirstLetter=" << (aStatus.FirstLetterComplete() ? 'Y' : 'N')
253 << "]";
254 return aStream;
257 #ifdef DEBUG
260 * Note: the log module is created during library initialization which
261 * means that you cannot perform logging before then.
263 mozilla::LazyLogModule nsIFrame::sFrameLogModule("frame");
265 #endif
267 NS_DECLARE_FRAME_PROPERTY_DELETABLE(AbsoluteContainingBlockProperty,
268 nsAbsoluteContainingBlock)
270 bool nsIFrame::HasAbsolutelyPositionedChildren() const {
271 return IsAbsoluteContainer() &&
272 GetAbsoluteContainingBlock()->HasAbsoluteFrames();
275 nsAbsoluteContainingBlock* nsIFrame::GetAbsoluteContainingBlock() const {
276 NS_ASSERTION(IsAbsoluteContainer(),
277 "The frame is not marked as an abspos container correctly");
278 nsAbsoluteContainingBlock* absCB =
279 GetProperty(AbsoluteContainingBlockProperty());
280 NS_ASSERTION(absCB,
281 "The frame is marked as an abspos container but doesn't have "
282 "the property");
283 return absCB;
286 void nsIFrame::MarkAsAbsoluteContainingBlock() {
287 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN));
288 NS_ASSERTION(!GetProperty(AbsoluteContainingBlockProperty()),
289 "Already has an abs-pos containing block property?");
290 NS_ASSERTION(!HasAnyStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN),
291 "Already has NS_FRAME_HAS_ABSPOS_CHILDREN state bit?");
292 AddStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN);
293 SetProperty(AbsoluteContainingBlockProperty(),
294 new nsAbsoluteContainingBlock(GetAbsoluteListID()));
297 void nsIFrame::MarkAsNotAbsoluteContainingBlock() {
298 NS_ASSERTION(!HasAbsolutelyPositionedChildren(), "Think of the children!");
299 NS_ASSERTION(GetProperty(AbsoluteContainingBlockProperty()),
300 "Should have an abs-pos containing block property");
301 NS_ASSERTION(HasAnyStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN),
302 "Should have NS_FRAME_HAS_ABSPOS_CHILDREN state bit");
303 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN));
304 RemoveStateBits(NS_FRAME_HAS_ABSPOS_CHILDREN);
305 RemoveProperty(AbsoluteContainingBlockProperty());
308 bool nsIFrame::CheckAndClearPaintedState() {
309 bool result = HasAnyStateBits(NS_FRAME_PAINTED_THEBES);
310 RemoveStateBits(NS_FRAME_PAINTED_THEBES);
312 for (const auto& childList : ChildLists()) {
313 for (nsIFrame* child : childList.mList) {
314 if (child->CheckAndClearPaintedState()) {
315 result = true;
319 return result;
322 bool nsIFrame::CheckAndClearDisplayListState() {
323 bool result = BuiltDisplayList();
324 SetBuiltDisplayList(false);
326 for (const auto& childList : ChildLists()) {
327 for (nsIFrame* child : childList.mList) {
328 if (child->CheckAndClearDisplayListState()) {
329 result = true;
333 return result;
336 bool nsIFrame::IsVisibleConsideringAncestors(uint32_t aFlags) const {
337 if (!StyleVisibility()->IsVisible()) {
338 return false;
341 if (PresShell()->IsUnderHiddenEmbedderElement()) {
342 return false;
345 const nsIFrame* frame = this;
346 while (frame) {
347 nsView* view = frame->GetView();
348 if (view && view->GetVisibility() == ViewVisibility::Hide) {
349 return false;
352 if (frame->StyleUIReset()->mMozSubtreeHiddenOnlyVisually) {
353 return false;
356 // This method is used to determine if a frame is focusable, because it's
357 // called by nsIFrame::IsFocusable. `content-visibility: auto` should not
358 // force this frame to be unfocusable, so we only take into account
359 // `content-visibility: hidden` here.
360 if (this != frame &&
361 frame->HidesContent(IncludeContentVisibility::Hidden)) {
362 return false;
365 if (nsIFrame* parent = frame->GetParent()) {
366 frame = parent;
367 } else {
368 parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(frame);
369 if (!parent) break;
371 if ((aFlags & nsIFrame::VISIBILITY_CROSS_CHROME_CONTENT_BOUNDARY) == 0 &&
372 parent->PresContext()->IsChrome() &&
373 !frame->PresContext()->IsChrome()) {
374 break;
377 frame = parent;
381 return true;
384 void nsIFrame::FindCloserFrameForSelection(
385 const nsPoint& aPoint, FrameWithDistance* aCurrentBestFrame) {
386 if (nsLayoutUtils::PointIsCloserToRect(aPoint, mRect,
387 aCurrentBestFrame->mXDistance,
388 aCurrentBestFrame->mYDistance)) {
389 aCurrentBestFrame->mFrame = this;
393 void nsIFrame::ElementStateChanged(mozilla::dom::ElementState aStates) {}
395 void WeakFrame::Clear(mozilla::PresShell* aPresShell) {
396 if (aPresShell) {
397 aPresShell->RemoveWeakFrame(this);
399 mFrame = nullptr;
402 AutoWeakFrame::AutoWeakFrame(const WeakFrame& aOther)
403 : mPrev(nullptr), mFrame(nullptr) {
404 Init(aOther.GetFrame());
407 void AutoWeakFrame::Clear(mozilla::PresShell* aPresShell) {
408 if (aPresShell) {
409 aPresShell->RemoveAutoWeakFrame(this);
411 mFrame = nullptr;
412 mPrev = nullptr;
415 AutoWeakFrame::~AutoWeakFrame() {
416 Clear(mFrame ? mFrame->PresContext()->GetPresShell() : nullptr);
419 void AutoWeakFrame::Init(nsIFrame* aFrame) {
420 Clear(mFrame ? mFrame->PresContext()->GetPresShell() : nullptr);
421 mFrame = aFrame;
422 if (mFrame) {
423 mozilla::PresShell* presShell = mFrame->PresContext()->GetPresShell();
424 NS_WARNING_ASSERTION(presShell, "Null PresShell in AutoWeakFrame!");
425 if (presShell) {
426 presShell->AddAutoWeakFrame(this);
427 } else {
428 mFrame = nullptr;
433 void WeakFrame::Init(nsIFrame* aFrame) {
434 Clear(mFrame ? mFrame->PresContext()->GetPresShell() : nullptr);
435 mFrame = aFrame;
436 if (mFrame) {
437 mozilla::PresShell* presShell = mFrame->PresContext()->GetPresShell();
438 MOZ_ASSERT(presShell, "Null PresShell in WeakFrame!");
439 if (presShell) {
440 presShell->AddWeakFrame(this);
441 } else {
442 mFrame = nullptr;
447 nsIFrame* NS_NewEmptyFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
448 return new (aPresShell) nsIFrame(aStyle, aPresShell->GetPresContext());
451 nsIFrame::~nsIFrame() {
452 MOZ_COUNT_DTOR(nsIFrame);
454 MOZ_ASSERT(GetVisibility() != Visibility::ApproximatelyVisible,
455 "Visible nsFrame is being destroyed");
458 NS_IMPL_FRAMEARENA_HELPERS(nsIFrame)
460 // Dummy operator delete. Will never be called, but must be defined
461 // to satisfy some C++ ABIs.
462 void nsIFrame::operator delete(void*, size_t) {
463 MOZ_CRASH("nsIFrame::operator delete should never be called");
466 NS_QUERYFRAME_HEAD(nsIFrame)
467 NS_QUERYFRAME_ENTRY(nsIFrame)
468 NS_QUERYFRAME_TAIL_INHERITANCE_ROOT
470 /////////////////////////////////////////////////////////////////////////////
471 // nsIFrame
473 static bool IsFontSizeInflationContainer(nsIFrame* aFrame,
474 const nsStyleDisplay* aStyleDisplay) {
476 * Font size inflation is built around the idea that we're inflating
477 * the fonts for a pan-and-zoom UI so that when the user scales up a
478 * block or other container to fill the width of the device, the fonts
479 * will be readable. To do this, we need to pick what counts as a
480 * container.
482 * From a code perspective, the only hard requirement is that frames
483 * that are line participants
484 * (nsIFrame::IsFrameOfType(nsIFrame::eLineParticipant)) are never
485 * containers, since line layout assumes that the inflation is
486 * consistent within a line.
488 * This is not an imposition, since we obviously want a bunch of text
489 * (possibly with inline elements) flowing within a block to count the
490 * block (or higher) as its container.
492 * We also want form controls, including the text in the anonymous
493 * content inside of them, to match each other and the text next to
494 * them, so they and their anonymous content should also not be a
495 * container.
497 * However, because we can't reliably compute sizes across XUL during
498 * reflow, any XUL frame with a XUL parent is always a container.
500 * There are contexts where it would be nice if some blocks didn't
501 * count as a container, so that, for example, an indented quotation
502 * didn't end up with a smaller font size. However, it's hard to
503 * distinguish these situations where we really do want the indented
504 * thing to count as a container, so we don't try, and blocks are
505 * always containers.
508 // The root frame should always be an inflation container.
509 if (!aFrame->GetParent()) {
510 return true;
513 nsIContent* content = aFrame->GetContent();
514 if (content && content->IsInNativeAnonymousSubtree()) {
515 // Native anonymous content shouldn't be a font inflation root,
516 // except for the canvas custom content container.
517 nsCanvasFrame* canvas = aFrame->PresShell()->GetCanvasFrame();
518 return canvas && canvas->GetCustomContentContainer() == content;
521 LayoutFrameType frameType = aFrame->Type();
522 bool isInline =
523 aFrame->GetDisplay().IsInlineFlow() || RubyUtils::IsRubyBox(frameType) ||
524 (aStyleDisplay->IsFloatingStyle() &&
525 frameType == LayoutFrameType::Letter) ||
526 // Given multiple frames for the same node, only the
527 // outer one should be considered a container.
528 // (Important, e.g., for nsSelectsAreaFrame.)
529 (aFrame->GetParent()->GetContent() == content) ||
530 (content &&
531 // Form controls shouldn't become inflation containers.
532 (content->IsAnyOfHTMLElements(nsGkAtoms::option, nsGkAtoms::optgroup,
533 nsGkAtoms::select, nsGkAtoms::input,
534 nsGkAtoms::button, nsGkAtoms::textarea)));
535 NS_ASSERTION(!aFrame->IsFrameOfType(nsIFrame::eLineParticipant) || isInline ||
536 // br frames and mathml frames report being line
537 // participants even when their position or display is
538 // set
539 aFrame->IsBrFrame() ||
540 aFrame->IsFrameOfType(nsIFrame::eMathML),
541 "line participants must not be containers");
542 return !isInline;
545 static void MaybeScheduleReflowSVGNonDisplayText(nsIFrame* aFrame) {
546 if (!aFrame->IsInSVGTextSubtree()) {
547 return;
550 // We need to ensure that any non-display SVGTextFrames get reflowed when a
551 // child text frame gets new style. Thus we need to schedule a reflow in
552 // |DidSetComputedStyle|. We also need to call it from |DestroyFrom|,
553 // because otherwise we won't get notified when style changes to
554 // "display:none".
555 SVGTextFrame* svgTextFrame = static_cast<SVGTextFrame*>(
556 nsLayoutUtils::GetClosestFrameOfType(aFrame, LayoutFrameType::SVGText));
557 nsIFrame* anonBlock = svgTextFrame->PrincipalChildList().FirstChild();
559 // Note that we must check NS_FRAME_FIRST_REFLOW on our SVGTextFrame's
560 // anonymous block frame rather than our aFrame, since NS_FRAME_FIRST_REFLOW
561 // may be set on us if we're a new frame that has been inserted after the
562 // document's first reflow. (In which case this DidSetComputedStyle call may
563 // be happening under frame construction under a Reflow() call.)
564 if (!anonBlock || anonBlock->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
565 return;
568 if (!svgTextFrame->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY) ||
569 svgTextFrame->HasAnyStateBits(NS_STATE_SVG_TEXT_IN_REFLOW)) {
570 return;
573 svgTextFrame->ScheduleReflowSVGNonDisplayText(
574 IntrinsicDirty::FrameAncestorsAndDescendants);
577 bool nsIFrame::IsPrimaryFrameOfRootOrBodyElement() const {
578 if (!IsPrimaryFrame()) {
579 return false;
581 nsIContent* content = GetContent();
582 Document* document = content->OwnerDoc();
583 return content == document->GetRootElement() ||
584 content == document->GetBodyElement();
587 bool nsIFrame::IsRenderedLegend() const {
588 if (auto* parent = GetParent(); parent && parent->IsFieldSetFrame()) {
589 return static_cast<nsFieldSetFrame*>(parent)->GetLegend() == this;
591 return false;
594 void nsIFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
595 nsIFrame* aPrevInFlow) {
596 MOZ_ASSERT(nsQueryFrame::FrameIID(mClass) == GetFrameId());
597 MOZ_ASSERT(!mContent, "Double-initing a frame?");
598 NS_ASSERTION(IsFrameOfType(eDEBUGAllFrames) && !IsFrameOfType(eDEBUGNoFrames),
599 "IsFrameOfType implementation that doesn't call base class");
601 mContent = aContent;
602 mParent = aParent;
603 MOZ_DIAGNOSTIC_ASSERT(!mParent || PresShell() == mParent->PresShell());
605 if (aPrevInFlow) {
606 mWritingMode = aPrevInFlow->GetWritingMode();
608 // Copy some state bits from prev-in-flow (the bits that should apply
609 // throughout a continuation chain). The bits are sorted according to their
610 // order in nsFrameStateBits.h.
612 // clang-format off
613 AddStateBits(aPrevInFlow->GetStateBits() &
614 (NS_FRAME_GENERATED_CONTENT |
615 NS_FRAME_OUT_OF_FLOW |
616 NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN |
617 NS_FRAME_INDEPENDENT_SELECTION |
618 NS_FRAME_PART_OF_IBSPLIT |
619 NS_FRAME_MAY_BE_TRANSFORMED |
620 NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR));
621 // clang-format on
623 // Copy other bits in nsIFrame from prev-in-flow.
624 mHasColumnSpanSiblings = aPrevInFlow->HasColumnSpanSiblings();
625 } else {
626 PresContext()->ConstructedFrame();
629 if (GetParent()) {
630 if (MOZ_UNLIKELY(mContent == PresContext()->Document()->GetRootElement() &&
631 mContent == GetParent()->GetContent())) {
632 // Our content is the root element and we have the same content as our
633 // parent. That is, we are the internal anonymous frame of the root
634 // element. Copy the used mWritingMode from our parent because
635 // mDocElementContainingBlock gets its mWritingMode from <body>.
636 mWritingMode = GetParent()->GetWritingMode();
639 // Copy some state bits from our parent (the bits that should apply
640 // recursively throughout a subtree). The bits are sorted according to their
641 // order in nsFrameStateBits.h.
643 // clang-format off
644 AddStateBits(GetParent()->GetStateBits() &
645 (NS_FRAME_GENERATED_CONTENT |
646 NS_FRAME_INDEPENDENT_SELECTION |
647 NS_FRAME_IS_SVG_TEXT |
648 NS_FRAME_IN_POPUP |
649 NS_FRAME_IS_NONDISPLAY));
650 // clang-format on
652 if (HasAnyStateBits(NS_FRAME_IN_POPUP) && TrackingVisibility()) {
653 // Assume all frames in popups are visible.
654 IncApproximateVisibleCount();
657 if (aPrevInFlow) {
658 mMayHaveOpacityAnimation = aPrevInFlow->MayHaveOpacityAnimation();
659 mMayHaveTransformAnimation = aPrevInFlow->MayHaveTransformAnimation();
660 } else if (mContent) {
661 // It's fine to fetch the EffectSet for the style frame here because in the
662 // following code we take care of the case where animations may target
663 // a different frame.
664 EffectSet* effectSet = EffectSet::GetForStyleFrame(this);
665 if (effectSet) {
666 mMayHaveOpacityAnimation = effectSet->MayHaveOpacityAnimation();
668 if (effectSet->MayHaveTransformAnimation()) {
669 // If we are the inner table frame for display:table content, then
670 // transform animations should go on our parent frame (the table wrapper
671 // frame).
673 // We do this when initializing the child frame (table inner frame),
674 // because when initializng the table wrapper frame, we don't yet have
675 // access to its children so we can't tell if we have transform
676 // animations or not.
677 if (IsFrameOfType(eSupportsCSSTransforms)) {
678 mMayHaveTransformAnimation = true;
679 AddStateBits(NS_FRAME_MAY_BE_TRANSFORMED);
680 } else if (aParent && nsLayoutUtils::GetStyleFrame(aParent) == this) {
681 MOZ_ASSERT(
682 aParent->IsFrameOfType(eSupportsCSSTransforms),
683 "Style frames that don't support transforms should have parents"
684 " that do");
685 aParent->mMayHaveTransformAnimation = true;
686 aParent->AddStateBits(NS_FRAME_MAY_BE_TRANSFORMED);
692 const nsStyleDisplay* disp = StyleDisplay();
693 if (disp->HasTransform(this)) {
694 // If 'transform' dynamically changes, RestyleManager takes care of
695 // updating this bit.
696 AddStateBits(NS_FRAME_MAY_BE_TRANSFORMED);
699 if (nsLayoutUtils::FontSizeInflationEnabled(PresContext()) ||
700 !GetParent()
701 #ifdef DEBUG
702 // We have assertions that check inflation invariants even when
703 // font size inflation is not enabled.
704 || true
705 #endif
707 if (IsFontSizeInflationContainer(this, disp)) {
708 AddStateBits(NS_FRAME_FONT_INFLATION_CONTAINER);
709 if (!GetParent() ||
710 // I'd use NS_FRAME_OUT_OF_FLOW, but it's not set yet.
711 disp->IsFloating(this) || disp->IsAbsolutelyPositioned(this) ||
712 GetParent()->IsFlexContainerFrame() ||
713 GetParent()->IsGridContainerFrame()) {
714 AddStateBits(NS_FRAME_FONT_INFLATION_FLOW_ROOT);
717 NS_ASSERTION(
718 GetParent() || HasAnyStateBits(NS_FRAME_FONT_INFLATION_CONTAINER),
719 "root frame should always be a container");
722 if (PresShell()->AssumeAllFramesVisible() && TrackingVisibility()) {
723 IncApproximateVisibleCount();
726 DidSetComputedStyle(nullptr);
728 // For a newly created frame, we need to update this frame's visibility state.
729 // Usually we update the state when the frame is restyled and has a
730 // VisibilityChange change hint but we don't generate any change hints for
731 // newly created frames.
732 // Note: We don't need to do this for placeholders since placeholders have
733 // different styles so that the styles don't have visibility:hidden even if
734 // the parent has visibility:hidden style. We also don't need to update the
735 // state when creating continuations because its visibility is the same as its
736 // prev-in-flow, and the animation code cares only primary frames.
737 if (!IsPlaceholderFrame() && !aPrevInFlow) {
738 UpdateVisibleDescendantsState();
742 void nsIFrame::InitPrimaryFrame() {
743 MOZ_ASSERT(IsPrimaryFrame());
744 const nsStyleDisplay* disp = StyleDisplay();
746 if (disp->mContainerType != StyleContainerType::Normal) {
747 PresContext()->RegisterContainerQueryFrame(this);
750 if (StyleDisplay()->ContentVisibility(*this) ==
751 StyleContentVisibility::Auto) {
752 PresShell()->RegisterContentVisibilityAutoFrame(this);
753 auto* element = Element::FromNodeOrNull(GetContent());
754 MOZ_ASSERT(element);
755 PresContext()->Document()->ObserveForContentVisibility(*element);
756 } else if (auto* element = Element::FromNodeOrNull(GetContent())) {
757 element->ClearContentRelevancy();
760 // TODO(mrobinson): Once bug 1765615 is fixed, this should be called on
761 // layout changes. In addition, when `content-visibility: auto` is implemented
762 // this should also be called when scrolling or focus causes content to be
763 // skipped or unskipped.
764 UpdateAnimationVisibility();
766 HandleLastRememberedSize();
769 void nsIFrame::Destroy(DestroyContext& aContext) {
770 NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
771 "destroy called on frame while scripts not blocked");
772 NS_ASSERTION(!GetNextSibling() && !GetPrevSibling(),
773 "Frames should be removed before destruction.");
774 MOZ_ASSERT(!HasAbsolutelyPositionedChildren());
775 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT),
776 "NS_FRAME_PART_OF_IBSPLIT set on non-nsContainerFrame?");
778 MaybeScheduleReflowSVGNonDisplayText(this);
780 SVGObserverUtils::InvalidateDirectRenderingObservers(this);
782 const auto* disp = StyleDisplay();
783 if (disp->mPosition == StylePositionProperty::Sticky) {
784 if (auto* ssc =
785 StickyScrollContainer::GetStickyScrollContainerForFrame(this)) {
786 ssc->RemoveFrame(this);
790 if (disp->mContainerType != StyleContainerType::Normal) {
791 PresContext()->UnregisterContainerQueryFrame(this);
794 nsPresContext* presContext = PresContext();
795 mozilla::PresShell* presShell = presContext->GetPresShell();
796 if (HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
797 if (nsPlaceholderFrame* placeholder = GetPlaceholderFrame()) {
798 placeholder->SetOutOfFlowFrame(nullptr);
802 if (IsPrimaryFrame()) {
803 // This needs to happen before we clear our Properties() table.
804 ActiveLayerTracker::TransferActivityToContent(this, mContent);
807 ScrollAnchorContainer* anchor = nullptr;
808 if (IsScrollAnchor(&anchor)) {
809 anchor->InvalidateAnchor();
812 if (HasCSSAnimations() || HasCSSTransitions() ||
813 // It's fine to look up the style frame here since if we're destroying the
814 // frames for display:table content we should be destroying both wrapper
815 // and inner frame.
816 EffectSet::GetForStyleFrame(this)) {
817 // If no new frame for this element is created by the end of the
818 // restyling process, stop animations and transitions for this frame
819 RestyleManager::AnimationsWithDestroyedFrame* adf =
820 presContext->RestyleManager()->GetAnimationsWithDestroyedFrame();
821 // AnimationsWithDestroyedFrame only lives during the restyling process.
822 if (adf) {
823 adf->Put(mContent, mComputedStyle);
827 if (StyleDisplay()->ContentVisibility(*this) ==
828 StyleContentVisibility::Auto) {
829 if (auto* element = Element::FromNodeOrNull(GetContent())) {
830 PresContext()->Document()->UnobserveForContentVisibility(*element);
834 // Disable visibility tracking. Note that we have to do this before we clear
835 // frame properties and lose track of whether we were previously visible.
836 // XXX(seth): It'd be ideal to assert that we're already marked nonvisible
837 // here, but it's unfortunately tricky to guarantee in the face of things like
838 // frame reconstruction induced by style changes.
839 DisableVisibilityTracking();
841 // Ensure that we're not in the approximately visible list anymore.
842 PresContext()->GetPresShell()->RemoveFrameFromApproximatelyVisibleList(this);
844 presShell->NotifyDestroyingFrame(this);
846 if (HasAnyStateBits(NS_FRAME_EXTERNAL_REFERENCE)) {
847 presShell->ClearFrameRefs(this);
850 nsView* view = GetView();
851 if (view) {
852 view->SetFrame(nullptr);
853 view->Destroy();
856 // Make sure that our deleted frame can't be returned from GetPrimaryFrame()
857 if (IsPrimaryFrame()) {
858 mContent->SetPrimaryFrame(nullptr);
860 // Pass the root of a generated content subtree (e.g. ::after/::before) to
861 // aPostDestroyData to unbind it after frame destruction is done.
862 if (HasAnyStateBits(NS_FRAME_GENERATED_CONTENT) &&
863 mContent->IsRootOfNativeAnonymousSubtree()) {
864 aContext.AddAnonymousContent(mContent.forget());
868 // Remove all properties attached to the frame, to ensure any property
869 // destructors that need the frame pointer are handled properly.
870 RemoveAllProperties();
872 // Must retrieve the object ID before calling destructors, so the
873 // vtable is still valid.
875 // Note to future tweakers: having the method that returns the
876 // object size call the destructor will not avoid an indirect call;
877 // the compiler cannot devirtualize the call to the destructor even
878 // if it's from a method defined in the same class.
880 nsQueryFrame::FrameIID id = GetFrameId();
881 this->~nsIFrame();
883 #ifdef DEBUG
885 nsIFrame* rootFrame = presShell->GetRootFrame();
886 MOZ_ASSERT(rootFrame);
887 if (this != rootFrame) {
888 auto* builder = nsLayoutUtils::GetRetainedDisplayListBuilder(rootFrame);
889 auto* data = builder ? builder->Data() : nullptr;
891 const bool inData =
892 data && (data->IsModified(this) || data->HasProps(this));
894 if (inData) {
895 DL_LOG(LogLevel::Warning, "Frame %p found in retained data", this);
898 MOZ_ASSERT(!inData, "Deleted frame in retained data!");
901 #endif
903 // Now that we're totally cleaned out, we need to add ourselves to
904 // the presshell's recycler.
905 presShell->FreeFrame(id, this);
908 std::pair<int32_t, int32_t> nsIFrame::GetOffsets() const {
909 return std::make_pair(0, 0);
912 static void CompareLayers(
913 const nsStyleImageLayers* aFirstLayers,
914 const nsStyleImageLayers* aSecondLayers,
915 const std::function<void(imgRequestProxy* aReq)>& aCallback) {
916 NS_FOR_VISIBLE_IMAGE_LAYERS_BACK_TO_FRONT(i, (*aFirstLayers)) {
917 const auto& image = aFirstLayers->mLayers[i].mImage;
918 if (!image.IsImageRequestType() || !image.IsResolved()) {
919 continue;
922 // aCallback is called when the style image in aFirstLayers is thought to
923 // be different with the corresponded one in aSecondLayers
924 if (!aSecondLayers || i >= aSecondLayers->mImageCount ||
925 (!aSecondLayers->mLayers[i].mImage.IsResolved() ||
926 image.GetImageRequest() !=
927 aSecondLayers->mLayers[i].mImage.GetImageRequest())) {
928 if (imgRequestProxy* req = image.GetImageRequest()) {
929 aCallback(req);
935 static void AddAndRemoveImageAssociations(
936 ImageLoader& aImageLoader, nsIFrame* aFrame,
937 const nsStyleImageLayers* aOldLayers,
938 const nsStyleImageLayers* aNewLayers) {
939 // If the old context had a background-image image, or mask-image image,
940 // and new context does not have the same image, clear the image load
941 // notifier (which keeps the image loading, if it still is) for the frame.
942 // We want to do this conservatively because some frames paint their
943 // backgrounds from some other frame's style data, and we don't want
944 // to clear those notifiers unless we have to. (They'll be reset
945 // when we paint, although we could miss a notification in that
946 // interval.)
947 if (aOldLayers && aFrame->HasImageRequest()) {
948 CompareLayers(aOldLayers, aNewLayers, [&](imgRequestProxy* aReq) {
949 aImageLoader.DisassociateRequestFromFrame(aReq, aFrame);
953 CompareLayers(aNewLayers, aOldLayers, [&](imgRequestProxy* aReq) {
954 aImageLoader.AssociateRequestToFrame(aReq, aFrame);
958 void nsIFrame::AddDisplayItem(nsDisplayItem* aItem) {
959 MOZ_DIAGNOSTIC_ASSERT(!mDisplayItems.Contains(aItem));
960 mDisplayItems.AppendElement(aItem);
963 bool nsIFrame::RemoveDisplayItem(nsDisplayItem* aItem) {
964 return mDisplayItems.RemoveElement(aItem);
967 bool nsIFrame::HasDisplayItems() { return !mDisplayItems.IsEmpty(); }
969 bool nsIFrame::HasDisplayItem(nsDisplayItem* aItem) {
970 return mDisplayItems.Contains(aItem);
973 bool nsIFrame::HasDisplayItem(uint32_t aKey) {
974 for (nsDisplayItem* i : mDisplayItems) {
975 if (i->GetPerFrameKey() == aKey) {
976 return true;
979 return false;
982 template <typename Condition>
983 static void DiscardDisplayItems(nsIFrame* aFrame, Condition aCondition) {
984 for (nsDisplayItem* i : aFrame->DisplayItems()) {
985 // Only discard items that are invalidated by this frame, as we're only
986 // guaranteed to rebuild those items. Table background items are created by
987 // the relevant table part, but have the cell frame as the primary frame,
988 // and we don't want to remove them if this is the cell.
989 if (aCondition(i) && i->FrameForInvalidation() == aFrame) {
990 i->SetCantBeReused();
995 static void DiscardOldItems(nsIFrame* aFrame) {
996 DiscardDisplayItems(aFrame,
997 [](nsDisplayItem* aItem) { return aItem->IsOldItem(); });
1000 void nsIFrame::RemoveDisplayItemDataForDeletion() {
1001 // Destroying a WebRenderUserDataTable can cause destruction of other objects
1002 // which can remove frame properties in their destructor. If we delete a frame
1003 // property it runs the destructor of the stored object in the middle of
1004 // updating the frame property table, so if the destruction of that object
1005 // causes another update to the frame property table it would leave the frame
1006 // property table in an inconsistent state. So we remove it from the table and
1007 // then destroy it. (bug 1530657)
1008 WebRenderUserDataTable* userDataTable =
1009 TakeProperty(WebRenderUserDataProperty::Key());
1010 if (userDataTable) {
1011 for (const auto& data : userDataTable->Values()) {
1012 data->RemoveFromTable();
1014 delete userDataTable;
1017 if (!nsLayoutUtils::AreRetainedDisplayListsEnabled()) {
1018 // Retained display lists are disabled, no need to update
1019 // RetainedDisplayListData.
1020 return;
1023 auto* builder = nsLayoutUtils::GetRetainedDisplayListBuilder(this);
1024 if (!builder) {
1025 MOZ_ASSERT(DisplayItems().IsEmpty());
1026 MOZ_ASSERT(!IsFrameModified());
1027 return;
1030 for (nsDisplayItem* i : DisplayItems()) {
1031 if (i->GetDependentFrame() == this && !i->HasDeletedFrame()) {
1032 i->Frame()->MarkNeedsDisplayItemRebuild();
1034 i->RemoveFrame(this);
1037 DisplayItems().Clear();
1039 nsAutoString name;
1040 #ifdef DEBUG_FRAME_DUMP
1041 if (DL_LOG_TEST(LogLevel::Debug)) {
1042 GetFrameName(name);
1044 #endif
1045 DL_LOGV("Removing display item data for frame %p (%s)", this,
1046 NS_ConvertUTF16toUTF8(name).get());
1048 auto* data = builder->Data();
1049 if (MayHaveWillChangeBudget()) {
1050 // Keep the frame in list, so it can be removed from the will-change budget.
1051 data->Flags(this) = RetainedDisplayListData::FrameFlag::HadWillChange;
1052 } else {
1053 data->Remove(this);
1057 void nsIFrame::MarkNeedsDisplayItemRebuild() {
1058 if (!nsLayoutUtils::AreRetainedDisplayListsEnabled() || IsFrameModified() ||
1059 HasAnyStateBits(NS_FRAME_IN_POPUP)) {
1060 // Skip frames that are already marked modified.
1061 return;
1064 if (Type() == LayoutFrameType::Placeholder) {
1065 nsIFrame* oof = static_cast<nsPlaceholderFrame*>(this)->GetOutOfFlowFrame();
1066 if (oof) {
1067 oof->MarkNeedsDisplayItemRebuild();
1069 // Do not mark placeholder frames modified.
1070 return;
1073 #ifdef ACCESSIBILITY
1074 if (nsAccessibilityService* accService = GetAccService()) {
1075 accService->NotifyOfPossibleBoundsChange(PresShell(), mContent);
1077 #endif
1079 nsIFrame* rootFrame = PresShell()->GetRootFrame();
1081 if (rootFrame->IsFrameModified()) {
1082 // The whole frame tree is modified.
1083 return;
1086 auto* builder = nsLayoutUtils::GetRetainedDisplayListBuilder(this);
1087 if (!builder) {
1088 MOZ_ASSERT(DisplayItems().IsEmpty());
1089 return;
1092 RetainedDisplayListData* data = builder->Data();
1093 MOZ_ASSERT(data);
1095 if (data->AtModifiedFrameLimit()) {
1096 // This marks the whole frame tree modified.
1097 // See |RetainedDisplayListBuilder::ShouldBuildPartial()|.
1098 data->AddModifiedFrame(rootFrame);
1099 return;
1102 nsAutoString name;
1103 #ifdef DEBUG_FRAME_DUMP
1104 if (DL_LOG_TEST(LogLevel::Debug)) {
1105 GetFrameName(name);
1107 #endif
1109 DL_LOGV("RDL - Rebuilding display items for frame %p (%s)", this,
1110 NS_ConvertUTF16toUTF8(name).get());
1112 data->AddModifiedFrame(this);
1114 MOZ_ASSERT(
1115 PresContext()->LayoutPhaseCount(nsLayoutPhase::DisplayListBuilding) == 0);
1117 // Hopefully this is cheap, but we could use a frame state bit to note
1118 // the presence of dependencies to speed it up.
1119 for (nsDisplayItem* i : DisplayItems()) {
1120 if (i->HasDeletedFrame() || i->Frame() == this) {
1121 // Ignore the items with deleted frames, and the items with |this| as
1122 // the primary frame.
1123 continue;
1126 if (i->GetDependentFrame() == this) {
1127 // For items with |this| as a dependent frame, mark the primary frame
1128 // for rebuild.
1129 i->Frame()->MarkNeedsDisplayItemRebuild();
1134 // Subclass hook for style post processing
1135 /* virtual */
1136 void nsIFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
1137 #ifdef ACCESSIBILITY
1138 // Don't notify for reconstructed frames here, since the frame is still being
1139 // constructed at this point and so LocalAccessible::GetFrame() will return
1140 // null. Style changes for reconstructed frames are handled in
1141 // DocAccessible::PruneOrInsertSubtree.
1142 if (aOldComputedStyle) {
1143 if (nsAccessibilityService* accService = GetAccService()) {
1144 accService->NotifyOfComputedStyleChange(PresShell(), mContent);
1147 #endif
1149 MaybeScheduleReflowSVGNonDisplayText(this);
1151 Document* doc = PresContext()->Document();
1152 ImageLoader* loader = doc->StyleImageLoader();
1153 // Continuing text frame doesn't initialize its continuation pointer before
1154 // reaching here for the first time, so we have to exclude text frames. This
1155 // doesn't affect correctness because text can't match selectors.
1157 // FIXME(emilio): We should consider fixing that.
1159 // TODO(emilio): Can we avoid doing some / all of the image stuff when
1160 // isNonTextFirstContinuation is false? We should consider doing this just for
1161 // primary frames and pseudos, but the first-line reparenting code makes it
1162 // all bad, should get around to bug 1465474 eventually :(
1163 const bool isNonText = !IsTextFrame();
1164 if (isNonText) {
1165 mComputedStyle->StartImageLoads(*doc, aOldComputedStyle);
1168 const nsStyleImageLayers* oldLayers =
1169 aOldComputedStyle ? &aOldComputedStyle->StyleBackground()->mImage
1170 : nullptr;
1171 const nsStyleImageLayers* newLayers = &StyleBackground()->mImage;
1172 AddAndRemoveImageAssociations(*loader, this, oldLayers, newLayers);
1174 oldLayers =
1175 aOldComputedStyle ? &aOldComputedStyle->StyleSVGReset()->mMask : nullptr;
1176 newLayers = &StyleSVGReset()->mMask;
1177 AddAndRemoveImageAssociations(*loader, this, oldLayers, newLayers);
1179 const nsStyleDisplay* disp = StyleDisplay();
1180 bool handleStickyChange = false;
1181 if (aOldComputedStyle) {
1182 // Detect style changes that should trigger a scroll anchor adjustment
1183 // suppression.
1184 // https://drafts.csswg.org/css-scroll-anchoring/#suppression-triggers
1185 bool needAnchorSuppression = false;
1187 // If we detect a change on margin, padding or border, we store the old
1188 // values on the frame itself between now and reflow, so if someone
1189 // calls GetUsed(Margin|Border|Padding)() before the next reflow, we
1190 // can give an accurate answer.
1191 // We don't want to set the property if one already exists.
1192 nsMargin oldValue(0, 0, 0, 0);
1193 nsMargin newValue(0, 0, 0, 0);
1194 const nsStyleMargin* oldMargin = aOldComputedStyle->StyleMargin();
1195 if (oldMargin->GetMargin(oldValue)) {
1196 if (!StyleMargin()->GetMargin(newValue) || oldValue != newValue) {
1197 if (!HasProperty(UsedMarginProperty())) {
1198 AddProperty(UsedMarginProperty(), new nsMargin(oldValue));
1200 needAnchorSuppression = true;
1204 const nsStylePadding* oldPadding = aOldComputedStyle->StylePadding();
1205 if (oldPadding->GetPadding(oldValue)) {
1206 if (!StylePadding()->GetPadding(newValue) || oldValue != newValue) {
1207 if (!HasProperty(UsedPaddingProperty())) {
1208 AddProperty(UsedPaddingProperty(), new nsMargin(oldValue));
1210 needAnchorSuppression = true;
1214 const nsStyleBorder* oldBorder = aOldComputedStyle->StyleBorder();
1215 oldValue = oldBorder->GetComputedBorder();
1216 newValue = StyleBorder()->GetComputedBorder();
1217 if (oldValue != newValue && !HasProperty(UsedBorderProperty())) {
1218 AddProperty(UsedBorderProperty(), new nsMargin(oldValue));
1221 const nsStyleDisplay* oldDisp = aOldComputedStyle->StyleDisplay();
1222 if (oldDisp->mOverflowAnchor != disp->mOverflowAnchor) {
1223 if (auto* container = ScrollAnchorContainer::FindFor(this)) {
1224 container->InvalidateAnchor();
1226 if (nsIScrollableFrame* scrollableFrame = do_QueryFrame(this)) {
1227 scrollableFrame->Anchor()->InvalidateAnchor();
1231 if (mInScrollAnchorChain) {
1232 const nsStylePosition* pos = StylePosition();
1233 const nsStylePosition* oldPos = aOldComputedStyle->StylePosition();
1234 if (!needAnchorSuppression &&
1235 (oldPos->mOffset != pos->mOffset || oldPos->mWidth != pos->mWidth ||
1236 oldPos->mMinWidth != pos->mMinWidth ||
1237 oldPos->mMaxWidth != pos->mMaxWidth ||
1238 oldPos->mHeight != pos->mHeight ||
1239 oldPos->mMinHeight != pos->mMinHeight ||
1240 oldPos->mMaxHeight != pos->mMaxHeight ||
1241 oldDisp->mPosition != disp->mPosition ||
1242 oldDisp->mTransform != disp->mTransform)) {
1243 needAnchorSuppression = true;
1246 if (needAnchorSuppression &&
1247 StaticPrefs::layout_css_scroll_anchoring_suppressions_enabled()) {
1248 ScrollAnchorContainer::FindFor(this)->SuppressAdjustments();
1252 if (disp->mPosition != oldDisp->mPosition) {
1253 if (!disp->IsRelativelyOrStickyPositionedStyle() &&
1254 oldDisp->IsRelativelyOrStickyPositionedStyle()) {
1255 RemoveProperty(NormalPositionProperty());
1258 handleStickyChange = disp->mPosition == StylePositionProperty::Sticky ||
1259 oldDisp->mPosition == StylePositionProperty::Sticky;
1261 if (disp->mScrollSnapAlign != oldDisp->mScrollSnapAlign) {
1262 ScrollSnapUtils::PostPendingResnapFor(this);
1264 if (aOldComputedStyle->IsRootElementStyle() &&
1265 disp->mScrollSnapType != oldDisp->mScrollSnapType) {
1266 if (nsIScrollableFrame* scrollableFrame =
1267 PresShell()->GetRootScrollFrameAsScrollable()) {
1268 scrollableFrame->PostPendingResnap();
1271 if (StyleUIReset()->mMozSubtreeHiddenOnlyVisually &&
1272 !aOldComputedStyle->StyleUIReset()->mMozSubtreeHiddenOnlyVisually) {
1273 PresShell::ClearMouseCapture(this);
1275 } else { // !aOldComputedStyle
1276 handleStickyChange = disp->mPosition == StylePositionProperty::Sticky;
1279 if (handleStickyChange && !HasAnyStateBits(NS_FRAME_IS_NONDISPLAY) &&
1280 !GetPrevInFlow()) {
1281 // Note that we only add first continuations, but we really only
1282 // want to add first continuation-or-ib-split-siblings. But since we don't
1283 // yet know if we're a later part of a block-in-inline split, we'll just
1284 // add later members of a block-in-inline split here, and then
1285 // StickyScrollContainer will remove them later.
1286 if (auto* ssc =
1287 StickyScrollContainer::GetStickyScrollContainerForFrame(this)) {
1288 if (disp->mPosition == StylePositionProperty::Sticky) {
1289 ssc->AddFrame(this);
1290 } else {
1291 ssc->RemoveFrame(this);
1296 imgIRequest* oldBorderImage =
1297 aOldComputedStyle
1298 ? aOldComputedStyle->StyleBorder()->GetBorderImageRequest()
1299 : nullptr;
1300 imgIRequest* newBorderImage = StyleBorder()->GetBorderImageRequest();
1301 // FIXME (Bug 759996): The following is no longer true.
1302 // For border-images, we can't be as conservative (we need to set the
1303 // new loaders if there has been any change) since the CalcDifference
1304 // call depended on the result of GetComputedBorder() and that result
1305 // depends on whether the image has loaded, start the image load now
1306 // so that we'll get notified when it completes loading and can do a
1307 // restyle. Otherwise, the image might finish loading from the
1308 // network before we start listening to its notifications, and then
1309 // we'll never know that it's finished loading. Likewise, we want to
1310 // do this for freshly-created frames to prevent a similar race if the
1311 // image loads between reflow (which can depend on whether the image
1312 // is loaded) and paint. We also don't really care about any callers who try
1313 // to paint borders with a different style, because they won't have the
1314 // correct size for the border either.
1315 if (oldBorderImage != newBorderImage) {
1316 // stop and restart the image loading/notification
1317 if (oldBorderImage && HasImageRequest()) {
1318 loader->DisassociateRequestFromFrame(oldBorderImage, this);
1320 if (newBorderImage) {
1321 loader->AssociateRequestToFrame(newBorderImage, this);
1325 auto GetShapeImageRequest = [](const ComputedStyle* aStyle) -> imgIRequest* {
1326 if (!aStyle) {
1327 return nullptr;
1329 auto& shape = aStyle->StyleDisplay()->mShapeOutside;
1330 if (!shape.IsImage()) {
1331 return nullptr;
1333 return shape.AsImage().GetImageRequest();
1336 imgIRequest* oldShapeImage = GetShapeImageRequest(aOldComputedStyle);
1337 imgIRequest* newShapeImage = GetShapeImageRequest(Style());
1338 if (oldShapeImage != newShapeImage) {
1339 if (oldShapeImage && HasImageRequest()) {
1340 loader->DisassociateRequestFromFrame(oldShapeImage, this);
1342 if (newShapeImage) {
1343 loader->AssociateRequestToFrame(
1344 newShapeImage, this,
1345 ImageLoader::Flags::
1346 RequiresReflowOnFirstFrameCompleteAndLoadEventBlocking);
1350 // SVGObserverUtils::GetEffectProperties() asserts that we only invoke it with
1351 // the first continuation so we need to check that in advance.
1352 const bool isNonTextFirstContinuation = isNonText && !GetPrevContinuation();
1353 if (isNonTextFirstContinuation) {
1354 // Kick off loading of external SVG resources referenced from properties if
1355 // any. This currently includes filter, clip-path, and mask.
1356 SVGObserverUtils::InitiateResourceDocLoads(this);
1359 // If the page contains markup that overrides text direction, and
1360 // does not contain any characters that would activate the Unicode
1361 // bidi algorithm, we need to call |SetBidiEnabled| on the pres
1362 // context before reflow starts. See bug 115921.
1363 if (StyleVisibility()->mDirection == StyleDirection::Rtl) {
1364 PresContext()->SetBidiEnabled();
1367 // The following part is for caching offset-path:path(). We cache the
1368 // flatten gfx path, so we don't have to rebuild and re-flattern it at
1369 // each cycle if we have animations on offset-* with a fixed offset-path.
1370 const StyleOffsetPath* oldPath =
1371 aOldComputedStyle ? &aOldComputedStyle->StyleDisplay()->mOffsetPath
1372 : nullptr;
1373 const StyleOffsetPath& newPath = StyleDisplay()->mOffsetPath;
1374 if (!oldPath || *oldPath != newPath) {
1375 // FIXME: Bug 1837042. Cache all basic shapes.
1376 if (newPath.IsPath()) {
1377 RefPtr<gfx::PathBuilder> builder = MotionPathUtils::GetPathBuilder();
1378 RefPtr<gfx::Path> path =
1379 MotionPathUtils::BuildSVGPath(newPath.AsSVGPathData(), builder);
1380 if (path) {
1381 // The newPath could be path('') (i.e. empty path), so its gfx path
1382 // could be nullptr, and so we only set property for a non-empty path.
1383 SetProperty(nsIFrame::OffsetPathCache(), path.forget().take());
1384 } else {
1385 // May have an old cached path, so we have to delete it.
1386 RemoveProperty(nsIFrame::OffsetPathCache());
1388 } else if (oldPath) {
1389 RemoveProperty(nsIFrame::OffsetPathCache());
1393 if (IsPrimaryFrame()) {
1394 HandleLastRememberedSize();
1397 RemoveStateBits(NS_FRAME_SIMPLE_EVENT_REGIONS | NS_FRAME_SIMPLE_DISPLAYLIST);
1399 mMayHaveRoundedCorners = true;
1402 void nsIFrame::HandleLastRememberedSize() {
1403 MOZ_ASSERT(IsPrimaryFrame());
1404 // Storing a last remembered size requires contain-intrinsic-size, and using
1405 // a previously stored last remembered size requires content-visibility.
1406 if (!StaticPrefs::layout_css_contain_intrinsic_size_enabled() ||
1407 !StaticPrefs::layout_css_content_visibility_enabled()) {
1408 return;
1410 auto* element = Element::FromNodeOrNull(mContent);
1411 if (!element) {
1412 return;
1414 const WritingMode wm = GetWritingMode();
1415 const nsStylePosition* stylePos = StylePosition();
1416 bool canRememberBSize = stylePos->ContainIntrinsicBSize(wm).HasAuto();
1417 bool canRememberISize = stylePos->ContainIntrinsicISize(wm).HasAuto();
1418 if (!canRememberBSize) {
1419 element->RemoveLastRememberedBSize();
1421 if (!canRememberISize) {
1422 element->RemoveLastRememberedISize();
1424 if ((canRememberBSize || canRememberISize) && !HidesContent()) {
1425 bool isNonReplacedInline = IsFrameOfType(nsIFrame::eLineParticipant) &&
1426 !IsFrameOfType(nsIFrame::eReplaced);
1427 if (!isNonReplacedInline) {
1428 PresContext()->Document()->ObserveForLastRememberedSize(*element);
1429 return;
1432 PresContext()->Document()->UnobserveForLastRememberedSize(*element);
1435 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
1436 void nsIFrame::AssertNewStyleIsSane(ComputedStyle& aNewStyle) {
1437 MOZ_DIAGNOSTIC_ASSERT(
1438 aNewStyle.GetPseudoType() == mComputedStyle->GetPseudoType() ||
1439 // ::first-line continuations are weird, this should probably be fixed via
1440 // bug 1465474.
1441 (mComputedStyle->GetPseudoType() == PseudoStyleType::firstLine &&
1442 aNewStyle.GetPseudoType() == PseudoStyleType::mozLineFrame) ||
1443 // ::first-letter continuations are broken, in particular floating ones,
1444 // see bug 1490281. The construction code tries to fix this up after the
1445 // fact, then restyling undoes it...
1446 (mComputedStyle->GetPseudoType() == PseudoStyleType::mozText &&
1447 aNewStyle.GetPseudoType() == PseudoStyleType::firstLetterContinuation) ||
1448 (mComputedStyle->GetPseudoType() ==
1449 PseudoStyleType::firstLetterContinuation &&
1450 aNewStyle.GetPseudoType() == PseudoStyleType::mozText));
1452 #endif
1454 void nsIFrame::ReparentFrameViewTo(nsViewManager* aViewManager,
1455 nsView* aNewParentView) {
1456 if (HasView()) {
1457 if (IsMenuPopupFrame()) {
1458 // This view must be parented by the root view, don't reparent it.
1459 return;
1461 nsView* view = GetView();
1462 aViewManager->RemoveChild(view);
1464 // The view will remember the Z-order and other attributes that have been
1465 // set on it.
1466 nsView* insertBefore =
1467 nsLayoutUtils::FindSiblingViewFor(aNewParentView, this);
1468 aViewManager->InsertChild(aNewParentView, view, insertBefore,
1469 insertBefore != nullptr);
1470 } else if (HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
1471 for (const auto& childList : ChildLists()) {
1472 // Iterate the child frames, and check each child frame to see if it has
1473 // a view
1474 for (nsIFrame* child : childList.mList) {
1475 child->ReparentFrameViewTo(aViewManager, aNewParentView);
1481 void nsIFrame::SyncFrameViewProperties(nsView* aView) {
1482 if (!aView) {
1483 aView = GetView();
1484 if (!aView) {
1485 return;
1489 nsViewManager* vm = aView->GetViewManager();
1491 // Make sure visibility is correct. This only affects nsSubDocumentFrame.
1492 if (!SupportsVisibilityHidden()) {
1493 // See if the view should be hidden or visible
1494 ComputedStyle* sc = Style();
1495 vm->SetViewVisibility(aView, sc->StyleVisibility()->IsVisible()
1496 ? ViewVisibility::Show
1497 : ViewVisibility::Hide);
1500 const auto zIndex = ZIndex();
1501 const bool autoZIndex = !zIndex;
1502 vm->SetViewZIndex(aView, autoZIndex, zIndex.valueOr(0));
1505 void nsIFrame::CreateView() {
1506 MOZ_ASSERT(!HasView());
1508 nsView* parentView = GetParent()->GetClosestView();
1509 MOZ_ASSERT(parentView, "no parent with view");
1511 nsViewManager* viewManager = parentView->GetViewManager();
1512 MOZ_ASSERT(viewManager, "null view manager");
1514 nsView* view = viewManager->CreateView(GetRect(), parentView);
1515 SyncFrameViewProperties(view);
1517 nsView* insertBefore = nsLayoutUtils::FindSiblingViewFor(parentView, this);
1518 // we insert this view 'above' the insertBefore view, unless insertBefore is
1519 // null, in which case we want to call with aAbove == false to insert at the
1520 // beginning in document order
1521 viewManager->InsertChild(parentView, view, insertBefore,
1522 insertBefore != nullptr);
1524 // REVIEW: Don't create a widget for fixed-pos elements anymore.
1525 // ComputeRepaintRegionForCopy will calculate the right area to repaint
1526 // when we scroll.
1527 // Reparent views on any child frames (or their descendants) to this
1528 // view. We can just call ReparentFrameViewTo on this frame because
1529 // we know this frame has no view, so it will crawl the children. Also,
1530 // we know that any descendants with views must have 'parentView' as their
1531 // parent view.
1532 ReparentFrameViewTo(viewManager, view);
1534 // Remember our view
1535 SetView(view);
1537 NS_FRAME_LOG(NS_FRAME_TRACE_CALLS,
1538 ("nsIFrame::CreateView: frame=%p view=%p", this, view));
1541 /* virtual */
1542 nsMargin nsIFrame::GetUsedMargin() const {
1543 nsMargin margin(0, 0, 0, 0);
1544 if (((mState & NS_FRAME_FIRST_REFLOW) && !(mState & NS_FRAME_IN_REFLOW)) ||
1545 IsInSVGTextSubtree())
1546 return margin;
1548 nsMargin* m = GetProperty(UsedMarginProperty());
1549 if (m) {
1550 margin = *m;
1551 } else {
1552 if (!StyleMargin()->GetMargin(margin)) {
1553 // If we get here, our caller probably shouldn't be calling us...
1554 NS_ERROR(
1555 "Returning bogus 0-sized margin, because this margin "
1556 "depends on layout & isn't cached!");
1559 return margin;
1562 /* virtual */
1563 nsMargin nsIFrame::GetUsedBorder() const {
1564 nsMargin border(0, 0, 0, 0);
1565 if (((mState & NS_FRAME_FIRST_REFLOW) && !(mState & NS_FRAME_IN_REFLOW)) ||
1566 IsInSVGTextSubtree())
1567 return border;
1569 // Theme methods don't use const-ness.
1570 nsIFrame* mutable_this = const_cast<nsIFrame*>(this);
1572 const nsStyleDisplay* disp = StyleDisplay();
1573 if (mutable_this->IsThemed(disp)) {
1574 nsPresContext* pc = PresContext();
1575 LayoutDeviceIntMargin widgetBorder = pc->Theme()->GetWidgetBorder(
1576 pc->DeviceContext(), mutable_this, disp->EffectiveAppearance());
1577 border =
1578 LayoutDevicePixel::ToAppUnits(widgetBorder, pc->AppUnitsPerDevPixel());
1579 return border;
1582 nsMargin* b = GetProperty(UsedBorderProperty());
1583 if (b) {
1584 border = *b;
1585 } else {
1586 border = StyleBorder()->GetComputedBorder();
1588 return border;
1591 /* virtual */
1592 nsMargin nsIFrame::GetUsedPadding() const {
1593 nsMargin padding(0, 0, 0, 0);
1594 if (((mState & NS_FRAME_FIRST_REFLOW) && !(mState & NS_FRAME_IN_REFLOW)) ||
1595 IsInSVGTextSubtree())
1596 return padding;
1598 // Theme methods don't use const-ness.
1599 nsIFrame* mutable_this = const_cast<nsIFrame*>(this);
1601 const nsStyleDisplay* disp = StyleDisplay();
1602 if (mutable_this->IsThemed(disp)) {
1603 nsPresContext* pc = PresContext();
1604 LayoutDeviceIntMargin widgetPadding;
1605 if (pc->Theme()->GetWidgetPadding(pc->DeviceContext(), mutable_this,
1606 disp->EffectiveAppearance(),
1607 &widgetPadding)) {
1608 return LayoutDevicePixel::ToAppUnits(widgetPadding,
1609 pc->AppUnitsPerDevPixel());
1613 nsMargin* p = GetProperty(UsedPaddingProperty());
1614 if (p) {
1615 padding = *p;
1616 } else {
1617 if (!StylePadding()->GetPadding(padding)) {
1618 // If we get here, our caller probably shouldn't be calling us...
1619 NS_ERROR(
1620 "Returning bogus 0-sized padding, because this padding "
1621 "depends on layout & isn't cached!");
1624 return padding;
1627 nsIFrame::Sides nsIFrame::GetSkipSides() const {
1628 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
1629 StyleBoxDecorationBreak::Clone) &&
1630 !HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1631 return Sides();
1634 // Convert the logical skip sides to physical sides using the frame's
1635 // writing mode
1636 WritingMode writingMode = GetWritingMode();
1637 LogicalSides logicalSkip = GetLogicalSkipSides();
1638 Sides skip;
1640 if (logicalSkip.BStart()) {
1641 if (writingMode.IsVertical()) {
1642 skip |= writingMode.IsVerticalLR() ? SideBits::eLeft : SideBits::eRight;
1643 } else {
1644 skip |= SideBits::eTop;
1648 if (logicalSkip.BEnd()) {
1649 if (writingMode.IsVertical()) {
1650 skip |= writingMode.IsVerticalLR() ? SideBits::eRight : SideBits::eLeft;
1651 } else {
1652 skip |= SideBits::eBottom;
1656 if (logicalSkip.IStart()) {
1657 if (writingMode.IsVertical()) {
1658 skip |= SideBits::eTop;
1659 } else {
1660 skip |= writingMode.IsBidiLTR() ? SideBits::eLeft : SideBits::eRight;
1664 if (logicalSkip.IEnd()) {
1665 if (writingMode.IsVertical()) {
1666 skip |= SideBits::eBottom;
1667 } else {
1668 skip |= writingMode.IsBidiLTR() ? SideBits::eRight : SideBits::eLeft;
1671 return skip;
1674 nsRect nsIFrame::GetPaddingRectRelativeToSelf() const {
1675 nsMargin border = GetUsedBorder().ApplySkipSides(GetSkipSides());
1676 nsRect r(0, 0, mRect.width, mRect.height);
1677 r.Deflate(border);
1678 return r;
1681 nsRect nsIFrame::GetPaddingRect() const {
1682 return GetPaddingRectRelativeToSelf() + GetPosition();
1685 WritingMode nsIFrame::WritingModeForLine(WritingMode aSelfWM,
1686 nsIFrame* aSubFrame) const {
1687 MOZ_ASSERT(aSelfWM == GetWritingMode());
1688 WritingMode writingMode = aSelfWM;
1690 if (StyleTextReset()->mUnicodeBidi == StyleUnicodeBidi::Plaintext) {
1691 mozilla::intl::BidiEmbeddingLevel frameLevel =
1692 nsBidiPresUtils::GetFrameBaseLevel(aSubFrame);
1693 writingMode.SetDirectionFromBidiLevel(frameLevel);
1696 return writingMode;
1699 nsRect nsIFrame::GetMarginRect() const {
1700 return GetMarginRectRelativeToSelf() + GetPosition();
1703 nsRect nsIFrame::GetMarginRectRelativeToSelf() const {
1704 nsMargin m = GetUsedMargin().ApplySkipSides(GetSkipSides());
1705 nsRect r(0, 0, mRect.width, mRect.height);
1706 r.Inflate(m);
1707 return r;
1710 bool nsIFrame::IsTransformed() const {
1711 if (!HasAnyStateBits(NS_FRAME_MAY_BE_TRANSFORMED)) {
1712 MOZ_ASSERT(!IsCSSTransformed());
1713 MOZ_ASSERT(!IsSVGTransformed());
1714 return false;
1716 return IsCSSTransformed() || IsSVGTransformed();
1719 bool nsIFrame::IsCSSTransformed() const {
1720 return HasAnyStateBits(NS_FRAME_MAY_BE_TRANSFORMED) &&
1721 (StyleDisplay()->HasTransform(this) || HasAnimationOfTransform());
1724 bool nsIFrame::HasAnimationOfTransform() const {
1725 return IsPrimaryFrame() &&
1726 nsLayoutUtils::HasAnimationOfTransformAndMotionPath(this) &&
1727 IsFrameOfType(eSupportsCSSTransforms);
1730 bool nsIFrame::ChildrenHavePerspective(
1731 const nsStyleDisplay* aStyleDisplay) const {
1732 MOZ_ASSERT(aStyleDisplay == StyleDisplay());
1733 return aStyleDisplay->HasPerspective(this);
1736 bool nsIFrame::HasAnimationOfOpacity(EffectSet* aEffectSet) const {
1737 return ((nsLayoutUtils::IsPrimaryStyleFrame(this) ||
1738 nsLayoutUtils::FirstContinuationOrIBSplitSibling(this)
1739 ->IsPrimaryFrame()) &&
1740 nsLayoutUtils::HasAnimationOfPropertySet(
1741 this, nsCSSPropertyIDSet::OpacityProperties(), aEffectSet));
1744 bool nsIFrame::HasOpacityInternal(float aThreshold,
1745 const nsStyleDisplay* aStyleDisplay,
1746 const nsStyleEffects* aStyleEffects,
1747 EffectSet* aEffectSet) const {
1748 MOZ_ASSERT(0.0 <= aThreshold && aThreshold <= 1.0, "Invalid argument");
1749 if (aStyleEffects->mOpacity < aThreshold ||
1750 aStyleDisplay->mWillChange.bits & StyleWillChangeBits::OPACITY) {
1751 return true;
1754 if (!mMayHaveOpacityAnimation) {
1755 return false;
1758 return HasAnimationOfOpacity(aEffectSet);
1761 bool nsIFrame::IsSVGTransformed(gfx::Matrix* aOwnTransforms,
1762 gfx::Matrix* aFromParentTransforms) const {
1763 return false;
1766 bool nsIFrame::Extend3DContext(const nsStyleDisplay* aStyleDisplay,
1767 const nsStyleEffects* aStyleEffects,
1768 mozilla::EffectSet* aEffectSetForOpacity) const {
1769 if (!HasAnyStateBits(NS_FRAME_MAY_BE_TRANSFORMED)) {
1770 return false;
1772 const nsStyleDisplay* disp = StyleDisplayWithOptionalParam(aStyleDisplay);
1773 if (disp->mTransformStyle != StyleTransformStyle::Preserve3d ||
1774 !IsFrameOfType(nsIFrame::eSupportsCSSTransforms)) {
1775 return false;
1778 // If we're all scroll frame, then all descendants will be clipped, so we
1779 // can't preserve 3d.
1780 if (IsScrollFrame()) {
1781 return false;
1784 const nsStyleEffects* effects = StyleEffectsWithOptionalParam(aStyleEffects);
1785 if (HasOpacity(disp, effects, aEffectSetForOpacity)) {
1786 return false;
1789 return ShouldApplyOverflowClipping(disp) == PhysicalAxes::None &&
1790 !GetClipPropClipRect(disp, effects, GetSize()) &&
1791 !SVGIntegrationUtils::UsingEffectsForFrame(this) &&
1792 !effects->HasMixBlendMode() &&
1793 disp->mIsolation != StyleIsolation::Isolate;
1796 bool nsIFrame::Combines3DTransformWithAncestors() const {
1797 // Check these first as they are faster then both calls below and are we are
1798 // likely to hit the early return (backface hidden is uncommon and
1799 // GetReferenceFrame is a hot caller of this which only calls this if
1800 // IsCSSTransformed is false).
1801 if (!IsCSSTransformed() && !BackfaceIsHidden()) {
1802 return false;
1804 nsIFrame* parent = GetClosestFlattenedTreeAncestorPrimaryFrame();
1805 return parent && parent->Extend3DContext();
1808 bool nsIFrame::In3DContextAndBackfaceIsHidden() const {
1809 // While both tests fail most of the time, test BackfaceIsHidden()
1810 // first since it's likely to fail faster.
1811 return BackfaceIsHidden() && Combines3DTransformWithAncestors();
1814 bool nsIFrame::HasPerspective() const {
1815 if (!IsCSSTransformed()) {
1816 return false;
1818 nsIFrame* parent = GetClosestFlattenedTreeAncestorPrimaryFrame();
1819 if (!parent) {
1820 return false;
1822 return parent->ChildrenHavePerspective();
1825 nsRect nsIFrame::GetContentRectRelativeToSelf() const {
1826 nsMargin bp = GetUsedBorderAndPadding().ApplySkipSides(GetSkipSides());
1827 nsRect r(0, 0, mRect.width, mRect.height);
1828 r.Deflate(bp);
1829 return r;
1832 nsRect nsIFrame::GetContentRect() const {
1833 return GetContentRectRelativeToSelf() + GetPosition();
1836 bool nsIFrame::ComputeBorderRadii(const BorderRadius& aBorderRadius,
1837 const nsSize& aFrameSize,
1838 const nsSize& aBorderArea, Sides aSkipSides,
1839 nscoord aRadii[8]) {
1840 // Percentages are relative to whichever side they're on.
1841 for (const auto i : mozilla::AllPhysicalHalfCorners()) {
1842 const LengthPercentage& c = aBorderRadius.Get(i);
1843 nscoord axis = HalfCornerIsX(i) ? aFrameSize.width : aFrameSize.height;
1844 aRadii[i] = std::max(0, c.Resolve(axis));
1847 if (aSkipSides.Top()) {
1848 aRadii[eCornerTopLeftX] = 0;
1849 aRadii[eCornerTopLeftY] = 0;
1850 aRadii[eCornerTopRightX] = 0;
1851 aRadii[eCornerTopRightY] = 0;
1854 if (aSkipSides.Right()) {
1855 aRadii[eCornerTopRightX] = 0;
1856 aRadii[eCornerTopRightY] = 0;
1857 aRadii[eCornerBottomRightX] = 0;
1858 aRadii[eCornerBottomRightY] = 0;
1861 if (aSkipSides.Bottom()) {
1862 aRadii[eCornerBottomRightX] = 0;
1863 aRadii[eCornerBottomRightY] = 0;
1864 aRadii[eCornerBottomLeftX] = 0;
1865 aRadii[eCornerBottomLeftY] = 0;
1868 if (aSkipSides.Left()) {
1869 aRadii[eCornerBottomLeftX] = 0;
1870 aRadii[eCornerBottomLeftY] = 0;
1871 aRadii[eCornerTopLeftX] = 0;
1872 aRadii[eCornerTopLeftY] = 0;
1875 // css3-background specifies this algorithm for reducing
1876 // corner radii when they are too big.
1877 bool haveRadius = false;
1878 double ratio = 1.0f;
1879 for (const auto side : mozilla::AllPhysicalSides()) {
1880 uint32_t hc1 = SideToHalfCorner(side, false, true);
1881 uint32_t hc2 = SideToHalfCorner(side, true, true);
1882 nscoord length =
1883 SideIsVertical(side) ? aBorderArea.height : aBorderArea.width;
1884 nscoord sum = aRadii[hc1] + aRadii[hc2];
1885 if (sum) {
1886 haveRadius = true;
1887 // avoid floating point division in the normal case
1888 if (length < sum) {
1889 ratio = std::min(ratio, double(length) / sum);
1893 if (ratio < 1.0) {
1894 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
1895 aRadii[corner] *= ratio;
1899 return haveRadius;
1902 void nsIFrame::AdjustBorderRadii(nscoord aRadii[8], const nsMargin& aOffsets) {
1903 auto AdjustOffset = [](const uint32_t aRadius, const nscoord aOffset) {
1904 // Implement the cubic formula to adjust offset when aOffset > 0 and
1905 // aRadius / aOffset < 1.
1906 // https://drafts.csswg.org/css-shapes/#valdef-shape-box-margin-box
1907 if (aOffset > 0) {
1908 const double ratio = aRadius / double(aOffset);
1909 if (ratio < 1.0) {
1910 return nscoord(aOffset * (1.0 + std::pow(ratio - 1, 3)));
1913 return aOffset;
1916 for (const auto side : mozilla::AllPhysicalSides()) {
1917 const nscoord offset = aOffsets.Side(side);
1918 const uint32_t hc1 = SideToHalfCorner(side, false, false);
1919 const uint32_t hc2 = SideToHalfCorner(side, true, false);
1920 if (aRadii[hc1] > 0) {
1921 const nscoord offset1 = AdjustOffset(aRadii[hc1], offset);
1922 aRadii[hc1] = std::max(0, aRadii[hc1] + offset1);
1924 if (aRadii[hc2] > 0) {
1925 const nscoord offset2 = AdjustOffset(aRadii[hc2], offset);
1926 aRadii[hc2] = std::max(0, aRadii[hc2] + offset2);
1931 static inline bool RadiiAreDefinitelyZero(const BorderRadius& aBorderRadius) {
1932 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
1933 if (!aBorderRadius.Get(corner).IsDefinitelyZero()) {
1934 return false;
1937 return true;
1940 /* virtual */
1941 bool nsIFrame::GetBorderRadii(const nsSize& aFrameSize,
1942 const nsSize& aBorderArea, Sides aSkipSides,
1943 nscoord aRadii[8]) const {
1944 if (!mMayHaveRoundedCorners) {
1945 memset(aRadii, 0, sizeof(nscoord) * 8);
1946 return false;
1949 if (IsThemed()) {
1950 // When we're themed, the native theme code draws the border and
1951 // background, and therefore it doesn't make sense to tell other
1952 // code that's interested in border-radius that we have any radii.
1954 // In an ideal world, we might have a way for the them to tell us an
1955 // border radius, but since we don't, we're better off assuming
1956 // zero.
1957 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
1958 aRadii[corner] = 0;
1960 return false;
1963 const auto& radii = StyleBorder()->mBorderRadius;
1964 const bool hasRadii =
1965 ComputeBorderRadii(radii, aFrameSize, aBorderArea, aSkipSides, aRadii);
1966 if (!hasRadii) {
1967 // TODO(emilio): Maybe we can just remove this bit and do the
1968 // IsDefinitelyZero check unconditionally. That should still avoid most of
1969 // the work, though maybe not the cache miss of going through the style and
1970 // the border struct.
1971 const_cast<nsIFrame*>(this)->mMayHaveRoundedCorners =
1972 !RadiiAreDefinitelyZero(radii);
1974 return hasRadii;
1977 bool nsIFrame::GetBorderRadii(nscoord aRadii[8]) const {
1978 nsSize sz = GetSize();
1979 return GetBorderRadii(sz, sz, GetSkipSides(), aRadii);
1982 bool nsIFrame::GetMarginBoxBorderRadii(nscoord aRadii[8]) const {
1983 return GetBoxBorderRadii(aRadii, GetUsedMargin());
1986 bool nsIFrame::GetPaddingBoxBorderRadii(nscoord aRadii[8]) const {
1987 return GetBoxBorderRadii(aRadii, -GetUsedBorder());
1990 bool nsIFrame::GetContentBoxBorderRadii(nscoord aRadii[8]) const {
1991 return GetBoxBorderRadii(aRadii, -GetUsedBorderAndPadding());
1994 bool nsIFrame::GetBoxBorderRadii(nscoord aRadii[8],
1995 const nsMargin& aOffsets) const {
1996 if (!GetBorderRadii(aRadii)) {
1997 return false;
1999 AdjustBorderRadii(aRadii, aOffsets);
2000 for (const auto corner : mozilla::AllPhysicalHalfCorners()) {
2001 if (aRadii[corner]) {
2002 return true;
2005 return false;
2008 bool nsIFrame::GetShapeBoxBorderRadii(nscoord aRadii[8]) const {
2009 using Tag = StyleShapeOutside::Tag;
2010 auto& shapeOutside = StyleDisplay()->mShapeOutside;
2011 auto box = StyleShapeBox::MarginBox;
2012 switch (shapeOutside.tag) {
2013 case Tag::Image:
2014 case Tag::None:
2015 return false;
2016 case Tag::Box:
2017 box = shapeOutside.AsBox();
2018 break;
2019 case Tag::Shape:
2020 box = shapeOutside.AsShape()._1;
2021 break;
2024 switch (box) {
2025 case StyleShapeBox::ContentBox:
2026 return GetContentBoxBorderRadii(aRadii);
2027 case StyleShapeBox::PaddingBox:
2028 return GetPaddingBoxBorderRadii(aRadii);
2029 case StyleShapeBox::BorderBox:
2030 return GetBorderRadii(aRadii);
2031 case StyleShapeBox::MarginBox:
2032 return GetMarginBoxBorderRadii(aRadii);
2033 default:
2034 MOZ_ASSERT_UNREACHABLE("Unexpected box value");
2035 return false;
2039 ComputedStyle* nsIFrame::GetAdditionalComputedStyle(int32_t aIndex) const {
2040 MOZ_ASSERT(aIndex >= 0, "invalid index number");
2041 return nullptr;
2044 void nsIFrame::SetAdditionalComputedStyle(int32_t aIndex,
2045 ComputedStyle* aComputedStyle) {
2046 MOZ_ASSERT(aIndex >= 0, "invalid index number");
2049 nscoord nsIFrame::SynthesizeFallbackBaseline(
2050 WritingMode aWM, BaselineSharingGroup aBaselineGroup) const {
2051 const auto margin = GetLogicalUsedMargin(aWM);
2052 NS_ASSERTION(!IsSubtreeDirty(), "frame must not be dirty");
2053 if (aWM.IsCentralBaseline()) {
2054 return (BSize(aWM) + GetLogicalUsedMargin(aWM).BEnd(aWM)) / 2;
2056 // Baseline for inverted line content is the top (block-start) margin edge,
2057 // as the frame is in effect "flipped" for alignment purposes.
2058 if (aWM.IsLineInverted()) {
2059 const auto marginStart = margin.BStart(aWM);
2060 return aBaselineGroup == BaselineSharingGroup::First
2061 ? -marginStart
2062 : BSize(aWM) + marginStart;
2064 // Otherwise, the bottom margin edge, per CSS2.1's definition of the
2065 // 'baseline' value of 'vertical-align'.
2066 const auto marginEnd = margin.BEnd(aWM);
2067 return aBaselineGroup == BaselineSharingGroup::First ? BSize(aWM) + marginEnd
2068 : -marginEnd;
2071 nscoord nsIFrame::GetLogicalBaseline(WritingMode aWM) const {
2072 return GetLogicalBaseline(aWM, GetDefaultBaselineSharingGroup(),
2073 BaselineExportContext::LineLayout);
2076 nscoord nsIFrame::GetLogicalBaseline(
2077 WritingMode aWM, BaselineSharingGroup aBaselineGroup,
2078 BaselineExportContext aExportContext) const {
2079 const auto result =
2080 GetNaturalBaselineBOffset(aWM, aBaselineGroup, aExportContext)
2081 .valueOrFrom([this, aWM, aBaselineGroup]() {
2082 return SynthesizeFallbackBaseline(aWM, aBaselineGroup);
2084 if (aBaselineGroup == BaselineSharingGroup::Last) {
2085 return BSize(aWM) - result;
2087 return result;
2090 const nsFrameList& nsIFrame::GetChildList(ChildListID aListID) const {
2091 if (IsAbsoluteContainer() && aListID == GetAbsoluteListID()) {
2092 return GetAbsoluteContainingBlock()->GetChildList();
2093 } else {
2094 return nsFrameList::EmptyList();
2098 void nsIFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
2099 if (IsAbsoluteContainer()) {
2100 const nsFrameList& absoluteList =
2101 GetAbsoluteContainingBlock()->GetChildList();
2102 absoluteList.AppendIfNonempty(aLists, GetAbsoluteListID());
2106 AutoTArray<nsIFrame::ChildList, 4> nsIFrame::CrossDocChildLists() {
2107 AutoTArray<ChildList, 4> childLists;
2108 nsSubDocumentFrame* subdocumentFrame = do_QueryFrame(this);
2109 if (subdocumentFrame) {
2110 // Descend into the subdocument
2111 nsIFrame* root = subdocumentFrame->GetSubdocumentRootFrame();
2112 if (root) {
2113 childLists.EmplaceBack(
2114 nsFrameList(root, nsLayoutUtils::GetLastSibling(root)),
2115 FrameChildListID::Principal);
2119 GetChildLists(&childLists);
2120 return childLists;
2123 nsIFrame::CaretBlockAxisMetrics nsIFrame::GetCaretBlockAxisMetrics(
2124 mozilla::WritingMode aWM, const nsFontMetrics& aFM) const {
2125 // Note(dshin): Ultimately, this does something highly similar (But still
2126 // different) to `nsLayoutUtils::GetFirstLinePosition`.
2127 const auto baseline = GetCaretBaseline();
2128 nscoord ascent = 0, descent = 0;
2129 ascent = aFM.MaxAscent();
2130 descent = aFM.MaxDescent();
2131 const nscoord height = ascent + descent;
2132 if (aWM.IsVertical() && aWM.IsLineInverted()) {
2133 return CaretBlockAxisMetrics{.mOffset = baseline - descent,
2134 .mExtent = height};
2136 return CaretBlockAxisMetrics{.mOffset = baseline - ascent, .mExtent = height};
2139 const nsAtom* nsIFrame::ComputePageValue() const {
2140 const nsAtom* value = nsGkAtoms::_empty;
2141 const nsIFrame* frame = this;
2142 // Find what CSS page name value this frame's subtree has, if any.
2143 // Starting with this frame, check if a page name other than auto is present,
2144 // and record it if so. Then, if the current frame is a container frame, find
2145 // the first non-placeholder child and repeat.
2146 // This will find the most deeply nested first in-flow child of this frame's
2147 // subtree, and return its page name (with auto resolved if applicable, and
2148 // subtrees with no page-names returning the empty atom rather than null).
2149 do {
2150 if (const nsAtom* maybePageName = frame->GetStylePageName()) {
2151 value = maybePageName;
2153 // Get the next frame to read from.
2154 const nsIFrame* firstNonPlaceholderFrame = nullptr;
2155 // If this is a container frame, inspect its in-flow children.
2156 if (const nsContainerFrame* containerFrame = do_QueryFrame(frame)) {
2157 for (const nsIFrame* childFrame : containerFrame->PrincipalChildList()) {
2158 if (!childFrame->IsPlaceholderFrame()) {
2159 firstNonPlaceholderFrame = childFrame;
2160 break;
2164 frame = firstNonPlaceholderFrame;
2165 } while (frame);
2166 return value;
2169 Visibility nsIFrame::GetVisibility() const {
2170 if (!HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED)) {
2171 return Visibility::Untracked;
2174 bool isSet = false;
2175 uint32_t visibleCount = GetProperty(VisibilityStateProperty(), &isSet);
2177 MOZ_ASSERT(isSet,
2178 "Should have a VisibilityStateProperty value "
2179 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2181 return visibleCount > 0 ? Visibility::ApproximatelyVisible
2182 : Visibility::ApproximatelyNonVisible;
2185 void nsIFrame::UpdateVisibilitySynchronously() {
2186 mozilla::PresShell* presShell = PresShell();
2187 if (!presShell) {
2188 return;
2191 if (presShell->AssumeAllFramesVisible()) {
2192 presShell->EnsureFrameInApproximatelyVisibleList(this);
2193 return;
2196 bool visible = StyleVisibility()->IsVisible();
2197 nsIFrame* f = GetParent();
2198 nsRect rect = GetRectRelativeToSelf();
2199 nsIFrame* rectFrame = this;
2200 while (f && visible) {
2201 nsIScrollableFrame* sf = do_QueryFrame(f);
2202 if (sf) {
2203 nsRect transformedRect =
2204 nsLayoutUtils::TransformFrameRectToAncestor(rectFrame, rect, f);
2205 if (!sf->IsRectNearlyVisible(transformedRect)) {
2206 visible = false;
2207 break;
2210 // In this code we're trying to synchronously update *approximate*
2211 // visibility. (In the future we may update precise visibility here as
2212 // well, which is why the method name does not contain 'approximate'.) The
2213 // IsRectNearlyVisible() check above tells us that the rect we're checking
2214 // is approximately visible within the scrollframe, but we still need to
2215 // ensure that, even if it was scrolled into view, it'd be visible when we
2216 // consider the rest of the document. To do that, we move transformedRect
2217 // to be contained in the scrollport as best we can (it might not fit) to
2218 // pretend that it was scrolled into view.
2219 rect = transformedRect.MoveInsideAndClamp(sf->GetScrollPortRect());
2220 rectFrame = f;
2222 nsIFrame* parent = f->GetParent();
2223 if (!parent) {
2224 parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(f);
2225 if (parent && parent->PresContext()->IsChrome()) {
2226 break;
2229 f = parent;
2232 if (visible) {
2233 presShell->EnsureFrameInApproximatelyVisibleList(this);
2234 } else {
2235 presShell->RemoveFrameFromApproximatelyVisibleList(this);
2239 void nsIFrame::EnableVisibilityTracking() {
2240 if (HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED)) {
2241 return; // Nothing to do.
2244 MOZ_ASSERT(!HasProperty(VisibilityStateProperty()),
2245 "Shouldn't have a VisibilityStateProperty value "
2246 "if NS_FRAME_VISIBILITY_IS_TRACKED is not set");
2248 // Add the state bit so we know to track visibility for this frame, and
2249 // initialize the frame property.
2250 AddStateBits(NS_FRAME_VISIBILITY_IS_TRACKED);
2251 SetProperty(VisibilityStateProperty(), 0);
2253 mozilla::PresShell* presShell = PresShell();
2254 if (!presShell) {
2255 return;
2258 // Schedule a visibility update. This method will virtually always be called
2259 // when layout has changed anyway, so it's very unlikely that any additional
2260 // visibility updates will be triggered by this, but this way we guarantee
2261 // that if this frame is currently visible we'll eventually find out.
2262 presShell->ScheduleApproximateFrameVisibilityUpdateSoon();
2265 void nsIFrame::DisableVisibilityTracking() {
2266 if (!HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED)) {
2267 return; // Nothing to do.
2270 bool isSet = false;
2271 uint32_t visibleCount = TakeProperty(VisibilityStateProperty(), &isSet);
2273 MOZ_ASSERT(isSet,
2274 "Should have a VisibilityStateProperty value "
2275 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2277 RemoveStateBits(NS_FRAME_VISIBILITY_IS_TRACKED);
2279 if (visibleCount == 0) {
2280 return; // We were nonvisible.
2283 // We were visible, so send an OnVisibilityChange() notification.
2284 OnVisibilityChange(Visibility::ApproximatelyNonVisible);
2287 void nsIFrame::DecApproximateVisibleCount(
2288 const Maybe<OnNonvisible>& aNonvisibleAction
2289 /* = Nothing() */) {
2290 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED));
2292 bool isSet = false;
2293 uint32_t visibleCount = GetProperty(VisibilityStateProperty(), &isSet);
2295 MOZ_ASSERT(isSet,
2296 "Should have a VisibilityStateProperty value "
2297 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2298 MOZ_ASSERT(visibleCount > 0,
2299 "Frame is already nonvisible and we're "
2300 "decrementing its visible count?");
2302 visibleCount--;
2303 SetProperty(VisibilityStateProperty(), visibleCount);
2304 if (visibleCount > 0) {
2305 return;
2308 // We just became nonvisible, so send an OnVisibilityChange() notification.
2309 OnVisibilityChange(Visibility::ApproximatelyNonVisible, aNonvisibleAction);
2312 void nsIFrame::IncApproximateVisibleCount() {
2313 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_VISIBILITY_IS_TRACKED));
2315 bool isSet = false;
2316 uint32_t visibleCount = GetProperty(VisibilityStateProperty(), &isSet);
2318 MOZ_ASSERT(isSet,
2319 "Should have a VisibilityStateProperty value "
2320 "if NS_FRAME_VISIBILITY_IS_TRACKED is set");
2322 visibleCount++;
2323 SetProperty(VisibilityStateProperty(), visibleCount);
2324 if (visibleCount > 1) {
2325 return;
2328 // We just became visible, so send an OnVisibilityChange() notification.
2329 OnVisibilityChange(Visibility::ApproximatelyVisible);
2332 void nsIFrame::OnVisibilityChange(Visibility aNewVisibility,
2333 const Maybe<OnNonvisible>& aNonvisibleAction
2334 /* = Nothing() */) {
2335 // XXX(seth): In bug 1218990 we'll implement visibility tracking for CSS
2336 // images here.
2339 static nsIFrame* GetActiveSelectionFrame(nsPresContext* aPresContext,
2340 nsIFrame* aFrame) {
2341 nsIContent* capturingContent = PresShell::GetCapturingContent();
2342 if (capturingContent) {
2343 nsIFrame* activeFrame = aPresContext->GetPrimaryFrameFor(capturingContent);
2344 return activeFrame ? activeFrame : aFrame;
2347 return aFrame;
2350 int16_t nsIFrame::DetermineDisplaySelection() {
2351 int16_t selType = nsISelectionController::SELECTION_OFF;
2353 nsCOMPtr<nsISelectionController> selCon;
2354 nsresult result =
2355 GetSelectionController(PresContext(), getter_AddRefs(selCon));
2356 if (NS_SUCCEEDED(result) && selCon) {
2357 result = selCon->GetDisplaySelection(&selType);
2358 if (NS_SUCCEEDED(result) &&
2359 (selType != nsISelectionController::SELECTION_OFF)) {
2360 // Check whether style allows selection.
2361 if (!IsSelectable(nullptr)) {
2362 selType = nsISelectionController::SELECTION_OFF;
2366 return selType;
2369 static Element* FindElementAncestorForMozSelection(nsIContent* aContent) {
2370 NS_ENSURE_TRUE(aContent, nullptr);
2371 while (aContent && aContent->IsInNativeAnonymousSubtree()) {
2372 aContent = aContent->GetClosestNativeAnonymousSubtreeRootParentOrHost();
2374 NS_ASSERTION(aContent, "aContent isn't in non-anonymous tree?");
2375 return aContent ? aContent->GetAsElementOrParentElement() : nullptr;
2378 already_AddRefed<ComputedStyle> nsIFrame::ComputeSelectionStyle(
2379 int16_t aSelectionStatus) const {
2380 // Just bail out if not a selection-status that ::selection applies to.
2381 if (aSelectionStatus != nsISelectionController::SELECTION_ON &&
2382 aSelectionStatus != nsISelectionController::SELECTION_DISABLED) {
2383 return nullptr;
2385 Element* element = FindElementAncestorForMozSelection(GetContent());
2386 if (!element) {
2387 return nullptr;
2389 RefPtr<ComputedStyle> pseudoStyle =
2390 PresContext()->StyleSet()->ProbePseudoElementStyle(
2391 *element, PseudoStyleType::selection, nullptr, Style());
2392 if (!pseudoStyle) {
2393 return nullptr;
2395 // When in high-contrast mode, the style system ends up ignoring the color
2396 // declarations, which means that the ::selection style becomes the inherited
2397 // color, and default background. That's no good.
2398 // When force-color-adjust is set to none allow using the color styles,
2399 // as they will not be replaced.
2400 if (PresContext()->ForcingColors() &&
2401 pseudoStyle->StyleText()->mForcedColorAdjust !=
2402 StyleForcedColorAdjust::None) {
2403 return nullptr;
2405 return do_AddRef(pseudoStyle);
2408 already_AddRefed<ComputedStyle> nsIFrame::ComputeHighlightSelectionStyle(
2409 nsAtom* aHighlightName) {
2410 Element* element = FindElementAncestorForMozSelection(GetContent());
2411 if (!element) {
2412 return nullptr;
2414 return PresContext()->StyleSet()->ProbePseudoElementStyle(
2415 *element, PseudoStyleType::highlight, aHighlightName, Style());
2418 template <typename SizeOrMaxSize>
2419 static inline bool IsIntrinsicKeyword(const SizeOrMaxSize& aSize) {
2420 // All keywords other than auto/none/-moz-available depend on intrinsic sizes.
2421 return aSize.IsMaxContent() || aSize.IsMinContent() || aSize.IsFitContent() ||
2422 aSize.IsFitContentFunction();
2425 bool nsIFrame::CanBeDynamicReflowRoot() const {
2426 const auto& display = *StyleDisplay();
2427 if (IsFrameOfType(nsIFrame::eLineParticipant) || display.mDisplay.IsRuby() ||
2428 display.IsInnerTableStyle() ||
2429 display.DisplayInside() == StyleDisplayInside::Table) {
2430 // We have a display type where 'width' and 'height' don't actually set the
2431 // width or height (i.e., the size depends on content).
2432 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_DYNAMIC_REFLOW_ROOT),
2433 "should not have dynamic reflow root bit");
2434 return false;
2437 // In general, frames that have contain:layout+size can be reflow roots.
2438 // (One exception: table-wrapper frames don't work well as reflow roots,
2439 // because their inner-table ReflowInput init path tries to reuse & deref
2440 // the wrapper's containing block's reflow input, which may be null if we
2441 // initiate reflow from the table-wrapper itself.)
2443 // Changes to `contain` force frame reconstructions, so we used to use
2444 // NS_FRAME_REFLOW_ROOT, this bit could be set for the whole lifetime of
2445 // this frame. But after the support of `content-visibility: auto` which
2446 // is with contain layout + size when it's not relevant to user, and only
2447 // with contain layout when it is relevant. The frame does not reconstruct
2448 // when the relevancy changes. So we use NS_FRAME_DYNAMIC_REFLOW_ROOT instead.
2450 // We place it above the pref check on purpose, to make sure it works for
2451 // containment even with the pref disabled.
2452 if (display.IsContainLayout() && GetContainSizeAxes().IsBoth()) {
2453 return true;
2456 if (!StaticPrefs::layout_dynamic_reflow_roots_enabled()) {
2457 return false;
2460 // We can't serve as a dynamic reflow root if our used 'width' and 'height'
2461 // might be influenced by content.
2463 // FIXME: For display:block, we should probably optimize inline-size: auto.
2464 // FIXME: Other flex and grid cases?
2465 const auto& pos = *StylePosition();
2466 const auto& width = pos.mWidth;
2467 const auto& height = pos.mHeight;
2468 if (!width.IsLengthPercentage() || width.HasPercent() ||
2469 !height.IsLengthPercentage() || height.HasPercent() ||
2470 IsIntrinsicKeyword(pos.mMinWidth) || IsIntrinsicKeyword(pos.mMaxWidth) ||
2471 IsIntrinsicKeyword(pos.mMinHeight) ||
2472 IsIntrinsicKeyword(pos.mMaxHeight) ||
2473 ((pos.mMinWidth.IsAuto() || pos.mMinHeight.IsAuto()) &&
2474 IsFlexOrGridItem())) {
2475 return false;
2478 // If our flex-basis is 'auto', it'll defer to 'width' (or 'height') which
2479 // we've already checked. Otherwise, it preempts them, so we need to
2480 // perform the same "could-this-value-be-influenced-by-content" checks that
2481 // we performed for 'width' and 'height' above.
2482 if (IsFlexItem()) {
2483 const auto& flexBasis = pos.mFlexBasis;
2484 if (!flexBasis.IsAuto()) {
2485 if (!flexBasis.IsSize() || !flexBasis.AsSize().IsLengthPercentage() ||
2486 flexBasis.AsSize().HasPercent()) {
2487 return false;
2492 if (!IsFixedPosContainingBlock()) {
2493 // We can't treat this frame as a reflow root, since dynamic changes
2494 // to absolutely-positioned frames inside of it require that we
2495 // reflow the placeholder before we reflow the absolutely positioned
2496 // frame.
2497 // FIXME: Alternatively, we could sort the reflow roots in
2498 // PresShell::ProcessReflowCommands by depth in the tree, from
2499 // deepest to least deep. However, for performance (FIXME) we
2500 // should really be sorting them in the opposite order!
2501 return false;
2504 // If we participate in a container's block reflow context, or margins
2505 // can collapse through us, we can't be a dynamic reflow root.
2506 if (IsBlockFrameOrSubclass() &&
2507 !HasAllStateBits(NS_BLOCK_FLOAT_MGR | NS_BLOCK_MARGIN_ROOT)) {
2508 return false;
2511 // Subgrids are never reflow roots, but 'contain:layout/paint' prevents
2512 // creating a subgrid in the first place.
2513 if (pos.mGridTemplateColumns.IsSubgrid() ||
2514 pos.mGridTemplateRows.IsSubgrid()) {
2515 // NOTE: we could check that 'display' of our parent's primary frame is
2516 // '[inline-]grid' here but that's probably not worth it in practice.
2517 if (!display.IsContainLayout() && !display.IsContainPaint()) {
2518 return false;
2522 // If we are split, we can't be a dynamic reflow root. Our reflow status may
2523 // change after reflow, and our parent is responsible to create or delete our
2524 // next-in-flow.
2525 if (GetPrevContinuation() || GetNextContinuation()) {
2526 return false;
2529 return true;
2532 /********************************************************
2533 * Refreshes each content's frame
2534 *********************************************************/
2536 void nsIFrame::DisplayOutlineUnconditional(nsDisplayListBuilder* aBuilder,
2537 const nsDisplayListSet& aLists) {
2538 // Per https://drafts.csswg.org/css-tables-3/#global-style-overrides:
2539 // "All css properties of table-column and table-column-group boxes are
2540 // ignored, except when explicitly specified by this specification."
2541 // CSS outlines fall into this category, so we skip them on these boxes.
2542 MOZ_ASSERT(!IsTableColGroupFrame() && !IsTableColFrame());
2543 const auto& outline = *StyleOutline();
2545 if (!outline.ShouldPaintOutline()) {
2546 return;
2549 // Outlines are painted by the table wrapper frame.
2550 if (IsTableFrame()) {
2551 return;
2554 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT) &&
2555 ScrollableOverflowRect().IsEmpty()) {
2556 // Skip parts of IB-splits with an empty overflow rect, see bug 434301.
2557 // We may still want to fix some of the overflow area calculations over in
2558 // that bug.
2559 return;
2562 // We don't display outline-style: auto on themed frames that have their own
2563 // focus indicators.
2564 if (outline.mOutlineStyle.IsAuto()) {
2565 auto* disp = StyleDisplay();
2566 if (IsThemed(disp) && PresContext()->Theme()->ThemeDrawsFocusForWidget(
2567 this, disp->EffectiveAppearance())) {
2568 return;
2572 aLists.Outlines()->AppendNewToTop<nsDisplayOutline>(aBuilder, this);
2575 void nsIFrame::DisplayOutline(nsDisplayListBuilder* aBuilder,
2576 const nsDisplayListSet& aLists) {
2577 if (!IsVisibleForPainting()) return;
2579 DisplayOutlineUnconditional(aBuilder, aLists);
2582 void nsIFrame::DisplayInsetBoxShadowUnconditional(
2583 nsDisplayListBuilder* aBuilder, nsDisplayList* aList) {
2584 // XXXbz should box-shadow for rows/rowgroups/columns/colgroups get painted
2585 // just because we're visible? Or should it depend on the cell visibility
2586 // when we're not the whole table?
2587 const auto* effects = StyleEffects();
2588 if (effects->HasBoxShadowWithInset(true)) {
2589 aList->AppendNewToTop<nsDisplayBoxShadowInner>(aBuilder, this);
2593 void nsIFrame::DisplayInsetBoxShadow(nsDisplayListBuilder* aBuilder,
2594 nsDisplayList* aList) {
2595 if (!IsVisibleForPainting()) return;
2597 DisplayInsetBoxShadowUnconditional(aBuilder, aList);
2600 void nsIFrame::DisplayOutsetBoxShadowUnconditional(
2601 nsDisplayListBuilder* aBuilder, nsDisplayList* aList) {
2602 // XXXbz should box-shadow for rows/rowgroups/columns/colgroups get painted
2603 // just because we're visible? Or should it depend on the cell visibility
2604 // when we're not the whole table?
2605 const auto* effects = StyleEffects();
2606 if (effects->HasBoxShadowWithInset(false)) {
2607 aList->AppendNewToTop<nsDisplayBoxShadowOuter>(aBuilder, this);
2611 void nsIFrame::DisplayOutsetBoxShadow(nsDisplayListBuilder* aBuilder,
2612 nsDisplayList* aList) {
2613 if (!IsVisibleForPainting()) return;
2615 DisplayOutsetBoxShadowUnconditional(aBuilder, aList);
2618 void nsIFrame::DisplayCaret(nsDisplayListBuilder* aBuilder,
2619 nsDisplayList* aList) {
2620 if (!IsVisibleForPainting()) return;
2622 aList->AppendNewToTop<nsDisplayCaret>(aBuilder, this);
2625 nscolor nsIFrame::GetCaretColorAt(int32_t aOffset) {
2626 return nsLayoutUtils::GetColor(this, &nsStyleUI::mCaretColor);
2629 auto nsIFrame::ComputeShouldPaintBackground() const -> ShouldPaintBackground {
2630 nsPresContext* pc = PresContext();
2631 ShouldPaintBackground settings{pc->GetBackgroundColorDraw(),
2632 pc->GetBackgroundImageDraw()};
2633 if (settings.mColor && settings.mImage) {
2634 return settings;
2637 if (StyleVisibility()->mPrintColorAdjust == StylePrintColorAdjust::Exact) {
2638 return {true, true};
2641 return settings;
2644 bool nsIFrame::DisplayBackgroundUnconditional(nsDisplayListBuilder* aBuilder,
2645 const nsDisplayListSet& aLists) {
2646 if (aBuilder->IsForEventDelivery() && !aBuilder->HitTestIsForVisibility()) {
2647 // For hit-testing, we generally just need a light-weight data structure
2648 // like nsDisplayEventReceiver. But if the hit-testing is for visibility,
2649 // then we need to know the opaque region in order to determine whether to
2650 // stop or not.
2651 aLists.BorderBackground()->AppendNewToTop<nsDisplayEventReceiver>(aBuilder,
2652 this);
2653 return false;
2656 const AppendedBackgroundType result =
2657 nsDisplayBackgroundImage::AppendBackgroundItemsToTop(
2658 aBuilder, this,
2659 GetRectRelativeToSelf() + aBuilder->ToReferenceFrame(this),
2660 aLists.BorderBackground());
2662 if (result == AppendedBackgroundType::None) {
2663 aBuilder->BuildCompositorHitTestInfoIfNeeded(this,
2664 aLists.BorderBackground());
2667 return result == AppendedBackgroundType::ThemedBackground;
2670 void nsIFrame::DisplayBorderBackgroundOutline(nsDisplayListBuilder* aBuilder,
2671 const nsDisplayListSet& aLists) {
2672 // The visibility check belongs here since child elements have the
2673 // opportunity to override the visibility property and display even if
2674 // their parent is hidden.
2675 if (!IsVisibleForPainting()) {
2676 return;
2679 DisplayOutsetBoxShadowUnconditional(aBuilder, aLists.BorderBackground());
2681 bool bgIsThemed = DisplayBackgroundUnconditional(aBuilder, aLists);
2682 DisplayInsetBoxShadowUnconditional(aBuilder, aLists.BorderBackground());
2684 // If there's a themed background, we should not create a border item.
2685 // It won't be rendered.
2686 // Don't paint borders for tables here, since they paint them in a different
2687 // order.
2688 if (!bgIsThemed && StyleBorder()->HasBorder() && !IsTableFrame()) {
2689 aLists.BorderBackground()->AppendNewToTop<nsDisplayBorder>(aBuilder, this);
2692 DisplayOutlineUnconditional(aBuilder, aLists);
2695 inline static bool IsSVGContentWithCSSClip(const nsIFrame* aFrame) {
2696 // The CSS spec says that the 'clip' property only applies to absolutely
2697 // positioned elements, whereas the SVG spec says that it applies to SVG
2698 // elements regardless of the value of the 'position' property. Here we obey
2699 // the CSS spec for outer-<svg> (since that's what we generally do), but
2700 // obey the SVG spec for other SVG elements to which 'clip' applies.
2701 return aFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT) &&
2702 aFrame->GetContent()->IsAnyOfSVGElements(nsGkAtoms::svg,
2703 nsGkAtoms::foreignObject);
2706 Maybe<nsRect> nsIFrame::GetClipPropClipRect(const nsStyleDisplay* aDisp,
2707 const nsStyleEffects* aEffects,
2708 const nsSize& aSize) const {
2709 if (aEffects->mClip.IsAuto() ||
2710 !(aDisp->IsAbsolutelyPositioned(this) || IsSVGContentWithCSSClip(this))) {
2711 return Nothing();
2714 auto& clipRect = aEffects->mClip.AsRect();
2715 nsRect rect = clipRect.ToLayoutRect();
2716 if (MOZ_LIKELY(StyleBorder()->mBoxDecorationBreak ==
2717 StyleBoxDecorationBreak::Slice)) {
2718 // The clip applies to the joined boxes so it's relative the first
2719 // continuation.
2720 nscoord y = 0;
2721 for (nsIFrame* f = GetPrevContinuation(); f; f = f->GetPrevContinuation()) {
2722 y += f->GetRect().height;
2724 rect.MoveBy(nsPoint(0, -y));
2727 if (clipRect.right.IsAuto()) {
2728 rect.width = aSize.width - rect.x;
2730 if (clipRect.bottom.IsAuto()) {
2731 rect.height = aSize.height - rect.y;
2733 return Some(rect);
2737 * If the CSS 'overflow' property applies to this frame, and is not
2738 * handled by constructing a dedicated nsHTML/XULScrollFrame, set up clipping
2739 * for that overflow in aBuilder->ClipState() to clip all containing-block
2740 * descendants.
2742 static void ApplyOverflowClipping(
2743 nsDisplayListBuilder* aBuilder, const nsIFrame* aFrame,
2744 nsIFrame::PhysicalAxes aClipAxes,
2745 DisplayListClipState::AutoClipMultiple& aClipState) {
2746 // Only 'clip' is handled here (and 'hidden' for table frames, and any
2747 // non-'visible' value for blocks in a paginated context).
2748 // We allow 'clip' to apply to any kind of frame. This is required by
2749 // comboboxes which make their display text (an inline frame) have clipping.
2750 MOZ_ASSERT(aClipAxes != nsIFrame::PhysicalAxes::None);
2751 MOZ_ASSERT(aFrame->ShouldApplyOverflowClipping(aFrame->StyleDisplay()) ==
2752 aClipAxes);
2754 nsRect clipRect;
2755 bool haveRadii = false;
2756 nscoord radii[8];
2757 auto* disp = aFrame->StyleDisplay();
2758 // Only deflate the padding if we clip to the content-box in that axis.
2759 auto wm = aFrame->GetWritingMode();
2760 bool cbH = (wm.IsVertical() ? disp->mOverflowClipBoxBlock
2761 : disp->mOverflowClipBoxInline) ==
2762 StyleOverflowClipBox::ContentBox;
2763 bool cbV = (wm.IsVertical() ? disp->mOverflowClipBoxInline
2764 : disp->mOverflowClipBoxBlock) ==
2765 StyleOverflowClipBox::ContentBox;
2767 nsMargin boxMargin = -aFrame->GetUsedPadding();
2768 if (!cbH) {
2769 boxMargin.left = boxMargin.right = nscoord(0);
2771 if (!cbV) {
2772 boxMargin.top = boxMargin.bottom = nscoord(0);
2775 auto clipMargin = aFrame->OverflowClipMargin(aClipAxes);
2777 boxMargin -= aFrame->GetUsedBorder();
2778 boxMargin += nsMargin(clipMargin.height, clipMargin.width, clipMargin.height,
2779 clipMargin.width);
2780 boxMargin.ApplySkipSides(aFrame->GetSkipSides());
2782 nsRect rect(nsPoint(0, 0), aFrame->GetSize());
2783 rect.Inflate(boxMargin);
2784 if (MOZ_UNLIKELY(!(aClipAxes & nsIFrame::PhysicalAxes::Horizontal))) {
2785 // NOTE(mats) We shouldn't be clipping at all in this dimension really,
2786 // but clipping in just one axis isn't supported by our GFX APIs so we
2787 // clip to our visual overflow rect instead.
2788 nsRect o = aFrame->InkOverflowRect();
2789 rect.x = o.x;
2790 rect.width = o.width;
2792 if (MOZ_UNLIKELY(!(aClipAxes & nsIFrame::PhysicalAxes::Vertical))) {
2793 // See the note above.
2794 nsRect o = aFrame->InkOverflowRect();
2795 rect.y = o.y;
2796 rect.height = o.height;
2798 clipRect = rect + aBuilder->ToReferenceFrame(aFrame);
2799 haveRadii = aFrame->GetBoxBorderRadii(radii, boxMargin);
2800 aClipState.ClipContainingBlockDescendantsExtra(clipRect,
2801 haveRadii ? radii : nullptr);
2804 nsSize nsIFrame::OverflowClipMargin(PhysicalAxes aClipAxes) const {
2805 nsSize result;
2806 if (aClipAxes == PhysicalAxes::None) {
2807 return result;
2809 const auto& margin = StyleMargin()->mOverflowClipMargin;
2810 if (margin.IsZero()) {
2811 return result;
2813 nscoord marginAu = margin.ToAppUnits();
2814 if (aClipAxes & PhysicalAxes::Horizontal) {
2815 result.width = marginAu;
2817 if (aClipAxes & PhysicalAxes::Vertical) {
2818 result.height = marginAu;
2820 return result;
2824 * Returns whether a display item that gets created with the builder's current
2825 * state will have a scrolled clip, i.e. a clip that is scrolled by a scroll
2826 * frame which does not move the item itself.
2828 static bool BuilderHasScrolledClip(nsDisplayListBuilder* aBuilder) {
2829 const DisplayItemClipChain* currentClip =
2830 aBuilder->ClipState().GetCurrentCombinedClipChain(aBuilder);
2831 if (!currentClip) {
2832 return false;
2835 const ActiveScrolledRoot* currentClipASR = currentClip->mASR;
2836 const ActiveScrolledRoot* currentASR = aBuilder->CurrentActiveScrolledRoot();
2837 return ActiveScrolledRoot::PickDescendant(currentClipASR, currentASR) !=
2838 currentASR;
2841 class AutoSaveRestoreContainsBlendMode {
2842 nsDisplayListBuilder& mBuilder;
2843 bool mSavedContainsBlendMode;
2845 public:
2846 explicit AutoSaveRestoreContainsBlendMode(nsDisplayListBuilder& aBuilder)
2847 : mBuilder(aBuilder),
2848 mSavedContainsBlendMode(aBuilder.ContainsBlendMode()) {}
2850 ~AutoSaveRestoreContainsBlendMode() {
2851 mBuilder.SetContainsBlendMode(mSavedContainsBlendMode);
2855 static bool IsFrameOrAncestorApzAware(nsIFrame* aFrame) {
2856 nsIContent* node = aFrame->GetContent();
2857 if (!node) {
2858 return false;
2861 do {
2862 if (node->IsNodeApzAware()) {
2863 return true;
2865 nsIContent* shadowRoot = node->GetShadowRoot();
2866 if (shadowRoot && shadowRoot->IsNodeApzAware()) {
2867 return true;
2870 // Even if the node owning aFrame doesn't have apz-aware event listeners
2871 // itself, its shadow root or display: contents ancestors (which have no
2872 // frames) might, so we need to account for them too.
2873 } while ((node = node->GetFlattenedTreeParent()) && node->IsElement() &&
2874 node->AsElement()->IsDisplayContents());
2876 return false;
2879 static void CheckForApzAwareEventHandlers(nsDisplayListBuilder* aBuilder,
2880 nsIFrame* aFrame) {
2881 if (aBuilder->GetAncestorHasApzAwareEventHandler()) {
2882 return;
2885 if (IsFrameOrAncestorApzAware(aFrame)) {
2886 aBuilder->SetAncestorHasApzAwareEventHandler(true);
2890 static void UpdateCurrentHitTestInfo(nsDisplayListBuilder* aBuilder,
2891 nsIFrame* aFrame) {
2892 if (!aBuilder->BuildCompositorHitTestInfo()) {
2893 // Compositor hit test info is not used.
2894 return;
2897 CheckForApzAwareEventHandlers(aBuilder, aFrame);
2899 const CompositorHitTestInfo info = aFrame->GetCompositorHitTestInfo(aBuilder);
2900 aBuilder->SetCompositorHitTestInfo(info);
2904 * True if aDescendant participates the context aAncestor participating.
2906 static bool FrameParticipatesIn3DContext(nsIFrame* aAncestor,
2907 nsIFrame* aDescendant) {
2908 MOZ_ASSERT(aAncestor != aDescendant);
2909 MOZ_ASSERT(aAncestor->GetContent() != aDescendant->GetContent());
2910 MOZ_ASSERT(aAncestor->Extend3DContext());
2912 nsIFrame* ancestor = aAncestor->FirstContinuation();
2913 MOZ_ASSERT(ancestor->IsPrimaryFrame());
2915 nsIFrame* frame;
2916 for (frame = aDescendant->GetClosestFlattenedTreeAncestorPrimaryFrame();
2917 frame && ancestor != frame;
2918 frame = frame->GetClosestFlattenedTreeAncestorPrimaryFrame()) {
2919 if (!frame->Extend3DContext()) {
2920 return false;
2924 MOZ_ASSERT(frame == ancestor);
2925 return true;
2928 static bool ItemParticipatesIn3DContext(nsIFrame* aAncestor,
2929 nsDisplayItem* aItem) {
2930 auto type = aItem->GetType();
2931 const bool isContainer = type == DisplayItemType::TYPE_WRAP_LIST ||
2932 type == DisplayItemType::TYPE_CONTAINER;
2934 if (isContainer && aItem->GetChildren()->Length() == 1) {
2935 // If the wraplist has only one child item, use the type of that item.
2936 type = aItem->GetChildren()->GetBottom()->GetType();
2939 if (type != DisplayItemType::TYPE_TRANSFORM &&
2940 type != DisplayItemType::TYPE_PERSPECTIVE) {
2941 return false;
2943 nsIFrame* transformFrame = aItem->Frame();
2944 if (aAncestor->GetContent() == transformFrame->GetContent()) {
2945 return true;
2947 return FrameParticipatesIn3DContext(aAncestor, transformFrame);
2950 static void WrapSeparatorTransform(nsDisplayListBuilder* aBuilder,
2951 nsIFrame* aFrame,
2952 nsDisplayList* aNonParticipants,
2953 nsDisplayList* aParticipants, int aIndex,
2954 nsDisplayItem** aSeparator) {
2955 if (aNonParticipants->IsEmpty()) {
2956 return;
2959 nsDisplayTransform* item = MakeDisplayItemWithIndex<nsDisplayTransform>(
2960 aBuilder, aFrame, aIndex, aNonParticipants, aBuilder->GetVisibleRect());
2962 if (*aSeparator == nullptr && item) {
2963 *aSeparator = item;
2966 aParticipants->AppendToTop(item);
2969 // Try to compute a clip rect to bound the contents of the mask item
2970 // that will be built for |aMaskedFrame|. If we're not able to compute
2971 // one, return an empty Maybe.
2972 // The returned clip rect, if there is one, is relative to |aMaskedFrame|.
2973 static Maybe<nsRect> ComputeClipForMaskItem(nsDisplayListBuilder* aBuilder,
2974 nsIFrame* aMaskedFrame) {
2975 const nsStyleSVGReset* svgReset = aMaskedFrame->StyleSVGReset();
2977 SVGUtils::MaskUsage maskUsage;
2978 SVGUtils::DetermineMaskUsage(aMaskedFrame, false, maskUsage);
2980 nsPoint offsetToUserSpace =
2981 nsLayoutUtils::ComputeOffsetToUserSpace(aBuilder, aMaskedFrame);
2982 int32_t devPixelRatio = aMaskedFrame->PresContext()->AppUnitsPerDevPixel();
2983 gfxPoint devPixelOffsetToUserSpace =
2984 nsLayoutUtils::PointToGfxPoint(offsetToUserSpace, devPixelRatio);
2985 CSSToLayoutDeviceScale cssToDevScale =
2986 aMaskedFrame->PresContext()->CSSToDevPixelScale();
2988 nsPoint toReferenceFrame;
2989 aBuilder->FindReferenceFrameFor(aMaskedFrame, &toReferenceFrame);
2991 Maybe<gfxRect> combinedClip;
2992 if (maskUsage.shouldApplyBasicShapeOrPath) {
2993 Maybe<Rect> result =
2994 CSSClipPathInstance::GetBoundingRectForBasicShapeOrPathClip(
2995 aMaskedFrame, svgReset->mClipPath);
2996 if (result) {
2997 combinedClip = Some(ThebesRect(*result));
2999 } else if (maskUsage.shouldApplyClipPath) {
3000 gfxRect result = SVGUtils::GetBBox(
3001 aMaskedFrame,
3002 SVGUtils::eBBoxIncludeClipped | SVGUtils::eBBoxIncludeFill |
3003 SVGUtils::eBBoxIncludeMarkers | SVGUtils::eBBoxIncludeStroke |
3004 SVGUtils::eDoNotClipToBBoxOfContentInsideClipPath);
3005 combinedClip = Some(
3006 ThebesRect((CSSRect::FromUnknownRect(ToRect(result)) * cssToDevScale)
3007 .ToUnknownRect()));
3008 } else {
3009 // The code for this case is adapted from ComputeMaskGeometry().
3011 nsRect borderArea(toReferenceFrame, aMaskedFrame->GetSize());
3012 borderArea -= offsetToUserSpace;
3014 // Use an infinite dirty rect to pass into nsCSSRendering::
3015 // GetImageLayerClip() because we don't have an actual dirty rect to
3016 // pass in. This is fine because the only time GetImageLayerClip() will
3017 // not intersect the incoming dirty rect with something is in the "NoClip"
3018 // case, and we handle that specially.
3019 nsRect dirtyRect(nscoord_MIN / 2, nscoord_MIN / 2, nscoord_MAX,
3020 nscoord_MAX);
3022 nsIFrame* firstFrame =
3023 nsLayoutUtils::FirstContinuationOrIBSplitSibling(aMaskedFrame);
3024 nsTArray<SVGMaskFrame*> maskFrames;
3025 // XXX check return value?
3026 SVGObserverUtils::GetAndObserveMasks(firstFrame, &maskFrames);
3028 for (uint32_t i = 0; i < maskFrames.Length(); ++i) {
3029 gfxRect clipArea;
3030 if (maskFrames[i]) {
3031 clipArea = maskFrames[i]->GetMaskArea(aMaskedFrame);
3032 clipArea = ThebesRect(
3033 (CSSRect::FromUnknownRect(ToRect(clipArea)) * cssToDevScale)
3034 .ToUnknownRect());
3035 } else {
3036 const auto& layer = svgReset->mMask.mLayers[i];
3037 if (layer.mClip == StyleGeometryBox::NoClip) {
3038 return Nothing();
3041 nsCSSRendering::ImageLayerClipState clipState;
3042 nsCSSRendering::GetImageLayerClip(
3043 layer, aMaskedFrame, *aMaskedFrame->StyleBorder(), borderArea,
3044 dirtyRect, false /* aWillPaintBorder */, devPixelRatio, &clipState);
3045 clipArea = clipState.mDirtyRectInDevPx;
3047 combinedClip = UnionMaybeRects(combinedClip, Some(clipArea));
3050 if (combinedClip) {
3051 if (combinedClip->IsEmpty()) {
3052 // *clipForMask might be empty if all mask references are not resolvable
3053 // or the size of them are empty. We still need to create a transparent
3054 // mask before bug 1276834 fixed, so don't clip ctx by an empty rectangle
3055 // for for now.
3056 return Nothing();
3059 // Convert to user space.
3060 *combinedClip += devPixelOffsetToUserSpace;
3062 // Round the clip out. In FrameLayerBuilder we round clips to nearest
3063 // pixels, and if we have a really thin clip here, that can cause the
3064 // clip to become empty if we didn't round out here.
3065 // The rounding happens in coordinates that are relative to the reference
3066 // frame, which matches what FrameLayerBuilder does.
3067 combinedClip->RoundOut();
3069 // Convert to app units.
3070 nsRect result =
3071 nsLayoutUtils::RoundGfxRectToAppRect(*combinedClip, devPixelRatio);
3073 // The resulting clip is relative to the reference frame, but the caller
3074 // expects it to be relative to the masked frame, so adjust it.
3075 result -= toReferenceFrame;
3076 return Some(result);
3078 return Nothing();
3081 struct AutoCheckBuilder {
3082 explicit AutoCheckBuilder(nsDisplayListBuilder* aBuilder)
3083 : mBuilder(aBuilder) {
3084 aBuilder->Check();
3087 ~AutoCheckBuilder() { mBuilder->Check(); }
3089 nsDisplayListBuilder* mBuilder;
3093 * Tries to reuse a top-level stacking context item from the previous paint.
3094 * Returns true if an item was reused, otherwise false.
3096 bool TryToReuseStackingContextItem(nsDisplayListBuilder* aBuilder,
3097 nsDisplayList* aList, nsIFrame* aFrame) {
3098 if (!aBuilder->IsForPainting() || !aBuilder->IsPartialUpdate() ||
3099 aBuilder->InInvalidSubtree()) {
3100 return false;
3103 if (aFrame->IsFrameModified() || aFrame->HasModifiedDescendants()) {
3104 return false;
3107 auto& items = aFrame->DisplayItems();
3108 auto* res = std::find_if(
3109 items.begin(), items.end(),
3110 [](nsDisplayItem* aItem) { return aItem->IsPreProcessed(); });
3112 if (res == items.end()) {
3113 return false;
3116 nsDisplayItem* container = *res;
3117 MOZ_ASSERT(container->Frame() == aFrame);
3118 DL_LOGD("RDL - Found SC item %p (%s) (frame: %p)", container,
3119 container->Name(), container->Frame());
3121 aList->AppendToTop(container);
3122 aBuilder->ReuseDisplayItem(container);
3123 return true;
3126 void nsIFrame::BuildDisplayListForStackingContext(
3127 nsDisplayListBuilder* aBuilder, nsDisplayList* aList,
3128 bool* aCreatedContainerItem) {
3129 #ifdef DEBUG
3130 DL_LOGV("BuildDisplayListForStackingContext (%p) <", this);
3131 ScopeExit e(
3132 [this]() { DL_LOGV("> BuildDisplayListForStackingContext (%p)", this); });
3133 #endif
3135 AutoCheckBuilder check(aBuilder);
3137 if (aBuilder->IsReusingStackingContextItems() &&
3138 TryToReuseStackingContextItem(aBuilder, aList, this)) {
3139 if (aCreatedContainerItem) {
3140 *aCreatedContainerItem = true;
3142 return;
3145 if (HasAnyStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE)) {
3146 return;
3149 const auto& style = *Style();
3150 const nsStyleDisplay* disp = style.StyleDisplay();
3151 const nsStyleEffects* effects = style.StyleEffects();
3152 EffectSet* effectSetForOpacity =
3153 EffectSet::GetForFrame(this, nsCSSPropertyIDSet::OpacityProperties());
3154 // We can stop right away if this is a zero-opacity stacking context and
3155 // we're painting, and we're not animating opacity.
3156 bool needHitTestInfo = aBuilder->BuildCompositorHitTestInfo() &&
3157 Style()->PointerEvents() != StylePointerEvents::None;
3158 bool opacityItemForEventsOnly = false;
3159 if (effects->IsTransparent() && aBuilder->IsForPainting() &&
3160 !(disp->mWillChange.bits & StyleWillChangeBits::OPACITY) &&
3161 !nsLayoutUtils::HasAnimationOfPropertySet(
3162 this, nsCSSPropertyIDSet::OpacityProperties(), effectSetForOpacity)) {
3163 if (needHitTestInfo) {
3164 opacityItemForEventsOnly = true;
3165 } else {
3166 return;
3170 if (aBuilder->IsForPainting() && disp->mWillChange.bits) {
3171 aBuilder->AddToWillChangeBudget(this, GetSize());
3174 // For preserves3d, use the dirty rect already installed on the
3175 // builder, since aDirtyRect maybe distorted for transforms along
3176 // the chain.
3177 nsRect visibleRect = aBuilder->GetVisibleRect();
3178 nsRect dirtyRect = aBuilder->GetDirtyRect();
3180 // We build an opacity item if it's not going to be drawn by SVG content.
3181 // We could in principle skip creating an nsDisplayOpacity item if
3182 // nsDisplayOpacity::NeedsActiveLayer returns false and usingSVGEffects is
3183 // true (the nsDisplayFilter/nsDisplayMasksAndClipPaths could handle the
3184 // opacity). Since SVG has perf issues where we sometimes spend a lot of
3185 // time creating display list items that might be helpful. We'd need to
3186 // restore our mechanism to do that (changed in bug 1482403), and we'd
3187 // need to invalidate the frame if the value that would be return from
3188 // NeedsActiveLayer was to change, which we don't currently do.
3189 const bool useOpacity =
3190 HasVisualOpacity(disp, effects, effectSetForOpacity) &&
3191 !SVGUtils::CanOptimizeOpacity(this);
3193 const bool isTransformed = IsTransformed();
3194 const bool hasPerspective = isTransformed && HasPerspective();
3195 const bool extend3DContext =
3196 Extend3DContext(disp, effects, effectSetForOpacity);
3197 const bool combines3DTransformWithAncestors =
3198 (extend3DContext || isTransformed) && Combines3DTransformWithAncestors();
3200 Maybe<nsDisplayListBuilder::AutoPreserves3DContext> autoPreserves3DContext;
3201 if (extend3DContext && !combines3DTransformWithAncestors) {
3202 // Start a new preserves3d context to keep informations on
3203 // nsDisplayListBuilder.
3204 autoPreserves3DContext.emplace(aBuilder);
3205 // Save dirty rect on the builder to avoid being distorted for
3206 // multiple transforms along the chain.
3207 aBuilder->SavePreserves3DRect();
3209 // We rebuild everything within preserve-3d and don't try
3210 // to retain, so override the dirty rect now.
3211 if (aBuilder->IsRetainingDisplayList()) {
3212 dirtyRect = visibleRect;
3213 aBuilder->SetDisablePartialUpdates(true);
3217 const bool useBlendMode = effects->mMixBlendMode != StyleBlend::Normal;
3218 if (useBlendMode) {
3219 aBuilder->SetContainsBlendMode(true);
3222 // reset blend mode so we can keep track if this stacking context needs have
3223 // a nsDisplayBlendContainer. Set the blend mode back when the routine exits
3224 // so we keep track if the parent stacking context needs a container too.
3225 AutoSaveRestoreContainsBlendMode autoRestoreBlendMode(*aBuilder);
3226 aBuilder->SetContainsBlendMode(false);
3228 // NOTE: When changing this condition make sure to tweak nsGfxScrollFrame as
3229 // well.
3230 bool usingBackdropFilter = effects->HasBackdropFilters() &&
3231 IsVisibleForPainting() &&
3232 !style.IsRootElementStyle();
3234 nsRect visibleRectOutsideTransform = visibleRect;
3235 nsDisplayTransform::PrerenderInfo prerenderInfo;
3236 bool inTransform = aBuilder->IsInTransform();
3237 if (isTransformed) {
3238 prerenderInfo = nsDisplayTransform::ShouldPrerenderTransformedContent(
3239 aBuilder, this, &visibleRect);
3241 switch (prerenderInfo.mDecision) {
3242 case nsDisplayTransform::PrerenderDecision::Full:
3243 case nsDisplayTransform::PrerenderDecision::Partial:
3244 dirtyRect = visibleRect;
3245 break;
3246 case nsDisplayTransform::PrerenderDecision::No: {
3247 // If we didn't prerender an animated frame in a preserve-3d context,
3248 // then we want disable async animations for the rest of the preserve-3d
3249 // (especially ancestors).
3250 if ((extend3DContext || combines3DTransformWithAncestors) &&
3251 prerenderInfo.mHasAnimations) {
3252 aBuilder->SavePreserves3DAllowAsyncAnimation(false);
3255 const nsRect overflow = InkOverflowRectRelativeToSelf();
3256 if (overflow.IsEmpty() && !extend3DContext) {
3257 return;
3260 // If we're in preserve-3d then grab the dirty rect that was given to
3261 // the root and transform using the combined transform.
3262 if (combines3DTransformWithAncestors) {
3263 visibleRect = dirtyRect = aBuilder->GetPreserves3DRect();
3266 float appPerDev = PresContext()->AppUnitsPerDevPixel();
3267 auto transform = nsDisplayTransform::GetResultingTransformMatrix(
3268 this, nsPoint(), appPerDev,
3269 nsDisplayTransform::kTransformRectFlags);
3270 nsRect untransformedDirtyRect;
3271 if (nsDisplayTransform::UntransformRect(dirtyRect, overflow, transform,
3272 appPerDev,
3273 &untransformedDirtyRect)) {
3274 dirtyRect = untransformedDirtyRect;
3275 nsDisplayTransform::UntransformRect(visibleRect, overflow, transform,
3276 appPerDev, &visibleRect);
3277 } else {
3278 // This should only happen if the transform is singular, in which case
3279 // nothing is visible anyway
3280 dirtyRect.SetEmpty();
3281 visibleRect.SetEmpty();
3285 inTransform = true;
3286 } else if (IsFixedPosContainingBlock()) {
3287 // Restict the building area to the overflow rect for these frames, since
3288 // RetainedDisplayListBuilder uses it to know if the size of the stacking
3289 // context changed.
3290 visibleRect.IntersectRect(visibleRect, InkOverflowRect());
3291 dirtyRect.IntersectRect(dirtyRect, InkOverflowRect());
3294 bool hasOverrideDirtyRect = false;
3295 // If we're doing a partial build, we're not invalid and we're capable
3296 // of having an override building rect (stacking context and fixed pos
3297 // containing block), then we should assume we have one.
3298 // Either we have an explicit one, or nothing in our subtree changed and
3299 // we have an implicit empty rect.
3301 // These conditions should match |CanStoreDisplayListBuildingRect()| in
3302 // RetainedDisplayListBuilder.cpp
3303 if (!aBuilder->IsReusingStackingContextItems() &&
3304 aBuilder->IsPartialUpdate() && !aBuilder->InInvalidSubtree() &&
3305 !IsFrameModified() && IsFixedPosContainingBlock() &&
3306 !GetPrevContinuation() && !GetNextContinuation()) {
3307 dirtyRect = nsRect();
3308 if (HasOverrideDirtyRegion()) {
3309 nsDisplayListBuilder::DisplayListBuildingData* data =
3310 GetProperty(nsDisplayListBuilder::DisplayListBuildingRect());
3311 if (data) {
3312 dirtyRect = data->mDirtyRect.Intersect(visibleRect);
3313 hasOverrideDirtyRect = true;
3318 bool usingFilter = effects->HasFilters() && !style.IsRootElementStyle();
3319 bool usingMask = SVGIntegrationUtils::UsingMaskOrClipPathForFrame(this);
3320 bool usingSVGEffects = usingFilter || usingMask;
3322 nsRect visibleRectOutsideSVGEffects = visibleRect;
3323 nsDisplayList hoistedScrollInfoItemsStorage(aBuilder);
3324 if (usingSVGEffects) {
3325 dirtyRect =
3326 SVGIntegrationUtils::GetRequiredSourceForInvalidArea(this, dirtyRect);
3327 visibleRect =
3328 SVGIntegrationUtils::GetRequiredSourceForInvalidArea(this, visibleRect);
3329 aBuilder->EnterSVGEffectsContents(this, &hoistedScrollInfoItemsStorage);
3332 bool useStickyPosition = disp->mPosition == StylePositionProperty::Sticky;
3334 bool useFixedPosition =
3335 disp->mPosition == StylePositionProperty::Fixed &&
3336 (DisplayPortUtils::IsFixedPosFrameInDisplayPort(this) ||
3337 BuilderHasScrolledClip(aBuilder));
3339 nsDisplayListBuilder::AutoBuildingDisplayList buildingDisplayList(
3340 aBuilder, this, visibleRect, dirtyRect, isTransformed);
3342 UpdateCurrentHitTestInfo(aBuilder, this);
3344 // Depending on the effects that are applied to this frame, we can create
3345 // multiple container display items and wrap them around our contents.
3346 // This enum lists all the potential container display items, in the order
3347 // outside to inside.
3348 enum class ContainerItemType : uint8_t {
3349 None = 0,
3350 OwnLayerIfNeeded,
3351 BlendMode,
3352 FixedPosition,
3353 OwnLayerForTransformWithRoundedClip,
3354 Perspective,
3355 Transform,
3356 SeparatorTransforms,
3357 Opacity,
3358 Filter,
3359 BlendContainer
3362 nsDisplayListBuilder::AutoContainerASRTracker contASRTracker(aBuilder);
3364 auto cssClip = GetClipPropClipRect(disp, effects, GetSize());
3365 auto ApplyClipProp = [&](DisplayListClipState::AutoSaveRestore& aClipState) {
3366 if (!cssClip) {
3367 return;
3369 nsPoint offset = aBuilder->GetCurrentFrameOffsetToReferenceFrame();
3370 aBuilder->IntersectDirtyRect(*cssClip);
3371 aBuilder->IntersectVisibleRect(*cssClip);
3372 aClipState.ClipContentDescendants(*cssClip + offset);
3375 // The CSS clip property is effectively inside the transform, but outside the
3376 // filters. So if we're not transformed we can apply it just here for
3377 // simplicity, instead of on each of the places that handle clipCapturedBy.
3378 DisplayListClipState::AutoSaveRestore untransformedCssClip(aBuilder);
3379 if (!isTransformed) {
3380 ApplyClipProp(untransformedCssClip);
3383 // If there is a current clip, then depending on the container items we
3384 // create, different things can happen to it. Some container items simply
3385 // propagate the clip to their children and aren't clipped themselves.
3386 // But other container items, especially those that establish a different
3387 // geometry for their contents (e.g. transforms), capture the clip on
3388 // themselves and unset the clip for their contents. If we create more than
3389 // one of those container items, the clip will be captured on the outermost
3390 // one and the inner container items will be unclipped.
3391 ContainerItemType clipCapturedBy = ContainerItemType::None;
3392 if (useFixedPosition) {
3393 clipCapturedBy = ContainerItemType::FixedPosition;
3394 } else if (isTransformed) {
3395 const DisplayItemClipChain* currentClip =
3396 aBuilder->ClipState().GetCurrentCombinedClipChain(aBuilder);
3397 if ((hasPerspective || extend3DContext) &&
3398 (currentClip && currentClip->HasRoundedCorners())) {
3399 // If we're creating an nsDisplayTransform item that is going to combine
3400 // its transform with its children (preserve-3d or perspective), then we
3401 // can't have an intermediate surface. Mask layers force an intermediate
3402 // surface, so if we're going to need both then create a separate
3403 // wrapping layer for the mask.
3404 clipCapturedBy = ContainerItemType::OwnLayerForTransformWithRoundedClip;
3405 } else if (hasPerspective) {
3406 clipCapturedBy = ContainerItemType::Perspective;
3407 } else {
3408 clipCapturedBy = ContainerItemType::Transform;
3410 } else if (usingFilter) {
3411 clipCapturedBy = ContainerItemType::Filter;
3414 DisplayListClipState::AutoSaveRestore clipState(aBuilder);
3415 if (clipCapturedBy != ContainerItemType::None) {
3416 clipState.Clear();
3419 DisplayListClipState::AutoSaveRestore transformedCssClip(aBuilder);
3420 if (isTransformed) {
3421 // FIXME(emilio, bug 1525159): In the case we have a both a transform _and_
3422 // filters, this clips the input to the filters as well, which is not
3423 // correct (clipping by the `clip` property is supposed to happen after
3424 // applying the filter effects, per [1].
3426 // This is not a regression though, since we used to do that anyway before
3427 // bug 1514384, and even without the transform we get it wrong.
3429 // [1]: https://drafts.fxtf.org/css-masking/#placement
3430 ApplyClipProp(transformedCssClip);
3433 nsDisplayListCollection set(aBuilder);
3434 Maybe<nsRect> clipForMask;
3436 DisplayListClipState::AutoSaveRestore nestedClipState(aBuilder);
3437 nsDisplayListBuilder::AutoInTransformSetter inTransformSetter(aBuilder,
3438 inTransform);
3439 nsDisplayListBuilder::AutoEnterFilter filterASRSetter(aBuilder,
3440 usingFilter);
3441 nsDisplayListBuilder::AutoInEventsOnly inEventsSetter(
3442 aBuilder, opacityItemForEventsOnly);
3444 // If we have a mask, compute a clip to bound the masked content.
3445 // This is necessary in case the content moves with an ancestor
3446 // ASR of the mask.
3447 // Don't do this if we also have a filter, because then the clip
3448 // would be applied before the filter, violating
3449 // https://www.w3.org/TR/filter-effects-1/#placement.
3450 // Filters are a containing block for fixed and absolute descendants,
3451 // so the masked content cannot move with an ancestor ASR.
3452 if (usingMask && !usingFilter) {
3453 clipForMask = ComputeClipForMaskItem(aBuilder, this);
3454 if (clipForMask) {
3455 aBuilder->IntersectDirtyRect(*clipForMask);
3456 aBuilder->IntersectVisibleRect(*clipForMask);
3457 nestedClipState.ClipContentDescendants(
3458 *clipForMask + aBuilder->GetCurrentFrameOffsetToReferenceFrame());
3462 // extend3DContext also guarantees that applyAbsPosClipping and
3463 // usingSVGEffects are false We only modify the preserve-3d rect if we are
3464 // the top of a preserve-3d heirarchy
3465 if (extend3DContext) {
3466 // Mark these first so MarkAbsoluteFramesForDisplayList knows if we are
3467 // going to be forced to descend into frames.
3468 aBuilder->MarkPreserve3DFramesForDisplayList(this);
3471 aBuilder->AdjustWindowDraggingRegion(this);
3473 MarkAbsoluteFramesForDisplayList(aBuilder);
3474 aBuilder->Check();
3475 BuildDisplayList(aBuilder, set);
3476 SetBuiltDisplayList(true);
3477 aBuilder->Check();
3478 aBuilder->DisplayCaret(this, set.Outlines());
3480 // Blend modes are a real pain for retained display lists. We build a blend
3481 // container item if the built list contains any blend mode items within
3482 // the current stacking context. This can change without an invalidation
3483 // to the stacking context frame, or the blend mode frame (e.g. by moving
3484 // an intermediate frame).
3485 // When we gain/remove a blend container item, we need to mark this frame
3486 // as invalid and have the full display list for merging to track
3487 // the change correctly.
3488 // It seems really hard to track this in advance, as the bookkeeping
3489 // required to note which stacking contexts have blend descendants
3490 // is complex and likely to be buggy.
3491 // Instead we're doing the sad thing, detecting it afterwards, and just
3492 // repeating display list building if it changed.
3493 // We have to repeat building for the entire display list (or at least
3494 // the outer stacking context), since we need to mark this frame as invalid
3495 // to remove any existing content that isn't wrapped in the blend container,
3496 // and then we need to build content infront/behind the blend container
3497 // to get correct positioning during merging.
3498 if (aBuilder->ContainsBlendMode() && aBuilder->IsRetainingDisplayList()) {
3499 if (aBuilder->IsPartialUpdate()) {
3500 aBuilder->SetPartialBuildFailed(true);
3501 } else {
3502 aBuilder->SetDisablePartialUpdates(true);
3507 if (aBuilder->IsBackgroundOnly()) {
3508 set.BlockBorderBackgrounds()->DeleteAll(aBuilder);
3509 set.Floats()->DeleteAll(aBuilder);
3510 set.Content()->DeleteAll(aBuilder);
3511 set.PositionedDescendants()->DeleteAll(aBuilder);
3512 set.Outlines()->DeleteAll(aBuilder);
3515 if (hasOverrideDirtyRect &&
3516 StaticPrefs::layout_display_list_show_rebuild_area()) {
3517 nsDisplaySolidColor* color = MakeDisplayItem<nsDisplaySolidColor>(
3518 aBuilder, this,
3519 dirtyRect + aBuilder->GetCurrentFrameOffsetToReferenceFrame(),
3520 NS_RGBA(255, 0, 0, 64), false);
3521 if (color) {
3522 color->SetOverrideZIndex(INT32_MAX);
3523 set.PositionedDescendants()->AppendToTop(color);
3527 nsIContent* content = GetContent();
3528 if (!content) {
3529 content = PresContext()->Document()->GetRootElement();
3532 nsDisplayList resultList(aBuilder);
3533 set.SerializeWithCorrectZOrder(&resultList, content);
3535 // Get the ASR to use for the container items that we create here.
3536 const ActiveScrolledRoot* containerItemASR = contASRTracker.GetContainerASR();
3538 bool createdContainer = false;
3540 // If adding both a nsDisplayBlendContainer and a nsDisplayBlendMode to the
3541 // same list, the nsDisplayBlendContainer should be added first. This only
3542 // happens when the element creating this stacking context has mix-blend-mode
3543 // and also contains a child which has mix-blend-mode.
3544 // The nsDisplayBlendContainer must be added to the list first, so it does not
3545 // isolate the containing element blending as well.
3546 if (aBuilder->ContainsBlendMode()) {
3547 resultList.AppendToTop(nsDisplayBlendContainer::CreateForMixBlendMode(
3548 aBuilder, this, &resultList, containerItemASR));
3549 createdContainer = true;
3552 if (usingBackdropFilter) {
3553 nsRect backdropRect =
3554 GetRectRelativeToSelf() + aBuilder->ToReferenceFrame(this);
3555 resultList.AppendNewToTop<nsDisplayBackdropFilters>(
3556 aBuilder, this, &resultList, backdropRect, this);
3557 createdContainer = true;
3560 // If there are any SVG effects, wrap the list up in an SVG effects item
3561 // (which also handles CSS group opacity). Note that we create an SVG effects
3562 // item even if resultList is empty, since a filter can produce graphical
3563 // output even if the element being filtered wouldn't otherwise do so.
3564 if (usingSVGEffects) {
3565 MOZ_ASSERT(usingFilter || usingMask,
3566 "Beside filter & mask/clip-path, what else effect do we have?");
3568 if (clipCapturedBy == ContainerItemType::Filter) {
3569 clipState.Restore();
3571 // Revert to the post-filter dirty rect.
3572 aBuilder->SetVisibleRect(visibleRectOutsideSVGEffects);
3574 // Skip all filter effects while generating glyph mask.
3575 if (usingFilter && !aBuilder->IsForGenerateGlyphMask()) {
3576 /* List now emptied, so add the new list to the top. */
3577 resultList.AppendNewToTop<nsDisplayFilters>(aBuilder, this, &resultList,
3578 this, usingBackdropFilter);
3579 createdContainer = true;
3582 if (usingMask) {
3583 // The mask should move with aBuilder->CurrentActiveScrolledRoot(), so
3584 // that's the ASR we prefer to use for the mask item. However, we can
3585 // only do this if the mask if clipped with respect to that ASR, because
3586 // an item always needs to have finite bounds with respect to its ASR.
3587 // If we weren't able to compute a clip for the mask, we fall back to
3588 // using containerItemASR, which is the lowest common ancestor clip of
3589 // the mask's contents. That's not entirely correct, but it satisfies
3590 // the base requirement of the ASR system (that items have finite bounds
3591 // wrt. their ASR).
3592 const ActiveScrolledRoot* maskASR =
3593 clipForMask.isSome() ? aBuilder->CurrentActiveScrolledRoot()
3594 : containerItemASR;
3595 /* List now emptied, so add the new list to the top. */
3596 resultList.AppendNewToTop<nsDisplayMasksAndClipPaths>(
3597 aBuilder, this, &resultList, maskASR, usingBackdropFilter);
3598 createdContainer = true;
3601 // TODO(miko): We could probably create a wraplist here and avoid creating
3602 // it later in |BuildDisplayListForChild()|.
3603 createdContainer = false;
3605 // Also add the hoisted scroll info items. We need those for APZ scrolling
3606 // because nsDisplayMasksAndClipPaths items can't build active layers.
3607 aBuilder->ExitSVGEffectsContents();
3608 resultList.AppendToTop(&hoistedScrollInfoItemsStorage);
3611 // If the list is non-empty and there is CSS group opacity without SVG
3612 // effects, wrap it up in an opacity item.
3613 if (useOpacity) {
3614 const bool needsActiveOpacityLayer =
3615 nsDisplayOpacity::NeedsActiveLayer(aBuilder, this);
3616 resultList.AppendNewToTop<nsDisplayOpacity>(
3617 aBuilder, this, &resultList, containerItemASR, opacityItemForEventsOnly,
3618 needsActiveOpacityLayer, usingBackdropFilter);
3619 createdContainer = true;
3622 // If we're going to apply a transformation and don't have preserve-3d set,
3623 // wrap everything in an nsDisplayTransform. If there's nothing in the list,
3624 // don't add anything.
3626 // For the preserve-3d case we want to individually wrap every child in the
3627 // list with a separate nsDisplayTransform instead. When the child is already
3628 // an nsDisplayTransform, we can skip this step, as the computed transform
3629 // will already include our own.
3631 // We also traverse into sublists created by nsDisplayWrapList, so that we
3632 // find all the correct children.
3633 if (isTransformed && extend3DContext) {
3634 // Install dummy nsDisplayTransform as a leaf containing
3635 // descendants not participating this 3D rendering context.
3636 nsDisplayList nonparticipants(aBuilder);
3637 nsDisplayList participants(aBuilder);
3638 int index = 1;
3640 nsDisplayItem* separator = nullptr;
3642 // TODO: This can be simplified: |participants| is just |resultList|.
3643 for (nsDisplayItem* item : resultList.TakeItems()) {
3644 if (ItemParticipatesIn3DContext(this, item) &&
3645 !item->GetClip().HasClip()) {
3646 // The frame of this item participates the same 3D context.
3647 WrapSeparatorTransform(aBuilder, this, &nonparticipants, &participants,
3648 index++, &separator);
3650 participants.AppendToTop(item);
3651 } else {
3652 // The frame of the item doesn't participate the current
3653 // context, or has no transform.
3655 // For items participating but not transformed, they are add
3656 // to nonparticipants to get a separator layer for handling
3657 // clips, if there is, on an intermediate surface.
3658 // \see ContainerLayer::DefaultComputeEffectiveTransforms().
3659 nonparticipants.AppendToTop(item);
3662 WrapSeparatorTransform(aBuilder, this, &nonparticipants, &participants,
3663 index++, &separator);
3665 if (separator) {
3666 createdContainer = true;
3669 resultList.AppendToTop(&participants);
3672 if (isTransformed) {
3673 transformedCssClip.Restore();
3674 if (clipCapturedBy == ContainerItemType::Transform) {
3675 // Restore clip state now so nsDisplayTransform is clipped properly.
3676 clipState.Restore();
3678 // Revert to the dirtyrect coming in from the parent, without our transform
3679 // taken into account.
3680 aBuilder->SetVisibleRect(visibleRectOutsideTransform);
3682 if (this != aBuilder->RootReferenceFrame()) {
3683 // Revert to the outer reference frame and offset because all display
3684 // items we create from now on are outside the transform.
3685 nsPoint toOuterReferenceFrame;
3686 const nsIFrame* outerReferenceFrame =
3687 aBuilder->FindReferenceFrameFor(GetParent(), &toOuterReferenceFrame);
3688 toOuterReferenceFrame += GetPosition();
3690 buildingDisplayList.SetReferenceFrameAndCurrentOffset(
3691 outerReferenceFrame, toOuterReferenceFrame);
3694 // We would like to block async animations for ancestors of ones not
3695 // prerendered in the preserve-3d tree. Now that we've finished processing
3696 // all descendants, update allowAsyncAnimation to take their prerender
3697 // state into account
3698 // FIXME: We don't block async animations for previous siblings because
3699 // their prerender decisions have been made. We may have to figure out a
3700 // better way to rollback their prerender decisions.
3701 // Alternatively we could not block animations for later siblings, and only
3702 // block them for ancestors of a blocked one.
3703 if ((extend3DContext || combines3DTransformWithAncestors) &&
3704 prerenderInfo.CanUseAsyncAnimations() &&
3705 !aBuilder->GetPreserves3DAllowAsyncAnimation()) {
3706 // aBuilder->GetPreserves3DAllowAsyncAnimation() means the inner or
3707 // previous silbing frames are allowed/disallowed for async animations.
3708 prerenderInfo.mDecision = nsDisplayTransform::PrerenderDecision::No;
3711 nsDisplayTransform* transformItem = MakeDisplayItem<nsDisplayTransform>(
3712 aBuilder, this, &resultList, visibleRect, prerenderInfo.mDecision);
3713 if (transformItem) {
3714 resultList.AppendToTop(transformItem);
3715 createdContainer = true;
3718 if (hasPerspective) {
3719 transformItem->MarkWithAssociatedPerspective();
3721 if (clipCapturedBy == ContainerItemType::Perspective) {
3722 clipState.Restore();
3724 resultList.AppendNewToTop<nsDisplayPerspective>(aBuilder, this,
3725 &resultList);
3726 createdContainer = true;
3730 if (clipCapturedBy ==
3731 ContainerItemType::OwnLayerForTransformWithRoundedClip) {
3732 clipState.Restore();
3733 resultList.AppendNewToTopWithIndex<nsDisplayOwnLayer>(
3734 aBuilder, this,
3735 /* aIndex = */ nsDisplayOwnLayer::OwnLayerForTransformWithRoundedClip,
3736 &resultList, aBuilder->CurrentActiveScrolledRoot(),
3737 nsDisplayOwnLayerFlags::None, ScrollbarData{},
3738 /* aForceActive = */ false, false);
3739 createdContainer = true;
3742 // If we have sticky positioning, wrap it in a sticky position item.
3743 if (useFixedPosition) {
3744 if (clipCapturedBy == ContainerItemType::FixedPosition) {
3745 clipState.Restore();
3747 // The ASR for the fixed item should be the ASR of our containing block,
3748 // which has been set as the builder's current ASR, unless this frame is
3749 // invisible and we hadn't saved display item data for it. In that case,
3750 // we need to take the containerItemASR since we might have fixed children.
3751 // For WebRender, we want to the know what |containerItemASR| is for the
3752 // case where the fixed-pos item is not a "real" fixed-pos item (e.g. it's
3753 // nested inside a scrolling transform), so we stash that on the display
3754 // item as well.
3755 const ActiveScrolledRoot* fixedASR = ActiveScrolledRoot::PickAncestor(
3756 containerItemASR, aBuilder->CurrentActiveScrolledRoot());
3757 resultList.AppendNewToTop<nsDisplayFixedPosition>(
3758 aBuilder, this, &resultList, fixedASR, containerItemASR);
3759 createdContainer = true;
3760 } else if (useStickyPosition) {
3761 // For position:sticky, the clip needs to be applied both to the sticky
3762 // container item and to the contents. The container item needs the clip
3763 // because a scrolled clip needs to move independently from the sticky
3764 // contents, and the contents need the clip so that they have finite
3765 // clipped bounds with respect to the container item's ASR. The latter is
3766 // a little tricky in the case where the sticky item has both fixed and
3767 // non-fixed descendants, because that means that the sticky container
3768 // item's ASR is the ASR of the fixed descendant.
3769 // For WebRender display list building, though, we still want to know the
3770 // the ASR that the sticky container item would normally have, so we stash
3771 // that on the display item as the "container ASR" (i.e. the normal ASR of
3772 // the container item, excluding the special behaviour induced by fixed
3773 // descendants).
3774 const ActiveScrolledRoot* stickyASR = ActiveScrolledRoot::PickAncestor(
3775 containerItemASR, aBuilder->CurrentActiveScrolledRoot());
3777 auto* stickyItem = MakeDisplayItem<nsDisplayStickyPosition>(
3778 aBuilder, this, &resultList, stickyASR,
3779 aBuilder->CurrentActiveScrolledRoot(),
3780 clipState.IsClippedToDisplayPort());
3782 bool shouldFlatten = true;
3784 StickyScrollContainer* stickyScrollContainer =
3785 StickyScrollContainer::GetStickyScrollContainerForFrame(this);
3786 if (stickyScrollContainer &&
3787 stickyScrollContainer->ScrollFrame()->IsMaybeAsynchronouslyScrolled()) {
3788 shouldFlatten = false;
3791 stickyItem->SetShouldFlatten(shouldFlatten);
3793 resultList.AppendToTop(stickyItem);
3794 createdContainer = true;
3796 // If the sticky element is inside a filter, annotate the scroll frame that
3797 // scrolls the filter as having out-of-flow content inside a filter (this
3798 // inhibits paint skipping).
3799 if (aBuilder->GetFilterASR() && aBuilder->GetFilterASR() == stickyASR) {
3800 aBuilder->GetFilterASR()
3801 ->mScrollableFrame->SetHasOutOfFlowContentInsideFilter();
3805 // If there's blending, wrap up the list in a blend-mode item. Note that
3806 // opacity can be applied before blending as the blend color is not affected
3807 // by foreground opacity (only background alpha).
3808 if (useBlendMode) {
3809 DisplayListClipState::AutoSaveRestore blendModeClipState(aBuilder);
3810 resultList.AppendNewToTop<nsDisplayBlendMode>(aBuilder, this, &resultList,
3811 effects->mMixBlendMode,
3812 containerItemASR, false);
3813 createdContainer = true;
3816 if (aBuilder->IsReusingStackingContextItems()) {
3817 if (resultList.IsEmpty()) {
3818 return;
3821 nsDisplayItem* container = resultList.GetBottom();
3822 if (resultList.Length() > 1 || container->Frame() != this) {
3823 container = MakeDisplayItem<nsDisplayContainer>(
3824 aBuilder, this, containerItemASR, &resultList);
3825 } else {
3826 MOZ_ASSERT(resultList.Length() == 1);
3827 resultList.Clear();
3830 // Mark the outermost display item as reusable. These display items and
3831 // their chidren can be reused during the next paint if no ancestor or
3832 // descendant frames have been modified.
3833 if (!container->IsReusedItem()) {
3834 container->SetReusable();
3836 aList->AppendToTop(container);
3837 createdContainer = true;
3838 } else {
3839 aList->AppendToTop(&resultList);
3842 if (aCreatedContainerItem) {
3843 *aCreatedContainerItem = createdContainer;
3847 static nsDisplayItem* WrapInWrapList(nsDisplayListBuilder* aBuilder,
3848 nsIFrame* aFrame, nsDisplayList* aList,
3849 const ActiveScrolledRoot* aContainerASR,
3850 bool aBuiltContainerItem = false) {
3851 nsDisplayItem* item = aList->GetBottom();
3852 if (!item) {
3853 return nullptr;
3856 // We need a wrap list if there are multiple items, or if the single
3857 // item has a different frame. This can change in a partial build depending
3858 // on which items we build, so we need to ensure that we don't transition
3859 // to/from a wrap list without invalidating correctly.
3860 bool needsWrapList =
3861 aList->Length() > 1 || item->Frame() != aFrame || item->GetChildren();
3863 // If we have an explicit container item (that can't change without an
3864 // invalidation) or we're doing a full build and don't need a wrap list, then
3865 // we can skip adding one.
3866 if (aBuiltContainerItem || (!aBuilder->IsPartialUpdate() && !needsWrapList)) {
3867 MOZ_ASSERT(aList->Length() == 1);
3868 aList->Clear();
3869 return item;
3872 // If we're doing a partial build and we didn't need a wrap list
3873 // previously then we can try to work from there.
3874 if (aBuilder->IsPartialUpdate() &&
3875 !aFrame->HasDisplayItem(uint32_t(DisplayItemType::TYPE_CONTAINER))) {
3876 // If we now need a wrap list, we must previously have had no display items
3877 // or a single one belonging to this frame. Mark the item itself as
3878 // discarded so that RetainedDisplayListBuilder uses the ones we just built.
3879 // We don't want to mark the frame as modified as that would invalidate
3880 // positioned descendants that might be outside of this list, and might not
3881 // have been rebuilt this time.
3882 if (needsWrapList) {
3883 DiscardOldItems(aFrame);
3884 } else {
3885 MOZ_ASSERT(aList->Length() == 1);
3886 aList->Clear();
3887 return item;
3891 // The last case we could try to handle is when we previously had a wrap list,
3892 // but no longer need it. Unfortunately we can't differentiate this case from
3893 // a partial build where other children exist but we just didn't build them
3894 // this time.
3895 // TODO:RetainedDisplayListBuilder's merge phase has the full list and
3896 // could strip them out.
3898 return MakeDisplayItem<nsDisplayContainer>(aBuilder, aFrame, aContainerASR,
3899 aList);
3903 * Check if a frame should be visited for building display list.
3905 static bool DescendIntoChild(nsDisplayListBuilder* aBuilder,
3906 const nsIFrame* aChild, const nsRect& aVisible,
3907 const nsRect& aDirty) {
3908 if (aChild->HasAnyStateBits(NS_FRAME_FORCE_DISPLAY_LIST_DESCEND_INTO)) {
3909 return true;
3912 // If the child is a scrollframe that we want to ignore, then we need
3913 // to descend into it because its scrolled child may intersect the dirty
3914 // area even if the scrollframe itself doesn't.
3915 if (aChild == aBuilder->GetIgnoreScrollFrame()) {
3916 return true;
3919 // There are cases where the "ignore scroll frame" on the builder is not set
3920 // correctly, and so we additionally want to catch cases where the child is
3921 // a root scrollframe and we are ignoring scrolling on the viewport.
3922 if (aChild == aBuilder->GetPresShellIgnoreScrollFrame()) {
3923 return true;
3926 nsRect overflow = aChild->InkOverflowRect();
3928 // On mobile, there may be a dynamic toolbar. The root content document's
3929 // root scroll frame's ink overflow rect does not include the toolbar
3930 // height, but if the toolbar is hidden, we still want to be able to target
3931 // content underneath the toolbar, so expand the overflow rect here to
3932 // allow display list building to descend into the scroll frame.
3933 if (aBuilder->IsForEventDelivery() &&
3934 aChild == aChild->PresShell()->GetRootScrollFrame() &&
3935 aChild->PresContext()->IsRootContentDocumentCrossProcess() &&
3936 aChild->PresContext()->HasDynamicToolbar()) {
3937 overflow.SizeTo(nsLayoutUtils::ExpandHeightForDynamicToolbar(
3938 aChild->PresContext(), overflow.Size()));
3941 if (aDirty.Intersects(overflow)) {
3942 return true;
3945 if (aChild->ForceDescendIntoIfVisible() && aVisible.Intersects(overflow)) {
3946 return true;
3949 if (aChild->IsFrameOfType(nsIFrame::eTablePart)) {
3950 // Relative positioning and transforms can cause table parts to move, but we
3951 // will still paint the backgrounds for their ancestor parts under them at
3952 // their 'normal' position. That means that we must consider the overflow
3953 // rects at both positions.
3955 // We convert the overflow rect into the nsTableFrame's coordinate
3956 // space, applying the normal position offset at each step. Then we
3957 // compare that against the builder's cached dirty rect in table
3958 // coordinate space.
3959 const nsIFrame* f = aChild;
3960 nsRect normalPositionOverflowRelativeToTable = overflow;
3962 while (f->IsFrameOfType(nsIFrame::eTablePart)) {
3963 normalPositionOverflowRelativeToTable += f->GetNormalPosition();
3964 f = f->GetParent();
3967 nsDisplayTableBackgroundSet* tableBGs = aBuilder->GetTableBackgroundSet();
3968 if (tableBGs && tableBGs->GetDirtyRect().Intersects(
3969 normalPositionOverflowRelativeToTable)) {
3970 return true;
3974 return false;
3977 void nsIFrame::BuildDisplayListForSimpleChild(nsDisplayListBuilder* aBuilder,
3978 nsIFrame* aChild,
3979 const nsDisplayListSet& aLists) {
3980 // This is the shortcut for frames been handled along the common
3981 // path, the most common one of THE COMMON CASE mentioned later.
3982 MOZ_ASSERT(aChild->Type() != LayoutFrameType::Placeholder);
3983 MOZ_ASSERT(!aBuilder->GetSelectedFramesOnly() &&
3984 !aBuilder->GetIncludeAllOutOfFlows(),
3985 "It should be held for painting to window");
3986 MOZ_ASSERT(aChild->HasAnyStateBits(NS_FRAME_SIMPLE_DISPLAYLIST));
3988 const nsPoint offset = aChild->GetOffsetTo(this);
3989 const nsRect visible = aBuilder->GetVisibleRect() - offset;
3990 const nsRect dirty = aBuilder->GetDirtyRect() - offset;
3992 if (!DescendIntoChild(aBuilder, aChild, visible, dirty)) {
3993 DL_LOGV("Skipped frame %p", aChild);
3994 return;
3997 // Child cannot be transformed since it is not a stacking context.
3998 nsDisplayListBuilder::AutoBuildingDisplayList buildingForChild(
3999 aBuilder, aChild, visible, dirty, false);
4001 UpdateCurrentHitTestInfo(aBuilder, aChild);
4003 aChild->MarkAbsoluteFramesForDisplayList(aBuilder);
4004 aBuilder->AdjustWindowDraggingRegion(aChild);
4005 aBuilder->Check();
4006 aChild->BuildDisplayList(aBuilder, aLists);
4007 aChild->SetBuiltDisplayList(true);
4008 aBuilder->Check();
4009 aBuilder->DisplayCaret(aChild, aLists.Outlines());
4012 static bool ShouldSkipFrame(nsDisplayListBuilder* aBuilder,
4013 const nsIFrame* aFrame) {
4014 // If painting is restricted to just the background of the top level frame,
4015 // then we have nothing to do here.
4016 if (aBuilder->IsBackgroundOnly()) {
4017 return true;
4019 if (aBuilder->IsForGenerateGlyphMask() &&
4020 (!aFrame->IsTextFrame() && aFrame->IsLeaf())) {
4021 return true;
4023 // The placeholder frame should have the same content as the OOF frame.
4024 if (aBuilder->GetSelectedFramesOnly() &&
4025 (aFrame->IsLeaf() && !aFrame->IsSelected())) {
4026 return true;
4028 static const nsFrameState skipFlags =
4029 (NS_FRAME_TOO_DEEP_IN_FRAME_TREE | NS_FRAME_IS_NONDISPLAY);
4030 if (aFrame->HasAnyStateBits(skipFlags)) {
4031 return true;
4033 return aFrame->StyleUIReset()->mMozSubtreeHiddenOnlyVisually;
4036 void nsIFrame::BuildDisplayListForChild(nsDisplayListBuilder* aBuilder,
4037 nsIFrame* aChild,
4038 const nsDisplayListSet& aLists,
4039 DisplayChildFlags aFlags) {
4040 AutoCheckBuilder check(aBuilder);
4041 #ifdef DEBUG
4042 DL_LOGV("BuildDisplayListForChild (%p) <", aChild);
4043 ScopeExit e(
4044 [aChild]() { DL_LOGV("> BuildDisplayListForChild (%p)", aChild); });
4045 #endif
4047 if (ShouldSkipFrame(aBuilder, aChild)) {
4048 return;
4051 if (HidesContent()) {
4052 return;
4055 // If we're generating a display list for printing, include Link items for
4056 // frames that correspond to HTML link elements so that we can have active
4057 // links in saved PDF output. Note that the state of "within a link" is
4058 // set on the display-list builder, such that all descendants of the link
4059 // element will generate display-list links.
4060 // TODO: we should be able to optimize this so as to avoid creating links
4061 // for the same destination that entirely overlap each other, which adds
4062 // nothing useful to the final PDF.
4063 Maybe<nsDisplayListBuilder::Linkifier> linkifier;
4064 if (StaticPrefs::print_save_as_pdf_links_enabled() &&
4065 aBuilder->IsForPrinting()) {
4066 linkifier.emplace(aBuilder, aChild, aLists.Content());
4067 linkifier->MaybeAppendLink(aBuilder, aChild);
4070 nsIFrame* child = aChild;
4071 auto* placeholder = child->IsPlaceholderFrame()
4072 ? static_cast<nsPlaceholderFrame*>(child)
4073 : nullptr;
4074 nsIFrame* childOrOutOfFlow =
4075 placeholder ? placeholder->GetOutOfFlowFrame() : child;
4077 nsIFrame* parent = childOrOutOfFlow->GetParent();
4078 const auto* parentDisplay = parent->StyleDisplay();
4079 const auto overflowClipAxes =
4080 parent->ShouldApplyOverflowClipping(parentDisplay);
4082 const bool isPaintingToWindow = aBuilder->IsPaintingToWindow();
4083 const bool doingShortcut =
4084 isPaintingToWindow &&
4085 child->HasAnyStateBits(NS_FRAME_SIMPLE_DISPLAYLIST) &&
4086 // Animations may change the stacking context state.
4087 // ShouldApplyOverflowClipping is affected by the parent style, which does
4088 // not invalidate the NS_FRAME_SIMPLE_DISPLAYLIST bit.
4089 !(overflowClipAxes != PhysicalAxes::None ||
4090 child->MayHaveTransformAnimation() || child->MayHaveOpacityAnimation());
4092 if (aBuilder->IsForPainting()) {
4093 aBuilder->ClearWillChangeBudgetStatus(child);
4096 if (StaticPrefs::layout_css_scroll_anchoring_highlight()) {
4097 if (child->FirstContinuation()->IsScrollAnchor()) {
4098 nsRect bounds = child->GetContentRectRelativeToSelf() +
4099 aBuilder->ToReferenceFrame(child);
4100 nsDisplaySolidColor* color = MakeDisplayItem<nsDisplaySolidColor>(
4101 aBuilder, child, bounds, NS_RGBA(255, 0, 255, 64));
4102 if (color) {
4103 color->SetOverrideZIndex(INT32_MAX);
4104 aLists.PositionedDescendants()->AppendToTop(color);
4109 if (doingShortcut) {
4110 BuildDisplayListForSimpleChild(aBuilder, child, aLists);
4111 return;
4114 // dirty rect in child-relative coordinates
4115 NS_ASSERTION(aBuilder->GetCurrentFrame() == this, "Wrong coord space!");
4116 const nsPoint offset = child->GetOffsetTo(this);
4117 nsRect visible = aBuilder->GetVisibleRect() - offset;
4118 nsRect dirty = aBuilder->GetDirtyRect() - offset;
4120 nsDisplayListBuilder::OutOfFlowDisplayData* savedOutOfFlowData = nullptr;
4121 if (placeholder) {
4122 if (placeholder->HasAnyStateBits(PLACEHOLDER_FOR_TOPLAYER)) {
4123 // If the out-of-flow frame is in the top layer, the viewport frame
4124 // will paint it. Skip it here. Note that, only out-of-flow frames
4125 // with this property should be skipped, because non-HTML elements
4126 // may stop their children from being out-of-flow. Those frames
4127 // should still be handled in the normal in-flow path.
4128 return;
4131 child = childOrOutOfFlow;
4132 if (aBuilder->IsForPainting()) {
4133 aBuilder->ClearWillChangeBudgetStatus(child);
4136 // If 'child' is a pushed float then it's owned by a block that's not an
4137 // ancestor of the placeholder, and it will be painted by that block and
4138 // should not be painted through the placeholder. Also recheck
4139 // NS_FRAME_TOO_DEEP_IN_FRAME_TREE and NS_FRAME_IS_NONDISPLAY.
4140 static const nsFrameState skipFlags =
4141 (NS_FRAME_IS_PUSHED_FLOAT | NS_FRAME_TOO_DEEP_IN_FRAME_TREE |
4142 NS_FRAME_IS_NONDISPLAY);
4143 if (child->HasAnyStateBits(skipFlags) || nsLayoutUtils::IsPopup(child)) {
4144 return;
4147 MOZ_ASSERT(child->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW));
4148 savedOutOfFlowData = nsDisplayListBuilder::GetOutOfFlowData(child);
4150 if (aBuilder->GetIncludeAllOutOfFlows()) {
4151 visible = child->InkOverflowRect();
4152 dirty = child->InkOverflowRect();
4153 } else if (savedOutOfFlowData) {
4154 visible =
4155 savedOutOfFlowData->GetVisibleRectForFrame(aBuilder, child, &dirty);
4156 } else {
4157 // The out-of-flow frame did not intersect the dirty area. We may still
4158 // need to traverse into it, since it may contain placeholders we need
4159 // to enter to reach other out-of-flow frames that are visible.
4160 visible.SetEmpty();
4161 dirty.SetEmpty();
4165 NS_ASSERTION(!child->IsPlaceholderFrame(),
4166 "Should have dealt with placeholders already");
4168 if (!DescendIntoChild(aBuilder, child, visible, dirty)) {
4169 DL_LOGV("Skipped frame %p", child);
4170 return;
4173 const bool isSVG = child->HasAnyStateBits(NS_FRAME_SVG_LAYOUT);
4175 // This flag is raised if the control flow strays off the common path.
4176 // The common path is the most common one of THE COMMON CASE mentioned later.
4177 bool awayFromCommonPath = !isPaintingToWindow;
4179 // true if this is a real or pseudo stacking context
4180 bool pseudoStackingContext =
4181 aFlags.contains(DisplayChildFlag::ForcePseudoStackingContext);
4183 if (!pseudoStackingContext && !isSVG &&
4184 aFlags.contains(DisplayChildFlag::Inline) &&
4185 !child->IsFrameOfType(eLineParticipant)) {
4186 // child is a non-inline frame in an inline context, i.e.,
4187 // it acts like inline-block or inline-table. Therefore it is a
4188 // pseudo-stacking-context.
4189 pseudoStackingContext = true;
4192 const nsStyleDisplay* ourDisp = StyleDisplay();
4193 // Don't paint our children if the theme object is a leaf.
4194 if (IsThemed(ourDisp) && !PresContext()->Theme()->WidgetIsContainer(
4195 ourDisp->EffectiveAppearance())) {
4196 return;
4199 // Since we're now sure that we're adding this frame to the display list
4200 // (which means we're painting it, modulo occlusion), mark it as visible
4201 // within the displayport.
4202 if (isPaintingToWindow && child->TrackingVisibility() &&
4203 child->IsVisibleForPainting()) {
4204 child->PresShell()->EnsureFrameInApproximatelyVisibleList(child);
4205 awayFromCommonPath = true;
4208 // Child is composited if it's transformed, partially transparent, or has
4209 // SVG effects or a blend mode..
4210 const nsStyleDisplay* disp = child->StyleDisplay();
4211 const nsStyleEffects* effects = child->StyleEffects();
4213 const bool isPositioned = disp->IsPositionedStyle();
4214 const bool isStackingContext =
4215 aFlags.contains(DisplayChildFlag::ForceStackingContext) ||
4216 child->IsStackingContext(disp, effects);
4218 if (pseudoStackingContext || isStackingContext || isPositioned ||
4219 placeholder || (!isSVG && disp->IsFloating(child)) ||
4220 (isSVG && effects->mClip.IsRect() && IsSVGContentWithCSSClip(child))) {
4221 pseudoStackingContext = true;
4222 awayFromCommonPath = true;
4225 NS_ASSERTION(!isStackingContext || pseudoStackingContext,
4226 "Stacking contexts must also be pseudo-stacking-contexts");
4228 nsDisplayListBuilder::AutoBuildingDisplayList buildingForChild(
4229 aBuilder, child, visible, dirty);
4231 UpdateCurrentHitTestInfo(aBuilder, child);
4233 DisplayListClipState::AutoClipMultiple clipState(aBuilder);
4234 nsDisplayListBuilder::AutoCurrentActiveScrolledRootSetter asrSetter(aBuilder);
4236 if (savedOutOfFlowData) {
4237 aBuilder->SetBuildingInvisibleItems(false);
4239 clipState.SetClipChainForContainingBlockDescendants(
4240 savedOutOfFlowData->mContainingBlockClipChain);
4241 asrSetter.SetCurrentActiveScrolledRoot(
4242 savedOutOfFlowData->mContainingBlockActiveScrolledRoot);
4243 asrSetter.SetCurrentScrollParentId(savedOutOfFlowData->mScrollParentId);
4244 MOZ_ASSERT(awayFromCommonPath,
4245 "It is impossible when savedOutOfFlowData is true");
4246 } else if (HasAnyStateBits(NS_FRAME_FORCE_DISPLAY_LIST_DESCEND_INTO) &&
4247 placeholder) {
4248 NS_ASSERTION(visible.IsEmpty(), "should have empty visible rect");
4249 // Every item we build from now until we descent into an out of flow that
4250 // does have saved out of flow data should be invisible. This state gets
4251 // restored when AutoBuildingDisplayList gets out of scope.
4252 aBuilder->SetBuildingInvisibleItems(true);
4254 // If we have nested out-of-flow frames and the outer one isn't visible
4255 // then we won't have stored clip data for it. We can just clear the clip
4256 // instead since we know we won't render anything, and the inner out-of-flow
4257 // frame will setup the correct clip for itself.
4258 clipState.SetClipChainForContainingBlockDescendants(nullptr);
4261 // Setup clipping for the parent's overflow:clip,
4262 // or overflow:hidden on elements that don't support scrolling (and therefore
4263 // don't create nsHTML/XULScrollFrame). This clipping needs to not clip
4264 // anything directly rendered by the parent, only the rendering of its
4265 // children.
4266 // Don't use overflowClip to restrict the dirty rect, since some of the
4267 // descendants may not be clipped by it. Even if we end up with unnecessary
4268 // display items, they'll be pruned during ComputeVisibility.
4270 // FIXME(emilio): Why can't we handle this more similarly to `clip` (on the
4271 // parent, rather than on the children)? Would ClipContentDescendants do what
4272 // we want?
4273 if (overflowClipAxes != PhysicalAxes::None) {
4274 ApplyOverflowClipping(aBuilder, parent, overflowClipAxes, clipState);
4275 awayFromCommonPath = true;
4278 nsDisplayList list(aBuilder);
4279 nsDisplayList extraPositionedDescendants(aBuilder);
4280 const ActiveScrolledRoot* wrapListASR;
4281 bool builtContainerItem = false;
4282 if (isStackingContext) {
4283 // True stacking context.
4284 // For stacking contexts, BuildDisplayListForStackingContext handles
4285 // clipping and MarkAbsoluteFramesForDisplayList.
4286 nsDisplayListBuilder::AutoContainerASRTracker contASRTracker(aBuilder);
4287 child->BuildDisplayListForStackingContext(aBuilder, &list,
4288 &builtContainerItem);
4289 wrapListASR = contASRTracker.GetContainerASR();
4290 if (!aBuilder->IsReusingStackingContextItems() &&
4291 aBuilder->GetCaretFrame() == child) {
4292 builtContainerItem = false;
4294 } else {
4295 Maybe<nsRect> clipPropClip =
4296 child->GetClipPropClipRect(disp, effects, child->GetSize());
4297 if (clipPropClip) {
4298 aBuilder->IntersectVisibleRect(*clipPropClip);
4299 aBuilder->IntersectDirtyRect(*clipPropClip);
4300 clipState.ClipContentDescendants(*clipPropClip +
4301 aBuilder->ToReferenceFrame(child));
4302 awayFromCommonPath = true;
4305 child->MarkAbsoluteFramesForDisplayList(aBuilder);
4306 child->SetBuiltDisplayList(true);
4308 if (!awayFromCommonPath &&
4309 // Some SVG frames might change opacity without invalidating the frame,
4310 // so exclude them from the fast-path.
4311 !child->IsFrameOfType(nsIFrame::eSVG)) {
4312 // The shortcut is available for the child for next time.
4313 child->AddStateBits(NS_FRAME_SIMPLE_DISPLAYLIST);
4316 if (!pseudoStackingContext) {
4317 // THIS IS THE COMMON CASE.
4318 // Not a pseudo or real stacking context. Do the simple thing and
4319 // return early.
4320 aBuilder->AdjustWindowDraggingRegion(child);
4321 aBuilder->Check();
4322 child->BuildDisplayList(aBuilder, aLists);
4323 aBuilder->Check();
4324 aBuilder->DisplayCaret(child, aLists.Outlines());
4325 return;
4328 // A pseudo-stacking context (e.g., a positioned element with z-index auto).
4329 // We allow positioned descendants of the child to escape to our parent
4330 // stacking context's positioned descendant list, because they might be
4331 // z-index:non-auto
4332 nsDisplayListCollection pseudoStack(aBuilder);
4334 aBuilder->AdjustWindowDraggingRegion(child);
4335 nsDisplayListBuilder::AutoContainerASRTracker contASRTracker(aBuilder);
4336 aBuilder->Check();
4337 child->BuildDisplayList(aBuilder, pseudoStack);
4338 aBuilder->Check();
4339 if (aBuilder->DisplayCaret(child, pseudoStack.Outlines())) {
4340 builtContainerItem = false;
4342 wrapListASR = contASRTracker.GetContainerASR();
4344 list.AppendToTop(pseudoStack.BorderBackground());
4345 list.AppendToTop(pseudoStack.BlockBorderBackgrounds());
4346 list.AppendToTop(pseudoStack.Floats());
4347 list.AppendToTop(pseudoStack.Content());
4348 list.AppendToTop(pseudoStack.Outlines());
4349 extraPositionedDescendants.AppendToTop(pseudoStack.PositionedDescendants());
4352 buildingForChild.RestoreBuildingInvisibleItemsValue();
4354 if (!list.IsEmpty()) {
4355 if (isPositioned || isStackingContext) {
4356 // Genuine stacking contexts, and positioned pseudo-stacking-contexts,
4357 // go in this level.
4358 nsDisplayItem* item = WrapInWrapList(aBuilder, child, &list, wrapListASR,
4359 builtContainerItem);
4360 if (isSVG) {
4361 aLists.Content()->AppendToTop(item);
4362 } else {
4363 aLists.PositionedDescendants()->AppendToTop(item);
4365 } else if (!isSVG && disp->IsFloating(child)) {
4366 aLists.Floats()->AppendToTop(
4367 WrapInWrapList(aBuilder, child, &list, wrapListASR));
4368 } else {
4369 aLists.Content()->AppendToTop(&list);
4372 // We delay placing the positioned descendants of positioned frames to here,
4373 // because in the absence of z-index this is the correct order for them.
4374 // This doesn't affect correctness because the positioned descendants list
4375 // is sorted by z-order and content in BuildDisplayListForStackingContext,
4376 // but it means that sort routine needs to do less work.
4377 aLists.PositionedDescendants()->AppendToTop(&extraPositionedDescendants);
4380 void nsIFrame::MarkAbsoluteFramesForDisplayList(
4381 nsDisplayListBuilder* aBuilder) {
4382 if (IsAbsoluteContainer()) {
4383 aBuilder->MarkFramesForDisplayList(
4384 this, GetAbsoluteContainingBlock()->GetChildList());
4388 nsresult nsIFrame::GetContentForEvent(const WidgetEvent* aEvent,
4389 nsIContent** aContent) {
4390 nsIFrame* f = nsLayoutUtils::GetNonGeneratedAncestor(this);
4391 *aContent = f->GetContent();
4392 NS_IF_ADDREF(*aContent);
4393 return NS_OK;
4396 void nsIFrame::FireDOMEvent(const nsAString& aDOMEventName,
4397 nsIContent* aContent) {
4398 nsIContent* target = aContent ? aContent : GetContent();
4400 if (target) {
4401 RefPtr<AsyncEventDispatcher> asyncDispatcher = new AsyncEventDispatcher(
4402 target, aDOMEventName, CanBubble::eYes, ChromeOnlyDispatch::eNo);
4403 DebugOnly<nsresult> rv = asyncDispatcher->PostDOMEvent();
4404 NS_ASSERTION(NS_SUCCEEDED(rv), "AsyncEventDispatcher failed to dispatch");
4408 nsresult nsIFrame::HandleEvent(nsPresContext* aPresContext,
4409 WidgetGUIEvent* aEvent,
4410 nsEventStatus* aEventStatus) {
4411 if (aEvent->mMessage == eMouseMove) {
4412 // XXX If the second argument of HandleDrag() is WidgetMouseEvent,
4413 // the implementation becomes simpler.
4414 return HandleDrag(aPresContext, aEvent, aEventStatus);
4417 if ((aEvent->mClass == eMouseEventClass &&
4418 aEvent->AsMouseEvent()->mButton == MouseButton::ePrimary) ||
4419 aEvent->mClass == eTouchEventClass) {
4420 if (aEvent->mMessage == eMouseDown || aEvent->mMessage == eTouchStart) {
4421 HandlePress(aPresContext, aEvent, aEventStatus);
4422 } else if (aEvent->mMessage == eMouseUp || aEvent->mMessage == eTouchEnd) {
4423 HandleRelease(aPresContext, aEvent, aEventStatus);
4425 return NS_OK;
4428 // When secondary buttion is down, we need to move selection to make users
4429 // possible to paste something at click point quickly.
4430 // When middle button is down, we need to just move selection and focus at
4431 // the clicked point. Note that even if middle click paste is not enabled,
4432 // Chrome moves selection at middle mouse button down. So, we should follow
4433 // the behavior for the compatibility.
4434 if (aEvent->mMessage == eMouseDown) {
4435 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
4436 if (mouseEvent && (mouseEvent->mButton == MouseButton::eSecondary ||
4437 mouseEvent->mButton == MouseButton::eMiddle)) {
4438 if (*aEventStatus == nsEventStatus_eConsumeNoDefault) {
4439 return NS_OK;
4441 return MoveCaretToEventPoint(aPresContext, mouseEvent, aEventStatus);
4445 return NS_OK;
4448 nsresult nsIFrame::GetDataForTableSelection(
4449 const nsFrameSelection* aFrameSelection, mozilla::PresShell* aPresShell,
4450 WidgetMouseEvent* aMouseEvent, nsIContent** aParentContent,
4451 int32_t* aContentOffset, TableSelectionMode* aTarget) {
4452 if (!aFrameSelection || !aPresShell || !aMouseEvent || !aParentContent ||
4453 !aContentOffset || !aTarget)
4454 return NS_ERROR_NULL_POINTER;
4456 *aParentContent = nullptr;
4457 *aContentOffset = 0;
4458 *aTarget = TableSelectionMode::None;
4460 int16_t displaySelection = aPresShell->GetSelectionFlags();
4462 bool selectingTableCells = aFrameSelection->IsInTableSelectionMode();
4464 // DISPLAY_ALL means we're in an editor.
4465 // If already in cell selection mode,
4466 // continue selecting with mouse drag or end on mouse up,
4467 // or when using shift key to extend block of cells
4468 // (Mouse down does normal selection unless Ctrl/Cmd is pressed)
4469 bool doTableSelection =
4470 displaySelection == nsISelectionDisplay::DISPLAY_ALL &&
4471 selectingTableCells &&
4472 (aMouseEvent->mMessage == eMouseMove ||
4473 (aMouseEvent->mMessage == eMouseUp &&
4474 aMouseEvent->mButton == MouseButton::ePrimary) ||
4475 aMouseEvent->IsShift());
4477 if (!doTableSelection) {
4478 // In Browser, special 'table selection' key must be pressed for table
4479 // selection or when just Shift is pressed and we're already in table/cell
4480 // selection mode
4481 #ifdef XP_MACOSX
4482 doTableSelection = aMouseEvent->IsMeta() ||
4483 (aMouseEvent->IsShift() && selectingTableCells);
4484 #else
4485 doTableSelection = aMouseEvent->IsControl() ||
4486 (aMouseEvent->IsShift() && selectingTableCells);
4487 #endif
4489 if (!doTableSelection) return NS_OK;
4491 // Get the cell frame or table frame (or parent) of the current content node
4492 nsIFrame* frame = this;
4493 bool foundCell = false;
4494 bool foundTable = false;
4496 // Get the limiting node to stop parent frame search
4497 nsIContent* limiter = aFrameSelection->GetLimiter();
4499 // If our content node is an ancestor of the limiting node,
4500 // we should stop the search right now.
4501 if (limiter && limiter->IsInclusiveDescendantOf(GetContent())) return NS_OK;
4503 // We don't initiate row/col selection from here now,
4504 // but we may in future
4505 // bool selectColumn = false;
4506 // bool selectRow = false;
4508 while (frame) {
4509 // Check for a table cell by querying to a known CellFrame interface
4510 nsITableCellLayout* cellElement = do_QueryFrame(frame);
4511 if (cellElement) {
4512 foundCell = true;
4513 // TODO: If we want to use proximity to top or left border
4514 // for row and column selection, this is the place to do it
4515 break;
4516 } else {
4517 // If not a cell, check for table
4518 // This will happen when starting frame is the table or child of a table,
4519 // such as a row (we were inbetween cells or in table border)
4520 nsTableWrapperFrame* tableFrame = do_QueryFrame(frame);
4521 if (tableFrame) {
4522 foundTable = true;
4523 // TODO: How can we select row when along left table edge
4524 // or select column when along top edge?
4525 break;
4526 } else {
4527 frame = frame->GetParent();
4528 // Stop if we have hit the selection's limiting content node
4529 if (frame && frame->GetContent() == limiter) break;
4533 // We aren't in a cell or table
4534 if (!foundCell && !foundTable) return NS_OK;
4536 nsIContent* tableOrCellContent = frame->GetContent();
4537 if (!tableOrCellContent) return NS_ERROR_FAILURE;
4539 nsCOMPtr<nsIContent> parentContent = tableOrCellContent->GetParent();
4540 if (!parentContent) return NS_ERROR_FAILURE;
4542 const int32_t offset =
4543 parentContent->ComputeIndexOf_Deprecated(tableOrCellContent);
4544 // Not likely?
4545 if (offset < 0) {
4546 return NS_ERROR_FAILURE;
4549 // Everything is OK -- set the return values
4550 parentContent.forget(aParentContent);
4552 *aContentOffset = offset;
4554 #if 0
4555 if (selectRow)
4556 *aTarget = TableSelectionMode::Row;
4557 else if (selectColumn)
4558 *aTarget = TableSelectionMode::Column;
4559 else
4560 #endif
4561 if (foundCell) {
4562 *aTarget = TableSelectionMode::Cell;
4563 } else if (foundTable) {
4564 *aTarget = TableSelectionMode::Table;
4567 return NS_OK;
4570 static bool IsEditingHost(const nsIFrame* aFrame) {
4571 auto* element = nsGenericHTMLElement::FromNodeOrNull(aFrame->GetContent());
4572 return element && element->IsEditableRoot();
4575 static StyleUserSelect UsedUserSelect(const nsIFrame* aFrame) {
4576 if (aFrame->IsGeneratedContentFrame()) {
4577 return StyleUserSelect::None;
4580 // Per https://drafts.csswg.org/css-ui-4/#content-selection:
4582 // The used value is the same as the computed value, except:
4584 // 1 - on editable elements where the used value is always 'contain'
4585 // regardless of the computed value
4586 // 2 - when the computed value is auto, in which case the used value is one
4587 // of the other values...
4589 // See https://github.com/w3c/csswg-drafts/issues/3344 to see why we do this
4590 // at used-value time instead of at computed-value time.
4592 if (aFrame->IsTextInputFrame() || IsEditingHost(aFrame)) {
4593 // We don't implement 'contain' itself, but we make 'text' behave as
4594 // 'contain' for contenteditable and <input> / <textarea> elements anyway so
4595 // this is ok.
4596 return StyleUserSelect::Text;
4599 auto style = aFrame->Style()->UserSelect();
4600 if (style != StyleUserSelect::Auto) {
4601 return style;
4604 auto* parent = nsLayoutUtils::GetParentOrPlaceholderFor(aFrame);
4605 return parent ? UsedUserSelect(parent) : StyleUserSelect::Text;
4608 bool nsIFrame::IsSelectable(StyleUserSelect* aSelectStyle) const {
4609 auto style = UsedUserSelect(this);
4610 if (aSelectStyle) {
4611 *aSelectStyle = style;
4613 return style != StyleUserSelect::None;
4616 bool nsIFrame::ShouldHaveLineIfEmpty() const {
4617 if (Style()->IsPseudoOrAnonBox() &&
4618 Style()->GetPseudoType() != PseudoStyleType::scrolledContent) {
4619 return false;
4621 return IsEditingHost(this);
4625 * Handles the Mouse Press Event for the frame
4627 NS_IMETHODIMP
4628 nsIFrame::HandlePress(nsPresContext* aPresContext, WidgetGUIEvent* aEvent,
4629 nsEventStatus* aEventStatus) {
4630 NS_ENSURE_ARG_POINTER(aEventStatus);
4631 if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
4632 return NS_OK;
4635 NS_ENSURE_ARG_POINTER(aEvent);
4636 if (aEvent->mClass == eTouchEventClass) {
4637 return NS_OK;
4640 return MoveCaretToEventPoint(aPresContext, aEvent->AsMouseEvent(),
4641 aEventStatus);
4644 nsresult nsIFrame::MoveCaretToEventPoint(nsPresContext* aPresContext,
4645 WidgetMouseEvent* aMouseEvent,
4646 nsEventStatus* aEventStatus) {
4647 MOZ_ASSERT(aPresContext);
4648 MOZ_ASSERT(aMouseEvent);
4649 MOZ_ASSERT(aMouseEvent->mMessage == eMouseDown);
4650 MOZ_ASSERT(aEventStatus);
4651 MOZ_ASSERT(nsEventStatus_eConsumeNoDefault != *aEventStatus);
4653 mozilla::PresShell* presShell = aPresContext->GetPresShell();
4654 if (!presShell) {
4655 return NS_ERROR_FAILURE;
4658 // We often get out of sync state issues with mousedown events that
4659 // get interrupted by alerts/dialogs.
4660 // Check with the ESM to see if we should process this one
4661 if (!aPresContext->EventStateManager()->EventStatusOK(aMouseEvent)) {
4662 return NS_OK;
4665 const nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
4666 aMouseEvent, RelativeTo{this});
4668 // When not using `alt`, and clicking on a draggable, but non-editable
4669 // element, don't do anything, and let d&d handle the event.
4671 // See bug 48876, bug 388659 and bug 55921 for context here.
4673 // FIXME(emilio): The .Contains(pt) check looks a bit fishy. When would it be
4674 // false given we're the event target? If it is needed, why not checking the
4675 // actual draggable node rect instead?
4676 if (!aMouseEvent->IsAlt() && GetRectRelativeToSelf().Contains(pt)) {
4677 for (nsIContent* content = mContent; content;
4678 content = content->GetFlattenedTreeParent()) {
4679 if (nsContentUtils::ContentIsDraggable(content) &&
4680 !content->IsEditable()) {
4681 return NS_OK;
4686 // If we are in Navigator and the click is in a draggable node, we don't want
4687 // to start selection because we don't want to interfere with a potential
4688 // drag of said node and steal all its glory.
4689 const bool isEditor =
4690 presShell->GetSelectionFlags() == nsISelectionDisplay::DISPLAY_ALL;
4692 // Don't do something if it's middle button down event.
4693 const bool isPrimaryButtonDown =
4694 aMouseEvent->mButton == MouseButton::ePrimary;
4696 // check whether style allows selection
4697 // if not, don't tell selection the mouse event even occurred.
4698 StyleUserSelect selectStyle;
4699 // check for select: none
4700 if (!IsSelectable(&selectStyle)) {
4701 return NS_OK;
4704 if (isPrimaryButtonDown) {
4705 // If the mouse is dragged outside the nearest enclosing scrollable area
4706 // while making a selection, the area will be scrolled. To do this, capture
4707 // the mouse on the nearest scrollable frame. If there isn't a scrollable
4708 // frame, or something else is already capturing the mouse, there's no
4709 // reason to capture.
4710 if (!PresShell::GetCapturingContent()) {
4711 nsIScrollableFrame* scrollFrame =
4712 nsLayoutUtils::GetNearestScrollableFrame(
4713 this, nsLayoutUtils::SCROLLABLE_SAME_DOC |
4714 nsLayoutUtils::SCROLLABLE_INCLUDE_HIDDEN);
4715 if (scrollFrame) {
4716 nsIFrame* capturingFrame = do_QueryFrame(scrollFrame);
4717 PresShell::SetCapturingContent(capturingFrame->GetContent(),
4718 CaptureFlags::IgnoreAllowedState);
4723 // XXX This is screwy; it really should use the selection frame, not the
4724 // event frame
4725 const nsFrameSelection* frameselection =
4726 selectStyle == StyleUserSelect::Text ? GetConstFrameSelection()
4727 : presShell->ConstFrameSelection();
4729 if (!frameselection || frameselection->GetDisplaySelection() ==
4730 nsISelectionController::SELECTION_OFF) {
4731 return NS_OK; // nothing to do we cannot affect selection from here
4734 #ifdef XP_MACOSX
4735 // If Control key is pressed on macOS, it should be treated as right click.
4736 // So, don't change selection.
4737 if (aMouseEvent->IsControl()) {
4738 return NS_OK;
4740 const bool control = aMouseEvent->IsMeta();
4741 #else
4742 const bool control = aMouseEvent->IsControl();
4743 #endif
4745 RefPtr<nsFrameSelection> fc = const_cast<nsFrameSelection*>(frameselection);
4746 if (isPrimaryButtonDown && aMouseEvent->mClickCount > 1) {
4747 // These methods aren't const but can't actually delete anything,
4748 // so no need for AutoWeakFrame.
4749 fc->SetDragState(true);
4750 return HandleMultiplePress(aPresContext, aMouseEvent, aEventStatus,
4751 control);
4754 ContentOffsets offsets = GetContentOffsetsFromPoint(pt, SKIP_HIDDEN);
4756 if (!offsets.content) {
4757 return NS_ERROR_FAILURE;
4760 if (aMouseEvent->mButton == MouseButton::eSecondary &&
4761 !MovingCaretToEventPointAllowedIfSecondaryButtonEvent(
4762 *frameselection, *aMouseEvent, *offsets.content)) {
4763 return NS_OK;
4766 if (aMouseEvent->mMessage == eMouseDown &&
4767 aMouseEvent->mButton == MouseButton::eMiddle &&
4768 !offsets.content->IsEditable()) {
4769 // However, some users don't like the Chrome compatible behavior of
4770 // middle mouse click. They want to keep selection after starting
4771 // autoscroll. However, the selection change is important for middle
4772 // mouse past. Therefore, we should allow users to take the traditional
4773 // behavior back by themselves unless middle click paste is enabled or
4774 // autoscrolling is disabled.
4775 if (!Preferences::GetBool("middlemouse.paste", false) &&
4776 Preferences::GetBool("general.autoScroll", false) &&
4777 Preferences::GetBool("general.autoscroll.prevent_to_collapse_selection_"
4778 "by_middle_mouse_down",
4779 false)) {
4780 return NS_OK;
4784 if (isPrimaryButtonDown) {
4785 // Let Ctrl/Cmd + left mouse down do table selection instead of drag
4786 // initiation.
4787 nsCOMPtr<nsIContent> parentContent;
4788 int32_t contentOffset;
4789 TableSelectionMode target;
4790 nsresult rv = GetDataForTableSelection(
4791 frameselection, presShell, aMouseEvent, getter_AddRefs(parentContent),
4792 &contentOffset, &target);
4793 if (NS_SUCCEEDED(rv) && parentContent) {
4794 fc->SetDragState(true);
4795 return fc->HandleTableSelection(parentContent, contentOffset, target,
4796 aMouseEvent);
4800 fc->SetDelayedCaretData(0);
4802 if (isPrimaryButtonDown) {
4803 // Check if any part of this frame is selected, and if the user clicked
4804 // inside the selected region, and if it's the left button. If so, we delay
4805 // starting a new selection since the user may be trying to drag the
4806 // selected region to some other app.
4808 if (GetContent() && GetContent()->IsMaybeSelected()) {
4809 bool inSelection = false;
4810 UniquePtr<SelectionDetails> details = frameselection->LookUpSelection(
4811 offsets.content, 0, offsets.EndOffset(), false);
4814 // If there are any details, check to see if the user clicked
4815 // within any selected region of the frame.
4818 for (SelectionDetails* curDetail = details.get(); curDetail;
4819 curDetail = curDetail->mNext.get()) {
4821 // If the user clicked inside a selection, then just
4822 // return without doing anything. We will handle placing
4823 // the caret later on when the mouse is released. We ignore
4824 // the spellcheck, find and url formatting selections.
4826 if (curDetail->mSelectionType != SelectionType::eSpellCheck &&
4827 curDetail->mSelectionType != SelectionType::eFind &&
4828 curDetail->mSelectionType != SelectionType::eURLSecondary &&
4829 curDetail->mSelectionType != SelectionType::eURLStrikeout &&
4830 curDetail->mSelectionType != SelectionType::eHighlight &&
4831 curDetail->mStart <= offsets.StartOffset() &&
4832 offsets.EndOffset() <= curDetail->mEnd) {
4833 inSelection = true;
4837 if (inSelection) {
4838 fc->SetDragState(false);
4839 fc->SetDelayedCaretData(aMouseEvent);
4840 return NS_OK;
4844 fc->SetDragState(true);
4847 // Do not touch any nsFrame members after this point without adding
4848 // weakFrame checks.
4849 const nsFrameSelection::FocusMode focusMode = [&]() {
4850 // If "Shift" and "Ctrl" are both pressed, "Shift" is given precedence. This
4851 // mimics the old behaviour.
4852 if (aMouseEvent->IsShift()) {
4853 // If clicked in a link when focused content is editable, we should
4854 // collapse selection in the link for compatibility with Blink.
4855 if (isEditor) {
4856 for (Element* element : mContent->InclusiveAncestorsOfType<Element>()) {
4857 if (element->IsLink()) {
4858 return nsFrameSelection::FocusMode::kCollapseToNewPoint;
4862 return nsFrameSelection::FocusMode::kExtendSelection;
4865 if (isPrimaryButtonDown && control) {
4866 return nsFrameSelection::FocusMode::kMultiRangeSelection;
4869 return nsFrameSelection::FocusMode::kCollapseToNewPoint;
4870 }();
4872 nsresult rv = fc->HandleClick(
4873 MOZ_KnownLive(offsets.content) /* bug 1636889 */, offsets.StartOffset(),
4874 offsets.EndOffset(), focusMode, offsets.associate);
4875 if (NS_FAILED(rv)) {
4876 return rv;
4879 // We don't handle mouse button up if it's middle button.
4880 if (isPrimaryButtonDown && offsets.offset != offsets.secondaryOffset) {
4881 fc->MaintainSelection();
4884 if (isPrimaryButtonDown && isEditor && !aMouseEvent->IsShift() &&
4885 (offsets.EndOffset() - offsets.StartOffset()) == 1) {
4886 // A single node is selected and we aren't extending an existing selection,
4887 // which means the user clicked directly on an object (either
4888 // `user-select: all` or a non-text node without children). Therefore,
4889 // disable selection extension during mouse moves.
4890 // XXX This is a bit hacky; shouldn't editor be able to deal with this?
4891 fc->SetDragState(false);
4894 return NS_OK;
4897 bool nsIFrame::MovingCaretToEventPointAllowedIfSecondaryButtonEvent(
4898 const nsFrameSelection& aFrameSelection,
4899 WidgetMouseEvent& aSecondaryButtonEvent,
4900 const nsIContent& aContentAtEventPoint) const {
4901 MOZ_ASSERT(aSecondaryButtonEvent.mButton == MouseButton::eSecondary);
4903 if (StaticPrefs::
4904 ui_mouse_right_click_collapse_selection_stop_if_non_collapsed_selection()) {
4905 if (Selection* selection =
4906 aFrameSelection.GetSelection(SelectionType::eNormal)) {
4907 if (selection->IsCollapsed()) {
4908 // If selection is collapsed, it may be allowed to move caret, let's
4909 // check other things.
4910 } else if (nsIContent* ancestorLimiter =
4911 selection->GetAncestorLimiter()) {
4912 // If currently selection is limited in an editing host, we should not
4913 // collapse selection if the clicked point is in the ancestor limiter.
4914 // Otherwise, this mouse click moves focus from the editing host to
4915 // different one or blur the editing host. In this case, we need to
4916 // update selection because keeping current selection in the editing
4917 // host looks like it's not blurred.
4918 return !aContentAtEventPoint.IsInclusiveDescendantOf(ancestorLimiter);
4920 // If currently selection is not limited in an editing host, we should
4921 // collapse selection only when this click moves focus to an editing host
4922 // because we need to update selection in this case.
4923 else if (!aContentAtEventPoint.IsEditable()) {
4924 return false;
4929 return !StaticPrefs::
4930 ui_mouse_right_click_collapse_selection_stop_if_non_editable_node() ||
4931 // The user does not want to collapse selection into non-editable
4932 // content by a right button click.
4933 aContentAtEventPoint.IsEditable() ||
4934 // Treat clicking in a text control as always clicked on editable
4935 // content because we want a hack only for clicking in normal text
4936 // nodes which is outside any editing hosts.
4937 aContentAtEventPoint.IsTextControlElement() ||
4938 TextControlElement::FromNodeOrNull(
4939 aContentAtEventPoint.GetClosestNativeAnonymousSubtreeRoot());
4942 nsresult nsIFrame::SelectByTypeAtPoint(nsPresContext* aPresContext,
4943 const nsPoint& aPoint,
4944 nsSelectionAmount aBeginAmountType,
4945 nsSelectionAmount aEndAmountType,
4946 uint32_t aSelectFlags) {
4947 NS_ENSURE_ARG_POINTER(aPresContext);
4949 // No point in selecting if selection is turned off
4950 if (DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF) {
4951 return NS_OK;
4954 ContentOffsets offsets = GetContentOffsetsFromPoint(aPoint, SKIP_HIDDEN);
4955 if (!offsets.content) {
4956 return NS_ERROR_FAILURE;
4959 int32_t offset;
4960 nsIFrame* frame = nsFrameSelection::GetFrameForNodeOffset(
4961 offsets.content, offsets.offset, offsets.associate, &offset);
4962 if (!frame) {
4963 return NS_ERROR_FAILURE;
4965 return frame->PeekBackwardAndForward(aBeginAmountType, aEndAmountType, offset,
4966 aBeginAmountType != eSelectWord,
4967 aSelectFlags);
4971 * Multiple Mouse Press -- line or paragraph selection -- for the frame.
4972 * Wouldn't it be nice if this didn't have to be hardwired into Frame code?
4974 NS_IMETHODIMP
4975 nsIFrame::HandleMultiplePress(nsPresContext* aPresContext,
4976 WidgetGUIEvent* aEvent,
4977 nsEventStatus* aEventStatus, bool aControlHeld) {
4978 NS_ENSURE_ARG_POINTER(aEvent);
4979 NS_ENSURE_ARG_POINTER(aEventStatus);
4981 if (nsEventStatus_eConsumeNoDefault == *aEventStatus ||
4982 DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF) {
4983 return NS_OK;
4986 // Find out whether we're doing line or paragraph selection.
4987 // If browser.triple_click_selects_paragraph is true, triple-click selects
4988 // paragraph. Otherwise, triple-click selects line, and quadruple-click
4989 // selects paragraph (on platforms that support quadruple-click).
4990 nsSelectionAmount beginAmount, endAmount;
4991 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
4992 if (!mouseEvent) {
4993 return NS_OK;
4996 if (mouseEvent->mClickCount == 4) {
4997 beginAmount = endAmount = eSelectParagraph;
4998 } else if (mouseEvent->mClickCount == 3) {
4999 if (Preferences::GetBool("browser.triple_click_selects_paragraph")) {
5000 beginAmount = endAmount = eSelectParagraph;
5001 } else {
5002 beginAmount = eSelectBeginLine;
5003 endAmount = eSelectEndLine;
5005 } else if (mouseEvent->mClickCount == 2) {
5006 // We only want inline frames; PeekBackwardAndForward dislikes blocks
5007 beginAmount = endAmount = eSelectWord;
5008 } else {
5009 return NS_OK;
5012 nsPoint relPoint = nsLayoutUtils::GetEventCoordinatesRelativeTo(
5013 mouseEvent, RelativeTo{this});
5014 return SelectByTypeAtPoint(aPresContext, relPoint, beginAmount, endAmount,
5015 (aControlHeld ? SELECT_ACCUMULATE : 0));
5018 nsresult nsIFrame::PeekBackwardAndForward(nsSelectionAmount aAmountBack,
5019 nsSelectionAmount aAmountForward,
5020 int32_t aStartPos, bool aJumpLines,
5021 uint32_t aSelectFlags) {
5022 nsIFrame* baseFrame = this;
5023 int32_t baseOffset = aStartPos;
5024 nsresult rv;
5026 PeekOffsetOptions peekOffsetOptions{PeekOffsetOption::ScrollViewStop};
5027 if (aJumpLines) {
5028 peekOffsetOptions += PeekOffsetOption::JumpLines;
5031 if (aAmountBack == eSelectWord) {
5032 // To avoid selecting the previous word when at start of word,
5033 // first move one character forward.
5034 PeekOffsetStruct pos(eSelectCharacter, eDirNext, aStartPos, nsPoint(0, 0),
5035 peekOffsetOptions);
5036 rv = PeekOffset(&pos);
5037 if (NS_SUCCEEDED(rv)) {
5038 baseFrame = pos.mResultFrame;
5039 baseOffset = pos.mContentOffset;
5043 // Search backward for a boundary.
5044 PeekOffsetStruct startpos(aAmountBack, eDirPrevious, baseOffset,
5045 nsPoint(0, 0), peekOffsetOptions);
5046 rv = baseFrame->PeekOffset(&startpos);
5047 if (NS_FAILED(rv)) {
5048 return rv;
5051 // If the backward search stayed within the same frame, search forward from
5052 // that position for the end boundary; but if it crossed out to a sibling or
5053 // ancestor, start from the original position.
5054 if (startpos.mResultFrame == baseFrame) {
5055 baseOffset = startpos.mContentOffset;
5056 } else {
5057 baseFrame = this;
5058 baseOffset = aStartPos;
5061 PeekOffsetStruct endpos(aAmountForward, eDirNext, baseOffset, nsPoint(0, 0),
5062 peekOffsetOptions);
5063 rv = baseFrame->PeekOffset(&endpos);
5064 if (NS_FAILED(rv)) {
5065 return rv;
5068 // Keep frameSelection alive.
5069 RefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
5071 const nsFrameSelection::FocusMode focusMode =
5072 (aSelectFlags & SELECT_ACCUMULATE)
5073 ? nsFrameSelection::FocusMode::kMultiRangeSelection
5074 : nsFrameSelection::FocusMode::kCollapseToNewPoint;
5075 rv = frameSelection->HandleClick(
5076 MOZ_KnownLive(startpos.mResultContent) /* bug 1636889 */,
5077 startpos.mContentOffset, startpos.mContentOffset, focusMode,
5078 CARET_ASSOCIATE_AFTER);
5079 if (NS_FAILED(rv)) {
5080 return rv;
5083 rv = frameSelection->HandleClick(
5084 MOZ_KnownLive(endpos.mResultContent) /* bug 1636889 */,
5085 endpos.mContentOffset, endpos.mContentOffset,
5086 nsFrameSelection::FocusMode::kExtendSelection, CARET_ASSOCIATE_BEFORE);
5087 if (NS_FAILED(rv)) {
5088 return rv;
5090 if (aAmountBack == eSelectWord) {
5091 frameSelection->SetIsDoubleClickSelection(true);
5094 // maintain selection
5095 return frameSelection->MaintainSelection(aAmountBack);
5098 NS_IMETHODIMP nsIFrame::HandleDrag(nsPresContext* aPresContext,
5099 WidgetGUIEvent* aEvent,
5100 nsEventStatus* aEventStatus) {
5101 MOZ_ASSERT(aEvent->mClass == eMouseEventClass,
5102 "HandleDrag can only handle mouse event");
5104 NS_ENSURE_ARG_POINTER(aEventStatus);
5106 RefPtr<nsFrameSelection> frameselection = GetFrameSelection();
5107 if (!frameselection) {
5108 return NS_OK;
5111 bool mouseDown = frameselection->GetDragState();
5112 if (!mouseDown) {
5113 return NS_OK;
5116 nsIFrame* scrollbar =
5117 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::Scrollbar);
5118 if (!scrollbar) {
5119 // XXX Do we really need to exclude non-selectable content here?
5120 // GetContentOffsetsFromPoint can handle it just fine, although some
5121 // other stuff might not like it.
5122 // NOTE: DetermineDisplaySelection() returns SELECTION_OFF for
5123 // non-selectable frames.
5124 if (DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF) {
5125 return NS_OK;
5129 frameselection->StopAutoScrollTimer();
5131 // Check if we are dragging in a table cell
5132 nsCOMPtr<nsIContent> parentContent;
5133 int32_t contentOffset;
5134 TableSelectionMode target;
5135 WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
5136 mozilla::PresShell* presShell = aPresContext->PresShell();
5137 nsresult result;
5138 result = GetDataForTableSelection(frameselection, presShell, mouseEvent,
5139 getter_AddRefs(parentContent),
5140 &contentOffset, &target);
5142 AutoWeakFrame weakThis = this;
5143 if (NS_SUCCEEDED(result) && parentContent) {
5144 result = frameselection->HandleTableSelection(parentContent, contentOffset,
5145 target, mouseEvent);
5146 if (NS_WARN_IF(NS_FAILED(result))) {
5147 return result;
5149 } else {
5150 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mouseEvent,
5151 RelativeTo{this});
5152 frameselection->HandleDrag(this, pt);
5155 // The frameselection object notifies selection listeners synchronously above
5156 // which might have killed us.
5157 if (!weakThis.IsAlive()) {
5158 return NS_OK;
5161 // get the nearest scrollframe
5162 nsIScrollableFrame* scrollFrame = nsLayoutUtils::GetNearestScrollableFrame(
5163 this, nsLayoutUtils::SCROLLABLE_SAME_DOC |
5164 nsLayoutUtils::SCROLLABLE_INCLUDE_HIDDEN);
5166 if (scrollFrame) {
5167 nsIFrame* capturingFrame = scrollFrame->GetScrolledFrame();
5168 if (capturingFrame) {
5169 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
5170 mouseEvent, RelativeTo{capturingFrame});
5171 frameselection->StartAutoScrollTimer(capturingFrame, pt, 30);
5175 return NS_OK;
5179 * This static method handles part of the nsIFrame::HandleRelease in a way
5180 * which doesn't rely on the nsFrame object to stay alive.
5182 MOZ_CAN_RUN_SCRIPT_BOUNDARY static nsresult HandleFrameSelection(
5183 nsFrameSelection* aFrameSelection, nsIFrame::ContentOffsets& aOffsets,
5184 bool aHandleTableSel, int32_t aContentOffsetForTableSel,
5185 TableSelectionMode aTargetForTableSel,
5186 nsIContent* aParentContentForTableSel, WidgetGUIEvent* aEvent,
5187 const nsEventStatus* aEventStatus) {
5188 if (!aFrameSelection) {
5189 return NS_OK;
5192 nsresult rv = NS_OK;
5194 if (nsEventStatus_eConsumeNoDefault != *aEventStatus) {
5195 if (!aHandleTableSel) {
5196 if (!aOffsets.content || !aFrameSelection->HasDelayedCaretData()) {
5197 return NS_ERROR_FAILURE;
5200 // We are doing this to simulate what we would have done on HandlePress.
5201 // We didn't do it there to give the user an opportunity to drag
5202 // the text, but since they didn't drag, we want to place the
5203 // caret.
5204 // However, we'll use the mouse position from the release, since:
5205 // * it's easier
5206 // * that's the normal click position to use (although really, in
5207 // the normal case, small movements that don't count as a drag
5208 // can do selection)
5209 aFrameSelection->SetDragState(true);
5211 const nsFrameSelection::FocusMode focusMode =
5212 aFrameSelection->IsShiftDownInDelayedCaretData()
5213 ? nsFrameSelection::FocusMode::kExtendSelection
5214 : nsFrameSelection::FocusMode::kCollapseToNewPoint;
5215 rv = aFrameSelection->HandleClick(
5216 MOZ_KnownLive(aOffsets.content) /* bug 1636889 */,
5217 aOffsets.StartOffset(), aOffsets.EndOffset(), focusMode,
5218 aOffsets.associate);
5219 if (NS_FAILED(rv)) {
5220 return rv;
5222 } else if (aParentContentForTableSel) {
5223 aFrameSelection->SetDragState(false);
5224 rv = aFrameSelection->HandleTableSelection(
5225 aParentContentForTableSel, aContentOffsetForTableSel,
5226 aTargetForTableSel, aEvent->AsMouseEvent());
5227 if (NS_FAILED(rv)) {
5228 return rv;
5231 aFrameSelection->SetDelayedCaretData(0);
5234 aFrameSelection->SetDragState(false);
5235 aFrameSelection->StopAutoScrollTimer();
5237 return NS_OK;
5240 NS_IMETHODIMP nsIFrame::HandleRelease(nsPresContext* aPresContext,
5241 WidgetGUIEvent* aEvent,
5242 nsEventStatus* aEventStatus) {
5243 if (aEvent->mClass != eMouseEventClass) {
5244 return NS_OK;
5247 nsIFrame* activeFrame = GetActiveSelectionFrame(aPresContext, this);
5249 nsCOMPtr<nsIContent> captureContent = PresShell::GetCapturingContent();
5251 bool selectionOff =
5252 (DetermineDisplaySelection() == nsISelectionController::SELECTION_OFF);
5254 RefPtr<nsFrameSelection> frameselection;
5255 ContentOffsets offsets;
5256 nsCOMPtr<nsIContent> parentContent;
5257 int32_t contentOffsetForTableSel = 0;
5258 TableSelectionMode targetForTableSel = TableSelectionMode::None;
5259 bool handleTableSelection = true;
5261 if (!selectionOff) {
5262 frameselection = GetFrameSelection();
5263 if (nsEventStatus_eConsumeNoDefault != *aEventStatus && frameselection) {
5264 // Check if the frameselection recorded the mouse going down.
5265 // If not, the user must have clicked in a part of the selection.
5266 // Place the caret before continuing!
5268 if (frameselection->MouseDownRecorded()) {
5269 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
5270 aEvent, RelativeTo{this});
5271 offsets = GetContentOffsetsFromPoint(pt, SKIP_HIDDEN);
5272 handleTableSelection = false;
5273 } else {
5274 GetDataForTableSelection(frameselection, PresShell(),
5275 aEvent->AsMouseEvent(),
5276 getter_AddRefs(parentContent),
5277 &contentOffsetForTableSel, &targetForTableSel);
5282 // We might be capturing in some other document and the event just happened to
5283 // trickle down here. Make sure that document's frame selection is notified.
5284 // Note, this may cause the current nsFrame object to be deleted, bug 336592.
5285 RefPtr<nsFrameSelection> frameSelection;
5286 if (activeFrame != this && activeFrame->DetermineDisplaySelection() !=
5287 nsISelectionController::SELECTION_OFF) {
5288 frameSelection = activeFrame->GetFrameSelection();
5291 // Also check the selection of the capturing content which might be in a
5292 // different document.
5293 if (!frameSelection && captureContent) {
5294 if (Document* doc = captureContent->GetComposedDoc()) {
5295 mozilla::PresShell* capturingPresShell = doc->GetPresShell();
5296 if (capturingPresShell &&
5297 capturingPresShell != PresContext()->GetPresShell()) {
5298 frameSelection = capturingPresShell->FrameSelection();
5303 if (frameSelection) {
5304 AutoWeakFrame wf(this);
5305 frameSelection->SetDragState(false);
5306 frameSelection->StopAutoScrollTimer();
5307 if (wf.IsAlive()) {
5308 nsIScrollableFrame* scrollFrame =
5309 nsLayoutUtils::GetNearestScrollableFrame(
5310 this, nsLayoutUtils::SCROLLABLE_SAME_DOC |
5311 nsLayoutUtils::SCROLLABLE_INCLUDE_HIDDEN);
5312 if (scrollFrame) {
5313 // Perform any additional scrolling needed to maintain CSS snap point
5314 // requirements when autoscrolling is over.
5315 scrollFrame->ScrollSnap();
5320 // Do not call any methods of the current object after this point!!!
5321 // The object is perhaps dead!
5323 return selectionOff ? NS_OK
5324 : HandleFrameSelection(
5325 frameselection, offsets, handleTableSelection,
5326 contentOffsetForTableSel, targetForTableSel,
5327 parentContent, aEvent, aEventStatus);
5330 struct MOZ_STACK_CLASS FrameContentRange {
5331 FrameContentRange(nsIContent* aContent, int32_t aStart, int32_t aEnd)
5332 : content(aContent), start(aStart), end(aEnd) {}
5333 nsCOMPtr<nsIContent> content;
5334 int32_t start;
5335 int32_t end;
5338 // Retrieve the content offsets of a frame
5339 static FrameContentRange GetRangeForFrame(const nsIFrame* aFrame) {
5340 nsIContent* content = aFrame->GetContent();
5341 if (!content) {
5342 NS_WARNING("Frame has no content");
5343 return FrameContentRange(nullptr, -1, -1);
5346 LayoutFrameType type = aFrame->Type();
5347 if (type == LayoutFrameType::Text) {
5348 auto [offset, offsetEnd] = aFrame->GetOffsets();
5349 return FrameContentRange(content, offset, offsetEnd);
5352 if (type == LayoutFrameType::Br) {
5353 nsIContent* parent = content->GetParent();
5354 const int32_t beginOffset = parent->ComputeIndexOf_Deprecated(content);
5355 return FrameContentRange(parent, beginOffset, beginOffset);
5358 while (content->IsRootOfNativeAnonymousSubtree()) {
5359 content = content->GetParent();
5362 nsIContent* parent = content->GetParent();
5363 if (aFrame->IsBlockOutside() || !parent) {
5364 return FrameContentRange(content, 0, content->GetChildCount());
5367 // TODO(emilio): Revise this in presence of Shadow DOM / display: contents,
5368 // it's likely that we don't want to just walk the light tree, and we need to
5369 // change the representation of FrameContentRange.
5370 const int32_t index = parent->ComputeIndexOf_Deprecated(content);
5371 MOZ_ASSERT(index >= 0);
5372 return FrameContentRange(parent, index, index + 1);
5375 // The FrameTarget represents the closest frame to a point that can be selected
5376 // The frame is the frame represented, frameEdge says whether one end of the
5377 // frame is the result (in which case different handling is needed), and
5378 // afterFrame says which end is represented if frameEdge is true
5379 struct FrameTarget {
5380 explicit operator bool() const { return !!frame; }
5382 nsIFrame* frame = nullptr;
5383 bool frameEdge = false;
5384 bool afterFrame = false;
5387 // See function implementation for information
5388 static FrameTarget GetSelectionClosestFrame(nsIFrame* aFrame,
5389 const nsPoint& aPoint,
5390 uint32_t aFlags);
5392 static bool SelfIsSelectable(nsIFrame* aFrame, uint32_t aFlags) {
5393 if ((aFlags & nsIFrame::SKIP_HIDDEN) &&
5394 !aFrame->StyleVisibility()->IsVisible()) {
5395 return false;
5397 return !aFrame->IsGeneratedContentFrame() &&
5398 aFrame->Style()->UserSelect() != StyleUserSelect::None;
5401 static bool SelectionDescendToKids(nsIFrame* aFrame) {
5402 // If we are only near (not directly over) then don't traverse
5403 // frames with independent selection (e.g. text and list controls, see bug
5404 // 268497). Note that this prevents any of the users of this method from
5405 // entering form controls.
5406 // XXX We might want some way to allow using the up-arrow to go into a form
5407 // control, but the focus didn't work right anyway; it'd probably be enough
5408 // if the left and right arrows could enter textboxes (which I don't believe
5409 // they can at the moment)
5410 if (aFrame->IsTextInputFrame() || aFrame->IsListControlFrame()) {
5411 MOZ_ASSERT(aFrame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION));
5412 return false;
5415 // Failure in this assertion means a new type of frame forms the root of an
5416 // NS_FRAME_INDEPENDENT_SELECTION subtree. In such case, the condition above
5417 // should be changed to handle it.
5418 MOZ_ASSERT_IF(
5419 aFrame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION),
5420 aFrame->GetParent()->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION));
5422 if (aFrame->IsGeneratedContentFrame()) {
5423 return false;
5426 auto style = aFrame->Style()->UserSelect();
5427 return style != StyleUserSelect::All && style != StyleUserSelect::None;
5430 static FrameTarget GetSelectionClosestFrameForChild(nsIFrame* aChild,
5431 const nsPoint& aPoint,
5432 uint32_t aFlags) {
5433 nsIFrame* parent = aChild->GetParent();
5434 if (SelectionDescendToKids(aChild)) {
5435 nsPoint pt = aPoint - aChild->GetOffsetTo(parent);
5436 return GetSelectionClosestFrame(aChild, pt, aFlags);
5438 return FrameTarget{aChild, false, false};
5441 // When the cursor needs to be at the beginning of a block, it shouldn't be
5442 // before the first child. A click on a block whose first child is a block
5443 // should put the cursor in the child. The cursor shouldn't be between the
5444 // blocks, because that's not where it's expected.
5445 // Note that this method is guaranteed to succeed.
5446 static FrameTarget DrillDownToSelectionFrame(nsIFrame* aFrame, bool aEndFrame,
5447 uint32_t aFlags) {
5448 if (SelectionDescendToKids(aFrame)) {
5449 nsIFrame* result = nullptr;
5450 nsIFrame* frame = aFrame->PrincipalChildList().FirstChild();
5451 if (!aEndFrame) {
5452 while (frame && (!SelfIsSelectable(frame, aFlags) || frame->IsEmpty()))
5453 frame = frame->GetNextSibling();
5454 if (frame) result = frame;
5455 } else {
5456 // Because the frame tree is singly linked, to find the last frame,
5457 // we have to iterate through all the frames
5458 // XXX I have a feeling this could be slow for long blocks, although
5459 // I can't find any slowdowns
5460 while (frame) {
5461 if (!frame->IsEmpty() && SelfIsSelectable(frame, aFlags))
5462 result = frame;
5463 frame = frame->GetNextSibling();
5466 if (result) return DrillDownToSelectionFrame(result, aEndFrame, aFlags);
5468 // If the current frame has no targetable children, target the current frame
5469 return FrameTarget{aFrame, true, aEndFrame};
5472 // This method finds the closest valid FrameTarget on a given line; if there is
5473 // no valid FrameTarget on the line, it returns a null FrameTarget
5474 static FrameTarget GetSelectionClosestFrameForLine(
5475 nsBlockFrame* aParent, nsBlockFrame::LineIterator aLine,
5476 const nsPoint& aPoint, uint32_t aFlags) {
5477 // Account for end of lines (any iterator from the block is valid)
5478 if (aLine == aParent->LinesEnd())
5479 return DrillDownToSelectionFrame(aParent, true, aFlags);
5480 nsIFrame* frame = aLine->mFirstChild;
5481 nsIFrame* closestFromIStart = nullptr;
5482 nsIFrame* closestFromIEnd = nullptr;
5483 nscoord closestIStart = aLine->IStart(), closestIEnd = aLine->IEnd();
5484 WritingMode wm = aLine->mWritingMode;
5485 LogicalPoint pt(wm, aPoint, aLine->mContainerSize);
5486 bool canSkipBr = false;
5487 bool lastFrameWasEditable = false;
5488 for (int32_t n = aLine->GetChildCount(); n;
5489 --n, frame = frame->GetNextSibling()) {
5490 // Skip brFrames. Can only skip if the line contains at least
5491 // one selectable and non-empty frame before. Also, avoid skipping brs if
5492 // the previous thing had a different editableness than us, since then we
5493 // may end up not being able to select after it if the br is the last thing
5494 // on the line.
5495 if (!SelfIsSelectable(frame, aFlags) || frame->IsEmpty() ||
5496 (canSkipBr && frame->IsBrFrame() &&
5497 lastFrameWasEditable == frame->GetContent()->IsEditable())) {
5498 continue;
5500 canSkipBr = true;
5501 lastFrameWasEditable =
5502 frame->GetContent() && frame->GetContent()->IsEditable();
5503 LogicalRect frameRect =
5504 LogicalRect(wm, frame->GetRect(), aLine->mContainerSize);
5505 if (pt.I(wm) >= frameRect.IStart(wm)) {
5506 if (pt.I(wm) < frameRect.IEnd(wm)) {
5507 return GetSelectionClosestFrameForChild(frame, aPoint, aFlags);
5509 if (frameRect.IEnd(wm) >= closestIStart) {
5510 closestFromIStart = frame;
5511 closestIStart = frameRect.IEnd(wm);
5513 } else {
5514 if (frameRect.IStart(wm) <= closestIEnd) {
5515 closestFromIEnd = frame;
5516 closestIEnd = frameRect.IStart(wm);
5520 if (!closestFromIStart && !closestFromIEnd) {
5521 // We should only get here if there are no selectable frames on a line
5522 // XXX Do we need more elaborate handling here?
5523 return FrameTarget();
5525 if (closestFromIStart &&
5526 (!closestFromIEnd ||
5527 (abs(pt.I(wm) - closestIStart) <= abs(pt.I(wm) - closestIEnd)))) {
5528 return GetSelectionClosestFrameForChild(closestFromIStart, aPoint, aFlags);
5530 return GetSelectionClosestFrameForChild(closestFromIEnd, aPoint, aFlags);
5533 // This method is for the special handling we do for block frames; they're
5534 // special because they represent paragraphs and because they are organized
5535 // into lines, which have bounds that are not stored elsewhere in the
5536 // frame tree. Returns a null FrameTarget for frames which are not
5537 // blocks or blocks with no lines except editable one.
5538 static FrameTarget GetSelectionClosestFrameForBlock(nsIFrame* aFrame,
5539 const nsPoint& aPoint,
5540 uint32_t aFlags) {
5541 nsBlockFrame* bf = do_QueryFrame(aFrame);
5542 if (!bf) {
5543 return FrameTarget();
5546 // This code searches for the correct line
5547 nsBlockFrame::LineIterator end = bf->LinesEnd();
5548 nsBlockFrame::LineIterator curLine = bf->LinesBegin();
5549 nsBlockFrame::LineIterator closestLine = end;
5551 if (curLine != end) {
5552 // Convert aPoint into a LogicalPoint in the writing-mode of this block
5553 WritingMode wm = curLine->mWritingMode;
5554 LogicalPoint pt(wm, aPoint, curLine->mContainerSize);
5555 do {
5556 // Check to see if our point lies within the line's block-direction bounds
5557 nscoord BCoord = pt.B(wm) - curLine->BStart();
5558 nscoord BSize = curLine->BSize();
5559 if (BCoord >= 0 && BCoord < BSize) {
5560 closestLine = curLine;
5561 break; // We found the line; stop looking
5563 if (BCoord < 0) break;
5564 ++curLine;
5565 } while (curLine != end);
5567 if (closestLine == end) {
5568 nsBlockFrame::LineIterator prevLine = curLine.prev();
5569 nsBlockFrame::LineIterator nextLine = curLine;
5570 // Avoid empty lines
5571 while (nextLine != end && nextLine->IsEmpty()) ++nextLine;
5572 while (prevLine != end && prevLine->IsEmpty()) --prevLine;
5574 // This hidden pref dictates whether a point above or below all lines
5575 // comes up with a line or the beginning or end of the frame; 0 on
5576 // Windows, 1 on other platforms by default at the writing of this code
5577 int32_t dragOutOfFrame =
5578 Preferences::GetInt("browser.drag_out_of_frame_style");
5580 if (prevLine == end) {
5581 if (dragOutOfFrame == 1 || nextLine == end)
5582 return DrillDownToSelectionFrame(aFrame, false, aFlags);
5583 closestLine = nextLine;
5584 } else if (nextLine == end) {
5585 if (dragOutOfFrame == 1)
5586 return DrillDownToSelectionFrame(aFrame, true, aFlags);
5587 closestLine = prevLine;
5588 } else { // Figure out which line is closer
5589 if (pt.B(wm) - prevLine->BEnd() < nextLine->BStart() - pt.B(wm))
5590 closestLine = prevLine;
5591 else
5592 closestLine = nextLine;
5597 do {
5598 if (auto target =
5599 GetSelectionClosestFrameForLine(bf, closestLine, aPoint, aFlags)) {
5600 return target;
5602 ++closestLine;
5603 } while (closestLine != end);
5605 // Fall back to just targeting the last targetable place
5606 return DrillDownToSelectionFrame(aFrame, true, aFlags);
5609 // Use frame edge for grid, flex, table, and non-editable images. Choose the
5610 // edge based on the point position past the frame rect. If past the middle,
5611 // caret should be at end, otherwise at start. This behavior matches Blink.
5613 // TODO(emilio): Can we use this code path for other replaced elements other
5614 // than images? Or even all other frames? We only get there when we didn't find
5615 // selectable children... At least one XUL test fails if we make this apply to
5616 // XUL labels. Also, editable images need _not_ to use the frame edge, see
5617 // below.
5618 static bool UseFrameEdge(nsIFrame* aFrame) {
5619 if (aFrame->IsFlexOrGridContainer() || aFrame->IsTableFrame()) {
5620 return true;
5622 const nsImageFrame* image = do_QueryFrame(aFrame);
5623 if (image && !aFrame->GetContent()->IsEditable()) {
5624 // Editable images are a special-case because editing relies on clicking on
5625 // an editable image selecting it, for it to show resizers.
5626 return true;
5628 return false;
5631 static FrameTarget LastResortFrameTargetForFrame(nsIFrame* aFrame,
5632 const nsPoint& aPoint) {
5633 if (!UseFrameEdge(aFrame)) {
5634 return {aFrame, false, false};
5636 const auto& rect = aFrame->GetRectRelativeToSelf();
5637 nscoord reference;
5638 nscoord middle;
5639 if (aFrame->GetWritingMode().IsVertical()) {
5640 reference = aPoint.y;
5641 middle = rect.Height() / 2;
5642 } else {
5643 reference = aPoint.x;
5644 middle = rect.Width() / 2;
5646 const bool afterFrame = reference > middle;
5647 return {aFrame, true, afterFrame};
5650 // GetSelectionClosestFrame is the helper function that calculates the closest
5651 // frame to the given point.
5652 // It doesn't completely account for offset styles, so needs to be used in
5653 // restricted environments.
5654 // Cannot handle overlapping frames correctly, so it should receive the output
5655 // of GetFrameForPoint
5656 // Guaranteed to return a valid FrameTarget.
5657 // aPoint is relative to aFrame.
5658 static FrameTarget GetSelectionClosestFrame(nsIFrame* aFrame,
5659 const nsPoint& aPoint,
5660 uint32_t aFlags) {
5661 // Handle blocks; if the frame isn't a block, the method fails
5662 if (auto target = GetSelectionClosestFrameForBlock(aFrame, aPoint, aFlags)) {
5663 return target;
5666 if (nsIFrame* kid = aFrame->PrincipalChildList().FirstChild()) {
5667 // Go through all the child frames to find the closest one
5668 nsIFrame::FrameWithDistance closest = {nullptr, nscoord_MAX, nscoord_MAX};
5669 for (; kid; kid = kid->GetNextSibling()) {
5670 if (!SelfIsSelectable(kid, aFlags) || kid->IsEmpty()) continue;
5672 kid->FindCloserFrameForSelection(aPoint, &closest);
5674 if (closest.mFrame) {
5675 if (closest.mFrame->IsInSVGTextSubtree())
5676 return FrameTarget{closest.mFrame, false, false};
5677 return GetSelectionClosestFrameForChild(closest.mFrame, aPoint, aFlags);
5681 return LastResortFrameTargetForFrame(aFrame, aPoint);
5684 static nsIFrame::ContentOffsets OffsetsForSingleFrame(nsIFrame* aFrame,
5685 const nsPoint& aPoint) {
5686 nsIFrame::ContentOffsets offsets;
5687 FrameContentRange range = GetRangeForFrame(aFrame);
5688 offsets.content = range.content;
5689 // If there are continuations (meaning it's not one rectangle), this is the
5690 // best this function can do
5691 if (aFrame->GetNextContinuation() || aFrame->GetPrevContinuation()) {
5692 offsets.offset = range.start;
5693 offsets.secondaryOffset = range.end;
5694 offsets.associate = CARET_ASSOCIATE_AFTER;
5695 return offsets;
5698 // Figure out whether the offsets should be over, after, or before the frame
5699 nsRect rect(nsPoint(0, 0), aFrame->GetSize());
5701 bool isBlock = !aFrame->StyleDisplay()->IsInlineFlow();
5702 bool isRtl = (aFrame->StyleVisibility()->mDirection == StyleDirection::Rtl);
5703 if ((isBlock && rect.y < aPoint.y) ||
5704 (!isBlock && ((isRtl && rect.x + rect.width / 2 > aPoint.x) ||
5705 (!isRtl && rect.x + rect.width / 2 < aPoint.x)))) {
5706 offsets.offset = range.end;
5707 if (rect.Contains(aPoint))
5708 offsets.secondaryOffset = range.start;
5709 else
5710 offsets.secondaryOffset = range.end;
5711 } else {
5712 offsets.offset = range.start;
5713 if (rect.Contains(aPoint))
5714 offsets.secondaryOffset = range.end;
5715 else
5716 offsets.secondaryOffset = range.start;
5718 offsets.associate = offsets.offset == range.start ? CARET_ASSOCIATE_AFTER
5719 : CARET_ASSOCIATE_BEFORE;
5720 return offsets;
5723 static nsIFrame* AdjustFrameForSelectionStyles(nsIFrame* aFrame) {
5724 nsIFrame* adjustedFrame = aFrame;
5725 for (nsIFrame* frame = aFrame; frame; frame = frame->GetParent()) {
5726 // These are the conditions that make all children not able to handle
5727 // a cursor.
5728 auto userSelect = frame->Style()->UserSelect();
5729 if (userSelect != StyleUserSelect::Auto &&
5730 userSelect != StyleUserSelect::All) {
5731 break;
5733 if (userSelect == StyleUserSelect::All ||
5734 frame->IsGeneratedContentFrame()) {
5735 adjustedFrame = frame;
5738 return adjustedFrame;
5741 nsIFrame::ContentOffsets nsIFrame::GetContentOffsetsFromPoint(
5742 const nsPoint& aPoint, uint32_t aFlags) {
5743 nsIFrame* adjustedFrame;
5744 if (aFlags & IGNORE_SELECTION_STYLE) {
5745 adjustedFrame = this;
5746 } else {
5747 // This section of code deals with special selection styles. Note that
5748 // -moz-all exists, even though it doesn't need to be explicitly handled.
5750 // The offset is forced not to end up in generated content; content offsets
5751 // cannot represent content outside of the document's content tree.
5753 adjustedFrame = AdjustFrameForSelectionStyles(this);
5755 // `user-select: all` needs special handling, because clicking on it should
5756 // lead to the whole frame being selected.
5757 if (adjustedFrame->Style()->UserSelect() == StyleUserSelect::All) {
5758 nsPoint adjustedPoint = aPoint + GetOffsetTo(adjustedFrame);
5759 return OffsetsForSingleFrame(adjustedFrame, adjustedPoint);
5762 // For other cases, try to find a closest frame starting from the parent of
5763 // the unselectable frame
5764 if (adjustedFrame != this) {
5765 adjustedFrame = adjustedFrame->GetParent();
5769 nsPoint adjustedPoint = aPoint + GetOffsetTo(adjustedFrame);
5771 FrameTarget closest =
5772 GetSelectionClosestFrame(adjustedFrame, adjustedPoint, aFlags);
5774 // If the correct offset is at one end of a frame, use offset-based
5775 // calculation method
5776 if (closest.frameEdge) {
5777 ContentOffsets offsets;
5778 FrameContentRange range = GetRangeForFrame(closest.frame);
5779 offsets.content = range.content;
5780 if (closest.afterFrame)
5781 offsets.offset = range.end;
5782 else
5783 offsets.offset = range.start;
5784 offsets.secondaryOffset = offsets.offset;
5785 offsets.associate = offsets.offset == range.start ? CARET_ASSOCIATE_AFTER
5786 : CARET_ASSOCIATE_BEFORE;
5787 return offsets;
5790 nsPoint pt;
5791 if (closest.frame != this) {
5792 if (closest.frame->IsInSVGTextSubtree()) {
5793 pt = nsLayoutUtils::TransformAncestorPointToFrame(
5794 RelativeTo{closest.frame}, aPoint, RelativeTo{this});
5795 } else {
5796 pt = aPoint - closest.frame->GetOffsetTo(this);
5798 } else {
5799 pt = aPoint;
5801 return closest.frame->CalcContentOffsetsFromFramePoint(pt);
5803 // XXX should I add some kind of offset standardization?
5804 // consider <b>xxxxx</b><i>zzzzz</i>; should any click between the last
5805 // x and first z put the cursor in the same logical position in addition
5806 // to the same visual position?
5809 nsIFrame::ContentOffsets nsIFrame::CalcContentOffsetsFromFramePoint(
5810 const nsPoint& aPoint) {
5811 return OffsetsForSingleFrame(this, aPoint);
5814 bool nsIFrame::AssociateImage(const StyleImage& aImage) {
5815 imgRequestProxy* req = aImage.GetImageRequest();
5816 if (!req) {
5817 return false;
5820 mozilla::css::ImageLoader* loader =
5821 PresContext()->Document()->StyleImageLoader();
5823 loader->AssociateRequestToFrame(req, this);
5824 return true;
5827 void nsIFrame::DisassociateImage(const StyleImage& aImage) {
5828 imgRequestProxy* req = aImage.GetImageRequest();
5829 if (!req) {
5830 return;
5833 mozilla::css::ImageLoader* loader =
5834 PresContext()->Document()->StyleImageLoader();
5836 loader->DisassociateRequestFromFrame(req, this);
5839 StyleImageRendering nsIFrame::UsedImageRendering() const {
5840 ComputedStyle* style;
5841 if (IsCanvasFrame()) {
5842 // XXXdholbert Maybe we should use FindCanvasBackground here (instead of
5843 // FindBackground), since we're inside an IsCanvasFrame check? Though then
5844 // we'd also have to copypaste or abstract-away the multi-part root-frame
5845 // lookup that the canvas-flavored API requires.
5846 style = nsCSSRendering::FindBackground(this);
5847 } else {
5848 style = Style();
5850 return style->StyleVisibility()->mImageRendering;
5853 // The touch-action CSS property applies to: all elements except: non-replaced
5854 // inline elements, table rows, row groups, table columns, and column groups.
5855 StyleTouchAction nsIFrame::UsedTouchAction() const {
5856 if (IsFrameOfType(eLineParticipant)) {
5857 return StyleTouchAction::AUTO;
5859 auto& disp = *StyleDisplay();
5860 if (disp.IsInternalTableStyleExceptCell()) {
5861 return StyleTouchAction::AUTO;
5863 return disp.mTouchAction;
5866 Maybe<nsIFrame::Cursor> nsIFrame::GetCursor(const nsPoint&) {
5867 StyleCursorKind kind = StyleUI()->Cursor().keyword;
5868 if (kind == StyleCursorKind::Auto) {
5869 // If this is editable, I-beam cursor is better for most elements.
5870 kind = (mContent && mContent->IsEditable()) ? StyleCursorKind::Text
5871 : StyleCursorKind::Default;
5873 if (kind == StyleCursorKind::Text && GetWritingMode().IsVertical()) {
5874 // Per CSS UI spec, UA may treat value 'text' as
5875 // 'vertical-text' for vertical text.
5876 kind = StyleCursorKind::VerticalText;
5879 return Some(Cursor{kind, AllowCustomCursorImage::Yes});
5882 // Resize and incremental reflow
5884 /* virtual */
5885 void nsIFrame::MarkIntrinsicISizesDirty() {
5886 // If we're a flex item, clear our flex-item-specific cached measurements
5887 // (which likely depended on our now-stale intrinsic isize).
5888 if (IsFlexItem()) {
5889 nsFlexContainerFrame::MarkCachedFlexMeasurementsDirty(this);
5892 if (HasAnyStateBits(NS_FRAME_FONT_INFLATION_FLOW_ROOT)) {
5893 nsFontInflationData::MarkFontInflationDataTextDirty(this);
5896 RemoveProperty(nsGridContainerFrame::CachedBAxisMeasurement::Prop());
5899 void nsIFrame::MarkSubtreeDirty() {
5900 if (HasAnyStateBits(NS_FRAME_IS_DIRTY)) {
5901 return;
5903 // Unconditionally mark given frame dirty.
5904 AddStateBits(NS_FRAME_IS_DIRTY);
5906 // Mark all descendants dirty, unless:
5907 // - Already dirty.
5908 // - TableColGroup
5909 // - XULBox
5910 AutoTArray<nsIFrame*, 32> stack;
5911 for (const auto& childLists : ChildLists()) {
5912 for (nsIFrame* kid : childLists.mList) {
5913 stack.AppendElement(kid);
5916 while (!stack.IsEmpty()) {
5917 nsIFrame* f = stack.PopLastElement();
5918 if (f->HasAnyStateBits(NS_FRAME_IS_DIRTY) || f->IsTableColGroupFrame()) {
5919 continue;
5922 f->AddStateBits(NS_FRAME_IS_DIRTY);
5924 for (const auto& childLists : f->ChildLists()) {
5925 for (nsIFrame* kid : childLists.mList) {
5926 stack.AppendElement(kid);
5932 /* virtual */
5933 nscoord nsIFrame::GetMinISize(gfxContext* aRenderingContext) {
5934 nscoord result = 0;
5935 DISPLAY_MIN_INLINE_SIZE(this, result);
5936 return result;
5939 /* virtual */
5940 nscoord nsIFrame::GetPrefISize(gfxContext* aRenderingContext) {
5941 nscoord result = 0;
5942 DISPLAY_PREF_INLINE_SIZE(this, result);
5943 return result;
5946 /* virtual */
5947 void nsIFrame::AddInlineMinISize(gfxContext* aRenderingContext,
5948 nsIFrame::InlineMinISizeData* aData) {
5949 nscoord isize = nsLayoutUtils::IntrinsicForContainer(
5950 aRenderingContext, this, IntrinsicISizeType::MinISize);
5951 aData->DefaultAddInlineMinISize(this, isize);
5954 /* virtual */
5955 void nsIFrame::AddInlinePrefISize(gfxContext* aRenderingContext,
5956 nsIFrame::InlinePrefISizeData* aData) {
5957 nscoord isize = nsLayoutUtils::IntrinsicForContainer(
5958 aRenderingContext, this, IntrinsicISizeType::PrefISize);
5959 aData->DefaultAddInlinePrefISize(isize);
5962 void nsIFrame::InlineMinISizeData::DefaultAddInlineMinISize(nsIFrame* aFrame,
5963 nscoord aISize,
5964 bool aAllowBreak) {
5965 auto parent = aFrame->GetParent();
5966 MOZ_ASSERT(parent, "Must have a parent if we get here!");
5967 const bool mayBreak = aAllowBreak && !aFrame->CanContinueTextRun() &&
5968 !parent->Style()->ShouldSuppressLineBreak() &&
5969 parent->StyleText()->WhiteSpaceCanWrap(parent);
5970 if (mayBreak) {
5971 OptionallyBreak();
5973 mTrailingWhitespace = 0;
5974 mSkipWhitespace = false;
5975 mCurrentLine += aISize;
5976 mAtStartOfLine = false;
5977 if (mayBreak) {
5978 OptionallyBreak();
5982 void nsIFrame::InlinePrefISizeData::DefaultAddInlinePrefISize(nscoord aISize) {
5983 mCurrentLine = NSCoordSaturatingAdd(mCurrentLine, aISize);
5984 mTrailingWhitespace = 0;
5985 mSkipWhitespace = false;
5986 mLineIsEmpty = false;
5989 void nsIFrame::InlineMinISizeData::ForceBreak() {
5990 mCurrentLine -= mTrailingWhitespace;
5991 mPrevLines = std::max(mPrevLines, mCurrentLine);
5992 mCurrentLine = mTrailingWhitespace = 0;
5994 for (uint32_t i = 0, i_end = mFloats.Length(); i != i_end; ++i) {
5995 nscoord float_min = mFloats[i].Width();
5996 if (float_min > mPrevLines) mPrevLines = float_min;
5998 mFloats.Clear();
5999 mSkipWhitespace = true;
6002 void nsIFrame::InlineMinISizeData::OptionallyBreak(nscoord aHyphenWidth) {
6003 // If we can fit more content into a smaller width by staying on this
6004 // line (because we're still at a negative offset due to negative
6005 // text-indent or negative margin), don't break. Otherwise, do the
6006 // same as ForceBreak. it doesn't really matter when we accumulate
6007 // floats.
6008 if (mCurrentLine + aHyphenWidth < 0 || mAtStartOfLine) return;
6009 mCurrentLine += aHyphenWidth;
6010 ForceBreak();
6013 void nsIFrame::InlinePrefISizeData::ForceBreak(StyleClear aClearType) {
6014 // If this force break is not clearing any float, we can leave all the
6015 // floats to the next force break.
6016 if (!mFloats.IsEmpty() && aClearType != StyleClear::None) {
6017 // preferred widths accumulated for floats that have already
6018 // been cleared past
6019 nscoord floats_done = 0,
6020 // preferred widths accumulated for floats that have not yet
6021 // been cleared past
6022 floats_cur_left = 0, floats_cur_right = 0;
6024 for (const FloatInfo& floatInfo : mFloats) {
6025 const nsStyleDisplay* floatDisp = floatInfo.Frame()->StyleDisplay();
6026 StyleClear clearType = floatDisp->mClear;
6027 if (clearType == StyleClear::Left || clearType == StyleClear::Right ||
6028 clearType == StyleClear::Both) {
6029 nscoord floats_cur =
6030 NSCoordSaturatingAdd(floats_cur_left, floats_cur_right);
6031 if (floats_cur > floats_done) {
6032 floats_done = floats_cur;
6034 if (clearType != StyleClear::Right) {
6035 floats_cur_left = 0;
6037 if (clearType != StyleClear::Left) {
6038 floats_cur_right = 0;
6042 StyleFloat floatStyle = floatDisp->mFloat;
6043 nscoord& floats_cur =
6044 floatStyle == StyleFloat::Left ? floats_cur_left : floats_cur_right;
6045 nscoord floatWidth = floatInfo.Width();
6046 // Negative-width floats don't change the available space so they
6047 // shouldn't change our intrinsic line width either.
6048 floats_cur = NSCoordSaturatingAdd(floats_cur, std::max(0, floatWidth));
6051 nscoord floats_cur =
6052 NSCoordSaturatingAdd(floats_cur_left, floats_cur_right);
6053 if (floats_cur > floats_done) floats_done = floats_cur;
6055 mCurrentLine = NSCoordSaturatingAdd(mCurrentLine, floats_done);
6057 if (aClearType == StyleClear::Both) {
6058 mFloats.Clear();
6059 } else {
6060 // If the break type does not clear all floats, it means there may
6061 // be some floats whose isize should contribute to the intrinsic
6062 // isize of the next line. The code here scans the current mFloats
6063 // and keeps floats which are not cleared by this break. Note that
6064 // floats may be cleared directly or indirectly. See below.
6065 nsTArray<FloatInfo> newFloats;
6066 MOZ_ASSERT(
6067 aClearType == StyleClear::Left || aClearType == StyleClear::Right,
6068 "Other values should have been handled in other branches");
6069 StyleFloat clearFloatType =
6070 aClearType == StyleClear::Left ? StyleFloat::Left : StyleFloat::Right;
6071 // Iterate the array in reverse so that we can stop when there are
6072 // no longer any floats we need to keep. See below.
6073 for (FloatInfo& floatInfo : Reversed(mFloats)) {
6074 const nsStyleDisplay* floatDisp = floatInfo.Frame()->StyleDisplay();
6075 if (floatDisp->mFloat != clearFloatType) {
6076 newFloats.AppendElement(floatInfo);
6077 } else {
6078 // This is a float on the side that this break directly clears
6079 // which means we're not keeping it in mFloats. However, if
6080 // this float clears floats on the opposite side (via a value
6081 // of either 'both' or one of 'left'/'right'), any remaining
6082 // (earlier) floats on that side would be indirectly cleared
6083 // as well. Thus, we should break out of this loop and stop
6084 // considering earlier floats to be kept in mFloats.
6085 StyleClear clearType = floatDisp->mClear;
6086 if (clearType != aClearType && clearType != StyleClear::None) {
6087 break;
6091 newFloats.Reverse();
6092 mFloats = std::move(newFloats);
6096 mCurrentLine =
6097 NSCoordSaturatingSubtract(mCurrentLine, mTrailingWhitespace, nscoord_MAX);
6098 mPrevLines = std::max(mPrevLines, mCurrentLine);
6099 mCurrentLine = mTrailingWhitespace = 0;
6100 mSkipWhitespace = true;
6101 mLineIsEmpty = true;
6104 static nscoord ResolveMargin(const LengthPercentageOrAuto& aStyle,
6105 nscoord aPercentageBasis) {
6106 if (aStyle.IsAuto()) {
6107 return nscoord(0);
6109 return nsLayoutUtils::ResolveToLength<false>(aStyle.AsLengthPercentage(),
6110 aPercentageBasis);
6113 static nscoord ResolvePadding(const LengthPercentage& aStyle,
6114 nscoord aPercentageBasis) {
6115 return nsLayoutUtils::ResolveToLength<true>(aStyle, aPercentageBasis);
6118 static nsIFrame::IntrinsicSizeOffsetData IntrinsicSizeOffsets(
6119 nsIFrame* aFrame, nscoord aPercentageBasis, bool aForISize) {
6120 nsIFrame::IntrinsicSizeOffsetData result;
6121 WritingMode wm = aFrame->GetWritingMode();
6122 const auto& margin = aFrame->StyleMargin()->mMargin;
6123 bool verticalAxis = aForISize == wm.IsVertical();
6124 if (verticalAxis) {
6125 result.margin += ResolveMargin(margin.Get(eSideTop), aPercentageBasis);
6126 result.margin += ResolveMargin(margin.Get(eSideBottom), aPercentageBasis);
6127 } else {
6128 result.margin += ResolveMargin(margin.Get(eSideLeft), aPercentageBasis);
6129 result.margin += ResolveMargin(margin.Get(eSideRight), aPercentageBasis);
6132 const auto& padding = aFrame->StylePadding()->mPadding;
6133 if (verticalAxis) {
6134 result.padding += ResolvePadding(padding.Get(eSideTop), aPercentageBasis);
6135 result.padding +=
6136 ResolvePadding(padding.Get(eSideBottom), aPercentageBasis);
6137 } else {
6138 result.padding += ResolvePadding(padding.Get(eSideLeft), aPercentageBasis);
6139 result.padding += ResolvePadding(padding.Get(eSideRight), aPercentageBasis);
6142 const nsStyleBorder* styleBorder = aFrame->StyleBorder();
6143 if (verticalAxis) {
6144 result.border += styleBorder->GetComputedBorderWidth(eSideTop);
6145 result.border += styleBorder->GetComputedBorderWidth(eSideBottom);
6146 } else {
6147 result.border += styleBorder->GetComputedBorderWidth(eSideLeft);
6148 result.border += styleBorder->GetComputedBorderWidth(eSideRight);
6151 const nsStyleDisplay* disp = aFrame->StyleDisplay();
6152 if (aFrame->IsThemed(disp)) {
6153 nsPresContext* presContext = aFrame->PresContext();
6155 LayoutDeviceIntMargin border = presContext->Theme()->GetWidgetBorder(
6156 presContext->DeviceContext(), aFrame, disp->EffectiveAppearance());
6157 result.border = presContext->DevPixelsToAppUnits(
6158 verticalAxis ? border.TopBottom() : border.LeftRight());
6160 LayoutDeviceIntMargin padding;
6161 if (presContext->Theme()->GetWidgetPadding(
6162 presContext->DeviceContext(), aFrame, disp->EffectiveAppearance(),
6163 &padding)) {
6164 result.padding = presContext->DevPixelsToAppUnits(
6165 verticalAxis ? padding.TopBottom() : padding.LeftRight());
6168 return result;
6171 /* virtual */ nsIFrame::IntrinsicSizeOffsetData nsIFrame::IntrinsicISizeOffsets(
6172 nscoord aPercentageBasis) {
6173 return IntrinsicSizeOffsets(this, aPercentageBasis, true);
6176 nsIFrame::IntrinsicSizeOffsetData nsIFrame::IntrinsicBSizeOffsets(
6177 nscoord aPercentageBasis) {
6178 return IntrinsicSizeOffsets(this, aPercentageBasis, false);
6181 /* virtual */
6182 IntrinsicSize nsIFrame::GetIntrinsicSize() {
6183 return IntrinsicSize(); // default is width/height set to eStyleUnit_None
6186 AspectRatio nsIFrame::GetAspectRatio() const {
6187 // Per spec, 'aspect-ratio' property applies to all elements except inline
6188 // boxes and internal ruby or table boxes.
6189 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio
6190 // For those frame types that don't support aspect-ratio, they must not have
6191 // the natural ratio, so this early return is fine.
6192 if (!IsFrameOfType(eSupportsAspectRatio)) {
6193 return AspectRatio();
6196 const StyleAspectRatio& aspectRatio = StylePosition()->mAspectRatio;
6197 // If aspect-ratio is zero or infinite, it's a degenerate ratio and behaves
6198 // as auto.
6199 // https://drafts.csswg.org/css-sizing-4/#valdef-aspect-ratio-ratio
6200 if (!aspectRatio.BehavesAsAuto()) {
6201 // Non-auto. Return the preferred aspect ratio from the aspect-ratio style.
6202 return aspectRatio.ratio.AsRatio().ToLayoutRatio(UseBoxSizing::Yes);
6205 // The rest of the cases are when aspect-ratio has 'auto'.
6206 if (auto intrinsicRatio = GetIntrinsicRatio()) {
6207 return intrinsicRatio;
6210 if (aspectRatio.HasRatio()) {
6211 // If it's a degenerate ratio, this returns 0. Just the same as the auto
6212 // case.
6213 return aspectRatio.ratio.AsRatio().ToLayoutRatio(UseBoxSizing::No);
6216 return AspectRatio();
6219 /* virtual */
6220 AspectRatio nsIFrame::GetIntrinsicRatio() const { return AspectRatio(); }
6222 static bool ShouldApplyAutomaticMinimumOnInlineAxis(
6223 WritingMode aWM, const nsStyleDisplay* aDisplay,
6224 const nsStylePosition* aPosition) {
6225 // Apply the automatic minimum size for aspect ratio:
6226 // Note: The replaced elements shouldn't be here, so we only check the scroll
6227 // container.
6228 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
6229 return !aDisplay->IsScrollableOverflow() && aPosition->MinISize(aWM).IsAuto();
6232 struct MinMaxSize {
6233 nscoord mMinSize = 0;
6234 nscoord mMaxSize = NS_UNCONSTRAINEDSIZE;
6236 nscoord ClampSizeToMinAndMax(nscoord aSize) const {
6237 return NS_CSS_MINMAX(aSize, mMinSize, mMaxSize);
6240 static MinMaxSize ComputeTransferredMinMaxInlineSize(
6241 const WritingMode aWM, const AspectRatio& aAspectRatio,
6242 const MinMaxSize& aMinMaxBSize, const LogicalSize& aBoxSizingAdjustment) {
6243 // Note: the spec mentions that
6244 // 1. This transferred minimum is capped by any definite preferred or maximum
6245 // size in the destination axis.
6246 // 2. This transferred maximum is floored by any definite preferred or minimum
6247 // size in the destination axis
6249 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio
6251 // The spec requires us to clamp these by the specified size (it calls it the
6252 // preferred size). However, we actually don't need to worry about that,
6253 // because we only use this if the inline size is indefinite.
6255 // We do not need to clamp the transferred minimum and maximum as long as we
6256 // always apply the transferred min/max size before the explicit min/max size,
6257 // the result will be identical.
6259 MinMaxSize transferredISize;
6261 if (aMinMaxBSize.mMinSize > 0) {
6262 transferredISize.mMinSize = aAspectRatio.ComputeRatioDependentSize(
6263 LogicalAxis::eLogicalAxisInline, aWM, aMinMaxBSize.mMinSize,
6264 aBoxSizingAdjustment);
6267 if (aMinMaxBSize.mMaxSize != NS_UNCONSTRAINEDSIZE) {
6268 transferredISize.mMaxSize = aAspectRatio.ComputeRatioDependentSize(
6269 LogicalAxis::eLogicalAxisInline, aWM, aMinMaxBSize.mMaxSize,
6270 aBoxSizingAdjustment);
6273 // Minimum size wins over maximum size.
6274 transferredISize.mMaxSize =
6275 std::max(transferredISize.mMinSize, transferredISize.mMaxSize);
6276 return transferredISize;
6279 /* virtual */
6280 nsIFrame::SizeComputationResult nsIFrame::ComputeSize(
6281 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
6282 nscoord aAvailableISize, const LogicalSize& aMargin,
6283 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
6284 ComputeSizeFlags aFlags) {
6285 MOZ_ASSERT(!GetIntrinsicRatio(),
6286 "Please override this method and call "
6287 "nsContainerFrame::ComputeSizeWithIntrinsicDimensions instead.");
6288 LogicalSize result =
6289 ComputeAutoSize(aRenderingContext, aWM, aCBSize, aAvailableISize, aMargin,
6290 aBorderPadding, aSizeOverrides, aFlags);
6291 const nsStylePosition* stylePos = StylePosition();
6292 const nsStyleDisplay* disp = StyleDisplay();
6293 auto aspectRatioUsage = AspectRatioUsage::None;
6295 const auto boxSizingAdjust = stylePos->mBoxSizing == StyleBoxSizing::Border
6296 ? aBorderPadding
6297 : LogicalSize(aWM);
6298 nscoord boxSizingToMarginEdgeISize = aMargin.ISize(aWM) +
6299 aBorderPadding.ISize(aWM) -
6300 boxSizingAdjust.ISize(aWM);
6302 const auto& styleISize = aSizeOverrides.mStyleISize
6303 ? *aSizeOverrides.mStyleISize
6304 : stylePos->ISize(aWM);
6305 const auto& styleBSize = aSizeOverrides.mStyleBSize
6306 ? *aSizeOverrides.mStyleBSize
6307 : stylePos->BSize(aWM);
6308 const auto& aspectRatio = aSizeOverrides.mAspectRatio
6309 ? *aSizeOverrides.mAspectRatio
6310 : GetAspectRatio();
6312 auto parentFrame = GetParent();
6313 auto alignCB = parentFrame;
6314 bool isGridItem = IsGridItem();
6315 if (parentFrame && parentFrame->IsTableWrapperFrame() && IsTableFrame()) {
6316 // An inner table frame is sized as a grid item if its table wrapper is,
6317 // because they actually have the same CB (the wrapper's CB).
6318 // @see ReflowInput::InitCBReflowInput
6319 auto tableWrapper = GetParent();
6320 auto grandParent = tableWrapper->GetParent();
6321 isGridItem = grandParent->IsGridContainerFrame() &&
6322 !tableWrapper->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW);
6323 if (isGridItem) {
6324 // When resolving justify/align-self below, we want to use the grid
6325 // container's justify/align-items value and WritingMode.
6326 alignCB = grandParent;
6329 const bool isFlexItem =
6330 IsFlexItem() && !parentFrame->HasAnyStateBits(
6331 NS_STATE_FLEX_IS_EMULATING_LEGACY_WEBKIT_BOX);
6332 // This variable only gets set (and used) if isFlexItem is true. It
6333 // indicates which axis (in this frame's own WM) corresponds to its
6334 // flex container's main axis.
6335 LogicalAxis flexMainAxis =
6336 eLogicalAxisInline; // (init to make valgrind happy)
6337 if (isFlexItem) {
6338 flexMainAxis = nsFlexContainerFrame::IsItemInlineAxisMainAxis(this)
6339 ? eLogicalAxisInline
6340 : eLogicalAxisBlock;
6343 const bool isOrthogonal = aWM.IsOrthogonalTo(alignCB->GetWritingMode());
6344 const bool isAutoISize = styleISize.IsAuto();
6345 const bool isAutoBSize =
6346 nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM));
6347 // Compute inline-axis size
6348 if (!isAutoISize) {
6349 auto iSizeResult = ComputeISizeValue(
6350 aRenderingContext, aWM, aCBSize, boxSizingAdjust,
6351 boxSizingToMarginEdgeISize, styleISize, aSizeOverrides, aFlags);
6352 result.ISize(aWM) = iSizeResult.mISize;
6353 aspectRatioUsage = iSizeResult.mAspectRatioUsage;
6354 } else if (MOZ_UNLIKELY(isGridItem) && !IsTrueOverflowContainer()) {
6355 // 'auto' inline-size for grid-level box - fill the CB for 'stretch' /
6356 // 'normal' and clamp it to the CB if requested:
6357 bool stretch = false;
6358 bool mayUseAspectRatio = aspectRatio && !isAutoBSize;
6359 if (!aFlags.contains(ComputeSizeFlag::ShrinkWrap) &&
6360 !StyleMargin()->HasInlineAxisAuto(aWM) &&
6361 !alignCB->IsMasonry(isOrthogonal ? eLogicalAxisBlock
6362 : eLogicalAxisInline)) {
6363 auto inlineAxisAlignment =
6364 isOrthogonal ? StylePosition()->UsedAlignSelf(alignCB->Style())._0
6365 : StylePosition()->UsedJustifySelf(alignCB->Style())._0;
6366 stretch = inlineAxisAlignment == StyleAlignFlags::STRETCH ||
6367 (inlineAxisAlignment == StyleAlignFlags::NORMAL &&
6368 !mayUseAspectRatio);
6371 // Apply the preferred aspect ratio for alignments other than *stretch* and
6372 // *normal without aspect ratio*.
6373 // The spec says all other values should size the items as fit-content, and
6374 // the intrinsic size should respect the preferred aspect ratio, so we also
6375 // apply aspect ratio for all other values.
6376 // https://drafts.csswg.org/css-grid/#grid-item-sizing
6377 if (!stretch && mayUseAspectRatio) {
6378 // Note: we don't need to handle aspect ratio for inline axis if both
6379 // width/height are auto. The default ratio-dependent axis is block axis
6380 // in this case, so we can simply get the block size from the non-auto
6381 // |styleBSize|.
6382 auto bSize = nsLayoutUtils::ComputeBSizeValue(
6383 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6384 styleBSize.AsLengthPercentage());
6385 result.ISize(aWM) = aspectRatio.ComputeRatioDependentSize(
6386 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
6387 aspectRatioUsage = AspectRatioUsage::ToComputeISize;
6390 if (stretch || aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
6391 auto iSizeToFillCB =
6392 std::max(nscoord(0), aCBSize.ISize(aWM) - aBorderPadding.ISize(aWM) -
6393 aMargin.ISize(aWM));
6394 if (stretch || result.ISize(aWM) > iSizeToFillCB) {
6395 result.ISize(aWM) = iSizeToFillCB;
6398 } else if (aspectRatio && !isAutoBSize) {
6399 auto bSize = nsLayoutUtils::ComputeBSizeValue(
6400 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6401 styleBSize.AsLengthPercentage());
6402 result.ISize(aWM) = aspectRatio.ComputeRatioDependentSize(
6403 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
6404 aspectRatioUsage = AspectRatioUsage::ToComputeISize;
6407 // Calculate and apply transferred min & max size contraints.
6408 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-size-transfers
6410 // Note: The basic principle is that sizing constraints transfer through the
6411 // aspect-ratio to the other side to preserve the aspect ratio to the extent
6412 // that they can without violating any sizes specified explicitly on that
6413 // affected axis.
6415 // FIXME: The spec words may not be correct, so we may have to update this
6416 // tentative solution once this spec issue gets resolved. Here, we clamp the
6417 // flex base size by the transferred min and max sizes, and don't include
6418 // the transferred min & max sizes into its used min & max sizes. So this
6419 // lets us match other browsers' current behaviors.
6420 // https://github.com/w3c/csswg-drafts/issues/6071
6422 // Note: This may make more sense if we clamp the flex base size in
6423 // FlexItem::ResolveFlexBaseSizeFromAspectRatio(). However, the result should
6424 // be identical. FlexItem::ResolveFlexBaseSizeFromAspectRatio() only handles
6425 // the case of the definite cross size, and the definite cross size is clamped
6426 // by the min & max cross sizes below in this function. This means its flex
6427 // base size has been clamped by the transferred min & max size already after
6428 // generating the flex items. So here we make the code more general for both
6429 // definite cross size and indefinite cross size.
6430 const bool isDefiniteISize = styleISize.IsLengthPercentage();
6431 const auto& minBSizeCoord = stylePos->MinBSize(aWM);
6432 const auto& maxBSizeCoord = stylePos->MaxBSize(aWM);
6433 const bool isAutoMinBSize =
6434 nsLayoutUtils::IsAutoBSize(minBSizeCoord, aCBSize.BSize(aWM));
6435 const bool isAutoMaxBSize =
6436 nsLayoutUtils::IsAutoBSize(maxBSizeCoord, aCBSize.BSize(aWM));
6437 if (aspectRatio && !isDefiniteISize) {
6438 const MinMaxSize minMaxBSize{
6439 isAutoMinBSize ? 0
6440 : nsLayoutUtils::ComputeBSizeValue(
6441 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6442 minBSizeCoord.AsLengthPercentage()),
6443 isAutoMaxBSize ? NS_UNCONSTRAINEDSIZE
6444 : nsLayoutUtils::ComputeBSizeValue(
6445 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6446 maxBSizeCoord.AsLengthPercentage())};
6447 MinMaxSize transferredMinMaxISize = ComputeTransferredMinMaxInlineSize(
6448 aWM, aspectRatio, minMaxBSize, boxSizingAdjust);
6450 result.ISize(aWM) =
6451 transferredMinMaxISize.ClampSizeToMinAndMax(result.ISize(aWM));
6454 // Flex items ignore their min & max sizing properties in their
6455 // flex container's main-axis. (Those properties get applied later in
6456 // the flexbox algorithm.)
6457 const bool isFlexItemInlineAxisMainAxis =
6458 isFlexItem && flexMainAxis == eLogicalAxisInline;
6459 const auto& maxISizeCoord = stylePos->MaxISize(aWM);
6460 nscoord maxISize = NS_UNCONSTRAINEDSIZE;
6461 if (!maxISizeCoord.IsNone() && !isFlexItemInlineAxisMainAxis) {
6462 maxISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
6463 boxSizingAdjust, boxSizingToMarginEdgeISize,
6464 maxISizeCoord, aSizeOverrides, aFlags)
6465 .mISize;
6466 result.ISize(aWM) = std::min(maxISize, result.ISize(aWM));
6469 const auto& minISizeCoord = stylePos->MinISize(aWM);
6470 nscoord minISize;
6471 if (!minISizeCoord.IsAuto() && !isFlexItemInlineAxisMainAxis) {
6472 minISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
6473 boxSizingAdjust, boxSizingToMarginEdgeISize,
6474 minISizeCoord, aSizeOverrides, aFlags)
6475 .mISize;
6476 } else if (MOZ_UNLIKELY(
6477 aFlags.contains(ComputeSizeFlag::IApplyAutoMinSize))) {
6478 // This implements "Implied Minimum Size of Grid Items".
6479 // https://drafts.csswg.org/css-grid/#min-size-auto
6480 minISize = std::min(maxISize, GetMinISize(aRenderingContext));
6481 if (styleISize.IsLengthPercentage()) {
6482 minISize = std::min(minISize, result.ISize(aWM));
6483 } else if (aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
6484 // "if the grid item spans only grid tracks that have a fixed max track
6485 // sizing function, its automatic minimum size in that dimension is
6486 // further clamped to less than or equal to the size necessary to fit
6487 // its margin box within the resulting grid area (flooring at zero)"
6488 // https://drafts.csswg.org/css-grid/#min-size-auto
6489 auto maxMinISize =
6490 std::max(nscoord(0), aCBSize.ISize(aWM) - aBorderPadding.ISize(aWM) -
6491 aMargin.ISize(aWM));
6492 minISize = std::min(minISize, maxMinISize);
6494 } else if (aspectRatioUsage == AspectRatioUsage::ToComputeISize &&
6495 ShouldApplyAutomaticMinimumOnInlineAxis(aWM, disp, stylePos)) {
6496 // This means we successfully applied aspect-ratio and now need to check
6497 // if we need to apply the implied minimum size:
6498 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
6499 MOZ_ASSERT(!IsFrameOfType(eReplacedSizing),
6500 "aspect-ratio minimums should not apply to replaced elements");
6501 // The inline size computed by aspect-ratio shouldn't less than the content
6502 // size.
6503 minISize = GetMinISize(aRenderingContext);
6504 } else {
6505 // Treat "min-width: auto" as 0.
6506 // NOTE: Technically, "auto" is supposed to behave like "min-content" on
6507 // flex items. However, we don't need to worry about that here, because
6508 // flex items' min-sizes are intentionally ignored until the flex
6509 // container explicitly considers them during space distribution.
6510 minISize = 0;
6512 result.ISize(aWM) = std::max(minISize, result.ISize(aWM));
6514 // Compute block-axis size
6515 // (but not if we have auto bsize -- then, we'll just stick with the bsize
6516 // that we already calculated in the initial ComputeAutoSize() call. However,
6517 // if we have a valid preferred aspect ratio, we still have to compute the
6518 // block size because aspect ratio affects the intrinsic content size.)
6519 if (!isAutoBSize) {
6520 result.BSize(aWM) = nsLayoutUtils::ComputeBSizeValue(
6521 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6522 styleBSize.AsLengthPercentage());
6523 } else if (MOZ_UNLIKELY(isGridItem) && styleBSize.IsAuto() &&
6524 !aFlags.contains(ComputeSizeFlag::IsGridMeasuringReflow) &&
6525 !IsTrueOverflowContainer() &&
6526 !alignCB->IsMasonry(isOrthogonal ? eLogicalAxisInline
6527 : eLogicalAxisBlock)) {
6528 auto cbSize = aCBSize.BSize(aWM);
6529 if (cbSize != NS_UNCONSTRAINEDSIZE) {
6530 // 'auto' block-size for grid-level box - fill the CB for 'stretch' /
6531 // 'normal' and clamp it to the CB if requested:
6532 bool stretch = false;
6533 bool mayUseAspectRatio =
6534 aspectRatio && result.ISize(aWM) != NS_UNCONSTRAINEDSIZE;
6535 if (!StyleMargin()->HasBlockAxisAuto(aWM)) {
6536 auto blockAxisAlignment =
6537 isOrthogonal ? StylePosition()->UsedJustifySelf(alignCB->Style())._0
6538 : StylePosition()->UsedAlignSelf(alignCB->Style())._0;
6539 stretch = blockAxisAlignment == StyleAlignFlags::STRETCH ||
6540 (blockAxisAlignment == StyleAlignFlags::NORMAL &&
6541 !mayUseAspectRatio);
6544 // Apply the preferred aspect ratio for alignments other than *stretch*
6545 // and *normal without aspect ratio*.
6546 // The spec says all other values should size the items as fit-content,
6547 // and the intrinsic size should respect the preferred aspect ratio, so
6548 // we also apply aspect ratio for all other values.
6549 // https://drafts.csswg.org/css-grid/#grid-item-sizing
6550 if (!stretch && mayUseAspectRatio) {
6551 result.BSize(aWM) = aspectRatio.ComputeRatioDependentSize(
6552 LogicalAxis::eLogicalAxisBlock, aWM, result.ISize(aWM),
6553 boxSizingAdjust);
6554 MOZ_ASSERT(aspectRatioUsage == AspectRatioUsage::None);
6555 aspectRatioUsage = AspectRatioUsage::ToComputeBSize;
6558 if (stretch || aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize)) {
6559 auto bSizeToFillCB =
6560 std::max(nscoord(0),
6561 cbSize - aBorderPadding.BSize(aWM) - aMargin.BSize(aWM));
6562 if (stretch || (result.BSize(aWM) != NS_UNCONSTRAINEDSIZE &&
6563 result.BSize(aWM) > bSizeToFillCB)) {
6564 result.BSize(aWM) = bSizeToFillCB;
6568 } else if (aspectRatio) {
6569 // If both inline and block dimensions are auto, the block axis is the
6570 // ratio-dependent axis by default.
6571 // If we have a super large inline size, aspect-ratio should still be
6572 // applied (so aspectRatioUsage flag is set as expected). That's why we
6573 // apply aspect-ratio unconditionally for auto block size here.
6574 result.BSize(aWM) = aspectRatio.ComputeRatioDependentSize(
6575 LogicalAxis::eLogicalAxisBlock, aWM, result.ISize(aWM),
6576 boxSizingAdjust);
6577 MOZ_ASSERT(aspectRatioUsage == AspectRatioUsage::None);
6578 aspectRatioUsage = AspectRatioUsage::ToComputeBSize;
6581 if (result.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
6582 const bool isFlexItemBlockAxisMainAxis =
6583 isFlexItem && flexMainAxis == eLogicalAxisBlock;
6584 if (!isAutoMaxBSize && !isFlexItemBlockAxisMainAxis) {
6585 nscoord maxBSize = nsLayoutUtils::ComputeBSizeValue(
6586 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6587 maxBSizeCoord.AsLengthPercentage());
6588 result.BSize(aWM) = std::min(maxBSize, result.BSize(aWM));
6591 if (!isAutoMinBSize && !isFlexItemBlockAxisMainAxis) {
6592 nscoord minBSize = nsLayoutUtils::ComputeBSizeValue(
6593 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
6594 minBSizeCoord.AsLengthPercentage());
6595 result.BSize(aWM) = std::max(minBSize, result.BSize(aWM));
6599 if (IsThemed(disp)) {
6600 nsPresContext* pc = PresContext();
6601 const LayoutDeviceIntSize widget = pc->Theme()->GetMinimumWidgetSize(
6602 pc, this, disp->EffectiveAppearance());
6604 // Convert themed widget's physical dimensions to logical coords
6605 LogicalSize size(aWM, LayoutDeviceIntSize::ToAppUnits(
6606 widget, pc->AppUnitsPerDevPixel()));
6608 // GetMinimumWidgetSize() returns border-box; we need content-box.
6609 size -= aBorderPadding;
6611 if (size.BSize(aWM) > result.BSize(aWM)) {
6612 result.BSize(aWM) = size.BSize(aWM);
6614 if (size.ISize(aWM) > result.ISize(aWM)) {
6615 result.ISize(aWM) = size.ISize(aWM);
6619 result.ISize(aWM) = std::max(0, result.ISize(aWM));
6620 result.BSize(aWM) = std::max(0, result.BSize(aWM));
6622 return {result, aspectRatioUsage};
6625 nsRect nsIFrame::ComputeTightBounds(DrawTarget* aDrawTarget) const {
6626 return InkOverflowRect();
6629 /* virtual */
6630 nsresult nsIFrame::GetPrefWidthTightBounds(gfxContext* aContext, nscoord* aX,
6631 nscoord* aXMost) {
6632 return NS_ERROR_NOT_IMPLEMENTED;
6635 /* virtual */
6636 LogicalSize nsIFrame::ComputeAutoSize(
6637 gfxContext* aRenderingContext, WritingMode aWM,
6638 const mozilla::LogicalSize& aCBSize, nscoord aAvailableISize,
6639 const mozilla::LogicalSize& aMargin,
6640 const mozilla::LogicalSize& aBorderPadding,
6641 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) {
6642 // Use basic shrink-wrapping as a default implementation.
6643 LogicalSize result(aWM, 0xdeadbeef, NS_UNCONSTRAINEDSIZE);
6645 // don't bother setting it if the result won't be used
6646 const auto& styleISize = aSizeOverrides.mStyleISize
6647 ? *aSizeOverrides.mStyleISize
6648 : StylePosition()->ISize(aWM);
6649 if (styleISize.IsAuto()) {
6650 nscoord availBased =
6651 aAvailableISize - aMargin.ISize(aWM) - aBorderPadding.ISize(aWM);
6652 result.ISize(aWM) = ShrinkISizeToFit(aRenderingContext, availBased, aFlags);
6654 return result;
6657 nscoord nsIFrame::ShrinkISizeToFit(gfxContext* aRenderingContext,
6658 nscoord aISizeInCB,
6659 ComputeSizeFlags aFlags) {
6660 // If we're a container for font size inflation, then shrink
6661 // wrapping inside of us should not apply font size inflation.
6662 AutoMaybeDisableFontInflation an(this);
6664 nscoord result;
6665 nscoord minISize = GetMinISize(aRenderingContext);
6666 if (minISize > aISizeInCB) {
6667 const bool clamp = aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize);
6668 result = MOZ_UNLIKELY(clamp) ? aISizeInCB : minISize;
6669 } else {
6670 nscoord prefISize = GetPrefISize(aRenderingContext);
6671 if (prefISize > aISizeInCB) {
6672 result = aISizeInCB;
6673 } else {
6674 result = prefISize;
6677 return result;
6680 Maybe<nscoord> nsIFrame::ComputeInlineSizeFromAspectRatio(
6681 WritingMode aWM, const LogicalSize& aCBSize,
6682 const LogicalSize& aContentEdgeToBoxSizing,
6683 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) const {
6684 // FIXME: Bug 1670151: Use GetAspectRatio() to cover replaced elements (and
6685 // then we can drop the check of eSupportsAspectRatio).
6686 const AspectRatio aspectRatio =
6687 aSizeOverrides.mAspectRatio
6688 ? *aSizeOverrides.mAspectRatio
6689 : StylePosition()->mAspectRatio.ToLayoutRatio();
6690 if (!IsFrameOfType(eSupportsAspectRatio) || !aspectRatio) {
6691 return Nothing();
6694 const StyleSize& styleBSize = aSizeOverrides.mStyleBSize
6695 ? *aSizeOverrides.mStyleBSize
6696 : StylePosition()->BSize(aWM);
6697 if (nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM))) {
6698 return Nothing();
6701 MOZ_ASSERT(styleBSize.IsLengthPercentage());
6702 nscoord bSize = nsLayoutUtils::ComputeBSizeValue(
6703 aCBSize.BSize(aWM), aContentEdgeToBoxSizing.BSize(aWM),
6704 styleBSize.AsLengthPercentage());
6705 return Some(aspectRatio.ComputeRatioDependentSize(
6706 LogicalAxis::eLogicalAxisInline, aWM, bSize, aContentEdgeToBoxSizing));
6709 nsIFrame::ISizeComputationResult nsIFrame::ComputeISizeValue(
6710 gfxContext* aRenderingContext, const WritingMode aWM,
6711 const LogicalSize& aContainingBlockSize,
6712 const LogicalSize& aContentEdgeToBoxSizing, nscoord aBoxSizingToMarginEdge,
6713 ExtremumLength aSize, Maybe<nscoord> aAvailableISizeOverride,
6714 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) {
6715 // If 'this' is a container for font size inflation, then shrink
6716 // wrapping inside of it should not apply font size inflation.
6717 AutoMaybeDisableFontInflation an(this);
6718 // If we have an aspect-ratio and a definite block size, we resolve the
6719 // min-content and max-content size by the aspect-ratio and the block size.
6720 // https://github.com/w3c/csswg-drafts/issues/5032
6721 Maybe<nscoord> intrinsicSizeFromAspectRatio =
6722 aSize == ExtremumLength::MozAvailable
6723 ? Nothing()
6724 : ComputeInlineSizeFromAspectRatio(aWM, aContainingBlockSize,
6725 aContentEdgeToBoxSizing,
6726 aSizeOverrides, aFlags);
6727 nscoord result;
6728 switch (aSize) {
6729 case ExtremumLength::MaxContent:
6730 result = intrinsicSizeFromAspectRatio ? *intrinsicSizeFromAspectRatio
6731 : GetPrefISize(aRenderingContext);
6732 NS_ASSERTION(result >= 0, "inline-size less than zero");
6733 return {result, intrinsicSizeFromAspectRatio
6734 ? AspectRatioUsage::ToComputeISize
6735 : AspectRatioUsage::None};
6736 case ExtremumLength::MinContent:
6737 result = intrinsicSizeFromAspectRatio ? *intrinsicSizeFromAspectRatio
6738 : GetMinISize(aRenderingContext);
6739 NS_ASSERTION(result >= 0, "inline-size less than zero");
6740 if (MOZ_UNLIKELY(
6741 aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize))) {
6742 auto available =
6743 aContainingBlockSize.ISize(aWM) -
6744 (aBoxSizingToMarginEdge + aContentEdgeToBoxSizing.ISize(aWM));
6745 result = std::min(available, result);
6747 return {result, intrinsicSizeFromAspectRatio
6748 ? AspectRatioUsage::ToComputeISize
6749 : AspectRatioUsage::None};
6750 case ExtremumLength::FitContentFunction:
6751 case ExtremumLength::FitContent: {
6752 nscoord pref = NS_UNCONSTRAINEDSIZE;
6753 nscoord min = 0;
6754 if (intrinsicSizeFromAspectRatio) {
6755 // The min-content and max-content size are identical and equal to the
6756 // size computed from the block size and the aspect ratio.
6757 pref = min = *intrinsicSizeFromAspectRatio;
6758 } else {
6759 pref = GetPrefISize(aRenderingContext);
6760 min = GetMinISize(aRenderingContext);
6763 nscoord fill = aAvailableISizeOverride
6764 ? *aAvailableISizeOverride
6765 : aContainingBlockSize.ISize(aWM) -
6766 (aBoxSizingToMarginEdge +
6767 aContentEdgeToBoxSizing.ISize(aWM));
6769 if (MOZ_UNLIKELY(
6770 aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize))) {
6771 min = std::min(min, fill);
6773 result = std::max(min, std::min(pref, fill));
6774 NS_ASSERTION(result >= 0, "inline-size less than zero");
6775 return {result};
6777 case ExtremumLength::MozAvailable:
6778 return {aContainingBlockSize.ISize(aWM) -
6779 (aBoxSizingToMarginEdge + aContentEdgeToBoxSizing.ISize(aWM))};
6781 MOZ_ASSERT_UNREACHABLE("Unknown extremum length?");
6782 return {};
6785 nscoord nsIFrame::ComputeISizeValue(const WritingMode aWM,
6786 const LogicalSize& aContainingBlockSize,
6787 const LogicalSize& aContentEdgeToBoxSizing,
6788 const LengthPercentage& aSize) {
6789 LAYOUT_WARN_IF_FALSE(
6790 aContainingBlockSize.ISize(aWM) != NS_UNCONSTRAINEDSIZE,
6791 "have unconstrained inline-size; this should only result from "
6792 "very large sizes, not attempts at intrinsic inline-size "
6793 "calculation");
6794 NS_ASSERTION(aContainingBlockSize.ISize(aWM) >= 0,
6795 "inline-size less than zero");
6797 nscoord result = aSize.Resolve(aContainingBlockSize.ISize(aWM));
6798 // The result of a calc() expression might be less than 0; we
6799 // should clamp at runtime (below). (Percentages and coords that
6800 // are less than 0 have already been dropped by the parser.)
6801 result -= aContentEdgeToBoxSizing.ISize(aWM);
6802 return std::max(0, result);
6805 void nsIFrame::DidReflow(nsPresContext* aPresContext,
6806 const ReflowInput* aReflowInput) {
6807 NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS, ("nsIFrame::DidReflow"));
6809 if (IsHiddenByContentVisibilityOfInFlowParentForLayout()) {
6810 RemoveStateBits(NS_FRAME_IN_REFLOW);
6811 return;
6814 SVGObserverUtils::InvalidateDirectRenderingObservers(
6815 this, SVGObserverUtils::INVALIDATE_REFLOW);
6817 RemoveStateBits(NS_FRAME_IN_REFLOW | NS_FRAME_FIRST_REFLOW |
6818 NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN);
6820 // Clear bits that were used in ReflowInput::InitResizeFlags (see
6821 // comment there for why we can't clear it there).
6822 SetHasBSizeChange(false);
6823 SetHasPaddingChange(false);
6825 // Notify the percent bsize observer if there is a percent bsize.
6826 // The observer may be able to initiate another reflow with a computed
6827 // bsize. This happens in the case where a table cell has no computed
6828 // bsize but can fabricate one when the cell bsize is known.
6829 if (aReflowInput && aReflowInput->mPercentBSizeObserver && !GetPrevInFlow()) {
6830 const auto& bsize =
6831 aReflowInput->mStylePosition->BSize(aReflowInput->GetWritingMode());
6832 if (bsize.HasPercent()) {
6833 aReflowInput->mPercentBSizeObserver->NotifyPercentBSize(*aReflowInput);
6837 aPresContext->ReflowedFrame();
6840 void nsIFrame::FinishReflowWithAbsoluteFrames(nsPresContext* aPresContext,
6841 ReflowOutput& aDesiredSize,
6842 const ReflowInput& aReflowInput,
6843 nsReflowStatus& aStatus,
6844 bool aConstrainBSize) {
6845 ReflowAbsoluteFrames(aPresContext, aDesiredSize, aReflowInput, aStatus,
6846 aConstrainBSize);
6848 FinishAndStoreOverflow(&aDesiredSize, aReflowInput.mStyleDisplay);
6851 void nsIFrame::ReflowAbsoluteFrames(nsPresContext* aPresContext,
6852 ReflowOutput& aDesiredSize,
6853 const ReflowInput& aReflowInput,
6854 nsReflowStatus& aStatus,
6855 bool aConstrainBSize) {
6856 if (HasAbsolutelyPositionedChildren()) {
6857 nsAbsoluteContainingBlock* absoluteContainer = GetAbsoluteContainingBlock();
6859 // Let the absolutely positioned container reflow any absolutely positioned
6860 // child frames that need to be reflowed
6862 // The containing block for the abs pos kids is formed by our padding edge.
6863 nsMargin usedBorder = GetUsedBorder();
6864 nscoord containingBlockWidth =
6865 std::max(0, aDesiredSize.Width() - usedBorder.LeftRight());
6866 nscoord containingBlockHeight =
6867 std::max(0, aDesiredSize.Height() - usedBorder.TopBottom());
6868 nsContainerFrame* container = do_QueryFrame(this);
6869 NS_ASSERTION(container,
6870 "Abs-pos children only supported on container frames for now");
6872 nsRect containingBlock(0, 0, containingBlockWidth, containingBlockHeight);
6873 AbsPosReflowFlags flags =
6874 AbsPosReflowFlags::CBWidthAndHeightChanged; // XXX could be optimized
6875 if (aConstrainBSize) {
6876 flags |= AbsPosReflowFlags::ConstrainHeight;
6878 absoluteContainer->Reflow(container, aPresContext, aReflowInput, aStatus,
6879 containingBlock, flags,
6880 &aDesiredSize.mOverflowAreas);
6884 /* virtual */
6885 bool nsIFrame::CanContinueTextRun() const {
6886 // By default, a frame will *not* allow a text run to be continued
6887 // through it.
6888 return false;
6891 void nsIFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize,
6892 const ReflowInput& aReflowInput,
6893 nsReflowStatus& aStatus) {
6894 MarkInReflow();
6895 DO_GLOBAL_REFLOW_COUNT("nsFrame");
6896 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
6897 aDesiredSize.ClearSize();
6900 bool nsIFrame::IsContentDisabled() const {
6901 // FIXME(emilio): Doing this via CSS means callers must ensure the style is up
6902 // to date, and they don't!
6903 if (StyleUI()->UserInput() == StyleUserInput::None) {
6904 return true;
6907 auto* element = nsGenericHTMLElement::FromNodeOrNull(GetContent());
6908 return element && element->IsDisabled();
6911 bool nsIFrame::IsContentRelevant() const {
6912 MOZ_ASSERT(StyleDisplay()->ContentVisibility(*this) ==
6913 StyleContentVisibility::Auto);
6915 auto* element = Element::FromNodeOrNull(GetContent());
6916 MOZ_ASSERT(element);
6918 Maybe<ContentRelevancy> relevancy = element->GetContentRelevancy();
6919 if (relevancy.isSome()) {
6920 return !relevancy->isEmpty();
6923 // If there is no relevancy set, then this frame still has not received had
6924 // the initial visibility callback call. In that case, only rely on whether
6925 // or not it is inside a top layer element which will never change for this
6926 // frame and allows proper rendering of the top layer.
6927 return IsDescendantOfTopLayerElement();
6930 bool nsIFrame::HidesContent(
6931 const EnumSet<IncludeContentVisibility>& aInclude) const {
6932 auto effectiveContentVisibility = StyleDisplay()->ContentVisibility(*this);
6933 if (aInclude.contains(IncludeContentVisibility::Hidden) &&
6934 effectiveContentVisibility == StyleContentVisibility::Hidden) {
6935 return true;
6938 if (aInclude.contains(IncludeContentVisibility::Auto) &&
6939 effectiveContentVisibility == StyleContentVisibility::Auto) {
6940 return !IsContentRelevant();
6943 return false;
6946 bool nsIFrame::HidesContentForLayout() const {
6947 return HidesContent() && !PresShell()->IsForcingLayoutForHiddenContent(this);
6950 bool nsIFrame::IsHiddenByContentVisibilityOfInFlowParentForLayout() const {
6951 const auto* parent = GetInFlowParent();
6952 // The anonymous children owned by parent are important for properly sizing
6953 // their parents.
6954 return parent && parent->HidesContentForLayout() &&
6955 !(parent->HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES) &&
6956 Style()->IsAnonBox());
6959 bool nsIFrame::IsHiddenByContentVisibilityOnAnyAncestor(
6960 const EnumSet<IncludeContentVisibility>& aInclude) const {
6961 if (!StaticPrefs::layout_css_content_visibility_enabled()) {
6962 return false;
6965 auto* parent = GetInFlowParent();
6966 bool isAnonymousBlock = Style()->IsAnonBox() && parent &&
6967 parent->HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES);
6968 for (nsIFrame* cur = parent; cur; cur = cur->GetInFlowParent()) {
6969 if (!isAnonymousBlock && cur->HidesContent(aInclude)) {
6970 return true;
6973 // Anonymous boxes are not hidden by the content-visibility of their first
6974 // non-anonymous ancestor, but can be hidden by ancestors further up the
6975 // tree.
6976 isAnonymousBlock = false;
6979 return false;
6982 bool nsIFrame::HasSelectionInSubtree() {
6983 if (IsSelected()) {
6984 return true;
6987 RefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
6988 if (!frameSelection) {
6989 return false;
6992 const Selection* selection =
6993 frameSelection->GetSelection(SelectionType::eNormal);
6994 if (!selection) {
6995 return false;
6998 for (uint32_t i = 0; i < selection->RangeCount(); i++) {
6999 auto* range = selection->GetRangeAt(i);
7000 MOZ_ASSERT(range);
7002 const auto* commonAncestorNode =
7003 range->GetRegisteredClosestCommonInclusiveAncestor();
7004 if (commonAncestorNode &&
7005 commonAncestorNode->IsInclusiveDescendantOf(GetContent())) {
7006 return true;
7010 return false;
7013 bool nsIFrame::IsDescendantOfTopLayerElement() const {
7014 if (!GetContent()) {
7015 return false;
7018 nsTArray<dom::Element*> topLayer = PresContext()->Document()->GetTopLayer();
7019 for (auto* element : topLayer) {
7020 if (GetContent()->IsInclusiveFlatTreeDescendantOf(element)) {
7021 return true;
7025 return false;
7028 void nsIFrame::UpdateIsRelevantContent(
7029 const ContentRelevancy& aRelevancyToUpdate) {
7030 MOZ_ASSERT(StyleDisplay()->ContentVisibility(*this) ==
7031 StyleContentVisibility::Auto);
7033 auto* element = Element::FromNodeOrNull(GetContent());
7034 MOZ_ASSERT(element);
7036 ContentRelevancy newRelevancy;
7037 Maybe<ContentRelevancy> oldRelevancy = element->GetContentRelevancy();
7038 if (oldRelevancy.isSome()) {
7039 newRelevancy = *oldRelevancy;
7042 auto setRelevancyValue = [&](ContentRelevancyReason reason, bool value) {
7043 if (value) {
7044 newRelevancy += reason;
7045 } else {
7046 newRelevancy -= reason;
7050 if (!oldRelevancy ||
7051 aRelevancyToUpdate.contains(ContentRelevancyReason::Visible)) {
7052 Maybe<bool> visible = element->GetVisibleForContentVisibility();
7053 if (visible.isSome()) {
7054 setRelevancyValue(ContentRelevancyReason::Visible, *visible);
7058 if (!oldRelevancy ||
7059 aRelevancyToUpdate.contains(ContentRelevancyReason::FocusInSubtree)) {
7060 setRelevancyValue(ContentRelevancyReason::FocusInSubtree,
7061 element->State().HasAtLeastOneOfStates(
7062 ElementState::FOCUS_WITHIN | ElementState::FOCUS));
7065 if (!oldRelevancy ||
7066 aRelevancyToUpdate.contains(ContentRelevancyReason::Selected)) {
7067 setRelevancyValue(ContentRelevancyReason::Selected,
7068 HasSelectionInSubtree());
7071 bool overallRelevancyChanged =
7072 !oldRelevancy || oldRelevancy->isEmpty() != newRelevancy.isEmpty();
7073 if (!oldRelevancy || *oldRelevancy != newRelevancy) {
7074 element->SetContentRelevancy(newRelevancy);
7077 if (!overallRelevancyChanged) {
7078 return;
7081 HandleLastRememberedSize();
7082 PresShell()->FrameNeedsReflow(
7083 this, IntrinsicDirty::FrameAncestorsAndDescendants, NS_FRAME_IS_DIRTY);
7084 InvalidateFrame();
7086 ContentVisibilityAutoStateChangeEventInit init;
7087 init.mSkipped = newRelevancy.isEmpty();
7088 RefPtr<ContentVisibilityAutoStateChangeEvent> event =
7089 ContentVisibilityAutoStateChangeEvent::Constructor(
7090 element, u"contentvisibilityautostatechange"_ns, init);
7092 // Per
7093 // https://drafts.csswg.org/css-contain/#content-visibility-auto-state-changed
7094 // "This event is dispatched by posting a task at the time when the state
7095 // change occurs."
7096 RefPtr<AsyncEventDispatcher> asyncDispatcher =
7097 new AsyncEventDispatcher(element, event.forget());
7098 DebugOnly<nsresult> rv = asyncDispatcher->PostDOMEvent();
7099 NS_ASSERTION(NS_SUCCEEDED(rv), "AsyncEventDispatcher failed to dispatch");
7102 nsresult nsIFrame::CharacterDataChanged(const CharacterDataChangeInfo&) {
7103 MOZ_ASSERT_UNREACHABLE("should only be called for text frames");
7104 return NS_OK;
7107 nsresult nsIFrame::AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute,
7108 int32_t aModType) {
7109 return NS_OK;
7112 // Flow member functions
7114 nsIFrame* nsIFrame::GetPrevContinuation() const { return nullptr; }
7116 void nsIFrame::SetPrevContinuation(nsIFrame* aPrevContinuation) {
7117 MOZ_ASSERT(false, "not splittable");
7120 nsIFrame* nsIFrame::GetNextContinuation() const { return nullptr; }
7122 void nsIFrame::SetNextContinuation(nsIFrame*) {
7123 MOZ_ASSERT(false, "not splittable");
7126 nsIFrame* nsIFrame::GetPrevInFlow() const { return nullptr; }
7128 void nsIFrame::SetPrevInFlow(nsIFrame* aPrevInFlow) {
7129 MOZ_ASSERT(false, "not splittable");
7132 nsIFrame* nsIFrame::GetNextInFlow() const { return nullptr; }
7134 void nsIFrame::SetNextInFlow(nsIFrame*) { MOZ_ASSERT(false, "not splittable"); }
7136 nsIFrame* nsIFrame::GetTailContinuation() {
7137 nsIFrame* frame = this;
7138 while (frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
7139 frame = frame->GetPrevContinuation();
7140 NS_ASSERTION(frame, "first continuation can't be overflow container");
7142 for (nsIFrame* next = frame->GetNextContinuation();
7143 next && !next->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
7144 next = frame->GetNextContinuation()) {
7145 frame = next;
7148 MOZ_ASSERT(frame, "illegal state in continuation chain.");
7149 return frame;
7152 // Associated view object
7153 void nsIFrame::SetView(nsView* aView) {
7154 if (aView) {
7155 aView->SetFrame(this);
7157 #ifdef DEBUG
7158 LayoutFrameType frameType = Type();
7159 NS_ASSERTION(frameType == LayoutFrameType::SubDocument ||
7160 frameType == LayoutFrameType::ListControl ||
7161 frameType == LayoutFrameType::Viewport ||
7162 frameType == LayoutFrameType::MenuPopup,
7163 "Only specific frame types can have an nsView");
7164 #endif
7166 // Store the view on the frame.
7167 SetViewInternal(aView);
7169 // Set the frame state bit that says the frame has a view
7170 AddStateBits(NS_FRAME_HAS_VIEW);
7172 // Let all of the ancestors know they have a descendant with a view.
7173 for (nsIFrame* f = GetParent();
7174 f && !f->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
7175 f = f->GetParent())
7176 f->AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
7177 } else {
7178 MOZ_ASSERT_UNREACHABLE("Destroying a view while the frame is alive?");
7179 RemoveStateBits(NS_FRAME_HAS_VIEW);
7180 SetViewInternal(nullptr);
7184 // Find the first geometric parent that has a view
7185 nsIFrame* nsIFrame::GetAncestorWithView() const {
7186 for (nsIFrame* f = GetParent(); nullptr != f; f = f->GetParent()) {
7187 if (f->HasView()) {
7188 return f;
7191 return nullptr;
7194 template <nsPoint (nsIFrame::*PositionGetter)() const>
7195 static nsPoint OffsetCalculator(const nsIFrame* aThis, const nsIFrame* aOther) {
7196 MOZ_ASSERT(aOther, "Must have frame for destination coordinate system!");
7198 NS_ASSERTION(aThis->PresContext() == aOther->PresContext(),
7199 "GetOffsetTo called on frames in different documents");
7201 nsPoint offset(0, 0);
7202 const nsIFrame* f;
7203 for (f = aThis; f != aOther && f; f = f->GetParent()) {
7204 offset += (f->*PositionGetter)();
7207 if (f != aOther) {
7208 // Looks like aOther wasn't an ancestor of |this|. So now we have
7209 // the root-frame-relative position of |this| in |offset|. Convert back
7210 // to the coordinates of aOther
7211 while (aOther) {
7212 offset -= (aOther->*PositionGetter)();
7213 aOther = aOther->GetParent();
7217 return offset;
7220 nsPoint nsIFrame::GetOffsetTo(const nsIFrame* aOther) const {
7221 return OffsetCalculator<&nsIFrame::GetPosition>(this, aOther);
7224 nsPoint nsIFrame::GetOffsetToIgnoringScrolling(const nsIFrame* aOther) const {
7225 return OffsetCalculator<&nsIFrame::GetPositionIgnoringScrolling>(this,
7226 aOther);
7229 nsPoint nsIFrame::GetOffsetToCrossDoc(const nsIFrame* aOther) const {
7230 return GetOffsetToCrossDoc(aOther, PresContext()->AppUnitsPerDevPixel());
7233 nsPoint nsIFrame::GetOffsetToCrossDoc(const nsIFrame* aOther,
7234 const int32_t aAPD) const {
7235 MOZ_ASSERT(aOther, "Must have frame for destination coordinate system!");
7236 NS_ASSERTION(PresContext()->GetRootPresContext() ==
7237 aOther->PresContext()->GetRootPresContext(),
7238 "trying to get the offset between frames in different document "
7239 "hierarchies?");
7240 if (PresContext()->GetRootPresContext() !=
7241 aOther->PresContext()->GetRootPresContext()) {
7242 // crash right away, we are almost certainly going to crash anyway.
7243 MOZ_CRASH(
7244 "trying to get the offset between frames in different "
7245 "document hierarchies?");
7248 const nsIFrame* root = nullptr;
7249 // offset will hold the final offset
7250 // docOffset holds the currently accumulated offset at the current APD, it
7251 // will be converted and added to offset when the current APD changes.
7252 nsPoint offset(0, 0), docOffset(0, 0);
7253 const nsIFrame* f = this;
7254 int32_t currAPD = PresContext()->AppUnitsPerDevPixel();
7255 while (f && f != aOther) {
7256 docOffset += f->GetPosition();
7257 nsIFrame* parent = f->GetParent();
7258 if (parent) {
7259 f = parent;
7260 } else {
7261 nsPoint newOffset(0, 0);
7262 root = f;
7263 f = nsLayoutUtils::GetCrossDocParentFrameInProcess(f, &newOffset);
7264 int32_t newAPD = f ? f->PresContext()->AppUnitsPerDevPixel() : 0;
7265 if (!f || newAPD != currAPD) {
7266 // Convert docOffset to the right APD and add it to offset.
7267 offset += docOffset.ScaleToOtherAppUnits(currAPD, aAPD);
7268 docOffset.x = docOffset.y = 0;
7270 currAPD = newAPD;
7271 docOffset += newOffset;
7274 if (f == aOther) {
7275 offset += docOffset.ScaleToOtherAppUnits(currAPD, aAPD);
7276 } else {
7277 // Looks like aOther wasn't an ancestor of |this|. So now we have
7278 // the root-document-relative position of |this| in |offset|. Subtract the
7279 // root-document-relative position of |aOther| from |offset|.
7280 // This call won't try to recurse again because root is an ancestor of
7281 // aOther.
7282 nsPoint negOffset = aOther->GetOffsetToCrossDoc(root, aAPD);
7283 offset -= negOffset;
7286 return offset;
7289 CSSIntRect nsIFrame::GetScreenRect() const {
7290 return CSSIntRect::FromAppUnitsToNearest(GetScreenRectInAppUnits());
7293 nsRect nsIFrame::GetScreenRectInAppUnits() const {
7294 nsPresContext* presContext = PresContext();
7295 nsIFrame* rootFrame = presContext->PresShell()->GetRootFrame();
7296 nsPoint rootScreenPos(0, 0);
7297 nsPoint rootFrameOffsetInParent(0, 0);
7298 nsIFrame* rootFrameParent = nsLayoutUtils::GetCrossDocParentFrameInProcess(
7299 rootFrame, &rootFrameOffsetInParent);
7300 if (rootFrameParent) {
7301 nsRect parentScreenRectAppUnits =
7302 rootFrameParent->GetScreenRectInAppUnits();
7303 nsPresContext* parentPresContext = rootFrameParent->PresContext();
7304 double parentScale = double(presContext->AppUnitsPerDevPixel()) /
7305 parentPresContext->AppUnitsPerDevPixel();
7306 nsPoint rootPt =
7307 parentScreenRectAppUnits.TopLeft() + rootFrameOffsetInParent;
7308 rootScreenPos.x = NS_round(parentScale * rootPt.x);
7309 rootScreenPos.y = NS_round(parentScale * rootPt.y);
7310 } else {
7311 nsCOMPtr<nsIWidget> rootWidget =
7312 presContext->PresShell()->GetViewManager()->GetRootWidget();
7313 if (rootWidget) {
7314 LayoutDeviceIntPoint rootDevPx = rootWidget->WidgetToScreenOffset();
7315 rootScreenPos.x = presContext->DevPixelsToAppUnits(rootDevPx.x);
7316 rootScreenPos.y = presContext->DevPixelsToAppUnits(rootDevPx.y);
7320 return nsRect(rootScreenPos + GetOffsetTo(rootFrame), GetSize());
7323 // Returns the offset from this frame to the closest geometric parent that
7324 // has a view. Also returns the containing view or null in case of error
7325 void nsIFrame::GetOffsetFromView(nsPoint& aOffset, nsView** aView) const {
7326 MOZ_ASSERT(nullptr != aView, "null OUT parameter pointer");
7327 nsIFrame* frame = const_cast<nsIFrame*>(this);
7329 *aView = nullptr;
7330 aOffset.MoveTo(0, 0);
7331 do {
7332 aOffset += frame->GetPosition();
7333 frame = frame->GetParent();
7334 } while (frame && !frame->HasView());
7336 if (frame) {
7337 *aView = frame->GetView();
7341 nsIWidget* nsIFrame::GetNearestWidget() const {
7342 return GetClosestView()->GetNearestWidget(nullptr);
7345 nsIWidget* nsIFrame::GetNearestWidget(nsPoint& aOffset) const {
7346 nsPoint offsetToView;
7347 nsPoint offsetToWidget;
7348 nsIWidget* widget =
7349 GetClosestView(&offsetToView)->GetNearestWidget(&offsetToWidget);
7350 aOffset = offsetToView + offsetToWidget;
7351 return widget;
7354 Matrix4x4Flagged nsIFrame::GetTransformMatrix(ViewportType aViewportType,
7355 RelativeTo aStopAtAncestor,
7356 nsIFrame** aOutAncestor,
7357 uint32_t aFlags) const {
7358 MOZ_ASSERT(aOutAncestor, "Need a place to put the ancestor!");
7360 /* If we're transformed, we want to hand back the combination
7361 * transform/translate matrix that will apply our current transform, then
7362 * shift us to our parent.
7364 const bool isTransformed = IsTransformed();
7365 const nsIFrame* zoomedContentRoot = nullptr;
7366 if (aStopAtAncestor.mViewportType == ViewportType::Visual) {
7367 zoomedContentRoot = ViewportUtils::IsZoomedContentRoot(this);
7368 if (zoomedContentRoot) {
7369 MOZ_ASSERT(aViewportType != ViewportType::Visual);
7373 if (isTransformed || zoomedContentRoot) {
7374 Matrix4x4 result;
7375 int32_t scaleFactor =
7376 ((aFlags & IN_CSS_UNITS) ? AppUnitsPerCSSPixel()
7377 : PresContext()->AppUnitsPerDevPixel());
7379 /* Compute the delta to the parent, which we need because we are converting
7380 * coordinates to our parent.
7382 if (isTransformed) {
7383 NS_ASSERTION(nsLayoutUtils::GetCrossDocParentFrameInProcess(this),
7384 "Cannot transform the viewport frame!");
7386 result = result * nsDisplayTransform::GetResultingTransformMatrix(
7387 this, nsPoint(0, 0), scaleFactor,
7388 nsDisplayTransform::INCLUDE_PERSPECTIVE |
7389 nsDisplayTransform::OFFSET_BY_ORIGIN);
7392 // The offset from a zoomed content root to its parent (e.g. from
7393 // a canvas frame to a scroll frame) is in layout coordinates, so
7394 // apply it before applying any layout-to-visual transform.
7395 *aOutAncestor = nsLayoutUtils::GetCrossDocParentFrameInProcess(this);
7396 nsPoint delta = GetOffsetToCrossDoc(*aOutAncestor);
7397 /* Combine the raw transform with a translation to our parent. */
7398 result.PostTranslate(NSAppUnitsToFloatPixels(delta.x, scaleFactor),
7399 NSAppUnitsToFloatPixels(delta.y, scaleFactor), 0.0f);
7401 if (zoomedContentRoot) {
7402 Matrix4x4 layoutToVisual;
7403 ScrollableLayerGuid::ViewID targetScrollId =
7404 nsLayoutUtils::FindOrCreateIDFor(zoomedContentRoot->GetContent());
7405 if (aFlags & nsIFrame::IN_CSS_UNITS) {
7406 layoutToVisual =
7407 ViewportUtils::GetVisualToLayoutTransform(targetScrollId)
7408 .Inverse()
7409 .ToUnknownMatrix();
7410 } else {
7411 layoutToVisual =
7412 ViewportUtils::GetVisualToLayoutTransform<LayoutDevicePixel>(
7413 targetScrollId)
7414 .Inverse()
7415 .ToUnknownMatrix();
7417 result = result * layoutToVisual;
7420 return result;
7423 *aOutAncestor = nsLayoutUtils::GetCrossDocParentFrameInProcess(this);
7425 /* Otherwise, we're not transformed. In that case, we'll walk up the frame
7426 * tree until we either hit the root frame or something that may be
7427 * transformed. We'll then change coordinates into that frame, since we're
7428 * guaranteed that nothing in-between can be transformed. First, however,
7429 * we have to check to see if we have a parent. If not, we'll set the
7430 * outparam to null (indicating that there's nothing left) and will hand back
7431 * the identity matrix.
7433 if (!*aOutAncestor) return Matrix4x4();
7435 /* Keep iterating while the frame can't possibly be transformed. */
7436 const nsIFrame* current = this;
7437 auto shouldStopAt = [](const nsIFrame* aCurrent, nsIFrame* aAncestor,
7438 uint32_t aFlags) {
7439 return aAncestor->IsTransformed() || nsLayoutUtils::IsPopup(aAncestor) ||
7440 ViewportUtils::IsZoomedContentRoot(aAncestor) ||
7441 ((aFlags & STOP_AT_STACKING_CONTEXT_AND_DISPLAY_PORT) &&
7442 (aAncestor->IsStackingContext() ||
7443 DisplayPortUtils::FrameHasDisplayPort(aAncestor, aCurrent)));
7445 while (*aOutAncestor != aStopAtAncestor.mFrame &&
7446 !shouldStopAt(current, *aOutAncestor, aFlags)) {
7447 /* If no parent, stop iterating. Otherwise, update the ancestor. */
7448 nsIFrame* parent =
7449 nsLayoutUtils::GetCrossDocParentFrameInProcess(*aOutAncestor);
7450 if (!parent) break;
7452 current = *aOutAncestor;
7453 *aOutAncestor = parent;
7456 NS_ASSERTION(*aOutAncestor, "Somehow ended up with a null ancestor...?");
7458 /* Translate from this frame to our ancestor, if it exists. That's the
7459 * entire transform, so we're done.
7461 nsPoint delta = GetOffsetToCrossDoc(*aOutAncestor);
7462 int32_t scaleFactor =
7463 ((aFlags & IN_CSS_UNITS) ? AppUnitsPerCSSPixel()
7464 : PresContext()->AppUnitsPerDevPixel());
7465 return Matrix4x4::Translation(NSAppUnitsToFloatPixels(delta.x, scaleFactor),
7466 NSAppUnitsToFloatPixels(delta.y, scaleFactor),
7467 0.0f);
7470 static void InvalidateRenderingObservers(nsIFrame* aDisplayRoot,
7471 nsIFrame* aFrame,
7472 bool aFrameChanged = true) {
7473 MOZ_ASSERT(aDisplayRoot == nsLayoutUtils::GetDisplayRootFrame(aFrame));
7474 SVGObserverUtils::InvalidateDirectRenderingObservers(aFrame);
7475 nsIFrame* parent = aFrame;
7476 while (parent != aDisplayRoot &&
7477 (parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(parent)) &&
7478 !parent->HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT)) {
7479 SVGObserverUtils::InvalidateDirectRenderingObservers(parent);
7482 if (!aFrameChanged) {
7483 return;
7486 aFrame->MarkNeedsDisplayItemRebuild();
7489 static void SchedulePaintInternal(
7490 nsIFrame* aDisplayRoot, nsIFrame* aFrame,
7491 nsIFrame::PaintType aType = nsIFrame::PAINT_DEFAULT) {
7492 MOZ_ASSERT(aDisplayRoot == nsLayoutUtils::GetDisplayRootFrame(aFrame));
7493 nsPresContext* pres = aDisplayRoot->PresContext()->GetRootPresContext();
7495 // No need to schedule a paint for an external document since they aren't
7496 // painted directly.
7497 if (!pres || (pres->Document() && pres->Document()->IsResourceDoc())) {
7498 return;
7500 if (!pres->GetContainerWeak()) {
7501 NS_WARNING("Shouldn't call SchedulePaint in a detached pres context");
7502 return;
7505 pres->PresShell()->ScheduleViewManagerFlush();
7507 if (aType == nsIFrame::PAINT_DEFAULT) {
7508 aDisplayRoot->AddStateBits(NS_FRAME_UPDATE_LAYER_TREE);
7512 static void InvalidateFrameInternal(nsIFrame* aFrame, bool aHasDisplayItem,
7513 bool aRebuildDisplayItems) {
7514 if (aHasDisplayItem) {
7515 aFrame->AddStateBits(NS_FRAME_NEEDS_PAINT);
7518 if (aRebuildDisplayItems) {
7519 aFrame->MarkNeedsDisplayItemRebuild();
7521 SVGObserverUtils::InvalidateDirectRenderingObservers(aFrame);
7522 bool needsSchedulePaint = false;
7523 if (nsLayoutUtils::IsPopup(aFrame)) {
7524 needsSchedulePaint = true;
7525 } else {
7526 nsIFrame* parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(aFrame);
7527 while (parent &&
7528 !parent->HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT)) {
7529 if (aHasDisplayItem && !parent->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
7530 parent->AddStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT);
7532 SVGObserverUtils::InvalidateDirectRenderingObservers(parent);
7534 // If we're inside a popup, then we need to make sure that we
7535 // call schedule paint so that the NS_FRAME_UPDATE_LAYER_TREE
7536 // flag gets added to the popup display root frame.
7537 if (nsLayoutUtils::IsPopup(parent)) {
7538 needsSchedulePaint = true;
7539 break;
7541 parent = nsLayoutUtils::GetCrossDocParentFrameInProcess(parent);
7543 if (!parent) {
7544 needsSchedulePaint = true;
7547 if (!aHasDisplayItem) {
7548 return;
7550 if (needsSchedulePaint) {
7551 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(aFrame);
7552 SchedulePaintInternal(displayRoot, aFrame);
7554 if (aFrame->HasAnyStateBits(NS_FRAME_HAS_INVALID_RECT)) {
7555 aFrame->RemoveProperty(nsIFrame::InvalidationRect());
7556 aFrame->RemoveStateBits(NS_FRAME_HAS_INVALID_RECT);
7560 void nsIFrame::InvalidateFrameSubtree(bool aRebuildDisplayItems /* = true */) {
7561 InvalidateFrame(0, aRebuildDisplayItems);
7563 if (HasAnyStateBits(NS_FRAME_ALL_DESCENDANTS_NEED_PAINT)) {
7564 return;
7567 AddStateBits(NS_FRAME_ALL_DESCENDANTS_NEED_PAINT);
7569 for (const auto& childList : CrossDocChildLists()) {
7570 for (nsIFrame* child : childList.mList) {
7571 // Don't explicitly rebuild display items for our descendants,
7572 // since we should be marked and it implicitly includes all
7573 // descendants.
7574 child->InvalidateFrameSubtree(false);
7579 void nsIFrame::ClearInvalidationStateBits() {
7580 if (HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT)) {
7581 for (const auto& childList : CrossDocChildLists()) {
7582 for (nsIFrame* child : childList.mList) {
7583 child->ClearInvalidationStateBits();
7588 RemoveStateBits(NS_FRAME_NEEDS_PAINT | NS_FRAME_DESCENDANT_NEEDS_PAINT |
7589 NS_FRAME_ALL_DESCENDANTS_NEED_PAINT);
7592 bool HasRetainedDataFor(const nsIFrame* aFrame, uint32_t aDisplayItemKey) {
7593 if (RefPtr<WebRenderUserData> data =
7594 GetWebRenderUserData<WebRenderFallbackData>(aFrame,
7595 aDisplayItemKey)) {
7596 return true;
7599 return false;
7602 void nsIFrame::InvalidateFrame(uint32_t aDisplayItemKey,
7603 bool aRebuildDisplayItems /* = true */) {
7604 bool hasDisplayItem =
7605 !aDisplayItemKey || HasRetainedDataFor(this, aDisplayItemKey);
7606 InvalidateFrameInternal(this, hasDisplayItem, aRebuildDisplayItems);
7609 void nsIFrame::InvalidateFrameWithRect(const nsRect& aRect,
7610 uint32_t aDisplayItemKey,
7611 bool aRebuildDisplayItems /* = true */) {
7612 if (aRect.IsEmpty()) {
7613 return;
7615 bool hasDisplayItem =
7616 !aDisplayItemKey || HasRetainedDataFor(this, aDisplayItemKey);
7617 bool alreadyInvalid = false;
7618 if (!HasAnyStateBits(NS_FRAME_NEEDS_PAINT)) {
7619 InvalidateFrameInternal(this, hasDisplayItem, aRebuildDisplayItems);
7620 } else {
7621 alreadyInvalid = true;
7624 if (!hasDisplayItem) {
7625 return;
7628 nsRect* rect;
7629 if (HasAnyStateBits(NS_FRAME_HAS_INVALID_RECT)) {
7630 rect = GetProperty(InvalidationRect());
7631 MOZ_ASSERT(rect);
7632 } else {
7633 if (alreadyInvalid) {
7634 return;
7636 rect = new nsRect();
7637 AddProperty(InvalidationRect(), rect);
7638 AddStateBits(NS_FRAME_HAS_INVALID_RECT);
7641 *rect = rect->Union(aRect);
7644 /*static*/
7645 uint8_t nsIFrame::sLayerIsPrerenderedDataKey;
7647 bool nsIFrame::IsInvalid(nsRect& aRect) {
7648 if (!HasAnyStateBits(NS_FRAME_NEEDS_PAINT)) {
7649 return false;
7652 if (HasAnyStateBits(NS_FRAME_HAS_INVALID_RECT)) {
7653 nsRect* rect = GetProperty(InvalidationRect());
7654 NS_ASSERTION(
7655 rect, "Must have an invalid rect if NS_FRAME_HAS_INVALID_RECT is set!");
7656 aRect = *rect;
7657 } else {
7658 aRect.SetEmpty();
7660 return true;
7663 void nsIFrame::SchedulePaint(PaintType aType, bool aFrameChanged) {
7664 if (PresShell()->IsPaintingSuppressed()) {
7665 // We can't have any display items yet, and when we unsuppress we will
7666 // invalidate the root frame.
7667 return;
7669 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(this);
7670 InvalidateRenderingObservers(displayRoot, this, aFrameChanged);
7671 SchedulePaintInternal(displayRoot, this, aType);
7674 void nsIFrame::SchedulePaintWithoutInvalidatingObservers(PaintType aType) {
7675 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(this);
7676 SchedulePaintInternal(displayRoot, this, aType);
7679 void nsIFrame::InvalidateLayer(DisplayItemType aDisplayItemKey,
7680 const nsIntRect* aDamageRect,
7681 const nsRect* aFrameDamageRect,
7682 uint32_t aFlags /* = 0 */) {
7683 NS_ASSERTION(aDisplayItemKey > DisplayItemType::TYPE_ZERO, "Need a key");
7685 nsIFrame* displayRoot = nsLayoutUtils::GetDisplayRootFrame(this);
7686 InvalidateRenderingObservers(displayRoot, this, false);
7688 // Check if frame supports WebRender's async update
7689 if ((aFlags & UPDATE_IS_ASYNC) &&
7690 WebRenderUserData::SupportsAsyncUpdate(this)) {
7691 // WebRender does not use layer, then return nullptr.
7692 return;
7695 if (aFrameDamageRect && aFrameDamageRect->IsEmpty()) {
7696 return;
7699 // In the bug 930056, dialer app startup but not shown on the
7700 // screen because sometimes we don't have any retainned data
7701 // for remote type displayitem and thus Repaint event is not
7702 // triggered. So, always invalidate in this case.
7703 DisplayItemType displayItemKey = aDisplayItemKey;
7704 if (aDisplayItemKey == DisplayItemType::TYPE_REMOTE) {
7705 displayItemKey = DisplayItemType::TYPE_ZERO;
7708 if (aFrameDamageRect) {
7709 InvalidateFrameWithRect(*aFrameDamageRect,
7710 static_cast<uint32_t>(displayItemKey));
7711 } else {
7712 InvalidateFrame(static_cast<uint32_t>(displayItemKey));
7716 static nsRect ComputeEffectsRect(nsIFrame* aFrame, const nsRect& aOverflowRect,
7717 const nsSize& aNewSize) {
7718 nsRect r = aOverflowRect;
7720 if (aFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
7721 // For SVG frames, we only need to account for filters.
7722 // TODO: We could also take account of clipPath and mask to reduce the
7723 // ink overflow, but that's not essential.
7724 if (aFrame->StyleEffects()->HasFilters()) {
7725 SetOrUpdateRectValuedProperty(aFrame, nsIFrame::PreEffectsBBoxProperty(),
7727 r = SVGUtils::GetPostFilterInkOverflowRect(aFrame, aOverflowRect);
7729 return r;
7732 // box-shadow
7733 r.UnionRect(r, nsLayoutUtils::GetBoxShadowRectForFrame(aFrame, aNewSize));
7735 // border-image-outset.
7736 // We need to include border-image-outset because it can cause the
7737 // border image to be drawn beyond the border box.
7739 // (1) It's important we not check whether there's a border-image
7740 // since the style hint for a change in border image doesn't cause
7741 // reflow, and that's probably more important than optimizing the
7742 // overflow areas for the silly case of border-image-outset without
7743 // border-image
7744 // (2) It's important that we not check whether the border-image
7745 // is actually loaded, since that would require us to reflow when
7746 // the image loads.
7747 const nsStyleBorder* styleBorder = aFrame->StyleBorder();
7748 nsMargin outsetMargin = styleBorder->GetImageOutset();
7750 if (outsetMargin != nsMargin(0, 0, 0, 0)) {
7751 nsRect outsetRect(nsPoint(0, 0), aNewSize);
7752 outsetRect.Inflate(outsetMargin);
7753 r.UnionRect(r, outsetRect);
7756 // Note that we don't remove the outlineInnerRect if a frame loses outline
7757 // style. That would require an extra property lookup for every frame,
7758 // or a new frame state bit to track whether a property had been stored,
7759 // or something like that. It's not worth doing that here. At most it's
7760 // only one heap-allocated rect per frame and it will be cleaned up when
7761 // the frame dies.
7763 if (SVGIntegrationUtils::UsingOverflowAffectingEffects(aFrame)) {
7764 SetOrUpdateRectValuedProperty(aFrame, nsIFrame::PreEffectsBBoxProperty(),
7766 r = SVGIntegrationUtils::ComputePostEffectsInkOverflowRect(aFrame, r);
7769 return r;
7772 void nsIFrame::SetPosition(const nsPoint& aPt) {
7773 if (mRect.TopLeft() == aPt) {
7774 return;
7776 mRect.MoveTo(aPt);
7777 MarkNeedsDisplayItemRebuild();
7780 void nsIFrame::MovePositionBy(const nsPoint& aTranslation) {
7781 nsPoint position = GetNormalPosition() + aTranslation;
7783 const nsMargin* computedOffsets = nullptr;
7784 if (IsRelativelyOrStickyPositioned()) {
7785 computedOffsets = GetProperty(nsIFrame::ComputedOffsetProperty());
7787 ReflowInput::ApplyRelativePositioning(
7788 this, computedOffsets ? *computedOffsets : nsMargin(), &position);
7789 SetPosition(position);
7792 nsRect nsIFrame::GetNormalRect() const {
7793 // It might be faster to first check
7794 // StyleDisplay()->IsRelativelyPositionedStyle().
7795 bool hasProperty;
7796 nsPoint normalPosition = GetProperty(NormalPositionProperty(), &hasProperty);
7797 if (hasProperty) {
7798 return nsRect(normalPosition, GetSize());
7800 return GetRect();
7803 nsRect nsIFrame::GetBoundingClientRect() {
7804 return nsLayoutUtils::GetAllInFlowRectsUnion(
7805 this, nsLayoutUtils::GetContainingBlockForClientRect(this),
7806 nsLayoutUtils::RECTS_ACCOUNT_FOR_TRANSFORMS);
7809 nsPoint nsIFrame::GetPositionIgnoringScrolling() const {
7810 return GetParent() ? GetParent()->GetPositionOfChildIgnoringScrolling(this)
7811 : GetPosition();
7814 nsRect nsIFrame::GetOverflowRect(OverflowType aType) const {
7815 // Note that in some cases the overflow area might not have been
7816 // updated (yet) to reflect any outline set on the frame or the area
7817 // of child frames. That's OK because any reflow that updates these
7818 // areas will invalidate the appropriate area, so any (mis)uses of
7819 // this method will be fixed up.
7821 if (mOverflow.mType == OverflowStorageType::Large) {
7822 // there is an overflow rect, and it's not stored as deltas but as
7823 // a separately-allocated rect
7824 return GetOverflowAreasProperty()->Overflow(aType);
7827 if (aType == OverflowType::Ink &&
7828 mOverflow.mType != OverflowStorageType::None) {
7829 return InkOverflowFromDeltas();
7832 return GetRectRelativeToSelf();
7835 OverflowAreas nsIFrame::GetOverflowAreas() const {
7836 if (mOverflow.mType == OverflowStorageType::Large) {
7837 // there is an overflow rect, and it's not stored as deltas but as
7838 // a separately-allocated rect
7839 return *GetOverflowAreasProperty();
7842 return OverflowAreas(InkOverflowFromDeltas(),
7843 nsRect(nsPoint(0, 0), GetSize()));
7846 OverflowAreas nsIFrame::GetOverflowAreasRelativeToSelf() const {
7847 if (IsTransformed()) {
7848 if (OverflowAreas* preTransformOverflows =
7849 GetProperty(PreTransformOverflowAreasProperty())) {
7850 return *preTransformOverflows;
7853 return GetOverflowAreas();
7856 OverflowAreas nsIFrame::GetOverflowAreasRelativeToParent() const {
7857 return GetOverflowAreas() + GetPosition();
7860 OverflowAreas nsIFrame::GetActualAndNormalOverflowAreasRelativeToParent()
7861 const {
7862 if (MOZ_LIKELY(!IsRelativelyOrStickyPositioned())) {
7863 return GetOverflowAreasRelativeToParent();
7866 const OverflowAreas overflows = GetOverflowAreas();
7867 OverflowAreas actualAndNormalOverflows = overflows + GetPosition();
7868 actualAndNormalOverflows.UnionWith(overflows + GetNormalPosition());
7869 return actualAndNormalOverflows;
7872 nsRect nsIFrame::ScrollableOverflowRectRelativeToParent() const {
7873 return ScrollableOverflowRect() + GetPosition();
7876 nsRect nsIFrame::InkOverflowRectRelativeToParent() const {
7877 return InkOverflowRect() + GetPosition();
7880 nsRect nsIFrame::ScrollableOverflowRectRelativeToSelf() const {
7881 if (IsTransformed()) {
7882 if (OverflowAreas* preTransformOverflows =
7883 GetProperty(PreTransformOverflowAreasProperty())) {
7884 return preTransformOverflows->ScrollableOverflow();
7887 return ScrollableOverflowRect();
7890 nsRect nsIFrame::InkOverflowRectRelativeToSelf() const {
7891 if (IsTransformed()) {
7892 if (OverflowAreas* preTransformOverflows =
7893 GetProperty(PreTransformOverflowAreasProperty())) {
7894 return preTransformOverflows->InkOverflow();
7897 return InkOverflowRect();
7900 nsRect nsIFrame::PreEffectsInkOverflowRect() const {
7901 nsRect* r = GetProperty(nsIFrame::PreEffectsBBoxProperty());
7902 return r ? *r : InkOverflowRectRelativeToSelf();
7905 bool nsIFrame::UpdateOverflow() {
7906 MOZ_ASSERT(FrameMaintainsOverflow(),
7907 "Non-display SVG do not maintain ink overflow rects");
7909 nsRect rect(nsPoint(0, 0), GetSize());
7910 OverflowAreas overflowAreas(rect, rect);
7912 if (!ComputeCustomOverflow(overflowAreas)) {
7913 // If updating overflow wasn't supported by this frame, then it should
7914 // have scheduled any necessary reflows. We can return false to say nothing
7915 // changed, and wait for reflow to correct it.
7916 return false;
7919 UnionChildOverflow(overflowAreas);
7921 if (FinishAndStoreOverflow(overflowAreas, GetSize())) {
7922 if (nsView* view = GetView()) {
7923 // Make sure the frame's view is properly sized.
7924 nsViewManager* vm = view->GetViewManager();
7925 vm->ResizeView(view, overflowAreas.InkOverflow(), true);
7928 return true;
7931 // Frames that combine their 3d transform with their ancestors
7932 // only compute a pre-transform overflow rect, and then contribute
7933 // to the normal overflow rect of the preserve-3d root. Always return
7934 // true here so that we propagate changes up to the root for final
7935 // calculation.
7936 return Combines3DTransformWithAncestors();
7939 /* virtual */
7940 bool nsIFrame::ComputeCustomOverflow(OverflowAreas& aOverflowAreas) {
7941 return true;
7944 bool nsIFrame::DoesClipChildrenInBothAxes() const {
7945 nsIScrollableFrame* sf = do_QueryFrame(this);
7946 const nsStyleDisplay* display = StyleDisplay();
7947 return sf || (display->mOverflowX == StyleOverflow::Clip &&
7948 display->mOverflowY == StyleOverflow::Clip);
7951 /* virtual */
7952 void nsIFrame::UnionChildOverflow(OverflowAreas& aOverflowAreas) {
7953 if (!DoesClipChildrenInBothAxes()) {
7954 nsLayoutUtils::UnionChildOverflow(this, aOverflowAreas);
7958 // Return true if this form control element's preferred size property (but not
7959 // percentage max size property) contains a percentage value that should be
7960 // resolved against zero when calculating its min-content contribution in the
7961 // corresponding axis.
7963 // For proper replaced elements, the percentage value in both their max size
7964 // property or preferred size property should be resolved against zero. This is
7965 // handled in IsPercentageResolvedAgainstZero().
7966 inline static bool FormControlShrinksForPercentSize(const nsIFrame* aFrame) {
7967 if (!aFrame->IsFrameOfType(nsIFrame::eReplaced)) {
7968 // Quick test to reject most frames.
7969 return false;
7972 LayoutFrameType fType = aFrame->Type();
7973 if (fType == LayoutFrameType::Meter || fType == LayoutFrameType::Progress ||
7974 fType == LayoutFrameType::Range) {
7975 // progress, meter and range do have this shrinking behavior
7976 // FIXME: Maybe these should be nsIFormControlFrame?
7977 return true;
7980 if (!static_cast<nsIFormControlFrame*>(do_QueryFrame(aFrame))) {
7981 // Not a form control. This includes fieldsets, which do not
7982 // shrink.
7983 return false;
7986 if (fType == LayoutFrameType::GfxButtonControl ||
7987 fType == LayoutFrameType::HTMLButtonControl) {
7988 // Buttons don't have this shrinking behavior. (Note that color
7989 // inputs do, even though they inherit from button, so we can't use
7990 // do_QueryFrame here.)
7991 return false;
7994 return true;
7997 bool nsIFrame::IsPercentageResolvedAgainstZero(
7998 const StyleSize& aStyleSize, const StyleMaxSize& aStyleMaxSize) const {
7999 const bool sizeHasPercent = aStyleSize.HasPercent();
8000 return ((sizeHasPercent || aStyleMaxSize.HasPercent()) &&
8001 IsFrameOfType(nsIFrame::eReplacedSizing)) ||
8002 (sizeHasPercent && FormControlShrinksForPercentSize(this));
8005 // Summary of the Cyclic-Percentage Intrinsic Size Contribution Rules:
8007 // Element Type | Replaced | Non-replaced
8008 // Contribution Type | min-content max-content | min-content max-content
8009 // ---------------------------------------------------------------------------
8010 // min size | zero zero | zero zero
8011 // max & preferred size | zero initial | initial initial
8013 // https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
8014 bool nsIFrame::IsPercentageResolvedAgainstZero(const LengthPercentage& aSize,
8015 SizeProperty aProperty) const {
8016 // Early return to avoid calling the virtual function, IsFrameOfType().
8017 if (aProperty == SizeProperty::MinSize) {
8018 return true;
8021 const bool hasPercentOnReplaced =
8022 aSize.HasPercent() && IsFrameOfType(nsIFrame::eReplacedSizing);
8023 if (aProperty == SizeProperty::MaxSize) {
8024 return hasPercentOnReplaced;
8027 MOZ_ASSERT(aProperty == SizeProperty::Size);
8028 return hasPercentOnReplaced ||
8029 (aSize.HasPercent() && FormControlShrinksForPercentSize(this));
8032 bool nsIFrame::IsBlockWrapper() const {
8033 auto pseudoType = Style()->GetPseudoType();
8034 return pseudoType == PseudoStyleType::mozBlockInsideInlineWrapper ||
8035 pseudoType == PseudoStyleType::buttonContent ||
8036 pseudoType == PseudoStyleType::cellContent ||
8037 pseudoType == PseudoStyleType::columnSpanWrapper;
8040 bool nsIFrame::IsBlockFrameOrSubclass() const {
8041 const nsBlockFrame* thisAsBlock = do_QueryFrame(this);
8042 return !!thisAsBlock;
8045 bool nsIFrame::IsImageFrameOrSubclass() const {
8046 const nsImageFrame* asImage = do_QueryFrame(this);
8047 return !!asImage;
8050 bool nsIFrame::IsSubgrid() const {
8051 return IsGridContainerFrame() &&
8052 static_cast<const nsGridContainerFrame*>(this)->IsSubgrid();
8055 static nsIFrame* GetNearestBlockContainer(nsIFrame* frame) {
8056 while (!frame->IsBlockContainer()) {
8057 frame = frame->GetParent();
8058 NS_ASSERTION(
8059 frame,
8060 "How come we got to the root frame without seeing a containing block?");
8062 return frame;
8065 bool nsIFrame::IsBlockContainer() const {
8066 // The block wrappers we use to wrap blocks inside inlines aren't
8067 // described in the CSS spec. We need to make them not be containing
8068 // blocks.
8069 // Since the parent of such a block is either a normal block or
8070 // another such pseudo, this shouldn't cause anything bad to happen.
8071 // Also the anonymous blocks inside table cells are not containing blocks.
8073 // If we ever start skipping table row groups from being containing blocks,
8074 // you need to remove the StickyScrollContainer hack referencing bug 1421660.
8075 return !IsFrameOfType(nsIFrame::eLineParticipant) && !IsBlockWrapper() &&
8076 !IsSubgrid() &&
8077 // Table rows are not containing blocks either
8078 !IsTableRowFrame();
8081 nsIFrame* nsIFrame::GetContainingBlock(
8082 uint32_t aFlags, const nsStyleDisplay* aStyleDisplay) const {
8083 MOZ_ASSERT(aStyleDisplay == StyleDisplay());
8085 // Keep this in sync with MightBeContainingBlockFor in ReflowInput.cpp.
8087 if (!GetParent()) {
8088 return nullptr;
8090 // MathML frames might have absolute positioning style, but they would
8091 // still be in-flow. So we have to check to make sure that the frame
8092 // is really out-of-flow too.
8093 nsIFrame* f;
8094 if (IsAbsolutelyPositioned(aStyleDisplay)) {
8095 f = GetParent(); // the parent is always the containing block
8096 } else {
8097 f = GetNearestBlockContainer(GetParent());
8100 if (aFlags & SKIP_SCROLLED_FRAME && f &&
8101 f->Style()->GetPseudoType() == PseudoStyleType::scrolledContent) {
8102 f = f->GetParent();
8104 return f;
8107 #ifdef DEBUG_FRAME_DUMP
8109 Maybe<uint32_t> nsIFrame::ContentIndexInContainer(const nsIFrame* aFrame) {
8110 if (nsIContent* content = aFrame->GetContent()) {
8111 return content->ComputeIndexInParentContent();
8113 return Nothing();
8116 nsAutoCString nsIFrame::ListTag() const {
8117 nsAutoString tmp;
8118 GetFrameName(tmp);
8120 nsAutoCString tag;
8121 tag += NS_ConvertUTF16toUTF8(tmp);
8122 tag += nsPrintfCString("@%p", static_cast<const void*>(this));
8123 return tag;
8126 std::string nsIFrame::ConvertToString(const LogicalRect& aRect,
8127 const WritingMode aWM, ListFlags aFlags) {
8128 if (aFlags.contains(ListFlag::DisplayInCSSPixels)) {
8129 // Abuse CSSRect to store all LogicalRect's dimensions in CSS pixels.
8130 return ToString(mozilla::CSSRect(CSSPixel::FromAppUnits(aRect.IStart(aWM)),
8131 CSSPixel::FromAppUnits(aRect.BStart(aWM)),
8132 CSSPixel::FromAppUnits(aRect.ISize(aWM)),
8133 CSSPixel::FromAppUnits(aRect.BSize(aWM))));
8135 return ToString(aRect);
8138 std::string nsIFrame::ConvertToString(const LogicalSize& aSize,
8139 const WritingMode aWM, ListFlags aFlags) {
8140 if (aFlags.contains(ListFlag::DisplayInCSSPixels)) {
8141 // Abuse CSSSize to store all LogicalSize's dimensions in CSS pixels.
8142 return ToString(CSSSize(CSSPixel::FromAppUnits(aSize.ISize(aWM)),
8143 CSSPixel::FromAppUnits(aSize.BSize(aWM))));
8145 return ToString(aSize);
8148 // Debugging
8149 void nsIFrame::ListGeneric(nsACString& aTo, const char* aPrefix,
8150 ListFlags aFlags) const {
8151 aTo += aPrefix;
8152 aTo += ListTag();
8153 if (HasView()) {
8154 aTo += nsPrintfCString(" [view=%p]", static_cast<void*>(GetView()));
8156 if (GetParent()) {
8157 aTo += nsPrintfCString(" parent=%p", static_cast<void*>(GetParent()));
8159 if (GetNextSibling()) {
8160 aTo += nsPrintfCString(" next=%p", static_cast<void*>(GetNextSibling()));
8162 if (GetPrevContinuation()) {
8163 bool fluid = GetPrevInFlow() == GetPrevContinuation();
8164 aTo += nsPrintfCString(" prev-%s=%p", fluid ? "in-flow" : "continuation",
8165 static_cast<void*>(GetPrevContinuation()));
8167 if (GetNextContinuation()) {
8168 bool fluid = GetNextInFlow() == GetNextContinuation();
8169 aTo += nsPrintfCString(" next-%s=%p", fluid ? "in-flow" : "continuation",
8170 static_cast<void*>(GetNextContinuation()));
8172 if (const nsAtom* const autoPageValue =
8173 GetProperty(AutoPageValueProperty())) {
8174 aTo += " AutoPage=";
8175 aTo += nsAtomCString(autoPageValue);
8177 if (const nsIFrame::PageValues* const pageValues =
8178 GetProperty(PageValuesProperty())) {
8179 aTo += " PageValues={";
8180 if (pageValues->mStartPageValue) {
8181 aTo += nsAtomCString(pageValues->mStartPageValue);
8182 } else {
8183 aTo += "<null>";
8185 aTo += ", ";
8186 if (pageValues->mEndPageValue) {
8187 aTo += nsAtomCString(pageValues->mEndPageValue);
8188 } else {
8189 aTo += "<null>";
8191 aTo += "}";
8193 void* IBsibling = GetProperty(IBSplitSibling());
8194 if (IBsibling) {
8195 aTo += nsPrintfCString(" IBSplitSibling=%p", IBsibling);
8197 void* IBprevsibling = GetProperty(IBSplitPrevSibling());
8198 if (IBprevsibling) {
8199 aTo += nsPrintfCString(" IBSplitPrevSibling=%p", IBprevsibling);
8201 if (nsLayoutUtils::FontSizeInflationEnabled(PresContext())) {
8202 if (HasAnyStateBits(NS_FRAME_FONT_INFLATION_FLOW_ROOT)) {
8203 aTo += nsPrintfCString(" FFR");
8204 if (nsFontInflationData* data =
8205 nsFontInflationData::FindFontInflationDataFor(this)) {
8206 aTo += nsPrintfCString(
8207 ",enabled=%s,UIS=%s", data->InflationEnabled() ? "yes" : "no",
8208 ConvertToString(data->UsableISize(), aFlags).c_str());
8211 if (HasAnyStateBits(NS_FRAME_FONT_INFLATION_CONTAINER)) {
8212 aTo += nsPrintfCString(" FIC");
8214 aTo += nsPrintfCString(" FI=%f", nsLayoutUtils::FontSizeInflationFor(this));
8216 aTo += nsPrintfCString(" %s", ConvertToString(mRect, aFlags).c_str());
8218 mozilla::WritingMode wm = GetWritingMode();
8219 if (wm.IsVertical() || wm.IsBidiRTL()) {
8220 aTo +=
8221 nsPrintfCString(" wm=%s logical-size=(%s)", ToString(wm).c_str(),
8222 ConvertToString(GetLogicalSize(), wm, aFlags).c_str());
8225 nsIFrame* parent = GetParent();
8226 if (parent) {
8227 WritingMode pWM = parent->GetWritingMode();
8228 if (pWM.IsVertical() || pWM.IsBidiRTL()) {
8229 nsSize containerSize = parent->mRect.Size();
8230 LogicalRect lr(pWM, mRect, containerSize);
8231 aTo += nsPrintfCString(" parent-wm=%s cs=(%s) logical-rect=%s",
8232 ToString(pWM).c_str(),
8233 ConvertToString(containerSize, aFlags).c_str(),
8234 ConvertToString(lr, pWM, aFlags).c_str());
8237 nsIFrame* f = const_cast<nsIFrame*>(this);
8238 if (f->HasOverflowAreas()) {
8239 nsRect io = f->InkOverflowRect();
8240 if (!io.IsEqualEdges(mRect)) {
8241 aTo += nsPrintfCString(" ink-overflow=%s",
8242 ConvertToString(io, aFlags).c_str());
8244 nsRect so = f->ScrollableOverflowRect();
8245 if (!so.IsEqualEdges(mRect)) {
8246 aTo += nsPrintfCString(" scr-overflow=%s",
8247 ConvertToString(so, aFlags).c_str());
8250 if (OverflowAreas* preTransformOverflows =
8251 f->GetProperty(PreTransformOverflowAreasProperty())) {
8252 nsRect io = preTransformOverflows->InkOverflow();
8253 if (!io.IsEqualEdges(mRect) &&
8254 (!f->HasOverflowAreas() || !io.IsEqualEdges(f->InkOverflowRect()))) {
8255 aTo += nsPrintfCString(" pre-transform-ink-overflow=%s",
8256 ConvertToString(io, aFlags).c_str());
8258 nsRect so = preTransformOverflows->ScrollableOverflow();
8259 if (!so.IsEqualEdges(mRect) &&
8260 (!f->HasOverflowAreas() ||
8261 !so.IsEqualEdges(f->ScrollableOverflowRect()))) {
8262 aTo += nsPrintfCString(" pre-transform-scr-overflow=%s",
8263 ConvertToString(so, aFlags).c_str());
8266 bool hasNormalPosition;
8267 nsPoint normalPosition = GetNormalPosition(&hasNormalPosition);
8268 if (hasNormalPosition) {
8269 aTo += nsPrintfCString(" normal-position=%s",
8270 ConvertToString(normalPosition, aFlags).c_str());
8272 if (HasProperty(BidiDataProperty())) {
8273 FrameBidiData bidi = GetBidiData();
8274 aTo += nsPrintfCString(" bidi(%d,%d,%d)", bidi.baseLevel.Value(),
8275 bidi.embeddingLevel.Value(),
8276 bidi.precedingControl.Value());
8278 if (IsTransformed()) {
8279 aTo += nsPrintfCString(" transformed");
8281 if (ChildrenHavePerspective()) {
8282 aTo += nsPrintfCString(" perspective");
8284 if (Extend3DContext()) {
8285 aTo += nsPrintfCString(" extend-3d");
8287 if (Combines3DTransformWithAncestors()) {
8288 aTo += nsPrintfCString(" combines-3d-transform-with-ancestors");
8290 if (mContent) {
8291 aTo += nsPrintfCString(" [content=%p]", static_cast<void*>(mContent));
8293 aTo += nsPrintfCString(" [cs=%p", static_cast<void*>(mComputedStyle));
8294 if (mComputedStyle) {
8295 auto pseudoType = mComputedStyle->GetPseudoType();
8296 aTo += ToString(pseudoType).c_str();
8298 aTo += "]";
8300 auto contentVisibility = StyleDisplay()->ContentVisibility(*this);
8301 if (contentVisibility != StyleContentVisibility::Visible) {
8302 aTo += nsPrintfCString(" [content-visibility=");
8303 if (contentVisibility == StyleContentVisibility::Auto) {
8304 aTo += "auto, "_ns;
8305 } else if (contentVisibility == StyleContentVisibility::Hidden) {
8306 aTo += "hiden, "_ns;
8309 if (HidesContent()) {
8310 aTo += "HidesContent=hidden"_ns;
8311 } else {
8312 aTo += "HidesContent=visibile"_ns;
8314 aTo += "]";
8317 if (IsFrameModified()) {
8318 aTo += nsPrintfCString(" modified");
8321 if (HasModifiedDescendants()) {
8322 aTo += nsPrintfCString(" has-modified-descendants");
8326 void nsIFrame::List(FILE* out, const char* aPrefix, ListFlags aFlags) const {
8327 nsCString str;
8328 ListGeneric(str, aPrefix, aFlags);
8329 fprintf_stderr(out, "%s\n", str.get());
8332 void nsIFrame::ListTextRuns(FILE* out) const {
8333 nsTHashSet<const void*> seen;
8334 ListTextRuns(out, seen);
8337 void nsIFrame::ListTextRuns(FILE* out, nsTHashSet<const void*>& aSeen) const {
8338 for (const auto& childList : ChildLists()) {
8339 for (const nsIFrame* kid : childList.mList) {
8340 kid->ListTextRuns(out, aSeen);
8345 void nsIFrame::ListMatchedRules(FILE* out, const char* aPrefix) const {
8346 nsTArray<const StyleLockedStyleRule*> rawRuleList;
8347 Servo_ComputedValues_GetStyleRuleList(mComputedStyle, &rawRuleList);
8348 for (const StyleLockedStyleRule* rawRule : rawRuleList) {
8349 nsAutoCString ruleText;
8350 Servo_StyleRule_GetCssText(rawRule, &ruleText);
8351 fprintf_stderr(out, "%s%s\n", aPrefix, ruleText.get());
8355 void nsIFrame::ListWithMatchedRules(FILE* out, const char* aPrefix) const {
8356 fprintf_stderr(out, "%s%s\n", aPrefix, ListTag().get());
8358 nsCString rulePrefix;
8359 rulePrefix += aPrefix;
8360 rulePrefix += " ";
8361 ListMatchedRules(out, rulePrefix.get());
8364 nsresult nsIFrame::GetFrameName(nsAString& aResult) const {
8365 return MakeFrameName(u"Frame"_ns, aResult);
8368 nsresult nsIFrame::MakeFrameName(const nsAString& aType,
8369 nsAString& aResult) const {
8370 aResult = aType;
8371 if (mContent && !mContent->IsText()) {
8372 nsAutoString buf;
8373 mContent->NodeInfo()->NameAtom()->ToString(buf);
8374 if (nsAtom* id = mContent->GetID()) {
8375 buf.AppendLiteral(" id=");
8376 buf.Append(nsDependentAtomString(id));
8378 if (IsSubDocumentFrame()) {
8379 nsAutoString src;
8380 mContent->AsElement()->GetAttr(nsGkAtoms::src, src);
8381 buf.AppendLiteral(" src=");
8382 buf.Append(src);
8384 aResult.Append('(');
8385 aResult.Append(buf);
8386 aResult.Append(')');
8388 aResult.Append('(');
8389 Maybe<uint32_t> index = ContentIndexInContainer(this);
8390 if (index.isSome()) {
8391 aResult.AppendInt(*index);
8392 } else {
8393 aResult.AppendInt(-1);
8395 aResult.Append(')');
8396 return NS_OK;
8399 void nsIFrame::DumpFrameTree() const {
8400 PresShell()->GetRootFrame()->List(stderr);
8403 void nsIFrame::DumpFrameTreeInCSSPixels() const {
8404 PresShell()->GetRootFrame()->List(stderr, "", ListFlag::DisplayInCSSPixels);
8407 void nsIFrame::DumpFrameTreeLimited() const { List(stderr); }
8408 void nsIFrame::DumpFrameTreeLimitedInCSSPixels() const {
8409 List(stderr, "", ListFlag::DisplayInCSSPixels);
8412 #endif
8414 bool nsIFrame::IsVisibleForPainting() const {
8415 return StyleVisibility()->IsVisible();
8418 bool nsIFrame::IsVisibleOrCollapsedForPainting() const {
8419 return StyleVisibility()->IsVisibleOrCollapsed();
8422 /* virtual */
8423 bool nsIFrame::IsEmpty() {
8424 return IsHiddenByContentVisibilityOfInFlowParentForLayout();
8427 bool nsIFrame::CachedIsEmpty() {
8428 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_DIRTY) ||
8429 IsHiddenByContentVisibilityOfInFlowParentForLayout(),
8430 "Must only be called on reflowed lines or those hidden by "
8431 "content-visibility.");
8432 return IsEmpty();
8435 /* virtual */
8436 bool nsIFrame::IsSelfEmpty() {
8437 return IsHiddenByContentVisibilityOfInFlowParentForLayout();
8440 nsresult nsIFrame::GetSelectionController(nsPresContext* aPresContext,
8441 nsISelectionController** aSelCon) {
8442 if (!aPresContext || !aSelCon) return NS_ERROR_INVALID_ARG;
8444 nsIFrame* frame = this;
8445 while (frame && frame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION)) {
8446 nsITextControlFrame* tcf = do_QueryFrame(frame);
8447 if (tcf) {
8448 return tcf->GetOwnedSelectionController(aSelCon);
8450 frame = frame->GetParent();
8453 *aSelCon = do_AddRef(aPresContext->PresShell()).take();
8454 return NS_OK;
8457 already_AddRefed<nsFrameSelection> nsIFrame::GetFrameSelection() {
8458 RefPtr<nsFrameSelection> fs =
8459 const_cast<nsFrameSelection*>(GetConstFrameSelection());
8460 return fs.forget();
8463 const nsFrameSelection* nsIFrame::GetConstFrameSelection() const {
8464 nsIFrame* frame = const_cast<nsIFrame*>(this);
8465 while (frame && frame->HasAnyStateBits(NS_FRAME_INDEPENDENT_SELECTION)) {
8466 nsITextControlFrame* tcf = do_QueryFrame(frame);
8467 if (tcf) {
8468 return tcf->GetOwnedFrameSelection();
8470 frame = frame->GetParent();
8473 return PresShell()->ConstFrameSelection();
8476 bool nsIFrame::IsFrameSelected() const {
8477 NS_ASSERTION(!GetContent() || GetContent()->IsMaybeSelected(),
8478 "use the public IsSelected() instead");
8479 return GetContent()->IsSelected(0, GetContent()->GetChildCount());
8482 nsresult nsIFrame::GetPointFromOffset(int32_t inOffset, nsPoint* outPoint) {
8483 MOZ_ASSERT(outPoint != nullptr, "Null parameter");
8484 nsRect contentRect = GetContentRectRelativeToSelf();
8485 nsPoint pt = contentRect.TopLeft();
8486 if (mContent) {
8487 nsIContent* newContent = mContent->GetParent();
8488 if (newContent) {
8489 const int32_t newOffset = newContent->ComputeIndexOf_Deprecated(mContent);
8491 // Find the direction of the frame from the EmbeddingLevelProperty,
8492 // which is the resolved bidi level set in
8493 // nsBidiPresUtils::ResolveParagraph (odd levels = right-to-left).
8494 // If the embedding level isn't set, just use the CSS direction
8495 // property.
8496 bool hasBidiData;
8497 FrameBidiData bidiData = GetProperty(BidiDataProperty(), &hasBidiData);
8498 bool isRTL = hasBidiData
8499 ? bidiData.embeddingLevel.IsRTL()
8500 : StyleVisibility()->mDirection == StyleDirection::Rtl;
8501 if ((!isRTL && inOffset > newOffset) ||
8502 (isRTL && inOffset <= newOffset)) {
8503 pt = contentRect.TopRight();
8507 *outPoint = pt;
8508 return NS_OK;
8511 nsresult nsIFrame::GetCharacterRectsInRange(int32_t aInOffset, int32_t aLength,
8512 nsTArray<nsRect>& aOutRect) {
8513 /* no text */
8514 return NS_ERROR_FAILURE;
8517 nsresult nsIFrame::GetChildFrameContainingOffset(int32_t inContentOffset,
8518 bool inHint,
8519 int32_t* outFrameContentOffset,
8520 nsIFrame** outChildFrame) {
8521 MOZ_ASSERT(outChildFrame && outFrameContentOffset, "Null parameter");
8522 *outFrameContentOffset = (int32_t)inHint;
8523 // the best frame to reflect any given offset would be a visible frame if
8524 // possible i.e. we are looking for a valid frame to place the blinking caret
8525 nsRect rect = GetRect();
8526 if (!rect.width || !rect.height) {
8527 // if we have a 0 width or height then lets look for another frame that
8528 // possibly has the same content. If we have no frames in flow then just
8529 // let us return 'this' frame
8530 nsIFrame* nextFlow = GetNextInFlow();
8531 if (nextFlow)
8532 return nextFlow->GetChildFrameContainingOffset(
8533 inContentOffset, inHint, outFrameContentOffset, outChildFrame);
8535 *outChildFrame = this;
8536 return NS_OK;
8540 // What I've pieced together about this routine:
8541 // Starting with a block frame (from which a line frame can be gotten)
8542 // and a line number, drill down and get the first/last selectable
8543 // frame on that line, depending on aPos->mDirection.
8544 // aOutSideLimit != 0 means ignore aLineStart, instead work from
8545 // the end (if > 0) or beginning (if < 0).
8547 static nsresult GetNextPrevLineFromBlockFrame(PeekOffsetStruct* aPos,
8548 nsIFrame* aBlockFrame,
8549 int32_t aLineStart,
8550 int8_t aOutSideLimit) {
8551 MOZ_ASSERT(aPos);
8552 MOZ_ASSERT(aBlockFrame);
8554 nsPresContext* pc = aBlockFrame->PresContext();
8556 // magic numbers: aLineStart will be -1 for end of block, 0 will be start of
8557 // block.
8559 aPos->mResultFrame = nullptr;
8560 aPos->mResultContent = nullptr;
8561 aPos->mAttach = aPos->mDirection == eDirNext ? CARET_ASSOCIATE_AFTER
8562 : CARET_ASSOCIATE_BEFORE;
8564 AutoAssertNoDomMutations guard;
8565 nsILineIterator* it = aBlockFrame->GetLineIterator();
8566 if (!it) {
8567 return NS_ERROR_FAILURE;
8569 int32_t searchingLine = aLineStart;
8570 int32_t countLines = it->GetNumLines();
8571 if (aOutSideLimit > 0) { // start at end
8572 searchingLine = countLines;
8573 } else if (aOutSideLimit < 0) { // start at beginning
8574 searchingLine = -1; //"next" will be 0
8575 } else if ((aPos->mDirection == eDirPrevious && searchingLine == 0) ||
8576 (aPos->mDirection == eDirNext &&
8577 searchingLine >= (countLines - 1))) {
8578 // Not found.
8579 return NS_ERROR_FAILURE;
8581 nsIFrame* resultFrame = nullptr;
8582 nsIFrame* farStoppingFrame = nullptr; // we keep searching until we find a
8583 // "this" frame then we go to next line
8584 nsIFrame* nearStoppingFrame = nullptr; // if we are backing up from edge,
8585 // stop here
8586 nsIFrame* firstFrame;
8587 nsIFrame* lastFrame;
8588 bool isBeforeFirstFrame, isAfterLastFrame;
8589 bool found = false;
8591 nsresult result = NS_OK;
8592 while (!found) {
8593 if (aPos->mDirection == eDirPrevious)
8594 searchingLine--;
8595 else
8596 searchingLine++;
8597 if ((aPos->mDirection == eDirPrevious && searchingLine < 0) ||
8598 (aPos->mDirection == eDirNext && searchingLine >= countLines)) {
8599 // we need to jump to new block frame.
8600 return NS_ERROR_FAILURE;
8602 auto line = it->GetLine(searchingLine).unwrap();
8603 if (!line.mNumFramesOnLine) {
8604 continue;
8606 lastFrame = firstFrame = line.mFirstFrameOnLine;
8607 for (int32_t lineFrameCount = line.mNumFramesOnLine; lineFrameCount > 1;
8608 lineFrameCount--) {
8609 lastFrame = lastFrame->GetNextSibling();
8610 if (!lastFrame) {
8611 NS_ERROR("GetLine promised more frames than could be found");
8612 return NS_ERROR_FAILURE;
8615 nsIFrame::GetLastLeaf(&lastFrame);
8617 if (aPos->mDirection == eDirNext) {
8618 nearStoppingFrame = firstFrame;
8619 farStoppingFrame = lastFrame;
8620 } else {
8621 nearStoppingFrame = lastFrame;
8622 farStoppingFrame = firstFrame;
8624 nsPoint offset;
8625 nsView* view; // used for call of get offset from view
8626 aBlockFrame->GetOffsetFromView(offset, &view);
8627 nsPoint newDesiredPos =
8628 aPos->mDesiredCaretPos -
8629 offset; // get desired position into blockframe coords
8630 result = it->FindFrameAt(searchingLine, newDesiredPos, &resultFrame,
8631 &isBeforeFirstFrame, &isAfterLastFrame);
8632 if (NS_FAILED(result)) {
8633 continue;
8636 if (resultFrame) {
8637 // check to see if this is ANOTHER blockframe inside the other one if so
8638 // then call into its lines
8639 if (resultFrame->CanProvideLineIterator()) {
8640 aPos->mResultFrame = resultFrame;
8641 return NS_OK;
8643 // resultFrame is not a block frame
8644 result = NS_ERROR_FAILURE;
8646 nsCOMPtr<nsIFrameEnumerator> frameTraversal;
8647 result = NS_NewFrameTraversal(
8648 getter_AddRefs(frameTraversal), pc, resultFrame, ePostOrder,
8649 false, // aVisual
8650 aPos->mOptions.contains(PeekOffsetOption::ScrollViewStop),
8651 false, // aFollowOOFs
8652 false // aSkipPopupChecks
8654 if (NS_FAILED(result)) {
8655 return result;
8658 auto FoundValidFrame = [aPos](const nsIFrame::ContentOffsets& aOffsets,
8659 const nsIFrame* aFrame) {
8660 if (!aOffsets.content) {
8661 return false;
8663 if (!aFrame->IsSelectable(nullptr)) {
8664 return false;
8666 if (aPos->mOptions.contains(PeekOffsetOption::ForceEditableRegion) &&
8667 !aOffsets.content->IsEditable()) {
8668 return false;
8670 return true;
8673 nsIFrame* storeOldResultFrame = resultFrame;
8674 while (!found) {
8675 nsPoint point;
8676 nsRect tempRect = resultFrame->GetRect();
8677 nsPoint offset;
8678 nsView* view; // used for call of get offset from view
8679 resultFrame->GetOffsetFromView(offset, &view);
8680 if (!view) {
8681 return NS_ERROR_FAILURE;
8683 if (resultFrame->GetWritingMode().IsVertical()) {
8684 point.y = aPos->mDesiredCaretPos.y;
8685 point.x = tempRect.width + offset.x;
8686 } else {
8687 point.y = tempRect.height + offset.y;
8688 point.x = aPos->mDesiredCaretPos.x;
8691 if (!resultFrame->HasView()) {
8692 nsView* view;
8693 nsPoint offset;
8694 resultFrame->GetOffsetFromView(offset, &view);
8695 nsIFrame::ContentOffsets offsets =
8696 resultFrame->GetContentOffsetsFromPoint(point - offset);
8697 aPos->mResultContent = offsets.content;
8698 aPos->mContentOffset = offsets.offset;
8699 aPos->mAttach = offsets.associate;
8700 if (FoundValidFrame(offsets, resultFrame)) {
8701 found = true;
8702 break;
8706 if (aPos->mDirection == eDirPrevious &&
8707 resultFrame == farStoppingFrame) {
8708 break;
8710 if (aPos->mDirection == eDirNext && resultFrame == nearStoppingFrame) {
8711 break;
8713 // always try previous on THAT line if that fails go the other way
8714 resultFrame = frameTraversal->Traverse(/* aForward = */ false);
8715 if (!resultFrame) {
8716 return NS_ERROR_FAILURE;
8720 if (!found) {
8721 resultFrame = storeOldResultFrame;
8723 result = NS_NewFrameTraversal(
8724 getter_AddRefs(frameTraversal), pc, resultFrame, eLeaf,
8725 false, // aVisual
8726 aPos->mOptions.contains(PeekOffsetOption::ScrollViewStop),
8727 false, // aFollowOOFs
8728 false // aSkipPopupChecks
8731 while (!found) {
8732 nsPoint point = aPos->mDesiredCaretPos;
8733 nsView* view;
8734 nsPoint offset;
8735 resultFrame->GetOffsetFromView(offset, &view);
8736 nsIFrame::ContentOffsets offsets =
8737 resultFrame->GetContentOffsetsFromPoint(point - offset);
8738 aPos->mResultContent = offsets.content;
8739 aPos->mContentOffset = offsets.offset;
8740 aPos->mAttach = offsets.associate;
8741 if (FoundValidFrame(offsets, resultFrame)) {
8742 found = true;
8743 if (resultFrame == farStoppingFrame)
8744 aPos->mAttach = CARET_ASSOCIATE_BEFORE;
8745 else
8746 aPos->mAttach = CARET_ASSOCIATE_AFTER;
8747 break;
8749 if (aPos->mDirection == eDirPrevious &&
8750 (resultFrame == nearStoppingFrame))
8751 break;
8752 if (aPos->mDirection == eDirNext && (resultFrame == farStoppingFrame))
8753 break;
8754 // previous didnt work now we try "next"
8755 nsIFrame* tempFrame = frameTraversal->Traverse(/* aForward = */ true);
8756 if (!tempFrame) break;
8757 resultFrame = tempFrame;
8759 aPos->mResultFrame = resultFrame;
8760 } else {
8761 // we need to jump to new block frame.
8762 aPos->mAmount = eSelectLine;
8763 aPos->mStartOffset = 0;
8764 aPos->mAttach = aPos->mDirection == eDirNext ? CARET_ASSOCIATE_BEFORE
8765 : CARET_ASSOCIATE_AFTER;
8766 if (aPos->mDirection == eDirPrevious)
8767 aPos->mStartOffset = -1; // start from end
8768 return aBlockFrame->PeekOffset(aPos);
8771 return NS_OK;
8774 nsIFrame::CaretPosition nsIFrame::GetExtremeCaretPosition(bool aStart) {
8775 CaretPosition result;
8777 FrameTarget targetFrame = DrillDownToSelectionFrame(this, !aStart, 0);
8778 FrameContentRange range = GetRangeForFrame(targetFrame.frame);
8779 result.mResultContent = range.content;
8780 result.mContentOffset = aStart ? range.start : range.end;
8781 return result;
8784 // If this is a preformatted text frame, see if it ends with a newline
8785 static nsContentAndOffset FindLineBreakInText(nsIFrame* aFrame,
8786 nsDirection aDirection) {
8787 nsContentAndOffset result;
8789 if (aFrame->IsGeneratedContentFrame() ||
8790 !aFrame->HasSignificantTerminalNewline()) {
8791 return result;
8794 int32_t endOffset = aFrame->GetOffsets().second;
8795 result.mContent = aFrame->GetContent();
8796 result.mOffset = endOffset - (aDirection == eDirPrevious ? 0 : 1);
8797 return result;
8800 // Find the first (or last) descendant of the given frame
8801 // which is either a block-level frame or a BRFrame, or some other kind of break
8802 // which stops the line.
8803 static nsContentAndOffset FindLineBreakingFrame(nsIFrame* aFrame,
8804 nsDirection aDirection) {
8805 nsContentAndOffset result;
8807 if (aFrame->IsGeneratedContentFrame()) {
8808 return result;
8811 // Treat form controls as inline leaves
8812 // XXX we really need a way to determine whether a frame is inline-level
8813 if (static_cast<nsIFormControlFrame*>(do_QueryFrame(aFrame))) {
8814 return result;
8817 // Check the frame itself
8818 // Fall through block-in-inline split frames because their mContent is
8819 // the content of the inline frames they were created from. The
8820 // first/last child of such frames is the real block frame we're
8821 // looking for.
8822 if ((aFrame->IsBlockOutside() &&
8823 !aFrame->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) ||
8824 aFrame->IsBrFrame()) {
8825 nsIContent* content = aFrame->GetContent();
8826 result.mContent = content->GetParent();
8827 // In some cases (bug 310589, bug 370174) we end up here with a null
8828 // content. This probably shouldn't ever happen, but since it sometimes
8829 // does, we want to avoid crashing here.
8830 NS_ASSERTION(result.mContent, "Unexpected orphan content");
8831 if (result.mContent) {
8832 result.mOffset = result.mContent->ComputeIndexOf_Deprecated(content) +
8833 (aDirection == eDirPrevious ? 1 : 0);
8835 return result;
8838 result = FindLineBreakInText(aFrame, aDirection);
8839 if (result.mContent) {
8840 return result;
8843 // Iterate over children and call ourselves recursively
8844 if (aDirection == eDirPrevious) {
8845 nsIFrame* child = aFrame->PrincipalChildList().LastChild();
8846 while (child && !result.mContent) {
8847 result = FindLineBreakingFrame(child, aDirection);
8848 child = child->GetPrevSibling();
8850 } else { // eDirNext
8851 nsIFrame* child = aFrame->PrincipalChildList().FirstChild();
8852 while (child && !result.mContent) {
8853 result = FindLineBreakingFrame(child, aDirection);
8854 child = child->GetNextSibling();
8857 return result;
8860 nsresult nsIFrame::PeekOffsetForParagraph(PeekOffsetStruct* aPos) {
8861 nsIFrame* frame = this;
8862 nsContentAndOffset blockFrameOrBR;
8863 blockFrameOrBR.mContent = nullptr;
8864 bool reachedLimit = frame->IsBlockOutside() || IsEditingHost(frame);
8866 auto traverse = [&aPos](nsIFrame* current) {
8867 return aPos->mDirection == eDirPrevious ? current->GetPrevSibling()
8868 : current->GetNextSibling();
8871 // Go through containing frames until reaching a block frame.
8872 // In each step, search the previous (or next) siblings for the closest
8873 // "stop frame" (a block frame or a BRFrame).
8874 // If found, set it to be the selection boundary and abort.
8875 while (!reachedLimit) {
8876 nsIFrame* parent = frame->GetParent();
8877 // Treat a frame associated with the root content as if it were a block
8878 // frame.
8879 if (!frame->mContent || !frame->mContent->GetParent()) {
8880 reachedLimit = true;
8881 break;
8884 if (aPos->mDirection == eDirNext) {
8885 // Try to find our own line-break before looking at our siblings.
8886 blockFrameOrBR = FindLineBreakInText(frame, eDirNext);
8889 nsIFrame* sibling = traverse(frame);
8890 while (sibling && !blockFrameOrBR.mContent) {
8891 blockFrameOrBR = FindLineBreakingFrame(sibling, aPos->mDirection);
8892 sibling = traverse(sibling);
8894 if (blockFrameOrBR.mContent) {
8895 aPos->mResultContent = blockFrameOrBR.mContent;
8896 aPos->mContentOffset = blockFrameOrBR.mOffset;
8897 break;
8899 frame = parent;
8900 reachedLimit = frame && (frame->IsBlockOutside() || IsEditingHost(frame));
8903 if (reachedLimit) { // no "stop frame" found
8904 aPos->mResultContent = frame->GetContent();
8905 if (aPos->mDirection == eDirPrevious) {
8906 aPos->mContentOffset = 0;
8907 } else if (aPos->mResultContent) {
8908 aPos->mContentOffset = aPos->mResultContent->GetChildCount();
8911 return NS_OK;
8914 // Determine movement direction relative to frame
8915 static bool IsMovingInFrameDirection(const nsIFrame* frame,
8916 nsDirection aDirection, bool aVisual) {
8917 bool isReverseDirection =
8918 aVisual && nsBidiPresUtils::IsReversedDirectionFrame(frame);
8919 return aDirection == (isReverseDirection ? eDirPrevious : eDirNext);
8922 // Determines "are we looking for a boundary between whitespace and
8923 // non-whitespace (in the direction we're moving in)". It is true when moving
8924 // forward and looking for a beginning of a word, or when moving backwards and
8925 // looking for an end of a word.
8926 static bool ShouldWordSelectionEatSpace(const PeekOffsetStruct& aPos) {
8927 if (aPos.mWordMovementType != eDefaultBehavior) {
8928 // aPos->mWordMovementType possible values:
8929 // eEndWord: eat the space if we're moving backwards
8930 // eStartWord: eat the space if we're moving forwards
8931 return (aPos.mWordMovementType == eEndWord) ==
8932 (aPos.mDirection == eDirPrevious);
8934 // Use the hidden preference which is based on operating system
8935 // behavior. This pref only affects whether moving forward by word
8936 // should go to the end of this word or start of the next word. When
8937 // going backwards, the start of the word is always used, on every
8938 // operating system.
8939 return aPos.mDirection == eDirNext &&
8940 StaticPrefs::layout_word_select_eat_space_to_next_word();
8943 enum class OffsetIsAtLineEdge : bool { No, Yes };
8945 static void SetPeekResultFromFrame(PeekOffsetStruct& aPos, nsIFrame* aFrame,
8946 int32_t aOffset,
8947 OffsetIsAtLineEdge aAtLineEdge) {
8948 FrameContentRange range = GetRangeForFrame(aFrame);
8949 aPos.mResultFrame = aFrame;
8950 aPos.mResultContent = range.content;
8951 // Output offset is relative to content, not frame
8952 aPos.mContentOffset =
8953 aOffset < 0 ? range.end + aOffset + 1 : range.start + aOffset;
8954 if (aAtLineEdge == OffsetIsAtLineEdge::Yes) {
8955 aPos.mAttach = aPos.mContentOffset == range.start ? CARET_ASSOCIATE_AFTER
8956 : CARET_ASSOCIATE_BEFORE;
8960 void nsIFrame::SelectablePeekReport::TransferTo(PeekOffsetStruct& aPos) const {
8961 return SetPeekResultFromFrame(aPos, mFrame, mOffset, OffsetIsAtLineEdge::No);
8964 nsIFrame::SelectablePeekReport::SelectablePeekReport(
8965 const mozilla::GenericErrorResult<nsresult>&& aErr) {
8966 MOZ_ASSERT(NS_FAILED(aErr.operator nsresult()));
8967 // Return an empty report
8970 nsresult nsIFrame::PeekOffsetForCharacter(PeekOffsetStruct* aPos,
8971 int32_t aOffset) {
8972 SelectablePeekReport current{this, aOffset};
8974 nsIFrame::FrameSearchResult peekSearchState = CONTINUE;
8976 while (peekSearchState != FOUND) {
8977 const bool movingInFrameDirection = IsMovingInFrameDirection(
8978 current.mFrame, aPos->mDirection,
8979 aPos->mOptions.contains(PeekOffsetOption::Visual));
8981 if (current.mJumpedLine) {
8982 // If we jumped lines, it's as if we found a character, but we still need
8983 // to eat non-renderable content on the new line.
8984 peekSearchState = current.PeekOffsetNoAmount(movingInFrameDirection);
8985 } else {
8986 PeekOffsetCharacterOptions options;
8987 options.mRespectClusters = aPos->mAmount == eSelectCluster;
8988 peekSearchState =
8989 current.PeekOffsetCharacter(movingInFrameDirection, options);
8992 current.mMovedOverNonSelectableText |=
8993 peekSearchState == CONTINUE_UNSELECTABLE;
8995 if (peekSearchState != FOUND) {
8996 SelectablePeekReport next = current.mFrame->GetFrameFromDirection(*aPos);
8997 if (next.Failed()) {
8998 return NS_ERROR_FAILURE;
9000 next.mJumpedLine |= current.mJumpedLine;
9001 next.mMovedOverNonSelectableText |= current.mMovedOverNonSelectableText;
9002 next.mHasSelectableFrame |= current.mHasSelectableFrame;
9003 current = next;
9006 // Found frame, but because we moved over non selectable text we want
9007 // the offset to be at the frame edge. Note that if we are extending the
9008 // selection, this doesn't matter.
9009 if (peekSearchState == FOUND && current.mMovedOverNonSelectableText &&
9010 (!aPos->mOptions.contains(PeekOffsetOption::Extend) ||
9011 current.mHasSelectableFrame)) {
9012 auto [start, end] = current.mFrame->GetOffsets();
9013 current.mOffset = aPos->mDirection == eDirNext ? 0 : end - start;
9017 // Set outputs
9018 current.TransferTo(*aPos);
9019 // If we're dealing with a text frame and moving backward positions us at
9020 // the end of that line, decrease the offset by one to make sure that
9021 // we're placed before the linefeed character on the previous line.
9022 if (current.mOffset < 0 && current.mJumpedLine &&
9023 aPos->mDirection == eDirPrevious &&
9024 current.mFrame->HasSignificantTerminalNewline() &&
9025 !current.mIgnoredBrFrame) {
9026 --aPos->mContentOffset;
9028 return NS_OK;
9031 nsresult nsIFrame::PeekOffsetForWord(PeekOffsetStruct* aPos, int32_t aOffset) {
9032 SelectablePeekReport current{this, aOffset};
9033 bool shouldStopAtHardBreak =
9034 aPos->mWordMovementType == eDefaultBehavior &&
9035 StaticPrefs::layout_word_select_eat_space_to_next_word();
9036 bool wordSelectEatSpace = ShouldWordSelectionEatSpace(*aPos);
9038 PeekWordState state;
9039 while (true) {
9040 bool movingInFrameDirection = IsMovingInFrameDirection(
9041 current.mFrame, aPos->mDirection,
9042 aPos->mOptions.contains(PeekOffsetOption::Visual));
9044 FrameSearchResult searchResult = current.mFrame->PeekOffsetWord(
9045 movingInFrameDirection, wordSelectEatSpace,
9046 aPos->mOptions.contains(PeekOffsetOption::IsKeyboardSelect),
9047 &current.mOffset, &state,
9048 !aPos->mOptions.contains(PeekOffsetOption::PreserveSpaces));
9049 if (searchResult == FOUND) {
9050 break;
9053 SelectablePeekReport next = current.mFrame->GetFrameFromDirection(*aPos);
9054 if (next.Failed()) {
9055 // If we've crossed the line boundary, check to make sure that we
9056 // have not consumed a trailing newline as whitespace if it's
9057 // significant.
9058 if (next.mJumpedLine && wordSelectEatSpace &&
9059 current.mFrame->HasSignificantTerminalNewline() &&
9060 current.mFrame->StyleText()->mWhiteSpace !=
9061 StyleWhiteSpace::PreLine) {
9062 current.mOffset -= 1;
9064 break;
9067 if (next.mJumpedLine && !wordSelectEatSpace && state.mSawBeforeType) {
9068 // We can't jump lines if we're looking for whitespace following
9069 // non-whitespace, and we already encountered non-whitespace.
9070 break;
9073 if (shouldStopAtHardBreak && next.mJumpedHardBreak) {
9075 * Prev, always: Jump and stop right there
9076 * Next, saw inline: just stop
9077 * Next, no inline: Jump and consume whitespaces
9079 if (aPos->mDirection == eDirPrevious) {
9080 // Try moving to the previous line if exists
9081 current.TransferTo(*aPos);
9082 current.mFrame->PeekOffsetForCharacter(aPos, current.mOffset);
9083 return NS_OK;
9085 if (state.mSawInlineCharacter || current.mJumpedHardBreak) {
9086 if (current.mFrame->HasSignificantTerminalNewline()) {
9087 current.mOffset -= 1;
9089 current.TransferTo(*aPos);
9090 return NS_OK;
9092 // Mark the state as whitespace and continue
9093 state.Update(false, true);
9096 if (next.mJumpedLine) {
9097 state.mContext.Truncate();
9099 current = next;
9100 // Jumping a line is equivalent to encountering whitespace
9101 // This affects only when it already met an actual character
9102 if (wordSelectEatSpace && next.mJumpedLine) {
9103 state.SetSawBeforeType();
9107 // Set outputs
9108 current.TransferTo(*aPos);
9109 return NS_OK;
9112 static nsIFrame* GetFirstSelectableDescendantWithLineIterator(
9113 nsIFrame* aParentFrame, bool aForceEditableRegion) {
9114 auto FoundValidFrame = [aForceEditableRegion](const nsIFrame* aFrame) {
9115 if (!aFrame->IsSelectable(nullptr)) {
9116 return false;
9118 if (aForceEditableRegion && !aFrame->GetContent()->IsEditable()) {
9119 return false;
9121 return true;
9124 for (nsIFrame* child : aParentFrame->PrincipalChildList()) {
9125 // some children may not be selectable, e.g. :before / :after pseudoelements
9126 // content with user-select: none, or contenteditable="false"
9127 // we need to skip them
9128 if (child->CanProvideLineIterator() && FoundValidFrame(child)) {
9129 return child;
9131 if (nsIFrame* nested = GetFirstSelectableDescendantWithLineIterator(
9132 child, aForceEditableRegion)) {
9133 return nested;
9136 return nullptr;
9139 nsresult nsIFrame::PeekOffsetForLine(PeekOffsetStruct* aPos) {
9140 nsIFrame* blockFrame = this;
9141 nsresult result = NS_ERROR_FAILURE;
9143 // outer loop
9144 // moving to a next block when no more blocks are available in a subtree
9145 AutoAssertNoDomMutations guard;
9146 while (NS_FAILED(result)) {
9147 auto [newBlock, lineFrame] = blockFrame->GetContainingBlockForLine(
9148 aPos->mOptions.contains(PeekOffsetOption::ScrollViewStop));
9149 if (!newBlock) {
9150 return NS_ERROR_FAILURE;
9152 blockFrame = newBlock;
9153 nsILineIterator* iter = blockFrame->GetLineIterator();
9154 int32_t thisLine = iter->FindLineContaining(lineFrame);
9155 if (NS_WARN_IF(thisLine < 0)) {
9156 return NS_ERROR_FAILURE;
9159 int8_t edgeCase = 0; // no edge case. This should look at thisLine
9161 // this part will find a frame or a block frame. If it's a block frame
9162 // it will "drill down" to find a viable frame or it will return an
9163 // error.
9164 nsIFrame* lastFrame = this;
9166 // inner loop - crawling the frames within a specific block subtree
9167 while (true) {
9168 result =
9169 GetNextPrevLineFromBlockFrame(aPos, blockFrame, thisLine, edgeCase);
9170 // we came back to same spot! keep going
9171 if (NS_SUCCEEDED(result) &&
9172 (!aPos->mResultFrame || aPos->mResultFrame == lastFrame)) {
9173 aPos->mResultFrame = nullptr;
9174 lastFrame = nullptr;
9175 if (aPos->mDirection == eDirPrevious) {
9176 thisLine--;
9177 } else {
9178 thisLine++;
9180 continue;
9183 if (NS_FAILED(result)) {
9184 break;
9187 lastFrame = aPos->mResultFrame; // set last frame
9188 /* SPECIAL CHECK FOR NAVIGATION INTO TABLES
9189 * when we hit a frame which doesn't have line iterator, we need to
9190 * drill down and find a child with the line iterator to prevent the
9191 * crawling process to prematurely finish. Note that this is only sound if
9192 * we're guaranteed to not have multiple children implementing
9193 * LineIterator.
9195 * So far known cases are:
9196 * 1) table wrapper (drill down into table row group)
9197 * 2) table cell (drill down into its only anon child)
9199 const bool shouldDrillIntoChildren =
9200 aPos->mResultFrame->IsTableWrapperFrame() ||
9201 aPos->mResultFrame->IsTableCellFrame();
9203 if (shouldDrillIntoChildren) {
9204 nsIFrame* child = GetFirstSelectableDescendantWithLineIterator(
9205 aPos->mResultFrame,
9206 aPos->mOptions.contains(PeekOffsetOption::ForceEditableRegion));
9207 if (child) {
9208 aPos->mResultFrame = child;
9212 if (!aPos->mResultFrame->CanProvideLineIterator()) {
9213 // no more selectable content at this level
9214 break;
9217 if (aPos->mResultFrame == blockFrame) {
9218 // Make sure block element is not the same as the one we had before.
9219 break;
9222 // we've struck another block element with selectable content!
9223 if (aPos->mDirection == eDirPrevious) {
9224 edgeCase = 1; // far edge, search from end backwards
9225 } else {
9226 edgeCase = -1; // near edge search from beginning onwards
9228 thisLine = 0; // this line means nothing now.
9229 // everything else means something so keep looking "inside" the
9230 // block
9231 blockFrame = aPos->mResultFrame;
9234 return result;
9237 nsresult nsIFrame::PeekOffsetForLineEdge(PeekOffsetStruct* aPos) {
9238 // Adjusted so that the caret can't get confused when content changes
9239 nsIFrame* frame = AdjustFrameForSelectionStyles(this);
9240 Element* editingHost = frame->GetContent()->GetEditingHost();
9242 auto [blockFrame, lineFrame] = frame->GetContainingBlockForLine(
9243 aPos->mOptions.contains(PeekOffsetOption::ScrollViewStop));
9244 if (!blockFrame) {
9245 return NS_ERROR_FAILURE;
9247 AutoAssertNoDomMutations guard;
9248 nsILineIterator* it = blockFrame->GetLineIterator();
9249 int32_t thisLine = it->FindLineContaining(lineFrame);
9250 if (thisLine < 0) {
9251 return NS_ERROR_FAILURE;
9254 nsIFrame* baseFrame = nullptr;
9255 bool endOfLine = eSelectEndLine == aPos->mAmount;
9257 if (aPos->mOptions.contains(PeekOffsetOption::Visual) &&
9258 PresContext()->BidiEnabled()) {
9259 nsIFrame* firstFrame;
9260 bool isReordered;
9261 nsIFrame* lastFrame;
9262 MOZ_TRY(
9263 it->CheckLineOrder(thisLine, &isReordered, &firstFrame, &lastFrame));
9264 baseFrame = endOfLine ? lastFrame : firstFrame;
9265 } else {
9266 auto line = it->GetLine(thisLine).unwrap();
9268 nsIFrame* frame = line.mFirstFrameOnLine;
9269 bool lastFrameWasEditable = false;
9270 for (int32_t count = line.mNumFramesOnLine; count;
9271 --count, frame = frame->GetNextSibling()) {
9272 if (frame->IsGeneratedContentFrame()) {
9273 continue;
9275 // When jumping to the end of the line with the "end" key,
9276 // try to skip over brFrames
9277 if (endOfLine && line.mNumFramesOnLine > 1 && frame->IsBrFrame() &&
9278 lastFrameWasEditable == frame->GetContent()->IsEditable()) {
9279 continue;
9281 lastFrameWasEditable =
9282 frame->GetContent() && frame->GetContent()->IsEditable();
9283 baseFrame = frame;
9284 if (!endOfLine) {
9285 break;
9289 if (!baseFrame) {
9290 return NS_ERROR_FAILURE;
9292 // Make sure we are not leaving our inline editing host if exists
9293 if (editingHost) {
9294 if (nsIFrame* frame = editingHost->GetPrimaryFrame()) {
9295 if (frame->IsInlineOutside() &&
9296 !editingHost->Contains(baseFrame->GetContent())) {
9297 baseFrame = frame;
9298 if (endOfLine) {
9299 baseFrame = baseFrame->LastContinuation();
9304 FrameTarget targetFrame = DrillDownToSelectionFrame(baseFrame, endOfLine, 0);
9305 SetPeekResultFromFrame(*aPos, targetFrame.frame, endOfLine ? -1 : 0,
9306 OffsetIsAtLineEdge::Yes);
9307 if (endOfLine && targetFrame.frame->HasSignificantTerminalNewline()) {
9308 // Do not position the caret after the terminating newline if we're
9309 // trying to move to the end of line (see bug 596506)
9310 --aPos->mContentOffset;
9312 if (!aPos->mResultContent) {
9313 return NS_ERROR_FAILURE;
9315 return NS_OK;
9318 nsresult nsIFrame::PeekOffset(PeekOffsetStruct* aPos) {
9319 MOZ_ASSERT(aPos);
9321 if (NS_WARN_IF(HasAnyStateBits(NS_FRAME_IS_DIRTY))) {
9322 // FIXME(Bug 1654362): <caption> currently can remain dirty.
9323 return NS_ERROR_UNEXPECTED;
9326 // Translate content offset to be relative to frame
9327 int32_t offset = aPos->mStartOffset - GetRangeForFrame(this).start;
9329 switch (aPos->mAmount) {
9330 case eSelectCharacter:
9331 case eSelectCluster:
9332 return PeekOffsetForCharacter(aPos, offset);
9333 case eSelectWordNoSpace:
9334 // eSelectWordNoSpace means that we should not be eating any whitespace
9335 // when moving to the adjacent word. This means that we should set aPos->
9336 // mWordMovementType to eEndWord if we're moving forwards, and to
9337 // eStartWord if we're moving backwards.
9338 if (aPos->mDirection == eDirPrevious) {
9339 aPos->mWordMovementType = eStartWord;
9340 } else {
9341 aPos->mWordMovementType = eEndWord;
9343 // Intentionally fall through the eSelectWord case.
9344 [[fallthrough]];
9345 case eSelectWord:
9346 return PeekOffsetForWord(aPos, offset);
9347 case eSelectLine:
9348 return PeekOffsetForLine(aPos);
9349 case eSelectBeginLine:
9350 case eSelectEndLine:
9351 return PeekOffsetForLineEdge(aPos);
9352 case eSelectParagraph:
9353 return PeekOffsetForParagraph(aPos);
9354 default: {
9355 NS_ASSERTION(false, "Invalid amount");
9356 return NS_ERROR_FAILURE;
9361 nsIFrame::FrameSearchResult nsIFrame::PeekOffsetNoAmount(bool aForward,
9362 int32_t* aOffset) {
9363 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
9364 // Sure, we can stop right here.
9365 return FOUND;
9368 nsIFrame::FrameSearchResult nsIFrame::PeekOffsetCharacter(
9369 bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
9370 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
9371 int32_t startOffset = *aOffset;
9372 // A negative offset means "end of frame", which in our case means offset 1.
9373 if (startOffset < 0) startOffset = 1;
9374 if (aForward == (startOffset == 0)) {
9375 // We're before the frame and moving forward, or after it and moving
9376 // backwards: skip to the other side and we're done.
9377 *aOffset = 1 - startOffset;
9378 return FOUND;
9380 return CONTINUE;
9383 nsIFrame::FrameSearchResult nsIFrame::PeekOffsetWord(
9384 bool aForward, bool aWordSelectEatSpace, bool aIsKeyboardSelect,
9385 int32_t* aOffset, PeekWordState* aState, bool /*aTrimSpaces*/) {
9386 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
9387 int32_t startOffset = *aOffset;
9388 // This isn't text, so truncate the context
9389 aState->mContext.Truncate();
9390 if (startOffset < 0) startOffset = 1;
9391 if (aForward == (startOffset == 0)) {
9392 // We're before the frame and moving forward, or after it and moving
9393 // backwards. If we're looking for non-whitespace, we found it (without
9394 // skipping this frame).
9395 if (!aState->mAtStart) {
9396 if (aState->mLastCharWasPunctuation) {
9397 // We're not punctuation, so this is a punctuation boundary.
9398 if (BreakWordBetweenPunctuation(aState, aForward, false, false,
9399 aIsKeyboardSelect))
9400 return FOUND;
9401 } else {
9402 // This is not a punctuation boundary.
9403 if (aWordSelectEatSpace && aState->mSawBeforeType) return FOUND;
9406 // Otherwise skip to the other side and note that we encountered
9407 // non-whitespace.
9408 *aOffset = 1 - startOffset;
9409 aState->Update(false, // not punctuation
9410 false // not whitespace
9412 if (!aWordSelectEatSpace) aState->SetSawBeforeType();
9414 return CONTINUE;
9417 // static
9418 bool nsIFrame::BreakWordBetweenPunctuation(const PeekWordState* aState,
9419 bool aForward, bool aPunctAfter,
9420 bool aWhitespaceAfter,
9421 bool aIsKeyboardSelect) {
9422 NS_ASSERTION(aPunctAfter != aState->mLastCharWasPunctuation,
9423 "Call this only at punctuation boundaries");
9424 if (aState->mLastCharWasWhitespace) {
9425 // We always stop between whitespace and punctuation
9426 return true;
9428 if (!StaticPrefs::layout_word_select_stop_at_punctuation()) {
9429 // When this pref is false, we never stop at a punctuation boundary unless
9430 // it's followed by whitespace (in the relevant direction).
9431 return aWhitespaceAfter;
9433 if (!aIsKeyboardSelect) {
9434 // mouse caret movement (e.g. word selection) always stops at every
9435 // punctuation boundary
9436 return true;
9438 bool afterPunct = aForward ? aState->mLastCharWasPunctuation : aPunctAfter;
9439 if (!afterPunct) {
9440 // keyboard caret movement only stops after punctuation (in content order)
9441 return false;
9443 // Stop only if we've seen some non-punctuation since the last whitespace;
9444 // don't stop after punctuation that follows whitespace.
9445 return aState->mSeenNonPunctuationSinceWhitespace;
9448 std::pair<nsIFrame*, nsIFrame*> nsIFrame::GetContainingBlockForLine(
9449 bool aLockScroll) const {
9450 const nsIFrame* parentFrame = this;
9451 const nsIFrame* frame;
9452 while (parentFrame) {
9453 frame = parentFrame;
9454 if (frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
9455 // if we are searching for a frame that is not in flow we will not find
9456 // it. we must instead look for its placeholder
9457 if (frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
9458 // abspos continuations don't have placeholders, get the fif
9459 frame = frame->FirstInFlow();
9461 frame = frame->GetPlaceholderFrame();
9462 if (!frame) {
9463 return std::pair(nullptr, nullptr);
9466 parentFrame = frame->GetParent();
9467 if (parentFrame) {
9468 if (aLockScroll && parentFrame->IsScrollFrame()) {
9469 return std::pair(nullptr, nullptr);
9471 if (parentFrame->CanProvideLineIterator()) {
9472 return std::pair(const_cast<nsIFrame*>(parentFrame),
9473 const_cast<nsIFrame*>(frame));
9477 return std::pair(nullptr, nullptr);
9480 Result<bool, nsresult> nsIFrame::IsVisuallyAtLineEdge(
9481 nsILineIterator* aLineIterator, int32_t aLine, nsDirection aDirection) {
9482 nsIFrame* firstFrame;
9483 nsIFrame* lastFrame;
9485 const bool lineIsRTL = aLineIterator->IsLineIteratorFlowRTL();
9486 bool isReordered;
9488 MOZ_TRY(aLineIterator->CheckLineOrder(aLine, &isReordered, &firstFrame,
9489 &lastFrame));
9491 nsIFrame** framePtr = aDirection == eDirPrevious ? &firstFrame : &lastFrame;
9492 if (!*framePtr) {
9493 return true;
9496 bool frameIsRTL = (nsBidiPresUtils::FrameDirection(*framePtr) ==
9497 mozilla::intl::BidiDirection::RTL);
9498 if ((frameIsRTL == lineIsRTL) == (aDirection == eDirPrevious)) {
9499 nsIFrame::GetFirstLeaf(framePtr);
9500 } else {
9501 nsIFrame::GetLastLeaf(framePtr);
9503 return *framePtr == this;
9506 Result<bool, nsresult> nsIFrame::IsLogicallyAtLineEdge(
9507 nsILineIterator* aLineIterator, int32_t aLine, nsDirection aDirection) {
9508 auto line = aLineIterator->GetLine(aLine).unwrap();
9510 if (aDirection == eDirPrevious) {
9511 nsIFrame* firstFrame = line.mFirstFrameOnLine;
9512 nsIFrame::GetFirstLeaf(&firstFrame);
9513 return firstFrame == this;
9516 // eDirNext
9517 nsIFrame* lastFrame = line.mFirstFrameOnLine;
9518 for (int32_t lineFrameCount = line.mNumFramesOnLine; lineFrameCount > 1;
9519 lineFrameCount--) {
9520 lastFrame = lastFrame->GetNextSibling();
9521 if (!lastFrame) {
9522 NS_ERROR("should not be reached nsIFrame");
9523 return Err(NS_ERROR_FAILURE);
9526 nsIFrame::GetLastLeaf(&lastFrame);
9527 return lastFrame == this;
9530 nsIFrame::SelectablePeekReport nsIFrame::GetFrameFromDirection(
9531 nsDirection aDirection, const PeekOffsetOptions& aOptions) {
9532 SelectablePeekReport result;
9534 nsPresContext* presContext = PresContext();
9535 const bool needsVisualTraversal =
9536 aOptions.contains(PeekOffsetOption::Visual) && presContext->BidiEnabled();
9537 nsCOMPtr<nsIFrameEnumerator> frameTraversal;
9538 MOZ_TRY(NS_NewFrameTraversal(
9539 getter_AddRefs(frameTraversal), presContext, this, eLeaf,
9540 needsVisualTraversal, aOptions.contains(PeekOffsetOption::ScrollViewStop),
9541 true, // aFollowOOFs
9542 false // aSkipPopupChecks
9545 // Find the prev/next selectable frame
9546 bool selectable = false;
9547 nsIFrame* traversedFrame = this;
9548 AutoAssertNoDomMutations guard;
9549 const nsIContent* const nativeAnonymousSubtreeContent =
9550 GetClosestNativeAnonymousSubtreeRoot();
9551 while (!selectable) {
9552 auto [blockFrame, lineFrame] = traversedFrame->GetContainingBlockForLine(
9553 aOptions.contains(PeekOffsetOption::ScrollViewStop));
9554 if (!blockFrame) {
9555 return result;
9558 nsILineIterator* it = blockFrame->GetLineIterator();
9559 int32_t thisLine = it->FindLineContaining(lineFrame);
9560 if (thisLine < 0) {
9561 return result;
9564 bool atLineEdge;
9565 MOZ_TRY_VAR(
9566 atLineEdge,
9567 needsVisualTraversal
9568 ? traversedFrame->IsVisuallyAtLineEdge(it, thisLine, aDirection)
9569 : traversedFrame->IsLogicallyAtLineEdge(it, thisLine, aDirection));
9570 if (atLineEdge) {
9571 result.mJumpedLine = true;
9572 if (!aOptions.contains(PeekOffsetOption::JumpLines)) {
9573 return result; // we are done. cannot jump lines
9575 int32_t lineToCheckWrap =
9576 aDirection == eDirPrevious ? thisLine - 1 : thisLine;
9577 if (lineToCheckWrap < 0 ||
9578 !it->GetLine(lineToCheckWrap).unwrap().mIsWrapped) {
9579 result.mJumpedHardBreak = true;
9583 traversedFrame = frameTraversal->Traverse(aDirection == eDirNext);
9584 if (!traversedFrame) {
9585 return result;
9588 auto IsSelectable =
9589 [aOptions, nativeAnonymousSubtreeContent](const nsIFrame* aFrame) {
9590 if (!aFrame->IsSelectable(nullptr)) {
9591 return false;
9593 // If the new frame is in a native anonymous subtree, we should treat
9594 // it as not selectable unless the frame and found frame are in same
9595 // subtree.
9596 if (aFrame->GetClosestNativeAnonymousSubtreeRoot() !=
9597 nativeAnonymousSubtreeContent) {
9598 return false;
9600 return !aOptions.contains(PeekOffsetOption::ForceEditableRegion) ||
9601 aFrame->GetContent()->IsEditable();
9604 // Skip br frames, but only if we can select something before hitting the
9605 // end of the line or a non-selectable region.
9606 if (atLineEdge && aDirection == eDirPrevious &&
9607 traversedFrame->IsBrFrame()) {
9608 for (nsIFrame* current = traversedFrame->GetPrevSibling(); current;
9609 current = current->GetPrevSibling()) {
9610 if (!current->IsBlockOutside() && IsSelectable(current)) {
9611 if (!current->IsBrFrame()) {
9612 result.mIgnoredBrFrame = true;
9614 break;
9617 if (result.mIgnoredBrFrame) {
9618 continue;
9622 selectable = IsSelectable(traversedFrame);
9623 if (!selectable) {
9624 if (traversedFrame->IsSelectable(nullptr)) {
9625 result.mHasSelectableFrame = true;
9627 result.mMovedOverNonSelectableText = true;
9629 } // while (!selectable)
9631 result.mOffset = (aDirection == eDirNext) ? 0 : -1;
9633 if (aOptions.contains(PeekOffsetOption::Visual) &&
9634 nsBidiPresUtils::IsReversedDirectionFrame(traversedFrame)) {
9635 // The new frame is reverse-direction, go to the other end
9636 result.mOffset = -1 - result.mOffset;
9638 result.mFrame = traversedFrame;
9639 return result;
9642 nsIFrame::SelectablePeekReport nsIFrame::GetFrameFromDirection(
9643 const PeekOffsetStruct& aPos) {
9644 return GetFrameFromDirection(aPos.mDirection, aPos.mOptions);
9647 nsView* nsIFrame::GetClosestView(nsPoint* aOffset) const {
9648 nsPoint offset(0, 0);
9649 for (const nsIFrame* f = this; f; f = f->GetParent()) {
9650 if (f->HasView()) {
9651 if (aOffset) *aOffset = offset;
9652 return f->GetView();
9654 offset += f->GetPosition();
9657 MOZ_ASSERT_UNREACHABLE("No view on any parent? How did that happen?");
9658 return nullptr;
9661 /* virtual */
9662 void nsIFrame::ChildIsDirty(nsIFrame* aChild) {
9663 MOZ_ASSERT_UNREACHABLE(
9664 "should never be called on a frame that doesn't "
9665 "inherit from nsContainerFrame");
9668 #ifdef ACCESSIBILITY
9669 a11y::AccType nsIFrame::AccessibleType() {
9670 if (IsTableCaption() && !GetRect().IsEmpty()) {
9671 return a11y::eHTMLCaptionType;
9673 return a11y::eNoType;
9675 #endif
9677 bool nsIFrame::ClearOverflowRects() {
9678 if (mOverflow.mType == OverflowStorageType::None) {
9679 return false;
9681 if (mOverflow.mType == OverflowStorageType::Large) {
9682 RemoveProperty(OverflowAreasProperty());
9684 mOverflow.mType = OverflowStorageType::None;
9685 return true;
9688 bool nsIFrame::SetOverflowAreas(const OverflowAreas& aOverflowAreas) {
9689 if (mOverflow.mType == OverflowStorageType::Large) {
9690 OverflowAreas* overflow = GetOverflowAreasProperty();
9691 bool changed = *overflow != aOverflowAreas;
9692 *overflow = aOverflowAreas;
9694 // Don't bother with converting to the deltas form if we already
9695 // have a property.
9696 return changed;
9699 const nsRect& vis = aOverflowAreas.InkOverflow();
9700 uint32_t l = -vis.x, // left edge: positive delta is leftwards
9701 t = -vis.y, // top: positive is upwards
9702 r = vis.XMost() - mRect.width, // right: positive is rightwards
9703 b = vis.YMost() - mRect.height; // bottom: positive is downwards
9704 if (aOverflowAreas.ScrollableOverflow().IsEqualEdges(
9705 nsRect(nsPoint(0, 0), GetSize())) &&
9706 l <= InkOverflowDeltas::kMax && t <= InkOverflowDeltas::kMax &&
9707 r <= InkOverflowDeltas::kMax && b <= InkOverflowDeltas::kMax &&
9708 // we have to check these against zero because we *never* want to
9709 // set a frame as having no overflow in this function. This is
9710 // because FinishAndStoreOverflow calls this function prior to
9711 // SetRect based on whether the overflow areas match aNewSize.
9712 // In the case where the overflow areas exactly match mRect but
9713 // do not match aNewSize, we need to store overflow in a property
9714 // so that our eventual SetRect/SetSize will know that it has to
9715 // reset our overflow areas.
9716 (l | t | r | b) != 0) {
9717 InkOverflowDeltas oldDeltas = mOverflow.mInkOverflowDeltas;
9718 // It's a "small" overflow area so we store the deltas for each edge
9719 // directly in the frame, rather than allocating a separate rect.
9720 // If they're all zero, that's fine; we're setting things to
9721 // no-overflow.
9722 mOverflow.mInkOverflowDeltas.mLeft = l;
9723 mOverflow.mInkOverflowDeltas.mTop = t;
9724 mOverflow.mInkOverflowDeltas.mRight = r;
9725 mOverflow.mInkOverflowDeltas.mBottom = b;
9726 // There was no scrollable overflow before, and there isn't now.
9727 return oldDeltas != mOverflow.mInkOverflowDeltas;
9728 } else {
9729 bool changed =
9730 !aOverflowAreas.ScrollableOverflow().IsEqualEdges(
9731 nsRect(nsPoint(0, 0), GetSize())) ||
9732 !aOverflowAreas.InkOverflow().IsEqualEdges(InkOverflowFromDeltas());
9734 // it's a large overflow area that we need to store as a property
9735 mOverflow.mType = OverflowStorageType::Large;
9736 AddProperty(OverflowAreasProperty(), new OverflowAreas(aOverflowAreas));
9737 return changed;
9741 enum class ApplyTransform : bool { No, Yes };
9744 * Compute the outline inner rect (so without outline-width and outline-offset)
9745 * of aFrame, maybe iterating over its descendants, in aFrame's coordinate space
9746 * or its post-transform coordinate space (depending on aApplyTransform).
9748 static nsRect ComputeOutlineInnerRect(
9749 nsIFrame* aFrame, ApplyTransform aApplyTransform, bool& aOutValid,
9750 const nsSize* aSizeOverride = nullptr,
9751 const OverflowAreas* aOverflowOverride = nullptr) {
9752 const nsRect bounds(nsPoint(0, 0),
9753 aSizeOverride ? *aSizeOverride : aFrame->GetSize());
9755 // The SVG container frames besides SVGTextFrame do not maintain
9756 // an accurate mRect. It will make the outline be larger than
9757 // we expect, we need to make them narrow to their children's outline.
9758 // aOutValid is set to false if the returned nsRect is not valid
9759 // and should not be included in the outline rectangle.
9760 aOutValid = !aFrame->HasAnyStateBits(NS_FRAME_SVG_LAYOUT) ||
9761 !aFrame->IsFrameOfType(nsIFrame::eSVGContainer) ||
9762 aFrame->IsSVGTextFrame();
9764 nsRect u;
9766 if (!aFrame->FrameMaintainsOverflow()) {
9767 return u;
9770 // Start from our border-box, transformed. See comment below about
9771 // transform of children.
9772 bool doTransform =
9773 aApplyTransform == ApplyTransform::Yes && aFrame->IsTransformed();
9774 TransformReferenceBox boundsRefBox(nullptr, bounds);
9775 if (doTransform) {
9776 u = nsDisplayTransform::TransformRect(bounds, aFrame, boundsRefBox);
9777 } else {
9778 u = bounds;
9781 if (aOutValid && !StaticPrefs::layout_outline_include_overflow()) {
9782 return u;
9785 // Only iterate through the children if the overflow areas suggest
9786 // that we might need to, and if the frame doesn't clip its overflow
9787 // anyway.
9788 if (aOverflowOverride) {
9789 if (!doTransform && bounds.IsEqualEdges(aOverflowOverride->InkOverflow()) &&
9790 bounds.IsEqualEdges(aOverflowOverride->ScrollableOverflow())) {
9791 return u;
9793 } else {
9794 if (!doTransform && bounds.IsEqualEdges(aFrame->InkOverflowRect()) &&
9795 bounds.IsEqualEdges(aFrame->ScrollableOverflowRect())) {
9796 return u;
9799 const nsStyleDisplay* disp = aFrame->StyleDisplay();
9800 LayoutFrameType fType = aFrame->Type();
9801 if (fType == LayoutFrameType::Scroll ||
9802 fType == LayoutFrameType::ListControl ||
9803 fType == LayoutFrameType::SVGOuterSVG) {
9804 return u;
9807 auto overflowClipAxes = aFrame->ShouldApplyOverflowClipping(disp);
9808 auto overflowClipMargin = aFrame->OverflowClipMargin(overflowClipAxes);
9809 if (overflowClipAxes == nsIFrame::PhysicalAxes::Both &&
9810 overflowClipMargin == nsSize()) {
9811 return u;
9814 const nsStyleEffects* effects = aFrame->StyleEffects();
9815 Maybe<nsRect> clipPropClipRect =
9816 aFrame->GetClipPropClipRect(disp, effects, bounds.Size());
9818 // Iterate over all children except pop-up, absolutely-positioned,
9819 // float, and overflow ones.
9820 const FrameChildListIDs skip = {
9821 FrameChildListID::Popup, FrameChildListID::Absolute,
9822 FrameChildListID::Fixed, FrameChildListID::Float,
9823 FrameChildListID::Overflow};
9824 for (const auto& [list, listID] : aFrame->ChildLists()) {
9825 if (skip.contains(listID)) {
9826 continue;
9829 for (nsIFrame* child : list) {
9830 if (child->IsPlaceholderFrame()) {
9831 continue;
9834 // Note that passing ApplyTransform::Yes when
9835 // child->Combines3DTransformWithAncestors() returns true is incorrect if
9836 // our aApplyTransform is No... but the opposite would be as well.
9837 // This is because elements within a preserve-3d scene are always
9838 // transformed up to the top of the scene. This means we don't have a
9839 // mechanism for getting a transform up to an intermediate point within
9840 // the scene. We choose to over-transform rather than under-transform
9841 // because this is consistent with other overflow areas.
9842 bool validRect = true;
9843 nsRect childRect =
9844 ComputeOutlineInnerRect(child, ApplyTransform::Yes, validRect) +
9845 child->GetPosition();
9847 if (!validRect) {
9848 continue;
9851 if (clipPropClipRect) {
9852 // Intersect with the clip before transforming.
9853 childRect.IntersectRect(childRect, *clipPropClipRect);
9856 // Note that we transform each child separately according to
9857 // aFrame's transform, and then union, which gives a different
9858 // (smaller) result from unioning and then transforming the
9859 // union. This doesn't match the way we handle overflow areas
9860 // with 2-D transforms, though it does match the way we handle
9861 // overflow areas in preserve-3d 3-D scenes.
9862 if (doTransform && !child->Combines3DTransformWithAncestors()) {
9863 childRect =
9864 nsDisplayTransform::TransformRect(childRect, aFrame, boundsRefBox);
9867 // If a SVGContainer has a non-SVGContainer child, we assign
9868 // its child's outline to this SVGContainer directly.
9869 if (!aOutValid && validRect) {
9870 u = childRect;
9871 aOutValid = true;
9872 } else {
9873 u = u.UnionEdges(childRect);
9878 if (overflowClipAxes != nsIFrame::PhysicalAxes::None) {
9879 OverflowAreas::ApplyOverflowClippingOnRect(u, bounds, overflowClipAxes,
9880 overflowClipMargin);
9882 return u;
9885 static void ComputeAndIncludeOutlineArea(nsIFrame* aFrame,
9886 OverflowAreas& aOverflowAreas,
9887 const nsSize& aNewSize) {
9888 const nsStyleOutline* outline = aFrame->StyleOutline();
9889 if (!outline->ShouldPaintOutline()) {
9890 return;
9893 // When the outline property is set on a :-moz-block-inside-inline-wrapper
9894 // pseudo-element, it inherited that outline from the inline that was broken
9895 // because it contained a block. In that case, we don't want a really wide
9896 // outline if the block inside the inline is narrow, so union the actual
9897 // contents of the anonymous blocks.
9898 nsIFrame* frameForArea = aFrame;
9899 do {
9900 PseudoStyleType pseudoType = frameForArea->Style()->GetPseudoType();
9901 if (pseudoType != PseudoStyleType::mozBlockInsideInlineWrapper) break;
9902 // If we're done, we really want it and all its later siblings.
9903 frameForArea = frameForArea->PrincipalChildList().FirstChild();
9904 NS_ASSERTION(frameForArea, "anonymous block with no children?");
9905 } while (frameForArea);
9907 // Find the union of the border boxes of all descendants, or in
9908 // the block-in-inline case, all descendants we care about.
9910 // Note that the interesting perspective-related cases are taken
9911 // care of by the code that handles those issues for overflow
9912 // calling FinishAndStoreOverflow again, which in turn calls this
9913 // function again. We still need to deal with preserve-3d a bit.
9914 nsRect innerRect;
9915 bool validRect = false;
9916 if (frameForArea == aFrame) {
9917 innerRect = ComputeOutlineInnerRect(aFrame, ApplyTransform::No, validRect,
9918 &aNewSize, &aOverflowAreas);
9919 } else {
9920 for (; frameForArea; frameForArea = frameForArea->GetNextSibling()) {
9921 nsRect r =
9922 ComputeOutlineInnerRect(frameForArea, ApplyTransform::Yes, validRect);
9924 // Adjust for offsets transforms up to aFrame's pre-transform
9925 // (i.e., normal) coordinate space; see comments in
9926 // UnionBorderBoxes for some of the subtlety here.
9927 for (nsIFrame *f = frameForArea, *parent = f->GetParent();
9928 /* see middle of loop */; f = parent, parent = f->GetParent()) {
9929 r += f->GetPosition();
9930 if (parent == aFrame) {
9931 break;
9933 if (parent->IsTransformed() && !f->Combines3DTransformWithAncestors()) {
9934 TransformReferenceBox refBox(parent);
9935 r = nsDisplayTransform::TransformRect(r, parent, refBox);
9939 innerRect.UnionRect(innerRect, r);
9943 // Keep this code in sync with nsDisplayOutline::GetInnerRect.
9944 if (innerRect == aFrame->GetRectRelativeToSelf()) {
9945 aFrame->RemoveProperty(nsIFrame::OutlineInnerRectProperty());
9946 } else {
9947 SetOrUpdateRectValuedProperty(aFrame, nsIFrame::OutlineInnerRectProperty(),
9948 innerRect);
9951 nsRect outerRect(innerRect);
9952 outerRect.Inflate(outline->EffectiveOffsetFor(outerRect));
9954 if (outline->mOutlineStyle.IsAuto()) {
9955 nsPresContext* pc = aFrame->PresContext();
9957 pc->Theme()->GetWidgetOverflow(pc->DeviceContext(), aFrame,
9958 StyleAppearance::FocusOutline, &outerRect);
9959 } else {
9960 const nscoord width = outline->GetOutlineWidth();
9961 outerRect.Inflate(width);
9964 nsRect& vo = aOverflowAreas.InkOverflow();
9965 vo = vo.UnionEdges(innerRect.Union(outerRect));
9968 bool nsIFrame::FinishAndStoreOverflow(OverflowAreas& aOverflowAreas,
9969 nsSize aNewSize, nsSize* aOldSize,
9970 const nsStyleDisplay* aStyleDisplay) {
9971 MOZ_ASSERT(FrameMaintainsOverflow(),
9972 "Don't call - overflow rects not maintained on these SVG frames");
9974 const nsStyleDisplay* disp = StyleDisplayWithOptionalParam(aStyleDisplay);
9975 bool hasTransform = IsTransformed();
9977 nsRect bounds(nsPoint(0, 0), aNewSize);
9978 // Store the passed in overflow area if we are a preserve-3d frame or we have
9979 // a transform, and it's not just the frame bounds.
9980 if (hasTransform || Combines3DTransformWithAncestors()) {
9981 if (!aOverflowAreas.InkOverflow().IsEqualEdges(bounds) ||
9982 !aOverflowAreas.ScrollableOverflow().IsEqualEdges(bounds)) {
9983 OverflowAreas* initial = GetProperty(nsIFrame::InitialOverflowProperty());
9984 if (!initial) {
9985 AddProperty(nsIFrame::InitialOverflowProperty(),
9986 new OverflowAreas(aOverflowAreas));
9987 } else if (initial != &aOverflowAreas) {
9988 *initial = aOverflowAreas;
9990 } else {
9991 RemoveProperty(nsIFrame::InitialOverflowProperty());
9993 #ifdef DEBUG
9994 SetProperty(nsIFrame::DebugInitialOverflowPropertyApplied(), true);
9995 #endif
9996 } else {
9997 #ifdef DEBUG
9998 RemoveProperty(nsIFrame::DebugInitialOverflowPropertyApplied());
9999 #endif
10002 nsSize oldSize = mRect.Size();
10003 bool sizeChanged = ((aOldSize ? *aOldSize : oldSize) != aNewSize);
10005 // Our frame size may not have been computed and set yet, but code under
10006 // functions such as ComputeEffectsRect (which we're about to call) use the
10007 // values that are stored in our frame rect to compute their results. We
10008 // need the results from those functions to be based on the frame size that
10009 // we *will* have, so we temporarily set our frame size here before calling
10010 // those functions.
10012 // XXX Someone should document here why we revert the frame size before we
10013 // return rather than just leaving it set.
10015 // We pass false here to avoid invalidating display items for this temporary
10016 // change. We sometimes reflow frames multiple times, with the final size
10017 // being the same as the initial. The single call to SetSize after reflow is
10018 // done will take care of invalidating display items if the size has actually
10019 // changed.
10020 SetSize(aNewSize, false);
10022 const auto overflowClipAxes = ShouldApplyOverflowClipping(disp);
10024 if (ChildrenHavePerspective(disp) && sizeChanged) {
10025 RecomputePerspectiveChildrenOverflow(this);
10027 if (overflowClipAxes != PhysicalAxes::Both) {
10028 aOverflowAreas.SetAllTo(bounds);
10029 DebugOnly<bool> ok = ComputeCustomOverflow(aOverflowAreas);
10031 // ComputeCustomOverflow() should not return false, when
10032 // FrameMaintainsOverflow() returns true.
10033 MOZ_ASSERT(ok, "FrameMaintainsOverflow() != ComputeCustomOverflow()");
10035 UnionChildOverflow(aOverflowAreas);
10039 // This is now called FinishAndStoreOverflow() instead of
10040 // StoreOverflow() because frame-generic ways of adding overflow
10041 // can happen here, e.g. CSS2 outline and native theme.
10042 // If the overflow area width or height is nscoord_MAX, then a
10043 // saturating union may have encounted an overflow, so the overflow may not
10044 // contain the frame border-box. Don't warn in that case.
10045 // Don't warn for SVG either, since SVG doesn't need the overflow area
10046 // to contain the frame bounds.
10047 for (const auto otype : AllOverflowTypes()) {
10048 DebugOnly<nsRect*> r = &aOverflowAreas.Overflow(otype);
10049 NS_ASSERTION(aNewSize.width == 0 || aNewSize.height == 0 ||
10050 r->width == nscoord_MAX || r->height == nscoord_MAX ||
10051 HasAnyStateBits(NS_FRAME_SVG_LAYOUT) ||
10052 r->Contains(nsRect(nsPoint(0, 0), aNewSize)),
10053 "Computed overflow area must contain frame bounds");
10056 // Overflow area must always include the frame's top-left and bottom-right,
10057 // even if the frame rect is empty (so we can scroll to those positions).
10058 const bool shouldIncludeBounds = [&] {
10059 if (aNewSize.width == 0 && IsInlineFrame()) {
10060 // Pending a real fix for bug 426879, don't do this for inline frames with
10061 // zero width.
10062 return false;
10064 if (HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
10065 // Do not do this for SVG either, since it will usually massively increase
10066 // the area unnecessarily (except for SVG that applies clipping, since
10067 // that's the pre-existing behavior, and breaks pre-rendering otherwise).
10068 // FIXME(bug 1770704): This check most likely wants to be removed or check
10069 // for specific frame types at least.
10070 return overflowClipAxes != PhysicalAxes::None;
10072 return true;
10073 }();
10075 if (shouldIncludeBounds) {
10076 for (const auto otype : AllOverflowTypes()) {
10077 nsRect& o = aOverflowAreas.Overflow(otype);
10078 o = o.UnionEdges(bounds);
10082 // If we clip our children, clear accumulated overflow area in the affected
10083 // dimension(s). The children are actually clipped to the padding-box, but
10084 // since the overflow area should include the entire border-box, just set it
10085 // to the border-box size here.
10086 if (overflowClipAxes != PhysicalAxes::None) {
10087 aOverflowAreas.ApplyClipping(bounds, overflowClipAxes,
10088 OverflowClipMargin(overflowClipAxes));
10091 ComputeAndIncludeOutlineArea(this, aOverflowAreas, aNewSize);
10093 // Nothing in here should affect scrollable overflow.
10094 aOverflowAreas.InkOverflow() =
10095 ComputeEffectsRect(this, aOverflowAreas.InkOverflow(), aNewSize);
10097 // Absolute position clipping
10098 const nsStyleEffects* effects = StyleEffects();
10099 Maybe<nsRect> clipPropClipRect = GetClipPropClipRect(disp, effects, aNewSize);
10100 if (clipPropClipRect) {
10101 for (const auto otype : AllOverflowTypes()) {
10102 nsRect& o = aOverflowAreas.Overflow(otype);
10103 o.IntersectRect(o, *clipPropClipRect);
10107 /* If we're transformed, transform the overflow rect by the current
10108 * transformation. */
10109 if (hasTransform) {
10110 SetProperty(nsIFrame::PreTransformOverflowAreasProperty(),
10111 new OverflowAreas(aOverflowAreas));
10113 if (Combines3DTransformWithAncestors()) {
10114 /* If we're a preserve-3d leaf frame, then our pre-transform overflow
10115 * should be correct. Our post-transform overflow is empty though, because
10116 * we only contribute to the overflow area of the preserve-3d root frame.
10117 * If we're an intermediate frame then the pre-transform overflow should
10118 * contain all our non-preserve-3d children, which is what we want. Again
10119 * we have no post-transform overflow.
10121 aOverflowAreas.SetAllTo(nsRect());
10122 } else {
10123 TransformReferenceBox refBox(this);
10124 for (const auto otype : AllOverflowTypes()) {
10125 nsRect& o = aOverflowAreas.Overflow(otype);
10126 o = nsDisplayTransform::TransformRect(o, this, refBox);
10129 /* If we're the root of the 3d context, then we want to include the
10130 * overflow areas of all the participants. This won't have happened yet as
10131 * the code above set their overflow area to empty. Manually collect these
10132 * overflow areas now.
10134 if (Extend3DContext(disp, effects)) {
10135 ComputePreserve3DChildrenOverflow(aOverflowAreas);
10138 } else {
10139 RemoveProperty(nsIFrame::PreTransformOverflowAreasProperty());
10142 /* Revert the size change in case some caller is depending on this. */
10143 SetSize(oldSize, false);
10145 bool anyOverflowChanged;
10146 if (aOverflowAreas != OverflowAreas(bounds, bounds)) {
10147 anyOverflowChanged = SetOverflowAreas(aOverflowAreas);
10148 } else {
10149 anyOverflowChanged = ClearOverflowRects();
10152 if (anyOverflowChanged) {
10153 SVGObserverUtils::InvalidateDirectRenderingObservers(this);
10154 if (nsBlockFrame* block = do_QueryFrame(this)) {
10155 // NOTE(emilio): we need to use BeforeReflow::Yes, because we want to
10156 // invalidate in cases where we _used_ to have an overflow marker and no
10157 // longer do.
10158 if (TextOverflow::CanHaveOverflowMarkers(
10159 block, TextOverflow::BeforeReflow::Yes)) {
10160 DiscardDisplayItems(this, [](nsDisplayItem* aItem) {
10161 return aItem->GetType() == DisplayItemType::TYPE_TEXT_OVERFLOW;
10163 SchedulePaint(PAINT_DEFAULT);
10167 return anyOverflowChanged;
10170 void nsIFrame::RecomputePerspectiveChildrenOverflow(
10171 const nsIFrame* aStartFrame) {
10172 for (const auto& childList : ChildLists()) {
10173 for (nsIFrame* child : childList.mList) {
10174 if (!child->FrameMaintainsOverflow()) {
10175 continue; // frame does not maintain overflow rects
10177 if (child->HasPerspective()) {
10178 OverflowAreas* overflow =
10179 child->GetProperty(nsIFrame::InitialOverflowProperty());
10180 nsRect bounds(nsPoint(0, 0), child->GetSize());
10181 if (overflow) {
10182 OverflowAreas overflowCopy = *overflow;
10183 child->FinishAndStoreOverflow(overflowCopy, bounds.Size());
10184 } else {
10185 OverflowAreas boundsOverflow;
10186 boundsOverflow.SetAllTo(bounds);
10187 child->FinishAndStoreOverflow(boundsOverflow, bounds.Size());
10189 } else if (child->GetContent() == aStartFrame->GetContent() ||
10190 child->GetClosestFlattenedTreeAncestorPrimaryFrame() ==
10191 aStartFrame) {
10192 // If a frame is using perspective, then the size used to compute
10193 // perspective-origin is the size of the frame belonging to its parent
10194 // style. We must find any descendant frames using our size
10195 // (by recursing into frames that have the same containing block)
10196 // to update their overflow rects too.
10197 child->RecomputePerspectiveChildrenOverflow(aStartFrame);
10203 void nsIFrame::ComputePreserve3DChildrenOverflow(
10204 OverflowAreas& aOverflowAreas) {
10205 // Find all descendants that participate in the 3d context, and include their
10206 // overflow. These descendants have an empty overflow, so won't have been
10207 // included in the normal overflow calculation. Any children that don't
10208 // participate have normal overflow, so will have been included already.
10210 nsRect childVisual;
10211 nsRect childScrollable;
10212 for (const auto& childList : ChildLists()) {
10213 for (nsIFrame* child : childList.mList) {
10214 // If this child participates in the 3d context, then take the
10215 // pre-transform region (which contains all descendants that aren't
10216 // participating in the 3d context) and transform it into the 3d context
10217 // root coordinate space.
10218 if (child->Combines3DTransformWithAncestors()) {
10219 OverflowAreas childOverflow = child->GetOverflowAreasRelativeToSelf();
10220 TransformReferenceBox refBox(child);
10221 for (const auto otype : AllOverflowTypes()) {
10222 nsRect& o = childOverflow.Overflow(otype);
10223 o = nsDisplayTransform::TransformRect(o, child, refBox);
10226 aOverflowAreas.UnionWith(childOverflow);
10228 // If this child also extends the 3d context, then recurse into it
10229 // looking for more participants.
10230 if (child->Extend3DContext()) {
10231 child->ComputePreserve3DChildrenOverflow(aOverflowAreas);
10238 bool nsIFrame::ZIndexApplies() const {
10239 return StyleDisplay()->IsPositionedStyle() || IsFlexOrGridItem() ||
10240 IsMenuPopupFrame();
10243 Maybe<int32_t> nsIFrame::ZIndex() const {
10244 if (!ZIndexApplies()) {
10245 return Nothing();
10247 const auto& zIndex = StylePosition()->mZIndex;
10248 if (zIndex.IsAuto()) {
10249 return Nothing();
10251 return Some(zIndex.AsInteger());
10254 bool nsIFrame::IsScrollAnchor(ScrollAnchorContainer** aOutContainer) {
10255 if (!mInScrollAnchorChain) {
10256 return false;
10259 nsIFrame* f = this;
10261 // FIXME(emilio, bug 1629280): We should find a non-null anchor if we have the
10262 // flag set, but bug 1629280 makes it so that we cannot really assert it /
10263 // make this just a `while (true)`, and uncomment the below assertion.
10264 while (auto* container = ScrollAnchorContainer::FindFor(f)) {
10265 // MOZ_ASSERT(f->IsInScrollAnchorChain());
10266 if (nsIFrame* anchor = container->AnchorNode()) {
10267 if (anchor != this) {
10268 return false;
10270 if (aOutContainer) {
10271 *aOutContainer = container;
10273 return true;
10276 f = container->Frame();
10279 return false;
10282 bool nsIFrame::IsInScrollAnchorChain() const { return mInScrollAnchorChain; }
10284 void nsIFrame::SetInScrollAnchorChain(bool aInChain) {
10285 mInScrollAnchorChain = aInChain;
10288 uint32_t nsIFrame::GetDepthInFrameTree() const {
10289 uint32_t result = 0;
10290 for (nsContainerFrame* ancestor = GetParent(); ancestor;
10291 ancestor = ancestor->GetParent()) {
10292 result++;
10294 return result;
10298 * This function takes a frame that is part of a block-in-inline split,
10299 * and _if_ that frame is an anonymous block created by an ib split it
10300 * returns the block's preceding inline. This is needed because the
10301 * split inline's style is the parent of the anonymous block's style.
10303 * If aFrame is not an anonymous block, null is returned.
10305 static nsIFrame* GetIBSplitSiblingForAnonymousBlock(const nsIFrame* aFrame) {
10306 MOZ_ASSERT(aFrame, "Must have a non-null frame!");
10307 NS_ASSERTION(aFrame->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT),
10308 "GetIBSplitSibling should only be called on ib-split frames");
10310 if (aFrame->Style()->GetPseudoType() !=
10311 PseudoStyleType::mozBlockInsideInlineWrapper) {
10312 // it's not an anonymous block
10313 return nullptr;
10316 // Find the first continuation of the frame. (Ugh. This ends up
10317 // being O(N^2) when it is called O(N) times.)
10318 aFrame = aFrame->FirstContinuation();
10321 * Now look up the nsGkAtoms::IBSplitPrevSibling
10322 * property.
10324 nsIFrame* ibSplitSibling =
10325 aFrame->GetProperty(nsIFrame::IBSplitPrevSibling());
10326 NS_ASSERTION(ibSplitSibling, "Broken frame tree?");
10327 return ibSplitSibling;
10331 * Get the parent, corrected for the mangled frame tree resulting from
10332 * having a block within an inline. The result only differs from the
10333 * result of |GetParent| when |GetParent| returns an anonymous block
10334 * that was created for an element that was 'display: inline' because
10335 * that element contained a block.
10337 * Also skip anonymous scrolled-content parents; inherit directly from the
10338 * outer scroll frame.
10340 * Also skip NAC parents if the child frame is NAC.
10342 static nsIFrame* GetCorrectedParent(const nsIFrame* aFrame) {
10343 nsIFrame* parent = aFrame->GetParent();
10344 if (!parent) {
10345 return nullptr;
10348 // For a table caption we want the _inner_ table frame (unless it's anonymous)
10349 // as the style parent.
10350 if (aFrame->IsTableCaption()) {
10351 nsIFrame* innerTable = parent->PrincipalChildList().FirstChild();
10352 if (!innerTable->Style()->IsAnonBox()) {
10353 return innerTable;
10357 // Table wrappers are always anon boxes; if we're in here for an outer
10358 // table, that actually means its the _inner_ table that wants to
10359 // know its parent. So get the pseudo of the inner in that case.
10360 auto pseudo = aFrame->Style()->GetPseudoType();
10361 if (pseudo == PseudoStyleType::tableWrapper) {
10362 pseudo =
10363 aFrame->PrincipalChildList().FirstChild()->Style()->GetPseudoType();
10366 // Prevent a NAC pseudo-element from inheriting from its NAC parent, and
10367 // inherit from the NAC generator element instead.
10368 if (pseudo != PseudoStyleType::NotPseudo) {
10369 MOZ_ASSERT(aFrame->GetContent());
10370 Element* element = Element::FromNode(aFrame->GetContent());
10371 // Make sure to avoid doing the fixup for non-element-backed pseudos like
10372 // ::first-line and such.
10373 if (element && !element->IsRootOfNativeAnonymousSubtree() &&
10374 element->GetPseudoElementType() == aFrame->Style()->GetPseudoType()) {
10375 while (parent->GetContent() &&
10376 !parent->GetContent()->IsRootOfNativeAnonymousSubtree()) {
10377 parent = parent->GetInFlowParent();
10379 parent = parent->GetInFlowParent();
10383 return nsIFrame::CorrectStyleParentFrame(parent, pseudo);
10386 /* static */
10387 nsIFrame* nsIFrame::CorrectStyleParentFrame(nsIFrame* aProspectiveParent,
10388 PseudoStyleType aChildPseudo) {
10389 MOZ_ASSERT(aProspectiveParent, "Must have a prospective parent");
10391 if (aChildPseudo != PseudoStyleType::NotPseudo) {
10392 // Non-inheriting anon boxes have no style parent frame at all.
10393 if (PseudoStyle::IsNonInheritingAnonBox(aChildPseudo)) {
10394 return nullptr;
10397 // Other anon boxes are parented to their actual parent already, except
10398 // for non-elements. Those should not be treated as an anon box.
10399 if (PseudoStyle::IsAnonBox(aChildPseudo) &&
10400 !nsCSSAnonBoxes::IsNonElement(aChildPseudo)) {
10401 NS_ASSERTION(aChildPseudo != PseudoStyleType::mozBlockInsideInlineWrapper,
10402 "Should have dealt with kids that have "
10403 "NS_FRAME_PART_OF_IBSPLIT elsewhere");
10404 return aProspectiveParent;
10408 // Otherwise, walk up out of all anon boxes. For placeholder frames, walk out
10409 // of all pseudo-elements as well. Otherwise ReparentComputedStyle could
10410 // cause style data to be out of sync with the frame tree.
10411 nsIFrame* parent = aProspectiveParent;
10412 do {
10413 if (parent->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
10414 nsIFrame* sibling = GetIBSplitSiblingForAnonymousBlock(parent);
10416 if (sibling) {
10417 // |parent| was a block in an {ib} split; use the inline as
10418 // |the style parent.
10419 parent = sibling;
10423 if (!parent->Style()->IsPseudoOrAnonBox()) {
10424 return parent;
10427 if (!parent->Style()->IsAnonBox() && aChildPseudo != PseudoStyleType::MAX) {
10428 // nsPlaceholderFrame passes in PseudoStyleType::MAX for
10429 // aChildPseudo (even though that's not a valid pseudo-type) just to
10430 // trigger this behavior of walking up to the nearest non-pseudo
10431 // ancestor.
10432 return parent;
10435 parent = parent->GetInFlowParent();
10436 } while (parent);
10438 if (aProspectiveParent->Style()->GetPseudoType() ==
10439 PseudoStyleType::viewportScroll) {
10440 // aProspectiveParent is the scrollframe for a viewport
10441 // and the kids are the anonymous scrollbars
10442 return aProspectiveParent;
10445 // We can get here if the root element is absolutely positioned.
10446 // We can't test for this very accurately, but it can only happen
10447 // when the prospective parent is a canvas frame.
10448 NS_ASSERTION(aProspectiveParent->IsCanvasFrame(),
10449 "Should have found a parent before this");
10450 return nullptr;
10453 ComputedStyle* nsIFrame::DoGetParentComputedStyle(
10454 nsIFrame** aProviderFrame) const {
10455 *aProviderFrame = nullptr;
10457 // Handle display:contents and the root frame, when there's no parent frame
10458 // to inherit from.
10459 if (MOZ_LIKELY(mContent)) {
10460 Element* parentElement = mContent->GetFlattenedTreeParentElement();
10461 if (MOZ_LIKELY(parentElement)) {
10462 auto pseudo = Style()->GetPseudoType();
10463 if (pseudo == PseudoStyleType::NotPseudo || !mContent->IsElement() ||
10464 (!PseudoStyle::IsAnonBox(pseudo) &&
10465 // Ensure that we don't return the display:contents style
10466 // of the parent content for pseudos that have the same content
10467 // as their primary frame (like -moz-list-bullets do):
10468 IsPrimaryFrame()) ||
10469 /* if next is true then it's really a request for the table frame's
10470 parent context, see nsTable[Outer]Frame::GetParentComputedStyle. */
10471 pseudo == PseudoStyleType::tableWrapper) {
10472 // In some edge cases involving display: contents, we may end up here
10473 // for something that's pending to be reframed. In this case we return
10474 // the wrong style from here (because we've already lost track of it!),
10475 // but it's not a big deal as we're going to be reframed anyway.
10476 if (MOZ_LIKELY(parentElement->HasServoData()) &&
10477 Servo_Element_IsDisplayContents(parentElement)) {
10478 RefPtr<ComputedStyle> style =
10479 ServoStyleSet::ResolveServoStyle(*parentElement);
10480 // NOTE(emilio): we return a weak reference because the element also
10481 // holds the style context alive. This is a bit silly (we could've
10482 // returned a weak ref directly), but it's probably not worth
10483 // optimizing, given this function has just one caller which is rare,
10484 // and this path is rare itself.
10485 return style;
10488 } else {
10489 if (Style()->GetPseudoType() == PseudoStyleType::NotPseudo) {
10490 // We're a frame for the root. We have no style parent.
10491 return nullptr;
10496 if (!HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
10498 * If this frame is an anonymous block created when an inline with a block
10499 * inside it got split, then the parent style is on its preceding inline. We
10500 * can get to it using GetIBSplitSiblingForAnonymousBlock.
10502 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
10503 nsIFrame* ibSplitSibling = GetIBSplitSiblingForAnonymousBlock(this);
10504 if (ibSplitSibling) {
10505 return (*aProviderFrame = ibSplitSibling)->Style();
10509 // If this frame is one of the blocks that split an inline, we must
10510 // return the "special" inline parent, i.e., the parent that this
10511 // frame would have if we didn't mangle the frame structure.
10512 *aProviderFrame = GetCorrectedParent(this);
10513 return *aProviderFrame ? (*aProviderFrame)->Style() : nullptr;
10516 // We're an out-of-flow frame. For out-of-flow frames, we must
10517 // resolve underneath the placeholder's parent. The placeholder is
10518 // reached from the first-in-flow.
10519 nsPlaceholderFrame* placeholder = FirstInFlow()->GetPlaceholderFrame();
10520 if (!placeholder) {
10521 MOZ_ASSERT_UNREACHABLE("no placeholder frame for out-of-flow frame");
10522 *aProviderFrame = GetCorrectedParent(this);
10523 return *aProviderFrame ? (*aProviderFrame)->Style() : nullptr;
10525 return placeholder->GetParentComputedStyleForOutOfFlow(aProviderFrame);
10528 void nsIFrame::GetLastLeaf(nsIFrame** aFrame) {
10529 if (!aFrame || !*aFrame) return;
10530 nsIFrame* child = *aFrame;
10531 // if we are a block frame then go for the last line of 'this'
10532 while (1) {
10533 child = child->PrincipalChildList().FirstChild();
10534 if (!child) return; // nothing to do
10535 nsIFrame* siblingFrame;
10536 nsIContent* content;
10537 // ignore anonymous elements, e.g. mozTableAdd* mozTableRemove*
10538 // see bug 278197 comment #12 #13 for details
10539 while ((siblingFrame = child->GetNextSibling()) &&
10540 (content = siblingFrame->GetContent()) &&
10541 !content->IsRootOfNativeAnonymousSubtree())
10542 child = siblingFrame;
10543 *aFrame = child;
10547 void nsIFrame::GetFirstLeaf(nsIFrame** aFrame) {
10548 if (!aFrame || !*aFrame) return;
10549 nsIFrame* child = *aFrame;
10550 while (1) {
10551 child = child->PrincipalChildList().FirstChild();
10552 if (!child) return; // nothing to do
10553 *aFrame = child;
10557 bool nsIFrame::IsFocusableDueToScrollFrame() {
10558 if (!IsScrollFrame()) {
10559 if (nsFieldSetFrame* fieldset = do_QueryFrame(this)) {
10560 // TODO: Do we have similar special-cases like this where we can have
10561 // anonymous scrollable boxes hanging off a primary frame?
10562 if (nsIFrame* inner = fieldset->GetInner()) {
10563 return inner->IsFocusableDueToScrollFrame();
10566 return false;
10568 if (!mContent->IsHTMLElement()) {
10569 return false;
10571 if (mContent->IsRootOfNativeAnonymousSubtree()) {
10572 return false;
10574 if (!mContent->GetParent()) {
10575 return false;
10577 if (mContent->AsElement()->HasAttr(nsGkAtoms::tabindex)) {
10578 return false;
10580 // Elements with scrollable view are focusable with script & tabbable
10581 // Otherwise you couldn't scroll them with keyboard, which is an accessibility
10582 // issue (e.g. Section 508 rules) However, we don't make them to be focusable
10583 // with the mouse, because the extra focus outlines are considered
10584 // unnecessarily ugly. When clicked on, the selection position within the
10585 // element will be enough to make them keyboard scrollable.
10586 nsIScrollableFrame* scrollFrame = do_QueryFrame(this);
10587 if (!scrollFrame) {
10588 return false;
10590 if (scrollFrame->IsForTextControlWithNoScrollbars()) {
10591 return false;
10593 if (scrollFrame->GetScrollStyles().IsHiddenInBothDirections()) {
10594 return false;
10596 if (scrollFrame->GetScrollRange().IsEqualEdges(nsRect(0, 0, 0, 0))) {
10597 return false;
10599 return true;
10602 nsIFrame::Focusable nsIFrame::IsFocusable(bool aWithMouse,
10603 bool aCheckVisibility) {
10604 // cannot focus content in print preview mode. Only the root can be focused,
10605 // but that's handled elsewhere.
10606 if (PresContext()->Type() == nsPresContext::eContext_PrintPreview) {
10607 return {};
10610 if (!mContent || !mContent->IsElement()) {
10611 return {};
10614 if (aCheckVisibility && !IsVisibleConsideringAncestors()) {
10615 return {};
10618 const nsStyleUI& ui = *StyleUI();
10619 if (ui.IsInert()) {
10620 return {};
10623 PseudoStyleType pseudo = Style()->GetPseudoType();
10624 if (pseudo == PseudoStyleType::anonymousItem) {
10625 return {};
10628 int32_t tabIndex = -1;
10629 if (ui.UserFocus() != StyleUserFocus::Ignore &&
10630 ui.UserFocus() != StyleUserFocus::None) {
10631 // Pass in default tabindex of -1 for nonfocusable and 0 for focusable
10632 tabIndex = 0;
10635 if (mContent->IsFocusable(&tabIndex, aWithMouse)) {
10636 // If the content is focusable, then we're done.
10637 return {true, tabIndex};
10640 // If we're focusing with the mouse we never focus scroll areas.
10641 if (!aWithMouse && IsFocusableDueToScrollFrame()) {
10642 return {true, 0};
10645 return {false, tabIndex};
10649 * @return true if this text frame ends with a newline character which is
10650 * treated as preformatted. It should return false if this is not a text frame.
10652 bool nsIFrame::HasSignificantTerminalNewline() const { return false; }
10654 static StyleVerticalAlignKeyword ConvertSVGDominantBaselineToVerticalAlign(
10655 StyleDominantBaseline aDominantBaseline) {
10656 // Most of these are approximate mappings.
10657 switch (aDominantBaseline) {
10658 case StyleDominantBaseline::Hanging:
10659 case StyleDominantBaseline::TextBeforeEdge:
10660 return StyleVerticalAlignKeyword::TextTop;
10661 case StyleDominantBaseline::TextAfterEdge:
10662 case StyleDominantBaseline::Ideographic:
10663 return StyleVerticalAlignKeyword::TextBottom;
10664 case StyleDominantBaseline::Central:
10665 case StyleDominantBaseline::Middle:
10666 case StyleDominantBaseline::Mathematical:
10667 return StyleVerticalAlignKeyword::Middle;
10668 case StyleDominantBaseline::Auto:
10669 case StyleDominantBaseline::Alphabetic:
10670 return StyleVerticalAlignKeyword::Baseline;
10671 default:
10672 MOZ_ASSERT_UNREACHABLE("unexpected aDominantBaseline value");
10673 return StyleVerticalAlignKeyword::Baseline;
10677 Maybe<StyleVerticalAlignKeyword> nsIFrame::VerticalAlignEnum() const {
10678 if (IsInSVGTextSubtree()) {
10679 StyleDominantBaseline dominantBaseline = StyleSVG()->mDominantBaseline;
10680 return Some(ConvertSVGDominantBaselineToVerticalAlign(dominantBaseline));
10683 const auto& verticalAlign = StyleDisplay()->mVerticalAlign;
10684 if (verticalAlign.IsKeyword()) {
10685 return Some(verticalAlign.AsKeyword());
10688 return Nothing();
10691 void nsIFrame::UpdateStyleOfChildAnonBox(nsIFrame* aChildFrame,
10692 ServoRestyleState& aRestyleState) {
10693 #ifdef DEBUG
10694 nsIFrame* parent = aChildFrame->GetInFlowParent();
10695 if (aChildFrame->IsTableFrame()) {
10696 parent = parent->GetParent();
10698 if (parent->IsLineFrame()) {
10699 parent = parent->GetParent();
10701 MOZ_ASSERT(nsLayoutUtils::FirstContinuationOrIBSplitSibling(parent) == this,
10702 "This should only be used for children!");
10703 #endif // DEBUG
10704 MOZ_ASSERT(!GetContent() || !aChildFrame->GetContent() ||
10705 aChildFrame->GetContent() == GetContent(),
10706 "What content node is it a frame for?");
10707 MOZ_ASSERT(!aChildFrame->GetPrevContinuation(),
10708 "Only first continuations should end up here");
10710 // We could force the caller to pass in the pseudo, since some callers know it
10711 // statically... But this API is a bit nicer.
10712 auto pseudo = aChildFrame->Style()->GetPseudoType();
10713 MOZ_ASSERT(PseudoStyle::IsAnonBox(pseudo), "Child is not an anon box?");
10714 MOZ_ASSERT(!PseudoStyle::IsNonInheritingAnonBox(pseudo),
10715 "Why did the caller bother calling us?");
10717 // Anon boxes inherit from their parent; that's us.
10718 RefPtr<ComputedStyle> newContext =
10719 aRestyleState.StyleSet().ResolveInheritingAnonymousBoxStyle(pseudo,
10720 Style());
10722 nsChangeHint childHint =
10723 UpdateStyleOfOwnedChildFrame(aChildFrame, newContext, aRestyleState);
10725 // Now that we've updated the style on aChildFrame, check whether it itself
10726 // has anon boxes to deal with.
10727 ServoRestyleState childrenState(*aChildFrame, aRestyleState, childHint,
10728 ServoRestyleState::Type::InFlow);
10729 aChildFrame->UpdateStyleOfOwnedAnonBoxes(childrenState);
10731 // Assuming anon boxes don't have ::backdrop associated with them... if that
10732 // ever changes, we'd need to handle that here, like we do in
10733 // RestyleManager::ProcessPostTraversal
10735 // We do need to handle block pseudo-elements here, though. Especially list
10736 // bullets.
10737 if (nsBlockFrame* block = do_QueryFrame(aChildFrame)) {
10738 block->UpdatePseudoElementStyles(childrenState);
10742 /* static */
10743 nsChangeHint nsIFrame::UpdateStyleOfOwnedChildFrame(
10744 nsIFrame* aChildFrame, ComputedStyle* aNewComputedStyle,
10745 ServoRestyleState& aRestyleState,
10746 const Maybe<ComputedStyle*>& aContinuationComputedStyle) {
10747 MOZ_ASSERT(!aChildFrame->GetAdditionalComputedStyle(0),
10748 "We don't handle additional styles here");
10750 // Figure out whether we have an actual change. It's important that we do
10751 // this, for several reasons:
10753 // 1) Even if all the child's changes are due to properties it inherits from
10754 // us, it's possible that no one ever asked us for those style structs and
10755 // hence changes to them aren't reflected in the changes handled at all.
10757 // 2) Content can change stylesheets that change the styles of pseudos, and
10758 // extensions can add/remove stylesheets that change the styles of
10759 // anonymous boxes directly.
10760 uint32_t equalStructs; // Not used, actually.
10761 nsChangeHint childHint = aChildFrame->Style()->CalcStyleDifference(
10762 *aNewComputedStyle, &equalStructs);
10764 // If aChildFrame is out of flow, then aRestyleState's "changes handled by the
10765 // parent" doesn't apply to it, because it may have some other parent in the
10766 // frame tree.
10767 if (!aChildFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
10768 childHint = NS_RemoveSubsumedHints(
10769 childHint, aRestyleState.ChangesHandledFor(aChildFrame));
10771 if (childHint) {
10772 if (childHint & nsChangeHint_ReconstructFrame) {
10773 // If we generate a reconstruct here, remove any non-reconstruct hints we
10774 // may have already generated for this content.
10775 aRestyleState.ChangeList().PopChangesForContent(
10776 aChildFrame->GetContent());
10778 aRestyleState.ChangeList().AppendChange(
10779 aChildFrame, aChildFrame->GetContent(), childHint);
10782 aChildFrame->SetComputedStyle(aNewComputedStyle);
10783 ComputedStyle* continuationStyle = aContinuationComputedStyle
10784 ? *aContinuationComputedStyle
10785 : aNewComputedStyle;
10786 for (nsIFrame* kid = aChildFrame->GetNextContinuation(); kid;
10787 kid = kid->GetNextContinuation()) {
10788 MOZ_ASSERT(!kid->GetAdditionalComputedStyle(0));
10789 kid->SetComputedStyle(continuationStyle);
10792 return childHint;
10795 /* static */
10796 void nsIFrame::AddInPopupStateBitToDescendants(nsIFrame* aFrame) {
10797 if (!aFrame->HasAnyStateBits(NS_FRAME_IN_POPUP) &&
10798 aFrame->TrackingVisibility()) {
10799 // Assume all frames in popups are visible.
10800 aFrame->IncApproximateVisibleCount();
10803 aFrame->AddStateBits(NS_FRAME_IN_POPUP);
10805 for (const auto& childList : aFrame->CrossDocChildLists()) {
10806 for (nsIFrame* child : childList.mList) {
10807 AddInPopupStateBitToDescendants(child);
10812 /* static */
10813 void nsIFrame::RemoveInPopupStateBitFromDescendants(nsIFrame* aFrame) {
10814 if (!aFrame->HasAnyStateBits(NS_FRAME_IN_POPUP) ||
10815 nsLayoutUtils::IsPopup(aFrame)) {
10816 return;
10819 aFrame->RemoveStateBits(NS_FRAME_IN_POPUP);
10821 if (aFrame->TrackingVisibility()) {
10822 // We assume all frames in popups are visible, so this decrement balances
10823 // out the increment in AddInPopupStateBitToDescendants above.
10824 aFrame->DecApproximateVisibleCount();
10826 for (const auto& childList : aFrame->CrossDocChildLists()) {
10827 for (nsIFrame* child : childList.mList) {
10828 RemoveInPopupStateBitFromDescendants(child);
10833 void nsIFrame::SetParent(nsContainerFrame* aParent) {
10834 // If our parent is a wrapper anon box, our new parent should be too. We
10835 // _can_ change parent if our parent is a wrapper anon box, because some
10836 // wrapper anon boxes can have continuations.
10837 MOZ_ASSERT_IF(ParentIsWrapperAnonBox(),
10838 aParent->Style()->IsInheritingAnonBox());
10840 // Note that the current mParent may already be destroyed at this point.
10841 mParent = aParent;
10842 MOZ_DIAGNOSTIC_ASSERT(!mParent || PresShell() == mParent->PresShell());
10844 if (HasAnyStateBits(NS_FRAME_HAS_VIEW | NS_FRAME_HAS_CHILD_WITH_VIEW)) {
10845 for (nsIFrame* f = aParent;
10846 f && !f->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
10847 f = f->GetParent()) {
10848 f->AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
10852 if (HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
10853 for (nsIFrame* f = aParent; f; f = f->GetParent()) {
10854 if (f->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
10855 break;
10857 f->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE);
10861 if (HasAnyStateBits(NS_FRAME_DESCENDANT_INTRINSIC_ISIZE_DEPENDS_ON_BSIZE)) {
10862 for (nsIFrame* f = aParent; f; f = f->GetParent()) {
10863 if (f->HasAnyStateBits(
10864 NS_FRAME_DESCENDANT_INTRINSIC_ISIZE_DEPENDS_ON_BSIZE)) {
10865 break;
10867 f->AddStateBits(NS_FRAME_DESCENDANT_INTRINSIC_ISIZE_DEPENDS_ON_BSIZE);
10871 if (HasInvalidFrameInSubtree()) {
10872 for (nsIFrame* f = aParent;
10873 f && !f->HasAnyStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT |
10874 NS_FRAME_IS_NONDISPLAY);
10875 f = nsLayoutUtils::GetCrossDocParentFrameInProcess(f)) {
10876 f->AddStateBits(NS_FRAME_DESCENDANT_NEEDS_PAINT);
10880 if (aParent->HasAnyStateBits(NS_FRAME_IN_POPUP)) {
10881 AddInPopupStateBitToDescendants(this);
10882 } else {
10883 RemoveInPopupStateBitFromDescendants(this);
10886 // If our new parent only has invalid children, then we just invalidate
10887 // ourselves too. This is probably faster than clearing the flag all
10888 // the way up the frame tree.
10889 if (aParent->HasAnyStateBits(NS_FRAME_ALL_DESCENDANTS_NEED_PAINT)) {
10890 InvalidateFrame();
10891 } else {
10892 SchedulePaint();
10896 bool nsIFrame::IsStackingContext(const nsStyleDisplay* aStyleDisplay,
10897 const nsStyleEffects* aStyleEffects) {
10898 // Properties that influence the output of this function should be handled in
10899 // change_bits_for_longhand as well.
10900 if (HasOpacity(aStyleDisplay, aStyleEffects, nullptr)) {
10901 return true;
10903 if (IsTransformed()) {
10904 return true;
10906 auto willChange = aStyleDisplay->mWillChange.bits;
10907 if (aStyleDisplay->IsContainPaint() || aStyleDisplay->IsContainLayout() ||
10908 willChange & StyleWillChangeBits::CONTAIN) {
10909 if (IsFrameOfType(eSupportsContainLayoutAndPaint)) {
10910 return true;
10913 // strictly speaking, 'perspective' doesn't require visual atomicity,
10914 // but the spec says it acts like the rest of these
10915 if (aStyleDisplay->HasPerspectiveStyle() ||
10916 willChange & StyleWillChangeBits::PERSPECTIVE) {
10917 if (IsFrameOfType(eSupportsCSSTransforms)) {
10918 return true;
10921 if (!StylePosition()->mZIndex.IsAuto() ||
10922 willChange & StyleWillChangeBits::Z_INDEX) {
10923 if (ZIndexApplies()) {
10924 return true;
10927 return aStyleEffects->mMixBlendMode != StyleBlend::Normal ||
10928 SVGIntegrationUtils::UsingEffectsForFrame(this) ||
10929 aStyleDisplay->IsPositionForcingStackingContext() ||
10930 aStyleDisplay->mIsolation != StyleIsolation::Auto ||
10931 willChange & StyleWillChangeBits::STACKING_CONTEXT_UNCONDITIONAL;
10934 bool nsIFrame::IsStackingContext() {
10935 return IsStackingContext(StyleDisplay(), StyleEffects());
10938 static bool IsFrameScrolledOutOfView(const nsIFrame* aTarget,
10939 const nsRect& aTargetRect,
10940 const nsIFrame* aParent) {
10941 // The ancestor frame we are checking if it clips out aTargetRect relative to
10942 // aTarget.
10943 nsIFrame* clipParent = nullptr;
10945 // find the first scrollable frame or root frame if we are in a fixed pos
10946 // subtree
10947 for (nsIFrame* f = const_cast<nsIFrame*>(aParent); f;
10948 f = nsLayoutUtils::GetCrossDocParentFrameInProcess(f)) {
10949 nsIScrollableFrame* scrollableFrame = do_QueryFrame(f);
10950 if (scrollableFrame) {
10951 clipParent = f;
10952 break;
10954 if (f->StyleDisplay()->mPosition == StylePositionProperty::Fixed &&
10955 nsLayoutUtils::IsReallyFixedPos(f)) {
10956 clipParent = f->GetParent();
10957 break;
10961 if (!clipParent) {
10962 // Even if we couldn't find the nearest scrollable frame, it might mean we
10963 // are in an out-of-process iframe, try to see if |aTarget| frame is
10964 // scrolled out of view in an scrollable frame in a cross-process ancestor
10965 // document.
10966 return nsLayoutUtils::FrameIsScrolledOutOfViewInCrossProcess(aTarget);
10969 nsRect clipRect = clipParent->InkOverflowRectRelativeToSelf();
10970 // We consider that the target is scrolled out if the scrollable (or root)
10971 // frame is empty.
10972 if (clipRect.IsEmpty()) {
10973 return true;
10976 nsRect transformedRect = nsLayoutUtils::TransformFrameRectToAncestor(
10977 aTarget, aTargetRect, clipParent);
10979 if (transformedRect.IsEmpty()) {
10980 // If the transformed rect is empty it represents a line or a point that we
10981 // should check is outside the the scrollable rect.
10982 if (transformedRect.x > clipRect.XMost() ||
10983 transformedRect.y > clipRect.YMost() ||
10984 clipRect.x > transformedRect.XMost() ||
10985 clipRect.y > transformedRect.YMost()) {
10986 return true;
10988 } else if (!transformedRect.Intersects(clipRect)) {
10989 return true;
10992 nsIFrame* parent = clipParent->GetParent();
10993 if (!parent) {
10994 return false;
10997 return IsFrameScrolledOutOfView(aTarget, aTargetRect, parent);
11000 bool nsIFrame::IsScrolledOutOfView() const {
11001 nsRect rect = InkOverflowRectRelativeToSelf();
11002 return IsFrameScrolledOutOfView(this, rect, this);
11005 gfx::Matrix nsIFrame::ComputeWidgetTransform() const {
11006 const nsStyleUIReset* uiReset = StyleUIReset();
11007 if (uiReset->mMozWindowTransform.IsNone()) {
11008 return gfx::Matrix();
11011 TransformReferenceBox refBox(nullptr, nsRect(nsPoint(), GetSize()));
11013 nsPresContext* presContext = PresContext();
11014 int32_t appUnitsPerDevPixel = presContext->AppUnitsPerDevPixel();
11015 gfx::Matrix4x4 matrix = nsStyleTransformMatrix::ReadTransforms(
11016 uiReset->mMozWindowTransform, refBox, float(appUnitsPerDevPixel));
11018 // Apply the -moz-window-transform-origin translation to the matrix.
11019 const StyleTransformOrigin& origin = uiReset->mWindowTransformOrigin;
11020 Point transformOrigin = nsStyleTransformMatrix::Convert2DPosition(
11021 origin.horizontal, origin.vertical, refBox, appUnitsPerDevPixel);
11022 matrix.ChangeBasis(Point3D(transformOrigin.x, transformOrigin.y, 0));
11024 gfx::Matrix result2d;
11025 if (!matrix.CanDraw2D(&result2d)) {
11026 // FIXME: It would be preferable to reject non-2D transforms at parse time.
11027 NS_WARNING(
11028 "-moz-window-transform does not describe a 2D transform, "
11029 "but only 2d transforms are supported");
11030 return gfx::Matrix();
11033 return result2d;
11036 void nsIFrame::DoUpdateStyleOfOwnedAnonBoxes(ServoRestyleState& aRestyleState) {
11037 // As a special case, we check for {ib}-split block frames here, rather
11038 // than have an nsInlineFrame::AppendDirectlyOwnedAnonBoxes implementation
11039 // that returns them.
11041 // (If we did handle them in AppendDirectlyOwnedAnonBoxes, we would have to
11042 // return *all* of the in-flow {ib}-split block frames, not just the first
11043 // one. For restyling, we really just need the first in flow, and the other
11044 // user of the AppendOwnedAnonBoxes API, AllChildIterator, doesn't need to
11045 // know about them at all, since these block frames never create NAC. So we
11046 // avoid any unncessary hashtable lookups for the {ib}-split frames by calling
11047 // UpdateStyleOfOwnedAnonBoxesForIBSplit directly here.)
11048 if (IsInlineFrame()) {
11049 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
11050 static_cast<nsInlineFrame*>(this)->UpdateStyleOfOwnedAnonBoxesForIBSplit(
11051 aRestyleState);
11053 return;
11056 AutoTArray<OwnedAnonBox, 4> frames;
11057 AppendDirectlyOwnedAnonBoxes(frames);
11058 for (OwnedAnonBox& box : frames) {
11059 if (box.mUpdateStyleFn) {
11060 box.mUpdateStyleFn(this, box.mAnonBoxFrame, aRestyleState);
11061 } else {
11062 UpdateStyleOfChildAnonBox(box.mAnonBoxFrame, aRestyleState);
11067 /* virtual */
11068 void nsIFrame::AppendDirectlyOwnedAnonBoxes(nsTArray<OwnedAnonBox>& aResult) {
11069 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES));
11070 MOZ_ASSERT(false, "Why did this get called?");
11073 void nsIFrame::DoAppendOwnedAnonBoxes(nsTArray<OwnedAnonBox>& aResult) {
11074 size_t i = aResult.Length();
11075 AppendDirectlyOwnedAnonBoxes(aResult);
11077 // After appending the directly owned anonymous boxes of this frame to
11078 // aResult above, we need to check each of them to see if they own
11079 // any anonymous boxes themselves. Note that we keep progressing
11080 // through aResult, looking for additional entries in aResult from these
11081 // subsequent AppendDirectlyOwnedAnonBoxes calls. (Thus we can't
11082 // use a ranged for loop here.)
11084 while (i < aResult.Length()) {
11085 nsIFrame* f = aResult[i].mAnonBoxFrame;
11086 if (f->HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES)) {
11087 f->AppendDirectlyOwnedAnonBoxes(aResult);
11089 ++i;
11093 nsIFrame::CaretPosition::CaretPosition() : mContentOffset(0) {}
11095 nsIFrame::CaretPosition::~CaretPosition() = default;
11097 bool nsIFrame::HasCSSAnimations() {
11098 auto* collection = AnimationCollection<CSSAnimation>::Get(this);
11099 return collection && !collection->mAnimations.IsEmpty();
11102 bool nsIFrame::HasCSSTransitions() {
11103 auto* collection = AnimationCollection<CSSTransition>::Get(this);
11104 return collection && !collection->mAnimations.IsEmpty();
11107 void nsIFrame::AddSizeOfExcludingThisForTree(nsWindowSizes& aSizes) const {
11108 aSizes.mLayoutFramePropertiesSize +=
11109 mProperties.SizeOfExcludingThis(aSizes.mState.mMallocSizeOf);
11111 // We don't do this for Gecko because this stuff is stored in the nsPresArena
11112 // and so measured elsewhere.
11113 if (!aSizes.mState.HaveSeenPtr(mComputedStyle)) {
11114 mComputedStyle->AddSizeOfIncludingThis(aSizes,
11115 &aSizes.mLayoutComputedValuesNonDom);
11118 // And our additional styles.
11119 int32_t index = 0;
11120 while (auto* extra = GetAdditionalComputedStyle(index++)) {
11121 if (!aSizes.mState.HaveSeenPtr(extra)) {
11122 extra->AddSizeOfIncludingThis(aSizes,
11123 &aSizes.mLayoutComputedValuesNonDom);
11127 for (const auto& childList : ChildLists()) {
11128 for (const nsIFrame* f : childList.mList) {
11129 f->AddSizeOfExcludingThisForTree(aSizes);
11134 nsRect nsIFrame::GetCompositorHitTestArea(nsDisplayListBuilder* aBuilder) {
11135 nsRect area;
11137 nsIScrollableFrame* scrollFrame = nsLayoutUtils::GetScrollableFrameFor(this);
11138 if (scrollFrame) {
11139 // If the frame is content of a scrollframe, then we need to pick up the
11140 // area corresponding to the overflow rect as well. Otherwise the parts of
11141 // the overflow that are not occupied by descendants get skipped and the
11142 // APZ code sends touch events to the content underneath instead.
11143 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1127773#c15.
11144 area = ScrollableOverflowRect();
11145 } else {
11146 area = GetRectRelativeToSelf();
11149 if (!area.IsEmpty()) {
11150 return area + aBuilder->ToReferenceFrame(this);
11153 return area;
11156 CompositorHitTestInfo nsIFrame::GetCompositorHitTestInfo(
11157 nsDisplayListBuilder* aBuilder) {
11158 CompositorHitTestInfo result = CompositorHitTestInvisibleToHit;
11160 if (aBuilder->IsInsidePointerEventsNoneDoc()) {
11161 // Somewhere up the parent document chain is a subdocument with pointer-
11162 // events:none set on it.
11163 return result;
11165 if (!GetParent()) {
11166 MOZ_ASSERT(IsViewportFrame());
11167 // Viewport frames are never event targets, other frames, like canvas
11168 // frames, are the event targets for any regions viewport frames may cover.
11169 return result;
11171 if (Style()->PointerEvents() == StylePointerEvents::None) {
11172 return result;
11174 if (!StyleVisibility()->IsVisible()) {
11175 return result;
11178 // Anything that didn't match the above conditions is visible to hit-testing.
11179 result = CompositorHitTestFlags::eVisibleToHitTest;
11180 if (SVGIntegrationUtils::UsingMaskOrClipPathForFrame(this)) {
11181 // If WebRender is enabled, simple clip-paths can be converted into WR
11182 // clips that WR knows how to hit-test against, so we don't need to mark
11183 // it as an irregular area.
11184 if (!SVGIntegrationUtils::UsingSimpleClipPathForFrame(this)) {
11185 result += CompositorHitTestFlags::eIrregularArea;
11189 if (aBuilder->IsBuildingNonLayerizedScrollbar()) {
11190 // Scrollbars may be painted into a layer below the actual layer they will
11191 // scroll, and therefore wheel events may be dispatched to the outer frame
11192 // instead of the intended scrollframe. To address this, we force a d-t-c
11193 // region on scrollbar frames that won't be placed in their own layer. See
11194 // bug 1213324 for details.
11195 result += CompositorHitTestFlags::eInactiveScrollframe;
11196 } else if (aBuilder->GetAncestorHasApzAwareEventHandler()) {
11197 result += CompositorHitTestFlags::eApzAwareListeners;
11198 } else if (IsRangeFrame()) {
11199 // Range frames handle touch events directly without having a touch listener
11200 // so we need to let APZ know that this area cares about events.
11201 result += CompositorHitTestFlags::eApzAwareListeners;
11204 if (aBuilder->IsTouchEventPrefEnabledDoc()) {
11205 // Inherit the touch-action flags from the parent, if there is one. We do
11206 // this because of how the touch-action on a frame combines the touch-action
11207 // from ancestor DOM elements. Refer to the documentation in
11208 // TouchActionHelper.cpp for details; this code is meant to be equivalent to
11209 // that code, but woven into the top-down recursive display list building
11210 // process.
11211 CompositorHitTestInfo inheritedTouchAction =
11212 aBuilder->GetCompositorHitTestInfo() & CompositorHitTestTouchActionMask;
11214 nsIFrame* touchActionFrame = this;
11215 if (nsIScrollableFrame* scrollFrame =
11216 nsLayoutUtils::GetScrollableFrameFor(this)) {
11217 ScrollStyles ss = scrollFrame->GetScrollStyles();
11218 if (ss.mVertical != StyleOverflow::Hidden ||
11219 ss.mHorizontal != StyleOverflow::Hidden) {
11220 touchActionFrame = do_QueryFrame(scrollFrame);
11221 // On scrollframes, stop inheriting the pan-x and pan-y flags; instead,
11222 // reset them back to zero to allow panning on the scrollframe unless we
11223 // encounter an element that disables it that's inside the scrollframe.
11224 // This is equivalent to the |considerPanning| variable in
11225 // TouchActionHelper.cpp, but for a top-down traversal.
11226 CompositorHitTestInfo panMask(
11227 CompositorHitTestFlags::eTouchActionPanXDisabled,
11228 CompositorHitTestFlags::eTouchActionPanYDisabled);
11229 inheritedTouchAction -= panMask;
11233 result += inheritedTouchAction;
11235 const StyleTouchAction touchAction = touchActionFrame->UsedTouchAction();
11236 // The CSS allows the syntax auto | none | [pan-x || pan-y] | manipulation
11237 // so we can eliminate some combinations of things.
11238 if (touchAction == StyleTouchAction::AUTO) {
11239 // nothing to do
11240 } else if (touchAction & StyleTouchAction::MANIPULATION) {
11241 result += CompositorHitTestFlags::eTouchActionAnimatingZoomDisabled;
11242 } else {
11243 // This path handles the cases none | [pan-x || pan-y || pinch-zoom] so
11244 // double-tap is disabled in here.
11245 if (!(touchAction & StyleTouchAction::PINCH_ZOOM)) {
11246 result += CompositorHitTestFlags::eTouchActionPinchZoomDisabled;
11249 result += CompositorHitTestFlags::eTouchActionAnimatingZoomDisabled;
11251 if (!(touchAction & StyleTouchAction::PAN_X)) {
11252 result += CompositorHitTestFlags::eTouchActionPanXDisabled;
11254 if (!(touchAction & StyleTouchAction::PAN_Y)) {
11255 result += CompositorHitTestFlags::eTouchActionPanYDisabled;
11257 if (touchAction & StyleTouchAction::NONE) {
11258 // all the touch-action disabling flags will already have been set above
11259 MOZ_ASSERT(result.contains(CompositorHitTestTouchActionMask));
11264 const Maybe<ScrollDirection> scrollDirection =
11265 aBuilder->GetCurrentScrollbarDirection();
11266 if (scrollDirection.isSome()) {
11267 if (GetContent()->IsXULElement(nsGkAtoms::thumb)) {
11268 const bool thumbGetsLayer = aBuilder->GetCurrentScrollbarTarget() !=
11269 layers::ScrollableLayerGuid::NULL_SCROLL_ID;
11270 if (thumbGetsLayer) {
11271 result += CompositorHitTestFlags::eScrollbarThumb;
11272 } else {
11273 result += CompositorHitTestFlags::eInactiveScrollframe;
11277 if (*scrollDirection == ScrollDirection::eVertical) {
11278 result += CompositorHitTestFlags::eScrollbarVertical;
11281 // includes the ScrollbarFrame, SliderFrame, anything else that
11282 // might be inside the xul:scrollbar
11283 result += CompositorHitTestFlags::eScrollbar;
11286 return result;
11289 // Returns true if we can guarantee there is no visible descendants.
11290 static bool HasNoVisibleDescendants(const nsIFrame* aFrame) {
11291 for (const auto& childList : aFrame->ChildLists()) {
11292 for (nsIFrame* f : childList.mList) {
11293 if (nsPlaceholderFrame::GetRealFrameFor(f)
11294 ->IsVisibleOrMayHaveVisibleDescendants()) {
11295 return false;
11299 return true;
11302 void nsIFrame::UpdateVisibleDescendantsState() {
11303 if (StyleVisibility()->IsVisible()) {
11304 // Notify invisible ancestors that a visible descendant exists now.
11305 nsIFrame* ancestor;
11306 for (ancestor = GetInFlowParent();
11307 ancestor && !ancestor->StyleVisibility()->IsVisible();
11308 ancestor = ancestor->GetInFlowParent()) {
11309 ancestor->mAllDescendantsAreInvisible = false;
11311 } else {
11312 mAllDescendantsAreInvisible = HasNoVisibleDescendants(this);
11316 void nsIFrame::UpdateAnimationVisibility() {
11317 auto* animationCollection = AnimationCollection<CSSAnimation>::Get(this);
11318 auto* transitionCollection = AnimationCollection<CSSTransition>::Get(this);
11320 if ((!animationCollection || animationCollection->mAnimations.IsEmpty()) &&
11321 (!transitionCollection || transitionCollection->mAnimations.IsEmpty())) {
11322 return;
11325 bool hidden = IsHiddenByContentVisibilityOnAnyAncestor();
11326 if (animationCollection) {
11327 for (auto& animation : animationCollection->mAnimations) {
11328 animation->SetHiddenByContentVisibility(hidden);
11332 if (transitionCollection) {
11333 for (auto& transition : transitionCollection->mAnimations) {
11334 transition->SetHiddenByContentVisibility(hidden);
11339 nsIFrame::PhysicalAxes nsIFrame::ShouldApplyOverflowClipping(
11340 const nsStyleDisplay* aDisp) const {
11341 MOZ_ASSERT(aDisp == StyleDisplay(), "Wrong display struct");
11343 // 'contain:paint', which we handle as 'overflow:clip' here. Except for
11344 // scrollframes we don't need contain:paint to add any clipping, because
11345 // the scrollable frame will already clip overflowing content, and because
11346 // 'contain:paint' should prevent all means of escaping that clipping
11347 // (e.g. because it forms a fixed-pos containing block).
11348 if (aDisp->IsContainPaint() && !IsScrollFrame() &&
11349 IsFrameOfType(eSupportsContainLayoutAndPaint)) {
11350 return PhysicalAxes::Both;
11353 // and overflow:hidden that we should interpret as clip
11354 if (aDisp->mOverflowX == StyleOverflow::Hidden &&
11355 aDisp->mOverflowY == StyleOverflow::Hidden) {
11356 // REVIEW: these are the frame types that set up clipping.
11357 LayoutFrameType type = Type();
11358 switch (type) {
11359 case LayoutFrameType::Table:
11360 case LayoutFrameType::TableCell:
11361 case LayoutFrameType::SVGOuterSVG:
11362 case LayoutFrameType::SVGInnerSVG:
11363 case LayoutFrameType::SVGSymbol:
11364 case LayoutFrameType::SVGForeignObject:
11365 return PhysicalAxes::Both;
11366 default:
11367 if (IsFrameOfType(nsIFrame::eReplacedContainsBlock)) {
11368 if (type == mozilla::LayoutFrameType::TextInput) {
11369 // It has an anonymous scroll frame that handles any overflow.
11370 return PhysicalAxes::None;
11372 return PhysicalAxes::Both;
11377 // clip overflow:clip, except for nsListControlFrame which is
11378 // an nsHTMLScrollFrame sub-class.
11379 if (MOZ_UNLIKELY((aDisp->mOverflowX == mozilla::StyleOverflow::Clip ||
11380 aDisp->mOverflowY == mozilla::StyleOverflow::Clip) &&
11381 !IsListControlFrame())) {
11382 // FIXME: we could use GetViewportScrollStylesOverrideElement() here instead
11383 // if that worked correctly in a print context. (see bug 1654667)
11384 const auto* element = Element::FromNodeOrNull(GetContent());
11385 if (!element ||
11386 !PresContext()->ElementWouldPropagateScrollStyles(*element)) {
11387 uint8_t axes = uint8_t(PhysicalAxes::None);
11388 if (aDisp->mOverflowX == mozilla::StyleOverflow::Clip) {
11389 axes |= uint8_t(PhysicalAxes::Horizontal);
11391 if (aDisp->mOverflowY == mozilla::StyleOverflow::Clip) {
11392 axes |= uint8_t(PhysicalAxes::Vertical);
11394 return PhysicalAxes(axes);
11398 if (HasAnyStateBits(NS_FRAME_SVG_LAYOUT)) {
11399 return PhysicalAxes::None;
11402 // If we're paginated and a block, and have NS_BLOCK_CLIP_PAGINATED_OVERFLOW
11403 // set, then we want to clip our overflow.
11404 bool clip = HasAnyStateBits(NS_BLOCK_CLIP_PAGINATED_OVERFLOW) &&
11405 PresContext()->IsPaginated() && IsBlockFrame();
11406 return clip ? PhysicalAxes::Both : PhysicalAxes::None;
11409 #ifdef DEBUG
11410 static void GetTagName(nsIFrame* aFrame, nsIContent* aContent, int aResultSize,
11411 char* aResult) {
11412 if (aContent) {
11413 snprintf(aResult, aResultSize, "%s@%p",
11414 nsAtomCString(aContent->NodeInfo()->NameAtom()).get(), aFrame);
11415 } else {
11416 snprintf(aResult, aResultSize, "@%p", aFrame);
11420 void nsIFrame::Trace(const char* aMethod, bool aEnter) {
11421 if (NS_FRAME_LOG_TEST(sFrameLogModule, NS_FRAME_TRACE_CALLS)) {
11422 char tagbuf[40];
11423 GetTagName(this, mContent, sizeof(tagbuf), tagbuf);
11424 printf_stderr("%s: %s %s", tagbuf, aEnter ? "enter" : "exit", aMethod);
11428 void nsIFrame::Trace(const char* aMethod, bool aEnter,
11429 const nsReflowStatus& aStatus) {
11430 if (NS_FRAME_LOG_TEST(sFrameLogModule, NS_FRAME_TRACE_CALLS)) {
11431 char tagbuf[40];
11432 GetTagName(this, mContent, sizeof(tagbuf), tagbuf);
11433 printf_stderr("%s: %s %s, status=%scomplete%s", tagbuf,
11434 aEnter ? "enter" : "exit", aMethod,
11435 aStatus.IsIncomplete() ? "not" : "",
11436 (aStatus.NextInFlowNeedsReflow()) ? "+reflow" : "");
11440 void nsIFrame::TraceMsg(const char* aFormatString, ...) {
11441 if (NS_FRAME_LOG_TEST(sFrameLogModule, NS_FRAME_TRACE_CALLS)) {
11442 // Format arguments into a buffer
11443 char argbuf[200];
11444 va_list ap;
11445 va_start(ap, aFormatString);
11446 VsprintfLiteral(argbuf, aFormatString, ap);
11447 va_end(ap);
11449 char tagbuf[40];
11450 GetTagName(this, mContent, sizeof(tagbuf), tagbuf);
11451 printf_stderr("%s: %s", tagbuf, argbuf);
11455 void nsIFrame::VerifyDirtyBitSet(const nsFrameList& aFrameList) {
11456 for (nsIFrame* f : aFrameList) {
11457 NS_ASSERTION(f->HasAnyStateBits(NS_FRAME_IS_DIRTY), "dirty bit not set");
11461 // Start Display Reflow
11462 DR_cookie::DR_cookie(nsPresContext* aPresContext, nsIFrame* aFrame,
11463 const ReflowInput& aReflowInput, ReflowOutput& aMetrics,
11464 nsReflowStatus& aStatus)
11465 : mPresContext(aPresContext),
11466 mFrame(aFrame),
11467 mReflowInput(aReflowInput),
11468 mMetrics(aMetrics),
11469 mStatus(aStatus) {
11470 MOZ_COUNT_CTOR(DR_cookie);
11471 mValue = nsIFrame::DisplayReflowEnter(aPresContext, mFrame, mReflowInput);
11474 DR_cookie::~DR_cookie() {
11475 MOZ_COUNT_DTOR(DR_cookie);
11476 nsIFrame::DisplayReflowExit(mPresContext, mFrame, mMetrics, mStatus, mValue);
11479 DR_layout_cookie::DR_layout_cookie(nsIFrame* aFrame) : mFrame(aFrame) {
11480 MOZ_COUNT_CTOR(DR_layout_cookie);
11481 mValue = nsIFrame::DisplayLayoutEnter(mFrame);
11484 DR_layout_cookie::~DR_layout_cookie() {
11485 MOZ_COUNT_DTOR(DR_layout_cookie);
11486 nsIFrame::DisplayLayoutExit(mFrame, mValue);
11489 DR_intrinsic_inline_size_cookie::DR_intrinsic_inline_size_cookie(
11490 nsIFrame* aFrame, const char* aType, nscoord& aResult)
11491 : mFrame(aFrame), mType(aType), mResult(aResult) {
11492 MOZ_COUNT_CTOR(DR_intrinsic_inline_size_cookie);
11493 mValue = nsIFrame::DisplayIntrinsicISizeEnter(mFrame, mType);
11496 DR_intrinsic_inline_size_cookie::~DR_intrinsic_inline_size_cookie() {
11497 MOZ_COUNT_DTOR(DR_intrinsic_inline_size_cookie);
11498 nsIFrame::DisplayIntrinsicISizeExit(mFrame, mType, mResult, mValue);
11501 DR_intrinsic_size_cookie::DR_intrinsic_size_cookie(nsIFrame* aFrame,
11502 const char* aType,
11503 nsSize& aResult)
11504 : mFrame(aFrame), mType(aType), mResult(aResult) {
11505 MOZ_COUNT_CTOR(DR_intrinsic_size_cookie);
11506 mValue = nsIFrame::DisplayIntrinsicSizeEnter(mFrame, mType);
11509 DR_intrinsic_size_cookie::~DR_intrinsic_size_cookie() {
11510 MOZ_COUNT_DTOR(DR_intrinsic_size_cookie);
11511 nsIFrame::DisplayIntrinsicSizeExit(mFrame, mType, mResult, mValue);
11514 DR_init_constraints_cookie::DR_init_constraints_cookie(
11515 nsIFrame* aFrame, ReflowInput* aState, nscoord aCBWidth, nscoord aCBHeight,
11516 const mozilla::Maybe<mozilla::LogicalMargin> aBorder,
11517 const mozilla::Maybe<mozilla::LogicalMargin> aPadding)
11518 : mFrame(aFrame), mState(aState) {
11519 MOZ_COUNT_CTOR(DR_init_constraints_cookie);
11520 nsMargin border;
11521 if (aBorder) {
11522 border = aBorder->GetPhysicalMargin(aFrame->GetWritingMode());
11524 nsMargin padding;
11525 if (aPadding) {
11526 padding = aPadding->GetPhysicalMargin(aFrame->GetWritingMode());
11528 mValue = ReflowInput::DisplayInitConstraintsEnter(
11529 mFrame, mState, aCBWidth, aCBHeight, aBorder ? &border : nullptr,
11530 aPadding ? &padding : nullptr);
11533 DR_init_constraints_cookie::~DR_init_constraints_cookie() {
11534 MOZ_COUNT_DTOR(DR_init_constraints_cookie);
11535 ReflowInput::DisplayInitConstraintsExit(mFrame, mState, mValue);
11538 DR_init_offsets_cookie::DR_init_offsets_cookie(
11539 nsIFrame* aFrame, SizeComputationInput* aState, nscoord aPercentBasis,
11540 WritingMode aCBWritingMode,
11541 const mozilla::Maybe<mozilla::LogicalMargin> aBorder,
11542 const mozilla::Maybe<mozilla::LogicalMargin> aPadding)
11543 : mFrame(aFrame), mState(aState) {
11544 MOZ_COUNT_CTOR(DR_init_offsets_cookie);
11545 nsMargin border;
11546 if (aBorder) {
11547 border = aBorder->GetPhysicalMargin(aFrame->GetWritingMode());
11549 nsMargin padding;
11550 if (aPadding) {
11551 padding = aPadding->GetPhysicalMargin(aFrame->GetWritingMode());
11553 mValue = SizeComputationInput::DisplayInitOffsetsEnter(
11554 mFrame, mState, aPercentBasis, aCBWritingMode,
11555 aBorder ? &border : nullptr, aPadding ? &padding : nullptr);
11558 DR_init_offsets_cookie::~DR_init_offsets_cookie() {
11559 MOZ_COUNT_DTOR(DR_init_offsets_cookie);
11560 SizeComputationInput::DisplayInitOffsetsExit(mFrame, mState, mValue);
11563 struct DR_Rule;
11565 struct DR_FrameTypeInfo {
11566 DR_FrameTypeInfo(LayoutFrameType aFrameType, const char* aFrameNameAbbrev,
11567 const char* aFrameName);
11568 ~DR_FrameTypeInfo();
11570 LayoutFrameType mType;
11571 char mNameAbbrev[16];
11572 char mName[32];
11573 nsTArray<DR_Rule*> mRules;
11575 private:
11576 DR_FrameTypeInfo& operator=(const DR_FrameTypeInfo&) = delete;
11579 struct DR_FrameTreeNode;
11580 struct DR_Rule;
11582 struct DR_State {
11583 DR_State();
11584 ~DR_State();
11585 void Init();
11586 void AddFrameTypeInfo(LayoutFrameType aFrameType,
11587 const char* aFrameNameAbbrev, const char* aFrameName);
11588 DR_FrameTypeInfo* GetFrameTypeInfo(LayoutFrameType aFrameType);
11589 DR_FrameTypeInfo* GetFrameTypeInfo(char* aFrameName);
11590 void InitFrameTypeTable();
11591 DR_FrameTreeNode* CreateTreeNode(nsIFrame* aFrame,
11592 const ReflowInput* aReflowInput);
11593 void FindMatchingRule(DR_FrameTreeNode& aNode);
11594 bool RuleMatches(DR_Rule& aRule, DR_FrameTreeNode& aNode);
11595 bool GetToken(FILE* aFile, char* aBuf, size_t aBufSize);
11596 DR_Rule* ParseRule(FILE* aFile);
11597 void ParseRulesFile();
11598 void AddRule(nsTArray<DR_Rule*>& aRules, DR_Rule& aRule);
11599 bool IsWhiteSpace(int c);
11600 bool GetNumber(char* aBuf, int32_t& aNumber);
11601 void PrettyUC(nscoord aSize, char* aBuf, int aBufSize);
11602 void PrintMargin(const char* tag, const nsMargin* aMargin);
11603 void DisplayFrameTypeInfo(nsIFrame* aFrame, int32_t aIndent);
11604 void DeleteTreeNode(DR_FrameTreeNode& aNode);
11606 bool mInited;
11607 bool mActive;
11608 int32_t mCount;
11609 int32_t mAssert;
11610 int32_t mIndent;
11611 bool mIndentUndisplayedFrames;
11612 bool mDisplayPixelErrors;
11613 nsTArray<DR_Rule*> mWildRules;
11614 nsTArray<DR_FrameTypeInfo> mFrameTypeTable;
11615 // reflow specific state
11616 nsTArray<DR_FrameTreeNode*> mFrameTreeLeaves;
11619 static DR_State* DR_state; // the one and only DR_State
11621 struct DR_RulePart {
11622 explicit DR_RulePart(LayoutFrameType aFrameType)
11623 : mFrameType(aFrameType), mNext(0) {}
11625 void Destroy();
11627 LayoutFrameType mFrameType;
11628 DR_RulePart* mNext;
11631 void DR_RulePart::Destroy() {
11632 if (mNext) {
11633 mNext->Destroy();
11635 delete this;
11638 struct DR_Rule {
11639 DR_Rule() : mLength(0), mTarget(nullptr), mDisplay(false) {
11640 MOZ_COUNT_CTOR(DR_Rule);
11642 ~DR_Rule() {
11643 if (mTarget) mTarget->Destroy();
11644 MOZ_COUNT_DTOR(DR_Rule);
11646 void AddPart(LayoutFrameType aFrameType);
11648 uint32_t mLength;
11649 DR_RulePart* mTarget;
11650 bool mDisplay;
11653 void DR_Rule::AddPart(LayoutFrameType aFrameType) {
11654 DR_RulePart* newPart = new DR_RulePart(aFrameType);
11655 newPart->mNext = mTarget;
11656 mTarget = newPart;
11657 mLength++;
11660 DR_FrameTypeInfo::~DR_FrameTypeInfo() {
11661 int32_t numElements;
11662 numElements = mRules.Length();
11663 for (int32_t i = numElements - 1; i >= 0; i--) {
11664 delete mRules.ElementAt(i);
11668 DR_FrameTypeInfo::DR_FrameTypeInfo(LayoutFrameType aFrameType,
11669 const char* aFrameNameAbbrev,
11670 const char* aFrameName) {
11671 mType = aFrameType;
11672 PL_strncpyz(mNameAbbrev, aFrameNameAbbrev, sizeof(mNameAbbrev));
11673 PL_strncpyz(mName, aFrameName, sizeof(mName));
11676 struct DR_FrameTreeNode {
11677 DR_FrameTreeNode(nsIFrame* aFrame, DR_FrameTreeNode* aParent)
11678 : mFrame(aFrame), mParent(aParent), mDisplay(0), mIndent(0) {
11679 MOZ_COUNT_CTOR(DR_FrameTreeNode);
11682 MOZ_COUNTED_DTOR(DR_FrameTreeNode)
11684 nsIFrame* mFrame;
11685 DR_FrameTreeNode* mParent;
11686 bool mDisplay;
11687 uint32_t mIndent;
11690 // DR_State implementation
11692 DR_State::DR_State()
11693 : mInited(false),
11694 mActive(false),
11695 mCount(0),
11696 mAssert(-1),
11697 mIndent(0),
11698 mIndentUndisplayedFrames(false),
11699 mDisplayPixelErrors(false) {
11700 MOZ_COUNT_CTOR(DR_State);
11703 void DR_State::Init() {
11704 char* env = PR_GetEnv("GECKO_DISPLAY_REFLOW_ASSERT");
11705 int32_t num;
11706 if (env) {
11707 if (GetNumber(env, num))
11708 mAssert = num;
11709 else
11710 printf("GECKO_DISPLAY_REFLOW_ASSERT - invalid value = %s", env);
11713 env = PR_GetEnv("GECKO_DISPLAY_REFLOW_INDENT_START");
11714 if (env) {
11715 if (GetNumber(env, num))
11716 mIndent = num;
11717 else
11718 printf("GECKO_DISPLAY_REFLOW_INDENT_START - invalid value = %s", env);
11721 env = PR_GetEnv("GECKO_DISPLAY_REFLOW_INDENT_UNDISPLAYED_FRAMES");
11722 if (env) {
11723 if (GetNumber(env, num))
11724 mIndentUndisplayedFrames = num;
11725 else
11726 printf(
11727 "GECKO_DISPLAY_REFLOW_INDENT_UNDISPLAYED_FRAMES - invalid value = %s",
11728 env);
11731 env = PR_GetEnv("GECKO_DISPLAY_REFLOW_FLAG_PIXEL_ERRORS");
11732 if (env) {
11733 if (GetNumber(env, num))
11734 mDisplayPixelErrors = num;
11735 else
11736 printf("GECKO_DISPLAY_REFLOW_FLAG_PIXEL_ERRORS - invalid value = %s",
11737 env);
11740 InitFrameTypeTable();
11741 ParseRulesFile();
11742 mInited = true;
11745 DR_State::~DR_State() {
11746 MOZ_COUNT_DTOR(DR_State);
11747 int32_t numElements, i;
11748 numElements = mWildRules.Length();
11749 for (i = numElements - 1; i >= 0; i--) {
11750 delete mWildRules.ElementAt(i);
11752 numElements = mFrameTreeLeaves.Length();
11753 for (i = numElements - 1; i >= 0; i--) {
11754 delete mFrameTreeLeaves.ElementAt(i);
11758 bool DR_State::GetNumber(char* aBuf, int32_t& aNumber) {
11759 if (sscanf(aBuf, "%d", &aNumber) > 0)
11760 return true;
11761 else
11762 return false;
11765 bool DR_State::IsWhiteSpace(int c) {
11766 return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
11769 bool DR_State::GetToken(FILE* aFile, char* aBuf, size_t aBufSize) {
11770 bool haveToken = false;
11771 aBuf[0] = 0;
11772 // get the 1st non whitespace char
11773 int c = -1;
11774 for (c = getc(aFile); (c > 0) && IsWhiteSpace(c); c = getc(aFile)) {
11777 if (c > 0) {
11778 haveToken = true;
11779 aBuf[0] = c;
11780 // get everything up to the next whitespace char
11781 size_t cX;
11782 for (cX = 1; cX + 1 < aBufSize; cX++) {
11783 c = getc(aFile);
11784 if (c < 0) { // EOF
11785 ungetc(' ', aFile);
11786 break;
11787 } else {
11788 if (IsWhiteSpace(c)) {
11789 break;
11790 } else {
11791 aBuf[cX] = c;
11795 aBuf[cX] = 0;
11797 return haveToken;
11800 DR_Rule* DR_State::ParseRule(FILE* aFile) {
11801 char buf[128];
11802 int32_t doDisplay;
11803 DR_Rule* rule = nullptr;
11804 while (GetToken(aFile, buf, sizeof(buf))) {
11805 if (GetNumber(buf, doDisplay)) {
11806 if (rule) {
11807 rule->mDisplay = !!doDisplay;
11808 break;
11809 } else {
11810 printf("unexpected token - %s \n", buf);
11812 } else {
11813 if (!rule) {
11814 rule = new DR_Rule;
11816 if (strcmp(buf, "*") == 0) {
11817 rule->AddPart(LayoutFrameType::None);
11818 } else {
11819 DR_FrameTypeInfo* info = GetFrameTypeInfo(buf);
11820 if (info) {
11821 rule->AddPart(info->mType);
11822 } else {
11823 printf("invalid frame type - %s \n", buf);
11828 return rule;
11831 void DR_State::AddRule(nsTArray<DR_Rule*>& aRules, DR_Rule& aRule) {
11832 int32_t numRules = aRules.Length();
11833 for (int32_t ruleX = 0; ruleX < numRules; ruleX++) {
11834 DR_Rule* rule = aRules.ElementAt(ruleX);
11835 NS_ASSERTION(rule, "program error");
11836 if (aRule.mLength > rule->mLength) {
11837 aRules.InsertElementAt(ruleX, &aRule);
11838 return;
11841 aRules.AppendElement(&aRule);
11844 static Maybe<bool> ShouldLogReflow(const char* processes) {
11845 switch (processes[0]) {
11846 case 'A':
11847 case 'a':
11848 return Some(true);
11849 case 'P':
11850 case 'p':
11851 return Some(XRE_IsParentProcess());
11852 case 'C':
11853 case 'c':
11854 return Some(XRE_IsContentProcess());
11855 default:
11856 return Nothing{};
11860 void DR_State::ParseRulesFile() {
11861 char* processes = PR_GetEnv("GECKO_DISPLAY_REFLOW_PROCESSES");
11862 if (processes) {
11863 Maybe<bool> enableLog = ShouldLogReflow(processes);
11864 if (enableLog.isNothing()) {
11865 MOZ_CRASH("GECKO_DISPLAY_REFLOW_PROCESSES: [a]ll [p]arent [c]ontent");
11866 } else if (enableLog.value()) {
11867 DR_Rule* rule = new DR_Rule;
11868 rule->AddPart(LayoutFrameType::None);
11869 rule->mDisplay = true;
11870 AddRule(mWildRules, *rule);
11871 mActive = true;
11873 return;
11876 char* path = PR_GetEnv("GECKO_DISPLAY_REFLOW_RULES_FILE");
11877 if (path) {
11878 FILE* inFile = fopen(path, "r");
11879 if (!inFile) {
11880 MOZ_CRASH(
11881 "Failed to open the specified rules file; Try `--setpref "
11882 "security.sandbox.content.level=2` if the sandbox is at cause");
11884 for (DR_Rule* rule = ParseRule(inFile); rule; rule = ParseRule(inFile)) {
11885 if (rule->mTarget) {
11886 LayoutFrameType fType = rule->mTarget->mFrameType;
11887 if (fType != LayoutFrameType::None) {
11888 DR_FrameTypeInfo* info = GetFrameTypeInfo(fType);
11889 AddRule(info->mRules, *rule);
11890 } else {
11891 AddRule(mWildRules, *rule);
11893 mActive = true;
11897 fclose(inFile);
11901 void DR_State::AddFrameTypeInfo(LayoutFrameType aFrameType,
11902 const char* aFrameNameAbbrev,
11903 const char* aFrameName) {
11904 mFrameTypeTable.EmplaceBack(aFrameType, aFrameNameAbbrev, aFrameName);
11907 DR_FrameTypeInfo* DR_State::GetFrameTypeInfo(LayoutFrameType aFrameType) {
11908 int32_t numEntries = mFrameTypeTable.Length();
11909 NS_ASSERTION(numEntries != 0, "empty FrameTypeTable");
11910 for (int32_t i = 0; i < numEntries; i++) {
11911 DR_FrameTypeInfo& info = mFrameTypeTable.ElementAt(i);
11912 if (info.mType == aFrameType) {
11913 return &info;
11916 return &mFrameTypeTable.ElementAt(numEntries -
11917 1); // return unknown frame type
11920 DR_FrameTypeInfo* DR_State::GetFrameTypeInfo(char* aFrameName) {
11921 int32_t numEntries = mFrameTypeTable.Length();
11922 NS_ASSERTION(numEntries != 0, "empty FrameTypeTable");
11923 for (int32_t i = 0; i < numEntries; i++) {
11924 DR_FrameTypeInfo& info = mFrameTypeTable.ElementAt(i);
11925 if ((strcmp(aFrameName, info.mName) == 0) ||
11926 (strcmp(aFrameName, info.mNameAbbrev) == 0)) {
11927 return &info;
11930 return &mFrameTypeTable.ElementAt(numEntries -
11931 1); // return unknown frame type
11934 void DR_State::InitFrameTypeTable() {
11935 AddFrameTypeInfo(LayoutFrameType::Block, "block", "block");
11936 AddFrameTypeInfo(LayoutFrameType::Br, "br", "br");
11937 AddFrameTypeInfo(LayoutFrameType::ColorControl, "color", "colorControl");
11938 AddFrameTypeInfo(LayoutFrameType::GfxButtonControl, "button",
11939 "gfxButtonControl");
11940 AddFrameTypeInfo(LayoutFrameType::HTMLButtonControl, "HTMLbutton",
11941 "HTMLButtonControl");
11942 AddFrameTypeInfo(LayoutFrameType::HTMLCanvas, "HTMLCanvas", "HTMLCanvas");
11943 AddFrameTypeInfo(LayoutFrameType::SubDocument, "subdoc", "subDocument");
11944 AddFrameTypeInfo(LayoutFrameType::Image, "img", "image");
11945 AddFrameTypeInfo(LayoutFrameType::Inline, "inline", "inline");
11946 AddFrameTypeInfo(LayoutFrameType::Letter, "letter", "letter");
11947 AddFrameTypeInfo(LayoutFrameType::Line, "line", "line");
11948 AddFrameTypeInfo(LayoutFrameType::ListControl, "select", "select");
11949 AddFrameTypeInfo(LayoutFrameType::Page, "page", "page");
11950 AddFrameTypeInfo(LayoutFrameType::Placeholder, "place", "placeholder");
11951 AddFrameTypeInfo(LayoutFrameType::Canvas, "canvas", "canvas");
11952 AddFrameTypeInfo(LayoutFrameType::Scroll, "scroll", "scroll");
11953 AddFrameTypeInfo(LayoutFrameType::TableCell, "cell", "tableCell");
11954 AddFrameTypeInfo(LayoutFrameType::TableCol, "col", "tableCol");
11955 AddFrameTypeInfo(LayoutFrameType::TableColGroup, "colG", "tableColGroup");
11956 AddFrameTypeInfo(LayoutFrameType::Table, "tbl", "table");
11957 AddFrameTypeInfo(LayoutFrameType::TableWrapper, "tblW", "tableWrapper");
11958 AddFrameTypeInfo(LayoutFrameType::TableRowGroup, "rowG", "tableRowGroup");
11959 AddFrameTypeInfo(LayoutFrameType::TableRow, "row", "tableRow");
11960 AddFrameTypeInfo(LayoutFrameType::TextInput, "textCtl", "textInput");
11961 AddFrameTypeInfo(LayoutFrameType::Text, "text", "text");
11962 AddFrameTypeInfo(LayoutFrameType::Viewport, "VP", "viewport");
11963 AddFrameTypeInfo(LayoutFrameType::Slider, "Slider", "Slider");
11964 AddFrameTypeInfo(LayoutFrameType::None, "unknown", "unknown");
11967 void DR_State::DisplayFrameTypeInfo(nsIFrame* aFrame, int32_t aIndent) {
11968 DR_FrameTypeInfo* frameTypeInfo = GetFrameTypeInfo(aFrame->Type());
11969 if (frameTypeInfo) {
11970 for (int32_t i = 0; i < aIndent; i++) {
11971 printf(" ");
11973 if (!strcmp(frameTypeInfo->mNameAbbrev, "unknown")) {
11974 if (aFrame) {
11975 nsAutoString name;
11976 aFrame->GetFrameName(name);
11977 printf("%s %p ", NS_LossyConvertUTF16toASCII(name).get(),
11978 (void*)aFrame);
11979 } else {
11980 printf("%s %p ", frameTypeInfo->mNameAbbrev, (void*)aFrame);
11982 } else {
11983 printf("%s %p ", frameTypeInfo->mNameAbbrev, (void*)aFrame);
11988 bool DR_State::RuleMatches(DR_Rule& aRule, DR_FrameTreeNode& aNode) {
11989 NS_ASSERTION(aRule.mTarget, "program error");
11991 DR_RulePart* rulePart;
11992 DR_FrameTreeNode* parentNode;
11993 for (rulePart = aRule.mTarget->mNext, parentNode = aNode.mParent;
11994 rulePart && parentNode;
11995 rulePart = rulePart->mNext, parentNode = parentNode->mParent) {
11996 if (rulePart->mFrameType != LayoutFrameType::None) {
11997 if (parentNode->mFrame) {
11998 if (rulePart->mFrameType != parentNode->mFrame->Type()) {
11999 return false;
12001 } else
12002 NS_ASSERTION(false, "program error");
12004 // else wild card match
12006 return true;
12009 void DR_State::FindMatchingRule(DR_FrameTreeNode& aNode) {
12010 if (!aNode.mFrame) {
12011 NS_ASSERTION(false, "invalid DR_FrameTreeNode \n");
12012 return;
12015 bool matchingRule = false;
12017 DR_FrameTypeInfo* info = GetFrameTypeInfo(aNode.mFrame->Type());
12018 NS_ASSERTION(info, "program error");
12019 int32_t numRules = info->mRules.Length();
12020 for (int32_t ruleX = 0; ruleX < numRules; ruleX++) {
12021 DR_Rule* rule = info->mRules.ElementAt(ruleX);
12022 if (rule && RuleMatches(*rule, aNode)) {
12023 aNode.mDisplay = rule->mDisplay;
12024 matchingRule = true;
12025 break;
12028 if (!matchingRule) {
12029 int32_t numWildRules = mWildRules.Length();
12030 for (int32_t ruleX = 0; ruleX < numWildRules; ruleX++) {
12031 DR_Rule* rule = mWildRules.ElementAt(ruleX);
12032 if (rule && RuleMatches(*rule, aNode)) {
12033 aNode.mDisplay = rule->mDisplay;
12034 break;
12040 DR_FrameTreeNode* DR_State::CreateTreeNode(nsIFrame* aFrame,
12041 const ReflowInput* aReflowInput) {
12042 // find the frame of the parent reflow input (usually just the parent of
12043 // aFrame)
12044 nsIFrame* parentFrame;
12045 if (aReflowInput) {
12046 const ReflowInput* parentRI = aReflowInput->mParentReflowInput;
12047 parentFrame = (parentRI) ? parentRI->mFrame : nullptr;
12048 } else {
12049 parentFrame = aFrame->GetParent();
12052 // find the parent tree node leaf
12053 DR_FrameTreeNode* parentNode = nullptr;
12055 DR_FrameTreeNode* lastLeaf = nullptr;
12056 if (mFrameTreeLeaves.Length())
12057 lastLeaf = mFrameTreeLeaves.ElementAt(mFrameTreeLeaves.Length() - 1);
12058 if (lastLeaf) {
12059 for (parentNode = lastLeaf;
12060 parentNode && (parentNode->mFrame != parentFrame);
12061 parentNode = parentNode->mParent) {
12064 DR_FrameTreeNode* newNode = new DR_FrameTreeNode(aFrame, parentNode);
12065 FindMatchingRule(*newNode);
12067 newNode->mIndent = mIndent;
12068 if (newNode->mDisplay || mIndentUndisplayedFrames) {
12069 ++mIndent;
12072 if (lastLeaf && (lastLeaf == parentNode)) {
12073 mFrameTreeLeaves.RemoveLastElement();
12075 mFrameTreeLeaves.AppendElement(newNode);
12076 mCount++;
12078 return newNode;
12081 void DR_State::PrettyUC(nscoord aSize, char* aBuf, int aBufSize) {
12082 if (NS_UNCONSTRAINEDSIZE == aSize) {
12083 strcpy(aBuf, "UC");
12084 } else {
12085 if ((nscoord)0xdeadbeefU == aSize) {
12086 strcpy(aBuf, "deadbeef");
12087 } else {
12088 snprintf(aBuf, aBufSize, "%d", aSize);
12093 void DR_State::PrintMargin(const char* tag, const nsMargin* aMargin) {
12094 if (aMargin) {
12095 char t[16], r[16], b[16], l[16];
12096 PrettyUC(aMargin->top, t, 16);
12097 PrettyUC(aMargin->right, r, 16);
12098 PrettyUC(aMargin->bottom, b, 16);
12099 PrettyUC(aMargin->left, l, 16);
12100 printf(" %s=%s,%s,%s,%s", tag, t, r, b, l);
12101 } else {
12102 // use %p here for consistency with other null-pointer printouts
12103 printf(" %s=%p", tag, (void*)aMargin);
12107 void DR_State::DeleteTreeNode(DR_FrameTreeNode& aNode) {
12108 mFrameTreeLeaves.RemoveElement(&aNode);
12109 int32_t numLeaves = mFrameTreeLeaves.Length();
12110 if ((0 == numLeaves) ||
12111 (aNode.mParent != mFrameTreeLeaves.ElementAt(numLeaves - 1))) {
12112 mFrameTreeLeaves.AppendElement(aNode.mParent);
12115 if (aNode.mDisplay || mIndentUndisplayedFrames) {
12116 --mIndent;
12118 // delete the tree node
12119 delete &aNode;
12122 static void CheckPixelError(nscoord aSize, int32_t aPixelToTwips) {
12123 if (NS_UNCONSTRAINEDSIZE != aSize) {
12124 if ((aSize % aPixelToTwips) > 0) {
12125 printf("VALUE %d is not a whole pixel \n", aSize);
12130 static void DisplayReflowEnterPrint(nsPresContext* aPresContext,
12131 nsIFrame* aFrame,
12132 const ReflowInput& aReflowInput,
12133 DR_FrameTreeNode& aTreeNode,
12134 bool aChanged) {
12135 if (aTreeNode.mDisplay) {
12136 DR_state->DisplayFrameTypeInfo(aFrame, aTreeNode.mIndent);
12138 char width[16];
12139 char height[16];
12141 DR_state->PrettyUC(aReflowInput.AvailableWidth(), width, 16);
12142 DR_state->PrettyUC(aReflowInput.AvailableHeight(), height, 16);
12143 printf("Reflow a=%s,%s ", width, height);
12145 DR_state->PrettyUC(aReflowInput.ComputedWidth(), width, 16);
12146 DR_state->PrettyUC(aReflowInput.ComputedHeight(), height, 16);
12147 printf("c=%s,%s ", width, height);
12149 if (aFrame->HasAnyStateBits(NS_FRAME_IS_DIRTY)) printf("dirty ");
12151 if (aFrame->HasAnyStateBits(NS_FRAME_HAS_DIRTY_CHILDREN))
12152 printf("dirty-children ");
12154 if (aReflowInput.mFlags.mSpecialBSizeReflow) printf("special-bsize ");
12156 if (aReflowInput.IsHResize()) printf("h-resize ");
12158 if (aReflowInput.IsVResize()) printf("v-resize ");
12160 nsIFrame* inFlow = aFrame->GetPrevInFlow();
12161 if (inFlow) {
12162 printf("pif=%p ", (void*)inFlow);
12164 inFlow = aFrame->GetNextInFlow();
12165 if (inFlow) {
12166 printf("nif=%p ", (void*)inFlow);
12168 if (aChanged)
12169 printf("CHANGED \n");
12170 else
12171 printf("cnt=%d \n", DR_state->mCount);
12172 if (DR_state->mDisplayPixelErrors) {
12173 int32_t d2a = aPresContext->AppUnitsPerDevPixel();
12174 CheckPixelError(aReflowInput.AvailableWidth(), d2a);
12175 CheckPixelError(aReflowInput.AvailableHeight(), d2a);
12176 CheckPixelError(aReflowInput.ComputedWidth(), d2a);
12177 CheckPixelError(aReflowInput.ComputedHeight(), d2a);
12182 void* nsIFrame::DisplayReflowEnter(nsPresContext* aPresContext,
12183 nsIFrame* aFrame,
12184 const ReflowInput& aReflowInput) {
12185 if (!DR_state->mInited) DR_state->Init();
12186 if (!DR_state->mActive) return nullptr;
12188 NS_ASSERTION(aFrame, "invalid call");
12190 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, &aReflowInput);
12191 if (treeNode) {
12192 DisplayReflowEnterPrint(aPresContext, aFrame, aReflowInput, *treeNode,
12193 false);
12195 return treeNode;
12198 void* nsIFrame::DisplayLayoutEnter(nsIFrame* aFrame) {
12199 if (!DR_state->mInited) DR_state->Init();
12200 if (!DR_state->mActive) return nullptr;
12202 NS_ASSERTION(aFrame, "invalid call");
12204 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12205 if (treeNode && treeNode->mDisplay) {
12206 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12207 printf("XULLayout\n");
12209 return treeNode;
12212 void* nsIFrame::DisplayIntrinsicISizeEnter(nsIFrame* aFrame,
12213 const char* aType) {
12214 if (!DR_state->mInited) DR_state->Init();
12215 if (!DR_state->mActive) return nullptr;
12217 NS_ASSERTION(aFrame, "invalid call");
12219 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12220 if (treeNode && treeNode->mDisplay) {
12221 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12222 printf("Get%sISize\n", aType);
12224 return treeNode;
12227 void* nsIFrame::DisplayIntrinsicSizeEnter(nsIFrame* aFrame, const char* aType) {
12228 if (!DR_state->mInited) DR_state->Init();
12229 if (!DR_state->mActive) return nullptr;
12231 NS_ASSERTION(aFrame, "invalid call");
12233 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12234 if (treeNode && treeNode->mDisplay) {
12235 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12236 printf("Get%sSize\n", aType);
12238 return treeNode;
12241 void nsIFrame::DisplayReflowExit(nsPresContext* aPresContext, nsIFrame* aFrame,
12242 ReflowOutput& aMetrics,
12243 const nsReflowStatus& aStatus,
12244 void* aFrameTreeNode) {
12245 if (!DR_state->mActive) return;
12247 NS_ASSERTION(aFrame, "DisplayReflowExit - invalid call");
12248 if (!aFrameTreeNode) return;
12250 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12251 if (treeNode->mDisplay) {
12252 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12254 char width[16];
12255 char height[16];
12256 char x[16];
12257 char y[16];
12258 DR_state->PrettyUC(aMetrics.Width(), width, 16);
12259 DR_state->PrettyUC(aMetrics.Height(), height, 16);
12260 printf("Reflow d=%s,%s", width, height);
12262 if (!aStatus.IsEmpty()) {
12263 printf(" status=%s", ToString(aStatus).c_str());
12265 if (aFrame->HasOverflowAreas()) {
12266 DR_state->PrettyUC(aMetrics.InkOverflow().x, x, 16);
12267 DR_state->PrettyUC(aMetrics.InkOverflow().y, y, 16);
12268 DR_state->PrettyUC(aMetrics.InkOverflow().width, width, 16);
12269 DR_state->PrettyUC(aMetrics.InkOverflow().height, height, 16);
12270 printf(" vis-o=(%s,%s) %s x %s", x, y, width, height);
12272 nsRect storedOverflow = aFrame->InkOverflowRect();
12273 DR_state->PrettyUC(storedOverflow.x, x, 16);
12274 DR_state->PrettyUC(storedOverflow.y, y, 16);
12275 DR_state->PrettyUC(storedOverflow.width, width, 16);
12276 DR_state->PrettyUC(storedOverflow.height, height, 16);
12277 printf(" vis-sto=(%s,%s) %s x %s", x, y, width, height);
12279 DR_state->PrettyUC(aMetrics.ScrollableOverflow().x, x, 16);
12280 DR_state->PrettyUC(aMetrics.ScrollableOverflow().y, y, 16);
12281 DR_state->PrettyUC(aMetrics.ScrollableOverflow().width, width, 16);
12282 DR_state->PrettyUC(aMetrics.ScrollableOverflow().height, height, 16);
12283 printf(" scr-o=(%s,%s) %s x %s", x, y, width, height);
12285 storedOverflow = aFrame->ScrollableOverflowRect();
12286 DR_state->PrettyUC(storedOverflow.x, x, 16);
12287 DR_state->PrettyUC(storedOverflow.y, y, 16);
12288 DR_state->PrettyUC(storedOverflow.width, width, 16);
12289 DR_state->PrettyUC(storedOverflow.height, height, 16);
12290 printf(" scr-sto=(%s,%s) %s x %s", x, y, width, height);
12292 printf("\n");
12293 if (DR_state->mDisplayPixelErrors) {
12294 int32_t d2a = aPresContext->AppUnitsPerDevPixel();
12295 CheckPixelError(aMetrics.Width(), d2a);
12296 CheckPixelError(aMetrics.Height(), d2a);
12299 DR_state->DeleteTreeNode(*treeNode);
12302 void nsIFrame::DisplayLayoutExit(nsIFrame* aFrame, void* aFrameTreeNode) {
12303 if (!DR_state->mActive) return;
12305 NS_ASSERTION(aFrame, "non-null frame required");
12306 if (!aFrameTreeNode) return;
12308 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12309 if (treeNode->mDisplay) {
12310 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12311 nsRect rect = aFrame->GetRect();
12312 printf("XULLayout=%d,%d,%d,%d\n", rect.x, rect.y, rect.width, rect.height);
12314 DR_state->DeleteTreeNode(*treeNode);
12317 void nsIFrame::DisplayIntrinsicISizeExit(nsIFrame* aFrame, const char* aType,
12318 nscoord aResult,
12319 void* aFrameTreeNode) {
12320 if (!DR_state->mActive) return;
12322 NS_ASSERTION(aFrame, "non-null frame required");
12323 if (!aFrameTreeNode) return;
12325 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12326 if (treeNode->mDisplay) {
12327 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12328 char iSize[16];
12329 DR_state->PrettyUC(aResult, iSize, 16);
12330 printf("Get%sISize=%s\n", aType, iSize);
12332 DR_state->DeleteTreeNode(*treeNode);
12335 void nsIFrame::DisplayIntrinsicSizeExit(nsIFrame* aFrame, const char* aType,
12336 nsSize aResult, void* aFrameTreeNode) {
12337 if (!DR_state->mActive) return;
12339 NS_ASSERTION(aFrame, "non-null frame required");
12340 if (!aFrameTreeNode) return;
12342 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aFrameTreeNode;
12343 if (treeNode->mDisplay) {
12344 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12346 char width[16];
12347 char height[16];
12348 DR_state->PrettyUC(aResult.width, width, 16);
12349 DR_state->PrettyUC(aResult.height, height, 16);
12350 printf("Get%sSize=%s,%s\n", aType, width, height);
12352 DR_state->DeleteTreeNode(*treeNode);
12355 /* static */
12356 void nsIFrame::DisplayReflowStartup() { DR_state = new DR_State(); }
12358 /* static */
12359 void nsIFrame::DisplayReflowShutdown() {
12360 delete DR_state;
12361 DR_state = nullptr;
12364 void DR_cookie::Change() const {
12365 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)mValue;
12366 if (treeNode && treeNode->mDisplay) {
12367 DisplayReflowEnterPrint(mPresContext, mFrame, mReflowInput, *treeNode,
12368 true);
12372 /* static */
12373 void* ReflowInput::DisplayInitConstraintsEnter(nsIFrame* aFrame,
12374 ReflowInput* aState,
12375 nscoord aContainingBlockWidth,
12376 nscoord aContainingBlockHeight,
12377 const nsMargin* aBorder,
12378 const nsMargin* aPadding) {
12379 MOZ_ASSERT(aFrame, "non-null frame required");
12380 MOZ_ASSERT(aState, "non-null state required");
12382 if (!DR_state->mInited) DR_state->Init();
12383 if (!DR_state->mActive) return nullptr;
12385 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, aState);
12386 if (treeNode && treeNode->mDisplay) {
12387 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12389 printf("InitConstraints parent=%p", (void*)aState->mParentReflowInput);
12391 char width[16];
12392 char height[16];
12394 DR_state->PrettyUC(aContainingBlockWidth, width, 16);
12395 DR_state->PrettyUC(aContainingBlockHeight, height, 16);
12396 printf(" cb=%s,%s", width, height);
12398 DR_state->PrettyUC(aState->AvailableWidth(), width, 16);
12399 DR_state->PrettyUC(aState->AvailableHeight(), height, 16);
12400 printf(" as=%s,%s", width, height);
12402 DR_state->PrintMargin("b", aBorder);
12403 DR_state->PrintMargin("p", aPadding);
12404 putchar('\n');
12406 return treeNode;
12409 /* static */
12410 void ReflowInput::DisplayInitConstraintsExit(nsIFrame* aFrame,
12411 ReflowInput* aState,
12412 void* aValue) {
12413 MOZ_ASSERT(aFrame, "non-null frame required");
12414 MOZ_ASSERT(aState, "non-null state required");
12416 if (!DR_state->mActive) return;
12417 if (!aValue) return;
12419 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aValue;
12420 if (treeNode->mDisplay) {
12421 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12422 char cmiw[16], cw[16], cmxw[16], cmih[16], ch[16], cmxh[16];
12423 DR_state->PrettyUC(aState->ComputedMinWidth(), cmiw, 16);
12424 DR_state->PrettyUC(aState->ComputedWidth(), cw, 16);
12425 DR_state->PrettyUC(aState->ComputedMaxWidth(), cmxw, 16);
12426 DR_state->PrettyUC(aState->ComputedMinHeight(), cmih, 16);
12427 DR_state->PrettyUC(aState->ComputedHeight(), ch, 16);
12428 DR_state->PrettyUC(aState->ComputedMaxHeight(), cmxh, 16);
12429 printf("InitConstraints= cw=(%s <= %s <= %s) ch=(%s <= %s <= %s)", cmiw, cw,
12430 cmxw, cmih, ch, cmxh);
12431 const nsMargin m = aState->ComputedPhysicalOffsets();
12432 DR_state->PrintMargin("co", &m);
12433 putchar('\n');
12435 DR_state->DeleteTreeNode(*treeNode);
12438 /* static */
12439 void* SizeComputationInput::DisplayInitOffsetsEnter(
12440 nsIFrame* aFrame, SizeComputationInput* aState, nscoord aPercentBasis,
12441 WritingMode aCBWritingMode, const nsMargin* aBorder,
12442 const nsMargin* aPadding) {
12443 MOZ_ASSERT(aFrame, "non-null frame required");
12444 MOZ_ASSERT(aState, "non-null state required");
12446 if (!DR_state->mInited) DR_state->Init();
12447 if (!DR_state->mActive) return nullptr;
12449 // aState is not necessarily a ReflowInput
12450 DR_FrameTreeNode* treeNode = DR_state->CreateTreeNode(aFrame, nullptr);
12451 if (treeNode && treeNode->mDisplay) {
12452 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12454 char pctBasisStr[16];
12455 DR_state->PrettyUC(aPercentBasis, pctBasisStr, 16);
12456 printf("InitOffsets pct_basis=%s", pctBasisStr);
12458 DR_state->PrintMargin("b", aBorder);
12459 DR_state->PrintMargin("p", aPadding);
12460 putchar('\n');
12462 return treeNode;
12465 /* static */
12466 void SizeComputationInput::DisplayInitOffsetsExit(nsIFrame* aFrame,
12467 SizeComputationInput* aState,
12468 void* aValue) {
12469 MOZ_ASSERT(aFrame, "non-null frame required");
12470 MOZ_ASSERT(aState, "non-null state required");
12472 if (!DR_state->mActive) return;
12473 if (!aValue) return;
12475 DR_FrameTreeNode* treeNode = (DR_FrameTreeNode*)aValue;
12476 if (treeNode->mDisplay) {
12477 DR_state->DisplayFrameTypeInfo(aFrame, treeNode->mIndent);
12478 printf("InitOffsets=");
12479 const auto m = aState->ComputedPhysicalMargin();
12480 DR_state->PrintMargin("m", &m);
12481 const auto p = aState->ComputedPhysicalPadding();
12482 DR_state->PrintMargin("p", &p);
12483 const auto bp = aState->ComputedPhysicalBorderPadding();
12484 DR_state->PrintMargin("b+p", &bp);
12485 putchar('\n');
12487 DR_state->DeleteTreeNode(*treeNode);
12490 // End Display Reflow
12492 // Validation of SideIsVertical.
12493 # define CASE(side, result) \
12494 static_assert(SideIsVertical(side) == result, "SideIsVertical is wrong")
12495 CASE(eSideTop, false);
12496 CASE(eSideRight, true);
12497 CASE(eSideBottom, false);
12498 CASE(eSideLeft, true);
12499 # undef CASE
12501 // Validation of HalfCornerIsX.
12502 # define CASE(corner, result) \
12503 static_assert(HalfCornerIsX(corner) == result, "HalfCornerIsX is wrong")
12504 CASE(eCornerTopLeftX, true);
12505 CASE(eCornerTopLeftY, false);
12506 CASE(eCornerTopRightX, true);
12507 CASE(eCornerTopRightY, false);
12508 CASE(eCornerBottomRightX, true);
12509 CASE(eCornerBottomRightY, false);
12510 CASE(eCornerBottomLeftX, true);
12511 CASE(eCornerBottomLeftY, false);
12512 # undef CASE
12514 // Validation of HalfToFullCorner.
12515 # define CASE(corner, result) \
12516 static_assert(HalfToFullCorner(corner) == result, \
12517 "HalfToFullCorner is " \
12518 "wrong")
12519 CASE(eCornerTopLeftX, eCornerTopLeft);
12520 CASE(eCornerTopLeftY, eCornerTopLeft);
12521 CASE(eCornerTopRightX, eCornerTopRight);
12522 CASE(eCornerTopRightY, eCornerTopRight);
12523 CASE(eCornerBottomRightX, eCornerBottomRight);
12524 CASE(eCornerBottomRightY, eCornerBottomRight);
12525 CASE(eCornerBottomLeftX, eCornerBottomLeft);
12526 CASE(eCornerBottomLeftY, eCornerBottomLeft);
12527 # undef CASE
12529 // Validation of FullToHalfCorner.
12530 # define CASE(corner, vert, result) \
12531 static_assert(FullToHalfCorner(corner, vert) == result, \
12532 "FullToHalfCorner is wrong")
12533 CASE(eCornerTopLeft, false, eCornerTopLeftX);
12534 CASE(eCornerTopLeft, true, eCornerTopLeftY);
12535 CASE(eCornerTopRight, false, eCornerTopRightX);
12536 CASE(eCornerTopRight, true, eCornerTopRightY);
12537 CASE(eCornerBottomRight, false, eCornerBottomRightX);
12538 CASE(eCornerBottomRight, true, eCornerBottomRightY);
12539 CASE(eCornerBottomLeft, false, eCornerBottomLeftX);
12540 CASE(eCornerBottomLeft, true, eCornerBottomLeftY);
12541 # undef CASE
12543 // Validation of SideToFullCorner.
12544 # define CASE(side, second, result) \
12545 static_assert(SideToFullCorner(side, second) == result, \
12546 "SideToFullCorner is wrong")
12547 CASE(eSideTop, false, eCornerTopLeft);
12548 CASE(eSideTop, true, eCornerTopRight);
12550 CASE(eSideRight, false, eCornerTopRight);
12551 CASE(eSideRight, true, eCornerBottomRight);
12553 CASE(eSideBottom, false, eCornerBottomRight);
12554 CASE(eSideBottom, true, eCornerBottomLeft);
12556 CASE(eSideLeft, false, eCornerBottomLeft);
12557 CASE(eSideLeft, true, eCornerTopLeft);
12558 # undef CASE
12560 // Validation of SideToHalfCorner.
12561 # define CASE(side, second, parallel, result) \
12562 static_assert(SideToHalfCorner(side, second, parallel) == result, \
12563 "SideToHalfCorner is wrong")
12564 CASE(eSideTop, false, true, eCornerTopLeftX);
12565 CASE(eSideTop, false, false, eCornerTopLeftY);
12566 CASE(eSideTop, true, true, eCornerTopRightX);
12567 CASE(eSideTop, true, false, eCornerTopRightY);
12569 CASE(eSideRight, false, false, eCornerTopRightX);
12570 CASE(eSideRight, false, true, eCornerTopRightY);
12571 CASE(eSideRight, true, false, eCornerBottomRightX);
12572 CASE(eSideRight, true, true, eCornerBottomRightY);
12574 CASE(eSideBottom, false, true, eCornerBottomRightX);
12575 CASE(eSideBottom, false, false, eCornerBottomRightY);
12576 CASE(eSideBottom, true, true, eCornerBottomLeftX);
12577 CASE(eSideBottom, true, false, eCornerBottomLeftY);
12579 CASE(eSideLeft, false, false, eCornerBottomLeftX);
12580 CASE(eSideLeft, false, true, eCornerBottomLeftY);
12581 CASE(eSideLeft, true, false, eCornerTopLeftX);
12582 CASE(eSideLeft, true, true, eCornerTopLeftY);
12583 # undef CASE
12585 #endif