Skip to content

Commit

Permalink
Merge pull request #173 from 10up/empty-page-optimization-bug
Browse files Browse the repository at this point in the history
ensure blocks are present before processing
  • Loading branch information
dkotter authored Feb 6, 2024
2 parents 72a69f0 + 9aeb236 commit bc674d9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"plugins": [".", "./tests/cypress/test-plugin"],
"plugins": [
".",
"./tests/cypress/test-plugin/e2e-test-plugin.php",
"./tests/cypress/test-plugin/e2e-test-plugin-optimizer.php"
],
"env": {
"tests": {
"mappings": {
Expand Down
4 changes: 4 additions & 0 deletions assets/js/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ import {select, subscribe} from '@wordpress/data';
subscribe(() => {
if (editorStore.isSavingPost()) {
const changes = editorStore.getPostEdits();
// make sure we have blocks to process
if (!Array.isArray(changes.blocks) || !changes.blocks.length) {
return;
}
for (const changedBlock of changes.blocks) {
const blockName = changedBlock?.name ?? '';
const innerBlocks = changedBlock?.innerBlocks ?? [];
Expand Down
1 change: 1 addition & 0 deletions tests/cypress/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { defineConfig } = require('cypress')

module.exports = defineConfig({
chromeWebSecurity: false,
fixturesFolder: 'tests/cypress/fixtures',
screenshotsFolder: 'tests/cypress/screenshots',
videosFolder: 'tests/cypress/videos',
Expand Down
9 changes: 9 additions & 0 deletions tests/cypress/e2e/safe-svg.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ describe('Safe SVG Tests', () => {

cy.get('.media-item .error-div.error').should('exist').contains('has failed to upload');
});


// Test plugin doesn't break the block editor when no blocks are added
it('Plugin should not break the block editor when optimizer enabled', () => {
// Activate Test Plugin
cy.deactivatePlugin('safe-svg-cypress-test-plugin');
cy.activatePlugin('safe-svg-cypress-optimizer-test-plugin');
cy.createPost('Hello World');
});
});
12 changes: 12 additions & 0 deletions tests/cypress/test-plugin/e2e-test-plugin-optimizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Plugin name: Safe SVG Cypress Optimizer Test plugin
* Description: Test plugin for Safe SVG to test the optimizer.
*
* @package safe-svg
*/

/**
* Optimizer and allowed attributes can't be mutually enabled, hence the secondary testing plugin.
*/
add_filter('safe_svg_optimizer_enabled', '__return_true');

0 comments on commit bc674d9

Please sign in to comment.