Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ public static String toHtml(Spanned text) {
String normalizedBlockQuote =
normalizedCodeBlock.replaceAll("</blockquote>\\n<br>", "</blockquote>");

// Replace empty <p> tags (with or without style attributes) with <br>
String normalizedHtml = normalizedBlockQuote.replaceAll("<p[^>]*></p>", "<br>");
// Replace empty <p> tags with <br>, except when they carry text-align
String normalizedHtml =
normalizedBlockQuote.replaceAll("<p(?![^>]*text-align\\s*:)[^>]*></p>", "<br>");

return "<html>\n" + normalizedHtml + "</html>";
}
Expand Down Expand Up @@ -593,7 +594,14 @@ private void handleEndTag(String tag) {
if (tag.equalsIgnoreCase("br")) {
handleBr(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("p")) {
boolean empty = isEmptyTag;
Alignment pendingAlignment = empty ? getLast(mSpannableStringBuilder, Alignment.class) : null;
endBlockElement(mSpannableStringBuilder, mSpanFactory);
// Plain empty paragraphs never reach setParagraphSpanFromMark (no ZWS).
// Represent them as a blank line so they round-trip as <br>.
if (empty && pendingAlignment == null) {
handleBr(mSpannableStringBuilder);
}
} else if (tag.equalsIgnoreCase("ul")) {
currentListAlignmentCssValue = null;
endBlockElement(mSpannableStringBuilder, mSpanFactory);
Expand Down
19 changes: 17 additions & 2 deletions ios/htmlParser/HtmlParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,16 @@ + (NSString *)parseToHtmlFromRange:(NSRange)range
inCheckboxList = NO;
}
} else {
[result appendString:@"\n<br>"];
NSString *cssStyleString =
[self prepareCssStyleString:currentRange.location
isOpeningTag:YES
host:host];
if (cssStyleString.length > 0) {
[result appendString:[NSString stringWithFormat:@"\n<p%@></p>",
cssStyleString]];
} else {
[result appendString:@"\n<br>"];
}
}
} else {
// newline finishes a paragraph and all style tags need to be closed
Expand Down Expand Up @@ -1456,7 +1465,7 @@ + (NSString *)prepareCssStyleString:(NSInteger)location
}

+ (void)checkForAlignments:(NSArray *)tagData
plainText:(NSString *)plainText
plainText:(NSMutableString *)plainText
foundAlignments:(NSMutableArray<AlignmentEntry *> *)foundAlignments
precedingImageCount:(NSInteger)precedingImageCount {
if (tagData == nil) {
Expand All @@ -1474,6 +1483,12 @@ + (void)checkForAlignments:(NSArray *)tagData
NSInteger actualStart = startLoc + precedingImageCount;
NSInteger length = plainText.length - startLoc;

// Empty aligned paragraphs have no characters to attach alignment to.
if (length == 0) {
[plainText appendString:@"\u200B"];
length = 1;
}

if (length > 0) {
AlignmentEntry *entry = [[AlignmentEntry alloc] init];
entry.alignment = align;
Expand Down
Loading