Keep list of icons in a separate file for use in the accessibility app
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
parent
0b2ef7e608
commit
d21ded67a7
|
@ -166,8 +166,8 @@ class AccessibilityController extends Controller {
|
||||||
$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
|
$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
|
||||||
$css = $this->rebaseUrls($css, $appWebRoot . '/css');
|
$css = $this->rebaseUrls($css, $appWebRoot . '/css');
|
||||||
|
|
||||||
if (in_array('themedark', $userValues) && $this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) {
|
if (in_array('themedark', $userValues) && $this->iconsCacher->getCachedList() && $this->iconsCacher->getCachedList()->getSize() > 0) {
|
||||||
$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedCSS()->getContent());
|
$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedList()->getContent());
|
||||||
$css = $css . $iconsCss;
|
$css = $css . $iconsCss;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ use OCP\AppFramework\Http\NotFoundResponse;
|
||||||
use OCP\AppFramework\Utility\ITimeFactory;
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
use OCP\App\IAppManager;
|
use OCP\App\IAppManager;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
use OC\Template\IconsCacher;
|
||||||
|
|
||||||
class SvgController extends Controller {
|
class SvgController extends Controller {
|
||||||
|
|
||||||
|
@ -43,15 +44,20 @@ class SvgController extends Controller {
|
||||||
/** @var IAppManager */
|
/** @var IAppManager */
|
||||||
protected $appManager;
|
protected $appManager;
|
||||||
|
|
||||||
|
/** @var IconsCacher */
|
||||||
|
private $iconsCacher;
|
||||||
|
|
||||||
public function __construct(string $appName,
|
public function __construct(string $appName,
|
||||||
IRequest $request,
|
IRequest $request,
|
||||||
ITimeFactory $timeFactory,
|
ITimeFactory $timeFactory,
|
||||||
IAppManager $appManager) {
|
IAppManager $appManager,
|
||||||
|
IconsCacher $iconsCacher) {
|
||||||
parent::__construct($appName, $request);
|
parent::__construct($appName, $request);
|
||||||
|
|
||||||
$this->serverRoot = \OC::$SERVERROOT;
|
$this->serverRoot = \OC::$SERVERROOT;
|
||||||
$this->timeFactory = $timeFactory;
|
$this->timeFactory = $timeFactory;
|
||||||
$this->appManager = $appManager;
|
$this->appManager = $appManager;
|
||||||
|
$this->iconsCacher = $iconsCacher;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -114,17 +120,11 @@ class SvgController extends Controller {
|
||||||
|
|
||||||
$svg = file_get_contents($path);
|
$svg = file_get_contents($path);
|
||||||
|
|
||||||
if (is_null($svg)) {
|
if ($svg === null) {
|
||||||
return new NotFoundResponse();
|
return new NotFoundResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
// add fill (fill is not present on black elements)
|
$svg = $this->iconsCacher->colorizeSvg($svg, $color);
|
||||||
$fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi';
|
|
||||||
$svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
|
|
||||||
|
|
||||||
// replace any fill or stroke colors
|
|
||||||
$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
|
|
||||||
$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
|
|
||||||
|
|
||||||
$response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
$response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
||||||
|
|
||||||
|
|
|
@ -52,10 +52,13 @@ class IconsCacher {
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $fileName = 'icons-vars.css';
|
private $fileName = 'icons-vars.css';
|
||||||
|
|
||||||
|
private $iconList = 'icons-list.template';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ILogger $logger
|
* @param ILogger $logger
|
||||||
* @param Factory $appDataFactory
|
* @param Factory $appDataFactory
|
||||||
* @param IURLGenerator $urlGenerator
|
* @param IURLGenerator $urlGenerator
|
||||||
|
* @throws \OCP\Files\NotPermittedException
|
||||||
*/
|
*/
|
||||||
public function __construct(ILogger $logger,
|
public function __construct(ILogger $logger,
|
||||||
Factory $appDataFactory,
|
Factory $appDataFactory,
|
||||||
|
@ -71,7 +74,7 @@ class IconsCacher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getIconsFromCss(string $css): array{
|
private function getIconsFromCss(string $css): array {
|
||||||
preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
|
preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
|
||||||
$icons = [];
|
$icons = [];
|
||||||
foreach ($matches as $icon) {
|
foreach ($matches as $icon) {
|
||||||
|
@ -80,82 +83,103 @@ class IconsCacher {
|
||||||
|
|
||||||
return $icons;
|
return $icons;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Parse and cache css
|
|
||||||
*
|
|
||||||
* @param string $css
|
|
||||||
*/
|
|
||||||
public function setIconsCss(string $css) {
|
|
||||||
|
|
||||||
$cachedFile = $this->getCachedCSS();
|
/**
|
||||||
|
* @param string $css
|
||||||
|
* @return string
|
||||||
|
* @throws NotFoundException
|
||||||
|
* @throws \OCP\Files\NotPermittedException
|
||||||
|
*/
|
||||||
|
public function setIconsCss(string $css): string {
|
||||||
|
|
||||||
|
$cachedFile = $this->getCachedList();
|
||||||
if (!$cachedFile) {
|
if (!$cachedFile) {
|
||||||
$currentData = '';
|
$currentData = '';
|
||||||
|
$cachedFile = $this->folder->newFile($this->iconList);
|
||||||
} else {
|
} else {
|
||||||
$currentData = $cachedFile->getContent();
|
$currentData = $cachedFile->getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove :root
|
$cachedVarsCssFile = $this->getCachedCSS();
|
||||||
$currentData = str_replace([':root {', '}'], '', $currentData);
|
if (!$cachedVarsCssFile) {
|
||||||
|
$cachedVarsCssFile = $this->folder->newFile($this->fileName);
|
||||||
|
}
|
||||||
|
|
||||||
$icons = $this->getIconsFromCss($currentData . $css);
|
$icons = $this->getIconsFromCss($currentData . $css);
|
||||||
|
|
||||||
$data = '';
|
$data = '';
|
||||||
|
$list = '';
|
||||||
foreach ($icons as $icon => $url) {
|
foreach ($icons as $icon => $url) {
|
||||||
$base = $this->getRoutePrefix() . '/svg/';
|
$list .= "--$icon: url('$url');";
|
||||||
$svg = false;
|
list($location,$color) = $this->parseUrl($url);
|
||||||
if (strpos($url, $base . 'core') === 0) {
|
$svg = file_get_contents($location);
|
||||||
$cleanUrl = substr($url, strlen($base.'core'));
|
|
||||||
$cleanUrl = substr($cleanUrl, 0, strpos($cleanUrl, '?'));
|
|
||||||
$parts = explode('/', $cleanUrl);
|
|
||||||
$color = array_pop($parts);
|
|
||||||
$cleanUrl = implode('/', $parts);
|
|
||||||
$location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
|
|
||||||
$svg = file_get_contents($location);
|
|
||||||
} elseif (strpos($url, $base) === 0) {
|
|
||||||
$cleanUrl = substr($url, strlen($base));
|
|
||||||
$cleanUrl = substr($cleanUrl, 0, strpos($cleanUrl, '?'));
|
|
||||||
$parts = explode('/', $cleanUrl);
|
|
||||||
$app = array_shift($parts);
|
|
||||||
$color = array_pop($parts);
|
|
||||||
$cleanUrl = implode('/', $parts);
|
|
||||||
$location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
|
|
||||||
if ($app === 'settings') {
|
|
||||||
$location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
|
|
||||||
}
|
|
||||||
$svg = file_get_contents($location);
|
|
||||||
}
|
|
||||||
if ($svg === false) {
|
if ($svg === false) {
|
||||||
$this->logger->debug('Failed to get icon file ' . $location);
|
$this->logger->debug('Failed to get icon file ' . $location);
|
||||||
$data .= "--$icon: url('$url');";
|
$data .= "--$icon: url('$url');";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// TODO: Copied from SvgController (we should put this into a separate method so the controller can use it as well)
|
$encode = base64_encode($this->colorizeSvg($svg, $color));
|
||||||
// add fill (fill is not present on black elements)
|
$data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
|
||||||
$fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi';
|
|
||||||
$svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
|
|
||||||
|
|
||||||
// replace any fill or stroke colors
|
|
||||||
$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
|
|
||||||
$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
|
|
||||||
|
|
||||||
$encode = base64_encode($svg);
|
|
||||||
$data .= "--$icon: url(data:image/svg+xml;base64,$encode);";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen($data) > 0) {
|
if (\strlen($data) > 0 && \strlen($list) > 0) {
|
||||||
if (!$cachedFile) {
|
$data = ":root {\n$data\n}";
|
||||||
$cachedFile = $this->folder->newFile($this->fileName);
|
$cachedVarsCssFile->putContent($data);
|
||||||
}
|
$list = ":root {\n$list\n}";
|
||||||
|
$cachedFile->putContent($list);
|
||||||
$data = ":root {
|
|
||||||
$data
|
|
||||||
}";
|
|
||||||
$cachedFile->putContent($data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return preg_replace($this->iconVarRE, '', $css);
|
return preg_replace($this->iconVarRE, '', $css);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $url
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function parseUrl($url): array {
|
||||||
|
$location = '';
|
||||||
|
$color = '';
|
||||||
|
$base = $this->getRoutePrefix() . '/svg/';
|
||||||
|
$cleanUrl = \substr($url, \strlen($base));
|
||||||
|
if (\strpos($url, $base . 'core') === 0) {
|
||||||
|
$cleanUrl = \substr($cleanUrl, \strlen('core'), \strpos($cleanUrl, '?')-\strlen('core'));
|
||||||
|
$parts = \explode('/', $cleanUrl);
|
||||||
|
$color = \array_pop($parts);
|
||||||
|
$cleanUrl = \implode('/', $parts);
|
||||||
|
$location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
|
||||||
|
} elseif (\strpos($url, $base) === 0) {
|
||||||
|
$cleanUrl = \substr($cleanUrl, 0, \strpos($cleanUrl, '?'));
|
||||||
|
$parts = \explode('/', $cleanUrl);
|
||||||
|
$app = \array_shift($parts);
|
||||||
|
$color = \array_pop($parts);
|
||||||
|
$cleanUrl = \implode('/', $parts);
|
||||||
|
$location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
|
||||||
|
if ($app === 'settings') {
|
||||||
|
$location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
$location,
|
||||||
|
$color
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $svg
|
||||||
|
* @param $color
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function colorizeSvg($svg, $color): string {
|
||||||
|
// add fill (fill is not present on black elements)
|
||||||
|
$fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi';
|
||||||
|
$svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
|
||||||
|
|
||||||
|
// replace any fill or stroke colors
|
||||||
|
$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
|
||||||
|
$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
|
||||||
|
return $svg;
|
||||||
|
}
|
||||||
|
|
||||||
private function getRoutePrefix() {
|
private function getRoutePrefix() {
|
||||||
$frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
|
$frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
|
||||||
$prefix = \OC::$WEBROOT . '/index.php';
|
$prefix = \OC::$WEBROOT . '/index.php';
|
||||||
|
@ -177,6 +201,18 @@ class IconsCacher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get icon-vars list template
|
||||||
|
* @return ISimpleFile|boolean
|
||||||
|
*/
|
||||||
|
public function getCachedList() {
|
||||||
|
try {
|
||||||
|
return $this->folder->getFile($this->iconList);
|
||||||
|
} catch (NotFoundException $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function injectCss() {
|
public function injectCss() {
|
||||||
// Only inject once
|
// Only inject once
|
||||||
foreach (\OC_Util::$headers as $header) {
|
foreach (\OC_Util::$headers as $header) {
|
||||||
|
|
|
@ -26,6 +26,7 @@ namespace Tests\Core\Controller;
|
||||||
|
|
||||||
use OC\AppFramework\Http;
|
use OC\AppFramework\Http;
|
||||||
use OC\Core\Controller\SvgController;
|
use OC\Core\Controller\SvgController;
|
||||||
|
use OC\Template\IconsCacher;
|
||||||
use OCP\App\IAppManager;
|
use OCP\App\IAppManager;
|
||||||
use OCP\AppFramework\Utility\ITimeFactory;
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
@ -89,7 +90,8 @@ class SvgControllerTest extends TestCase {
|
||||||
$request = $this->getMockBuilder(IRequest::class)->getMock();
|
$request = $this->getMockBuilder(IRequest::class)->getMock();
|
||||||
$timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
|
$timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
|
||||||
$appManager = $this->getMockBuilder(IAppManager::class)->getMock();
|
$appManager = $this->getMockBuilder(IAppManager::class)->getMock();
|
||||||
$this->svgController = new SvgController('core', $request, $timeFactory, $appManager);
|
$iconsCacher = $this->getMockBuilder(IconsCacher::class)->disableOriginalConstructor()->setMethods(['__construct'])->getMock();
|
||||||
|
$this->svgController = new SvgController('core', $request, $timeFactory, $appManager, $iconsCacher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -104,7 +104,7 @@ class IconsCacherTest extends \Test\TestCase {
|
||||||
public function testSetIconsFromValidCss() {
|
public function testSetIconsFromValidCss() {
|
||||||
$css = "
|
$css = "
|
||||||
icon.test {
|
icon.test {
|
||||||
--icon-test: url('/svg/core/actions/add/000?v=1');
|
--icon-test: url('/index.php/svg/core/actions/add/000?v=1');
|
||||||
background-image: var(--icon-test);
|
background-image: var(--icon-test);
|
||||||
}
|
}
|
||||||
";
|
";
|
||||||
|
@ -116,10 +116,10 @@ class IconsCacherTest extends \Test\TestCase {
|
||||||
";
|
";
|
||||||
|
|
||||||
$iconsFile = $this->createMock(ISimpleFile::class);
|
$iconsFile = $this->createMock(ISimpleFile::class);
|
||||||
$this->folder->expects($this->once())
|
$this->folder->expects($this->exactly(2))
|
||||||
->method('getFile')
|
->method('getFile')
|
||||||
->willReturn($iconsFile);
|
->willReturn($iconsFile);
|
||||||
|
|
||||||
$actual = $this->iconsCacher->setIconsCss($css);
|
$actual = $this->iconsCacher->setIconsCss($css);
|
||||||
$this->assertEquals($expected, $actual);
|
$this->assertEquals($expected, $actual);
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ class IconsCacherTest extends \Test\TestCase {
|
||||||
public function testSetIconsFromValidCssMultipleTimes() {
|
public function testSetIconsFromValidCssMultipleTimes() {
|
||||||
$css = "
|
$css = "
|
||||||
icon.test {
|
icon.test {
|
||||||
--icon-test: url('/svg/core/actions/add/000?v=1');
|
--icon-test: url('/index.php/svg/core/actions/add/000?v=1');
|
||||||
background-image: var(--icon-test);
|
background-image: var(--icon-test);
|
||||||
}
|
}
|
||||||
";
|
";
|
||||||
|
@ -139,14 +139,14 @@ class IconsCacherTest extends \Test\TestCase {
|
||||||
";
|
";
|
||||||
|
|
||||||
$iconsFile = $this->createMock(ISimpleFile::class);
|
$iconsFile = $this->createMock(ISimpleFile::class);
|
||||||
$this->folder->expects($this->exactly(3))
|
$this->folder->expects($this->exactly(6))
|
||||||
->method('getFile')
|
->method('getFile')
|
||||||
->willReturn($iconsFile);
|
->willReturn($iconsFile);
|
||||||
|
|
||||||
$actual = $this->iconsCacher->setIconsCss($css);
|
$actual = $this->iconsCacher->setIconsCss($css);
|
||||||
$actual = $this->iconsCacher->setIconsCss($actual);
|
$actual = $this->iconsCacher->setIconsCss($actual);
|
||||||
$actual = $this->iconsCacher->setIconsCss($actual);
|
$actual = $this->iconsCacher->setIconsCss($actual);
|
||||||
$this->assertEquals($expected, $actual);
|
$this->assertEquals($expected, $actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue