Skip to content

Commit eb5a3c1

Browse files
author
ArthurHub
committed
* Fix not parsing css style if contained in html comment
1 parent 8e12082 commit eb5a3c1

5 files changed

Lines changed: 519 additions & 503 deletions

File tree

‎Source/Demo/Samples/07.Additional features.htm‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<title>Additional features</title>
44
<link rel="Stylesheet" href="StyleSheet" />
55
<style>
6+
<!--
67
.g1, .g2, .g3, .g4, .g5 {
78
background-color: red;
89
background-gradient: yellow;
@@ -55,6 +56,7 @@
5556
td {
5657
text-align: center;
5758
}
59+
-->
5860
</style>
5961
</head>
6062
<body>

‎Source/HtmlRenderer/Parse/CssParser.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ internal static class CssParser
3131
/// </summary>
3232
private static readonly char[] _cssBlockSplitters = new[] { '}', ';' };
3333

34+
/// <summary>
35+
/// The chars to trim the css class name by
36+
/// </summary>
37+
private static readonly char[] _cssClassTrimChars = new[] {'\r', '\n', '\t', ' ', '-', '!', '<', '>'};
38+
3439
#endregion
3540

3641
/// <summary>
@@ -192,7 +197,7 @@ private static void ParseMediaStyleBlocks(CssData cssData, string stylesheet)
192197
string atrule;
193198
while ((atrule = RegexParserUtils.GetCssAtRules(stylesheet, ref startIdx)) != null)
194199
{
195-
//Just processs @media rules
200+
//Just process @media rules
196201
if (!atrule.StartsWith("@media",StringComparison.InvariantCultureIgnoreCase)) continue;
197202

198203
//Extract specified media types
@@ -245,7 +250,7 @@ private static void FeedStyleBlock(CssData cssData, string block, string media =
245250

246251
foreach (string cls in classes)
247252
{
248-
string className = cls.Trim();
253+
string className = cls.Trim(_cssClassTrimChars);
249254
if (!String.IsNullOrEmpty(className))
250255
{
251256
var newblock = ParseCssBlockImp(className, blockSource);

‎Source/HtmlRenderer/Parse/DomParser.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ private static void CascadeStyles(CssBox box, HtmlContainer htmlContainer, ref C
106106
}
107107

108108
// Check for the <style> tag
109-
if (box.HtmlTag.Name.Equals("style", StringComparison.CurrentCultureIgnoreCase) && box.Boxes.Count == 1)
109+
if (box.HtmlTag.Name.Equals("style", StringComparison.CurrentCultureIgnoreCase) && box.Boxes.Count > 0)
110110
{
111111
CloneCssData(ref cssData, ref cssDataChanged);
112-
CssParser.ParseStyleSheet(cssData, box.Boxes[0].Text.CutSubstring());
112+
foreach (var child in box.Boxes)
113+
CssParser.ParseStyleSheet(cssData, child.Text.CutSubstring());
113114
}
114115

115116
// Check for the <link rel=stylesheet> tag

‎Source/HtmlRenderer/Parse/HtmlParser.cs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ public static CssBox ParseDocument(string source)
6161
{
6262
// parse element tag to css box structure
6363
endIdx = ParseHtmlTag(source, tagIdx, ref curBox) + 1;
64+
65+
if( curBox.HtmlTag != null && curBox.HtmlTag.Name.Equals(HtmlConstants.Style, StringComparison.OrdinalIgnoreCase) )
66+
{
67+
var endIdxS = endIdx;
68+
endIdx = source.IndexOf("</style>", endIdx, StringComparison.OrdinalIgnoreCase);
69+
if(endIdx > -1)
70+
AddTextBox(source, endIdxS, endIdx, ref curBox);
71+
}
6472
}
6573
}
6674
startIdx = tagIdx > -1 && endIdx > 0 ? endIdx : -1;
6775
}
6876

69-
// handle pices of html without proper structure
70-
if (endIdx < source.Length)
77+
// handle pieces of html without proper structure
78+
if (endIdx > -1 && endIdx < source.Length)
7179
{
7280
// there is text after the end of last element
7381
var endText = new SubString(source, endIdx, source.Length - endIdx);

0 commit comments

Comments
 (0)