10up / safe-svg

Enable SVG uploads and sanitize them to stop XML/SVG vulnerabilities in your WordPress website.
https://wordpress.org/plugins/safe-svg/
GNU General Public License v2.0
263 stars 31 forks source link

When svg optimization is turned on, a javascript error can occur that breaks saving content #170

Closed dkotter closed 7 months ago

dkotter commented 7 months ago

Describe the bug

As reported on WordPress.org, if svg optimization is turned on (which is a feature added in #79), there's a scenario where saving content doesn't work.

In testing myself, I was able to reproduce and discover the problem. A javascript console error ends up being thrown (n.blocks is not iterable) which causes any other javascript to no longer execute (like the save action).

This error comes from this line: https://github.com/10up/safe-svg/blob/72a69f0f93e7f589f6113facb9db751a18626a8a/assets/js/admin/admin.js#L249

In looking at that code, it seems to listen for the save event and then it tries iterating through all the block content to find any svgs that need optimization. The problem here is if no block changes happened, this causes an error because we try iterating on an element that doesn't actually exist.

There may be a better fix for this but at the very least, we need to ensure the changes.blocks attribute exists before we try iterating over it.

Steps to Reproduce

Follow the instructions shown in the video here: https://wordpress.org/support/topic/pages-fails-to-update-when-optimizer-enabled/

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress information

No response

Code of Conduct

dkotter commented 7 months ago

Note we'll want to ensure @tictag gets props for this once a fix is in place, as they provided the initial report with very helpful reproduction steps