Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nested definition elements #120

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/Svg/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class Document extends AbstractTag
{
protected $filename;
protected $_defs_depth = 0;
public $inDefs = false;

protected $x;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -215,6 +241,7 @@ protected function before($attributes)

public function render(SurfaceInterface $surface)
{
$this->_defs_depth = 0;
$this->inDefs = false;
$this->surface = $surface;

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

switch (strtolower($name)) {
case 'defs':
$this->inDefs = true;
$this->enterDefs();
return;

case 'svg':
Expand Down Expand Up @@ -328,7 +355,7 @@ private function _tagStart($parser, $name, $attributes)
break;

case 'symbol':
$this->inDefs = true;
$this->enterDefs();
$tag = new Symbol($this, $name);
break;

Expand Down Expand Up @@ -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;

Expand Down
Loading