Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove rootTagsToEliminate prop from native Rich Text component #48983

Merged
merged 3 commits into from
Apr 3, 2023
Merged
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
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