Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validator for SVGs #17753

Merged
merged 6 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion scripts/clean-up-svg-icons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const globPattern = paths.map( pathToIcon => {

let statusCode = 0;

const missingViewBoxIcons = [];

globSync( globPattern )
.map( upath.toUnix )
.filter( filterExcludedIcons )
Expand All @@ -75,6 +77,15 @@ if ( verifyOnly && statusCode ) {
process.exit( statusCode );
}

if ( missingViewBoxIcons.length ) {
console.log();
console.log( chalk.red( 'Following icons have missing or invalid ViewBox:' ) );
console.log( missingViewBoxIcons.map( path => ` - ${ path }` ).join( '\n' ) );
console.log();

process.exit( statusCode );
}

function parseArguments( args ) {
const config = {
boolean: [
Expand Down Expand Up @@ -120,7 +131,19 @@ function processIcon( pathToIcon ) {
svgoOptions.push( '-o -' );
}

const result = execSync( `svgo ${ svgoOptions.join( ' ' ) }`, { encoding: 'utf-8' } ).trim();
let result;

try {
result = execSync( `svgo ${ svgoOptions.join( ' ' ) }`, { encoding: 'utf-8', stdio: 'pipe' } ).trim();
} catch ( err ) {
if ( err.message.includes( 'Error: Invalid or missing viewBox.' ) ) {
missingViewBoxIcons.push( pathToIcon );

statusCode = 1;
} else {
throw new Error( err );
}
}

if ( verifyOnly ) {
const iconFile = fs.readFileSync( pathToIcon, 'utf-8' ).trim();
Expand Down
5 changes: 4 additions & 1 deletion scripts/svgo.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/

const svgoViewBoxPlugin = require( './utils/svgo-viewBox-plugin.cjs' );

/* eslint-env node */

module.exports = {
Expand All @@ -28,6 +30,7 @@ module.exports = {
},
{ name: 'removeTitle' },
{ name: 'removeComments' },
{ name: 'removeMetadata' }
{ name: 'removeMetadata' },
svgoViewBoxPlugin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works as expected.

image

However, it turns out that the commercial repository has not executed this validator.

Fortunately, icons will land in a single repository soon so that we can deal with it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to reproduce this issue, could you provide reproduction steps?

]
};
46 changes: 46 additions & 0 deletions scripts/utils/svgo-viewBox-plugin.cjs
przemyslaw-zan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/

/* global module */

module.exports = {
name: 'svgoViewBoxPlugin',
description: 'Plugin that ensures that every SVG has defined valid viewBox.',
type: 'visitor',
active: true,
fn: () => ( {
element: {
enter: ( node, parentNode ) => {
if ( parentNode.type !== 'root' ) {
return;
}

if ( node.name !== 'svg' ) {
return;
}

if ( isViewBoxValid( node ) ) {
return;
}

throw new Error( 'Invalid or missing viewBox.' );
}
}
} )
};

function isViewBoxValid( node ) {
if ( !node.attributes ) {
return false;
}

if ( !node.attributes.viewBox ) {
return false;
}

const viewBoxPattern = /^\d+ \d+ \d+ \d+$/;

return viewBoxPattern.test( node.attributes.viewBox );
}
Loading