diff --git a/packages/rich-text/src/create.js b/packages/rich-text/src/create.js index fdbbde943aef1a..9fbed28072764c 100644 --- a/packages/rich-text/src/create.js +++ b/packages/rich-text/src/create.js @@ -14,6 +14,7 @@ import { LINE_SEPARATOR, OBJECT_REPLACEMENT_CHARACTER, } from './special-characters'; +import { replace } from './replace'; /** * Browser dependencies @@ -81,6 +82,22 @@ function toFormat( { type, attributes } ) { }; } +/** + * Given a value, removes trailing spaces, because they will not be displaged + * by the browser. + * + * @param {Object} value Value to trim. + * + * @return {Object} Trimmed value. + */ +function trim( value ) { + value = replace( value, /^ +/, '' ); + value = replace( value, / +$/, '' ); + value = replace( value, / {2,}/, ' ' ); + + return value; +} + /** * Create a RichText value from an `Element` tree (DOM), an HTML string or a * plain text string, with optionally a `Range` object to set the selection. If @@ -137,14 +154,14 @@ export function create( { } if ( ! multilineTag ) { - return createFromElement( { + return trim( createFromElement( { element, range, removeNode, unwrapNode, filterString, removeAttribute, - } ); + } ) ); } return createFromMultilineElement( { @@ -297,11 +314,10 @@ function createFromElement( { const length = element.childNodes.length; - // Remove any line breaks in text nodes. They are not content, but used to - // format the HTML. Line breaks in HTML are stored as BR elements. - // See https://www.w3.org/TR/html5/syntax.html#newlines. const filterStringComplete = ( string ) => { - string = string.replace( /[\r\n]/g, '' ); + // Reduce any whitespace used for HTML formatting to one space + // character, because it will also be displayed as such by the browser. + string = string.replace( /[\n\r\t ]+/g, ' ' ); if ( filterString ) { string = filterString( string ); @@ -499,7 +515,7 @@ function createFromMultilineElement( { continue; } - let value = createFromElement( { + const value = trim( createFromElement( { element: node, range, multilineTag, @@ -509,24 +525,7 @@ function createFromMultilineElement( { unwrapNode, filterString, removeAttribute, - } ); - - // If a line consists of one single line break (invisible), consider the - // line empty, wether this is the browser's doing or not. - if ( value.text === '\n' ) { - const start = value.start; - const end = value.end; - - value = createEmptyValue(); - - if ( start !== undefined ) { - value.start = 0; - } - - if ( end !== undefined ) { - value.end = 0; - } - } + } ) ); // Multiline value text should be separated by a double line break. if ( index !== 0 || currentWrapperTags.length > 0 ) { diff --git a/packages/rich-text/src/replace.js b/packages/rich-text/src/replace.js index 110fc186bd6386..3934ff2e731c6e 100644 --- a/packages/rich-text/src/replace.js +++ b/packages/rich-text/src/replace.js @@ -20,35 +20,64 @@ import { normaliseFormats } from './normalise-formats'; * * @return {Object} A new value with replacements applied. */ -export function replace( { formats, text, start, end }, pattern, replacement ) { - text = text.replace( pattern, ( match, ...rest ) => { +export function replace( value, pattern, replacement ) { + const { formats, text, start, end } = value; + + let newFormats = formats.slice(); + let newStart = start; + let newEnd = end; + + const newText = text.replace( pattern, ( match, ...rest ) => { const offset = rest[ rest.length - 2 ]; - let newText = replacement; - let newFormats; + let replacementText; + let replacementFormats; - if ( typeof newText === 'function' ) { - newText = replacement( match, ...rest ); + if ( typeof replacement === 'function' ) { + replacement = replacement( match, ...rest ); } - if ( typeof newText === 'object' ) { - newFormats = newText.formats; - newText = newText.text; + if ( typeof replacement === 'object' ) { + replacementText = replacement.text; + replacementFormats = replacement.formats; } else { - newFormats = Array( newText.length ); + replacementText = replacement; + replacementFormats = Array( replacementText.length ); - if ( formats[ offset ] ) { - newFormats = newFormats.fill( formats[ offset ] ); + if ( newFormats[ offset ] ) { + replacementFormats = replacementFormats.fill( newFormats[ offset ] ); } } - formats = formats.slice( 0, offset ).concat( newFormats, formats.slice( offset + match.length ) ); + newFormats = newFormats.slice( 0, offset ).concat( replacementFormats, newFormats.slice( offset + match.length ) ); + + if ( newStart !== undefined && newStart >= offset ) { + if ( newStart <= offset + match.length ) { + newStart = offset + replacementText.length; + } else { + newStart = newStart - match.length + replacementText.length; + } + } - if ( start ) { - start = end = offset + newText.length; + if ( newEnd !== undefined && newEnd >= offset ) { + if ( newEnd <= offset + match.length ) { + newEnd = offset + replacementText.length; + } else { + newEnd = newEnd - match.length + replacementText.length; + } } - return newText; + return replacementText; } ); - return normaliseFormats( { formats, text, start, end } ); + // No replacement occured. + if ( text === newText ) { + return value; + } + + return normaliseFormats( { + formats: newFormats, + text: newText, + start: newStart, + end: newEnd, + } ); }