commit 41e4c77b77e3d31c416f58cbea290e5d011a6963
parent a0afdea225a83ec43c47652b676c5d2f9dce6f88
Author: longsonr <longsonr@gmail.com>
Date: Tue, 7 Oct 2025 17:40:32 +0000
Bug 1992972 - Return an identity matrix if we can't calculate one in getCTM or getScreenCTM r=emilio
Differential Revision: https://phabricator.services.mozilla.com/D267832
Diffstat:
4 files changed, 26 insertions(+), 49 deletions(-)
diff --git a/dom/svg/SVGContentUtils.cpp b/dom/svg/SVGContentUtils.cpp
@@ -464,7 +464,7 @@ static gfx::Matrix GetCTMInternal(SVGElement* aElement, CTMType aCTMType,
matrix *= getLocalTransformHelper(element, true);
if (aCTMType == CTMType::NearestViewport) {
if (element->IsSVGElement(nsGkAtoms::foreignObject)) {
- return gfx::Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
+ return {};
}
if (EstablishesViewport(element)) {
// XXX spec seems to say x,y translation should be undone for IsInnerSVG
@@ -475,11 +475,11 @@ static gfx::Matrix GetCTMInternal(SVGElement* aElement, CTMType aCTMType,
}
if (aCTMType == CTMType::NearestViewport) {
// didn't find a nearestViewportElement
- return gfx::Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
+ return {};
}
if (!element->IsSVGElement(nsGkAtoms::svg)) {
// Not a valid SVG fragment
- return gfx::Matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
+ return {};
}
if (element == aElement && !aHaveRecursed) {
// We get here when getScreenCTM() is called on an outer-<svg>.
diff --git a/dom/svg/SVGGraphicsElement.cpp b/dom/svg/SVGGraphicsElement.cpp
@@ -121,9 +121,7 @@ already_AddRefed<SVGMatrix> SVGGraphicsElement::GetCTM() {
currentDoc->FlushPendingNotifications(FlushType::Layout);
}
gfx::Matrix m = SVGContentUtils::GetCTM(this);
- RefPtr<SVGMatrix> mat =
- m.IsSingular() ? nullptr : new SVGMatrix(ThebesMatrix(m));
- return mat.forget();
+ return do_AddRef(new SVGMatrix(ThebesMatrix(m)));
}
already_AddRefed<SVGMatrix> SVGGraphicsElement::GetScreenCTM() {
@@ -132,9 +130,7 @@ already_AddRefed<SVGMatrix> SVGGraphicsElement::GetScreenCTM() {
currentDoc->FlushPendingNotifications(FlushType::Layout);
}
gfx::Matrix m = SVGContentUtils::GetScreenCTM(this);
- RefPtr<SVGMatrix> mat =
- m.IsSingular() ? nullptr : new SVGMatrix(ThebesMatrix(m));
- return mat.forget();
+ return do_AddRef(new SVGMatrix(ThebesMatrix(m)));
}
bool SVGGraphicsElement::IsSVGFocusable(bool* aIsFocusable,
diff --git a/dom/svg/test/test_getCTM.html b/dom/svg/test/test_getCTM.html
@@ -27,6 +27,15 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=366697
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
+function isMatrix(actual, expected, s) {
+ is(actual.a, expected.a, s + " a");
+ is(actual.b, expected.b, s + " b");
+ is(actual.c, expected.c, s + " c");
+ is(actual.d, expected.d, s + " d");
+ is(actual.e, expected.e, s + " e");
+ is(actual.f, expected.f, s + " f");
+}
+
function runTest() {
let doc = $("svg").contentWindow.document;
@@ -42,12 +51,11 @@ function runTest() {
let padsvg1 = document.getElementById("padsvg1");
let ctm = padsvg1.getScreenCTM();
let rect = padsvg1.getBoundingClientRect();
- // Use isfuzzy to ignore some miniscule floating-point precision error on
- // certain platforms:
isfuzzy(ctm.e - rect.x, 27, 0.0001, "padsvg1.getScreenCTM().e");
is(ctm.f - rect.y, 43, "padsvg1.getScreenCTM().f");
let root = doc.documentElement;
+ const identity = root.createSVGMatrix();
let inner = doc.getElementById("inner");
let g1 = doc.getElementById("g1");
let outer = doc.getElementById("outer");
@@ -58,65 +66,46 @@ function runTest() {
let g5 = doc.getElementById("g5");
let symbolRect = doc.getElementById("symbolRect");
let fO = doc.getElementById("fO");
- /* Tests the consistency with nearestViewportElement
- (code is from test_viewport.html) */
- // root.nearestViewportElement == null
- is((function() { try { return root.getCTM(); } catch (e) { return e; } })(), null, "root.getCTM()");
- // inner.nearestViewportElement == root
+ isMatrix((function() { try { return root.getCTM(); } catch (e) { return e; } })(), identity, "root.getCTM()");
is((function() { try { return inner.getCTM().e; } catch (e) { return e; } })(), 1, "inner.getCTM().e");
is((function() { try { return inner.getCTM().f; } catch (e) { return e; } })(), 2, "inner.getCTM().f");
- // g1.nearestViewportElement == inner
is((function() { try { return g1.getCTM().e; } catch (e) { return e; } })(), 30, "g1.getCTM().e");
is((function() { try { return g1.getCTM().f; } catch (e) { return e; } })(), 40, "g1.getCTM().f");
- // outer.nearestViewportElement == null
- is((function() { try { return outer.getCTM(); } catch (e) { return e; } })(), null, "outer.getCTM()");
- // g2.nearestViewportElement == outer
+ isMatrix((function() { try { return outer.getCTM(); } catch (e) { return e; } })(), identity, "outer.getCTM()");
is((function() { try { return g2.getCTM().e; } catch (e) { return e; } })(), 600, "g2.getCTM().e");
is((function() { try { return g2.getCTM().f; } catch (e) { return e; } })(), 700, "g2.getCTM().f");
- // g3.nearestViewportElement == null
- is((function() { try { return g3.getCTM(); } catch (e) { return e; } })(), null, "g3.getCTM()");
- // g4.nearestViewportElement == null
+ isMatrix((function() { try { return g3.getCTM(); } catch (e) { return e; } })(), identity, "g3.getCTM()");
is((function() { try { return g4.getCTM().e; } catch (e) { return e; } })(), 1, "g4.getCTM().e");
is((function() { try { return g4.getCTM().f; } catch (e) { return e; } })(), 2, "g4.getCTM().f");
- // symbolRect.nearestViewportElement == sym
is((function() { try { return symbolRect.getCTM().e; } catch (e) { return e; } })(), 71, "symbolRect.getCTM().e");
is((function() { try { return symbolRect.getCTM().f; } catch (e) { return e; } })(), 82, "symbolRect.getCTM().f");
- // fO.nearestViewportElement == <svg> with no 'id'
is((function() { try { return fO.getCTM().e; } catch (e) { return e; } })(), 2, "fO.getCTM().e");
is((function() { try { return fO.getCTM().f; } catch (e) { return e; } })(), 3, "fO.getCTM().f");
- // g5.nearestViewportElement == inner-2
- is((function() { try { return g5.getCTM(); } catch (e) { return e; } })(), null, "g5.getCTM()");
+ let singular = root.createSVGMatrix();
+ singular.a = 0;
+ singular.d = 0;
+ isMatrix((function() { try { return g5.getCTM(); } catch (e) { return e; } })(), singular, "g5.getCTM()");
- /* Tests the consistency with farthestViewportElement
- (code is from test_viewport.html) */
- // root.farthestViewportElement == null (but actually == root)
is((function() { try { return root.getScreenCTM().e; } catch (e) { return e; } })(), 11, "root.getScreenCTM().e");
is((function() { try { return root.getScreenCTM().f; } catch (e) { return e; } })(), 22, "root.getScreenCTM().f");
- // inner.farthestViewportElement == root
is((function() { try { return inner.getScreenCTM().e; } catch (e) { return e; } })(), 15, "inner.getScreenCTM().e");
is((function() { try { return inner.getScreenCTM().f; } catch (e) { return e; } })(), 28, "inner.getScreenCTM().f");
- // g1.farthestViewportElement == root
is((function() { try { return g1.getScreenCTM().e; } catch (e) { return e; } })(), 45, "g1.getScreenCTM().e");
is((function() { try { return g1.getScreenCTM().f; } catch (e) { return e; } })(), 68, "g1.getScreenCTM().f");
- // outer.farthestViewportElement == null (but actually == root)
is((function() { try { return outer.getScreenCTM().e; } catch (e) { return e; } })(), 46, "outer.getScreenCTM().e");
is((function() { try { return outer.getScreenCTM().f; } catch (e) { return e; } })(), 69, "outer.getScreenCTM().f");
- // outer.farthestViewportElement == null (but actually == root)
is((function() { try { return outer2.getScreenCTM().e; } catch (e) { return e; } })(), -4, "outer2.getScreenCTM().e");
is((function() { try { return outer2.getScreenCTM().f; } catch (e) { return e; } })(), 19, "outer2.getScreenCTM().f");
- // g2.farthestViewportElement == outer (but actually == root)
is((function() { try { return g2.getScreenCTM().e; } catch (e) { return e; } })(), 646, "g2.getScreenCTM().e");
is((function() { try { return g2.getScreenCTM().f; } catch (e) { return e; } })(), 769, "g2.getScreenCTM().f");
- // g3.farthestViewportElement == null (but actually == null)
- is((function() { try { return g3.getScreenCTM(); } catch (e) { return e; } })(), null, "g3.getScreenCTM()");
- // symbolRect.farthestViewportElement == root
+ isMatrix((function() { try { return g3.getScreenCTM(); } catch (e) { return e; } })(), identity, "g3.getScreenCTM()");
is((function() { try { return symbolRect.getScreenCTM().e; } catch (e) { return e; } })(), 85, "symbolRect.getScreenCTM().e");
is((function() { try { return symbolRect.getScreenCTM().f; } catch (e) { return e; } })(), 108, "symbolRect.getScreenCTM().f");
- // fO.farthestViewportElement == root
is((function() { try { return fO.getScreenCTM().e; } catch (e) { return e; } })(), 16, "symbolRect.getScreenCTM().e");
is((function() { try { return fO.getScreenCTM().f; } catch (e) { return e; } })(), 29, "symbolRect.getScreenCTM().f");
- // g5.farthestViewportElement == root
- is((function() { try { return g5.getScreenCTM(); } catch (e) { return e; } })(), null, "g5.getScreenCTM()");
+ singular.e = 15;
+ singular.f = 28;
+ isMatrix((function() { try { return g5.getScreenCTM(); } catch (e) { return e; } })(), singular, "g5.getScreenCTM()");
SimpleTest.finish();
}
diff --git a/testing/web-platform/meta/svg/types/scripted/SVGGraphicsElement.svg.ini b/testing/web-platform/meta/svg/types/scripted/SVGGraphicsElement.svg.ini
@@ -1,8 +0,0 @@
-[SVGGraphicsElement.svg]
- expected:
- if (os == "android") and fission: [TIMEOUT, OK]
- [getCTM() returns instance of SVGMatrix]
- expected: FAIL
-
- [getScreenCTM() returns instance of SVGMatrix]
- expected: FAIL