Skip to content

Commit

Permalink
more cleanup in embedSVG
Browse files Browse the repository at this point in the history
now comments and line breaks between tags are removed
  • Loading branch information
splitbrain committed Jan 31, 2017
1 parent eb8a855 commit 0849fa8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
11 changes: 11 additions & 0 deletions _test/data/media/wiki/test.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions _test/tests/inc/common_embedSVG.test.php
@@ -0,0 +1,34 @@
<?php

class common_embedSVG_test extends DokuWikiTest {

/**
* embed should succeed with a cleaned up result
*/
function test_success() {
$file = mediaFN('wiki:test.svg');
$clean =
'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" '.
'width="64" height="64" viewBox="0 0 64 64"><path d="M64 20l-32-16-32 16 32 16 32-16zM32 '.
'9.311l21.379 10.689-21.379 10.689-21.379-10.689 21.379-10.689zM57.59 28.795l6.41 3.205-32 16-32-16 '.
'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);
}

/**
* 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);
}

}
6 changes: 4 additions & 2 deletions inc/common.php
Expand Up @@ -2031,8 +2031,10 @@ function embedSVG($file, $maxsize = 2048) {
if(filesize($file) > $maxsize) return false;
if(!is_readable($file)) return false;
$content = file_get_contents($file);
$content = preg_replace('/<\?xml .*?\?>/i', '', $content);
$content = preg_replace('/<!DOCTYPE .*?>/i', '', $content);
$content = preg_replace('/<!--.*?(-->)/s','', $content); // comments
$content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header
$content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type
$content = preg_replace('/>\s+</s', '><', $content); // newlines between tags
$content = trim($content);
if(substr($content, 0, 5) !== '<svg ') return false;
echo $content;
Expand Down

0 comments on commit 0849fa8

Please sign in to comment.