From a37ffe392d5cee22dd0d4488da594c280a40d410 Mon Sep 17 00:00:00 2001 From: Milutin Kristofic Date: Fri, 28 Apr 2023 10:44:42 +0200 Subject: [PATCH] User Suggested edit - workaround for preserving tabs Markedjs by default changes leading tabs to spaces. More details: https://github.com/markedjs/marked/issues/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 --- .../gr-formatted-text/gr-formatted-text.ts | 16 +++++++++++++--- polygerrit-ui/app/utils/comment-util.ts | 14 +++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts index e230faf21e6..6e0a11f0b7f 100644 --- a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts +++ b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts @@ -21,7 +21,10 @@ import '../gr-account-chip/gr-account-chip'; import '../gr-user-suggestion-fix/gr-user-suggestion-fix'; import {KnownExperimentId} from '../../../services/flags/flags'; import {getAppContext} from '../../../services/app-context'; -import {USER_SUGGESTION_INFO_STRING} from '../../../utils/comment-util'; +import { + getUserSuggestionFromString, + USER_SUGGESTION_INFO_STRING, +} from '../../../utils/comment-util'; /** * This element optionally renders markdown and also applies some regex @@ -298,9 +301,16 @@ export class GrFormattedText extends LitElement { } private convertCodeToSuggestions() { - for (const userSuggestionMark of this.renderRoot.querySelectorAll('mark')) { + const marks = this.renderRoot.querySelectorAll('mark'); + if (marks.length > 0) { + const userSuggestionMark = marks[0]; const userSuggestion = document.createElement('gr-user-suggestion-fix'); - userSuggestion.textContent = userSuggestionMark.textContent ?? ''; + // Temporary workaround for bug - tabs replacement + if (this.content.includes('\t')) { + userSuggestion.textContent = getUserSuggestionFromString(this.content); + } else { + userSuggestion.textContent = userSuggestionMark.textContent ?? ''; + } userSuggestionMark.parentNode?.replaceChild( userSuggestion, userSuggestionMark diff --git a/polygerrit-ui/app/utils/comment-util.ts b/polygerrit-ui/app/utils/comment-util.ts index 4646bf619e3..ee1a44c2e7f 100644 --- a/polygerrit-ui/app/utils/comment-util.ts +++ b/polygerrit-ui/app/utils/comment-util.ts @@ -476,13 +476,17 @@ export function hasUserSuggestion(comment: Comment) { return comment.message?.includes(USER_SUGGESTION_START_PATTERN) ?? false; } -export function getUserSuggestion(comment: Comment) { - if (!comment.message) return; +export function getUserSuggestionFromString(content: string) { const start = - comment.message.indexOf(USER_SUGGESTION_START_PATTERN) + + content.indexOf(USER_SUGGESTION_START_PATTERN) + USER_SUGGESTION_START_PATTERN.length; - const end = comment.message.indexOf('\n```', start); - return comment.message.substring(start, end); + const end = content.indexOf('\n```', start); + return content.substring(start, end); +} + +export function getUserSuggestion(comment: Comment) { + if (!comment.message) return; + return getUserSuggestionFromString(comment.message); } export function getContentInCommentRange(