const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); test.use( { writingFlowUtils: async ( { page, editor }, use ) => { await use( new WritingFlowUtils( { page, editor } ) ); }, } ); test.describe( 'Writing Flow (@firefox, @webkit)', () => { test.beforeEach( async ( { admin, editor } ) => { await admin.createNewPost(); await expect( editor.canvas.getByRole( 'textbox', { name: 'Add title' } ) ).toBeFocused(); } ); test.afterAll( async ( { requestUtils } ) => { await requestUtils.deleteAllPosts(); } ); test( 'Should navigate inner blocks with arrow keys', async ( { editor, page, writingFlowUtils, } ) => { // Assertions are made both against the active DOM element and the // editor state, in order to protect against potential disparities. // // See: https://github.com/WordPress/gutenberg/issues/18928 await writingFlowUtils.addDemoContent(); // The element owning the caret: the focused element, or the selected // block while a focused editing host owns the selection. const activeElementLocator = editor.canvas.locator( 'body:not(:focus) :focus, body:focus .is-selected' ); // Arrow up into nested context focuses last text input. await page.keyboard.press( 'ArrowUp' ); await expect .poll( writingFlowUtils.getActiveBlockName ) .toBe( 'core/paragraph' ); await expect( activeElementLocator ).toHaveCount( 1 ); await expect( activeElementLocator ).toHaveText( '2nd col' ); // Arrow up in inner blocks should navigate through (1) column wrapper, // (2) text fields. await page.keyboard.press( 'ArrowUp' ); await expect .poll( writingFlowUtils.getActiveBlockName ) .toBe( 'core/column' ); await page.keyboard.press( 'ArrowUp' ); await expect .poll( writingFlowUtils.getActiveBlockName ) .toBe( 'core/columns' ); // Arrow up from focused (columns) block wrapper exits nested context // to prior text input. await page.keyboard.press( 'ArrowUp' ); await expect .poll( writingFlowUtils.getActiveBlockName ) .toBe( 'core/paragraph' ); await expect( activeElementLocator ).toHaveCount( 1 ); await expect( activeElementLocator ).toHaveText( 'First paragraph' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'First paragraph' }, }, { name: 'core/columns', attributes: {}, innerBlocks: [ { name: 'core/column', attributes: {}, innerBlocks: [ { name: 'core/paragraph', attributes: { content: '1st col' }, }, ], }, { name: 'core/column', attributes: {}, innerBlocks: [ { name: 'core/paragraph', attributes: { content: '2nd col' }, }, ], }, ], }, { name: 'core/paragraph', attributes: { content: 'Second paragraph' }, }, ] ); } ); test( 'should not select list wrapper when pressing arrow up from list', async ( { editor, page, writingFlowUtils, } ) => { // Insert a paragraph block first. await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'First paragraph' }, } ); // Insert a list block. await editor.insertBlock( { name: 'core/list' } ); await page.keyboard.type( 'List item' ); // The caret is now inside the list item. // Press ArrowUp - should skip the list wrapper and go to the paragraph. await page.keyboard.press( 'ArrowUp' ); // Verify we're in the paragraph, NOT the list wrapper. await expect .poll( writingFlowUtils.getActiveBlockName ) .toBe( 'core/paragraph' ); // Verify the element owning the caret has the paragraph content. const activeElementLocator = editor.canvas.locator( 'body:not(:focus) :focus, body:focus .is-selected' ); await expect( activeElementLocator ).toHaveText( 'First paragraph' ); } ); test( 'should navigate around inline boundaries', async ( { editor, page, pageUtils, } ) => { // Add demo content. await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'First' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'Second' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'Third' ); // Navigate to second paragraph. await pageUtils.pressKeys( 'ArrowLeft', { times: 6 } ); // Bold second paragraph text. await page.keyboard.down( 'Shift' ); await pageUtils.pressKeys( 'ArrowLeft', { times: 6 } ); await page.keyboard.up( 'Shift' ); await pageUtils.pressKeys( 'primary+b' ); // Arrow left from selected bold should collapse to before the inline // boundary. Arrow once more to traverse into first paragraph. await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.type( 'After' ); // Arrow right from end of first should traverse to second, *BEFORE* // the bolded text. Another press should move within inline boundary. await pageUtils.pressKeys( 'ArrowRight', { times: 2 } ); await page.keyboard.type( 'Inside' ); // Arrow left from end of beginning of inline boundary should move to // the outside of the inline boundary. await pageUtils.pressKeys( 'ArrowLeft', { times: 6 } ); await page.keyboard.press( 'ArrowLeft' ); // Separate for emphasis. await page.keyboard.type( 'Before' ); // Likewise, test at the end of the inline boundary for same effect. await page.keyboard.press( 'ArrowRight' ); // Move inside await pageUtils.pressKeys( 'ArrowRight', { times: 12 } ); await page.keyboard.type( 'Inside' ); await page.keyboard.press( 'ArrowRight' ); // Edge case: Verify that workaround to test for ZWSP at beginning of // focus node does not take effect when on the right edge of inline // boundary (thus preventing traversing to the next block by arrow). await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'ArrowLeft' ); // Should be after the inline boundary again. await page.keyboard.type( 'After' ); // Finally, ensure that ArrowRight from end of unbolded text moves to // the last paragraph. await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'Before' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'FirstAfter' }, }, { name: 'core/paragraph', attributes: { content: 'BeforeInsideSecondInsideAfter', }, }, { name: 'core/paragraph', attributes: { content: 'BeforeThird' }, }, ] ); } ); test( 'should navigate around nested inline boundaries', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await pageUtils.pressKeys( 'primary+b' ); await page.keyboard.type( '1 2' ); await page.keyboard.down( 'Shift' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.up( 'Shift' ); await pageUtils.pressKeys( 'primary+i' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.down( 'Shift' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.up( 'Shift' ); await pageUtils.pressKeys( 'primary+i' ); await page.keyboard.press( 'ArrowLeft' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

1 2

` ); await page.keyboard.type( 'a' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'b' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'c' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'd' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'e' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'f' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'g' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'h' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'i' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( 'j' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

abc1de fg2hij

` ); } ); test( 'should insert line break at end', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'a' ); await pageUtils.pressKeys( 'shift+Enter' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

a

` ); } ); test( 'should insert line break at end and continue writing', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'a' ); await pageUtils.pressKeys( 'shift+Enter' ); await page.keyboard.type( 'b' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

a
b

` ); } ); test( 'should insert line break mid text', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'ab' ); await page.keyboard.press( 'ArrowLeft' ); await pageUtils.pressKeys( 'shift+Enter' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

a
b

` ); } ); test( 'should insert line break at start', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'a' ); await page.keyboard.press( 'ArrowLeft' ); await pageUtils.pressKeys( 'shift+Enter' ); await expect.poll( editor.getEditedPostContent ) .toBe( `


a

` ); } ); test( 'should insert line break in empty container', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await pageUtils.pressKeys( 'shift+Enter' ); await expect.poll( editor.getEditedPostContent ) .toBe( `


` ); } ); test( 'should navigate up over an empty line inside a block', async ( { editor, page, } ) => { await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'Paragraph block' }, } ); await editor.insertBlock( { name: 'core/preformatted' } ); await page.keyboard.type( 'Pre block' ); await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'Foo' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'Paragraph block' }, }, { name: 'core/preformatted', attributes: { content: 'Pre block

Foo' }, }, ] ); // The second ArrowUp starts on the empty line and must stop on the // first line, not leave the block. await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( 'X' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'Paragraph block' }, }, { name: 'core/preformatted', attributes: { content: 'PreX block

Foo' }, }, ] ); } ); test( 'should navigate up from an empty line at the end of a citation', async ( { editor, page, pageUtils, } ) => { await editor.insertBlock( { name: 'core/quote' } ); await page.keyboard.type( 'Quote text' ); await editor.clickBlockToolbarButton( 'Select parent block: Quote' ); await editor.clickBlockToolbarButton( 'Add citation' ); await editor.canvas .getByRole( 'textbox', { name: 'Quote citation' } ) .click(); await page.keyboard.type( 'Cite line' ); await pageUtils.pressKeys( 'shift+Enter' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/quote', attributes: { citation: 'Cite line
' }, innerBlocks: [ { name: 'core/paragraph', attributes: { content: 'Quote text' }, }, ], }, ] ); // ArrowUp from the empty last line must move to the citation's first // line, not into the quoted text. await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( 'X' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/quote', attributes: { citation: 'XCite line
' }, innerBlocks: [ { name: 'core/paragraph', attributes: { content: 'Quote text' }, }, ], }, ] ); } ); test( 'should not create extra line breaks in multiline value', async ( { editor, page, } ) => { await editor.insertBlock( { name: 'core/list' } ); await page.keyboard.type( 'a' ); await page.keyboard.press( 'Backspace' ); await expect.poll( editor.getEditedPostContent ).toBe( ` ` ); } ); // See: https://github.com/WordPress/gutenberg/issues/9626 test( 'should navigate native inputs vertically, not horizontally', async ( { editor, page, pageUtils, } ) => { await editor.insertBlock( { name: 'core/shortcode' } ); await editor.insertBlock( { name: 'core/paragraph' } ); await page.keyboard.type( 'abc' ); // Need content to remove placeholder label. await editor.selectBlocks( editor.canvas.locator( 'role=document[name="Block: Shortcode"i]' ) ); // Should remain in title upon ArrowRight: await page.keyboard.press( 'ArrowRight' ); await expect( editor.canvas.locator( 'role=document[name="Block: Shortcode"i]' ) ).toHaveClass( /is-selected/ ); // Should remain in title upon modifier + ArrowDown: await pageUtils.pressKeys( 'primary+ArrowDown' ); await expect( editor.canvas.locator( 'role=document[name="Block: Shortcode"i]' ) ).toHaveClass( /is-selected/ ); // Should navigate to the next block. await page.keyboard.press( 'ArrowDown' ); await expect( editor.canvas.locator( 'role=document[name="Block: Paragraph"i]' ) ).toHaveClass( /is-selected/ ); } ); test( 'should not delete surrounding space when deleting a word with Backspace', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1 2 3' ); await pageUtils.pressKeys( 'ArrowLeft', { times: ' 3'.length } ); await page.keyboard.press( 'Backspace' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

1 3

` ); await page.keyboard.type( '2' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

1 2 3

` ); } ); test( 'should not delete surrounding space when deleting a word with Alt+Backspace', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'alpha beta gamma' ); await pageUtils.pressKeys( 'ArrowLeft', { times: ' gamma'.length } ); await pageUtils.pressKeys( `${ process.platform === 'darwin' ? 'Alt' : 'primary' }+Backspace` ); await expect.poll( editor.getEditedPostContent ) .toBe( `

alpha gamma

` ); await page.keyboard.type( 'beta' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

alpha beta gamma

` ); } ); test( 'should not delete surrounding space when deleting a selected word', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'alpha beta gamma' ); await pageUtils.pressKeys( 'ArrowLeft', { times: ' gamma'.length } ); await page.keyboard.down( 'Shift' ); await pageUtils.pressKeys( 'ArrowLeft', { times: 'beta'.length } ); await page.keyboard.up( 'Shift' ); await page.keyboard.press( 'Backspace' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

alpha gamma

` ); await page.keyboard.type( 'beta' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

alpha beta gamma

` ); } ); test( 'should create valid paragraph blocks when rapidly pressing Enter', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await pageUtils.pressKeys( 'Enter', { times: 10 } ); // Check that none of the paragraph blocks have
in them. expect( await editor.getBlocks() ).toMatchObject( Array( 11 ).fill( { name: 'core/paragraph', attributes: { content: '' }, } ) ); } ); test( 'should navigate empty paragraphs', async ( { editor, page } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( '3' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '1' }, }, { name: 'core/paragraph', attributes: { content: '' }, }, { name: 'core/paragraph', attributes: { content: '3' }, }, ] ); } ); test( 'should navigate contenteditable with padding', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await editor.canvas.locator( ':root' ).evaluate( () => { document.activeElement.style.paddingTop = '100px'; } ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( '1' ); await editor.canvas.locator( ':root' ).evaluate( () => { document.activeElement.style.paddingBottom = '100px'; } ); await page.keyboard.press( 'ArrowDown' ); await page.keyboard.type( '2' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '1' }, }, { name: 'core/paragraph', attributes: { content: '2' }, }, ] ); } ); test( 'should navigate contenteditable with normal line height', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await editor.canvas.locator( ':root' ).evaluate( () => { document.activeElement.style.lineHeight = 'normal'; } ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( '1' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '1' }, }, { name: 'core/paragraph', attributes: { content: '' }, }, ] ); } ); // Regression test for https://github.com/WordPress/gutenberg/issues/72053. test( 'should navigate contenteditable with border radius', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await editor.canvas.locator( ':root' ).evaluate( () => { document.activeElement.style.borderRadius = '50px'; } ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( '1' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '1' }, }, { name: 'core/paragraph', attributes: { content: '' }, }, ] ); } ); test( 'should not prematurely multi-select', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '><<' ); await pageUtils.pressKeys( 'shift+Enter' ); await page.keyboard.type( '<<<' ); await page.keyboard.down( 'Shift' ); await pageUtils.pressKeys( 'ArrowLeft', { times: '<<\n<<<'.length } ); await page.keyboard.up( 'Shift' ); await page.keyboard.press( 'Backspace' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '1' }, }, { name: 'core/paragraph', attributes: { content: '>' }, }, ] ); } ); test( 'should remove first empty paragraph on Backspace', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '2' ); await page.keyboard.press( 'ArrowUp' ); // Ensure setup is correct. expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '' }, }, { name: 'core/paragraph', attributes: { content: '2' }, }, ] ); await page.keyboard.press( 'Backspace' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '2' }, }, ] ); } ); test( 'should merge paragraphs', async ( { editor, page } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '2' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.press( 'Backspace' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

12

` ); } ); test( 'should merge and then split paragraphs', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'abc' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '123' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'Delete' ); await page.keyboard.press( 'Enter' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

abc

123

` ); } ); test( 'should merge and then soft line break', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '2' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'Delete' ); await page.keyboard.down( 'Shift' ); await page.keyboard.press( 'Enter' ); await page.keyboard.up( 'Shift' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

1
2

` ); } ); test( 'should merge forwards', async ( { editor, page } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '3' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'Delete' ); await page.keyboard.type( '2' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

123

` ); } ); test( 'should merge forwards properly on multiple triggers', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '2' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '3' ); await pageUtils.pressKeys( 'ArrowUp', { times: 2 } ); await pageUtils.pressKeys( 'Delete', { times: 2 } ); await expect.poll( editor.getEditedPostContent ) .toBe( `

1

3

` ); await page.keyboard.press( 'Delete' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

13

` ); } ); test( 'should preserve horizontal position when navigating vertically between blocks (-webkit)', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'abc' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '23' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.press( 'ArrowLeft' ); await page.keyboard.press( 'ArrowDown' ); await page.keyboard.type( '1' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

abc

123

` ); } ); test( 'should remember initial vertical position', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await pageUtils.pressKeys( 'shift+Enter' ); await page.keyboard.type( '2' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( 'x' ); // Should be right after "1". await expect.poll( editor.getEditedPostContent ) .toBe( `

1x


2

` ); } ); test( 'should navigate contenteditable with side padding', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'Enter' ); await editor.canvas.locator( ':root' ).evaluate( () => { document.activeElement.style.paddingLeft = '100px'; } ); await page.keyboard.press( 'Enter' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( '1' ); expect( await editor.getBlocks() ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '1' }, }, { name: 'core/paragraph', attributes: { content: '' }, }, { name: 'core/paragraph', attributes: { content: '' }, }, ] ); } ); test( 'should extend selection into paragraph for list with longer last item', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'a' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '* b' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'cd' ); // Selects part of the first list item, although invisible. await pageUtils.pressKeys( 'shift+ArrowUp' ); await page.evaluate( () => new Promise( window.requestIdleCallback ) ); // Extends selection into the first paragraph await pageUtils.pressKeys( 'shift+ArrowUp' ); await page.evaluate( () => new Promise( window.requestIdleCallback ) ); // Mixed selection, so all content will be removed. await page.keyboard.press( 'Backspace' ); await expect.poll( editor.getEditedPostContent ).toBe( '' ); } ); test( 'should place the caret at the click position when clicking a block that is not selected', async ( { editor, page, } ) => { await editor.canvas .locator( 'role=document[name="Add default block"i]' ) .click(); await page.keyboard.type( 'First' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'Second' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'First' } }, { name: 'core/paragraph', attributes: { content: 'Second' } }, ] ); // Click the first paragraph. The click lands at the center of the // paragraph element, past the end of the short text, so the caret // must be placed at the end of the text. await editor.canvas.locator( 'p:text("First")' ).click(); await page.keyboard.type( '!' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'First!' } }, { name: 'core/paragraph', attributes: { content: 'Second' } }, ] ); } ); test( 'should not have a dead zone between blocks (lower)', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '2' ); await page.keyboard.press( 'ArrowUp' ); const paragraphBlock = editor.canvas .locator( 'role=document[name="Block: Paragraph"i]' ) .first(); const paragraphRect = await paragraphBlock.boundingBox(); const x = paragraphRect.x + ( 2 * paragraphRect.width ) / 3; const y = paragraphRect.y + paragraphRect.height + 1; // The typing observer requires two mouse moves to detect an actual // move. await page.mouse.move( x - 1, y - 1 ); await page.mouse.move( x, y ); const inserter = page.locator( 'role=button[name="Add block"i]' ); await expect( inserter ).toBeVisible(); const inserterRect = await inserter.boundingBox(); const lowerInserterY = inserterRect.y + ( 2 * inserterRect.height ) / 3; await page.mouse.click( x, lowerInserterY ); await page.keyboard.type( '3' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

1

23

` ); } ); test( 'should not have a dead zone above an aligned block', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '1' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( '/image' ); await expect( page.getByRole( 'option', { name: 'Image', selected: true } ) ).toBeVisible(); await page.keyboard.press( 'Enter' ); await editor.clickBlockToolbarButton( 'Align block' ); const wideButton = page.locator( 'role=menuitemradio[name="Wide width"i]' ); await wideButton.click(); // Focus the block content await pageUtils.pressKeys( 'Tab' ); // Select the previous block. await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'ArrowUp' ); // Confirm correct setup. await expect.poll( editor.getEditedPostContent ) .toBe( `

1

` ); const paragraphBlock = editor.canvas.locator( 'role=document[name="Block: Paragraph"i]' ); // Find a point outside the paragraph between the blocks where it's // expected that the sibling inserter would be placed. const paragraphRect = await paragraphBlock.boundingBox(); const x = paragraphRect.x + ( 2 * paragraphRect.width ) / 3; const y = paragraphRect.y + paragraphRect.height + 1; await page.mouse.move( x, y ); const inserter = page.locator( 'role=button[name="Add block"i]' ); await expect( inserter ).toBeVisible(); // Find the space between the inserter and the image block. const inserterRect = await inserter.boundingBox(); const lowerInserterY = inserterRect.y + ( 2 * inserterRect.height ) / 3; // Clicking that in-between space should select the image block. await page.mouse.click( x, lowerInserterY ); await expect( editor.canvas.locator( 'role=document[name="Block: Image"i]' ) ).toHaveClass( /is-selected/ ); } ); test( 'should focus preceding tabbable using shift+tab from post title and writing flow container', async ( { editor, page, } ) => { const optionsButton = page .getByRole( 'region', { name: 'Editor top bar' } ) .getByRole( 'button', { name: 'Options' } ); await page.keyboard.press( 'Shift+Tab' ); await expect( optionsButton ).toBeFocused(); const editorCanvasBody = editor.canvas.locator( 'body' ); // Focuses the editor canvas body. In the editor the click doesn’t have // to be on the element itself – just somewhere that won’t focus a block. await editorCanvasBody.click(); await page.keyboard.press( 'Shift+Tab' ); await expect( optionsButton ).toBeFocused(); } ); test( 'should only consider the content as one tab stop', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '/table' ); await expect( page.getByRole( 'option', { name: 'Table', selected: true } ) ).toBeVisible(); await page.keyboard.press( 'Enter' ); // Tab to the "Create table" button. await pageUtils.pressKeys( 'Tab' ); await pageUtils.pressKeys( 'Tab' ); // Create the table. await page.keyboard.press( 'Space' ); await expect( editor.canvas.locator( 'role=document[name="Block: Table"i]' ) ).toBeVisible(); // Navigate to the second cell. await page.keyboard.press( 'ArrowRight' ); await page.keyboard.type( '2' ); // Confirm correct setup. await expect.poll( editor.getEditedPostContent ) .toBe( `
2
` ); } ); // Checks for regressions of https://github.com/WordPress/gutenberg/issues/40091. test( 'does not deselect the block when selecting text outside the editor canvas', async ( { editor, page, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'Random Paragraph' ); await editor.openDocumentSettingsSidebar(); const blockDescription = page.locator( '.block-editor-block-card__description' ); await expect( blockDescription ).toBeVisible(); const boundingBox = await blockDescription.boundingBox(); const startPosition = { x: boundingBox.x + 10, y: boundingBox.y + 8, }; const endPosition = { x: startPosition.x + 50, y: startPosition.y, }; await page.mouse.move( startPosition.x, startPosition.y ); await page.mouse.down(); await page.mouse.move( endPosition.x, endPosition.y ); await page.mouse.up(); await expect( editor.canvas.locator( 'role=document[name="Block: Paragraph"i]' ) ).toHaveClass( /is-selected/ ); } ); test( 'should format a multi selection with the shortcut, not the browser default', async ( { editor, page, pageUtils, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'first' ); await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'second' ); // Multi select both paragraphs. await pageUtils.pressKeys( 'ArrowLeft', { times: 2 } ); await page.keyboard.down( 'Shift' ); await pageUtils.pressKeys( 'ArrowLeft', { times: 2 } ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.up( 'Shift' ); await pageUtils.pressKeys( 'primary+b' ); await expect.poll( editor.getEditedPostContent ) .toBe( `

first

second

` ); } ); test( 'should move to the start of the first line on ArrowUp', async ( { page, editor, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'a' ); async function getHeight() { return await editor.canvas .locator( ':root' ) .evaluate( () => document.activeElement.offsetHeight ); } const height = await getHeight(); // Keep typing until the height of the element increases. We need two // lines. while ( height === ( await getHeight() ) ) { await page.keyboard.type( 'a' ); } // Move to the start of the second line. await page.keyboard.press( 'ArrowLeft' ); // Move to the start of the first line. await page.keyboard.press( 'ArrowUp' ); // Insert a "." for testing. await page.keyboard.type( '.' ); // Expect the "." to be added at the start of the paragraph await expect( editor.canvas.locator( 'role=document[name="Block: Paragraph"i]' ) ).toHaveText( /^\.a+$/ ); } ); test( 'should not scroll the page when moving the caret within a block taller than the viewport', async ( { page, editor, } ) => { const lines = Array.from( { length: 70 }, ( _, i ) => `line ${ i + 1 }` ).join( '\n' ); await editor.insertBlock( { name: 'core/code', attributes: { content: lines }, } ); const frame = page.frame( { name: 'editor-canvas' } ); // Scroll the middle of the code block into view and click there. await frame.evaluate( () => { const code = document.querySelector( '[data-type="core/code"]' ); const rect = code.getBoundingClientRect(); const middle = rect.top + document.documentElement.scrollTop + rect.height / 2; document.documentElement.scrollTop = middle - window.innerHeight / 2; } ); const codeBox = await editor.canvas .locator( '[data-type="core/code"] code' ) .boundingBox(); await page.mouse.click( codeBox.x + 60, codeBox.y + codeBox.height / 2 ); const getCaretLine = () => frame.evaluate( () => parseInt( document .getSelection() .focusNode?.textContent.match( /line (\d+)/ )?.[ 1 ], 10 ) ); const lineBefore = await getCaretLine(); // Without a caret the line reads are NaN and the final assertion // would compare NaN to NaN, which passes. expect( lineBefore ).toBeGreaterThan( 1 ); // Record every scroll movement for a second. Moving the caret // within the block must not scroll the page: the caret stays // within the viewport. const scrollWatcher = frame.evaluate( () => new Promise( ( resolve ) => { const positions = []; // Capture phase so scrolls of nested containers are // caught too, not only the document. document.addEventListener( 'scroll', ( event ) => { positions.push( event.target === document ? document.documentElement.scrollTop : event.target.scrollTop ); }, true ); setTimeout( () => resolve( positions ), 1000 ); } ) ); await page.keyboard.press( 'ArrowDown' ); await page.keyboard.press( 'ArrowDown' ); await page.keyboard.press( 'ArrowUp' ); // The caret moved down one line net (down, down, up). await expect.poll( getCaretLine ).toBe( lineBefore + 1 ); expect( await scrollWatcher ).toEqual( [] ); } ); test( 'should move the caret to the next block when only the block padding is below the viewport', async ( { page, editor, } ) => { for ( let i = 1; i <= 20; i++ ) { await editor.insertBlock( { name: 'core/preformatted', attributes: { content: `Block ${ i }` }, } ); } await editor.canvas.getByText( 'Block 10', { exact: true } ).click(); // Scroll so the block's text is within the viewport and its bottom // edge, just past the text, is 3px below it. const offset = await editor.canvas.locator( ':root' ).evaluate( () => { const { bottom } = document.activeElement.getBoundingClientRect(); document.documentElement.scrollTop += bottom - window.innerHeight - 3; return document.getSelection().focusOffset; } ); await page.keyboard.press( 'ArrowDown' ); await page.keyboard.type( '.' ); // The text is monospaced, so the caret keeps its offset. await expect .poll( async () => ( await editor.getBlocks() ).slice( 9, 11 ) ) .toMatchObject( [ { name: 'core/preformatted', attributes: { content: 'Block 10' }, }, { name: 'core/preformatted', attributes: { content: `${ 'Block 11'.slice( 0, offset ) }.${ 'Block 11'.slice( offset ) }`, }, }, ] ); } ); test( 'should move the caret to the previous block when only the block padding is above the viewport', async ( { page, editor, } ) => { for ( let i = 1; i <= 20; i++ ) { await editor.insertBlock( { name: 'core/preformatted', attributes: { content: `Block ${ i }` }, } ); } await editor.canvas.getByText( 'Block 10', { exact: true } ).click(); // Scroll so the block's text is within the viewport and its top edge, // just before the text, is 3px above it. const offset = await editor.canvas.locator( ':root' ).evaluate( () => { const { top } = document.activeElement.getBoundingClientRect(); document.documentElement.scrollTop += top + 3; return document.getSelection().focusOffset; } ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.type( '.' ); // The text is monospaced, so the caret keeps its offset. await expect .poll( async () => ( await editor.getBlocks() ).slice( 8, 10 ) ) .toMatchObject( [ { name: 'core/preformatted', attributes: { content: `${ 'Block 9'.slice( 0, offset ) }.${ 'Block 9'.slice( offset ) }`, }, }, { name: 'core/preformatted', attributes: { content: 'Block 10' }, }, ] ); } ); test( 'should vertically move the caret from corner to corner (-webkit)', async ( { page, editor, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'a' ); async function getHeight() { return await editor.canvas .locator( ':root' ) .evaluate( () => document.activeElement.offsetHeight ); } const height = await getHeight(); // Keep typing until the height of the element increases. We need two // lines. while ( height === ( await getHeight() ) ) { await page.keyboard.type( 'a' ); } // Create a new paragraph. await page.keyboard.press( 'Enter' ); // Move to the start of the first line. await page.keyboard.press( 'ArrowUp' ); // Insert a "." for testing. await page.keyboard.type( '.' ); // Expect the "." to be added at the start of the paragraph await expect( editor.canvas.locator( 'role=document[name="Block: Paragraph"i]' ) ).toHaveText( /^a+\.a$/ ); } ); test( 'should vertically move the caret when pressing Alt', async ( { page, pageUtils, editor, } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'a' ); async function getHeight() { return await editor.canvas .locator( ':root' ) .evaluate( () => document.activeElement.offsetHeight ); } const height = await getHeight(); // Keep typing until the height of the element increases. We need two // lines. while ( height === ( await getHeight() ) ) { await page.keyboard.type( 'a' ); } // Create a new paragraph. await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'b' ); await page.keyboard.press( 'ArrowLeft' ); await pageUtils.pressKeys( 'alt+ArrowUp' ); await page.keyboard.type( '.' ); // Expect the "." to be added at the start of the paragraph await expect( editor.canvas.locator( 'role=document[name="Block: Paragraph"i] >> nth = 0' ) ).toHaveText( /^.a+$/ ); } ); test( 'should select synced pattern', async ( { page, editor } ) => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'synced' ); await editor.clickBlockOptionsMenuItem( 'Create pattern' ); const createPatternDialog = editor.page.getByRole( 'dialog', { name: 'add pattern', } ); await createPatternDialog .getByRole( 'textbox', { name: 'Name' } ) .fill( 'test' ); await page.keyboard.press( 'Enter' ); await expect( editor.canvas.locator( '[data-type="core/block"] [data-type="core/paragraph"]' ) ).toBeVisible(); await editor.insertBlock( { name: 'core/paragraph' } ); await page.keyboard.press( 'ArrowUp' ); await expect( editor.canvas.locator( '[data-type="core/block"]' ) ).toBeFocused(); } ); test( 'should select non-contenteditable block via click after partial selection', async ( { editor, pageUtils, } ) => { await editor.insertBlock( { name: 'core/paragraph' } ); await editor.insertBlock( { name: 'core/spacer' } ); await editor.canvas .getByRole( 'document', { name: 'Empty block' } ) .fill( 'A partial selection' ); await pageUtils.pressKeys( 'shiftAlt+ArrowLeft' ); const spacer = editor.canvas.getByRole( 'document', { name: 'Block: Spacer', } ); await spacer.click(); await expect( spacer ).toBeFocused(); await expect( editor.canvas.getByRole( 'document', { name: 'Block: Paragraph' } ) ).not.toBeFocused(); } ); test( 'should show format toolbar when selecting text from the left edge of a block', async ( { editor, page, browserName, } ) => { // eslint-disable-next-line playwright/no-skipped-test test.skip( browserName === 'chromium', 'Chromium does not extend a selection from non-editable padding into editable text.' ); await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'Hello world' }, } ); // Deselect the block. await page.evaluate( () => window.wp.data.dispatch( 'core/block-editor' ).clearSelectedBlock() ); const paragraphBlock = editor.canvas.getByRole( 'document', { name: 'Block: Paragraph', } ); const box = await paragraphBlock.boundingBox(); // Start the drag from just before the left edge of the paragraph // (on the block wrapper padding) and drag into the text. const startX = box.x - 5; const startY = box.y + box.height / 2; const endX = box.x + box.width / 2; const endY = startY; await page.mouse.move( startX, startY ); await page.mouse.down(); await page.mouse.move( endX, endY, { steps: 10 } ); await page.mouse.up(); // The Bold button should be visible in the inline block toolbar. await expect( page .getByRole( 'toolbar', { name: 'Block tools' } ) .getByRole( 'button', { name: 'Bold' } ) ).toBeVisible(); // The selection is editable: typing replaces it. A selection made // while the text was non-editable would show the toolbar but not // take input. The engines differ in where the selection starts. const selected = await editor.canvas .locator( ':root' ) .evaluate( ( root ) => root.ownerDocument.getSelection().toString() ); expect( selected ).not.toBe( '' ); await page.keyboard.type( 'x' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'Hello world'.replace( selected, 'x' ) }, }, ] ); } ); // Regression test: ArrowDown should not skip over a paragraph that contains // a link. See https://github.com/WordPress/gutenberg/issues/77473. test( 'should not skip paragraph with link when navigating down with ArrowDown', async ( { editor, page, } ) => { await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'a' }, } ); await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'a' }, } ); await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'a' }, } ); // Position cursor at the start of the first paragraph. await editor.canvas .locator( 'role=document[name="Block: Paragraph"i]' ) .first() .click(); await page.keyboard.press( 'Home' ); // ArrowDown should move to the second paragraph (with the link), // not skip over it to the third. await page.keyboard.press( 'ArrowDown' ); // The element owning the caret should be the second paragraph, which // contains a link. const focusedElement = editor.canvas.locator( 'body:not(:focus) :focus, body:focus .is-selected' ); await expect( focusedElement.locator( 'a[href="#"]' ) ).toBeVisible(); } ); test( 'inserts the container default block on Enter on a selected block', async ( { editor, page, } ) => { await editor.insertBlock( { name: 'core/gallery', innerBlocks: [ { name: 'core/image', attributes: { url: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=', }, }, { name: 'core/image', attributes: { url: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=', }, }, ], } ); const firstImage = editor.canvas .getByRole( 'document', { name: 'Block: Image' } ) .first(); await editor.selectBlocks( firstImage ); await expect( firstImage ).toBeFocused(); // A gallery defines the image as its default block, so Enter // inserts a new empty image after the selected one. await page.keyboard.press( 'Enter' ); await expect .poll( () => editor.getBlocks().then( ( [ gallery ] ) => gallery.innerBlocks.map( ( { name, attributes } ) => ( { name, hasUrl: !! attributes.url, } ) ) ) ) .toEqual( [ { name: 'core/image', hasUrl: true }, { name: 'core/image', hasUrl: false }, { name: 'core/image', hasUrl: true }, ] ); } ); } ); class WritingFlowUtils { constructor( { page, editor } ) { this.page = page; this.editor = editor; this.getActiveBlockName = this.getActiveBlockName.bind( this ); } async getActiveBlockName() { return await this.page.evaluate( () => window.wp.data.select( 'core/block-editor' ).getSelectedBlock() ?.name ); } async addDemoContent() { await this.editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'First paragraph', }, } ); await this.editor.insertBlock( { name: 'core/columns', } ); await this.editor.canvas .locator( 'role=button[name="Two columns; equal split"i]' ) .click(); const firstColumn = this.editor.canvas.getByRole( 'document', { name: 'Block: Column (1 of 2)', } ); await firstColumn.focus(); await firstColumn.getByRole( 'button', { name: 'Add block' } ).click(); await this.page .getByRole( 'listbox', { name: 'Blocks' } ) .getByRole( 'option', { name: 'Paragraph' } ) .click(); await this.page.keyboard.type( '1st col' ); // If this text is too long, it may wrap to a new line and cause test failure. That's why we're using "1st" instead of "First" here. await this.editor.canvas .locator( 'role=document[name="Block: Column (2 of 2)"i]' ) .focus(); await this.editor.canvas .locator( 'role=button[name="Add block"i]' ) .click(); await this.page .getByRole( 'listbox', { name: 'Blocks' } ) .getByRole( 'option', { name: 'Paragraph' } ) .click(); await this.page.keyboard.type( '2nd col' ); // If this text is too long, it may wrap to a new line and cause test failure. That's why we're using "2nd" instead of "Second" here. await this.editor.showBlockToolbar(); await this.page.keyboard.press( 'Shift+Tab' ); // Move to toolbar to select parent await this.page.keyboard.press( 'Enter' ); // Selects the column block. await this.page.keyboard.press( 'Shift+Tab' ); // Move to toolbar to select parent await this.page.keyboard.press( 'Enter' ); // Selects the columns block. await this.page.keyboard.press( 'Enter' ); // Creates a paragraph after the columns block. await this.page.keyboard.type( 'Second paragraph' ); } }