From e178e4df4a3fed2cb6808561b2e7f81058a564c1 Mon Sep 17 00:00:00 2001 From: Brandon Kelly Date: Sat, 12 Dec 2020 11:38:36 -0800 Subject: [PATCH] utils/svg action + namespace system SVG files Resolves #7247 --- CHANGELOG.md | 2 + .../controllers/utils/SvgController.php | 96 +++++++++++++++++++ src/icons/alert.svg | 2 +- src/icons/arrow-up.svg | 6 +- src/icons/book.svg | 2 +- src/icons/broken-image.svg | 14 +-- src/icons/bug.svg | 6 +- src/icons/bullhorn.svg | 2 +- src/icons/buoey.svg | 2 +- src/icons/c-debug.svg | 6 +- src/icons/c-outline.svg | 4 +- src/icons/check.svg | 6 +- src/icons/clock.svg | 6 +- src/icons/craft-partners.svg | 12 +-- src/icons/craftid.svg | 10 +- src/icons/database.svg | 6 +- src/icons/default-plugin.svg | 6 +- src/icons/envelope.svg | 6 +- src/icons/excite.svg | 6 +- src/icons/feed.svg | 6 +- src/icons/field.svg | 6 +- src/icons/file.svg | 10 +- src/icons/folder-open.svg | 6 +- src/icons/globe.svg | 6 +- src/icons/graphql.svg | 2 +- src/icons/hash.svg | 2 +- src/icons/info-circle.svg | 6 +- src/icons/magnifying-glass.svg | 2 +- src/icons/newspaper.svg | 6 +- src/icons/photo.svg | 6 +- src/icons/play.svg | 2 +- src/icons/plugin.svg | 6 +- src/icons/routes.svg | 6 +- src/icons/search.svg | 6 +- src/icons/sliders.svg | 6 +- src/icons/tags.svg | 6 +- src/icons/template.svg | 2 +- src/icons/tip.svg | 2 +- src/icons/tools.svg | 2 +- src/icons/trash.svg | 6 +- src/icons/user.svg | 2 +- src/icons/users.svg | 6 +- src/icons/wand.svg | 6 +- src/icons/world.svg | 6 +- 44 files changed, 210 insertions(+), 112 deletions(-) create mode 100644 src/console/controllers/utils/SvgController.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 87a47fbe219..957a24a92e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - Edit Section and Edit Entry Type pages now have a “Save and continue editing” Save menu option, and the Ctrl/Command + S 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)) @@ -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 diff --git a/src/console/controllers/utils/SvgController.php b/src/console/controllers/utils/SvgController.php new file mode 100644 index 00000000000..a875c922bce --- /dev/null +++ b/src/console/controllers/utils/SvgController.php @@ -0,0 +1,96 @@ + + * @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; + } +} diff --git a/src/icons/alert.svg b/src/icons/alert.svg index fb6e6fd47ae..ddbc06539df 100644 --- a/src/icons/alert.svg +++ b/src/icons/alert.svg @@ -1,6 +1,6 @@ - - - + + + \ No newline at end of file diff --git a/src/icons/book.svg b/src/icons/book.svg index 4f861691170..ea2e28c8a7d 100644 --- a/src/icons/book.svg +++ b/src/icons/book.svg @@ -1,6 +1,6 @@ - - - - - - - + + + + + + + \ No newline at end of file diff --git a/src/icons/bug.svg b/src/icons/bug.svg index ab7a6de7a2b..4b1f4ab81ce 100644 --- a/src/icons/bug.svg +++ b/src/icons/bug.svg @@ -6,11 +6,11 @@ bug Created with Sketch. - - + + id="xnrcdhcjdi-Shape"> \ No newline at end of file diff --git a/src/icons/bullhorn.svg b/src/icons/bullhorn.svg index efec6b3cdfe..a3dd85df032 100644 --- a/src/icons/bullhorn.svg +++ b/src/icons/bullhorn.svg @@ -1,6 +1,6 @@ - - - - diff --git a/src/icons/c-outline.svg b/src/icons/c-outline.svg index e165830fee3..5be41c740bc 100644 --- a/src/icons/c-outline.svg +++ b/src/icons/c-outline.svg @@ -2,7 +2,7 @@ Craft icon - - + + diff --git a/src/icons/check.svg b/src/icons/check.svg index 560c6017a6a..9db467b99d1 100644 --- a/src/icons/check.svg +++ b/src/icons/check.svg @@ -6,11 +6,11 @@ check Created with Sketch. - - + + id="kwgehxlcgw-Shape"> \ No newline at end of file diff --git a/src/icons/clock.svg b/src/icons/clock.svg index bbf9bbcc038..2fb1ec1e583 100644 --- a/src/icons/clock.svg +++ b/src/icons/clock.svg @@ -4,9 +4,9 @@ chat-bubbles Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/craft-partners.svg b/src/icons/craft-partners.svg index 002d8c6cf58..881278745bb 100644 --- a/src/icons/craft-partners.svg +++ b/src/icons/craft-partners.svg @@ -1,12 +1,12 @@ - - - - - + + + + + - + diff --git a/src/icons/craftid.svg b/src/icons/craftid.svg index ecfcbc1fcfb..edc9ea55d81 100644 --- a/src/icons/craftid.svg +++ b/src/icons/craftid.svg @@ -4,11 +4,11 @@ craftid* Created with Sketch. - - - - - + + + + + diff --git a/src/icons/database.svg b/src/icons/database.svg index 8f1b9a7ba45..a58d4722305 100644 --- a/src/icons/database.svg +++ b/src/icons/database.svg @@ -6,11 +6,11 @@ database Created with Sketch. - - + + id="qjtgsrwnfb-Shape"> \ No newline at end of file diff --git a/src/icons/default-plugin.svg b/src/icons/default-plugin.svg index 6342ca8d98f..6e901d9ea29 100644 --- a/src/icons/default-plugin.svg +++ b/src/icons/default-plugin.svg @@ -4,9 +4,9 @@ default_plugin Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/envelope.svg b/src/icons/envelope.svg index 266aca784f5..3dae15ab58d 100644 --- a/src/icons/envelope.svg +++ b/src/icons/envelope.svg @@ -4,9 +4,9 @@ envelope Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/excite.svg b/src/icons/excite.svg index a73ada1fb25..95e1f6abe6a 100644 --- a/src/icons/excite.svg +++ b/src/icons/excite.svg @@ -4,9 +4,9 @@ excite Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/feed.svg b/src/icons/feed.svg index 4821bcb9206..1b8182e1e73 100644 --- a/src/icons/feed.svg +++ b/src/icons/feed.svg @@ -4,9 +4,9 @@ feed Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/field.svg b/src/icons/field.svg index 32ddc1f8f32..ee3f2fe97c9 100644 --- a/src/icons/field.svg +++ b/src/icons/field.svg @@ -4,9 +4,9 @@ field Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/file.svg b/src/icons/file.svg index b88df8f13de..c6e0f1d01aa 100644 --- a/src/icons/file.svg +++ b/src/icons/file.svg @@ -1,13 +1,13 @@ - - - + + diff --git a/src/icons/folder-open.svg b/src/icons/folder-open.svg index 3ecbe7a40b6..41fc85df43e 100644 --- a/src/icons/folder-open.svg +++ b/src/icons/folder-open.svg @@ -4,9 +4,9 @@ folder-open Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/globe.svg b/src/icons/globe.svg index 9481bfabe76..b9ff26faf8b 100644 --- a/src/icons/globe.svg +++ b/src/icons/globe.svg @@ -4,9 +4,9 @@ globe Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/graphql.svg b/src/icons/graphql.svg index 6e8e0503748..e07d3eb7182 100644 --- a/src/icons/graphql.svg +++ b/src/icons/graphql.svg @@ -1,7 +1,7 @@ - \ No newline at end of file diff --git a/src/icons/magnifying-glass.svg b/src/icons/magnifying-glass.svg index ef2ec80e70a..d9af44aebd4 100644 --- a/src/icons/magnifying-glass.svg +++ b/src/icons/magnifying-glass.svg @@ -1,6 +1,6 @@ - - - + + + \ No newline at end of file diff --git a/src/icons/photo.svg b/src/icons/photo.svg index bdf9cb767e7..5d0458ec01a 100644 --- a/src/icons/photo.svg +++ b/src/icons/photo.svg @@ -4,9 +4,9 @@ photo Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/play.svg b/src/icons/play.svg index 6f2cd5aac7b..29a319d2039 100644 --- a/src/icons/play.svg +++ b/src/icons/play.svg @@ -1,6 +1,6 @@ - diff --git a/src/icons/plugin.svg b/src/icons/plugin.svg index cdfc6b06da4..423f2dac6eb 100644 --- a/src/icons/plugin.svg +++ b/src/icons/plugin.svg @@ -4,9 +4,9 @@ plugin Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/routes.svg b/src/icons/routes.svg index da102f8fe37..79643723a07 100644 --- a/src/icons/routes.svg +++ b/src/icons/routes.svg @@ -4,9 +4,9 @@ routes Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/search.svg b/src/icons/search.svg index 3abfb402310..57088def954 100644 --- a/src/icons/search.svg +++ b/src/icons/search.svg @@ -4,9 +4,9 @@ search Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/sliders.svg b/src/icons/sliders.svg index 1d9651820a1..301e7a142e7 100644 --- a/src/icons/sliders.svg +++ b/src/icons/sliders.svg @@ -4,9 +4,9 @@ sliders Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/tags.svg b/src/icons/tags.svg index 286e613efdd..e571c815b62 100644 --- a/src/icons/tags.svg +++ b/src/icons/tags.svg @@ -4,9 +4,9 @@ tags Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/template.svg b/src/icons/template.svg index 015677af341..862f21b6de5 100644 --- a/src/icons/template.svg +++ b/src/icons/template.svg @@ -1,6 +1,6 @@ - - \ No newline at end of file + \ No newline at end of file diff --git a/src/icons/trash.svg b/src/icons/trash.svg index b6c5819a81e..2df972badf8 100644 --- a/src/icons/trash.svg +++ b/src/icons/trash.svg @@ -4,9 +4,9 @@ trash Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/user.svg b/src/icons/user.svg index 6b463f598c9..6fd285f0201 100644 --- a/src/icons/user.svg +++ b/src/icons/user.svg @@ -1,6 +1,6 @@ - - - + + + \ No newline at end of file diff --git a/src/icons/wand.svg b/src/icons/wand.svg index 36c5253d294..dc5a5bb4560 100644 --- a/src/icons/wand.svg +++ b/src/icons/wand.svg @@ -4,9 +4,9 @@ search Created with Sketch. - - - + + + \ No newline at end of file diff --git a/src/icons/world.svg b/src/icons/world.svg index 62c3e3a4901..f9ced5b2513 100644 --- a/src/icons/world.svg +++ b/src/icons/world.svg @@ -4,9 +4,9 @@ world Created with Sketch. - - - + + +