Skip to content

Commit 092e32c

Browse files
committed
Improve use handling
- ensures nested groups are rendered - merges styles from use element with referenced element - fixes order of end render calls
1 parent bb2eee6 commit 092e32c

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/Svg/Tag/UseTag.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ protected function before($attributes)
4848
$link = $attributes["href"] ?? $attributes["xlink:href"];
4949
$this->reference = $document->getDef($link);
5050

51-
if ($this->reference) {
52-
$this->reference->before($attributes);
53-
}
54-
5551
$surface = $document->getSurface();
5652
$surface->save();
5753

@@ -63,11 +59,6 @@ protected function after() {
6359
return;
6460
}
6561
parent::after();
66-
67-
if ($this->reference) {
68-
$this->reference->after();
69-
}
70-
7162
$this->getDocument()->getSurface()->restore();
7263
}
7364

@@ -84,19 +75,22 @@ public function handle($attributes)
8475
return;
8576
}
8677

78+
$originalAttributes = array_merge($this->reference->attributes);
79+
$originalStyle = $this->reference->getStyle();
8780
$mergedAttributes = $this->reference->attributes;
88-
$attributesToNotMerge = ['x', 'y', 'width', 'height', 'href', 'xlink:href', 'id'];
81+
$attributesToNotMerge = ['x', 'y', 'width', 'height', 'href', 'xlink:href', 'id', 'style'];
8982
foreach ($attributes as $attrKey => $attrVal) {
9083
if (!in_array($attrKey, $attributesToNotMerge) && !isset($mergedAttributes[$attrKey])) {
9184
$mergedAttributes[$attrKey] = $attrVal;
9285
}
9386
}
87+
$mergedAttributes['style'] = ($attributes['style'] ?? '') . ';' . ($mergedAttributes['style'] ?? '');
9488

95-
$this->reference->handle($mergedAttributes);
89+
$this->_handle($this->reference, $mergedAttributes);
9690

97-
foreach ($this->reference->children as $_child) {
98-
$_attributes = array_merge($_child->attributes, $mergedAttributes);
99-
$_child->handle($_attributes);
91+
$this->reference->attributes = $originalAttributes;
92+
if ($originalStyle !== null) {
93+
$this->reference->setStyle($originalStyle);
10094
}
10195
}
10296

@@ -107,16 +101,32 @@ public function handleEnd()
107101
return;
108102
}
109103

104+
if ($this->reference) {
105+
$this->_handleEnd($this->reference);
106+
}
107+
110108
parent::handleEnd();
109+
}
111110

112-
if (!$this->reference) {
113-
return;
111+
private function _handle($tag, $attributes) {
112+
$tag->handle($attributes);
113+
foreach ($tag->children as $child) {
114+
$originalAttributes = array_merge($child->attributes);
115+
$originalStyle = $child->getStyle();
116+
$mergedAttributes = $child->attributes;
117+
$mergedAttributes['style'] = ($attributes['style'] ?? '') . ';' . ($mergedAttributes['style'] ?? '');
118+
$this->_handle($child, $mergedAttributes);
119+
$child->attributes = $originalAttributes;
120+
if ($originalStyle !== null) {
121+
$child->setStyle($originalStyle);
122+
}
114123
}
124+
}
115125

116-
$this->reference->handleEnd();
117-
118-
foreach ($this->reference->children as $_child) {
119-
$_child->handleEnd();
126+
private function _handleEnd($tag) {
127+
foreach ($tag->children as $child) {
128+
$this->_handleEnd($child);
120129
}
130+
$tag->handleEnd();
121131
}
122132
}

0 commit comments

Comments
 (0)