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

Count non-ASCII comment characters by their encoded length #14752

Merged
merged 4 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,16 @@ function getNewMarkerReportActionID(report, sortedAndFilteredReportActions) {
: '';
}

/**
* Replace code points > 127 with C escape sequences, and return the resulting string's overall length
* Used for compatibility with the backend auth validator for AddComment
* @param {String} textComment
* @returns {Number}
*/
function commentLength(textComment) {
redstar504 marked this conversation as resolved.
Show resolved Hide resolved
return textComment.replace(/[^ -~]/g, '\\u????').length;
}

export {
getReportParticipantsTitle,
isReportMessageAttachment,
Expand Down Expand Up @@ -1425,4 +1435,5 @@ export {
getOldDotDefaultAvatar,
getNewMarkerReportActionID,
canSeeDefaultRoom,
commentLength,
};
7 changes: 4 additions & 3 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class ReportActionCompose extends React.Component {
const trimmedComment = this.comment.trim();

// Don't submit empty comments or comments that exceed the character limit
if (this.state.isCommentEmpty || trimmedComment.length > CONST.MAX_COMMENT_LENGTH) {
if (this.state.isCommentEmpty || ReportUtils.commentLength(trimmedComment) > CONST.MAX_COMMENT_LENGTH) {
redstar504 marked this conversation as resolved.
Show resolved Hide resolved
return '';
}

Expand Down Expand Up @@ -534,7 +534,8 @@ class ReportActionCompose extends React.Component {
const isComposeDisabled = this.props.isDrawerOpen && this.props.isSmallScreenWidth;
const isBlockedFromConcierge = ReportUtils.chatIncludesConcierge(this.props.report) && User.isBlockedFromConcierge(this.props.blockedFromConcierge);
const inputPlaceholder = this.getInputPlaceholder();
const hasExceededMaxCommentLength = this.comment.length > CONST.MAX_COMMENT_LENGTH;
const encodedCommentLength = ReportUtils.commentLength(this.comment);
const hasExceededMaxCommentLength = encodedCommentLength > CONST.MAX_COMMENT_LENGTH;

return (
<View style={[
Expand Down Expand Up @@ -729,7 +730,7 @@ class ReportActionCompose extends React.Component {
>
{!this.props.isSmallScreenWidth && <OfflineIndicator containerStyles={[styles.chatItemComposeSecondaryRow]} />}
<ReportTypingIndicator reportID={this.props.reportID} />
<ExceededCommentLength commentLength={this.comment.length} />
<ExceededCommentLength commentLength={encodedCommentLength} />
</View>
{this.state.isDraggingOver && <ReportDropUI />}
</View>
Expand Down
6 changes: 4 additions & 2 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ReportActionComposeFocusManager from '../../../libs/ReportActionComposeFo
import compose from '../../../libs/compose';
import EmojiPickerButton from '../../../components/EmojiPicker/EmojiPickerButton';
import * as ReportActionContextMenu from './ContextMenu/ReportActionContextMenu';
import * as ReportUtils from '../../../libs/ReportUtils';
import * as EmojiUtils from '../../../libs/EmojiUtils';
import reportPropTypes from '../../reportPropTypes';
import ExceededCommentLength from '../../../components/ExceededCommentLength';
Expand Down Expand Up @@ -214,7 +215,8 @@ class ReportActionItemMessageEdit extends React.Component {
}

render() {
const hasExceededMaxCommentLength = this.state.draft.length > CONST.MAX_COMMENT_LENGTH;
const draftLength = ReportUtils.commentLength(this.state.draft);
const hasExceededMaxCommentLength = draftLength > CONST.MAX_COMMENT_LENGTH;
return (
<View style={styles.chatItemMessage}>
<View
Expand Down Expand Up @@ -279,7 +281,7 @@ class ReportActionItemMessageEdit extends React.Component {
onPress={this.publishDraft}
text={this.props.translate('common.saveChanges')}
/>
<ExceededCommentLength commentLength={this.state.draft.length} />
<ExceededCommentLength commentLength={draftLength} />
</View>
</View>
);
Expand Down