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: preserve multiple spaces on front-end #56280

Closed
wants to merge 4 commits into from
Closed
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
55 changes: 50 additions & 5 deletions packages/rich-text/src/component/use-input-and-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useRefEffect } from '@wordpress/compose';
*/
import { getActiveFormats } from '../get-active-formats';
import { updateFormats } from '../update-formats';
import { isCollapsed } from '../is-collapsed';

/**
* All inserting input types that would insert HTML into the DOM.
Expand Down Expand Up @@ -96,15 +97,59 @@ export function useInputAndSelection( props ) {
return;
}

const currentValue = createRecord();
const { start, activeFormats: oldActiveFormats = [] } =
const newValue = createRecord();
const { start: oldStart, activeFormats: oldActiveFormats = [] } =
record.current;

const SP = ' ';
const NBSP = '\u00a0';
const isSpOrNbsp = ( char ) => char === SP || char === NBSP;

// When inserting multiple spaces, alternate between a normal space
// and a non-breaking space. This is to prevent browsers from
// collapsing multiple spaces into one.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yikes. this seems good, but dang - so hard to make an editor in the browser!

if (
isCollapsed( newValue ) &&
oldStart !== newValue.start &&
( oldStart < newValue.start
? // Check if the inserted character is a space.
newValue.text.slice( oldStart, newValue.start ) === SP
: // Check if the deleted character is a space or
// non-breaking space (in both cases we need to fix the
// alternating pattern).
isSpOrNbsp(
record.current.text.slice(
newValue.start,
oldStart
)
) )
) {
const text = newValue.text;
let startOffset = newValue.start;
let endOffset = newValue.start;

// We need to make sure the alternating pattern is maintained,
// so replace all spaces before and after the selection.
while ( isSpOrNbsp( text[ startOffset - 1 ] ) ) {
startOffset--;
}
while ( isSpOrNbsp( text[ endOffset ] ) ) {
endOffset++;
}

newValue.text =
text.slice( 0, startOffset ) +
Array.from( { length: endOffset - startOffset } )
.map( ( _, index ) => ( index % 2 === 0 ? SP : NBSP ) )
.join( '' ) +
text.slice( endOffset );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense here to use a regular expression to handle this?

// Replace all successive spaces with an alternating pattern of space/nbsp.
newValue.text = newValue.replace( /[ \u00a0]+/g, match => ''.padStart( match.length, ' \u00a0' ) );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to be replacing all spaces. We shouldn't modify existing content too much imo, only the spaces around the selection.

}

// Update the formats between the last and new caret position.
const change = updateFormats( {
value: currentValue,
start,
end: currentValue.start,
value: newValue,
start: oldStart,
end: newValue.start,
formats: oldActiveFormats,
} );

Expand Down
4 changes: 1 addition & 3 deletions test/e2e/specs/editor/blocks/links.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,9 +969,7 @@ test.describe( 'Links', () => {
name: 'core/paragraph',
attributes: {
content:
'Text with leading and trailing<a href="https://wordpress.org/gutenberg">' +
textToSelect +
'</a>',
'Text with leading and trailing<a href="https://wordpress.org/gutenberg">         spaces     </a>',
},
},
] );
Expand Down
60 changes: 60 additions & 0 deletions test/e2e/specs/editor/various/rich-text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,4 +853,64 @@
},
] );
} );

test( 'should pad multiple spaces', async ( { page, editor } ) => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'a b' );
expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'a  b' },
},
] );
} );

test( 'should pad starting from space', async ( { page, editor } ) => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'a b' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.type( ' ' );
expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'a  b' },
},
] );
} );

test( 'should restore alternating padding on backspace', async ( {
page,
editor,
} ) => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'a b' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'Backspace' );
expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'a   b' },
},
] );
} );

test( 'should restore alternating padding on delete', async ( {
page,
editor,
} ) => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'a b' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'Delete' );
expect( await editor.getBlocks() ).toMatchObject( [

Check failure on line 909 in test/e2e/specs/editor/various/rich-text.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/rich-text.spec.js:899:2 › RichText › should restore alternating padding on delete

1) [chromium] › editor/various/rich-text.spec.js:899:2 › RichText › should restore alternating padding on delete Error: expect(received).toMatchObject(expected) - Expected - 1 + Received + 1 Array [ Object { "attributes": Object { - "content": "a   b", + "content": "a   b", }, "name": "core/paragraph", }, ] 907 | await page.keyboard.press( 'ArrowLeft' ); 908 | await page.keyboard.press( 'Delete' ); > 909 | expect( await editor.getBlocks() ).toMatchObject( [ | ^ 910 | { 911 | name: 'core/paragraph', 912 | attributes: { content: 'a   b' }, at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/rich-text.spec.js:909:38

Check failure on line 909 in test/e2e/specs/editor/various/rich-text.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/rich-text.spec.js:899:2 › RichText › should restore alternating padding on delete

1) [chromium] › editor/various/rich-text.spec.js:899:2 › RichText › should restore alternating padding on delete Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toMatchObject(expected) - Expected - 1 + Received + 1 Array [ Object { "attributes": Object { - "content": "a   b", + "content": "a   b", }, "name": "core/paragraph", }, ] 907 | await page.keyboard.press( 'ArrowLeft' ); 908 | await page.keyboard.press( 'Delete' ); > 909 | expect( await editor.getBlocks() ).toMatchObject( [ | ^ 910 | { 911 | name: 'core/paragraph', 912 | attributes: { content: 'a   b' }, at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/rich-text.spec.js:909:38

Check failure on line 909 in test/e2e/specs/editor/various/rich-text.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/rich-text.spec.js:899:2 › RichText › should restore alternating padding on delete

1) [chromium] › editor/various/rich-text.spec.js:899:2 › RichText › should restore alternating padding on delete Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toMatchObject(expected) - Expected - 1 + Received + 1 Array [ Object { "attributes": Object { - "content": "a   b", + "content": "a   b", }, "name": "core/paragraph", }, ] 907 | await page.keyboard.press( 'ArrowLeft' ); 908 | await page.keyboard.press( 'Delete' ); > 909 | expect( await editor.getBlocks() ).toMatchObject( [ | ^ 910 | { 911 | name: 'core/paragraph', 912 | attributes: { content: 'a   b' }, at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/rich-text.spec.js:909:38
{
name: 'core/paragraph',
attributes: { content: 'a   b' },
},
] );
} );
} );
Loading