Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/**
* WordPress dependencies
*/
import {
TextareaControl,
Notice,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { TextareaControl, Notice } from '@wordpress/components';
import { Stack } from '@wordpress/ui';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

Expand All @@ -29,58 +26,66 @@ export function validateCSS( css ) {
return true;
}

/**
* Returns the error message string if the CSS contains HTML markup, or null if it is clean.
*
* @param {string} css The CSS string to check.
* @return {string|null} An error message, or null if the CSS is valid.
*/
function getMarkupValidationError( css ) {
return validateCSS( css )
? null
: __( 'The custom CSS is invalid. Do not use <> markup.' );
}

/**
* Full CSS validation: markup check first (fast), then a CSS parser (slower).
*
* @param {string} css The CSS string to validate.
* @return {string|null} An error message, or null if the CSS is valid.
*/
function getCSSValidationError( css ) {
if ( ! css ) {
return null;
}
const markupError = getMarkupValidationError( css );
if ( markupError ) {
return markupError;
}
const [ transformed ] = transformStyles(
[ { css } ],
'.for-validation-only'
);
return transformed === null
? __( 'There is an error with your CSS structure.' )
: null;
}

export default function AdvancedPanel( {
value,
onChange,
inheritedValue = value,
help,
} ) {
// Custom CSS
const [ cssError, setCSSError ] = useState( null );
const customCSS = inheritedValue?.css;
const [ cssError, setCSSError ] = useState( () =>
getCSSValidationError( customCSS )
);
function handleOnChange( newValue ) {
onChange( {
...value,
css: newValue,
} );

// Validate immediately on change for quick feedback.
if ( ! validateCSS( newValue ) ) {
setCSSError(
__( 'The custom CSS is invalid. Do not use <> markup.' )
);
return;
}

// Clear HTML markup error if CSS is now valid.
if ( cssError ) {
setCSSError( null );
}
setCSSError( getMarkupValidationError( newValue ) );
}
function handleOnBlur( event ) {
const cssValue = event?.target?.value;

if ( ! cssValue || ! validateCSS( cssValue ) ) {
return;
}

// Check if the value is valid CSS structure on blur (more expensive check).
// Pass a wrapping selector to ensure that `transformStyles` validates the CSS.
// Note that the wrapping selector here is not used in the actual output of any styles.
const [ transformed ] = transformStyles(
[ { css: cssValue } ],
'.for-validation-only'
);

setCSSError(
transformed === null
? __( 'There is an error with your CSS structure.' )
: null
);
setCSSError( getCSSValidationError( event?.target?.value ) );
}

return (
<VStack spacing={ 3 }>
<Stack direction="column" gap="md">
{ cssError && (
<Notice status="error" onRemove={ () => setCSSError( null ) }>
{ cssError }
Expand All @@ -95,6 +100,6 @@ export default function AdvancedPanel( {
spellCheck={ false }
help={ help }
/>
</VStack>
</Stack>
);
}
10 changes: 0 additions & 10 deletions tools/eslint/suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@
"count": 2
}
},
"packages/block-editor/src/components/global-styles/advanced-panel.js": {
"@wordpress/use-recommended-components": {
"count": 1
}
},
"packages/block-editor/src/components/global-styles/color-panel.js": {
"@wordpress/use-recommended-components": {
"count": 3
Expand Down Expand Up @@ -1554,11 +1549,6 @@
"count": 1
}
},
"packages/media-editor/src/image-editor/react/hooks/use-cropper-state.ts": {
"react-hooks/refs": {
"count": 2
}
},
"packages/media-editor/src/image-editor/react/hooks/use-interaction.ts": {
"react-hooks/refs": {
"count": 5
Expand Down
Loading