Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich Text: Prevent line breaks when disableLineBreaks is true #67412

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { pasteHandler } from '@wordpress/blocks';
import { isEmpty, insert, create } from '@wordpress/rich-text';
import { isEmpty, insert, create, replace } from '@wordpress/rich-text';
import { isURL } from '@wordpress/url';

/**
Expand All @@ -25,6 +25,7 @@ export default ( props ) => ( element ) => {
__unstableEmbedURLOnPaste,
preserveWhiteSpace,
pastePlainText,
disableLineBreaks,
} = props.current;

// The event listener is attached to the window, so we need to check if
Expand Down Expand Up @@ -71,7 +72,12 @@ export default ( props ) => ( element ) => {
if ( transformed !== value ) {
onChange( transformed );
} else {
const valueToInsert = create( { html: content } );
let valueToInsert = create( { html: content } );

if ( disableLineBreaks ) {
valueToInsert = replace( valueToInsert, /\r?\n/g, ' ' );
}

addActiveFormats( valueToInsert, value.activeFormats );
onChange( insert( value, valueToInsert ) );
}
Expand Down
52 changes: 52 additions & 0 deletions test/e2e/specs/editor/various/rich-text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,58 @@ test.describe( 'RichText (@firefox, @webkit)', () => {
await admin.createNewPost();
} );

test( 'should strip line breaks when pasting with disableLineBreaks enabled', async ( {
editor,
pageUtils,
} ) => {
await editor.insertBlock( { name: 'core/site-title' } );

await editor.canvas
.locator( 'role=textbox[name="Site title text"]' )
.click();

const multiLineContent = 'First<br/>Second<br/>Third';
pageUtils.setClipboardData( {
plainText: multiLineContent,
html: multiLineContent.replace( /\n/g, '<br>' ),
} );

await pageUtils.pressKeys( 'primary+a' );
await pageUtils.pressKeys( 'Backspace' );
await pageUtils.pressKeys( 'primary+v' );

const renderedText = await editor.canvas
.locator( 'role=textbox[name="Site title text"]' )
.innerText();
expect( renderedText ).toBe( 'First Second Third' );
} );

test( 'should not strip line breaks when pasting with disableLineBreaks diabled', async ( {
editor,
pageUtils,
} ) => {
await editor.canvas
.locator( 'role=button[name="Add default block"i]' )
.click();

const multiLineContent = 'First<br/>Second<br/>Third';
pageUtils.setClipboardData( {
plainText: multiLineContent,
html: multiLineContent.replace( /\n/g, '<br>' ),
} );

await pageUtils.pressKeys( 'primary+a' );
await pageUtils.pressKeys( 'Backspace' );
await pageUtils.pressKeys( 'primary+v' );

expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'First<br>Second<br>Third' },
},
] );
} );

test( 'should handle change in tag name gracefully', async ( {
page,
editor,
Expand Down
Loading