From 421876c36d67b596d8a894f8d760f7a3112f4353 Mon Sep 17 00:00:00 2001 From: iseulde Date: Mon, 4 Feb 2019 14:48:39 +0100 Subject: [PATCH] Do not mutate attirbutes object --- packages/rich-text/src/to-tree.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/rich-text/src/to-tree.js b/packages/rich-text/src/to-tree.js index 9a2aa9caf01f1..613c8a7bef2cf 100644 --- a/packages/rich-text/src/to-tree.js +++ b/packages/rich-text/src/to-tree.js @@ -9,14 +9,24 @@ import { ZERO_WIDTH_NO_BREAK_SPACE, } from './special-characters'; -function fromFormat( { type, attributes, unregisteredAttributes, object } ) { +function fromFormat( { type, attributes, unregisteredAttributes, object, boundaryClass } ) { const formatType = getFormatType( type ); + let elementAttributes = {}; + + if ( boundaryClass ) { + elementAttributes[ 'data-rich-text-format-boundary' ] = 'true'; + } + if ( ! formatType ) { - return { type, attributes, object }; + if ( attributes ) { + elementAttributes = { ...attributes, ...elementAttributes }; + } + + return { type, attributes: elementAttributes, object }; } - const elementAttributes = { ...unregisteredAttributes }; + elementAttributes = { ...unregisteredAttributes, ...elementAttributes }; for ( const name in attributes ) { const key = formatType.attributes ? formatType.attributes[ name ] : false; @@ -195,16 +205,14 @@ export function toTree( { return; } - const { type, attributes = {}, unregisteredAttributes, object } = format; + const { type, attributes, unregisteredAttributes, object } = format; - if ( + const boundaryClass = ( isEditableTree && ! object && character !== LINE_SEPARATOR && format === deepestActiveFormat - ) { - attributes[ 'data-rich-text-format-boundary' ] = 'true'; - } + ); const parent = getParent( pointer ); const newNode = append( parent, fromFormat( { @@ -212,6 +220,7 @@ export function toTree( { attributes, unregisteredAttributes, object, + boundaryClass, } ) ); if ( isText( pointer ) && getText( pointer ).length === 0 ) {