changed embedSVG to inlineSVG and make it return contents

This makes it more flexible to use on the expense of needing one echo
more.
This commit is contained in:
Andreas Gohr 2017-02-05 04:42:37 +01:00
parent 70635395b7
commit 71de557260
3 changed files with 9 additions and 14 deletions

View File

@ -14,10 +14,7 @@ class common_embedSVG_test extends DokuWikiTest {
'6.41-3.205 25.59 12.795zM57.59 40.795l6.41 3.205-32 16-32-16 6.41-3.205 25.59 12.795z" '.
'fill="#000000"></path></svg>';
ob_start();
$this->assertTrue(embedSVG($file));
$svg = ob_get_clean();
$this->assertEquals($clean, $svg);
$this->assertEquals($clean, inlinSVG($file));
}
/**
@ -25,10 +22,7 @@ class common_embedSVG_test extends DokuWikiTest {
*/
function test_fail() {
$file = mediaFN('wiki:test.svg');
ob_start();
$this->assertFalse(embedSVG($file, 100));
$svg = ob_get_clean();
$this->assertEquals('', $svg);
$this->assertFalse(inlinSVG($file, 100));
}
}

View File

@ -119,7 +119,7 @@ class Admin extends Ui {
echo '<li><div class="li">';
echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
echo '<span class="icon">';
embedSVG($item['icon']);
echo inlinSVG($item['icon']);
echo '</span>';
echo '<span class="prompt">';
echo $item['prompt'];

View File

@ -2014,17 +2014,19 @@ function stripsourcemaps(&$text){
}
/**
* Embeds the contents of a given SVG file in the current context
* Returns the contents of a given SVG file for embedding
*
* Inlining SVGs saves on HTTP requests and more importantly allows for styling them through
* CSS. However it should used with small SVGs only. The $maxsize setting ensures only small
* files are embedded.
*
* This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG!
*
* @param string $file full path to the SVG file
* @param int $maxsize maximum allowed size for the SVG to be embedded
* @return bool true if the file was embedded, false otherwise
* @return string|false the SVG content, false if the file couldn't be loaded
*/
function embedSVG($file, $maxsize = 2048) {
function inlinSVG($file, $maxsize = 2048) {
$file = trim($file);
if($file === '') return false;
if(!file_exists($file)) return false;
@ -2037,8 +2039,7 @@ function embedSVG($file, $maxsize = 2048) {
$content = preg_replace('/>\s+</s', '><', $content); // newlines between tags
$content = trim($content);
if(substr($content, 0, 5) !== '<svg ') return false;
echo $content;
return true;
return $content;
}
//Setup VIM: ex: et ts=2 :