From cdfae67cc12f308e3ddc3fe226b4a920b11fe703 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Thu, 3 Dec 2026 12:26:49 +0100 Subject: [PATCH 1/5] fix: add iOS parser try catch and remove dead code branches --- ios/EnrichedTextInputView.mm | 4 +++- ios/htmlParser/HtmlParser.mm | 27 ++------------------------ ios/inputHtmlParser/InputHtmlParser.mm | 12 ++++++++++-- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/ios/EnrichedTextInputView.mm b/ios/EnrichedTextInputView.mm index b38d56f54..045012fd5 100644 --- a/ios/EnrichedTextInputView.mm +++ b/ios/EnrichedTextInputView.mm @@ -2275,7 +2275,9 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { parseToHtmlFromRange:NSMakeRange(0, textView.textStorage.string.length) host:self]; NSString *initiallyProcessedHtml = [parser initiallyProcessHtml:currentHtml]; - [parser replaceWholeFromHtml:initiallyProcessedHtml]; + if (initiallyProcessedHtml != nullptr) { + [parser replaceWholeFromHtml:initiallyProcessedHtml]; + } textView.selectedRange = prevSelectedRange; [self anyTextMayHaveBeenModified]; diff --git a/ios/htmlParser/HtmlParser.mm b/ios/htmlParser/HtmlParser.mm index 5a3e7e637..08ffa8bde 100644 --- a/ios/htmlParser/HtmlParser.mm +++ b/ios/htmlParser/HtmlParser.mm @@ -232,36 +232,13 @@ + (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html [htmlWithoutSpaces hasSuffix:@""]; if (isWrapped) { - // remove html tags, might be with newlines or without them + // Editor's own HTML: remove the html tags; their surrounding newlines were + // already stripped. fixedHtml = [htmlWithoutSpaces copy]; - // firstly remove newlined html tags if any: - fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n" - withString:@""]; - fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n" - withString:@""]; - // fallback; remove html tags without their newlines fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"" withString:@""]; fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"" withString:@""]; - - // Additionally, try getting the content from between body tags if there - // are some: - - // Firstly make sure there are no newlines between them. - fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n" - withString:@""]; - fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n" - withString:@""]; - // Then, if there actually are body tags, use the content between them. - NSRange openingBodyRange = [htmlWithoutSpaces rangeOfString:@""]; - NSRange closingBodyRange = [htmlWithoutSpaces rangeOfString:@""]; - if (openingBodyRange.length != 0 && closingBodyRange.length != 0) { - NSInteger newStart = openingBodyRange.location + 6; - NSInteger newEnd = closingBodyRange.location - 1; - fixedHtml = [htmlWithoutSpaces - substringWithRange:NSMakeRange(newStart, newEnd - newStart + 1)]; - } } else if (useHtmlNormalizer) { // External HTML (from Google Docs, Word, web pages, etc.) // Run through the Gumbo-based normalizer to convert arbitrary HTML diff --git a/ios/inputHtmlParser/InputHtmlParser.mm b/ios/inputHtmlParser/InputHtmlParser.mm index 18011cd92..e3deda174 100644 --- a/ios/inputHtmlParser/InputHtmlParser.mm +++ b/ios/inputHtmlParser/InputHtmlParser.mm @@ -227,8 +227,16 @@ - (void)applyProcessedAlignments:(NSArray *)alignments } - (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html { - return [HtmlParser initiallyProcessHtml:html - useHtmlNormalizer:_input->useHtmlNormalizer]; + @try { + return [HtmlParser initiallyProcessHtml:html + useHtmlNormalizer:_input->useHtmlNormalizer]; + } @catch (NSException *exception) { + // nil makes the callers fall back to inserting the raw input as plain text + RCTLogWarn(@"[EnrichedTextInput]: Failed to process HTML: (%@), falling " + @"back to raw input.", + exception.reason); + return nil; + } } @end From 2a2dd8f0839a2efbcb5680269192b722ff65cb03 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Thu, 3 Dec 2026 20:49:23 +0100 Subject: [PATCH 2/5] fix: keep a body tag removal safeguard on iOS --- ios/htmlParser/HtmlParser.mm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ios/htmlParser/HtmlParser.mm b/ios/htmlParser/HtmlParser.mm index 08ffa8bde..7562d9c5e 100644 --- a/ios/htmlParser/HtmlParser.mm +++ b/ios/htmlParser/HtmlParser.mm @@ -233,12 +233,12 @@ + (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html if (isWrapped) { // Editor's own HTML: remove the html tags; their surrounding newlines were - // already stripped. + // already stripped. Body tags are removed too. fixedHtml = [htmlWithoutSpaces copy]; - fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"" - withString:@""]; - fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"" - withString:@""]; + for (NSString *tag in @[ @"", @"", @"", @"" ]) { + fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:tag + withString:@""]; + } } else if (useHtmlNormalizer) { // External HTML (from Google Docs, Word, web pages, etc.) // Run through the Gumbo-based normalizer to convert arbitrary HTML From add1c1d193d1ee82df93c6485a4af1ce9ff8e2b6 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Thu, 3 Dec 2026 22:59:00 +0100 Subject: [PATCH 3/5] fix: adjust web normalizing/sanitizing flow to native platforms --- src/web/EnrichedText.tsx | 10 +---- src/web/EnrichedTextInput.tsx | 11 ++++- src/web/normalization/incomingHtml.ts | 45 +++++++++++++++++++ src/web/normalization/prepareHtmlForWeb.ts | 32 ++++++++++--- src/web/normalization/tiptapHtmlNormalizer.ts | 26 ++++++++--- src/web/utils/pasteExternalHtmlAsText.ts | 29 ++++++++++++ 6 files changed, 133 insertions(+), 20 deletions(-) create mode 100644 src/web/normalization/incomingHtml.ts create mode 100644 src/web/utils/pasteExternalHtmlAsText.ts diff --git a/src/web/EnrichedText.tsx b/src/web/EnrichedText.tsx index cb864a52d..17aa9d7f9 100644 --- a/src/web/EnrichedText.tsx +++ b/src/web/EnrichedText.tsx @@ -18,7 +18,6 @@ import { } from './constants/classNames'; import { enrichedTextThemingToCSSProperties } from './styleConversion/enrichedThemingToCSSProperties'; import { buildMentionRulesCSS } from './styleConversion/buildMentionRulesCSS'; -import { sanitizeHtml } from './sanitization/htmlSanitizer'; import { prepareHtmlForWeb } from './normalization/prepareHtmlForWeb'; import { INLINE_IMAGE_CSS_VARIABLES } from './styleConversion/inlineImageCSSVariables'; import { useImageErrorFallback } from './htmlExtensions/useImageErrorFallback'; @@ -66,14 +65,9 @@ export const EnrichedText = memo( }, })); - const sanitizedHtml = useMemo( - () => sanitizeHtml(children, sanitizationConfig), - [children, sanitizationConfig] - ); - const finalHtml = useMemo( - () => prepareHtmlForWeb(sanitizedHtml, useHtmlNormalizer), - [sanitizedHtml, useHtmlNormalizer] + () => prepareHtmlForWeb(children, useHtmlNormalizer, sanitizationConfig), + [children, useHtmlNormalizer, sanitizationConfig] ); const [clampedHtml, setClampedHtml] = useState( diff --git a/src/web/EnrichedTextInput.tsx b/src/web/EnrichedTextInput.tsx index 2938cecb0..0c367f8f7 100644 --- a/src/web/EnrichedTextInput.tsx +++ b/src/web/EnrichedTextInput.tsx @@ -31,6 +31,7 @@ import { useOnLinkDetected } from './tiptapWatchers/useOnLinkDetected'; import type { LinkEmitterState } from './tiptapWatchers/emitLinkDetected'; import { prepareHtmlForTiptap, + preparePastedHtmlForTiptap, normalizeHtmlFromTiptap, } from './normalization/tiptapHtmlNormalizer'; import { ENRICHED_TEXT_INPUT_DEFAULT_PROPS } from '../utils/EnrichedTextInputDefaultProps'; @@ -66,6 +67,7 @@ import { MergeAdjacentSameKindBlocksPlugin } from './pmPlugins/MergeAdjacentSame import { OrderedListMarkerWidthPlugin } from './pmPlugins/OrderedListMarkerWidthPlugin'; import { StripMarksInCodeBlockPlugin } from './pmPlugins/StripMarksInCodeBlockPlugin'; import { handleClipboardPasteImages } from './utils/pasteImages'; +import { handlePasteExternalHtmlAsText } from './utils/pasteExternalHtmlAsText'; import { MentionPlugin, setMention, @@ -311,18 +313,23 @@ export const EnrichedTextInput = ({ }, mousedown: (_view, event) => handleLinkMouseDown(event), }, - handlePaste: (_view, event) => + handlePaste: (view, event) => handleClipboardPasteImages( event, () => editorInstanceRef.current, () => onPasteImagesRef.current + ) || + handlePasteExternalHtmlAsText( + view, + event, + useHtmlNormalizerRef.current ), attributes: { autoCapitalize, enterkeyhint: returnKeyTypeToEnterKeyHint(returnKeyType), }, transformPastedHTML: (html) => { - return prepareHtmlForTiptap( + return preparePastedHtmlForTiptap( html, useHtmlNormalizerRef.current, sanitizationConfigRef.current diff --git a/src/web/normalization/incomingHtml.ts b/src/web/normalization/incomingHtml.ts new file mode 100644 index 000000000..51c7c523d --- /dev/null +++ b/src/web/normalization/incomingHtml.ts @@ -0,0 +1,45 @@ +import type { SanitizationConfig } from '../../types'; +import { sanitizeHtml } from '../sanitization/htmlSanitizer'; +import { normalizeHtml } from './htmlNormalizer'; + +/** + * The editor's own HTML is wrapped in ...; surrounding whitespace + * is ignored. + */ +export function isInternalHtml(html: string): boolean { + const trimmed = html.trim(); + return trimmed.startsWith('') && trimmed.endsWith(''); +} + +/** + * Turns a value (defaultValue, setValue, children) into canonical HTML: + * - the editor's own HTML is used as is, + * - other HTML is normalized when the normalizer is on, + * - otherwise the value is shown as plain text. + */ +export function prepareIncomingHtml( + html: string, + useHtmlNormalizer: boolean | undefined, + sanitizationConfig?: SanitizationConfig +): string { + // Checked before sanitization, which drops the wrapper. + if (isInternalHtml(html)) return sanitizeHtml(html, sanitizationConfig); + if (!useHtmlNormalizer) return plainTextToHtml(html); + return normalizeHtml(sanitizeHtml(html, sanitizationConfig)); +} + +/** A paragraph per line, an empty line as
, and any tags as literal text. */ +export function plainTextToHtml(text: string): string { + if (text === '') return ''; + return text + .split(/\r\n?|\n/) + .map((line) => (line === '' ? '
' : `

${escapeHtml(line)}

`)) + .join(''); +} + +function escapeHtml(text: string): string { + return text + .replace(/&/g, '&') + .replace(//g, '>'); +} diff --git a/src/web/normalization/prepareHtmlForWeb.ts b/src/web/normalization/prepareHtmlForWeb.ts index 29ad25f30..6eabc5c87 100644 --- a/src/web/normalization/prepareHtmlForWeb.ts +++ b/src/web/normalization/prepareHtmlForWeb.ts @@ -1,17 +1,21 @@ -import { normalizeHtml } from './htmlNormalizer'; +import type { SanitizationConfig } from '../../types'; +import { isInternalHtml, prepareIncomingHtml } from './incomingHtml'; import { withBaseFontFamilyFallback } from '../styleConversion/enrichedStyleToCSSVariables'; export function prepareHtmlForWeb( html: string, - useHtmlNormalizer: boolean | undefined + useHtmlNormalizer: boolean | undefined, + sanitizationConfig?: SanitizationConfig ): string { - if (useHtmlNormalizer) { - html = normalizeHtml(html); - } + const isInternal = isInternalHtml(html); + html = prepareIncomingHtml(html, useHtmlNormalizer, sanitizationConfig); const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); + if (isInternal) { + removeLayoutWhitespace(doc.body); + } wrapBareLiContentInParagraph(doc); checkboxHtmlToWeb(doc); addFontFamilyFallback(doc); @@ -19,6 +23,24 @@ export function prepareHtmlForWeb( return doc.body.innerHTML; } +/* + * The editor's HTML can be pretty-printed. That whitespace would render as empty + * lines, so drop whitespace-only text outside of text tags. + */ +function removeLayoutWhitespace(body: HTMLElement) { + const containers = [ + body, + ...body.querySelectorAll('ul, ol, blockquote, codeblock'), + ]; + for (const container of containers) { + for (const node of Array.from(container.childNodes)) { + if (node.nodeType === Node.TEXT_NODE && !node.textContent?.trim()) { + node.remove(); + } + } + } +} + /* * Native list format: *