Skip to content

Commit

Permalink
Remove rootTagsToEliminate prop from native Rich Text component (#48983)
Browse files Browse the repository at this point in the history
* Remove rootTagsToEliminate prop from Rich Text

* Fix typo in removeRootTagsProducedByAztec function name
  • Loading branch information
derekblank authored Apr 3, 2023
1 parent d0fa047 commit c60c26c
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const Caption = ( {
onChange={ onChange }
placeholder={ placeholder }
placeholderTextColor={ placeholderTextColor }
rootTagsToEliminate={ [ 'p' ] }
style={ style }
tagName="p"
textAlign="center"
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function removeNativeProps( props ) {
textAlign,
selectionColor,
tagsToEliminate,
rootTagsToEliminate,
disableEditingMenu,
fontSize,
fontFamily,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ function RichTextWrapper(
textAlign,
selectionColor,
tagsToEliminate,
rootTagsToEliminate,
disableEditingMenu,
fontSize,
fontFamily,
Expand Down Expand Up @@ -628,7 +627,6 @@ function RichTextWrapper(
textAlign={ textAlign }
selectionColor={ selectionColor }
tagsToEliminate={ tagsToEliminate }
rootTagsToEliminate={ rootTagsToEliminate }
disableEditingMenu={ disableEditingMenu }
fontSize={ fontSize }
fontFamily={ fontFamily }
Expand Down
2 changes: 0 additions & 2 deletions packages/block-library/src/file/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ export class FileEdit extends Component {
__unstableMobileNoFocusOnMount
onChange={ this.onChangeFileName }
placeholder={ __( 'File name' ) }
rootTagsToEliminate={ [ 'p' ] }
tagName="p"
underlineColorAndroid="transparent"
value={ fileName }
Expand Down Expand Up @@ -502,7 +501,6 @@ export class FileEdit extends Component {
<RichText
withoutInteractiveFormatting
__unstableMobileNoFocusOnMount
rootTagsToEliminate={ [ 'p' ] }
tagName="p"
textAlign="center"
minWidth={ minWidth }
Expand Down
19 changes: 8 additions & 11 deletions packages/rich-text/src/component/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class RichText extends Component {

valueToFormat( value ) {
// Remove the outer root tags.
return this.removeRootTagsProduceByAztec(
return this.removeRootTagsProducedByAztec(
toHTMLString( {
value,
multilineTag: this.multilineTag,
Expand Down Expand Up @@ -272,30 +272,27 @@ export class RichText extends Component {
* Cleans up any root tags produced by aztec.
* TODO: This should be removed on a later version when aztec doesn't return the top tag of the text being edited
*/
removeRootTagsProduceByAztec( html ) {
removeRootTagsProducedByAztec( html ) {
let result = this.removeRootTag( this.props.tagName, html );
// Temporary workaround for https://github.com/WordPress/gutenberg/pull/13763
if ( this.props.rootTagsToEliminate ) {
this.props.rootTagsToEliminate.forEach( ( element ) => {
result = this.removeRootTag( element, result );
} );
}

if ( this.props.tagsToEliminate ) {
this.props.tagsToEliminate.forEach( ( element ) => {
result = this.removeTag( element, result );
} );
}

return result;
}

removeRootTag( tag, html ) {
const openingTagRegexp = RegExp( '^<' + tag + '[^>]*>', 'gim' );
const closingTagRegexp = RegExp( '</' + tag + '>$', 'gim' );

return html
.replace( openingTagRegexp, '' )
.replace( closingTagRegexp, '' );
}

removeTag( tag, html ) {
const openingTagRegexp = RegExp( '<' + tag + '>', 'gim' );
const closingTagRegexp = RegExp( '</' + tag + '>', 'gim' );
Expand All @@ -312,7 +309,7 @@ export class RichText extends Component {
return;
}

const contentWithoutRootTag = this.removeRootTagsProduceByAztec(
const contentWithoutRootTag = this.removeRootTagsProducedByAztec(
unescapeSpaces( event.nativeEvent.text )
);
// On iOS, onChange can be triggered after selection changes, even though there are no content changes.
Expand All @@ -327,7 +324,7 @@ export class RichText extends Component {
}

onTextUpdate( event ) {
const contentWithoutRootTag = this.removeRootTagsProduceByAztec(
const contentWithoutRootTag = this.removeRootTagsProducedByAztec(
unescapeSpaces( event.nativeEvent.text )
);
let formattedContent = contentWithoutRootTag;
Expand Down Expand Up @@ -652,7 +649,7 @@ export class RichText extends Component {
const realEnd = Math.max( start, end );

// Check and dicsard stray event, where the text and selection is equal to the ones already cached.
const contentWithoutRootTag = this.removeRootTagsProduceByAztec(
const contentWithoutRootTag = this.removeRootTagsProducedByAztec(
unescapeSpaces( event.nativeEvent.text )
);
if (
Expand Down

0 comments on commit c60c26c

Please sign in to comment.