summaryrefslogtreecommitdiffstats
path: root/lib/private/Template
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2018-10-25 11:57:27 +0200
committerJulius Härtl <jus@bitgrid.net>2018-10-25 12:16:10 +0200
commitd21ded67a70672cfa56a48051fa752b431118604 (patch)
tree5f29256f45dbb761f5cc1ea80dd7d9802114196b /lib/private/Template
parent0b2ef7e60873fbb2036a85a934bf1af23ebb5bc9 (diff)
downloadnextcloud-server-d21ded67a70672cfa56a48051fa752b431118604.tar.gz
nextcloud-server-d21ded67a70672cfa56a48051fa752b431118604.zip
Keep list of icons in a separate file for use in the accessibility app
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private/Template')
-rw-r--r--lib/private/Template/IconsCacher.php134
1 files changed, 85 insertions, 49 deletions
diff --git a/lib/private/Template/IconsCacher.php b/lib/private/Template/IconsCacher.php
index aedda0e9c91..c1a78a567f9 100644
--- a/lib/private/Template/IconsCacher.php
+++ b/lib/private/Template/IconsCacher.php
@@ -52,10 +52,13 @@ class IconsCacher {
/** @var string */
private $fileName = 'icons-vars.css';
+ private $iconList = 'icons-list.template';
+
/**
* @param ILogger $logger
* @param Factory $appDataFactory
* @param IURLGenerator $urlGenerator
+ * @throws \OCP\Files\NotPermittedException
*/
public function __construct(ILogger $logger,
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);
$icons = [];
foreach ($matches as $icon) {
@@ -80,80 +83,101 @@ class IconsCacher {
return $icons;
}
+
/**
- * Parse and cache css
- *
* @param string $css
+ * @return string
+ * @throws NotFoundException
+ * @throws \OCP\Files\NotPermittedException
*/
- public function setIconsCss(string $css) {
+ public function setIconsCss(string $css): string {
- $cachedFile = $this->getCachedCSS();
+ $cachedFile = $this->getCachedList();
if (!$cachedFile) {
$currentData = '';
+ $cachedFile = $this->folder->newFile($this->iconList);
} else {
$currentData = $cachedFile->getContent();
}
- // remove :root
- $currentData = str_replace([':root {', '}'], '', $currentData);
+ $cachedVarsCssFile = $this->getCachedCSS();
+ if (!$cachedVarsCssFile) {
+ $cachedVarsCssFile = $this->folder->newFile($this->fileName);
+ }
$icons = $this->getIconsFromCss($currentData . $css);
$data = '';
+ $list = '';
foreach ($icons as $icon => $url) {
- $base = $this->getRoutePrefix() . '/svg/';
- $svg = false;
- if (strpos($url, $base . 'core') === 0) {
- $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);
- }
+ $list .= "--$icon: url('$url');";
+ list($location,$color) = $this->parseUrl($url);
+ $svg = file_get_contents($location);
if ($svg === false) {
$this->logger->debug('Failed to get icon file ' . $location);
$data .= "--$icon: url('$url');";
continue;
}
- // TODO: Copied from SvgController (we should put this into a separate method so the controller can use it as well)
- // 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);
+ $encode = base64_encode($this->colorizeSvg($svg, $color));
+ $data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
+ }
- $encode = base64_encode($svg);
- $data .= "--$icon: url(data:image/svg+xml;base64,$encode);";
+ if (\strlen($data) > 0 && \strlen($list) > 0) {
+ $data = ":root {\n$data\n}";
+ $cachedVarsCssFile->putContent($data);
+ $list = ":root {\n$list\n}";
+ $cachedFile->putContent($list);
}
- if (strlen($data) > 0) {
- if (!$cachedFile) {
- $cachedFile = $this->folder->newFile($this->fileName);
- }
+ return preg_replace($this->iconVarRE, '', $css);
+ }
- $data = ":root {
- $data
- }";
- $cachedFile->putContent($data);
+ /**
+ * @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
+ ];
+ }
- return preg_replace($this->iconVarRE, '', $css);
+ /**
+ * @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() {
@@ -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() {
// Only inject once
foreach (\OC_Util::$headers as $header) {