From 13d234da0606e2f44330c38daf9e0f30c75e1ed7 Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Thu, 28 Mar 2024 18:33:42 -0400 Subject: [PATCH] Handle nested definition elements Such as a clipPath or symbol contained within a defs element. --- src/Svg/Document.php | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Svg/Document.php b/src/Svg/Document.php index e5c2c24..b6448f3 100644 --- a/src/Svg/Document.php +++ b/src/Svg/Document.php @@ -31,6 +31,7 @@ class Document extends AbstractTag { protected $filename; + protected $_defs_depth = 0; public $inDefs = false; protected $x; @@ -81,6 +82,31 @@ public function __construct() { } + /** + * Increase the nesting level for defs-like elements + * + * @return int + */ + public function enterDefs () { + $this->_defs_depth++; + $this->inDefs = true; + return $this->_defs_depth; + } + + /** + * Decrease the nesting level for defs-like elements + * + * @return int + */ + public function exitDefs () { + $this->_defs_depth--; + if ($this->_defs_depth < 0) { + $this->_defs_depth = 0; + } + $this->inDefs = ($this->_defs_depth > 0 ? true : false); + return $this->_defs_depth; + } + /** * @return SurfaceInterface */ @@ -215,6 +241,7 @@ protected function before($attributes) public function render(SurfaceInterface $surface) { + $this->_defs_depth = 0; $this->inDefs = false; $this->surface = $surface; @@ -258,7 +285,7 @@ private function _tagStart($parser, $name, $attributes) switch (strtolower($name)) { case 'defs': - $this->inDefs = true; + $this->enterDefs(); return; case 'svg': @@ -328,7 +355,7 @@ private function _tagStart($parser, $name, $attributes) break; case 'symbol': - $this->inDefs = true; + $this->enterDefs(); $tag = new Symbol($this, $name); break; @@ -381,11 +408,11 @@ function _tagEnd($parser, $name) $tag = null; switch (strtolower($name)) { case 'defs': - $this->inDefs = false; + $this->exitDefs(); return; case 'symbol': - $this->inDefs = false; + $this->exitDefs(); $tag = array_pop($this->stack); break;