Skip to content

Commit

Permalink
changed embedSVG to inlineSVG and make it return contents
Browse files Browse the repository at this point in the history
This makes it more flexible to use on the expense of needing one echo
more.
  • Loading branch information
splitbrain committed Feb 5, 2017
1 parent 7063539 commit 71de557
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
Expand Up @@ -14,21 +14,15 @@ function test_success() {
'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));
}

/**
* embed should fail because of the file size limit
*/
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));
}

}
2 changes: 1 addition & 1 deletion inc/Ui/Admin.php
Expand Up @@ -119,7 +119,7 @@ protected function showMenuItem($item) {
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'];
Expand Down
11 changes: 6 additions & 5 deletions inc/common.php
Expand Up @@ -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;
Expand All @@ -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 :

0 comments on commit 71de557

Please sign in to comment.