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

Server-side render: Test for accuracy of removeBlockSupportAttributes #48898

Closed
wants to merge 5 commits into from
Closed
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
44 changes: 31 additions & 13 deletions packages/server-side-render/src/server-side-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,29 @@ import { addQueryArgs } from '@wordpress/url';
import { Placeholder, Spinner } from '@wordpress/components';
import { __experimentalSanitizeBlockAttributes } from '@wordpress/blocks';

const EMPTY_OBJECT = {};
export const KNOWN_ATTRIBUTES = new Set( [
'anchor',
'backgroundColor',
'borderColor',
'className',
'fontFamily',
'fontSize',
'gradient',
'layout',
'shadow',
'textColor',
] );

export const KNOWN_STYLES = new Set( [
'border',
'color',
'elements',
'layout',
'position',
'shadow',
'spacing',
'typography',
] );

export function rendererPath( block, attributes = null, urlQueryArgs = {} ) {
return addQueryArgs( `/wp/v2/block-renderer/${ block }`, {
Expand All @@ -25,19 +47,15 @@ export function rendererPath( block, attributes = null, urlQueryArgs = {} ) {
}

export function removeBlockSupportAttributes( attributes ) {
const {
backgroundColor,
borderColor,
fontFamily,
fontSize,
gradient,
textColor,
className,
...restAttributes
} = attributes;
const restAttributes = { ...attributes };
for ( const attribute of KNOWN_ATTRIBUTES ) {
delete restAttributes[ attribute ];
}

const { border, color, elements, spacing, typography, ...restStyles } =
attributes?.style || EMPTY_OBJECT;
const restStyles = { ...attributes?.style };
for ( const style of KNOWN_STYLES ) {
delete restStyles[ style ];
}

return {
...restAttributes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// @ts-check

/**
* External dependencies
*/
const { existsSync, readdirSync, readFileSync } = require( 'fs' );
const { join } = require( 'path' );

/**
* Internal dependencies
*/
const {
KNOWN_ATTRIBUTES,
KNOWN_STYLES,
} = require( '../server-side-render.js' );

/*
* Path to the lib/block-supports directory
*/
const BLOCK_SUPPORTS_DIR = 'lib/block-supports';

/*
* Regular expression to match instances of PHP code inside BLOCK_SUPPORTS_DIR
* where an attribute is being accessed by name.
*
* @example $block_type->attributes['anchor'] = array( ... )
*/
const ATTRIBUTE_KEY_PATTERN = /attributes\['(\w+)'\]/g;

/*
* Regular expression to match instances of PHP code inside BLOCK_SUPPORTS_DIR
* where a style property is being accessed by name.
*
* @example $block_attributes['style']['typography']['letterSpacing']
*/
const STYLE_KEY_PATTERN = /\['style'\]\['(\w+)'\]/g;

describe( 'removeBlockSupportAttributes', () => {
it( 'tests should be able to access lib/block-supports', () => {
expect( existsSync( BLOCK_SUPPORTS_DIR ) ).toBe( true );
} );

it( 'tests should be able to read source code from lib/block-supports', () => {
const dir = readdirSync( BLOCK_SUPPORTS_DIR );
expect( dir ).toContainEqual( expect.stringMatching( /\.php$/ ) );
} );

it( 'its constant KNOWN_ATTRIBUTES is up to date', () => {
// Get all PHP files in lib/block-supports
const sourceFiles = readdirSync( BLOCK_SUPPORTS_DIR )
.filter( ( file ) => file.match( /\.php$/ ) )
.map( ( file ) => join( BLOCK_SUPPORTS_DIR, file ) );

// Get all unique attribute names from the PHP files
const attributes = new Set(
sourceFiles.flatMap( ( file ) =>
grep( file, ATTRIBUTE_KEY_PATTERN )
.filter( ( attribute ) => attribute !== 'style' ) // Styles are handled separately
.map( ( attribute ) =>
attribute.replace( /^class$/, 'className' )
)
)
);

expect( attributes ).toEqual( KNOWN_ATTRIBUTES );
} );

it( 'its constant KNOWN_STYLES is up to date', () => {
// Get all PHP files in lib/block-supports
const sourceFiles = readdirSync( BLOCK_SUPPORTS_DIR )
.filter( ( file ) => file.match( /\.php$/ ) )
.map( ( file ) => join( BLOCK_SUPPORTS_DIR, file ) );

// Get all unique style keys from the PHP files
const styles = new Set(
sourceFiles.flatMap( ( file ) => grep( file, STYLE_KEY_PATTERN ) )
);

// styles should be a subset of KNOWN_STYLES
expect( Array.from( KNOWN_STYLES ) ).not.toEqual(
expect.not.arrayContaining( Array.from( styles ) )
);
} );
} );

/**
* Given a filename and a RegExp pattern, return all fragments of text in the
* given file that match the pattern.
*
* @param {string} filename Path to the source file.
* @param {RegExp} pattern Regular expression to match against.
* @return {string[]} Set of matching attribute names.
*/
function grep( filename, pattern ) {
const contents = readFileSync( filename, 'utf8' ).toString();
const matches = Array.from( contents.matchAll( pattern ) );
return matches.map( ( [ , match ] ) => match );
}