Skip to content

Commit 0e9dc9d

Browse files
committed
Handle nested definition elements
Such as a clipPath or symbol contained within a defs element.
1 parent 0e46722 commit 0e9dc9d

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/Svg/Document.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
class Document extends AbstractTag
3232
{
3333
protected $filename;
34+
protected $_defs_depth = 0;
3435
public $inDefs = false;
3536

3637
protected $x;
@@ -81,6 +82,31 @@ public function __construct() {
8182

8283
}
8384

85+
/**
86+
* Increase the nesting level for defs-like elements
87+
*
88+
* @return int
89+
*/
90+
public function enterDefs () {
91+
$this->_defs_depth++;
92+
$this->inDefs = true;
93+
return $this->_defs_depth;
94+
}
95+
96+
/**
97+
* Decrease the nesting level for defs-like elements
98+
*
99+
* @return int
100+
*/
101+
public function exitDefs () {
102+
$this->_defs_depth--;
103+
if ($this->_defs_depth < 0) {
104+
$this->_defs_depth = 0;
105+
}
106+
$this->inDefs = ($this->_defs_depth > 0 ? true : false);
107+
return $this->_defs_depth;
108+
}
109+
84110
/**
85111
* @return SurfaceInterface
86112
*/
@@ -215,6 +241,7 @@ protected function before($attributes)
215241

216242
public function render(SurfaceInterface $surface)
217243
{
244+
$this->_defs_depth = 0;
218245
$this->inDefs = false;
219246
$this->surface = $surface;
220247

@@ -258,7 +285,7 @@ private function _tagStart($parser, $name, $attributes)
258285

259286
switch (strtolower($name)) {
260287
case 'defs':
261-
$this->inDefs = true;
288+
$this->enterDefs();
262289
return;
263290

264291
case 'svg':
@@ -328,7 +355,7 @@ private function _tagStart($parser, $name, $attributes)
328355
break;
329356

330357
case 'symbol':
331-
$this->inDefs = true;
358+
$this->enterDefs();
332359
$tag = new Symbol($this, $name);
333360
break;
334361

@@ -381,11 +408,11 @@ function _tagEnd($parser, $name)
381408
$tag = null;
382409
switch (strtolower($name)) {
383410
case 'defs':
384-
$this->inDefs = false;
411+
$this->exitDefs();
385412
return;
386413

387414
case 'symbol':
388-
$this->inDefs = false;
415+
$this->exitDefs();
389416
$tag = array_pop($this->stack);
390417
break;
391418

0 commit comments

Comments
 (0)