Skip to content

Commit a37ffe3

Browse files
committed
User Suggested edit - workaround for preserving tabs
Markedjs by default changes leading tabs to spaces. More details: markedjs/marked#1559 As workaround we extract user suggestion from content of gr-formatted-text which has tabs and not spaces. Release-Notes: skip Google-Bug-Id: b/279925682 Change-Id: I0a6d0223b384838b29753c41437b9174dda0da46
1 parent 2253500 commit a37ffe3

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import '../gr-account-chip/gr-account-chip';
2121
import '../gr-user-suggestion-fix/gr-user-suggestion-fix';
2222
import {KnownExperimentId} from '../../../services/flags/flags';
2323
import {getAppContext} from '../../../services/app-context';
24-
import {USER_SUGGESTION_INFO_STRING} from '../../../utils/comment-util';
24+
import {
25+
getUserSuggestionFromString,
26+
USER_SUGGESTION_INFO_STRING,
27+
} from '../../../utils/comment-util';
2528

2629
/**
2730
* This element optionally renders markdown and also applies some regex
@@ -298,9 +301,16 @@ export class GrFormattedText extends LitElement {
298301
}
299302

300303
private convertCodeToSuggestions() {
301-
for (const userSuggestionMark of this.renderRoot.querySelectorAll('mark')) {
304+
const marks = this.renderRoot.querySelectorAll('mark');
305+
if (marks.length > 0) {
306+
const userSuggestionMark = marks[0];
302307
const userSuggestion = document.createElement('gr-user-suggestion-fix');
303-
userSuggestion.textContent = userSuggestionMark.textContent ?? '';
308+
// Temporary workaround for bug - tabs replacement
309+
if (this.content.includes('\t')) {
310+
userSuggestion.textContent = getUserSuggestionFromString(this.content);
311+
} else {
312+
userSuggestion.textContent = userSuggestionMark.textContent ?? '';
313+
}
304314
userSuggestionMark.parentNode?.replaceChild(
305315
userSuggestion,
306316
userSuggestionMark

polygerrit-ui/app/utils/comment-util.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,17 @@ export function hasUserSuggestion(comment: Comment) {
476476
return comment.message?.includes(USER_SUGGESTION_START_PATTERN) ?? false;
477477
}
478478

479-
export function getUserSuggestion(comment: Comment) {
480-
if (!comment.message) return;
479+
export function getUserSuggestionFromString(content: string) {
481480
const start =
482-
comment.message.indexOf(USER_SUGGESTION_START_PATTERN) +
481+
content.indexOf(USER_SUGGESTION_START_PATTERN) +
483482
USER_SUGGESTION_START_PATTERN.length;
484-
const end = comment.message.indexOf('\n```', start);
485-
return comment.message.substring(start, end);
483+
const end = content.indexOf('\n```', start);
484+
return content.substring(start, end);
485+
}
486+
487+
export function getUserSuggestion(comment: Comment) {
488+
if (!comment.message) return;
489+
return getUserSuggestionFromString(comment.message);
486490
}
487491

488492
export function getContentInCommentRange(

0 commit comments

Comments
 (0)