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
19 changes: 17 additions & 2 deletions Source/HtmlRenderer/Core/Parse/HtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,13 +39,23 @@ 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);

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:
Expand All @@ -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
{
Expand All @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions Source/Test/HtmlRenderer.Test/Parse/HtmlParserTests.cs
Original file line number Diff line number Diff line change
@@ -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("<body><noscript>Enable JS</noscript></body>")]
[DataRow("<html><head><noscript>Enable JS</noscript></head><body></body></html>")]
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><p><b>a</b><i>b</i></p>c</noscript>"), "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]));
}
}
Loading