Skip to content

Commit 711f1f7

Browse files
authored
[RN Mobile] Fix Rich-Text setSelection call on Android when BR tags at the end of content (#18138)
* Make sure to detect BR tags before closing P tag, and remove them from the Selection indexes on Android * Use the same RegExp of Aztec * Add comment and use grouping on the matching regexp * Make the logic that re-set the selection indexes more clear
1 parent c3af33c commit 711f1f7

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

packages/rich-text/src/component/index.native.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,25 @@ export class RichText extends Component {
652652
} else if ( this.props.selectionStart > record.text.length || this.props.selectionEnd > record.text.length ) {
653653
console.warn( 'Oops, selection will land outside the text, skipping setting it...' );
654654
selection = null;
655+
} else {
656+
// The following regular expression is used in Aztec here:
657+
// https://github.com/wordpress-mobile/AztecEditor-Android/blob/b1fad439d56fa6d4aa0b78526fef355c59d00dd3/aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt#L656
658+
const brBeforeParaMatches = html.match( /(<br>)+<\/p>$/g );
659+
if ( brBeforeParaMatches ) {
660+
console.warn( 'Oops, BR tag(s) at the end of content. Aztec will remove them, adapting the selection...' );
661+
const count = ( brBeforeParaMatches[ 0 ].match( /br/g ) || [] ).length;
662+
if ( count > 0 ) {
663+
let newSelectionStart = this.props.selectionStart - count;
664+
if ( newSelectionStart < 0 ) {
665+
newSelectionStart = 0;
666+
}
667+
let newSelectionEnd = this.props.selectionEnd - count;
668+
if ( newSelectionEnd < 0 ) {
669+
newSelectionEnd = 0;
670+
}
671+
selection = { start: newSelectionStart, end: newSelectionEnd };
672+
}
673+
}
655674
}
656675
}
657676
}

0 commit comments

Comments
 (0)