From 5c0b0fc3044c1f9a62fd811ef015439c3df42f40 Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Fri, 27 Nov 2026 20:11:58 +0100 Subject: [PATCH 1/3] fix: drop newlines inside iOS text nodes --- ios/htmlParser/HtmlParser.mm | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ios/htmlParser/HtmlParser.mm b/ios/htmlParser/HtmlParser.mm index 79c503baa..32ad3f121 100644 --- a/ios/htmlParser/HtmlParser.mm +++ b/ios/htmlParser/HtmlParser.mm @@ -23,7 +23,7 @@ + (BOOL)isBlockTag:(NSString *)tagName { /** * Prepares HTML for the parser by stripping extraneous whitespace and newlines - * from structural tags, while preserving them within text content. + * from structural tags, while preserving spaces within text content. * * APPROACH: * This function treats the HTML as having two distinct states: @@ -32,8 +32,10 @@ + (BOOL)isBlockTag:(NSString *)tagName { * considered layout artifacts and are REMOVED to prevent the parser from * creating unwanted spaces. * 2. Content Mode (Depth > 0): We are inside a text-containing tag (like p, - * b, li). In this mode, all whitespace is PRESERVED exactly as is, ensuring - * that sentences and inline formatting remain readable. + * b, li). In this mode, spaces are PRESERVED exactly as is, ensuring + * that sentences and inline formatting remain readable. A newline in text is + * not a line break in HTML, so it becomes a space. + * Our own HTML never has newlines inside text tags. * * The function iterates character-by-character, using a depth counter to track * nesting levels of the specific tags defined in `textTags`. @@ -102,7 +104,7 @@ + (NSString *)stripExtraWhiteSpacesAndNewlines:(NSString *)html { } if (textDepth > 0) { - [output appendFormat:@"%C", c]; + [output appendFormat:@"%C", c == '\n' ? ' ' : c]; } else { if (![whitespaceAndNewlineSet characterIsMember:c]) { [output appendFormat:@"%C", c]; @@ -260,7 +262,9 @@ + (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html // into our canonical tag subset. Gumbo already extracts the . NSString *normalized = [self normalizeExternalHtml:html]; if (normalized != nil) { - fixedHtml = normalized; + // The normalizer already drops layout whitespace between blocks, so this + // only turns newlines inside text into spaces. + fixedHtml = [self stripExtraWhiteSpacesAndNewlines:normalized]; } } From f00bc4126bfd321b084a7103e3859a22daf9273d Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Fri, 27 Nov 2026 20:57:06 +0100 Subject: [PATCH 2/3] fix: newline entity changes to space too --- ios/htmlParser/HtmlParser.mm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ios/htmlParser/HtmlParser.mm b/ios/htmlParser/HtmlParser.mm index 32ad3f121..9118ede61 100644 --- a/ios/htmlParser/HtmlParser.mm +++ b/ios/htmlParser/HtmlParser.mm @@ -625,7 +625,10 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml NSArray *entityInfo = htmlEntitiesDict[@(i)]; if (entityInfo != nullptr) { NSString *escaped = entityInfo[0]; - NSString *unescaped = entityInfo[1]; + // A newline written as an entity (e.g. ) becomes a space. + NSString *unescaped = + [entityInfo[1] stringByReplacingOccurrencesOfString:@"\n" + withString:@" "]; [plainText appendString:unescaped]; // the iterator will forward by 1 itself i += escaped.length - 1; From 36949dc9437ba591ef904973bf31b85d7ba8587a Mon Sep 17 00:00:00 2001 From: szydlovsky <9szydlowski9@gmail.com> Date: Fri, 27 Nov 2026 21:02:40 +0100 Subject: [PATCH 3/3] fix: dont strip layout whitespaces in normalizer pass --- ios/htmlParser/HtmlParser.mm | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ios/htmlParser/HtmlParser.mm b/ios/htmlParser/HtmlParser.mm index 9118ede61..5a3e7e637 100644 --- a/ios/htmlParser/HtmlParser.mm +++ b/ios/htmlParser/HtmlParser.mm @@ -40,12 +40,17 @@ + (BOOL)isBlockTag:(NSString *)tagName { * The function iterates character-by-character, using a depth counter to track * nesting levels of the specific tags defined in `textTags`. * + * `stripLayout` NO leaves whitespaces outside tags untouched. Used for + * normalizer output: the normalizer already drops layout whitespace, and text + * it leaves outside text tags is real content that we don't want to strip. + * * IMPORTANT: * The `textTags` set acts as a whitelist for "Content Mode". If you add support * for a new HTML tag that contains visible text (e.g., h4, h5, h6), * you MUST add it to the `textTags` set below. */ -+ (NSString *)stripExtraWhiteSpacesAndNewlines:(NSString *)html { ++ (NSString *)stripExtraWhiteSpacesAndNewlines:(NSString *)html + stripLayout:(BOOL)stripLayout { NSSet *textTags = [NSSet setWithObjects:@"p", @"h1", @"h2", @"h3", @"h4", @"h5", @"h6", @"li", @"b", @"a", @"s", @"mention", @"code", @"u", @@ -106,7 +111,7 @@ + (NSString *)stripExtraWhiteSpacesAndNewlines:(NSString *)html { if (textDepth > 0) { [output appendFormat:@"%C", c == '\n' ? ' ' : c]; } else { - if (![whitespaceAndNewlineSet characterIsMember:c]) { + if (!stripLayout || ![whitespaceAndNewlineSet characterIsMember:c]) { [output appendFormat:@"%C", c]; } } @@ -216,7 +221,8 @@ + (NSDictionary *)prepareCheckboxListStyleValue:(NSValue *)rangeValue + (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html useHtmlNormalizer:(BOOL)useHtmlNormalizer { - NSString *htmlWithoutSpaces = [self stripExtraWhiteSpacesAndNewlines:html]; + NSString *htmlWithoutSpaces = [self stripExtraWhiteSpacesAndNewlines:html + stripLayout:YES]; NSString *fixedHtml = nullptr; // 13 is the length of "". The length guard only applies to the @@ -262,9 +268,10 @@ + (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html // into our canonical tag subset. Gumbo already extracts the . NSString *normalized = [self normalizeExternalHtml:html]; if (normalized != nil) { - // The normalizer already drops layout whitespace between blocks, so this - // only turns newlines inside text into spaces. - fixedHtml = [self stripExtraWhiteSpacesAndNewlines:normalized]; + // Only turn newlines inside text tags into spaces; leave everything else + // as the normalizer wrote it. + fixedHtml = [self stripExtraWhiteSpacesAndNewlines:normalized + stripLayout:NO]; } }