Skip to content

Commit

Permalink
Only collapse white space when initialising rich text
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 10, 2023
1 parent 0c26493 commit 2ef1e3a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ _Optional._ A list of autocompleters to use instead of the default.

### `preserveWhiteSpace: Boolean`

_Optional._ Whether or not to preserve white space characters in the `value`.
Normally tab, newline and space characters are collapsed to a single space or
_Optional._ Whether or not to preserve white space characters in the `value`. Normally tab, newline and space characters are collapsed to a single space or
trimmed.

## RichText.Content
Expand Down
1 change: 0 additions & 1 deletion packages/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ _Parameters_
- _$1.text_ `[string]`: Text to create value from.
- _$1.html_ `[string]`: HTML to create value from.
- _$1.range_ `[Range]`: Range to create value from.
- _$1.preserveWhiteSpace_ `[boolean]`: Whether or not to collapse white space characters.
- _$1.\_\_unstableIsEditableTree_ `[boolean]`:

_Returns_
Expand Down
6 changes: 4 additions & 2 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useRegistry } from '@wordpress/data';
/**
* Internal dependencies
*/
import { create } from '../create';
import { collapseWhiteSpace, create } from '../create';
import { apply } from '../to-dom';
import { toHTMLString } from '../to-html-string';
import { useDefaultStyle } from './use-default-style';
Expand Down Expand Up @@ -70,7 +70,9 @@ export function useRichText( {

function setRecordFromProps() {
_value.current = value;
record.current = create( { html: value, preserveWhiteSpace } );
record.current = create( {
html: preserveWhiteSpace ? value : collapseWhiteSpace( value ),
} );
if ( disableFormats ) {
record.current.formats = Array( value.length );
record.current.replacements = Array( value.length );
Expand Down
6 changes: 1 addition & 5 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ function toFormat( { tagName, attributes } ) {
* @param {string} [$1.text] Text to create value from.
* @param {string} [$1.html] HTML to create value from.
* @param {Range} [$1.range] Range to create value from.
* @param {boolean} [$1.preserveWhiteSpace] Whether or not to collapse
* white space characters.
* @param {boolean} [$1.__unstableIsEditableTree]
*
* @return {RichTextValue} A rich text value.
Expand All @@ -147,7 +145,6 @@ export function create( {
html,
range,
__unstableIsEditableTree: isEditableTree,
preserveWhiteSpace,
} = {} ) {
if ( typeof text === 'string' && text.length > 0 ) {
return {
Expand All @@ -158,7 +155,6 @@ export function create( {
}

if ( typeof html === 'string' && html.length > 0 ) {
html = preserveWhiteSpace ? html : collapseWhiteSpace( html );
// It does not matter which document this is, we're just using it to
// parse.
element = createElement( document, html );
Expand Down Expand Up @@ -282,7 +278,7 @@ function filterRange( node, range, filter ) {
*
* @param {string} string
*/
function collapseWhiteSpace( string ) {
export function collapseWhiteSpace( string ) {
return string.replace( /[ \n\r\t]+/g, ' ' ).replace( /^ | $/g, '' );
}

Expand Down

0 comments on commit 2ef1e3a

Please sign in to comment.