Skip to content
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
49 changes: 24 additions & 25 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
LINE_SEPARATOR,
OBJECT_REPLACEMENT_CHARACTER,
} from './special-characters';
import { replace } from './replace';

/**
* Browser dependencies
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -137,14 +154,14 @@ export function create( {
}

if ( ! multilineTag ) {
return createFromElement( {
return trim( createFromElement( {
element,
range,
removeNode,
unwrapNode,
filterString,
removeAttribute,
} );
} ) );
}

return createFromMultilineElement( {
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -499,7 +515,7 @@ function createFromMultilineElement( {
continue;
}

let value = createFromElement( {
const value = trim( createFromElement( {
element: node,
range,
multilineTag,
Expand All @@ -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 ) {
Expand Down
63 changes: 46 additions & 17 deletions packages/rich-text/src/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} );
}