Skip to content

Commit e22db3b

Browse files
author
ArthurHub
committed
refactor selected tags to selected boxes in html generation
1 parent 1553783 commit e22db3b

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

Source/HtmlRenderer/Utils/DomUtils.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public static string GenerateHtml(CssBox root, HtmlGenerationStyle styleGen = Ht
437437
var sb = new StringBuilder();
438438
if (root != null)
439439
{
440-
WriteHtml(sb, root, styleGen, onlySelected ? CollectSelectedHtmlTags(root) : null);
440+
WriteHtml(sb, root, styleGen, onlySelected ? CollectSelectedBoxes(root) : null);
441441
}
442442
return sb.ToString();
443443
}
@@ -536,22 +536,23 @@ private static int GetSelectedPlainText(StringBuilder sb, CssBox box)
536536
/// </summary>
537537
/// <param name="root">the box to check its sub-tree</param>
538538
/// <returns>the collection to add the selected tags to</returns>
539-
private static Dictionary<HtmlTag, bool> CollectSelectedHtmlTags(CssBox root)
539+
private static Dictionary<CssBox, bool> CollectSelectedBoxes(CssBox root)
540540
{
541-
var selectedTags = new Dictionary<HtmlTag, bool>();
542-
var maybeTags = new Dictionary<HtmlTag, bool>();
543-
CollectSelectedHtmlTags(root, selectedTags, maybeTags);
541+
var selectedTags = new Dictionary<CssBox, bool>();
542+
var maybeTags = new Dictionary<CssBox, bool>();
543+
CollectSelectedBoxes(root, selectedTags, maybeTags);
544544
return selectedTags;
545545
}
546546

547547
/// <summary>
548548
/// Collect the html tags that have at least one word down the hierarchy that is selected recursively.<br/>
549+
/// Use <paramref name="maybeBoxes"/> to handle boxes that are between selected words but don't have selected word inside.<br/>
549550
/// </summary>
550551
/// <param name="box">the box to check its sub-tree</param>
551-
/// <param name="selectedTags">the collection to add the selected tags to</param>
552-
/// <param name="maybeTags">used to handle tags that are between selected words but don't have selected word inside</param>
552+
/// <param name="selectedBoxes">the hash to add the selected boxes to</param>
553+
/// <param name="maybeBoxes">used to handle boxes that are between selected words but don't have selected word inside</param>
553554
/// <returns>is the current box is in selected sub-tree</returns>
554-
private static bool CollectSelectedHtmlTags(CssBox box, Dictionary<HtmlTag, bool> selectedTags, Dictionary<HtmlTag, bool> maybeTags)
555+
private static bool CollectSelectedBoxes(CssBox box, Dictionary<CssBox, bool> selectedBoxes, Dictionary<CssBox, bool> maybeBoxes)
555556
{
556557
bool isInSelection = false;
557558
foreach (var word in box.Words)
@@ -560,47 +561,47 @@ private static bool CollectSelectedHtmlTags(CssBox box, Dictionary<HtmlTag, bool
560561
{
561562
if (box.HtmlTag != null)
562563
{
563-
selectedTags.Add(box.HtmlTag, true);
564+
selectedBoxes.Add(box, true);
564565
}
565-
foreach (var maybeTag in maybeTags)
566-
selectedTags[maybeTag.Key] = maybeTag.Value;
567-
maybeTags.Clear();
566+
foreach (var maybeTag in maybeBoxes)
567+
selectedBoxes[maybeTag.Key] = maybeTag.Value;
568+
maybeBoxes.Clear();
568569
isInSelection = true;
569570
}
570571
}
571572

572573
foreach (var childBox in box.Boxes)
573574
{
574-
var childInSelection = CollectSelectedHtmlTags(childBox, selectedTags, maybeTags);
575+
var childInSelection = CollectSelectedBoxes(childBox, selectedBoxes, maybeBoxes);
575576
if (childInSelection)
576577
{
577578
if (box.HtmlTag != null)
578579
{
579-
selectedTags[box.HtmlTag] = true;
580+
selectedBoxes[box] = true;
580581
}
581582
isInSelection = true;
582583
}
583584
}
584585

585-
if (box.HtmlTag != null && selectedTags.Count > 0)
586+
if (box.HtmlTag != null && selectedBoxes.Count > 0)
586587
{
587-
maybeTags[box.HtmlTag] = true;
588+
maybeBoxes[box] = true;
588589
}
589590

590591
return isInSelection;
591592
}
592593

593594
/// <summary>
594595
/// Write the given html DOM sub-tree into the given string builder.<br/>
595-
/// If <paramref name="selectedTags"/> are given write html only from those tags.
596+
/// If <paramref name="selectedBoxes"/> are given write html only from those tags.
596597
/// </summary>
597598
/// <param name="sb">the string builder to write html into</param>
598599
/// <param name="box">the html sub-tree to write</param>
599600
/// <param name="styleGen">Controls the way styles are generated when html is generated</param>
600-
/// <param name="selectedTags">Control if to generate only selected tags, if given only tags found in collection will be generated</param>
601-
private static void WriteHtml(StringBuilder sb, CssBox box, HtmlGenerationStyle styleGen, Dictionary<HtmlTag, bool> selectedTags)
601+
/// <param name="selectedBoxes">Control if to generate only selected boxes, if given only boxes found in hash will be generated</param>
602+
private static void WriteHtml(StringBuilder sb, CssBox box, HtmlGenerationStyle styleGen, Dictionary<CssBox, bool> selectedBoxes)
602603
{
603-
if (box.HtmlTag == null || selectedTags == null || selectedTags.ContainsKey(box.HtmlTag))
604+
if (box.HtmlTag == null || selectedBoxes == null || selectedBoxes.ContainsKey(box))
604605
{
605606
if (box.HtmlTag != null)
606607
{
@@ -622,17 +623,17 @@ private static void WriteHtml(StringBuilder sb, CssBox box, HtmlGenerationStyle
622623
{
623624
foreach (var word in box.Words)
624625
{
625-
if (selectedTags == null || word.Selected)
626+
if (selectedBoxes == null || word.Selected)
626627
{
627-
var wordText = GetSelectedWord(word, selectedTags != null);
628+
var wordText = GetSelectedWord(word, selectedBoxes != null);
628629
sb.Append(HtmlUtils.EncodeHtml(wordText));
629630
}
630631
}
631632
}
632633

633634
foreach (var childBox in box.Boxes)
634635
{
635-
WriteHtml(sb, childBox, styleGen, selectedTags);
636+
WriteHtml(sb, childBox, styleGen, selectedBoxes);
636637
}
637638

638639
if (box.HtmlTag != null && !box.HtmlTag.IsSingle)

0 commit comments

Comments
 (0)