diff --git a/packages/block-library/src/button/test/__snapshots__/index.js.snap b/packages/block-library/src/button/test/__snapshots__/index.js.snap
index ab72e31893bcd..fec3d15878b50 100644
--- a/packages/block-library/src/button/test/__snapshots__/index.js.snap
+++ b/packages/block-library/src/button/test/__snapshots__/index.js.snap
@@ -24,7 +24,7 @@ exports[`core/button block edit matches snapshot 1`] = `
role="textbox"
>
node.getAttribute( 'data-mce-bogus' ) === 'all', - unwrapNode: ( node ) => !! node.getAttribute( 'data-mce-bogus' ), - removeAttribute: ( attribute ) => attribute.indexOf( 'data-mce-' ) === 0, prepareEditableTree: this.props.prepareEditableTree, } ); } @@ -175,11 +172,6 @@ export class RichText extends Component { current: this.editableRef, multilineTag: this.multilineTag, multilineWrapperTags: this.multilineWrapperTags, - createLinePadding( doc ) { - const element = doc.createElement( 'br' ); - element.setAttribute( 'data-mce-bogus', '1' ); - return element; - }, prepareEditableTree: this.props.prepareEditableTree, } ); } @@ -740,11 +732,6 @@ export class RichText extends Component { value, multilineTag: this.multilineTag, multilineWrapperTags: this.multilineWrapperTags, - createLinePadding( doc ) { - const element = doc.createElement( 'br' ); - element.setAttribute( 'data-mce-bogus', '1' ); - return element; - }, prepareEditableTree: this.props.prepareEditableTree, } ).body.innerHTML; } diff --git a/packages/rich-text/src/create.js b/packages/rich-text/src/create.js index 4dcd1f4e5a6c5..6a74012979e78 100644 --- a/packages/rich-text/src/create.js +++ b/packages/rich-text/src/create.js @@ -99,12 +99,6 @@ function toFormat( { type, attributes } ) { * multiline. * @param {?Array} $1.multilineWrapperTags Tags where lines can be found if * nesting is possible. - * @param {?Function} $1.removeNode Function to declare whether the - * given node should be removed. - * @param {?Function} $1.unwrapNode Function to declare whether the - * given node should be unwrapped. - * @param {?Function} $1.removeAttribute Wether to remove an attribute - * based on the name. * * @return {Object} A rich text value. */ @@ -115,9 +109,6 @@ export function create( { range, multilineTag, multilineWrapperTags, - removeNode, - unwrapNode, - removeAttribute, } = {} ) { if ( typeof text === 'string' && text.length > 0 ) { return { @@ -138,9 +129,6 @@ export function create( { return createFromElement( { element, range, - removeNode, - unwrapNode, - removeAttribute, } ); } @@ -149,9 +137,6 @@ export function create( { range, multilineTag, multilineWrapperTags, - removeNode, - unwrapNode, - removeAttribute, } ); } @@ -267,12 +252,6 @@ function filterString( string ) { * multiline. * @param {?Array} $1.multilineWrapperTags Tags where lines can be found if * nesting is possible. - * @param {?Function} $1.removeNode Function to declare whether the - * given node should be removed. - * @param {?Function} $1.unwrapNode Function to declare whether the - * given node should be unwrapped. - * @param {?Function} $1.removeAttribute Wether to remove an attribute - * based on the name. * * @return {Object} A rich text value. */ @@ -282,9 +261,6 @@ function createFromElement( { multilineTag, multilineWrapperTags, currentWrapperTags = [], - removeNode, - unwrapNode, - removeAttribute, } ) { const accumulator = createEmptyValue(); @@ -319,10 +295,7 @@ function createFromElement( { continue; } - if ( - ( removeNode && removeNode( node ) ) || - ( unwrapNode && unwrapNode( node ) && ! node.hasChildNodes() ) - ) { + if ( node.getAttribute( 'data-rich-text-padding' ) ) { accumulateSelection( accumulator, node, range, createEmptyValue() ); continue; } @@ -336,37 +309,30 @@ function createFromElement( { const lastFormats = accumulator.formats[ accumulator.formats.length - 1 ]; const lastFormat = lastFormats && lastFormats[ lastFormats.length - 1 ]; - let format; - let value; + const newFormat = toFormat( { + type, + attributes: getAttributes( { element: node } ), + } ); - if ( ! unwrapNode || ! unwrapNode( node ) ) { - const newFormat = toFormat( { - type, - attributes: getAttributes( { - element: node, - removeAttribute, - } ), - } ); + let format; - if ( newFormat ) { - // Reuse the last format if it's equal. - if ( isFormatEqual( newFormat, lastFormat ) ) { - format = lastFormat; - } else { - format = newFormat; - } + if ( newFormat ) { + // Reuse the last format if it's equal. + if ( isFormatEqual( newFormat, lastFormat ) ) { + format = lastFormat; + } else { + format = newFormat; } } + let value; + if ( multilineWrapperTags && multilineWrapperTags.indexOf( type ) !== -1 ) { value = createFromMultilineElement( { element: node, range, multilineTag, multilineWrapperTags, - removeNode, - unwrapNode, - removeAttribute, currentWrapperTags: [ ...currentWrapperTags, format ], } ); format = undefined; @@ -376,9 +342,6 @@ function createFromElement( { range, multilineTag, multilineWrapperTags, - removeNode, - unwrapNode, - removeAttribute, } ); } @@ -446,12 +409,6 @@ function createFromElement( { * multiline. * @param {?Array} $1.multilineWrapperTags Tags where lines can be found if * nesting is possible. - * @param {?Function} $1.removeNode Function to declare whether the - * given node should be removed. - * @param {?Function} $1.unwrapNode Function to declare whether the - * given node should be unwrapped. - * @param {?Function} $1.removeAttribute Wether to remove an attribute - * based on the name. * @param {boolean} $1.currentWrapperTags Whether to prepend a line * separator. * @@ -462,9 +419,6 @@ function createFromMultilineElement( { range, multilineTag, multilineWrapperTags, - removeNode, - unwrapNode, - removeAttribute, currentWrapperTags = [], } ) { const accumulator = createEmptyValue(); @@ -489,9 +443,6 @@ function createFromMultilineElement( { multilineTag, multilineWrapperTags, currentWrapperTags, - removeNode, - unwrapNode, - removeAttribute, } ); // If a line consists of one single line break (invisible), consider the @@ -532,16 +483,11 @@ function createFromMultilineElement( { * * @param {Object} $1 Named argements. * @param {Element} $1.element Element to get attributes from. - * @param {?Function} $1.removeAttribute Wether to remove an attribute based on - * the name. * * @return {?Object} Attribute object or `undefined` if the element has no * attributes. */ -function getAttributes( { - element, - removeAttribute, -} ) { +function getAttributes( { element } ) { if ( ! element.hasAttributes() ) { return; } @@ -553,7 +499,7 @@ function getAttributes( { for ( let i = 0; i < length; i++ ) { const { name, value } = element.attributes[ i ]; - if ( removeAttribute && removeAttribute( name ) ) { + if ( name === 'data-rich-text-format-boundary' ) { continue; } diff --git a/packages/rich-text/src/test/__snapshots__/to-dom.js.snap b/packages/rich-text/src/test/__snapshots__/to-dom.js.snap index 5c531882df419..8a20d0865a0dd 100644 --- a/packages/rich-text/src/test/__snapshots__/to-dom.js.snap +++ b/packages/rich-text/src/test/__snapshots__/to-dom.js.snap @@ -3,7 +3,7 @@ exports[`recordToDom should create a value with formatting 1`] = ` test @@ -14,7 +14,7 @@ exports[`recordToDom should create a value with formatting 1`] = ` exports[`recordToDom should create a value with formatting for split tags 1`] = ` test @@ -25,7 +25,7 @@ exports[`recordToDom should create a value with formatting for split tags 1`] = exports[`recordToDom should create a value with formatting with attributes 1`] = ` test @@ -85,7 +85,7 @@ exports[`recordToDom should create a value with nested formatting 1`] = ` test @@ -103,21 +103,25 @@ exports[`recordToDom should create a value without formatting 1`] = ` exports[`recordToDom should create an empty value 1`] = ` -
+
`; exports[`recordToDom should create an empty value from empty tags 1`] = ` -
+
`; -exports[`recordToDom should filter format attributes with settings 1`] = ` +exports[`recordToDom should filter format boundary attributes 1`] = ` test @@ -125,22 +129,22 @@ exports[`recordToDom should filter format attributes with settings 1`] = ` `; -exports[`recordToDom should filter text at end with settings 1`] = ` +exports[`recordToDom should filter zero width space 1`] = ` - test + +
`; -exports[`recordToDom should filter text in format with settings 1`] = ` +exports[`recordToDom should filter zero width space at end 1`] = ` - - test - - + test `; -exports[`recordToDom should filter text outside format with settings 1`] = ` +exports[`recordToDom should filter zero width space in format 1`] = ` test @@ -149,10 +153,12 @@ exports[`recordToDom should filter text outside format with settings 1`] = ` `; -exports[`recordToDom should filter text with settings 1`] = ` +exports[`recordToDom should filter zero width space outside format 1`] = ` + + test + -
`; @@ -167,7 +173,7 @@ exports[`recordToDom should handle br 1`] = ` exports[`recordToDom should handle br with formatting 1`] = `
@@ -199,7 +205,9 @@ exports[`recordToDom should handle empty list value 1`] = `- `; @@ -208,7 +216,9 @@ exports[`recordToDom should handle empty multiline value 1`] = `
+
-
`; @@ -217,15 +227,21 @@ exports[`recordToDom should handle middle empty list value 1`] = `
+
-
+
-
+
- `; @@ -283,7 +299,9 @@ exports[`recordToDom should handle multiline value with empty 1`] = `
+
-
`; @@ -291,11 +309,15 @@ exports[`recordToDom should handle multiline value with empty 1`] = ` exports[`recordToDom should handle nested empty list value 1`] = `
+
- @@ -337,7 +359,7 @@ exports[`recordToDom should preserve emoji 1`] = ` exports[`recordToDom should preserve emoji in formatting 1`] = ` 🍒 @@ -354,20 +376,9 @@ exports[`recordToDom should preserve non breaking space 1`] = ` exports[`recordToDom should remove br with settings 1`] = ` -
+
- -
+
- -`; - -exports[`recordToDom should remove with children with settings 1`] = ` - - two - -`; - -exports[`recordToDom should remove with settings 1`] = ` - - -
+
`; @@ -376,13 +387,3 @@ exports[`recordToDom should replace characters to format HTML with space 1`] = ` `; - -exports[`recordToDom should unwrap with settings 1`] = ` - - te - - st - - - -`; diff --git a/packages/rich-text/src/test/create.js b/packages/rich-text/src/test/create.js index e08f6b82be6b4..5128c55273d78 100644 --- a/packages/rich-text/src/test/create.js +++ b/packages/rich-text/src/test/create.js @@ -29,7 +29,6 @@ describe( 'create', () => { description, multilineTag, multilineWrapperTags, - settings, html, createRange, record, @@ -46,7 +45,6 @@ describe( 'create', () => { range, multilineTag, multilineWrapperTags, - ...settings, } ); const formatsLength = getSparseArrayLength( record.formats ); const createdFormatsLength = getSparseArrayLength( createdRecord.formats ); diff --git a/packages/rich-text/src/test/helpers/index.js b/packages/rich-text/src/test/helpers/index.js index 9673619d57be2..fcf3bd913ed33 100644 --- a/packages/rich-text/src/test/helpers/index.js +++ b/packages/rich-text/src/test/helpers/index.js @@ -527,27 +527,6 @@ export const spec = [ text: 'one', }, }, - { - description: 'should remove with settings', - settings: { - unwrapNode: ( node ) => !! node.getAttribute( 'data-mce-bogus' ), - }, - html: '', - createRange: ( element ) => ( { - startOffset: 0, - startContainer: element, - endOffset: 1, - endContainer: element, - } ), - startPath: [ 0, 0 ], - endPath: [ 0, 0 ], - record: { - start: 0, - end: 0, - formats: [], - text: '', - }, - }, { description: 'should ignore formats at line separator', multilineTag: 'p', @@ -560,10 +539,7 @@ export const spec = [ }, { description: 'should remove br with settings', - settings: { - unwrapNode: ( node ) => !! node.getAttribute( 'data-mce-bogus' ), - }, - html: '
', + html: '
', createRange: ( element ) => ( { startOffset: 0, startContainer: element, @@ -580,53 +556,8 @@ export const spec = [ }, }, { - description: 'should unwrap with settings', - settings: { - unwrapNode: ( node ) => !! node.getAttribute( 'data-mce-bogus' ), - }, - html: 'test', - createRange: ( element ) => ( { - startOffset: 0, - startContainer: element, - endOffset: 1, - endContainer: element, - } ), - startPath: [ 0, 0 ], - endPath: [ 1, 0, 2 ], - record: { - start: 0, - end: 4, - formats: [ , , [ em ], [ em ] ], - text: 'test', - }, - }, - { - description: 'should remove with children with settings', - settings: { - removeNode: ( node ) => node.getAttribute( 'data-mce-bogus' ) === 'all', - }, - html: 'onetwo', - createRange: ( element ) => ( { - startOffset: 0, - startContainer: element.lastChild, - endOffset: 1, - endContainer: element.lastChild, - } ), - startPath: [ 0, 0 ], - endPath: [ 0, 1 ], - record: { - start: 0, - end: 1, - formats: [ , , , ], - text: 'two', - }, - }, - { - description: 'should filter format attributes with settings', - settings: { - removeAttribute: ( attribute ) => attribute.indexOf( 'data-mce-' ) === 0, - }, - html: 'test', + description: 'should filter format boundary attributes', + html: 'test', createRange: ( element ) => ( { startOffset: 0, startContainer: element, @@ -643,10 +574,7 @@ export const spec = [ }, }, { - description: 'should filter text with settings', - settings: { - filterString: ( string ) => string.replace( '\uFEFF', '' ), - }, + description: 'should filter zero width space', html: '', createRange: ( element ) => ( { startOffset: 0, @@ -664,10 +592,7 @@ export const spec = [ }, }, { - description: 'should filter text at end with settings', - settings: { - filterString: ( string ) => string.replace( '\uFEFF', '' ), - }, + description: 'should filter zero width space at end', html: 'test', createRange: ( element ) => ( { startOffset: 4, @@ -685,10 +610,7 @@ export const spec = [ }, }, { - description: 'should filter text in format with settings', - settings: { - filterString: ( string ) => string.replace( '\uFEFF', '' ), - }, + description: 'should filter zero width space in format', html: 'test', createRange: ( element ) => ( { startOffset: 5, @@ -706,10 +628,7 @@ export const spec = [ }, }, { - description: 'should filter text outside format with settings', - settings: { - filterString: ( string ) => string.replace( '\uFEFF', '' ), - }, + description: 'should filter zero width space outside format', html: 'test', createRange: ( element ) => ( { startOffset: 1, diff --git a/packages/rich-text/src/test/to-dom.js b/packages/rich-text/src/test/to-dom.js index 16fed3f66d0a2..1df1e8e25b229 100644 --- a/packages/rich-text/src/test/to-dom.js +++ b/packages/rich-text/src/test/to-dom.js @@ -34,7 +34,6 @@ describe( 'recordToDom', () => { value: record, multilineTag, multilineWrapperTags, - createLinePadding: ( doc ) => doc.createElement( 'br' ), } ); expect( body ).toMatchSnapshot(); expect( selection ).toEqual( { startPath, endPath } ); diff --git a/packages/rich-text/src/to-dom.js b/packages/rich-text/src/to-dom.js index c811bbc618059..071f38aae2a19 100644 --- a/packages/rich-text/src/to-dom.js +++ b/packages/rich-text/src/to-dom.js @@ -113,7 +113,13 @@ function remove( node ) { return node.parentNode.removeChild( node ); } -function padEmptyLines( { element, createLinePadding, multilineWrapperTags } ) { +function createLinePadding( doc ) { + const element = doc.createElement( 'br' ); + element.setAttribute( 'data-rich-text-padding', 'true' ); + return element; +} + +function padEmptyLines( { element, multilineWrapperTags } ) { const length = element.childNodes.length; const doc = element.ownerDocument; @@ -135,7 +141,7 @@ function padEmptyLines( { element, createLinePadding, multilineWrapperTags } ) { element.insertBefore( createLinePadding( doc ), child ); } - padEmptyLines( { element: child, createLinePadding, multilineWrapperTags } ); + padEmptyLines( { element: child, multilineWrapperTags } ); } } } @@ -150,7 +156,6 @@ export function toDom( { value, multilineTag, multilineWrapperTags, - createLinePadding, prepareEditableTree, } ) { let startPath = []; @@ -180,9 +185,7 @@ export function toDom( { isEditableTree: true, } ); - if ( createLinePadding ) { - padEmptyLines( { element: tree, createLinePadding, multilineWrapperTags } ); - } + padEmptyLines( { element: tree, multilineWrapperTags } ); return { body: tree, @@ -205,7 +208,6 @@ export function apply( { current, multilineTag, multilineWrapperTags, - createLinePadding, prepareEditableTree, } ) { // Construct a new element tree in memory. @@ -213,7 +215,6 @@ export function apply( { value, multilineTag, multilineWrapperTags, - createLinePadding, prepareEditableTree, } ); diff --git a/packages/rich-text/src/to-tree.js b/packages/rich-text/src/to-tree.js index 5928d1b35c5a3..36b24dd5b49da 100644 --- a/packages/rich-text/src/to-tree.js +++ b/packages/rich-text/src/to-tree.js @@ -164,7 +164,7 @@ export function toTree( { const { type, attributes = {}, unregisteredAttributes, object } = format; if ( isEditableTree && ! object && format === deepestActiveFormat ) { - attributes[ 'data-mce-selected' ] = 'inline-boundary'; + attributes[ 'data-rich-text-format-boundary' ] = 'true'; } const parent = getParent( pointer );