From 380de23122f9a5dc84d0ef9cb69121de63cbf7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 31 Aug 2024 19:43:15 +0200 Subject: [PATCH] pl-fe: don't strip quote-inline when quote is not included MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-fe/src/normalizers/status.ts | 6 +++--- packages/pl-fe/src/utils/html.ts | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/pl-fe/src/normalizers/status.ts b/packages/pl-fe/src/normalizers/status.ts index 75eb47452..fa2b6181e 100644 --- a/packages/pl-fe/src/normalizers/status.ts +++ b/packages/pl-fe/src/normalizers/status.ts @@ -62,7 +62,7 @@ const buildSearchContent = (status: Pick DOMPurify.sanitize(stripCompatibilityFeatures(emojify(text, emojiMap)), { USE_PROFILES: { html: true } }); +const calculateContent = (text: string, emojiMap: any, hasQuote?: boolean) => DOMPurify.sanitize(stripCompatibilityFeatures(emojify(text, emojiMap), hasQuote), { USE_PROFILES: { html: true } }); const calculateSpoiler = (text: string, emojiMap: any) => DOMPurify.sanitize(emojify(escapeTextContentForBrowser(text), emojiMap), { USE_PROFILES: { html: true } }); const calculateStatus = (status: BaseStatus, oldStatus?: OldStatus): CalculatedValues => { @@ -80,10 +80,10 @@ const calculateStatus = (status: BaseStatus, oldStatus?: OldStatus): CalculatedV return { search_index: domParser.parseFromString(searchContent, 'text/html').documentElement.textContent || '', - contentHtml: calculateContent(status.content, emojiMap), + contentHtml: calculateContent(status.content, emojiMap, !!status.quote), spoilerHtml: calculateSpoiler(status.spoiler_text, emojiMap), contentMapHtml: status.content_map - ? Object.fromEntries(Object.entries(status.content_map)?.map(([key, value]) => [key, calculateContent(value, emojiMap)])) + ? Object.fromEntries(Object.entries(status.content_map)?.map(([key, value]) => [key, calculateContent(value, emojiMap, !!status.quote)])) : undefined, spoilerMapHtml: status.spoiler_text_map ? Object.fromEntries(Object.entries(status.spoiler_text_map).map(([key, value]) => [key, calculateSpoiler(value, emojiMap)])) diff --git a/packages/pl-fe/src/utils/html.ts b/packages/pl-fe/src/utils/html.ts index 394a9fb3b..e3c5b6300 100644 --- a/packages/pl-fe/src/utils/html.ts +++ b/packages/pl-fe/src/utils/html.ts @@ -7,17 +7,18 @@ const unescapeHTML = (html: string = ''): string => { }; /** Remove compatibility markup for features pl-fe supports. */ -const stripCompatibilityFeatures = (html: string): string => { +const stripCompatibilityFeatures = (html: string, hasQuote = true): string => { const node = document.createElement('div'); node.innerHTML = html; const selectors = [ - // Quote posting - '.quote-inline', // Explicit mentions '.recipients-inline', ]; + // Quote posting + if (hasQuote) selectors.push('.quote-inline'); + // Remove all instances of all selectors selectors.forEach(selector => { node.querySelectorAll(selector).forEach(elem => {