diff --git a/backport-changelog/6.9/10190.md b/backport-changelog/6.9/10190.md new file mode 100644 index 00000000000000..248656d33cfabd --- /dev/null +++ b/backport-changelog/6.9/10190.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/10190 + +* https://github.com/WordPress/gutenberg/pull/71904 diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 411c02135ff22f..74d810290a9084 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -409,7 +409,7 @@ Introduce new sections and organize content to help visitors (and search engines - **Name:** core/heading - **Category:** text -- **Supports:** __unstablePasteTextInline, align (full, wide), anchor, className, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), splitting, typography (fontSize, lineHeight) +- **Supports:** __unstablePasteTextInline, align (full, wide), anchor, className, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), splitting, typography (fitText, fontSize, lineHeight) - **Attributes:** content, level, levelOptions, placeholder, textAlign ## Home Link @@ -581,7 +581,7 @@ Start with the basic building block of all narrative. ([Source](https://github.c - **Name:** core/paragraph - **Category:** text -- **Supports:** __unstablePasteTextInline, anchor, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), splitting, typography (fontSize, lineHeight), ~~className~~ +- **Supports:** __unstablePasteTextInline, anchor, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), splitting, typography (fitText, fontSize, lineHeight), ~~className~~ - **Attributes:** align, content, direction, dropCap, placeholder ## Pattern Placeholder diff --git a/lib/block-supports/typography.php b/lib/block-supports/typography.php index a4719b7bdd4099..3e21b1555c27fb 100644 --- a/lib/block-supports/typography.php +++ b/lib/block-supports/typography.php @@ -244,6 +244,10 @@ function gutenberg_typography_get_preset_inline_style_value( $style_value, $css_ * @return string Filtered block content. */ function gutenberg_render_typography_support( $block_content, $block ) { + if ( ! empty( $block['attrs']['fitText'] ) ) { + wp_enqueue_script_module( '@wordpress/block-editor/utils/fit-text-frontend' ); + } + if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) { return $block_content; } diff --git a/packages/block-editor/package.json b/packages/block-editor/package.json index 0628b480c3f6f0..687226c02b8271 100644 --- a/packages/block-editor/package.json +++ b/packages/block-editor/package.json @@ -27,6 +27,9 @@ "module": "build-module/index.js", "react-native": "src/index", "wpScript": true, + "wpScriptModuleExports": { + "./utils/fit-text-frontend": "./build-module/utils/fit-text-frontend.js" + }, "sideEffects": [ "build-style/**", "src/**/*.scss", diff --git a/packages/block-editor/src/hooks/fit-text.js b/packages/block-editor/src/hooks/fit-text.js new file mode 100644 index 00000000000000..69fab7282bbde8 --- /dev/null +++ b/packages/block-editor/src/hooks/fit-text.js @@ -0,0 +1,268 @@ +/** + * WordPress dependencies + */ +import { addFilter } from '@wordpress/hooks'; +import { hasBlockSupport } from '@wordpress/blocks'; +import { useEffect, useCallback } from '@wordpress/element'; +import { useSelect } from '@wordpress/data'; +import { __ } from '@wordpress/i18n'; +import { + ToggleControl, + __experimentalToolsPanelItem as ToolsPanelItem, +} from '@wordpress/components'; + +/** + * Internal dependencies + */ +import { optimizeFitText } from '../utils/fit-text-utils'; +import { store as blockEditorStore } from '../store'; +import { useBlockElement } from '../components/block-list/use-block-props/use-block-refs'; +import InspectorControls from '../components/inspector-controls'; + +export const FIT_TEXT_SUPPORT_KEY = 'typography.fitText'; + +/** + * Filters registered block settings, extending attributes to include + * the `fitText` attribute. + * + * @param {Object} settings Original block settings. + * @return {Object} Filtered block settings. + */ +function addAttributes( settings ) { + if ( ! hasBlockSupport( settings, FIT_TEXT_SUPPORT_KEY ) ) { + return settings; + } + + // Allow blocks to specify their own attribute definition. + if ( settings.attributes?.fitText ) { + return settings; + } + + // Add fitText attribute. + return { + ...settings, + attributes: { + ...settings.attributes, + fitText: { + type: 'boolean', + }, + }, + }; +} + +/** + * Custom hook to handle fit text functionality in the editor. + * + * @param {Object} props Component props. + * @param {?boolean} props.fitText Fit text attribute. + * @param {string} props.name Block name. + * @param {string} props.clientId Block client ID. + */ +function useFitText( { fitText, name, clientId } ) { + const hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ); + const blockElement = useBlockElement( clientId ); + + // Monitor block attribute changes + // Any attribute may change the available space. + const blockAttributes = useSelect( + ( select ) => { + if ( ! clientId ) { + return; + } + return select( blockEditorStore ).getBlockAttributes( clientId ); + }, + [ clientId ] + ); + + const applyFitText = useCallback( () => { + if ( ! blockElement || ! hasFitTextSupport || ! fitText ) { + return; + } + + // Get or create style element with unique ID + const styleId = `fit-text-${ clientId }`; + let styleElement = blockElement.ownerDocument.getElementById( styleId ); + if ( ! styleElement ) { + styleElement = blockElement.ownerDocument.createElement( 'style' ); + styleElement.id = styleId; + blockElement.ownerDocument.head.appendChild( styleElement ); + } + + const blockSelector = `#block-${ clientId }`; + + const applyStylesFn = ( css ) => { + styleElement.textContent = css; + }; + + optimizeFitText( blockElement, blockSelector, applyStylesFn ); + }, [ blockElement, clientId, hasFitTextSupport, fitText ] ); + + useEffect( () => { + if ( + ! fitText || + ! blockElement || + ! clientId || + ! hasFitTextSupport + ) { + return; + } + + // Apply initially + applyFitText(); + + // Store current element value for cleanup + const currentElement = blockElement; + + // Watch for size changes + let resizeObserver; + if ( window.ResizeObserver && currentElement.parentElement ) { + resizeObserver = new window.ResizeObserver( applyFitText ); + resizeObserver.observe( currentElement.parentElement ); + } + + // Cleanup function + return () => { + if ( resizeObserver ) { + resizeObserver.disconnect(); + } + + const styleId = `fit-text-${ clientId }`; + const styleElement = + currentElement.ownerDocument.getElementById( styleId ); + if ( styleElement ) { + styleElement.remove(); + } + }; + }, [ fitText, clientId, applyFitText, blockElement, hasFitTextSupport ] ); + + // Trigger fit text recalculation when content changes + useEffect( () => { + if ( fitText && blockElement && hasFitTextSupport ) { + // Small delay to ensure DOM has updated after content changes + const timer = setTimeout( () => { + if ( blockElement ) { + applyFitText(); + } + }, 1000 ); + + return () => clearTimeout( timer ); + } + }, [ + blockAttributes, + fitText, + applyFitText, + blockElement, + hasFitTextSupport, + ] ); +} + +/** + * Fit text control component for the typography panel. + * + * @param {Object} props Component props. + * @param {string} props.clientId Block client ID. + * @param {Function} props.setAttributes Function to set block attributes. + * @param {string} props.name Block name. + * @param {boolean} props.fitText Whether fit text is enabled. + */ +export function FitTextControl( { + clientId, + fitText = false, + setAttributes, + name, +} ) { + if ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) { + return null; + } + return ( + + fitText } + label={ __( 'Fit text' ) } + onDeselect={ () => setAttributes( { fitText: undefined } ) } + resetAllFilter={ () => ( { fitText: undefined } ) } + panelId={ clientId } + > + + setAttributes( { fitText: ! fitText || undefined } ) + } + help={ + fitText + ? __( 'Text will resize to fit its container.' ) + : __( 'Resize text to fit its container.' ) + } + /> + + + ); +} + +/** + * Override props applied to the block element on save. + * + * @param {Object} props Additional props applied to the block element. + * @param {Object} blockType Block type. + * @param {Object} attributes Block attributes. + * @return {Object} Filtered props applied to the block element. + */ +function addSaveProps( props, blockType, attributes ) { + if ( ! hasBlockSupport( blockType, FIT_TEXT_SUPPORT_KEY ) ) { + return props; + } + + const { fitText } = attributes; + + if ( ! fitText ) { + return props; + } + + // Add CSS class for frontend detection and styling + const className = props.className + ? `${ props.className } has-fit-text` + : 'has-fit-text'; + + return { + ...props, + className, + }; +} +/** + * Override props applied to the block element in the editor. + * + * @param {Object} props Component props including block attributes. + * @param {string} props.name Block name. + * @param {boolean} props.fitText Whether fit text is enabled. + * @param {string} props.clientId Block client ID. + * @return {Object} Filtered props applied to the block element. + */ +function useBlockProps( { name, fitText, clientId } ) { + useFitText( { fitText, name, clientId } ); + if ( ! fitText || ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) { + return {}; + } + return { + className: 'has-fit-text', + }; +} + +addFilter( + 'blocks.registerBlockType', + 'core/fit-text/addAttribute', + addAttributes +); + +const hasFitTextSupport = ( blockNameOrType ) => { + return hasBlockSupport( blockNameOrType, FIT_TEXT_SUPPORT_KEY ); +}; + +export default { + useBlockProps, + addSaveProps, + attributeKeys: [ 'fitText' ], + hasSupport: hasFitTextSupport, + edit: FitTextControl, +}; diff --git a/packages/block-editor/src/hooks/index.js b/packages/block-editor/src/hooks/index.js index 7f9b29376ad1fb..37aab8d27ef185 100644 --- a/packages/block-editor/src/hooks/index.js +++ b/packages/block-editor/src/hooks/index.js @@ -22,6 +22,7 @@ import duotone from './duotone'; import fontFamily from './font-family'; import fontSize from './font-size'; import textAlign from './text-align'; +import fitText from './fit-text'; import border from './border'; import position from './position'; import blockStyleVariation from './block-style-variation'; @@ -42,6 +43,7 @@ createBlockEditFilter( customClassName, style, duotone, + fitText, position, layout, contentLockUI, @@ -60,6 +62,7 @@ createBlockListBlockFilter( [ duotone, fontFamily, fontSize, + fitText, border, position, blockStyleVariation, @@ -72,6 +75,7 @@ createBlockSaveFilter( [ ariaLabel, customClassName, border, + fitText, color, style, fontFamily, diff --git a/packages/block-editor/src/hooks/typography.js b/packages/block-editor/src/hooks/typography.js index 2607f6d9301067..066211c15aafe9 100644 --- a/packages/block-editor/src/hooks/typography.js +++ b/packages/block-editor/src/hooks/typography.js @@ -18,6 +18,7 @@ import { LINE_HEIGHT_SUPPORT_KEY } from './line-height'; import { FONT_FAMILY_SUPPORT_KEY } from './font-family'; import { FONT_SIZE_SUPPORT_KEY } from './font-size'; import { TEXT_ALIGN_SUPPORT_KEY } from './text-align'; +import { FIT_TEXT_SUPPORT_KEY } from './fit-text'; import { cleanEmptyObject } from './utils'; import { store as blockEditorStore } from '../store'; @@ -47,6 +48,7 @@ export const TYPOGRAPHY_SUPPORT_KEYS = [ WRITING_MODE_SUPPORT_KEY, TEXT_TRANSFORM_SUPPORT_KEY, LETTER_SPACING_SUPPORT_KEY, + FIT_TEXT_SUPPORT_KEY, ]; function styleToAttributes( style ) { diff --git a/packages/block-editor/src/utils/fit-text-frontend.js b/packages/block-editor/src/utils/fit-text-frontend.js new file mode 100644 index 00000000000000..d8095643aad502 --- /dev/null +++ b/packages/block-editor/src/utils/fit-text-frontend.js @@ -0,0 +1,84 @@ +/** + * Frontend fit text functionality. + * Automatically detects and initializes fit text on blocks with the has-fit-text class. + */ + +/** + * Internal dependencies + */ +import { optimizeFitText } from './fit-text-utils'; + +/** + * Counter for generating unique element IDs. + */ +let idCounter = 0; + +/** + * Get or create a unique style element for a fit text element. + * + * @param {string} elementId Unique identifier for the element. + * @return {HTMLElement} Style element. + */ +function getOrCreateStyleElement( elementId ) { + const styleId = `fit-text-${ elementId }`; + let styleElement = document.getElementById( styleId ); + if ( ! styleElement ) { + styleElement = document.createElement( 'style' ); + styleElement.id = styleId; + document.head.appendChild( styleElement ); + } + return styleElement; +} + +/** + * Generate a unique identifier for a fit text element. + * + * @param {HTMLElement} element The element to identify. + * @return {string} Unique identifier. + */ +function getElementIdentifier( element ) { + if ( ! element.dataset.fitTextId ) { + element.dataset.fitTextId = `fit-text-${ ++idCounter }`; + } + return element.dataset.fitTextId; +} + +/** + * Initialize fit text functionality for a single element. + * + * @param {HTMLElement} element Element with fit text enabled. + */ +function initializeFitText( element ) { + const elementId = getElementIdentifier( element ); + + const applyFitText = () => { + const styleElement = getOrCreateStyleElement( elementId ); + const elementSelector = `[data-fit-text-id=\"${ elementId }\"]`; + + // Style management callback + const applyStylesFn = ( css ) => { + styleElement.textContent = css; + }; + + optimizeFitText( element, elementSelector, applyStylesFn ); + }; + + // Initial sizing + applyFitText(); + + // Watch for parent container resize + if ( window.ResizeObserver && element.parentElement ) { + const resizeObserver = new window.ResizeObserver( applyFitText ); + resizeObserver.observe( element.parentElement ); + } +} + +/** + * Initialize fit text on all elements with the has-fit-text class. + */ +function initializeAllFitText() { + const elements = document.querySelectorAll( '.has-fit-text' ); + elements.forEach( initializeFitText ); +} + +window.addEventListener( 'load', initializeAllFitText ); diff --git a/packages/block-editor/src/utils/fit-text-utils.js b/packages/block-editor/src/utils/fit-text-utils.js new file mode 100644 index 00000000000000..64b128171559c2 --- /dev/null +++ b/packages/block-editor/src/utils/fit-text-utils.js @@ -0,0 +1,75 @@ +/** + * Shared utility functions for fit text functionality. + * Uses callback-based approach for maximum code reuse between editor and frontend. + */ + +/** + * Generate CSS rule for single text element. + * + * @param {string} elementSelector CSS selector for the text element + * @param {number} fontSize Font size in pixels + * @return {string} CSS rule string + */ +function generateCSSRule( elementSelector, fontSize ) { + return `${ elementSelector } { font-size: ${ fontSize }px !important; }`; +} + +/** + * Find optimal font size using simple binary search between 5-600px. + * + * @param {HTMLElement} textElement The text element + * @param {string} elementSelector CSS selector for the text element + * @param {Function} applyStylesFn Function to apply test styles + * @return {number} Optimal font size + */ +function findOptimalFontSize( textElement, elementSelector, applyStylesFn ) { + const alreadyHasScrollableHeight = + textElement.scrollHeight > textElement.clientHeight; + let minSize = 5; + let maxSize = 600; + let bestSize = minSize; + + while ( minSize <= maxSize ) { + const midSize = Math.floor( ( minSize + maxSize ) / 2 ); + applyStylesFn( generateCSSRule( elementSelector, midSize ) ); + + const fitsWidth = textElement.scrollWidth <= textElement.clientWidth; + const fitsHeight = + alreadyHasScrollableHeight || + textElement.scrollHeight <= textElement.clientHeight; + + if ( fitsWidth && fitsHeight ) { + bestSize = midSize; + minSize = midSize + 1; + } else { + maxSize = midSize - 1; + } + } + + return bestSize; +} + +/** + * Complete fit text optimization for a single text element. + * Handles the full flow using callbacks for style management. + * + * @param {HTMLElement} textElement The text element (paragraph, heading, etc.) + * @param {string} elementSelector CSS selector for the text element + * @param {Function} applyStylesFn Function to apply CSS styles (pass empty string to clear) + */ +export function optimizeFitText( textElement, elementSelector, applyStylesFn ) { + if ( ! textElement ) { + return; + } + + applyStylesFn( '' ); + + const optimalSize = findOptimalFontSize( + textElement, + elementSelector, + applyStylesFn + ); + + const cssRule = generateCSSRule( elementSelector, optimalSize ); + applyStylesFn( cssRule ); +} diff --git a/packages/block-library/src/common.scss b/packages/block-library/src/common.scss index 15c2cd52b76aad..f4791a9d58b83a 100644 --- a/packages/block-library/src/common.scss +++ b/packages/block-library/src/common.scss @@ -46,6 +46,11 @@ text-align: right; } +// Fit Text +.has-fit-text { + white-space: nowrap !important; +} + // This tag marks the end of the styles that apply to editing canvas contents and need to be manipulated when we resize the editor. #end-resizable-editor-section { display: none; diff --git a/packages/block-library/src/heading/block.json b/packages/block-library/src/heading/block.json index 2869ee85c55206..d9488a3156528d 100644 --- a/packages/block-library/src/heading/block.json +++ b/packages/block-library/src/heading/block.json @@ -65,6 +65,7 @@ "__experimentalTextTransform": true, "__experimentalTextDecoration": true, "__experimentalWritingMode": true, + "fitText": true, "__experimentalDefaultControls": { "fontSize": true } diff --git a/packages/block-library/src/heading/index.js b/packages/block-library/src/heading/index.js index d591dc94ed97d9..2ab252c6c9c18f 100644 --- a/packages/block-library/src/heading/index.js +++ b/packages/block-library/src/heading/index.js @@ -13,6 +13,7 @@ import edit from './edit'; import metadata from './block.json'; import save from './save'; import transforms from './transforms'; +import variations from './variations'; const { name } = metadata; @@ -65,6 +66,7 @@ export const settings = { }, edit, save, + variations, }; export const init = () => initBlock( { name, metadata, settings } ); diff --git a/packages/block-library/src/heading/variations.js b/packages/block-library/src/heading/variations.js new file mode 100644 index 00000000000000..e1778749ccea7f --- /dev/null +++ b/packages/block-library/src/heading/variations.js @@ -0,0 +1,29 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { textColor } from '@wordpress/icons'; + +const variations = [ + { + name: 'Stretchy Heading', + title: __( 'Stretchy Heading' ), + description: __( + 'Heading text automatically resizes to fit the container width.' + ), + attributes: { fitText: true }, + scope: [ 'inserter' ], + isActive: ( blockAttributes ) => blockAttributes.fitText, + icon: textColor, + example: { + attributes: { + content: __( + 'This heading will automatically resize to fit its container width.' + ), + fitText: true, + }, + }, + }, +]; + +export default variations; diff --git a/packages/block-library/src/paragraph/block.json b/packages/block-library/src/paragraph/block.json index 7e004019cbf282..9617627ef5d0da 100644 --- a/packages/block-library/src/paragraph/block.json +++ b/packages/block-library/src/paragraph/block.json @@ -65,6 +65,7 @@ "__experimentalLetterSpacing": true, "__experimentalTextTransform": true, "__experimentalWritingMode": true, + "fitText": true, "__experimentalDefaultControls": { "fontSize": true } diff --git a/packages/block-library/src/paragraph/index.js b/packages/block-library/src/paragraph/index.js index 715fb35ec05ab1..a2328b1ecdf82d 100644 --- a/packages/block-library/src/paragraph/index.js +++ b/packages/block-library/src/paragraph/index.js @@ -13,6 +13,7 @@ import edit from './edit'; import metadata from './block.json'; import save from './save'; import transforms from './transforms'; +import variations from './variations'; const { name } = metadata; @@ -54,6 +55,7 @@ export const settings = { }, edit, save, + variations, }; export const init = () => initBlock( { name, metadata, settings } ); diff --git a/packages/block-library/src/paragraph/variations.js b/packages/block-library/src/paragraph/variations.js new file mode 100644 index 00000000000000..ddb4f436987159 --- /dev/null +++ b/packages/block-library/src/paragraph/variations.js @@ -0,0 +1,29 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { textColor } from '@wordpress/icons'; + +const variations = [ + { + name: 'Stretchy Paragraph', + title: __( 'Stretchy Paragraph' ), + description: __( + 'Text automatically resizes to fit the container width.' + ), + attributes: { fitText: true }, + scope: [ 'inserter' ], + isActive: ( blockAttributes ) => blockAttributes.fitText, + icon: textColor, + example: { + attributes: { + content: __( + 'This text will automatically resize to fit its container width.' + ), + fitText: true, + }, + }, + }, +]; + +export default variations; diff --git a/test/e2e/specs/editor/blocks/details.spec.js b/test/e2e/specs/editor/blocks/details.spec.js index 2b7e954b6eeaad..da54e99f3d063f 100644 --- a/test/e2e/specs/editor/blocks/details.spec.js +++ b/test/e2e/specs/editor/blocks/details.spec.js @@ -123,6 +123,7 @@ test.describe( 'Details', () => { await expect( listView.getByRole( 'link', { name: 'Paragraph', + exact: true, } ) ).toBeVisible(); @@ -131,6 +132,7 @@ test.describe( 'Details', () => { await expect( listView.getByRole( 'link', { name: 'Paragraph', + exact: true, } ) ).toBeFocused(); diff --git a/test/e2e/specs/editor/blocks/list.spec.js b/test/e2e/specs/editor/blocks/list.spec.js index 2736c6c645c2f9..5b51ad0c44708b 100644 --- a/test/e2e/specs/editor/blocks/list.spec.js +++ b/test/e2e/specs/editor/blocks/list.spec.js @@ -1422,7 +1422,9 @@ test.describe( 'List (@firefox)', () => { ); await page.getByRole( 'button', { name: 'List', exact: true } ).click(); - await page.getByRole( 'menuitem', { name: 'Paragraph' } ).click(); + await page + .getByRole( 'menuitem', { name: 'Paragraph', exact: true } ) + .click(); expect( await editor.getEditedPostContent() ) .toBe( ` diff --git a/test/e2e/specs/editor/plugins/allowed-blocks.spec.js b/test/e2e/specs/editor/plugins/allowed-blocks.spec.js index ed7490afe84fe9..e7939ca90685cc 100644 --- a/test/e2e/specs/editor/plugins/allowed-blocks.spec.js +++ b/test/e2e/specs/editor/plugins/allowed-blocks.spec.js @@ -33,7 +33,7 @@ test.describe( 'Allowed Blocks Filter', () => { await searchbox.fill( 'Paragraph' ); await expect( - page.getByRole( 'option', { name: 'Paragraph' } ) + page.getByRole( 'option', { name: 'Paragraph', exact: true } ) ).toBeVisible(); // The gallery block is not available. diff --git a/test/e2e/specs/editor/plugins/block-variations.spec.js b/test/e2e/specs/editor/plugins/block-variations.spec.js index 2b26aeb0404eaa..c1d90f49a28c42 100644 --- a/test/e2e/specs/editor/plugins/block-variations.spec.js +++ b/test/e2e/specs/editor/plugins/block-variations.spec.js @@ -77,7 +77,12 @@ test.describe( 'Block variations', () => { page .getByRole( 'listbox', { name: 'Blocks' } ) .getByRole( 'option' ) - ).toHaveText( [ 'Paragraph', 'Success Message', 'Warning Message' ] ); + ).toHaveText( [ + 'Paragraph', + 'Stretchy Paragraph', + 'Success Message', + 'Warning Message', + ] ); } ); test( 'Insert the Success Message block variation', async ( { @@ -88,7 +93,9 @@ test.describe( 'Block variations', () => { .locator( 'role=button[name="Add default block"i]' ) .click(); await page.keyboard.type( '/Heading' ); - await page.getByRole( 'option', { name: 'Heading' } ).click(); + await page + .getByRole( 'option', { name: 'Heading', exact: true } ) + .click(); await page.keyboard.type( '/Success Message' ); await page.getByRole( 'option', { name: 'Success Message' } ).click(); @@ -167,7 +174,9 @@ test.describe( 'Block variations', () => { .locator( 'role=button[name="Add default block"i]' ) .click(); await page.keyboard.type( '/Heading' ); - await page.getByRole( 'option', { name: 'Heading' } ).click(); + await page + .getByRole( 'option', { name: 'Heading', exact: true } ) + .click(); await page.keyboard.type( '/Success Message' ); await page.getByRole( 'option', { name: 'Success Message' } ).click(); @@ -203,7 +212,9 @@ test.describe( 'Block variations', () => { .locator( 'role=button[name="Add default block"i]' ) .click(); await page.keyboard.type( '/Heading' ); - await page.getByRole( 'option', { name: 'Heading' } ).click(); + await page + .getByRole( 'option', { name: 'Heading', exact: true } ) + .click(); await page.keyboard.type( '/Warning Message' ); await page.getByRole( 'option', { name: 'Warning Message' } ).click(); diff --git a/test/e2e/specs/editor/plugins/child-blocks.spec.js b/test/e2e/specs/editor/plugins/child-blocks.spec.js index 765b4040a4b976..eff8c1cd207d25 100644 --- a/test/e2e/specs/editor/plugins/child-blocks.spec.js +++ b/test/e2e/specs/editor/plugins/child-blocks.spec.js @@ -98,6 +98,7 @@ test.describe( 'Child Blocks', () => { await expect( blockLibrary ).toBeVisible(); await expect( blockLibrary.getByRole( 'option' ) ).toHaveText( [ 'Paragraph', + 'Stretchy Paragraph', 'Child Blocks Child', 'Image', ] ); diff --git a/test/e2e/specs/editor/plugins/inner-blocks-allowed-blocks.spec.js b/test/e2e/specs/editor/plugins/inner-blocks-allowed-blocks.spec.js index 4ee6d7a0fc4fec..b1b681a8003e99 100644 --- a/test/e2e/specs/editor/plugins/inner-blocks-allowed-blocks.spec.js +++ b/test/e2e/specs/editor/plugins/inner-blocks-allowed-blocks.spec.js @@ -105,7 +105,9 @@ test.describe( 'Allowed Blocks Setting on InnerBlocks', () => { await expect( blockLibrary ).toBeVisible(); await expect( blockLibrary.getByRole( 'option' ) ).toHaveText( [ 'Paragraph', + 'Stretchy Paragraph', 'Heading', + 'Stretchy Heading', 'Image', ] ); } ); diff --git a/test/e2e/specs/editor/plugins/post-type-locking.spec.js b/test/e2e/specs/editor/plugins/post-type-locking.spec.js index 68cbdfb89f4537..452a4d19bcc870 100644 --- a/test/e2e/specs/editor/plugins/post-type-locking.spec.js +++ b/test/e2e/specs/editor/plugins/post-type-locking.spec.js @@ -337,7 +337,7 @@ test.describe( 'Post-type locking', () => { const blockSwitcher = page .getByRole( 'toolbar', { name: 'Block tools' } ) - .getByRole( 'button', { name: 'Paragraph' } ); + .getByRole( 'button', { name: 'Paragraph', exact: true } ); // Verify the block switcher exists. await expect( blockSwitcher ).toHaveAttribute( @@ -349,7 +349,7 @@ test.describe( 'Post-type locking', () => { await blockSwitcher.click(); await expect( page - .getByRole( 'menu', { name: 'Paragraph' } ) + .getByRole( 'menu', { name: 'Paragraph', exact: true } ) .getByRole( 'menuitem' ) ).toHaveText( [ 'Heading', diff --git a/test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js b/test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js index 7143f3dfc96b7b..ccfae994a4cfbf 100644 --- a/test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js +++ b/test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js @@ -571,7 +571,7 @@ test.describe( 'Autocomplete (@firefox, @webkit)', () => { // Get the assertive live region screen reader announcement. await expect( page.getByText( - '3 results found, use up and down arrow keys to navigate.' + '4 results found, use up and down arrow keys to navigate.' ) ).toBeVisible(); } ); diff --git a/test/e2e/specs/editor/various/block-hierarchy-navigation.spec.js b/test/e2e/specs/editor/various/block-hierarchy-navigation.spec.js index f0bfe5bff203fb..5e2c19645bd61e 100644 --- a/test/e2e/specs/editor/various/block-hierarchy-navigation.spec.js +++ b/test/e2e/specs/editor/various/block-hierarchy-navigation.spec.js @@ -53,7 +53,10 @@ test.describe( 'Navigating the block hierarchy', () => { await page.keyboard.press( 'Enter' ); // Add a paragraph in the first column. - const paragraph = page.getByRole( 'option', { name: 'Paragraph' } ); + const paragraph = page.getByRole( 'option', { + name: 'Paragraph', + exact: true, + } ); await expect( paragraph ).toBeVisible(); await paragraph.click(); await page.keyboard.type( 'First column' ); @@ -108,7 +111,10 @@ test.describe( 'Navigating the block hierarchy', () => { await page.keyboard.press( 'Enter' ); // Add a paragraph in the first column. - const paragraph = page.getByRole( 'option', { name: 'Paragraph' } ); + const paragraph = page.getByRole( 'option', { + name: 'Paragraph', + exact: true, + } ); await expect( paragraph ).toBeVisible(); await paragraph.click(); await page.keyboard.type( 'First column' ); @@ -204,7 +210,10 @@ test.describe( 'Navigating the block hierarchy', () => { await page.keyboard.press( 'Enter' ); // Add some random blocks. - const paragraph = page.getByRole( 'option', { name: 'Paragraph' } ); + const paragraph = page.getByRole( 'option', { + name: 'Paragraph', + exact: true, + } ); await expect( paragraph ).toBeVisible(); await paragraph.click(); await page.keyboard.type( 'just a paragraph' ); diff --git a/test/e2e/specs/editor/various/block-switcher.spec.js b/test/e2e/specs/editor/various/block-switcher.spec.js index cb95c4d395c664..490ca7483d17fe 100644 --- a/test/e2e/specs/editor/various/block-switcher.spec.js +++ b/test/e2e/specs/editor/various/block-switcher.spec.js @@ -103,7 +103,7 @@ test.describe( 'Block Switcher', () => { await pageUtils.pressKeys( 'alt+F10' ); const button = page .getByRole( 'toolbar', { name: 'Block tools' } ) - .getByRole( 'button', { name: 'Paragraph' } ); + .getByRole( 'button', { name: 'Paragraph', exact: true } ); await expect( button ).toBeEnabled(); await editor.clickBlockOptionsMenuItem( 'Lock' ); diff --git a/test/e2e/specs/editor/various/block-visibility.spec.js b/test/e2e/specs/editor/various/block-visibility.spec.js index 6e4163f4ed5839..7a62916f9c2c8c 100644 --- a/test/e2e/specs/editor/various/block-visibility.spec.js +++ b/test/e2e/specs/editor/various/block-visibility.spec.js @@ -96,7 +96,7 @@ test.describe( 'Block Visibility', () => { await expect( page .getByRole( 'tabpanel', { name: 'Blocks' } ) - .getByRole( 'option', { name: 'Heading' } ), + .getByRole( 'option', { name: 'Heading', exact: true } ), 'Heading block should be visible' ).toBeVisible(); } ); diff --git a/test/e2e/specs/editor/various/inserting-blocks.spec.js b/test/e2e/specs/editor/various/inserting-blocks.spec.js index 4de9f736ceafa1..cd6d7ae2b743fc 100644 --- a/test/e2e/specs/editor/various/inserting-blocks.spec.js +++ b/test/e2e/specs/editor/various/inserting-blocks.spec.js @@ -512,7 +512,7 @@ test.describe( 'Inserting blocks (@firefox, @webkit)', () => { .click(); await page .getByRole( 'listbox', { name: 'Text' } ) - .getByRole( 'option', { name: 'Heading' } ) + .getByRole( 'option', { name: 'Heading', exact: true } ) .hover(); await expect( insertingBlocksUtils.indicator ).toBeVisible(); @@ -553,7 +553,7 @@ test.describe( 'Inserting blocks (@firefox, @webkit)', () => { await page.getByRole( 'button', { name: 'Browse All' } ).click(); await page .getByRole( 'listbox', { name: 'Text' } ) - .getByRole( 'option', { name: 'Paragraph' } ) + .getByRole( 'option', { name: 'Paragraph', exact: true } ) .click(); await editor.canvas .getByRole( 'document', { name: 'Empty block' } ) @@ -655,7 +655,7 @@ test.describe( 'Inserting blocks (@firefox, @webkit)', () => { .click(); await page .getByRole( 'listbox', { name: 'Text' } ) - .getByRole( 'option', { name: 'Paragraph' } ) + .getByRole( 'option', { name: 'Paragraph', exact: true } ) .hover(); await expect( diff --git a/test/e2e/specs/editor/various/list-view.spec.js b/test/e2e/specs/editor/various/list-view.spec.js index 2d5c0a9ee67298..7cc306b4464629 100644 --- a/test/e2e/specs/editor/various/list-view.spec.js +++ b/test/e2e/specs/editor/various/list-view.spec.js @@ -1163,7 +1163,7 @@ test.describe( 'List View', () => { window.wp.data.dispatch( 'core/block-editor' ).clearSelectedBlock() ); await listView - .getByRole( 'gridcell', { name: 'Paragraph' } ) + .getByRole( 'gridcell', { name: 'Paragraph', exact: true } ) .getByRole( 'link' ) .focus(); await expect diff --git a/test/e2e/specs/editor/various/navigable-toolbar.spec.js b/test/e2e/specs/editor/various/navigable-toolbar.spec.js index 0d087536530e69..a87eef6a056f01 100644 --- a/test/e2e/specs/editor/various/navigable-toolbar.spec.js +++ b/test/e2e/specs/editor/various/navigable-toolbar.spec.js @@ -140,7 +140,7 @@ test.describe( 'Block Toolbar', () => { await expect( page .getByRole( 'toolbar', { name: 'Block Tools' } ) - .getByRole( 'button', { name: 'Paragraph' } ) + .getByRole( 'button', { name: 'Paragraph', exact: true } ) ).toBeFocused(); } ); diff --git a/test/e2e/specs/widgets/editing-widgets.spec.js b/test/e2e/specs/widgets/editing-widgets.spec.js index 019e07fe87daac..bb1734ffaf3dcf 100644 --- a/test/e2e/specs/widgets/editing-widgets.spec.js +++ b/test/e2e/specs/widgets/editing-widgets.spec.js @@ -162,6 +162,7 @@ test.describe( 'Widgets screen', () => { const paragraphBlock = inlineQuickInserter.getByRole( 'option', { name: 'Paragraph', + exact: true, } ); await paragraphBlock.click(); @@ -214,7 +215,7 @@ test.describe( 'Widgets screen', () => { await page.keyboard.type( 'Heading' ); await inlineQuickInserter - .getByRole( 'option', { name: 'Heading' } ) + .getByRole( 'option', { name: 'Heading', exact: true } ) .click(); await expect( @@ -708,7 +709,10 @@ class WidgetsScreen { } ) .fill( blockName ); - return blockLibrary.getByRole( 'option', { name: blockName } ); + return blockLibrary.getByRole( 'option', { + name: blockName, + exact: true, + } ); }; saveWidgets = async () => {