[BUGFIX] Respect width/height settings in renderMode inline 13/53713/3
authorFrank Naegler <frank.naegler@typo3.org>
Wed, 16 Aug 2017 10:15:08 +0000 (12:15 +0200)
committerAndreas Fernandez <typo3@scripting-base.de>
Wed, 16 Aug 2017 13:10:53 +0000 (15:10 +0200)
The settings width and height are now supported also for the
renderMode inline of the SVG content object.

Resolves: #82111
Releases: master
Change-Id: I793579256a5a7d109c216cf6b2ff7be4531dfd02
Reviewed-on: https://review.typo3.org/53713
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/frontend/Classes/ContentObject/ScalableVectorGraphicsContentObject.php

index 28dde77..5f068d5 100644 (file)
@@ -49,21 +49,37 @@ class ScalableVectorGraphicsContentObject extends AbstractContentObject
     protected function renderInline(array $conf) : string
     {
         $src = $this->resolveAbsoluteSourcePath($conf);
+        list($width, $height, $isDefaultWidth, $isDefaultHeight) = $this->getDimensions($conf);
 
-        if (!file_exists($src)) {
-            return '';
-        }
-
-        $svgContent = file_get_contents($src);
-        $svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
-        // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
-        $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
-        $svgElement = simplexml_load_string($svgContent);
-        libxml_disable_entity_loader($previousValueOfEntityLoader);
+        $content = '';
+        if (file_exists($src)) {
+            $svgContent = file_get_contents($src);
+            $svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
+            // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
+            $previousValueOfEntityLoader = libxml_disable_entity_loader();
+            $svgElement = simplexml_load_string($svgContent);
+            libxml_disable_entity_loader($previousValueOfEntityLoader);
 
-        // remove xml version tag
-        $domXml = dom_import_simplexml($svgElement);
-        return $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
+            $domXml = dom_import_simplexml($svgElement);
+            if (!$isDefaultWidth) {
+                $domXml->setAttribute('width', $width);
+            }
+            if (!$isDefaultHeight) {
+                $domXml->setAttribute('height', $height);
+            }
+            // remove xml version tag
+            $content = $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
+        } else {
+            $value = isset($conf['value.']) ? $this->cObj->stdWrap($conf['value'], $conf['value.']) : $conf['value'];
+            if (!empty($value)) {
+                $content = [];
+                $content[] = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . (int)$width . '" height="' . (int)$height . '">';
+                $content[] = $value;
+                $content[] = '</svg>';
+                $content = implode(LF, $content);
+            }
+        }
+        return $content;
     }
 
     /**
@@ -74,15 +90,9 @@ class ScalableVectorGraphicsContentObject extends AbstractContentObject
      */
     protected function renderObject(array $conf) : string
     {
-        $width = isset($conf['width.']) ? $this->cObj->stdWrap($conf['width'], $conf['width.']) : $conf['width'];
-        if (!$width) {
-            $width = 600;
-        }
-        $height = isset($conf['height.']) ? $this->cObj->stdWrap($conf['height'], $conf['height.']) : $conf['height'];
-        if (!$height) {
-            $height = 400;
-        }
         $src = $this->resolveAbsoluteSourcePath($conf);
+        list($width, $height) = $this->getDimensions($conf);
+
         $src = $src === '' ? null : PathUtility::getAbsoluteWebPath($src);
 
         $value = isset($conf['value.']) ? $this->cObj->stdWrap($conf['value'], $conf['value.']) : $conf['value'];
@@ -125,4 +135,28 @@ class ScalableVectorGraphicsContentObject extends AbstractContentObject
         $src = isset($conf['src.']) ? $this->cObj->stdWrap($conf['src'], $conf['src.']) : $conf['src'];
         return GeneralUtility::getFileAbsFileName($src);
     }
+
+    /**
+     * @param array $conf
+     *
+     * @return array
+     */
+    protected function getDimensions(array $conf) : array
+    {
+        $isDefaultWidth = false;
+        $isDefaultHeight = false;
+        $width = isset($conf['width.']) ? $this->cObj->stdWrap($conf['width'], $conf['width.']) : $conf['width'];
+        $height = isset($conf['height.']) ? $this->cObj->stdWrap($conf['height'], $conf['height.']) : $conf['height'];
+
+        if (empty($width)) {
+            $isDefaultWidth = true;
+            $width = 600;
+        }
+        if (empty($height)) {
+            $isDefaultHeight = true;
+            $height = 400;
+        }
+
+        return [$width, $height, $isDefaultWidth, $isDefaultHeight];
+    }
 }