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

Integrate Mobile 1.28.1 release into master #22607

Merged
merged 5 commits into from
May 26, 2020
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
87 changes: 21 additions & 66 deletions packages/rich-text/src/component/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export class RichText extends Component {
this
);
this.valueToFormat = this.valueToFormat.bind( this );
this.willTrimSpaces = this.willTrimSpaces.bind( this );
this.getHtmlToRender = this.getHtmlToRender.bind( this );
this.insertString = this.insertString.bind( this );
this.state = {
Expand Down Expand Up @@ -647,29 +646,6 @@ export class RichText extends Component {
}
}

willTrimSpaces( html ) {
const { tagName } = this.props;

// aztec won't trim spaces in a case of <pre> block, so we are excluding it
if ( tagName === 'pre' ) {
return false;
}

// regex for detecting spaces around block element html tags
const blockHtmlElements =
'(div|br|blockquote|ul|ol|li|p|pre|h1|h2|h3|h4|h5|h6|iframe|hr)';
const leadingOrTrailingSpaces = new RegExp(
`(\\s+)<\/?${ blockHtmlElements }>|<\/?${ blockHtmlElements }>(\\s+)`,
'g'
);
const matches = html.match( leadingOrTrailingSpaces );
if ( matches && matches.length > 0 ) {
return true;
}

return false;
}

getHtmlToRender( record, tagName ) {
// Save back to HTML from React tree
let value = this.valueToFormat( record );
Expand Down Expand Up @@ -739,51 +715,30 @@ export class RichText extends Component {

// On AztecAndroid, setting the caret to an out-of-bounds position will crash the editor so, let's check for some cases.
if ( ! this.isIOS ) {
// Aztec performs some html text cleanup while parsing it so, its internal representation gets out-of-sync with the
// representation of the format-lib on the RN side. We need to avoid trying to set the caret position because it may
// be outside the text bounds and crash Aztec, at least on Android.
if ( this.willTrimSpaces( html ) ) {
// the html will get trimmed by the cleaning up functions in Aztec and caret position will get out-of-sync.
// So, skip forcing it, let Aztec just do its best and just log the fact.
console.warn(
'RichText value will be trimmed for spaces! Avoiding setting the caret position manually.'
);
selection = null;
} else if (
this.props.selectionStart > record.text.length ||
this.props.selectionEnd > record.text.length
) {
// The following regular expression is used in Aztec here:
// https://github.com/wordpress-mobile/AztecEditor-Android/blob/b1fad439d56fa6d4aa0b78526fef355c59d00dd3/aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt#L656
const brBeforeParaMatches = html.match( /(<br>)+<\/p>$/g );
if ( brBeforeParaMatches ) {
console.warn(
'Oops, selection will land outside the text, skipping setting it...'
'Oops, BR tag(s) at the end of content. Aztec will remove them, adapting the selection...'
);
selection = null;
} else {
// The following regular expression is used in Aztec here:
// https://github.com/wordpress-mobile/AztecEditor-Android/blob/b1fad439d56fa6d4aa0b78526fef355c59d00dd3/aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt#L656
const brBeforeParaMatches = html.match( /(<br>)+<\/p>$/g );
if ( brBeforeParaMatches ) {
console.warn(
'Oops, BR tag(s) at the end of content. Aztec will remove them, adapting the selection...'
);
const count = (
brBeforeParaMatches[ 0 ].match( /br/g ) || []
).length;
if ( count > 0 ) {
let newSelectionStart =
this.props.selectionStart - count;
if ( newSelectionStart < 0 ) {
newSelectionStart = 0;
}
let newSelectionEnd =
this.props.selectionEnd - count;
if ( newSelectionEnd < 0 ) {
newSelectionEnd = 0;
}
selection = {
start: newSelectionStart,
end: newSelectionEnd,
};
const count = (
brBeforeParaMatches[ 0 ].match( /br/g ) || []
).length;
if ( count > 0 ) {
let newSelectionStart =
this.props.selectionStart - count;
if ( newSelectionStart < 0 ) {
newSelectionStart = 0;
}
let newSelectionEnd = this.props.selectionEnd - count;
if ( newSelectionEnd < 0 ) {
newSelectionEnd = 0;
}
selection = {
start: newSelectionStart,
end: newSelectionEnd,
};
}
}
}
Expand Down
34 changes: 0 additions & 34 deletions packages/rich-text/src/component/test/index.native.js

This file was deleted.