diff --git a/Source/HtmlRenderer/Core/Parse/HtmlParser.cs b/Source/HtmlRenderer/Core/Parse/HtmlParser.cs index 534ace396..d7f8cb385 100644 --- a/Source/HtmlRenderer/Core/Parse/HtmlParser.cs +++ b/Source/HtmlRenderer/Core/Parse/HtmlParser.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using HtmlKit; using TheArtOfDev.HtmlRenderer.Core.Dom; using TheArtOfDev.HtmlRenderer.Core.Utils; @@ -38,6 +39,10 @@ public static CssBox ParseDocument(string source, CssBox root = null) var curBox = root; + // HtmlKit tokenizes noscript content as raw text, split into data tokens at every '<'. Without + // scripting it's markup (HTML 4.12.2), so it's collected and parsed with the noscript box as root. + StringBuilder noscriptContent = null; + using (var sourceReader = new StringReader(source)) { var tokenizer = new HtmlTokenizer(sourceReader); @@ -45,6 +50,12 @@ public static CssBox ParseDocument(string source, CssBox root = null) HtmlToken token; while (tokenizer.ReadNextToken(out token)) { + if (noscriptContent != null && token.Kind != HtmlTokenKind.Data) + { + ParseDocument(noscriptContent.ToString(), curBox); + noscriptContent = null; + } + switch (token.Kind) { case HtmlTokenKind.Tag: @@ -57,9 +68,10 @@ public static CssBox ParseDocument(string source, CssBox root = null) { var text = (HtmlDataToken)token; - if (curBox.HtmlTag != null && curBox.HtmlTag.Name.Equals(HtmlConstants.NoScript, StringComparison.OrdinalIgnoreCase)) + if (curBox != root && curBox.HtmlTag != null && curBox.HtmlTag.Name.Equals(HtmlConstants.NoScript, StringComparison.OrdinalIgnoreCase)) { - curBox = ParseDocument(text.Data, curBox); + noscriptContent ??= new StringBuilder(); + noscriptContent.Append(text.Data); } else { @@ -77,6 +89,9 @@ public static CssBox ParseDocument(string source, CssBox root = null) throw new ArgumentOutOfRangeException(); } } + + if (noscriptContent != null) + ParseDocument(noscriptContent.ToString(), curBox); } return root; diff --git a/Source/Test/HtmlRenderer.Test/Parse/HtmlParserTests.cs b/Source/Test/HtmlRenderer.Test/Parse/HtmlParserTests.cs new file mode 100644 index 000000000..b0340a94c --- /dev/null +++ b/Source/Test/HtmlRenderer.Test/Parse/HtmlParserTests.cs @@ -0,0 +1,47 @@ +using TheArtOfDev.HtmlRenderer.Core.Dom; +using TheArtOfDev.HtmlRenderer.Core.Parse; + +namespace HtmlRenderer.Test.Parse; + +[TestClass] +public sealed class HtmlParserTests +{ + private static CssBox Find(CssBox box, string tagName) + { + if (box.HtmlTag?.Name == tagName) + return box; + + foreach (var child in box.Boxes) + { + var found = Find(child, tagName); + if (found != null) + return found; + } + + return null!; + } + + private static string Text(CssBox box) => + box.HtmlTag == null && box.Text != null ? box.Text : string.Concat(box.Boxes.Select(Text)); + + [TestMethod] + [DataRow("
")] + [DataRow("")] + public void Noscript_TextIsParsedAsText(string html) + { + var noscript = Find(HtmlParser.ParseDocument(html), "noscript"); + + Assert.AreEqual("Enable JS", Text(noscript)); + } + + [TestMethod] + public void Noscript_MarkupKeepsItsStructure() + { + var noscript = Find(HtmlParser.ParseDocument(""), "noscript"); + + Assert.AreEqual(2, noscript.Boxes.Count); + Assert.AreEqual("p", noscript.Boxes[0].HtmlTag?.Name); + Assert.AreEqual("ab", Text(noscript.Boxes[0])); + Assert.AreEqual("c", Text(noscript.Boxes[1])); + } +}