diff --git a/src/app/components/RenderMessageContent.tsx b/src/app/components/RenderMessageContent.tsx index d8da91dd7..aaaf7fab4 100644 --- a/src/app/components/RenderMessageContent.tsx +++ b/src/app/components/RenderMessageContent.tsx @@ -90,11 +90,14 @@ type RenderMessageContentProps = { onOpenMedia?: (mEvent: MatrixEvent) => boolean; }; +const VIDEO_MEDIA_TYPE_REG = /\.(mp4|webm|ogg)$/i; +const IMAGE_MEDIA_TYPE_REG = /\.(png|jpg|jpeg|gif|webp)$/i; +const IMAGE_MEDIA_TYPE_AT_REG = /@(jpeg|webp|png|jpg)$/i; + const getMediaType = (url: string) => { const cleanUrl = url.toLowerCase(); - if (cleanUrl.match(/\.(mp4|webm|ogg)$/i)) return 'video'; - if (cleanUrl.match(/\.(png|jpg|jpeg|gif|webp)$/i) || cleanUrl.match(/@(jpeg|webp|png|jpg)$/i)) - return 'image'; + if (VIDEO_MEDIA_TYPE_REG.test(cleanUrl)) return 'video'; + if (IMAGE_MEDIA_TYPE_REG.test(cleanUrl) || IMAGE_MEDIA_TYPE_AT_REG.test(cleanUrl)) return 'image'; return null; }; diff --git a/src/app/features/room/message/hiddenLinkPreviews.ts b/src/app/features/room/message/hiddenLinkPreviews.ts index 6a2fd4a48..f057616ad 100644 --- a/src/app/features/room/message/hiddenLinkPreviews.ts +++ b/src/app/features/room/message/hiddenLinkPreviews.ts @@ -4,6 +4,15 @@ import { testMatrixTo } from '$plugins/matrix-to'; const LINK_URL = `(https?:\\/\\/.[A-Za-z0-9-._~:/?#[\\()@!$&'*+,;%=]+)`; const LINKINPUTREGEX = new RegExp(`\\(?(${LINK_URL})\\)?`, 'g'); +const WRAPPED_ESCAPED_URL_REG = new RegExp(String.raw`\\<(${LINK_URL})\\>`, 'g'); +const OPEN_ONLY_ESCAPED_URL_REG = new RegExp(String.raw`\\<(${LINK_URL})`, 'g'); +const CLOSE_ONLY_ESCAPED_URL_REG = new RegExp(String.raw`(${LINK_URL})\\>`, 'g'); +const ESCAPED_SUPPRESSED_MD_LINK_REG = new RegExp( + String.raw`\\<\[([^\]]*)\]\((\s]+>)\)\\>`, + 'g' +); +const WRONG_OUTER_ESCAPED_AUTOLINK_REG = /\\<\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)(?:>|\\>)/g; + /** * `htmlToMarkdown()` escapes `<` and `>` into `\<` and `\>` in text nodes. * @@ -15,22 +24,16 @@ const LINKINPUTREGEX = new RegExp(`\\(?(${LINK_URL})\\)?`, 'g'); export function stripMarkdownEscapesForHiddenPreviews(markdown: string): string { // Handle: \ // Also handle common surrounding parens: (\), (\) - const WRAPPED = new RegExp(String.raw`\\<(${LINK_URL})\\>`, 'g'); - const OPEN_ONLY = new RegExp(String.raw`\\<(${LINK_URL})`, 'g'); - const CLOSE_ONLY = new RegExp(String.raw`(${LINK_URL})\\>`, 'g'); - - let s = markdown.replace(WRAPPED, '<$1>').replace(OPEN_ONLY, '<$1').replace(CLOSE_ONLY, '$1>'); + let s = markdown + .replace(WRAPPED_ESCAPED_URL_REG, '<$1>') + .replace(OPEN_ONLY_ESCAPED_URL_REG, '<$1') + .replace(CLOSE_ONLY_ESCAPED_URL_REG, '$1>'); // Restore [label]() after htmlToMarkdown escaped a surrounding "\<...\>". - const ESCAPED_SUPPRESSED_MD_LINK = new RegExp( - String.raw`\\<\[([^\]]*)\]\((\s]+>)\)\\>`, - 'g' - ); - s = s.replace(ESCAPED_SUPPRESSED_MD_LINK, '[$1]($2)'); + s = s.replace(ESCAPED_SUPPRESSED_MD_LINK_REG, '[$1]($2)'); // Same for "\<[label](bare-url)>" when angle brackets were lost on the destination. - const WRONG_OUTER_ESCAPED_AUTOLINK = /\\<\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)(?:>|\\>)/g; - s = s.replace(WRONG_OUTER_ESCAPED_AUTOLINK, '[$1](<$2>)'); + s = s.replace(WRONG_OUTER_ESCAPED_AUTOLINK_REG, '[$1](<$2>)'); return s; } diff --git a/src/app/plugins/react-custom-html-parser.tsx b/src/app/plugins/react-custom-html-parser.tsx index 7c67d324f..928a9c60c 100644 --- a/src/app/plugins/react-custom-html-parser.tsx +++ b/src/app/plugins/react-custom-html-parser.tsx @@ -377,10 +377,9 @@ const scaleEmojiChunk = (text: string, output: (string | JSX.Element)[]) => { export const scaleSystemEmoji = (text: string): (string | JSX.Element)[] => { const parts: (string | JSX.Element)[] = []; - const urlReg = new RegExp(URL_REG, 'g'); let lastIndex = 0; - [...text.matchAll(urlReg)].forEach((match) => { + [...text.matchAll(URL_REG)].forEach((match) => { const start = match.index ?? 0; scaleEmojiChunk(text.slice(lastIndex, start), parts); parts.push(match[0]); diff --git a/src/app/utils/abbreviations.ts b/src/app/utils/abbreviations.ts index bee574165..d6ed5b92a 100644 --- a/src/app/utils/abbreviations.ts +++ b/src/app/utils/abbreviations.ts @@ -32,14 +32,23 @@ export type TextSegment = { termKey?: string; }; -export const splitByAbbreviations = (text: string, abbrMap: Map): TextSegment[] => { - if (abbrMap.size === 0) return [{ id: 'txt-0', text }]; +const abbrPatternCache = new WeakMap, RegExp>(); + +const getAbbrPattern = (abbrMap: Map): RegExp => { + const cached = abbrPatternCache.get(abbrMap); + if (cached) return cached; - // Build a regex that matches any of the terms at word boundaries. - // Sort longest first so "HTTP/2" matches before "HTTP". const terms = [...abbrMap.keys()].toSorted((a, b) => b.length - a.length); const escaped = terms.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); const pattern = new RegExp(`\\b(${escaped.join('|')})\\b`, 'gi'); + abbrPatternCache.set(abbrMap, pattern); + return pattern; +}; + +export const splitByAbbreviations = (text: string, abbrMap: Map): TextSegment[] => { + if (abbrMap.size === 0) return [{ id: 'txt-0', text }]; + + const pattern = getAbbrPattern(abbrMap); const segments: TextSegment[] = []; let lastIndex = 0; diff --git a/src/app/utils/sanitize.ts b/src/app/utils/sanitize.ts index 90d25c52d..b7f0c172b 100644 --- a/src/app/utils/sanitize.ts +++ b/src/app/utils/sanitize.ts @@ -4,6 +4,7 @@ import { testMatrixUri } from '../plugins/matrix-uri'; const MAX_TAG_NESTING = 100; const INTERNAL_IMG_SRC_ATTR = 'data-sable-img-src'; +const RESTORE_PROTECTED_SRC_REG = new RegExp(`\\s${INTERNAL_IMG_SRC_ATTR}="([^"]+)"`, 'g'); const permittedHtmlTags = [ 'del', @@ -254,15 +255,12 @@ function restoreProtectedImageSources( sanitizedHtml: string, protectedSources: Map ): string { - return sanitizedHtml.replace( - new RegExp(`\\s${INTERNAL_IMG_SRC_ATTR}="([^"]+)"`, 'g'), - (_match, sourceId: string) => { - const validatedSrc = getValidatedProtectedImageSource(sourceId, protectedSources); - if (!validatedSrc) return ''; + return sanitizedHtml.replace(RESTORE_PROTECTED_SRC_REG, (_match, sourceId: string) => { + const validatedSrc = getValidatedProtectedImageSource(sourceId, protectedSources); + if (!validatedSrc) return ''; - return ` src="${sanitizeText(validatedSrc)}"`; - } - ); + return ` src="${sanitizeText(validatedSrc)}"`; + }); } const enforceNestingLimit = (fragment: DocumentFragment): void => {