Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a92a74

Browse files
author
Arthur
committed
minor optimization, test sample for layout center with fixed size width
1 parent 8fd5b2b commit 9a92a74

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed
Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
11
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
22
<html>
3-
<body>
4-
<crap style="color: red"/>
5-
<v:shape style="color: blue" />
6-
<div>
7-
Single whitespace in seperate<span> </span>element
8-
</div>
9-
<div>
10-
Multiple whitespaces in seperate<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>element
11-
using &amp;nbsp;
12-
</div>
13-
<hr />
14-
TOP
15-
<div style="background-color: yellow">
16-
<div style="margin: 10%; background-color: gainsboro">
17-
there are margin with percentage
18-
</div>
3+
<body>
4+
<crap style="color: red" />
5+
<v:shape style="color: blue" />
6+
<div>
7+
Single whitespace in seperate<span> </span>element
8+
</div>
9+
<div>
10+
Multiple whitespaces in seperate<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>element
11+
using &amp;nbsp;
12+
</div>
13+
<hr />
14+
TOP
15+
<div style="background-color: yellow">
16+
<div style="margin: 10%; background-color: gainsboro">
17+
there are margin with percentage
1918
</div>
20-
BOTTOM
21-
<hr />
22-
<body>
23-
<div style="text-decoration: line-through">
24-
<span>Line through : Text Text Text Text</span>
25-
</div>
26-
</body>
27-
<hr />
28-
<div>
29-
Text <span style="display: inline-block; height: 16px; width: 26px; background-color: red;"></span>END
30-
</div>
31-
<hr />
32-
<div>
33-
&amp;#37;
34-
<b/>
35-
<p><a href="&#109;&#97;&#x69;&#108;&#x74;&#x6f;:&#x74;&#x68;&#105;&#115;&#x2d;&#105;&#115;&#x2d;&#x61;&#x2d;&#116;&#x65;&#x73;&#x74;&#64;&#x65;&#x78;&#x61;&#x6d;&#x70;&#108;&#x65;&#46;&#x63;&#x6f;&#x6d;">&#x74;&#x68;&#105;&#115;&#x2d;&#105;&#115;&#x2d;&#x61;&#x2d;&#116;&#x65;&#x73;&#x74;&#64;&#x65;&#x78;&#x61;&#x6d;&#x70;&#108;&#x65;&#46;&#x63;&#x6f;&#x6d;</a></p>
36-
</div>
37-
<hr />
38-
<div>
39-
R<sup>1</sup>H<sub>2</sub>O
19+
</div>
20+
BOTTOM
21+
<hr />
22+
<body>
23+
<div style="text-decoration: line-through">
24+
<span>Line through : Text Text Text Text</span>
4025
</div>
4126
</body>
27+
<hr />
28+
<div>
29+
Text <span style="display: inline-block; height: 16px; width: 26px; background-color: red;"></span>END
30+
</div>
31+
<hr />
32+
<div style="text-align:center">
33+
<span style="background-color: red; width: 70px;">text 1</span>, <span style="width: 80px;">text 2</span>, <span style="background-color: green; width: 70px;">text 3</span>
34+
</div>
35+
<hr />
36+
<div>
37+
&amp;#37;
38+
<b />
39+
<p><a href="&#109;&#97;&#x69;&#108;&#x74;&#x6f;:&#x74;&#x68;&#105;&#115;&#x2d;&#105;&#115;&#x2d;&#x61;&#x2d;&#116;&#x65;&#x73;&#x74;&#64;&#x65;&#x78;&#x61;&#x6d;&#x70;&#108;&#x65;&#46;&#x63;&#x6f;&#x6d;">&#x74;&#x68;&#105;&#115;&#x2d;&#105;&#115;&#x2d;&#x61;&#x2d;&#116;&#x65;&#x73;&#x74;&#64;&#x65;&#x78;&#x61;&#x6d;&#x70;&#108;&#x65;&#46;&#x63;&#x6f;&#x6d;</a></p>
40+
</div>
41+
<hr />
42+
<div>
43+
R<sup>1</sup>H<sub>2</sub>O
44+
</div>
45+
</body>
4246
</html>

Source/HtmlRenderer/Core/Dom/CssLayoutEngine.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,13 @@ private static void ApplyCenterAlignment(RGraphics g, CssLineBox line)
624624
word.Left += diff;
625625
}
626626

627-
foreach (CssBox b in ToList(line.Rectangles.Keys))
627+
if (line.Rectangles.Count > 0)
628628
{
629-
RRect r = line.Rectangles[b];
630-
line.Rectangles[b] = new RRect(r.X + diff, r.Y, r.Width, r.Height);
629+
foreach (CssBox b in ToList(line.Rectangles.Keys))
630+
{
631+
RRect r = line.Rectangles[b];
632+
line.Rectangles[b] = new RRect(r.X + diff, r.Y, r.Width, r.Height);
633+
}
631634
}
632635
}
633636
}
@@ -654,10 +657,13 @@ private static void ApplyRightAlignment(RGraphics g, CssLineBox line)
654657
word.Left += diff;
655658
}
656659

657-
foreach (CssBox b in ToList(line.Rectangles.Keys))
660+
if (line.Rectangles.Count > 0)
658661
{
659-
RRect r = line.Rectangles[b];
660-
line.Rectangles[b] = new RRect(r.X + diff, r.Y, r.Width, r.Height);
662+
foreach (CssBox b in ToList(line.Rectangles.Keys))
663+
{
664+
RRect r = line.Rectangles[b];
665+
line.Rectangles[b] = new RRect(r.X + diff, r.Y, r.Width, r.Height);
666+
}
661667
}
662668
}
663669
}
@@ -687,6 +693,9 @@ private static void ApplyLeftAlignment(RGraphics g, CssLineBox line)
687693
//}
688694
}
689695

696+
/// <summary>
697+
/// todo: optimizate, not creating a list each time
698+
/// </summary>
690699
private static List<T> ToList<T>(IEnumerable<T> collection)
691700
{
692701
List<T> result = new List<T>();

0 commit comments

Comments
 (0)