Skip to content

Commit 81dccee

Browse files
committed
test(svg): lock transform ancestor depth behavior
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 362d9ae commit 81dccee

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

src/Pdf/Svg/SvgTransformResolver.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ public function resolveElementTransformMatrix(DOMElement $element): array
3535
$ancestors = [];
3636
$cursor = $element;
3737

38-
for ($depth = 0; $depth < self::MAX_ANCESTOR_DEPTH; ++$depth) {
39-
if (!$cursor instanceof DOMElement) {
40-
break;
41-
}
42-
38+
for ($depth = 0; $depth < self::MAX_ANCESTOR_DEPTH && $cursor instanceof DOMElement; ++$depth) {
4339
$ancestors[] = $cursor;
4440
$cursor = $cursor->parentNode;
4541
}

tests/Unit/Pdf/Svg/SvgTransformResolverTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ public function testResolveElementTransformMatrixIncludesSvgRootAndTargetTransfo
7777
self::assertEqualsWithDelta(2.0, $actualY, 0.0001);
7878
}
7979

80+
public function testResolveElementTransformMatrixCapsAncestorDepthToPreventUnlimitedTraversal(): void
81+
{
82+
$resolver = new SvgTransformResolver();
83+
$element = $this->createDeepElementWithUnitTranslateTransforms(2100);
84+
85+
$matrix = $resolver->resolveElementTransformMatrix($element);
86+
[$actualX, $actualY] = $resolver->applyTransformToPoint($matrix, 0.0, 0.0);
87+
88+
self::assertEqualsWithDelta(2048.0, $actualX, 0.0001);
89+
self::assertEqualsWithDelta(0.0, $actualY, 0.0001);
90+
}
91+
8092
public function testResolveElementTransformMatrixIgnoresWhitespaceOnlyAncestorTransform(): void
8193
{
8294
$resolver = new SvgTransformResolver();
@@ -338,4 +350,27 @@ private function createElementWithRootAndTargetTransforms(
338350

339351
return $target;
340352
}
353+
354+
private function createDeepElementWithUnitTranslateTransforms(int $groupCount): DOMElement
355+
{
356+
$document = new DOMDocument('1.0', 'UTF-8');
357+
$svg = $document->createElement('svg');
358+
$svg->setAttribute('transform', 'translate(1,0)');
359+
$document->appendChild($svg);
360+
361+
$current = $svg;
362+
363+
for ($index = 0; $index < $groupCount; ++$index) {
364+
$group = $document->createElement('g');
365+
$group->setAttribute('transform', 'translate(1,0)');
366+
$current->appendChild($group);
367+
$current = $group;
368+
}
369+
370+
$target = $document->createElement('path');
371+
$target->setAttribute('transform', 'translate(1,0)');
372+
$current->appendChild($target);
373+
374+
return $target;
375+
}
341376
}

0 commit comments

Comments
 (0)