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

PostCSS style transformation: fail gracefully instead of throwing an error #56093

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 49 additions & 0 deletions packages/block-editor/src/utils/test/transform-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,55 @@
import transformStyles from '../transform-styles';

describe( 'transformStyles', () => {
describe( 'error handling', () => {
beforeEach( () => {
// Intentionally suppress the expected console errors and warnings to reduce
// noise in the test output.
jest.spyOn( console, 'warn' ).mockImplementation( jest.fn() );
} );

it( 'should not throw error in case of invalid css', () => {
const run = () =>
transformStyles(
[
{
css: 'h1 { color: red;', // invalid CSS
},
],
'.my-namespace'
);

expect( run ).not.toThrow();
expect( console ).toHaveWarned();
} );

it( 'should warn invalid css in the console', () => {
const run = () =>
transformStyles(
[
{
css: 'h1 { color: red; }', // valid CSS
},
{
css: 'h1 { color: red;', // invalid CSS
},
],
'.my-namespace'
);

const [ validCSS, invalidCSS ] = run();

expect( validCSS ).toBe( '.my-namespace h1 { color: red; }' );
expect( invalidCSS ).toBe( null );

expect( console ).toHaveWarnedWith(
'wp.blockEditor.transformStyles Failed to transform CSS.',
'<css input>:1:1: Unclosed block\n> 1 | h1 { color: red;\n | ^'
// ^^^^ In PostCSS, a tab is equal four spaces
);
} );
} );

describe( 'selector wrap', () => {
it( 'should wrap regular selectors', () => {
const input = `h1 { color: red; }`;
Expand Down
44 changes: 31 additions & 13 deletions packages/block-editor/src/utils/transform-styles/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import postcss from 'postcss';
import postcss, { CssSyntaxError } from 'postcss';
import wrap from 'postcss-prefixwrap';
import rebaseUrl from 'postcss-urlrebase';

Expand All @@ -19,18 +19,36 @@ import rebaseUrl from 'postcss-urlrebase';
*/
const transformStyles = ( styles, wrapperSelector = '' ) => {
return styles.map( ( { css, ignoredSelectors = [], baseURL } ) => {
return postcss(
[
wrapperSelector &&
wrap( wrapperSelector, {
ignoredSelectors: [
...ignoredSelectors,
wrapperSelector,
],
} ),
baseURL && rebaseUrl( { rootUrl: baseURL } ),
].filter( Boolean )
).process( css, {} ).css; // use sync PostCSS API
try {
return postcss(
[
wrapperSelector &&
wrap( wrapperSelector, {
ignoredSelectors: [
...ignoredSelectors,
wrapperSelector,
],
} ),
baseURL && rebaseUrl( { rootUrl: baseURL } ),
].filter( Boolean )
).process( css, {} ).css; // use sync PostCSS API
} catch ( error ) {
if ( error instanceof CssSyntaxError ) {
// eslint-disable-next-line no-console
console.warn(
'wp.blockEditor.transformStyles Failed to transform CSS.',
error.message + '\n' + error.showSourceCode( false )
);
} else {
// eslint-disable-next-line no-console
console.warn(
'wp.blockEditor.transformStyles Failed to transform CSS.',
error
);
}

return null;
}
} );
};

Expand Down
Loading