Repository navigation
Icons: Normalize SVG icons on commit and validate them in the build - #86657
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: 0 B Total Size: 8.66 MB
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. should apply a named font size using the font size buttons in
|
| @@ -1,3 +1,3 @@ | |||
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="fill: none" stroke="currentColor" stroke-width="1.5"> | |||
| <rect x="5" y="5" width="14" height="14" rx="2" stroke="currentColor" /> | |||
| <rect x="5" y="5" width="14" height="14" rx="2" /> | |||
There was a problem hiding this comment.
This shape repeated the root's stroke="currentColor", so the script removed the redundant attribute. The icon looks the same.
There was a problem hiding this comment.
To match the role of validateCollection(), the SVG tests here are now limited to checking that it turns the result of normalizeIconSvg() into a pass or a failure. The SVG rules themselves are covered in normalize-icon-svg.js.
accepts a normalized iconrejects an icon that is not normalizedrejects an icon that cannot be normalized
There was a problem hiding this comment.
This file holds the core logic of this PR. Sorry it is long. To see what it is trying to do, the unit tests in lib/test/normalize-icon-svg.js are the quickest read.
f3dd806 to
583042a
Compare
…lizer Co-authored-by: Joen A. <joen@automattic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URwrK9Tye4Yh48WoaqB
583042a to
c4ebbb8
Compare
|
@jasmussen, I haven't made icons in Figma very often, so I would really appreciate it if you could test whether this script works properly. |
|
Thanks a ton for your help here. This is the public workspace for icons. From it, I exported Here it is after running the git commit command locally: Looks pretty good to me, and the icon renders the same before and after. Notably it maintained the |
jasmussen
left a comment
There was a problem hiding this comment.
This probably needs a gut-check beyond my own, but from my usage, this fits the bill.
|
@jasmussen Thanks for testing! Let's leave this PR open for a bit to see if we can get a code review from someone else. |
| const fill = attributes.fill ?? rootFill; | ||
| const stroke = attributes.stroke ?? rootStroke; | ||
| const isFilled = isPainted( fill ); | ||
| const isStroked = isPainted( stroke ); |
There was a problem hiding this comment.
A fill such as #00000080 is accepted and rewritten to currentColor, which makes a half-transparent shape opaque. Could we preserve its alpha or reject paint values that cannot be converted safely, with a regression test?
There was a problem hiding this comment.
Good catch. Keeping the alpha in the color value would conflict with the rule that every shape inherits currentColor, and converting it to opacity automatically has too many edge cases to handle reliably. Instead, the script now rejects a paint value with an alpha channel and suggests using the opacity attribute instead.
Fixed in 6ca6973
| for ( const key of [ 'fill', 'stroke' ] ) { | ||
| if ( attributes[ key ]?.startsWith( 'url(' ) ) { | ||
| problems.push( | ||
| `${ key }="${ attributes[ key ] }" on <${ name }> (a library icon is drawn in a single color that inherits the text color)` | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
This check misses fill and stroke inherited from the root, so a root fill="url(#missing) none" is silently converted to a solid fill. Could we reject paint-server references on the root as well?
There was a problem hiding this comment.
Fixed in 3350586.The url( check now runs on the effective paint of each shape, so a paint server inherited from the root is rejected too.
| if ( METADATA_ELEMENTS.includes( name ) ) { | ||
| if ( ! isClosing ) { | ||
| droppedMetadata += 1; | ||
| } | ||
| continue; | ||
| } |
There was a problem hiding this comment.
This skips the metadata tags but still processes their contents: RDF metadata is rejected, and a path inside <metadata> is moved into the visible drawing. Could we skip the whole subtree and cover structured metadata in the tests?
There was a problem hiding this comment.
Fixed in e66c2e6. The script now skips everything inside <metadata>.
| const colors = [ | ||
| ...new Set( shapes.flatMap( ( shape ) => shape.colors ) ), | ||
| ]; | ||
| if ( colors.length > 1 ) { | ||
| problems.push( | ||
| `more than one color (${ colors.join( | ||
| ', ' | ||
| ) }; a library icon is drawn in a single color that inherits the text color, so a cut-out has to be part of the path, for example with fill-rule="evenodd")` | ||
| ); | ||
| } |
There was a problem hiding this comment.
This compares color strings, so #000 next to black, or #1e1e1e next to currentColor counts as two colors. Both errors then point the author at fill-rule="evenodd" cut-outs, which isn't the problem.
There was a problem hiding this comment.
Fixed in 852ba4d. Colors are now normalized before they are compared. For named colors, only black and white are normalized, so mixing another name with its hex value, such as red and #ff0000, still fails as before.
A fill such as `#80` was rewritten to `currentColor`, which made a translucent shape opaque. Reject it instead and point the author at the `opacity` attribute. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URvtxMTaiyorUJMSgTD
The `url(` check only looked at a shape's own attributes, so a root `fill="url(#missing) none"` was silently converted to a solid fill. Check the effective paint of each shape instead. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URvtxMTaiyorUJMSgTD
Only the metadata tags themselves were skipped, so RDF licensing inside `<metadata>` was rejected and a shape inside it was moved into the visible drawing. Skip the whole subtree instead. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URvtxMTaiyorUJMSgTD
A duplicate was only reported as a note, which neither the commit hook nor the build shows, so an already normalized icon with a stray copied layer passed silently. Fail instead. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URvtxMTaiyorUJMSgTD
The build fails on any icon that the script would change on a second pass, so check that each conversion's output normalizes to itself. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URvtxMTaiyorUJMSgTD
Colors were compared as strings, so `#000` next to `black` counted as two colors, and the error always pointed at `fill-rule="evenodd"` cut-outs. Compare common color forms by value, and ask for one color first, mentioning cut-outs only as one cause. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URvtxMTaiyorUJMSgTD
The commit hook writes, and the build checks through `validateCollection()`, so `--check` had no caller and no test. Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URvtxMTaiyorUJMSgTD
Co-authored-by: t-hamano <54422211+t-hamano@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011g9URytMUm4YLQnu9zcC4i
What?
Supersedes #85272.
Add a
normalize-svgscript to the icons package that converts a design-tool SVG export into the format the library uses.Why?
When you export an icon from a design tool such as Figma, the SVG does not match the format this library uses, and fixing it by hand for every icon is tedious and easy to get wrong. Until now, the build validated only part of the SVG markup, and it only reported problems without fixing them.
How?
npm run normalize-svg, converts an exported icon into the library format. For example, it removes the fixed size and sets the color in one place, so the icon follows the color of the surrounding text.validate-collection.cjsno longer has its own SVG checks, and focuses on the icon manifest instead. For each SVG, it only checks whether the new script would change the file or cannot convert it. If so, the icon is not normalized, and the build fails.Testing Instructions
npm run test:unit -- packages/icons/lib/test.npm run -w packages/icons buildand confirm it passes.packages/icons/src/library/, add it tomanifest.json, stage it and commit. Confirm the committed file is normalized.stroke-width="2"to a path in a library icon, and confirm the commit and the build both fail with a message naming the icon and the problem.Use of AI Tools
This PR was written with Claude Code and reviewed by me.