forked from bUnit-dev/bUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffMarkupFormatter.cs
59 lines (48 loc) · 2 KB
/
DiffMarkupFormatter.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
using System;
using AngleSharp;
using AngleSharp.Dom;
using AngleSharp.Html;
namespace Egil.RazorComponents.Testing.Diffing
{
/// <summary>
/// A markup formatter, that skips any special Blazor attributes added by the <see cref="TestRenderer"/>/<see cref="Htmlizer"/>.
/// </summary>
public class DiffMarkupFormatter : IMarkupFormatter
{
private readonly IMarkupFormatter _formatter = new PrettyMarkupFormatter()
{
NewLine = Environment.NewLine,
Indentation = " "
};
/// <inheritdoc />
public string Attribute(IAttr attribute)
=> Htmlizer.IsBlazorAttribute(attribute?.Name ?? string.Empty)
? string.Empty
: _formatter.Attribute(attribute);
/// <inheritdoc />
public string CloseTag(IElement element, bool selfClosing) => _formatter.CloseTag(element, selfClosing);
/// <inheritdoc />
public string Comment(IComment comment) => _formatter.Comment(comment);
/// <inheritdoc />
public string Doctype(IDocumentType doctype) => _formatter.Doctype(doctype);
/// <inheritdoc />
public string OpenTag(IElement element, bool selfClosing)
{
if(element is null) throw new ArgumentNullException(nameof(element));
var result = _formatter.OpenTag(element, selfClosing);
foreach (var attr in element.Attributes)
{
if (Htmlizer.IsBlazorAttribute(attr.Name))
{
var attrToRemove = " " + HtmlMarkupFormatter.Instance.Attribute(attr);
result = result.Replace(attrToRemove, "", StringComparison.Ordinal);
}
}
return result;
}
/// <inheritdoc />
public string Processing(IProcessingInstruction processing) => _formatter.Processing(processing);
/// <inheritdoc />
public string Text(ICharacterData text) => _formatter.Text(text);
}
}