import { beforeAll, describe, expect, it } from 'vitest'; import { pasteHandler, serialize } from '@wordpress/blocks'; import type { Block } from '../../../types'; // Dynamically import block-library modules so TypeScript does not traverse outside rootDir. const { init: initAndRegisterImageBlock } = await import( `${ '../../../../../block-library/src/image' }` ); const { init: initAndRegisterTableBlock } = await import( `${ '../../../../../block-library/src/table' }` ); const { init: initAndRegisterVideoBlock } = await import( `${ '../../../../../block-library/src/video' }` ); const tableWithHeaderFooterAndBodyUsingColspan = `
Colspan 2 Header Cell
Colspan 2 Cell Data
Colspan 2 Footer Cell
`; const tableWithHeaderFooterAndBodyUsingRowspan = `
Rowspan 2 Header Cell
Header Cell
Rowspan 2 Cell Data
Cell Data
Rowspan 2 Footer Cell
Footer Cell
`; const tableWithCellAlignments = `
A - Left B - Centered C - Right D - None
100 150 200 250
`; describe( 'pasteHandler', () => { beforeAll( () => { initAndRegisterTableBlock(); initAndRegisterVideoBlock(); } ); it( 'can handle a table with thead, tbody and tfoot using colspan', () => { const [ result ] = pasteHandler( { HTML: tableWithHeaderFooterAndBodyUsingColspan, tagName: 'p', } ) as Block[]; expect( console ).toHaveLogged(); delete result.attributes.caption; expect( result.attributes ).toEqual( { hasFixedLayout: true, head: [ { cells: [ { content: 'Colspan 2', tag: 'th', colspan: '2' }, { content: 'Header Cell', tag: 'th' }, ], }, ], body: [ { cells: [ { content: 'Colspan 2', tag: 'td', colspan: '2' }, { content: 'Cell Data', tag: 'td' }, ], }, ], foot: [ { cells: [ { content: 'Colspan 2', tag: 'th', colspan: '2' }, { content: 'Footer Cell', tag: 'th' }, ], }, ], } ); expect( result.name ).toEqual( 'core/table' ); expect( result.isValid ).toBeTruthy(); } ); it( 'can handle a table with thead, tbody and tfoot using rowspan', () => { const [ result ] = pasteHandler( { HTML: tableWithHeaderFooterAndBodyUsingRowspan, tagName: 'p', } ) as Block[]; expect( console ).toHaveLogged(); delete result.attributes.caption; expect( result.attributes ).toEqual( { hasFixedLayout: true, head: [ { cells: [ { content: 'Rowspan 2', tag: 'th', rowspan: '2' }, { content: 'Header Cell', tag: 'th' }, ], }, { cells: [ { content: 'Header Cell', tag: 'th' } ], }, ], body: [ { cells: [ { content: 'Rowspan 2', tag: 'td', rowspan: '2' }, { content: 'Cell Data', tag: 'td' }, ], }, { cells: [ { content: 'Cell Data', tag: 'td' } ], }, ], foot: [ { cells: [ { content: 'Rowspan 2', tag: 'td', rowspan: '2' }, { content: 'Footer Cell', tag: 'td' }, ], }, { cells: [ { content: 'Footer Cell', tag: 'td' } ], }, ], } ); expect( result.name ).toEqual( 'core/table' ); expect( result.isValid ).toBeTruthy(); } ); it( 'can handle a table with cell alignments', () => { const [ result ] = pasteHandler( { HTML: tableWithCellAlignments, tagName: 'p', } ) as Block[]; expect( console ).toHaveLogged(); delete result.attributes.caption; expect( result.name ).toEqual( 'core/table' ); expect( result.isValid ).toBeTruthy(); expect( result.attributes ).toEqual( { hasFixedLayout: true, head: [ { cells: [ { content: 'A - Left', tag: 'th', align: 'left', colspan: undefined, rowspan: undefined, }, { content: 'B - Centered', tag: 'th', align: 'center', colspan: undefined, rowspan: undefined, }, { content: 'C - Right', tag: 'th', align: 'right', colspan: undefined, rowspan: undefined, }, { content: 'D - None', tag: 'th', align: undefined, colspan: undefined, rowspan: undefined, }, ], }, ], body: [ { cells: [ { content: '100', tag: 'td', align: 'left', colspan: undefined, rowspan: undefined, }, { content: '150', tag: 'td', align: 'center', colspan: undefined, rowspan: undefined, }, { content: '200', tag: 'td', align: 'right', colspan: undefined, rowspan: undefined, }, { content: '250', tag: 'td', align: undefined, colspan: undefined, rowspan: undefined, }, ], }, ], foot: [], } ); } ); it( 'preserves wrapped in when source is plain text only', () => { const result = pasteHandler( { HTML: '', plainText: 'x', mode: 'INLINE', tagName: 'p', } ); expect( console ).toHaveLogged(); expect( result ).toBe( 'x' ); } ); it( 'preserves wrapped in when source is HTML', () => { const result = pasteHandler( { HTML: 'x', plainText: '', mode: 'INLINE', tagName: 'p', } ); expect( console ).toHaveLogged(); expect( result ).toBe( 'x' ); } ); it( 'can handle a video', () => { const [ result ] = pasteHandler( { HTML: '', tagName: 'p', // @ts-expect-error Deliberate legacy option. preserveWhiteSpace: false, } ) as Block[]; expect( console ).toHaveLogged(); delete result.attributes.caption; expect( result.attributes ).toEqual( { autoplay: true, loop: true, muted: true, controls: true, playsInline: true, preload: 'auto', poster: 'https://example.com/media.jpg', src: 'https://example.com/media.mp4', tracks: [], } ); expect( result.name ).toEqual( 'core/video' ); expect( result.isValid ).toBeTruthy(); } ); } ); describe( 'pasteHandler — core/image', () => { beforeAll( () => { initAndRegisterImageBlock(); } ); it( 'pins the width and lets the height follow the aspect ratio for a bare ', () => { const [ result ] = pasteHandler( { HTML: '', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); expect( result.attributes.url ).toBe( 'https://example.com/i.jpg' ); expect( result.attributes.width ).toBe( '77px' ); // Height is pinned to `auto` rather than the literal pixel value so the // image scales proportionally when the content width constrains it. expect( result.attributes.height ).toBe( 'auto' ); expect( result.attributes.aspectRatio ).toBe( '1' ); // The serialized style must keep `height:auto` plus the declared // aspect ratio so a capped width never stretches the image. const serialized = serialize( result ); expect( serialized ).toContain( 'width:77px' ); expect( serialized ).toContain( 'height:auto' ); expect( serialized ).toContain( 'aspect-ratio:1' ); } ); it( 'pins the width and lets the height follow the aspect ratio for an inside a
', () => { const [ result ] = pasteHandler( { HTML: '
', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); expect( result.attributes.width ).toBe( '120px' ); expect( result.attributes.height ).toBe( 'auto' ); expect( result.attributes.aspectRatio ).toBe( '1.5' ); } ); it( 'pins the width, sets the aspect ratio, and lifts the anchor for an anchor-wrapped (e.g. a Flickr embed)', () => { const [ result ] = pasteHandler( { HTML: 'pexels', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); expect( result.attributes.url ).toBe( 'https://live.staticflickr.com/65535/123_b.jpg' ); expect( result.attributes.width ).toBe( '1024px' ); expect( result.attributes.height ).toBe( 'auto' ); expect( result.attributes.aspectRatio ).toBe( String( 1024 / 683 ) ); expect( result.attributes.linkDestination ).toBe( 'custom' ); expect( result.attributes.href ).toBe( 'https://www.flickr.com/photos/x/123/' ); const serialized = serialize( result ); expect( serialized ).toContain( 'width:1024px' ); expect( serialized ).toContain( 'height:auto' ); expect( serialized ).toContain( `aspect-ratio:${ 1024 / 683 }` ); } ); it( 'pins the width and lets the height follow when only a width is present', () => { const [ result ] = pasteHandler( { HTML: '', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); expect( result.attributes.width ).toBe( '120px' ); expect( result.attributes.height ).toBe( 'auto' ); // A single declared dimension can't carry an aspect ratio. expect( result.attributes.aspectRatio ).toBeUndefined(); } ); it( 'pins the height and lets the width follow when only a height is present', () => { const [ result ] = pasteHandler( { HTML: '', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); // A height-only source keeps its height; the width follows via `auto`. expect( result.attributes.width ).toBe( 'auto' ); expect( result.attributes.height ).toBe( '80px' ); expect( result.attributes.aspectRatio ).toBeUndefined(); } ); it( 'omits the aspect ratio when a dimension is zero', () => { const [ result ] = pasteHandler( { HTML: '', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); // A zero dimension would divide to `Infinity`/`NaN`, so no ratio is set. expect( result.attributes.aspectRatio ).toBeUndefined(); } ); it( 'drops non-pixel width/height (e.g. percentages)', () => { const [ result ] = pasteHandler( { HTML: '', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); expect( result.attributes.width ).toBeUndefined(); expect( result.attributes.height ).toBeUndefined(); } ); it( 'leaves width/height unset when not present on the source', () => { const [ result ] = pasteHandler( { HTML: '', mode: 'BLOCKS', } ) as Block[]; expect( console ).toHaveLogged(); expect( result.name ).toBe( 'core/image' ); expect( result.attributes.width ).toBeUndefined(); expect( result.attributes.height ).toBeUndefined(); } ); } );