Bug 1748835 [wpt PR 32273] - Avoid reftest-wait timeout if ::target-text is unsupport...
[gecko.git] / layout / svg / SVGUseFrame.cpp
blobdf2cbc768ac71889bff4833ee1602eeb0c63c54d
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 #include "SVGUseFrame.h"
9 #include "mozilla/PresShell.h"
10 #include "mozilla/SVGObserverUtils.h"
11 #include "mozilla/SVGUtils.h"
12 #include "mozilla/dom/MutationEvent.h"
13 #include "mozilla/dom/SVGUseElement.h"
15 using namespace mozilla::dom;
17 //----------------------------------------------------------------------
18 // Implementation
20 nsIFrame* NS_NewSVGUseFrame(mozilla::PresShell* aPresShell,
21 mozilla::ComputedStyle* aStyle) {
22 return new (aPresShell)
23 mozilla::SVGUseFrame(aStyle, aPresShell->GetPresContext());
26 namespace mozilla {
28 NS_IMPL_FRAMEARENA_HELPERS(SVGUseFrame)
30 //----------------------------------------------------------------------
31 // nsIFrame methods:
33 void SVGUseFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
34 nsIFrame* aPrevInFlow) {
35 NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::use),
36 "Content is not an SVG use!");
38 mHasValidDimensions =
39 static_cast<SVGUseElement*>(aContent)->HasValidDimensions();
41 SVGGFrame::Init(aContent, aParent, aPrevInFlow);
44 nsresult SVGUseFrame::AttributeChanged(int32_t aNamespaceID, nsAtom* aAttribute,
45 int32_t aModType) {
46 // Currently our SMIL implementation does not modify the DOM attributes. Once
47 // we implement the SVG 2 SMIL behaviour this can be removed
48 // SVGUseElement::AfterSetAttr's implementation will be sufficient.
49 if (aModType == MutationEvent_Binding::SMIL) {
50 auto* content = SVGUseElement::FromNode(GetContent());
51 content->ProcessAttributeChange(aNamespaceID, aAttribute);
54 return SVGGFrame::AttributeChanged(aNamespaceID, aAttribute, aModType);
57 void SVGUseFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
58 SVGGFrame::DidSetComputedStyle(aOldComputedStyle);
60 if (!aOldComputedStyle) {
61 return;
63 const auto* newSVGReset = StyleSVGReset();
64 const auto* oldSVGReset = aOldComputedStyle->StyleSVGReset();
66 if (newSVGReset->mX != oldSVGReset->mX ||
67 newSVGReset->mY != oldSVGReset->mY) {
68 // make sure our cached transform matrix gets (lazily) updated
69 mCanvasTM = nullptr;
70 SVGUtils::ScheduleReflowSVG(this);
71 SVGUtils::NotifyChildrenOfSVGChange(this, TRANSFORM_CHANGED);
75 void SVGUseFrame::DimensionAttributeChanged(bool aHadValidDimensions,
76 bool aAttributeIsUsed) {
77 bool invalidate = aAttributeIsUsed;
78 if (mHasValidDimensions != aHadValidDimensions) {
79 mHasValidDimensions = !mHasValidDimensions;
80 invalidate = true;
83 if (invalidate) {
84 nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint{0},
85 nsChangeHint_InvalidateRenderingObservers);
86 SVGUtils::ScheduleReflowSVG(this);
90 void SVGUseFrame::HrefChanged() {
91 nsLayoutUtils::PostRestyleEvent(GetContent()->AsElement(), RestyleHint{0},
92 nsChangeHint_InvalidateRenderingObservers);
93 SVGUtils::ScheduleReflowSVG(this);
96 //----------------------------------------------------------------------
97 // ISVGDisplayableFrame methods
99 void SVGUseFrame::ReflowSVG() {
100 // We only handle x/y offset here, since any width/height that is in force is
101 // handled by the SVGOuterSVGFrame for the anonymous <svg> that will be
102 // created for that purpose.
103 auto [x, y] = ResolvePosition();
104 mRect.MoveTo(nsLayoutUtils::RoundGfxRectToAppRect(gfxRect(x, y, 0, 0),
105 AppUnitsPerCSSPixel())
106 .TopLeft());
108 // If we have a filter, we need to invalidate ourselves because filter
109 // output can change even if none of our descendants need repainting.
110 if (StyleEffects()->HasFilters()) {
111 InvalidateFrame();
114 SVGGFrame::ReflowSVG();
117 void SVGUseFrame::NotifySVGChanged(uint32_t aFlags) {
118 if (aFlags & COORD_CONTEXT_CHANGED && !(aFlags & TRANSFORM_CHANGED)) {
119 // Coordinate context changes affect mCanvasTM if we have a
120 // percentage 'x' or 'y'
121 if (StyleSVGReset()->mX.HasPercent() || StyleSVGReset()->mY.HasPercent()) {
122 aFlags |= TRANSFORM_CHANGED;
123 // Ancestor changes can't affect how we render from the perspective of
124 // any rendering observers that we may have, so we don't need to
125 // invalidate them. We also don't need to invalidate ourself, since our
126 // changed ancestor will have invalidated its entire area, which includes
127 // our area.
128 // For perf reasons we call this before calling NotifySVGChanged() below.
129 SVGUtils::ScheduleReflowSVG(this);
133 // We don't remove the TRANSFORM_CHANGED flag here if we have a viewBox or
134 // non-percentage width/height, since if they're set then they are cloned to
135 // an anonymous child <svg>, and its SVGInnerSVGFrame will do that.
137 SVGGFrame::NotifySVGChanged(aFlags);
140 SVGBBox SVGUseFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
141 uint32_t aFlags) {
142 SVGBBox bbox =
143 SVGDisplayContainerFrame::GetBBoxContribution(aToBBoxUserspace, aFlags);
145 if (aFlags & SVGUtils::eForGetClientRects) {
146 auto [x, y] = ResolvePosition();
147 bbox.MoveBy(x, y);
149 return bbox;
152 std::pair<float, float> SVGUseFrame::ResolvePosition() const {
153 auto* content = SVGUseElement::FromNode(GetContent());
154 return std::make_pair(SVGContentUtils::CoordToFloat(
155 content, StyleSVGReset()->mX, SVGContentUtils::X),
156 SVGContentUtils::CoordToFloat(
157 content, StyleSVGReset()->mY, SVGContentUtils::Y));
160 } // namespace mozilla