Skip to content
Open
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
42 changes: 41 additions & 1 deletion src/Nodes/Structures/SVGUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace SVG\Nodes\Structures;

use SVG\Nodes\Embedded\SVGImage;
use SVG\Nodes\SVGNodeContainer;
use SVG\Rasterization\SVGRasterizer;
use SVG\Rasterization\Transform\TransformParser;

/**
* Represents the SVG tag 'use'.
Expand All @@ -17,11 +19,49 @@ public function __construct()
parent::__construct();
}

/**
* @return string|null The referenced element.
*/
public function getHref(): ?string
{
return $this->getAttribute('xlink:href') ?: $this->getAttribute('href');
}

/**
* Sets the element reference.
*
* @param string|null $href The new element reference.
*
* @return $this This node instance, for call chaining.
*/
public function setHref(?string $href): SVGUse
{
return $this->setAttribute('xlink:href', $href);
}

/**
* @inheritdoc
*/
public function rasterize(SVGRasterizer $rasterizer): void
{
// Nothing to rasterize.
$element = $this->getHref();
if (empty($element)) {
return;
}

/** @var SVGDocumentFragment $root */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this doc comment

Suggested change
/** @var SVGDocumentFragment $root */

do {
$root = $this->getParent();
} while ($root->getParent() != null);
$element = $root->getElementById(substr($element, strpos($element, "#") + 1 ?: 0));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please avoid reusing variables, especially when the type changes ($element was string before this line, but is now a node object). Maybe rename the string variable to $href to reduce confusion.
  2. strpos($element, "#") + 1 ?: 0 is problematic - if the string is not found, strpos returns false, and false + 1 makes little sense.

However, it seems to me that the href always has to start with #, and so this can (and should) be rewritten:

  • At the start of the function, if $href does not start with #, return.
  • Otherwise, remove the first character, and use that for getElementById.

Sources: https://www.w3.org/TR/SVG11/struct.html#UseElement

if ($element === null) {
return;
}

TransformParser::parseTransformString($this->getAttribute('transform'), $rasterizer->pushTransform());

$element->rasterize($rasterizer);

$rasterizer->popTransform();
}
}
Loading