Skip to content

Commit

Permalink
Bug 1827662 - fix getBoundingClientRect when use elements are present…
Browse files Browse the repository at this point in the history
… r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D176629
  • Loading branch information
longsonr committed Apr 29, 2023
1 parent 38eec57 commit 2219808
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 27 deletions.
3 changes: 3 additions & 0 deletions dom/svg/test/bounds-helper.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions dom/svg/test/test_bounds.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
is(use1Bounds.width, 50, "use1.getBoundingClientRect().width");
is(use1Bounds.height, 10, "use1.getBoundingClientRect().height");

var useChildBounds = doc.getElementById("a-use").getBoundingClientRect();
is(useChildBounds.left, 100, "useChild.getBoundingClientRect().left");
is(useChildBounds.top, 50, "useChild.getBoundingClientRect().top");
is(useChildBounds.width, 50, "useChild.getBoundingClientRect().width");
is(useChildBounds.height, 10, "useChild.getBoundingClientRect().height");

var text1 = doc.getElementById("text1");

var text1Bounds = text1.getBoundingClientRect();
Expand Down
26 changes: 5 additions & 21 deletions layout/svg/SVGUseFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ void SVGUseFrame::ReflowSVG() {
// We only handle x/y offset here, since any width/height that is in force is
// handled by the SVGOuterSVGFrame for the anonymous <svg> that will be
// created for that purpose.
auto [x, y] = ResolvePosition();
auto* content = SVGUseElement::FromNode(GetContent());
float x = SVGContentUtils::CoordToFloat(content, StyleSVGReset()->mX,
SVGContentUtils::X);
float y = SVGContentUtils::CoordToFloat(content, StyleSVGReset()->mY,
SVGContentUtils::Y);
mRect.MoveTo(nsLayoutUtils::RoundGfxRectToAppRect(gfxRect(x, y, 0, 0),
AppUnitsPerCSSPixel())
.TopLeft());
Expand Down Expand Up @@ -138,24 +142,4 @@ void SVGUseFrame::NotifySVGChanged(uint32_t aFlags) {
SVGGFrame::NotifySVGChanged(aFlags);
}

SVGBBox SVGUseFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
uint32_t aFlags) {
SVGBBox bbox =
SVGDisplayContainerFrame::GetBBoxContribution(aToBBoxUserspace, aFlags);

if (aFlags & SVGUtils::eForGetClientRects) {
auto [x, y] = ResolvePosition();
bbox.MoveBy(x, y);
}
return bbox;
}

std::pair<float, float> SVGUseFrame::ResolvePosition() const {
auto* content = SVGUseElement::FromNode(GetContent());
return std::make_pair(SVGContentUtils::CoordToFloat(
content, StyleSVGReset()->mX, SVGContentUtils::X),
SVGContentUtils::CoordToFloat(
content, StyleSVGReset()->mY, SVGContentUtils::Y));
}

} // namespace mozilla
4 changes: 0 additions & 4 deletions layout/svg/SVGUseFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ class SVGUseFrame final : public SVGGFrame {
// ISVGDisplayableFrame interface:
void ReflowSVG() override;
void NotifySVGChanged(uint32_t aFlags) override;
SVGBBox GetBBoxContribution(const Matrix& aToBBoxUserspace,
uint32_t aFlags) override;

private:
std::pair<float, float> ResolvePosition() const;

bool mHasValidDimensions;
};

Expand Down
3 changes: 2 additions & 1 deletion layout/svg/SVGUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ nsIFrame* SVGUtils::GetOuterSVGFrameAndCoveredRegion(nsIFrame* aFrame,
} else {
uint32_t flags = SVGUtils::eForGetClientRects | SVGUtils::eBBoxIncludeFill |
SVGUtils::eBBoxIncludeStroke |
SVGUtils::eBBoxIncludeMarkers;
SVGUtils::eBBoxIncludeMarkers |
SVGUtils::eUseUserSpaceOfUseElement;

auto ctm = nsLayoutUtils::GetTransformToAncestor(RelativeTo{aFrame},
RelativeTo{outer});
Expand Down
1 change: 0 additions & 1 deletion layout/svg/SVGUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class SVGBBox final {
bool IsFinite() const { return mBBox.IsFinite(); }

void Scale(float aScale) { mBBox.Scale(aScale); }
void MoveBy(float x, float y) { mBBox.MoveBy(x, y); }

void UnionEdges(const SVGBBox& aSVGBBox) {
if (aSVGBBox.mIsEmpty) {
Expand Down

0 comments on commit 2219808

Please sign in to comment.