Skip to content

Commit c88f600

Browse files
committed
Fix empty lines of consecutive line break tags
1 parent 0a6d568 commit c88f600

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

Source/HtmlRenderer/Core/Parse/DomParser.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,9 @@ private static void CorrectImgBoxes(CssBox box)
638638
}
639639

640640
/// <summary>
641-
/// Correct the DOM tree recursively by turning "br" html boxes that should force a line break
642-
/// into a "\n" text word on the box itself, reusing the same forced-newline mechanism used for
643-
/// "white-space: pre/pre-line" content.
641+
/// Correct the DOM tree recursively by replacing "br" html boxes with anonymous blocks that respect br spec.<br/>
642+
/// If the "br" tag is after inline box then the anon block will have zero height only acting as newline,
643+
/// but if it is after block box then it will have min-height of the font size so it will create empty line.
644644
/// </summary>
645645
/// <param name="box">the current box to correct its sub-tree</param>
646646
private static void CorrectLineBreaksBlocks(CssBox box)
@@ -650,16 +650,27 @@ private static void CorrectLineBreaksBlocks(CssBox box)
650650
CorrectLineBreaksBlocks(childBox);
651651
}
652652

653-
if (!box.IsBrElement) return;
654-
655-
var previousSibling = DomUtils.GetPreviousSibling(box);
656-
if (previousSibling == null || previousSibling.IsBlock)
653+
var followingBlock = box.IsBlock;
654+
foreach (var childBox in box.Boxes)
657655
{
658-
var nextSibling = DomUtils.GetFollowingSiblings(box, b => b.IsInline && !b.IsBrElement, true).FirstOrDefault();
659-
if (nextSibling == null)
656+
if (childBox.IsBrElement)
657+
{
658+
childBox.Display = CssConstants.Block;
659+
if (followingBlock)
660+
{
661+
childBox.Height = ".95em"; // TODO:a check the height to min-height when it is supported
662+
}
663+
664+
// A BR itself is block-level after correction, so a following BR is treated as an empty-line break.
665+
followingBlock = true;
666+
}
667+
else if (childBox.Words.Count > 0)
668+
{
669+
followingBlock = false;
670+
}
671+
else if (childBox.IsBlock)
660672
{
661-
box.Text = "\n";
662-
box.ParseToWords();
673+
followingBlock = true;
663674
}
664675
}
665676
}

0 commit comments

Comments
 (0)