import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { createElement, Component } from '@wordpress/element';
import type { Block, BlockSaveProps, BlockType } from '../../types';
import '../../store';
import serialize, {
getCommentAttributes,
getSaveContent,
serializeAttributes,
getCommentDelimitedContent,
serializeBlock,
getBlockInnerHTML,
} from '../serializer';
import {
getBlockTypes,
registerBlockType,
unregisterBlockType,
setFreeformContentHandlerName,
setUnregisteredTypeHandlerName,
} from '../registration';
import { createBlock } from '../';
describe( 'block serializer', () => {
afterEach( () => {
setFreeformContentHandlerName( '' );
setUnregisteredTypeHandlerName( '' );
getBlockTypes().forEach( ( block ) => {
unregisterBlockType( block.name );
} );
} );
describe( 'getSaveContent()', () => {
describe( 'function save', () => {
const fruitBlockSave = ( {
attributes,
}: BlockSaveProps< { fruit?: string } > ) =>
createElement( 'div', null, attributes.fruit );
it( 'should return element as string if save returns element', () => {
const saved = getSaveContent(
{
name: 'core/fruit',
save: fruitBlockSave,
} as unknown as BlockType,
{ fruit: 'Bananas' }
);
expect( saved ).toBe( '
Bananas
' );
} );
it( 'should work when block type is passed as string', () => {
registerBlockType( 'core/fruit', {
apiVersion: 3,
title: 'Fruit',
category: 'widgets',
attributes: {},
save: fruitBlockSave,
} );
const saved = getSaveContent( 'core/fruit', {
fruit: 'Bananas',
} );
expect( saved ).toBe( 'Bananas
' );
} );
} );
describe( 'component save', () => {
it( 'should return element as string', () => {
const saved = getSaveContent(
{
save: class extends Component<
BlockSaveProps< { fruit?: string } >
> {
render() {
return createElement(
'div',
null,
this.props.attributes.fruit
);
}
},
name: 'core/fruit',
} as unknown as BlockType,
{ fruit: 'Bananas' }
);
expect( saved ).toBe( 'Bananas
' );
} );
} );
} );
describe( 'getCommentAttributes()', () => {
it( 'should return an empty set if no attributes provided', () => {
const attributes = getCommentAttributes( {} as BlockType, {} );
expect( attributes ).toEqual( {} );
} );
it( 'should only return attributes which are not matched from content', () => {
const attributes = getCommentAttributes(
{
attributes: {
fruit: {
type: 'string',
source: 'text',
},
category: {
type: 'string',
},
ripeness: {
type: 'string',
},
},
} as unknown as BlockType,
{
fruit: 'bananas',
category: 'food',
ripeness: 'ripe',
}
);
expect( attributes ).toEqual( {
category: 'food',
ripeness: 'ripe',
} );
} );
it( 'should skip attributes whose values are undefined', () => {
const attributes = getCommentAttributes(
{
attributes: {
fruit: {
type: 'string',
},
ripeness: {
type: 'string',
},
},
} as unknown as BlockType,
{
fruit: 'bananas',
ripeness: undefined,
}
);
expect( attributes ).toEqual( { fruit: 'bananas' } );
} );
it( 'should ignore local attributes', () => {
const attributes = getCommentAttributes(
{
attributes: {
blob: {
type: 'string',
role: 'local',
},
url: {
type: 'string',
},
},
} as unknown as BlockType,
{
blob: 'blob://false-url.com',
url: 'http://real-url.com',
}
);
expect( attributes ).toEqual( {
url: 'http://real-url.com',
} );
} );
} );
describe( 'serializeAttributes()', () => {
it( 'should not break HTML comments', () => {
expect( serializeAttributes( { a: '-- and --' } ) ).toBe(
'{"a":"\\u002d\\u002d and \\u002d\\u002d"}'
);
} );
it( 'should not break standard-non-compliant tools for "<"', () => {
expect( serializeAttributes( { a: '< and <' } ) ).toBe(
'{"a":"\\u003c and \\u003c"}'
);
} );
it( 'should not break standard-non-compliant tools for ">"', () => {
expect( serializeAttributes( { a: '> and >' } ) ).toBe(
'{"a":"\\u003e and \\u003e"}'
);
} );
it( 'should not break standard-non-compliant tools for "&"', () => {
expect( serializeAttributes( { a: '& and &' } ) ).toBe(
'{"a":"\\u0026 and \\u0026"}'
);
} );
it( 'should replace quotation marks', () => {
expect( serializeAttributes( { a: '" and "' } ) ).toBe(
'{"a":"\\u0022 and \\u0022"}'
);
} );
it( 'should handle backslash and quote combinations', () => {
const orig = {
bs: '\\',
bsQuote: '\\"',
bsQuoteBs: '\\"\\',
};
expect( JSON.parse( serializeAttributes( orig ) ) ).toEqual( orig );
expect( serializeAttributes( orig ) ).toBe(
'{"bs":"\\u005c","bsQuote":"\\u005c\\u0022","bsQuoteBs":"\\u005c\\u0022\\u005c"}'
);
} );
} );
describe( 'getCommentDelimitedContent()', () => {
it( 'should generate empty attributes void', () => {
const content = getCommentDelimitedContent(
'core/test-block',
{},
''
);
expect( content ).toBe( '' );
} );
it( 'should include the namespace for non-core blocks', () => {
const content = getCommentDelimitedContent(
'my-wonderful-namespace/test-block',
{},
''
);
expect( content ).toBe(
''
);
} );
it( 'should generate empty attributes non-void', () => {
const content = getCommentDelimitedContent(
'core/test-block',
{},
'Delicious'
);
expect( content ).toBe(
'\nDelicious\n'
);
} );
it( 'should generate non-empty attributes void', () => {
const content = getCommentDelimitedContent(
'core/test-block',
{ fruit: 'Banana' },
''
);
expect( content ).toBe(
''
);
} );
it( 'should generate non-empty attributes non-void', () => {
const content = getCommentDelimitedContent(
'core/test-block',
{ fruit: 'Banana' },
'Delicious'
);
expect( content ).toBe(
'\nDelicious\n'
);
} );
} );
describe( 'serializeBlock()', () => {
it( 'serializes the freeform content fallback block without comment delimiters', () => {
registerBlockType( 'core/freeform', {
apiVersion: 3,
category: 'text',
title: 'freeform block',
attributes: {
fruit: {
type: 'string',
},
},
save: ( {
attributes,
}: BlockSaveProps< { fruit?: string } > ) =>
attributes.fruit || null,
} );
setFreeformContentHandlerName( 'core/freeform' );
const block = createBlock( 'core/freeform', {
fruit: 'Bananas',
} );
const content = serializeBlock( block );
expect( content ).toBe( 'Bananas' );
} );
it( 'serializes the freeform content fallback block with comment delimiters in nested context', () => {
registerBlockType( 'core/freeform', {
apiVersion: 3,
category: 'text',
title: 'freeform block',
attributes: {
fruit: {
type: 'string',
},
},
save: ( {
attributes,
}: BlockSaveProps< { fruit?: string } > ) =>
attributes.fruit || null,
} );
setFreeformContentHandlerName( 'core/freeform' );
const block = createBlock( 'core/freeform', {
fruit: 'Bananas',
} );
const content = serializeBlock( block, { isInnerBlocks: true } );
expect( content ).toBe(
'\n' +
'Bananas\n' +
''
);
} );
it( 'serializes the unregistered fallback block without comment delimiters', () => {
registerBlockType( 'core/unregistered-block', {
apiVersion: 3,
category: 'text',
title: 'unregistered block',
attributes: {
fruit: {
type: 'string',
},
},
save: ( {
attributes,
}: BlockSaveProps< { fruit?: string } > ) =>
attributes.fruit || null,
} );
setUnregisteredTypeHandlerName( 'core/unregistered-block' );
const block = createBlock( 'core/unregistered-block', {
fruit: 'Bananas',
} );
const content = serializeBlock( block );
expect( content ).toBe( 'Bananas' );
} );
it( 'preserves content from invalid blocks when source information is present', () => {
registerBlockType( 'core/quote', {
apiVersion: 3,
category: 'text',
title: 'Quote',
attributes: { content: { type: 'string' } },
save: ( {
attributes,
}: BlockSaveProps< { content?: string } > ) =>
createElement(
'blockquote',
{},
attributes.content as React.ReactNode
),
} );
const block = {
...createBlock( 'core/quote' ),
isValid: false,
__unstableBlockSource: {
blockName: 'quote',
attrs: {},
innerHTML: 'Not a quote
',
innerBlocks: [],
innerContent: [ 'Not a quote
' ],
},
};
expect( serializeBlock( block ) ).toBe(
'\nNot a quote
\n'
);
} );
it( 're-generates content from invalid blocks when source information is missing (losing content)', () => {
registerBlockType( 'core/quote', {
apiVersion: 3,
category: 'text',
title: 'Quote',
attributes: { content: { type: 'string' } },
save: ( {
attributes,
}: BlockSaveProps< { content?: string } > ) =>
createElement(
'blockquote',
{},
attributes.content as React.ReactNode
),
} );
// missing attributes as a result of a failed parse
const block = {
...createBlock( 'core/quote' ),
isValid: false,
};
expect( serializeBlock( block ) ).toBe( '' );
} );
it( 'serializes the Custom HTML block from its static fragments', () => {
registerBlockType( 'core/html', {
apiVersion: 3,
category: 'text',
title: 'Custom HTML',
attributes: {},
save: () => null,
} );
registerBlockType( 'core/fruit', {
apiVersion: 3,
category: 'text',
title: 'fruit block',
attributes: {
fruit: {
type: 'string',
},
},
save: ( {
attributes,
}: BlockSaveProps< { fruit?: string } > ) =>
attributes.fruit || null,
} );
const block = createBlock(
'core/html',
{},
[ createBlock( 'core/fruit', { fruit: 'Bananas' } ) ],
[ '', null, '
' ]
);
expect( serializeBlock( block ) ).toBe(
'\n' +
'\n' +
'Bananas\n' +
'
\n' +
''
);
} );
it( 'appends inner blocks missing an innerContent placeholder', () => {
registerBlockType( 'core/html', {
apiVersion: 3,
category: 'text',
title: 'Custom HTML',
attributes: {},
save: () => null,
} );
registerBlockType( 'core/fruit', {
apiVersion: 3,
category: 'text',
title: 'fruit block',
attributes: {
fruit: {
type: 'string',
},
},
save: ( {
attributes,
}: BlockSaveProps< { fruit?: string } > ) =>
attributes.fruit || null,
} );
const block = createBlock(
'core/html',
{},
[ createBlock( 'core/fruit', { fruit: 'Bananas' } ) ],
[ '' ]
);
expect( serializeBlock( block ) ).toBe(
'\n' +
'\n' +
'Bananas\n' +
'\n' +
''
);
} );
} );
describe( 'serialize()', () => {
beforeEach( () => {
const blockType = {
apiVersion: 3,
attributes: {
throw: {
type: 'boolean',
},
defaulted: {
type: 'boolean',
default: false,
},
content: {
type: 'string',
source: 'text',
},
stuff: {
type: 'string',
},
},
save( {
attributes,
}: BlockSaveProps< {
throw?: boolean;
content?: string;
stuff?: string;
} > ) {
if ( attributes.throw ) {
throw new Error();
}
return { attributes.content }
;
},
category: 'text',
title: 'block title',
};
registerBlockType( 'core/test-block', blockType );
} );
it( 'should serialize the post content properly', () => {
const block = createBlock( 'core/test-block', {
content: 'Ribs & Chicken',
stuff: 'left & right -- but ',
} );
const expectedPostContent =
'\nRibs & Chicken
\n';
expect( serialize( [ block ] ) ).toEqual( expectedPostContent );
expect( serialize( block ) ).toEqual( expectedPostContent );
} );
it( 'should preserve content for invalid block', () => {
const block = createBlock( 'core/test-block', {
content: 'Incorrect',
} );
block.isValid = false;
block.originalContent = 'Correct';
expect( serialize( block ) ).toEqual(
'\nCorrect\n'
);
} );
it( 'should preserve content for crashing block', () => {
const block = createBlock( 'core/test-block', {
content: 'Incorrect',
throw: true,
} );
block.originalContent = 'Correct';
expect( serialize( block ) ).toEqual(
'\nCorrect\n'
);
} );
} );
describe( 'getBlockInnerHTML', () => {
it( "should return the block's serialized inner HTML", () => {
const blockType = {
apiVersion: 3,
attributes: {
content: {
type: 'string',
source: 'text',
},
},
save( { attributes }: BlockSaveProps< { content?: string } > ) {
return attributes.content || null;
},
category: 'text',
title: 'block title',
};
registerBlockType( 'core/chicken', blockType );
const block: Block< { content?: string } > = {
clientId: 'test-client-id',
name: 'core/chicken',
attributes: {
content: 'chicken',
},
innerBlocks: [],
isValid: true,
};
expect( getBlockInnerHTML( block ) ).toBe( 'chicken' );
} );
} );
} );