import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest'; import { getBlockSupport } from '@wordpress/blocks'; import { select } from '@wordpress/data'; import { getNodesWithStyles, getNodesWithSettings, generateCustomProperties, transformToStyles, transformToStylesWithTextClipBase, getBlockSelectors, generateGlobalStyles, } from '../core/render'; import type { GlobalStylesConfig } from '../types'; import { ROOT_BLOCK_SELECTOR, ROOT_CSS_PROPERTIES_SELECTOR, } from '../utils/common'; // Mock WordPress data store vi.mock( import( '@wordpress/data' ), async ( importOriginal ) => ( { ...( await importOriginal() ), select: vi.fn(), } ) ); // Mock WordPress blocks store vi.mock( import( '@wordpress/blocks' ), async ( importOriginal ) => ( { ...( await importOriginal() ), getBlockSupport: vi.fn(), getBlockTypes: vi.fn(), } ) ); const mockedSelect = vi.mocked( select ) as Mock; const mockedGetBlockSupport = vi.mocked( getBlockSupport ) as Mock; const FIT_CONTENT_EXCLUSIONS = ':not(.alignwide):not(.alignfull):not(.is-layout-flex > *):not(.is-layout-grid > *):not(.has-fit-text):where(.is-layout-flow > *, .wp-site-blocks > *, .wp-block-media-text__content > *)'; const getFitContentRuleset = ( selector: string ) => { const fitSelector = `:root :where(${ selector }${ FIT_CONTENT_EXCLUSIONS }:not([style*="rotate:"]))`; return ( `${ fitSelector }{width: fit-content;}` + `${ fitSelector }:where(.has-text-align-center){margin-left: auto !important;margin-right: auto !important;}` + `${ fitSelector }:where(.has-text-align-right){margin-left: auto !important;}` + `${ fitSelector }:where(.has-text-align-left){margin-right: auto !important;}` ); }; const mockFitContent = ( blockName: string ) => mockedGetBlockSupport.mockImplementation( ( name, path, defaultValue ) => name === blockName && path === 'transform.fitContent' ? true : defaultValue ); // Mock WordPress elements (minimal, no mocking of complex APIs) const ELEMENTS = { link: 'a:where(:not(.wp-element-button))', h1: 'h1', h2: 'h2', h3: 'h3', h4: 'h4', h5: 'h5', h6: 'h6', button: '.wp-element-button', caption: '.wp-element-caption', }; describe( 'global styles renderer', () => { describe( 'getNodesWithStyles', () => { it( 'should return the nodes with styles', () => { const tree = { styles: { color: { background: 'red', text: 'red', }, blocks: { 'core/heading': { color: { background: 'blue', text: 'blue', }, elements: { h1: { typography: { fontSize: '42px', }, }, h2: { typography: { fontSize: '23px', }, }, link: { ':hover': { color: { background: 'green', text: 'yellow', }, }, ':focus': { color: { background: 'green', text: 'yellow', }, }, }, }, }, 'core/image': { border: { radius: '9999px', }, }, }, elements: { link: { color: { background: 'yellow', text: 'yellow', }, ':hover': { color: { background: 'hotpink', text: 'black', }, }, ':focus': { color: { background: 'hotpink', text: 'black', }, }, }, }, }, }; const blockSelectors = { 'core/heading': { selector: '.my-heading1, .my-heading2', }, 'core/image': { selector: '.my-image', featureSelectors: '.my-image img, .my-image .crop-area', }, }; expect( getNodesWithStyles( tree, blockSelectors ) ).toEqual( [ { styles: { color: { background: 'red', text: 'red', }, }, selector: ROOT_BLOCK_SELECTOR, skipSelectorWrapper: true, }, { styles: { color: { background: 'yellow', text: 'yellow', }, ':hover': { color: { background: 'hotpink', text: 'black', }, }, ':focus': { color: { background: 'hotpink', text: 'black', }, }, }, selector: ELEMENTS.link, elementName: 'link', skipSelectorWrapper: true, }, { styles: { color: { background: 'blue', text: 'blue', }, }, selector: '.my-heading1, .my-heading2', name: 'core/heading', sourceStyles: tree.styles.blocks[ 'core/heading' ], }, { styles: { typography: { fontSize: '42px', }, }, selector: '.my-heading1 h1, .my-heading2 h1', elementName: 'h1', }, { styles: { typography: { fontSize: '23px', }, }, selector: '.my-heading1 h2, .my-heading2 h2', elementName: 'h2', }, { styles: { ':hover': { color: { background: 'green', text: 'yellow', }, }, ':focus': { color: { background: 'green', text: 'yellow', }, }, }, selector: '.my-heading1 a:where(:not(.wp-element-button)), .my-heading2 a:where(:not(.wp-element-button))', elementName: 'link', }, { styles: { border: { radius: '9999px', }, }, selector: '.my-image', name: 'core/image', featureSelectors: '.my-image img, .my-image .crop-area', sourceStyles: tree.styles.blocks[ 'core/image' ], }, ] ); } ); } ); describe( 'getNodesWithSettings', () => { it( 'should return nodes with settings', () => { const tree: GlobalStylesConfig = { styles: { color: { background: 'red', text: 'red', }, }, settings: { color: { palette: [ { name: 'White', slug: 'white', color: 'white' }, { name: 'Black', slug: 'black', color: 'black' }, ], }, blocks: { 'core/paragraph': { typography: { fontSizes: [ { name: 'small', slug: 'small', size: '12px', }, { name: 'medium', slug: 'medium', size: '23px', }, ], }, }, }, }, }; const blockSelectors = { 'core/paragraph': { selector: 'p', elements: { link: 'p a', h1: 'p h1', h2: 'p h2', h3: 'p h3', h4: 'p h4', h5: 'p h5', h6: 'p h6', }, }, }; expect( getNodesWithSettings( tree, blockSelectors ) ).toEqual( [ { presets: { color: { palette: [ { name: 'White', slug: 'white', color: 'white', }, { name: 'Black', slug: 'black', color: 'black', }, ], }, }, selector: ROOT_CSS_PROPERTIES_SELECTOR, }, { presets: { typography: { fontSizes: [ { name: 'small', slug: 'small', size: '12px', }, { name: 'medium', slug: 'medium', size: '23px', }, ], }, }, selector: 'p', }, ] ); } ); } ); describe( 'generateCustomProperties', () => { it( 'should return a ruleset', () => { const tree: GlobalStylesConfig = { settings: { dimensions: { aspectRatios: { default: [ { name: 'Square - 1:1', slug: 'square', ratio: '1', }, ], theme: [ { name: 'Wide - 16:9', slug: '16-9', ratio: '16/9', }, ], }, }, color: { palette: { custom: [ { name: 'White', slug: 'white', color: 'white', }, { name: 'Black', slug: 'black', color: 'black', }, { name: 'White to Black', slug: 'white2black', color: 'value', }, ], }, }, custom: { white2black: 'value', 'font-primary': 'value', 'line-height': { body: 1.7, heading: 1.3, }, }, blocks: { 'core/heading': { typography: { fontSizes: { theme: [ { name: 'small', slug: 'small', size: '12px', }, { name: 'medium', slug: 'medium', size: '23px', }, ], }, }, }, }, }, }; const blockSelectors = { 'core/heading': { selector: 'h1,h2,h3,h4,h5,h6', }, }; expect( generateCustomProperties( tree, blockSelectors ) ).toEqual( ':root{--wp--preset--aspect-ratio--square: 1;--wp--preset--aspect-ratio--16-9: 16/9;--wp--preset--color--white: white;--wp--preset--color--black: black;--wp--preset--color--white-2-black: value;--wp--custom--white-2-black: value;--wp--custom--font-primary: value;--wp--custom--line-height--body: 1.7;--wp--custom--line-height--heading: 1.3;}h1,h2,h3,h4,h5,h6{--wp--preset--font-size--small: 12px;--wp--preset--font-size--medium: 23px;}' ); } ); } ); describe( 'transformToStyles', () => { const minimalStyleOptions = { blockGap: false, blockStyles: true, layoutStyles: false, marginReset: false, presets: false, rootPadding: false, }; const textInputSelectorWith = ( pseudoSelector: string ) => `textarea${ pseudoSelector }, input:where([type=email],[type=number],[type=password],[type=search],[type=tel],[type=text],[type=url])${ pseudoSelector }`; const transformTextInputStyles = ( textInputStyles: Record< string, unknown > ) => transformToStyles( Object.freeze( { styles: { elements: { textInput: textInputStyles }, }, } as unknown as GlobalStylesConfig ), {}, false, false, true, true, minimalStyleOptions ); it( 'uses the row value for Flow and Constrained layouts and both values for Flex and Grid layouts when block spacing is axial', () => { const tree: GlobalStylesConfig = { styles: { blocks: { 'core/group': { spacing: { blockGap: { top: '1em', left: '2em' }, }, }, }, }, }; const blockSelectors = { 'core/group': { selector: '.wp-block-group', hasLayoutSupport: true, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, true, false, false, true, minimalStyleOptions ); expect( result ).toContain( ':root :where(.wp-block-group-is-layout-flow) > * { margin-block-start: 1em; margin-block-end: 0; }' ); expect( result ).toContain( ':root :where(.wp-block-group-is-layout-constrained) > * { margin-block-start: 1em; margin-block-end: 0; }' ); expect( result ).toContain( ':root :where(.wp-block-group-is-layout-flex) { gap: 1em 2em; }' ); expect( result ).toContain( ':root :where(.wp-block-group-is-layout-grid) { gap: 1em 2em; }' ); } ); it( 'should return a ruleset', () => { const tree = { settings: { color: { palette: { default: [ { name: 'White', slug: 'white', color: 'white', }, { name: 'Black', slug: 'black', color: 'black', }, ], }, }, blocks: { 'core/heading': { color: { palette: { default: [ { name: 'Blue', slug: 'blue', color: 'blue', }, ], }, }, }, }, }, styles: { spacing: { margin: '10px', padding: '10px', }, color: { background: 'red', }, elements: { h1: { typography: { fontSize: '42px', }, }, link: { color: { text: 'blue', }, ':hover': { color: { text: 'orange', }, }, ':focus': { color: { text: 'orange', }, }, }, }, blocks: { 'core/group': { spacing: { margin: { top: '10px', right: '20px', bottom: '30px', left: '40px', }, padding: { top: '11px', right: '22px', bottom: '33px', left: '44px', }, }, }, 'core/heading': { color: { text: 'orange', }, elements: { link: { color: { text: 'hotpink', }, ':hover': { color: { text: 'red', }, }, ':focus': { color: { text: 'red', }, }, }, }, }, 'core/image': { color: { text: 'red', }, border: { radius: '9999px', }, }, }, }, }; const blockSelectors = { 'core/group': { selector: '.wp-block-group', }, 'core/heading': { selector: 'h1,h2,h3,h4,h5,h6', }, 'core/image': { selector: '.wp-block-image', featureSelectors: { border: '.wp-block-image img, .wp-block-image .wp-crop-area', }, }, }; expect( transformToStyles( tree, blockSelectors ) ).toEqual( ':where(body) {margin: 0;}.is-layout-flow > .alignleft { float: left; margin-left: 0; margin-right: 2em; }.is-layout-flow > .alignright { float: right; margin-left: 2em; margin-right: 0; }.is-layout-flow > .aligncenter { margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > .alignleft { float: left; margin-left: 0; margin-right: 2em; }.is-layout-constrained > .alignright { float: right; margin-left: 2em; margin-right: 0; }.is-layout-constrained > .aligncenter { margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)) { max-width: var(--wp--style--global--content-size); margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > .alignwide { max-width: var(--wp--style--global--wide-size); }body .is-layout-flex { display:flex; }.is-layout-flex { flex-wrap: wrap; align-items: center; }.is-layout-flex > :is(*, div) { margin: 0; }body .is-layout-grid { display:grid; }.is-layout-grid > :is(*, div) { margin: 0; }body{background-color: red;margin: 10px;padding: 10px;}a:where(:not(.wp-element-button)){color: blue;}:root :where(a:where(:not(.wp-element-button)):hover){color: orange;}:root :where(a:where(:not(.wp-element-button)):focus){color: orange;}h1{font-size: 42px;}:root :where(.wp-block-group){margin-top: 10px;margin-right: 20px;margin-bottom: 30px;margin-left: 40px;padding-top: 11px;padding-right: 22px;padding-bottom: 33px;padding-left: 44px;}:root :where(h1,h2,h3,h4,h5,h6){color: orange;}:root :where(h1 a:where(:not(.wp-element-button)),h2 a:where(:not(.wp-element-button)),h3 a:where(:not(.wp-element-button)),h4 a:where(:not(.wp-element-button)),h5 a:where(:not(.wp-element-button)),h6 a:where(:not(.wp-element-button))){color: hotpink;}:root :where(h1 a:where(:not(.wp-element-button)):hover,h2 a:where(:not(.wp-element-button)):hover,h3 a:where(:not(.wp-element-button)):hover,h4 a:where(:not(.wp-element-button)):hover,h5 a:where(:not(.wp-element-button)):hover,h6 a:where(:not(.wp-element-button)):hover){color: red;}:root :where(h1 a:where(:not(.wp-element-button)):focus,h2 a:where(:not(.wp-element-button)):focus,h3 a:where(:not(.wp-element-button)):focus,h4 a:where(:not(.wp-element-button)):focus,h5 a:where(:not(.wp-element-button)):focus,h6 a:where(:not(.wp-element-button)):focus){color: red;}:root :where(.wp-block-image img, .wp-block-image .wp-crop-area){border-radius: 9999px;}:root :where(.wp-block-image){color: red;}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.has-white-color{color: var(--wp--preset--color--white) !important;}.has-white-background-color{background-color: var(--wp--preset--color--white) !important;}.has-white-border-color{border-color: var(--wp--preset--color--white) !important;}.has-black-color{color: var(--wp--preset--color--black) !important;}.has-black-background-color{background-color: var(--wp--preset--color--black) !important;}.has-black-border-color{border-color: var(--wp--preset--color--black) !important;}:where(h1,h2,h3,h4,h5,h6).has-blue-color{color: var(--wp--preset--color--blue) !important;}:where(h1,h2,h3,h4,h5,h6).has-blue-background-color{background-color: var(--wp--preset--color--blue) !important;}:where(h1,h2,h3,h4,h5,h6).has-blue-border-color{border-color: var(--wp--preset--color--blue) !important;}' ); } ); it( 'should handle feature selectors', () => { const tree = { styles: { blocks: { 'core/image': { color: { text: 'red', }, spacing: { padding: { top: '1px', }, }, border: { color: 'red', radius: '9999px', }, }, }, }, }; const blockSelectors = { 'core/image': { selector: '.wp-image', featureSelectors: { spacing: '.wp-image-spacing', border: { root: '.wp-image-border', color: '.wp-image-border-color', }, }, }, }; expect( transformToStyles( Object.freeze( tree ), blockSelectors ) ).toEqual( ':where(body) {margin: 0;}.is-layout-flow > .alignleft { float: left; margin-left: 0; margin-right: 2em; }.is-layout-flow > .alignright { float: right; margin-left: 2em; margin-right: 0; }.is-layout-flow > .aligncenter { margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > .alignleft { float: left; margin-left: 0; margin-right: 2em; }.is-layout-constrained > .alignright { float: right; margin-left: 2em; margin-right: 0; }.is-layout-constrained > .aligncenter { margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)) { max-width: var(--wp--style--global--content-size); margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > .alignwide { max-width: var(--wp--style--global--wide-size); }body .is-layout-flex { display:flex; }.is-layout-flex { flex-wrap: wrap; align-items: center; }.is-layout-flex > :is(*, div) { margin: 0; }body .is-layout-grid { display:grid; }.is-layout-grid > :is(*, div) { margin: 0; }:root :where(.wp-image-spacing){padding-top: 1px;}:root :where(.wp-image-border-color){border-color: red;}:root :where(.wp-image-border){border-radius: 9999px;}:root :where(.wp-image){color: red;}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }' ); } ); it( 'should handle block variations', () => { const tree: GlobalStylesConfig = { styles: { blocks: { 'core/image': { variations: { foo: { color: { text: 'blue', }, spacing: { padding: { top: '2px', }, }, border: { color: 'blue', }, }, }, }, }, }, }; const blockSelectors = { 'core/image': { selector: '.wp-image', featureSelectors: { spacing: '.wp-image-spacing', border: { root: '.wp-image-border', color: '.wp-image-border-color', }, }, styleVariationSelectors: { foo: '.is-style-foo.wp-image', }, }, }; const hasBlockGapSupport = false; const hasFallbackGapSupport = true; const disableLayoutStyles = true; const disableRootPadding = true; const styleOptions: Record< string, boolean > = { blockGap: false, blockStyles: true, layoutStyles: false, marginReset: false, presets: false, rootPadding: false, }; // Confirm no variation styles by default. const withoutVariations = transformToStyles( Object.freeze( tree ), blockSelectors, hasBlockGapSupport, hasFallbackGapSupport, disableLayoutStyles, disableRootPadding, styleOptions ); expect( withoutVariations ).toEqual( '' ); // Includes variation styles when requested. styleOptions.variationStyles = true; const withVariations = transformToStyles( Object.freeze( tree ), blockSelectors, hasBlockGapSupport, hasFallbackGapSupport, disableLayoutStyles, disableRootPadding, styleOptions ); expect( withVariations ).toEqual( ':root :where(.wp-image-spacing.is-style-foo){padding-top: 2px;}:root :where(.wp-image-border-color.is-style-foo){border-color: blue;}:root :where(.is-style-foo.wp-image){color: blue;}' ); } ); it( 'handles variation inner block states', () => { const tree = { styles: { blocks: { 'core/group': { variations: { foo: { blocks: { 'core/button': { color: { text: 'red', }, ':hover': { color: { text: 'blue', }, }, '@mobile': { color: { text: 'green', }, ':hover': { color: { text: 'yellow', }, }, }, }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/group': { selector: '.wp-block-group', styleVariationSelectors: { foo: '.is-style-foo.wp-block-group', }, }, 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true, } ); expect( result ).toEqual( ':root :where(.is-style-foo.wp-block-group .wp-block-button){color: red;}@media (width <= 480px){:root :where(.is-style-foo.wp-block-group .wp-block-button){color: green;}}:root :where(.is-style-foo.wp-block-group .wp-block-button:hover){color: blue;}@media (width <= 480px){:root :where(.is-style-foo.wp-block-group .wp-block-button:hover){color: yellow;}}' ); } ); it( 'should handle block pseudo selectors', () => { const tree = { styles: { blocks: { 'core/button': { color: { text: 'red', }, ':hover': { color: { text: 'blue', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { blockGap: false, blockStyles: true, layoutStyles: false, marginReset: false, presets: false, rootPadding: false, } ); expect( result ).toEqual( ':root :where(.wp-block-button){color: red;}:root :where(.wp-block-button:hover){color: blue;}' ); } ); it( 'renders text input focus color styles with capped specificity', () => { const css = transformTextInputStyles( { ':focus': { color: { text: 'blue' } }, } ); expect( css ).toEqual( `:root :where(${ textInputSelectorWith( ':focus' ) }){color: blue;}` ); } ); it( 'renders text input user-invalid spacing styles with capped specificity', () => { const css = transformTextInputStyles( { ':user-invalid': { spacing: { padding: { top: '1rem' } } }, } ); expect( css ).toEqual( `:root :where(${ textInputSelectorWith( ':user-invalid' ) }){padding-top: 1rem;}` ); } ); it( 'renders text input placeholder typography styles with the pseudo-element after :where()', () => { const css = transformTextInputStyles( { '::placeholder': { typography: { fontStyle: 'italic' } }, } ); expect( css ).toEqual( `:root :where(${ textInputSelectorWith( '' ) })::placeholder{font-style: italic;}` ); } ); it( 'outputs focus after validation states so focus color wins when both states match', () => { const css = transformTextInputStyles( { ':focus': { color: { text: 'blue' } }, ':user-valid': { color: { text: 'green' } }, ':user-invalid': { color: { text: 'red' } }, } ); expect( css ).toEqual( `:root :where(${ textInputSelectorWith( ':user-valid' ) }){color: green;}` + `:root :where(${ textInputSelectorWith( ':user-invalid' ) }){color: red;}` + `:root :where(${ textInputSelectorWith( ':focus' ) }){color: blue;}` ); } ); it( 'should handle select element pseudo selectors', () => { const tree = { styles: { elements: { select: { color: { text: 'red', }, ':hover': { color: { text: 'blue', }, }, ':visited': { color: { text: 'green', }, }, }, }, }, } as unknown as GlobalStylesConfig; const result = transformToStyles( Object.freeze( tree ), {}, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( 'select{color: red;}:root :where(select:hover){color: blue;}' ); } ); it( 'outputs default pseudo styles after responsive base styles', () => { const tree = { styles: { blocks: { 'core/button': { color: { text: 'black', }, ':hover': { color: { text: 'blue', }, }, '@mobile': { color: { text: 'red', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( ':root :where(.wp-block-button){color: black;}@media (width <= 480px){:root :where(.wp-block-button){color: red;}}:root :where(.wp-block-button:hover){color: blue;}' ); } ); describe( 'state styles under a text clip', () => { const textGradient = { background: { gradient: 'linear-gradient(135deg,#ff0000,#0000ff)', backgroundClip: 'text', }, }; const blockSelectors = { 'core/paragraph': { selector: '.wp-block-paragraph' }, }; const render = ( paragraphStyles: object ) => transformToStyles( Object.freeze( { styles: { blocks: { 'core/paragraph': paragraphStyles }, }, } as unknown as GlobalStylesConfig ), blockSelectors, false, false, true, true, minimalStyleOptions ); it( 'clears the inherited gradient when a breakpoint escapes the clip', () => { const result = render( { ...textGradient, '@mobile': { background: { backgroundClip: 'border-box' }, }, } ); expect( result ).toContain( '@media (width <= 480px){:root :where(.wp-block-paragraph){background-clip: border-box;-webkit-text-fill-color: currentColor;background-image: unset;}}' ); } ); it( 'leaves a background the breakpoint paints itself', () => { const result = render( { ...textGradient, '@mobile': { background: { gradient: 'linear-gradient(135deg,#00ff00,#ffff00)', backgroundClip: 'border-box', }, }, } ); expect( result ).not.toContain( 'background-image: unset' ); } ); it( 'restores the fill for a breakpoint text color', () => { const result = render( { ...textGradient, '@mobile': { color: { text: '#00ff00' } }, } ); expect( result ).toContain( '@media (width <= 480px){:root :where(.wp-block-paragraph){color: #00ff00;-webkit-text-fill-color: currentColor;}}' ); } ); it( 'restores the fill on the color feature selector', () => { const result = transformToStyles( Object.freeze( { styles: { blocks: { 'core/search': { ...textGradient, '@mobile': { color: { text: '#00ff00' } }, }, }, }, } as unknown as GlobalStylesConfig ), { 'core/search': { selector: '.wp-block-search', featureSelectors: { color: '.wp-block-search__button', background: '.wp-block-search__button', }, }, }, false, false, true, true, minimalStyleOptions ); expect( result ).toContain( '@media (width <= 480px){:root :where(.wp-block-search__button){color: #00ff00;-webkit-text-fill-color: currentColor;}}' ); } ); it( 'adds nothing when the breakpoint keeps the text clip', () => { const result = render( { ...textGradient, '@mobile': { background: { gradient: 'linear-gradient(135deg,#00ff00,#ffff00)', backgroundClip: 'text', }, }, } ); expect( result ).not.toContain( 'background-image: unset' ); expect( result ).not.toContain( '-webkit-text-fill-color: currentColor' ); } ); it( 'reads the clip the breakpoint sets for itself', () => { const result = transformToStyles( Object.freeze( { styles: { blocks: { 'core/button': { background: { backgroundClip: 'border-box', }, '@mobile': { background: { gradient: 'linear-gradient(135deg,#ff0000,#0000ff)', backgroundClip: 'text', }, ':hover': { color: { text: '#00ff00' }, }, }, }, }, }, } as unknown as GlobalStylesConfig ), { 'core/button': { selector: '.wp-block-button' } }, false, false, true, true, minimalStyleOptions ); expect( result ).toContain( '@media (width <= 480px){:root :where(.wp-block-button:hover){color: #00ff00;-webkit-text-fill-color: currentColor;}}' ); } ); it( 'reads the variation clip for a breakpoint inside it', () => { const result = transformToStyles( Object.freeze( { styles: { blocks: { 'core/group': { variations: { flashy: { ...textGradient, '@mobile': { background: { backgroundClip: 'border-box', }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig ), { 'core/group': { selector: '.wp-block-group', styleVariationSelectors: { flashy: '.is-style-flashy.wp-block-group', }, }, }, false, false, true, true, { ...minimalStyleOptions, variationStyles: true } ); expect( result ).toContain( 'background-image: unset' ); } ); it( 'reads the block clip for a breakpoint inside a variation', () => { const result = transformToStyles( Object.freeze( { styles: { blocks: { 'core/group': { ...textGradient, variations: { flashy: { '@mobile': { background: { backgroundClip: 'border-box', }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig ), { 'core/group': { selector: '.wp-block-group', styleVariationSelectors: { flashy: '.is-style-flashy.wp-block-group', }, }, }, false, false, true, true, { ...minimalStyleOptions, variationStyles: true } ); expect( result ).toContain( '@media (width <= 480px){:root :where(.is-style-flashy.wp-block-group){background-clip: border-box;-webkit-text-fill-color: currentColor;background-image: unset;}}' ); } ); it( 'reads the breakpoint clip for a pseudo state inside it', () => { const result = transformToStyles( Object.freeze( { styles: { blocks: { 'core/button': { ...textGradient, '@mobile': { background: { backgroundClip: 'border-box', }, ':hover': { color: { text: '#00ff00' }, }, }, }, }, }, } as unknown as GlobalStylesConfig ), { 'core/button': { selector: '.wp-block-button' } }, false, false, true, true, minimalStyleOptions ); // The breakpoint already escaped the clip, so no fill is needed. expect( result ).toContain( '@media (width <= 480px){:root :where(.wp-block-button:hover){color: #00ff00;}}' ); } ); it( 'reads the default pseudo state clip for a breakpoint pseudo state', () => { const result = transformToStyles( Object.freeze( { styles: { blocks: { 'core/button': { ':hover': textGradient, '@mobile': { ':hover': { color: { text: '#00ff00' }, }, }, }, }, }, } as unknown as GlobalStylesConfig ), { 'core/button': { selector: '.wp-block-button' } }, false, false, true, true, minimalStyleOptions ); expect( result ).toContain( '@media (width <= 480px){:root :where(.wp-block-button:hover){color: #00ff00;-webkit-text-fill-color: currentColor;}}' ); } ); } ); describe( 'block style variation instances over a block type text clip', () => { const textGradient = { background: { gradient: 'linear-gradient(135deg,#ff0000,#0000ff)', backgroundClip: 'text', }, }; const blockSelectors = { 'core/paragraph': { selector: '.wp-block-paragraph', styleVariationSelectors: { 'hl-1': '.is-style-hl-1.wp-block-paragraph', }, }, 'core/group': { selector: '.wp-block-group', styleVariationSelectors: { 'section-2': '.is-style-section-2.wp-block-group', }, }, 'core/button': { selector: '.wp-block-button', styleVariationSelectors: { 'hover-3': '.is-style-hover-3.wp-block-button', }, }, }; // The instance-only config shape the editor passes. const renderInstance = ( blockName: string, variation: string, variationStyles: Record< string, unknown >, textClipBaseBlocks?: Record< string, unknown > ) => transformToStylesWithTextClipBase( Object.freeze( { styles: { blocks: { [ blockName ]: { variations: { [ variation ]: variationStyles, }, }, }, }, } as unknown as GlobalStylesConfig ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true }, textClipBaseBlocks ); it( 'refills the letters for a variation text color', () => { expect( renderInstance( 'core/paragraph', 'hl-1', { color: { text: '#00aa00' } }, { 'core/paragraph': textGradient } ) ).toEqual( ':root :where(.is-style-hl-1.wp-block-paragraph){color: #00aa00;-webkit-text-fill-color: currentColor;}' ); } ); it( 'refills the letters for an inner block color', () => { expect( renderInstance( 'core/group', 'section-2', { blocks: { 'core/paragraph': { color: { text: '#aa00aa' }, }, }, }, { 'core/paragraph': textGradient } ) ).toEqual( ':root :where(.is-style-section-2.wp-block-group .wp-block-paragraph){color: #aa00aa;-webkit-text-fill-color: currentColor;}' ); } ); it( 'refills the letters for a variation breakpoint color', () => { expect( renderInstance( 'core/paragraph', 'hl-1', { '@mobile': { color: { text: '#0000aa' } } }, { 'core/paragraph': textGradient } ) ).toEqual( '@media (width <= 480px){:root :where(.is-style-hl-1.wp-block-paragraph){color: #0000aa;-webkit-text-fill-color: currentColor;}}' ); } ); it( 'refills the letters for a variation pseudo state color', () => { expect( renderInstance( 'core/button', 'hover-3', { ':hover': { color: { text: '#aa0000' } } }, { 'core/button': textGradient } ) ).toEqual( ':root :where(.is-style-hover-3.wp-block-button:hover){color: #aa0000;-webkit-text-fill-color: currentColor;}' ); } ); it( 'adds nothing when the block type has no text clip', () => { expect( renderInstance( 'core/paragraph', 'hl-1', { color: { text: '#00aa00' }, '@mobile': { background: { backgroundClip: 'border-box' }, }, }, { 'core/paragraph': { background: { gradient: textGradient.background.gradient, }, }, } ) ).toEqual( ':root :where(.is-style-hl-1.wp-block-paragraph){color: #00aa00;}@media (width <= 480px){:root :where(.is-style-hl-1.wp-block-paragraph){background-clip: border-box;-webkit-text-fill-color: currentColor;}}' ); } ); } ); it( 'ignores root-level state styles', () => { const tree = { styles: { color: { text: 'red', }, ':hover': { color: { text: 'blue', }, }, '@mobile': { color: { text: 'green', }, ':hover': { color: { text: 'yellow', }, }, }, }, } as unknown as GlobalStylesConfig; const result = transformToStyles( Object.freeze( tree ), {}, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( 'body{color: red;}' ); } ); it( 'should handle style variation pseudo selectors', () => { const tree = { styles: { blocks: { 'core/button': { variations: { foo: { color: { text: 'green', }, dimensions: { width: '10rem', }, ':hover': { color: { text: 'yellow', }, dimensions: { width: '20rem', }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button .wp-block-button__link', featureSelectors: { dimensions: { root: '.wp-block-button', width: '.wp-block-button', }, }, styleVariationSelectors: { foo: '.wp-block-button.is-style-foo .wp-block-button__link', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { blockGap: false, blockStyles: true, layoutStyles: false, marginReset: false, presets: false, rootPadding: false, variationStyles: true, } ); expect( result ).toEqual( ':root :where(.wp-block-button.is-style-foo){width: 10rem;}:root :where(.wp-block-button.is-style-foo .wp-block-button__link){color: green;}:root :where(.wp-block-button.is-style-foo:hover){width: 20rem;}:root :where(.wp-block-button.is-style-foo .wp-block-button__link:hover){color: yellow;}' ); } ); it( 'outputs variation responsive pseudo styles after default pseudo styles', () => { const tree = { styles: { blocks: { 'core/button': { variations: { foo: { color: { text: 'green', }, dimensions: { width: '10rem', }, ':hover': { color: { text: 'blue', }, dimensions: { width: '20rem', }, }, '@tablet': { color: { text: 'red', }, dimensions: { width: '30rem', }, ':hover': { color: { text: 'orange', }, dimensions: { width: '40rem', }, }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button .wp-block-button__link', featureSelectors: { dimensions: { root: '.wp-block-button', width: '.wp-block-button', }, }, styleVariationSelectors: { foo: '.wp-block-button.is-style-foo .wp-block-button__link', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true, } ); expect( result ).toEqual( ':root :where(.wp-block-button.is-style-foo){width: 10rem;}:root :where(.wp-block-button.is-style-foo .wp-block-button__link){color: green;}@media (480px < width <= 782px){:root :where(.wp-block-button.is-style-foo){width: 30rem;}:root :where(.wp-block-button.is-style-foo .wp-block-button__link){color: red;}}:root :where(.wp-block-button.is-style-foo:hover){width: 20rem;}:root :where(.wp-block-button.is-style-foo .wp-block-button__link:hover){color: blue;}@media (480px < width <= 782px){:root :where(.wp-block-button.is-style-foo:hover){width: 40rem;}:root :where(.wp-block-button.is-style-foo .wp-block-button__link:hover){color: orange;}}' ); } ); it( 'handles responsive block styles', () => { const tree = { styles: { blocks: { 'core/button': { color: { text: 'red', }, '@mobile': { color: { text: 'blue', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( ':root :where(.wp-block-button){color: red;}@media (width <= 480px){:root :where(.wp-block-button){color: blue;}}' ); } ); it( 'handles responsive block gap styles', () => { const tree = { styles: { blocks: { 'core/group': { '@mobile': { spacing: { blockGap: '24px', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/group': { selector: '.wp-block-group', hasLayoutSupport: true, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, true, false, false, true, minimalStyleOptions ); expect( result ).toContain( '@media (width <= 480px)' ); expect( result ).toContain( ':root :where(.wp-block-group-is-layout-flow) > * { margin-block-start: 24px; margin-block-end: 0; }' ); expect( result ).toContain( ':root :where(.wp-block-group-is-layout-flex) { gap: 24px; }' ); } ); it( 'ignores deprecated responsive block state aliases', () => { const tree = { styles: { blocks: { 'core/button': { color: { text: 'red', }, mobile: { color: { text: 'blue', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( ':root :where(.wp-block-button){color: red;}' ); } ); it( 'uses custom viewport breakpoints for responsive block styles', () => { const tree = { settings: { viewport: { mobile: '640px', tablet: '960px', }, }, styles: { blocks: { 'core/button': { '@mobile': { color: { text: 'blue', }, }, '@tablet': { color: { text: 'green', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( '@media (width <= 640px){:root :where(.wp-block-button){color: blue;}}@media (640px < width <= 960px){:root :where(.wp-block-button){color: green;}}' ); } ); it( 'omits tablet responsive styles when the tablet breakpoint is not larger than mobile', () => { const tree = { settings: { viewport: { mobile: '960px', tablet: '640px', }, }, styles: { blocks: { 'core/button': { '@mobile': { color: { text: 'blue', }, }, '@tablet': { color: { text: 'green', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( '@media (width <= 960px){:root :where(.wp-block-button){color: blue;}}' ); } ); it( 'uses a single max-width tablet query when only the tablet breakpoint is valid', () => { const tree = { settings: { viewport: { mobile: '100%', tablet: '64rem', }, }, styles: { blocks: { 'core/button': { '@mobile': { color: { text: 'blue', }, }, '@tablet': { color: { text: 'green', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( '@media (width <= 64rem){:root :where(.wp-block-button){color: green;}}' ); } ); it( 'handles responsive pseudo selector styles', () => { const tree = { styles: { blocks: { 'core/button': { ':hover': { color: { text: 'blue', }, }, '@mobile': { color: { text: 'red', }, ':hover': { color: { text: 'orange', }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( '@media (width <= 480px){:root :where(.wp-block-button){color: red;}}:root :where(.wp-block-button:hover){color: blue;}@media (width <= 480px){:root :where(.wp-block-button:hover){color: orange;}}' ); } ); it( 'outputs a rotation under the transform feature selector', () => { const tree = { settings: {}, styles: { blocks: { 'core/button': { transform: { rotate: '0.25turn', }, '@mobile': { transform: { rotate: '-15deg', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', featureSelectors: { transform: '.wp-block-button .wp-block-button__link', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( ':root :where(.wp-block-button .wp-block-button__link){rotate: 0.25turn;}@media (width <= 480px){:root :where(.wp-block-button .wp-block-button__link){rotate: -15deg;}}' ); } ); it( 'outputs a translation with a rotation under the transform feature selector', () => { const tree = { settings: {}, styles: { blocks: { 'core/button': { transform: { rotate: '5deg', translate: { x: '10px', y: '-50%' }, }, '@mobile': { transform: { translate: { y: '1rem' }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', featureSelectors: { transform: '.wp-block-button .wp-block-button__link', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( ':root :where(.wp-block-button .wp-block-button__link){rotate: 5deg;--wp--style--translate-x: 10px;--wp--style--translate-y: -50%;translate: var(--wp--style--translate-x, 0) var(--wp--style--translate-y, 0);}@media (width <= 480px){:root :where(.wp-block-button .wp-block-button__link){--wp--style--translate-y: 1rem;translate: var(--wp--style--translate-x, 0) var(--wp--style--translate-y, 0);}}' ); } ); it( 'fits a rotated block or block style variation to its content', () => { mockFitContent( 'core/heading' ); const tree = { settings: {}, styles: { blocks: { 'core/heading': { transform: { rotate: '15deg' }, '@mobile': { transform: { rotate: '5deg' }, }, variations: { tilted: { transform: { rotate: '-5deg' }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/heading': { selector: '.wp-block-heading', styleVariationSelectors: { tilted: '.wp-block-heading.is-style-tilted', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true } ); expect( result ).toEqual( ':root :where(.wp-block-heading){rotate: 15deg;}' + getFitContentRuleset( '.wp-block-heading' ) + '@media (width <= 480px){:root :where(.wp-block-heading){rotate: 5deg;}' + getFitContentRuleset( '.wp-block-heading' ) + '}' + ':root :where(.wp-block-heading.is-style-tilted){rotate: -5deg;}' + getFitContentRuleset( '.wp-block-heading.is-style-tilted' ) ); } ); it( 'moves a fitted block to its Global Styles text alignment', () => { mockFitContent( 'core/heading' ); const tree = { settings: {}, styles: { blocks: { 'core/heading': { typography: { textAlign: 'center' }, '@mobile': { transform: { rotate: '5deg' }, }, }, }, }, } as unknown as GlobalStylesConfig; const result = transformToStyles( Object.freeze( tree ), { 'core/heading': { selector: '.wp-block-heading' } }, false, false, true, true, minimalStyleOptions ); const textAlignSelector = `.wp-block-heading${ FIT_CONTENT_EXCLUSIONS }:not(.has-text-align-left):not(.has-text-align-center):not(.has-text-align-right)`; expect( result ).toEqual( ':root :where(.wp-block-heading){text-align: center;}' + `:root :where(${ textAlignSelector }.has-rotation){margin-left: auto !important;margin-right: auto !important;}` + '@media (width <= 480px){:root :where(.wp-block-heading){rotate: 5deg;}' + getFitContentRuleset( '.wp-block-heading' ) + `:root :where(${ textAlignSelector }:where(.has-rotation, :not([style*="rotate:"]))){margin-left: auto !important;margin-right: auto !important;}` + '}' ); } ); it.each( [ '0', '0deg', '-0deg', '+0.0rad', '.0grad', '0TURN' ] )( 'does not fit a block with a %s rotation', ( rotate ) => { mockFitContent( 'core/heading' ); const tree = { settings: {}, styles: { blocks: { 'core/heading': { transform: { rotate } }, }, }, } as unknown as GlobalStylesConfig; const result = transformToStyles( Object.freeze( tree ), { 'core/heading': { selector: '.wp-block-heading' } }, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( `:root :where(.wp-block-heading){rotate: ${ rotate };}` ); } ); it.each( [ [ 'block', { dimensions: { width: '20rem' } }, {}, false ], [ 'variation', {}, { dimensions: { width: '20rem' } }, true ], ] )( 'leaves a rotated block to a width set on the %s', ( _label, blockStyles, variationStyles, fitsBlock ) => { mockFitContent( 'core/heading' ); const tree = { settings: {}, styles: { blocks: { 'core/heading': { ...blockStyles, transform: { rotate: '15deg' }, variations: { tilted: { ...variationStyles, transform: { rotate: '-5deg' }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/heading': { selector: '.wp-block-heading', styleVariationSelectors: { tilted: '.wp-block-heading.is-style-tilted', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true } ); expect( result ).toContain( 'rotate: -5deg' ); expect( result.includes( '.wp-block-heading:not' ) ).toBe( fitsBlock ); expect( result ).not.toContain( '.is-style-tilted:not' ); } ); it( 'outputs a scale with a rotation on the block', () => { const tree = { settings: {}, styles: { blocks: { 'core/heading': { transform: { rotate: '5deg', scale: 1.2, }, '@mobile': { transform: { scale: '90%', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/heading': { selector: '.wp-block-heading', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( ':root :where(.wp-block-heading){rotate: 5deg;scale: 1.2;}@media (width <= 480px){:root :where(.wp-block-heading){scale: 90%;}}' ); } ); it( 'outputs no scale that is not a single non-negative number or percentage', () => { const tree = { settings: {}, styles: { blocks: { 'core/heading': { transform: { scale: 'calc(1 + 1)' }, }, 'core/group': { transform: { scale: -1 }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/heading': { selector: '.wp-block-heading' }, 'core/group': { selector: '.wp-block-group' }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).not.toContain( 'scale' ); } ); it( 'keeps the width of a rotated block that does not opt in to fitting', () => { const tree = { settings: {}, styles: { blocks: { 'core/group': { transform: { rotate: '4deg' }, '@mobile': { transform: { rotate: '2deg' }, }, variations: { tilted: { transform: { rotate: '-4deg' }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/group': { selector: '.wp-block-group', styleVariationSelectors: { tilted: '.wp-block-group.is-style-tilted', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true } ); expect( result ).toEqual( ':root :where(.wp-block-group){rotate: 4deg;}' + '@media (width <= 480px){:root :where(.wp-block-group){rotate: 2deg;}}' + ':root :where(.wp-block-group.is-style-tilted){rotate: -4deg;}' ); } ); it( 'handles responsive feature selector styles', () => { const tree = { settings: {}, styles: { blocks: { 'core/button': { dimensions: { width: '25%', }, '@mobile': { dimensions: { width: '50%', }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button .wp-block-button__link', featureSelectors: { dimensions: { root: '.wp-block-button', width: '.wp-block-button', }, }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( ':root :where(.wp-block-button){width: calc(25 * 1% - (var(--wp--style--block-gap, 0.5em) * (1 - 25 / 100)));}@media (width <= 480px){:root :where(.wp-block-button){width: calc(50 * 1% - (var(--wp--style--block-gap, 0.5em) * (1 - 50 / 100)));}}' ); } ); it( 'handles responsive element styles', () => { const tree = { styles: { elements: { link: { color: { text: 'blue', }, '@mobile': { color: { text: 'red', }, ':hover': { color: { text: 'orange', }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const result = transformToStyles( Object.freeze( tree ), {}, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( 'a:where(:not(.wp-element-button)){color: blue;}@media (width <= 480px){:root :where(a:where(:not(.wp-element-button))){color: red;}}@media (width <= 480px){:root :where(a:where(:not(.wp-element-button)):hover){color: orange;}}' ); } ); it( 'renders block element styles defined only in a viewport', () => { const tree = { styles: { blocks: { 'core/group': { '@mobile': { elements: { heading: { color: { text: 'red', }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/group': { selector: '.wp-block-group', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( '@media (width <= 480px){:root :where(.wp-block-group h1,.wp-block-group h2,.wp-block-group h3,.wp-block-group h4,.wp-block-group h5,.wp-block-group h6){color: red;}}' ); } ); it( 'renders block element pseudo styles defined only in a viewport', () => { const tree = { styles: { blocks: { 'core/group': { '@mobile': { elements: { link: { ':hover': { color: { text: 'red', }, }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/group': { selector: '.wp-block-group', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( '@media (width <= 480px){:root :where(.wp-block-group a:where(:not(.wp-element-button)):hover){color: red;}}' ); } ); it( 'renders block element styles defined in separate viewports', () => { const tree = { styles: { blocks: { 'core/group': { '@mobile': { elements: { link: { color: { text: 'red', }, }, }, }, '@tablet': { elements: { link: { color: { text: 'blue', }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/group': { selector: '.wp-block-group', }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, minimalStyleOptions ); expect( result ).toEqual( '@media (width <= 480px){:root :where(.wp-block-group a:where(:not(.wp-element-button))){color: red;}}@media (480px < width <= 782px){:root :where(.wp-block-group a:where(:not(.wp-element-button))){color: blue;}}' ); } ); it( 'handles responsive style variation styles', () => { const tree = { styles: { blocks: { 'core/button': { variations: { foo: { color: { text: 'green', }, '@mobile': { color: { text: 'yellow', }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button', styleVariationSelectors: { foo: '.is-style-foo.wp-block-button', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true, } ); expect( result ).toEqual( ':root :where(.is-style-foo.wp-block-button){color: green;}@media (width <= 480px){:root :where(.is-style-foo.wp-block-button){color: yellow;}}' ); } ); it( 'appends responsive style variation pseudo-element selectors after the :where() wrapper', () => { const tree = { styles: { blocks: { 'core/group': { variations: { 'section-1': { '@mobile': { elements: { textInput: { ':focus': { color: { text: 'blue' }, }, '::placeholder': { color: { text: 'green' }, }, }, }, }, }, }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/group': { selector: '.wp-block-group', styleVariationSelectors: { 'section-1': '.wp-block-group.is-style-section-1', }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors, false, false, true, true, { ...minimalStyleOptions, variationStyles: true, } ); const scoped = ( pseudoSelector: string ) => textInputSelectorWith( pseudoSelector ) .split( ', ' ) .map( ( part ) => `.wp-block-group.is-style-section-1 ${ part }` ) .join( ', ' ); expect( result ).toContain( `@media (width <= 480px){:root :where(${ scoped( ':focus' ) }){color: blue;}}` ); expect( result ).toContain( `@media (width <= 480px){:root :where(${ scoped( '' ) })::placeholder{color: green;}}` ); } ); it( 'should handle duotone filter', () => { const tree = { styles: { blocks: { 'core/image': { filter: { duotone: 'blur(5px)', }, }, }, }, }; const blockSelectors = { 'core/image': { selector: '.wp-image', duotoneSelector: '.wp-image img', }, }; expect( transformToStyles( Object.freeze( tree ), blockSelectors ) ).toEqual( ':where(body) {margin: 0;}.is-layout-flow > .alignleft { float: left; margin-left: 0; margin-right: 2em; }.is-layout-flow > .alignright { float: right; margin-left: 2em; margin-right: 0; }.is-layout-flow > .aligncenter { margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > .alignleft { float: left; margin-left: 0; margin-right: 2em; }.is-layout-constrained > .alignright { float: right; margin-left: 2em; margin-right: 0; }.is-layout-constrained > .aligncenter { margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)) { max-width: var(--wp--style--global--content-size); margin-left: auto !important; margin-right: auto !important; }.is-layout-constrained > .alignwide { max-width: var(--wp--style--global--wide-size); }body .is-layout-flex { display:flex; }.is-layout-flex { flex-wrap: wrap; align-items: center; }.is-layout-flex > :is(*, div) { margin: 0; }body .is-layout-grid { display:grid; }.is-layout-grid > :is(*, div) { margin: 0; }.wp-image img{filter: blur(5px);}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }' ); } ); it( 'should output content and wide size variables if values are specified', () => { const tree = { settings: { layout: { contentSize: '840px', wideSize: '1100px', }, }, }; expect( transformToStyles( Object.freeze( tree ), 'body' ) ).toEqual( ':root { --wp--style--global--content-size: 840px; --wp--style--global--wide-size: 1100px;}:where(body) {margin: 0;}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }' ); } ); } ); describe( 'generateGlobalStyles', () => { beforeEach( () => { vi.clearAllMocks(); mockedSelect.mockReturnValue( { getBlockStyles: () => [], } ); } ); it( 'should use css feature selector for block custom CSS when defined', () => { const config = { version: 3, settings: {}, styles: { blocks: { 'core/paragraph': { css: 'color:red;', }, }, }, }; const blockTypes = [ { name: 'core/paragraph', selectors: { root: 'p', css: '.custom-p', }, }, ]; const [ styles ] = generateGlobalStyles( config, blockTypes ); const customCssStylesheet = styles.find( ( s: any ) => s.css && s.css.includes( 'color:red;' ) ); expect( customCssStylesheet ).toBeDefined(); expect( customCssStylesheet.css ).toContain( ':root :where(.custom-p){color:red;}' ); expect( customCssStylesheet.css ).not.toContain( ':root :where(p){color:red;}' ); } ); it( 'should render viewport and pseudo-state block custom CSS', () => { const config = { version: 3, settings: {}, styles: { blocks: { 'core/button': { css: 'color:blue;', ':hover': { css: 'color:green;', }, '@mobile': { css: 'color:pink;', ':hover': { css: 'color:yellow;', }, }, }, }, }, }; const blockTypes = [ { name: 'core/button', selectors: { root: '.wp-block-button .wp-block-button__link', typography: { writingMode: '.wp-block-button' }, dimensions: { root: '.wp-block-button', width: '.wp-block-button', }, }, }, ]; const [ styles ] = generateGlobalStyles( config, blockTypes ); const combinedCss = styles.map( ( s: any ) => s.css ).join( '\n' ); expect( combinedCss ).toContain( 'color:blue' ); expect( combinedCss ).toContain( 'color:green' ); expect( combinedCss ).toContain( 'color:pink' ); expect( combinedCss ).toContain( 'color:yellow' ); } ); it( 'should use css feature selector object form with root subkey for block custom CSS', () => { const config = { version: 3, settings: {}, styles: { blocks: { 'core/paragraph': { css: 'color:red;', }, }, }, }; const blockTypes = [ { name: 'core/paragraph', selectors: { root: 'p', css: { root: '.custom-p' }, }, }, ]; const [ styles ] = generateGlobalStyles( config, blockTypes ); const customCssStylesheet = styles.find( ( s: any ) => s.css && s.css.includes( 'color:red;' ) ); expect( customCssStylesheet ).toBeDefined(); expect( customCssStylesheet.css ).toContain( ':root :where(.custom-p){color:red;}' ); expect( customCssStylesheet.css ).not.toContain( ':root :where(p){color:red;}' ); } ); it( 'should fall back to root selector for block custom CSS when no css feature selector is defined', () => { const config = { version: 3, settings: {}, styles: { blocks: { 'core/paragraph': { css: 'color:red;', }, }, }, }; const blockTypes = [ { name: 'core/paragraph', selectors: { root: 'p', }, }, ]; const [ styles ] = generateGlobalStyles( config, blockTypes ); const customCssStylesheet = styles.find( ( s: any ) => s.css && s.css.includes( 'color:red;' ) ); expect( customCssStylesheet ).toBeDefined(); expect( customCssStylesheet.css ).toContain( ':root :where(p){color:red;}' ); } ); it( 'should output duotone SVG filters with __unstableType of svgs', () => { mockedSelect.mockReturnValue( { getBlockStyles: () => [], } ); const config = { version: 3, settings: { color: { duotone: { theme: [ { slug: 'midnight', name: 'Midnight', colors: [ '#263135', '#69a8a7' ], }, ], default: [ { slug: 'grayscale', name: 'Grayscale', colors: [ '#000000', '#ffffff' ], }, ], // User-created duotones need a filter in the // editor too, or they render on the front end but // not on the canvas. custom: [ { slug: 'custom-duotone-1', name: 'Duotone 1', colors: [ '#0000ff', '#1a4548' ], }, ], }, }, }, styles: {}, }; const blockTypes = [ { name: 'core/image', selectors: { root: '.wp-block-image', filter: { duotone: '.wp-block-image img' }, }, }, ]; const [ styles ] = generateGlobalStyles( config, blockTypes ); const svgStyle = styles.find( ( s: any ) => s.__unstableType === 'svgs' ); expect( svgStyle ).toBeDefined(); expect( svgStyle.__unstableType ).toBe( 'svgs' ); const assets = svgStyle.assets.join( '' ); expect( assets ).toContain( 'wp-duotone-midnight' ); expect( assets ).toContain( 'wp-duotone-grayscale' ); expect( assets ).toContain( 'wp-duotone-custom-duotone-1' ); } ); describe( 'block gap support', () => { const blockTypes = [ { name: 'core/columns', selectors: { root: '.wp-block-columns' }, supports: { layout: true, spacing: { blockGap: { __experimentalDefault: '2em' }, }, }, }, ]; const getGlobalStylesCSS = ( settings: Record< string, any > ) => { const config: any = { version: 3, settings, styles: { spacing: { blockGap: '24px' }, blocks: { // WordPress adds an empty block gap placeholder for // blocks that declare a default block gap value. 'core/columns': { spacing: { blockGap: null } }, }, }, }; const [ styles ] = generateGlobalStyles( config, blockTypes ); return styles .map( ( style: any ) => style.css ?? '' ) .join( '' ); }; it( 'outputs block gap styles when the theme opts into block gap', () => { const css = getGlobalStylesCSS( { spacing: { blockGap: true }, } ); expect( css ).toContain( ':root :where(.is-layout-flex) { gap: 24px; }' ); expect( css ).toContain( '--wp--style--block-gap: 24px;' ); expect( css ).not.toContain( ':where(.wp-block-columns.is-layout-flex) { gap: 2em; }' ); } ); it( 'outputs fallback gap styles when the theme sets the block gap setting to null', () => { // WordPress' default theme.json sets `spacing.blockGap` to // `null` for themes that do not opt into block gap. const css = getGlobalStylesCSS( { spacing: { blockGap: null }, } ); expect( css ).toContain( ':where(.is-layout-flex) { gap: 0.5em; }' ); expect( css ).toContain( ':where(.wp-block-columns.is-layout-flex) { gap: 2em; }' ); expect( css ).not.toContain( ':root :where(.is-layout-flex) { gap: 24px; }' ); expect( css ).not.toContain( '--wp--style--block-gap' ); } ); it( 'outputs fallback gap styles when the block gap setting is missing', () => { const css = getGlobalStylesCSS( {} ); expect( css ).toContain( ':where(.is-layout-flex) { gap: 0.5em; }' ); expect( css ).toContain( ':where(.wp-block-columns.is-layout-flex) { gap: 2em; }' ); expect( css ).not.toContain( ':root :where(.is-layout-flex) { gap: 24px; }' ); } ); } ); } ); describe( 'getBlockSelectors', () => { beforeEach( () => { // Reset mocks before each test vi.clearAllMocks(); } ); it( 'should return block selectors data', () => { // Mock the select function to return getBlockStyles mockedSelect.mockReturnValue( { getBlockStyles: () => [ { name: 'foo', label: 'foo' } ], } ); const imageSelectors = { root: '.my-image', border: '.my-image img, .my-image .crop-area', filter: { duotone: 'img' }, }; const imageBlock = { name: 'core/image', selectors: imageSelectors, title: 'My Image', category: 'media', }; const blockTypes = [ imageBlock ]; expect( getBlockSelectors( blockTypes as unknown as Parameters< typeof getBlockSelectors >[ 0 ] ) ).toEqual( { 'core/image': { name: imageBlock.name, selector: imageSelectors.root, duotoneSelector: imageSelectors.filter.duotone, fallbackGapValue: undefined, featureSelectors: { root: '.my-image', border: '.my-image img, .my-image .crop-area', filter: { duotone: 'img' }, }, styleVariationSelectors: { foo: '.my-image.is-style-foo', }, hasLayoutSupport: false, }, } ); } ); it( 'should return block selectors data with old experimental selectors', () => { // Mock the select function to return getBlockStyles with empty array mockedSelect.mockReturnValue( { getBlockStyles: () => [], } ); // Mock getBlockSupport to handle experimental duotone support mockedGetBlockSupport.mockImplementation( ( blockType, path, defaultValue ) => { if ( path === 'color.__experimentalDuotone' ) { return 'img'; } return defaultValue; } ); const imageSupports = { __experimentalBorder: { radius: true, __experimentalSelector: 'img, .crop-area', }, color: { __experimentalDuotone: 'img', }, __experimentalSelector: '.my-image', }; const imageBlock = { name: 'core/image', supports: imageSupports, title: 'My Image', category: 'media', }; const blockTypes = [ imageBlock ]; expect( getBlockSelectors( blockTypes as unknown as Parameters< typeof getBlockSelectors >[ 0 ] ) ).toEqual( { 'core/image': { name: imageBlock.name, selector: imageSupports.__experimentalSelector, duotoneSelector: '.my-image img', fallbackGapValue: undefined, featureSelectors: { root: '.my-image', border: '.my-image img, .my-image .crop-area', }, hasLayoutSupport: false, }, } ); } ); } ); describe( 'button width declarations', () => { it( 'should convert direct percentage width to calc() formula', () => { const tree: GlobalStylesConfig = { settings: {}, styles: { blocks: { 'core/button': { dimensions: { width: '25%', }, }, }, }, }; const blockSelectors = { 'core/button': { selector: '.wp-block-button .wp-block-button__link', featureSelectors: { dimensions: { root: '.wp-block-button', width: '.wp-block-button', }, }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors ); expect( result ).toContain( ':root :where(.wp-block-button){width: calc(25 * 1% - (var(--wp--style--block-gap, 0.5em) * (1 - 25 / 100)));}' ); } ); it( 'should convert preset percentage width to calc() formula', () => { const tree = { settings: { blocks: { 'core/button': { dimensions: { dimensionSizes: { default: [ { slug: '50', name: '50%', size: '50%', }, ], }, }, }, }, }, styles: { blocks: { 'core/button': { dimensions: { width: 'var:preset|dimension|50', }, }, }, }, } as unknown as GlobalStylesConfig; const blockSelectors = { 'core/button': { selector: '.wp-block-button .wp-block-button__link', featureSelectors: { dimensions: { root: '.wp-block-button', width: '.wp-block-button', }, }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors ); expect( result ).toContain( ':root :where(.wp-block-button){width: calc(50 * 1% - (var(--wp--style--block-gap, 0.5em) * (1 - 50 / 100)));}' ); } ); it( 'should not convert non-percentage width', () => { const tree: GlobalStylesConfig = { settings: {}, styles: { blocks: { 'core/button': { dimensions: { width: '200px', }, }, }, }, }; const blockSelectors = { 'core/button': { selector: '.wp-block-button .wp-block-button__link', featureSelectors: { dimensions: { root: '.wp-block-button', width: '.wp-block-button', }, }, }, }; const result = transformToStyles( Object.freeze( tree ), blockSelectors ); expect( result ).toContain( ':root :where(.wp-block-button){width: 200px;}' ); } ); } ); } );