forked from bUnit-dev/bUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestHtmlParser.cs
70 lines (63 loc) · 2.27 KB
/
TestHtmlParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using AngleSharp.Html.Parser;
using AngleSharp;
using AngleSharp.Dom;
using System;
using Egil.RazorComponents.Testing.Diffing;
namespace Egil.RazorComponents.Testing.Diffing
{
/// <summary>
/// A AngleSharp based HTML Parse that can parse markup strings
/// into a <see cref="INodeList"/>.
/// </summary>
public sealed class TestHtmlParser : IDisposable
{
private readonly IBrowsingContext _context;
private readonly IHtmlParser _htmlParser;
private readonly IDocument _document;
/// <summary>
/// Creates an instance of the parser with a AngleSharp context
/// without a <see cref="TestRenderer"/> registered.
/// </summary>
public TestHtmlParser()
{
var config = Configuration.Default
.WithCss()
.With(new HtmlComparer())
.With(this);
_context = BrowsingContext.New(config);
_htmlParser = _context.GetService<IHtmlParser>();
_document = _context.OpenNewAsync().Result;
}
/// <summary>
/// Creates an instance of the parser with a AngleSharp context
/// with the <paramref name="testRenderer"/> and <paramref name="comparer"/> registered.
/// </summary>
/// <param name="testRenderer"></param>
/// <param name="comparer"></param>
public TestHtmlParser(TestRenderer testRenderer, HtmlComparer comparer)
{
var config = Configuration.Default
.WithCss()
.With(testRenderer)
.With(comparer)
.With(this);
_context = BrowsingContext.New(config);
_htmlParser = _context.GetService<IHtmlParser>();
_document = _context.OpenNewAsync().Result;
}
/// <summary>
/// Parses a markup HTML string using AngleSharps HTML5 parser.
/// </summary>
/// <param name="markup">The markup to parse.</param>
/// <returns>The <see cref="INodeList"/>.</returns>
public INodeList Parse(string markup)
{
return _htmlParser.ParseFragment(markup, _document.Body);
}
/// <inheritdoc/>
public void Dispose()
{
_document.Dispose();
}
}
}