Skip to content
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
5 changes: 5 additions & 0 deletions src/experiments/type-ahead/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ export const ALLOWED_BLOCKS = [ 'core/paragraph' ];
export const REQUEST_TIMEOUT_MS = 15000;
export const WHITESPACE_REGEX = /\s/;
export const LEADING_WHITESPACE_REGEX = /^\s/;

// A character from a script written without spaces between words (Chinese,
// Japanese, Thai, Lao, Khmer and Burmese), or the full-width punctuation used with them.
export const NO_SPACE_SCRIPT_REGEX =
/[\p{scx=Han}\p{scx=Hiragana}\p{scx=Katakana}\p{scx=Thai}\p{scx=Lao}\p{scx=Khmer}\p{scx=Myanmar}\u3000-\u303F\uFF01-\uFF0F\uFF1A-\uFF20\uFF3B-\uFF40\uFF5B-\uFF65]/u;
17 changes: 16 additions & 1 deletion src/experiments/type-ahead/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
/**
* Internal dependencies
*/
import { LEADING_WHITESPACE_REGEX, WHITESPACE_REGEX } from '../constants';
import {
LEADING_WHITESPACE_REGEX,
NO_SPACE_SCRIPT_REGEX,
WHITESPACE_REGEX,
} from '../constants';

/**
* Converts HTML to plain text.
Expand Down Expand Up @@ -104,5 +108,16 @@ export const addLeadingSpaceIfNeeded = (
return text;
}

// No space between two characters of a script that does not use them.
// Array.from() keeps a surrogate pair (a rarer Han character) whole.
const lastPrecedingChar = Array.from( precedingText ).at( -1 ) ?? '';
const firstChar = Array.from( text ).at( 0 ) ?? '';
if (
NO_SPACE_SCRIPT_REGEX.test( lastPrecedingChar ) &&
NO_SPACE_SCRIPT_REGEX.test( firstChar )
) {
return text;
}

return ` ${ text }`;
};
34 changes: 34 additions & 0 deletions tests/e2e/specs/experiments/type-ahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,40 @@ test.describe( 'Type-ahead Text Experiment', () => {
expect( requests ).toBe( 1 );
} );

test( 'Adds no space before a suggestion in a language written without spaces', async ( {
admin,
editor,
page,
} ) => {
await enableExperiment( admin, page, 'Type-ahead Text' );

await page.route(
/\/wp-abilities\/v1\/abilities\/ai\/type-ahead\/run/,
( route ) =>
route.fulfill( {
json: { suggestion: 'いいですね。', confidence: 1 },
} )
);

await admin.createNewPost();
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: '今日は天気が' },
} );

const paragraph = editor.canvas.getByRole( 'document', {
name: 'Block: Paragraph',
} );
await paragraph.click();

await expect( editor.canvas.getByRole( 'status' ) ).toHaveText(
'いいですね。'
);
await page.keyboard.press( 'Tab' );

await expect( paragraph ).toHaveText( '今日は天気がいいですね。' );
} );

test( 'Ensure the Type-ahead Text Experiment UI is not visible when the experiment is disabled', async ( {
admin,
editor,
Expand Down
Loading