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
9 changes: 9 additions & 0 deletions Source/HtmlRenderer/Core/Parse/HtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ private static void AddTextBox(HtmlDataToken token, ref CssBox curBox)
if (text.Length == 0) return;
}

// HtmlKit splits text into several data tokens, e.g. at each '<' in a style element,
// HTML appends them to the preceding Text node (parsing.html#insert-a-char)
var lastChild = curBox.Boxes.Count > 0 ? curBox.Boxes[curBox.Boxes.Count - 1] : null;
if (lastChild != null && lastChild.HtmlTag == null && lastChild.Text != null)
{
lastChild.Text += text;
return;
}

var box = CssBox.CreateBox(curBox);
box.Text = text;
}
Expand Down
11 changes: 11 additions & 0 deletions Source/Test/HtmlRenderer.Test/Css/RealWorldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public void ParseCss_WithStandardString_ExpectReadableProperties()
Assert.AreEqual(RColor.FromArgb(255, 0, 0), h2Box.ActualBackgroundColor);
}

[TestMethod]
[DataRow("#a { --x: \"<\"; } p { color: red }")]
[DataRow("p::before { content: \"<\" } p { color: red }")]
[DataRow("@media (width < 100000px) { p { color: red } }")]
public void StyleElement_TextWithLessThanSign_IsOneStyleSheet(string css)
{
var (root, _) = LayoutHarness.Layout($"<html><head><style>{css}</style></head><body><p>Text</p></body></html>");

Assert.AreEqual(RColor.FromArgb(255, 0, 0), FindByTag(root, "p").ActualColor);
}

private static CssBox FindByTag(CssBox root, string tag)
{
var box = LayoutHarness.Descendants(root).FirstOrDefault(b => b.HtmlTag != null && b.HtmlTag.Name == tag);
Expand Down
Loading