Skip to content

Commit

Permalink
utils/svg action + namespace system SVG files
Browse files Browse the repository at this point in the history
Resolves #7247
  • Loading branch information
brandonkelly committed Dec 12, 2020
1 parent af35fd9 commit e178e4d
Show file tree
Hide file tree
Showing 44 changed files with 210 additions and 112 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Added
- Edit Section and Edit Entry Type pages now have a “Save and continue editing” Save menu option, and the <kbd>Ctrl</kbd>/<kbd>Command</kbd> + <kbd>S</kbd> keyboard shortcut is now assigned to that. ([#2872](https://github.com/craftcms/cms/issues/2872))
- Added the `fullSchema` option to the `graphql/dump-schema` and `graphql/print-schema` commands. ([#7226](https://github.com/craftcms/cms/issues/7226))
- Added the `utils/svg` command, which can sanitize and/or namespace a batch of SVG files.

### Changed
- The Username, First Name, Last Name, Email, and New Password fields on Edit User pages now prevent LastPass from autofilling them. ([#7177](https://github.com/craftcms/cms/issues/7177))
Expand Down Expand Up @@ -33,6 +34,7 @@
- Fixed a bug where eager-loading would load incorrect elements in some cases when using GraphQL API.
- Fixed a bug where select inputs could bleed out of their container divs. ([#7183](https://github.com/craftcms/cms/issues/7183))
- Fixed a bug where Edit Entry pages would show “Save and add another” and “Save as a new entry” action options for users who didn’t have permission to create new entries in the section. ([#7232](https://github.com/craftcms/cms/issues/7232))
- Fixed a bug where the control panel SVG icons had conflicting IDs. ([#7247](https://github.com/craftcms/cms/pull/7247))

## 3.5.16 - 2020-11-24

Expand Down
96 changes: 96 additions & 0 deletions src/console/controllers/utils/SvgController.php
@@ -0,0 +1,96 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\console\controllers\utils;

use Craft;
use craft\console\Controller;
use craft\db\Table;
use craft\helpers\Console;
use craft\helpers\FileHelper;
use craft\helpers\Html;
use craft\helpers\StringHelper;
use yii\console\ExitCode;
use yii\db\Expression;

/**
* Processes SVG files.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.5.17
*/
class SvgController extends Controller
{
/**
* @var bool Whether the SVGs should be sanitized
*/
public $sanitize = false;

/**
* @var bool Whether `id` and other attributes should be namespaced
*/
public $namespace = false;

/**
* @inheritdoc
*/
public function options($actionID)
{
$actions = parent::options($actionID);
if ($actionID === 'index') {
$actions[] = 'sanitize';
$actions[] = 'namespace';
}
return $actions;
}

/**
* Processes SVG files.
*
* @param string $pattern The glob pattern to match SVG files.
* @return int
*/
public function actionIndex(string $pattern): int
{
$paths = glob($pattern);

foreach ($paths as $path) {
if (is_dir($path)) {
$files = FileHelper::findFiles($path, [
'filter' => function(string $path): bool {
return is_dir($path) || FileHelper::isSvg($path);
},
]);
} else {
$files = [$path];
}

foreach ($files as $file) {
$this->stdout('- Processing ');
$this->stdout($file, Console::FG_CYAN);
$this->stdout(' ... ');

$svg = file_get_contents($file);

if ($this->sanitize) {
$svg = Html::sanitizeSvg($svg);
}

// Namespace class names and IDs
if ($this->namespace) {
$ns = StringHelper::randomString(10);
$svg = Html::namespaceAttributes($svg, $ns, true);
}

FileHelper::writeToFile($file, $svg);
$this->stdout("done\n", Console::FG_GREEN);
}
}

return ExitCode::OK;
}
}
2 changes: 1 addition & 1 deletion src/icons/alert.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/icons/arrow-up.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/icons/book.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions src/icons/broken-image.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/icons/bug.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/icons/bullhorn.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/icons/buoey.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/icons/c-debug.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/icons/c-outline.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/icons/check.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e178e4d

Please sign in to comment.