如何使用 AngleSharp 制作自闭合标签
How to produce self-closing tags with AngleSharp
对于这个具体的例子,我正在处理 input
标签做一些工作并将它们替换为自定义标签。
输出为 <customTag>..</customTag>
var parser = new HtmlParser();
var html = parser.parse(htmlSnippet);
var inputs= originalHtml.QuerySelectorAll("input");
foreach (var element in inputs)
{
var newElement = html.CreateElement("customTag");
// do some work.
element.Replace(newElement);
}
return html.Body.InnerHtml();
是否可以 "produce" 使用 AngleSharp 自闭标签?
<customTag attr="x" />
用法:
var document = new HtmlParser().Parse("");
var tag = document.CreateElement("customTag");
tag.SetAttribute("attr", "x");
tag.AsSelfClosing();
Console.WriteLine(tag.OuterHtml);
tag.ToHtml(Console.Out, CustomHtmlMarkupFormatter.Instance);
输出:
<customtag attr="x">
<customtag attr="x" />
查看源代码,您会发现有 2 个地方可以让您进行一些工作以实现这样的目标。
readonly NodeFlags Node._flags
:请记住,这个字段、它的 属性 和主机 class 都没有暴露。所以你需要一些肮脏的技巧才能得到这份工作。此外,默认格式化程序 HtmlMarkupFormatter
仅使用 >
,而不使用 />
。
- 创建您自己的
IMarkupFormatter
。
这是一个同时使用上述两点的解决方案。
public static class ElementExtensions
{
public static void AsSelfClosing(this IElement element)
{
const int SelfClosing = 0x1;
var type = typeof(IElement).Assembly.GetType("AngleSharp.Dom.Node");
var field = type.GetField("_flags", BindingFlags.Instance | BindingFlags.NonPublic);
var flags = (uint)field.GetValue(element);
flags |= SelfClosing;
field.SetValue(element, Enum.ToObject(field.FieldType, flags));
}
}
public class CustomHtmlMarkupFormatter : IMarkupFormatter
{
public static readonly CustomHtmlMarkupFormatter Instance = new CustomHtmlMarkupFormatter();
public string Text(String text) => HtmlMarkupFormatter.Instance.Text(text);
public string Comment(IComment comment) => HtmlMarkupFormatter.Instance.Comment(comment);
public string Processing(IProcessingInstruction processing) => HtmlMarkupFormatter.Instance.Processing(processing);
public string Doctype(IDocumentType doctype) => HtmlMarkupFormatter.Instance.Doctype(doctype);
//public string OpenTag(IElement element, Boolean selfClosing) => HtmlMarkupFormatter.Instance.OpenTag(element, selfClosing);
public string CloseTag(IElement element, Boolean selfClosing) => HtmlMarkupFormatter.Instance.CloseTag(element, selfClosing);
public string Attribute(IAttr attribute) => HtmlMarkupFormatter.Instance.Attribute(attribute);
public string OpenTag(IElement element, Boolean selfClosing)
{
var temp = new StringBuilder();
temp.Append('<');
if (!String.IsNullOrEmpty(element.Prefix))
{
temp.Append(element.Prefix).Append(':');
}
temp.Append(element.LocalName);
foreach (var attribute in element.Attributes)
{
temp.Append(" ").Append(Instance.Attribute(attribute));
}
temp.Append(selfClosing ? " />" : ">");
return temp.ToString();
}
}
您还可以删除 ElementExtensions
并添加您自己的逻辑以决定何时在 CustomHtmlMarkupFormatter.OpenTag
中自行关闭元素。
对于这个具体的例子,我正在处理 input
标签做一些工作并将它们替换为自定义标签。
输出为 <customTag>..</customTag>
var parser = new HtmlParser();
var html = parser.parse(htmlSnippet);
var inputs= originalHtml.QuerySelectorAll("input");
foreach (var element in inputs)
{
var newElement = html.CreateElement("customTag");
// do some work.
element.Replace(newElement);
}
return html.Body.InnerHtml();
是否可以 "produce" 使用 AngleSharp 自闭标签?
<customTag attr="x" />
用法:
var document = new HtmlParser().Parse("");
var tag = document.CreateElement("customTag");
tag.SetAttribute("attr", "x");
tag.AsSelfClosing();
Console.WriteLine(tag.OuterHtml);
tag.ToHtml(Console.Out, CustomHtmlMarkupFormatter.Instance);
输出:
<customtag attr="x">
<customtag attr="x" />
查看源代码,您会发现有 2 个地方可以让您进行一些工作以实现这样的目标。
readonly NodeFlags Node._flags
:请记住,这个字段、它的 属性 和主机 class 都没有暴露。所以你需要一些肮脏的技巧才能得到这份工作。此外,默认格式化程序HtmlMarkupFormatter
仅使用>
,而不使用/>
。- 创建您自己的
IMarkupFormatter
。
这是一个同时使用上述两点的解决方案。
public static class ElementExtensions
{
public static void AsSelfClosing(this IElement element)
{
const int SelfClosing = 0x1;
var type = typeof(IElement).Assembly.GetType("AngleSharp.Dom.Node");
var field = type.GetField("_flags", BindingFlags.Instance | BindingFlags.NonPublic);
var flags = (uint)field.GetValue(element);
flags |= SelfClosing;
field.SetValue(element, Enum.ToObject(field.FieldType, flags));
}
}
public class CustomHtmlMarkupFormatter : IMarkupFormatter
{
public static readonly CustomHtmlMarkupFormatter Instance = new CustomHtmlMarkupFormatter();
public string Text(String text) => HtmlMarkupFormatter.Instance.Text(text);
public string Comment(IComment comment) => HtmlMarkupFormatter.Instance.Comment(comment);
public string Processing(IProcessingInstruction processing) => HtmlMarkupFormatter.Instance.Processing(processing);
public string Doctype(IDocumentType doctype) => HtmlMarkupFormatter.Instance.Doctype(doctype);
//public string OpenTag(IElement element, Boolean selfClosing) => HtmlMarkupFormatter.Instance.OpenTag(element, selfClosing);
public string CloseTag(IElement element, Boolean selfClosing) => HtmlMarkupFormatter.Instance.CloseTag(element, selfClosing);
public string Attribute(IAttr attribute) => HtmlMarkupFormatter.Instance.Attribute(attribute);
public string OpenTag(IElement element, Boolean selfClosing)
{
var temp = new StringBuilder();
temp.Append('<');
if (!String.IsNullOrEmpty(element.Prefix))
{
temp.Append(element.Prefix).Append(':');
}
temp.Append(element.LocalName);
foreach (var attribute in element.Attributes)
{
temp.Append(" ").Append(Instance.Attribute(attribute));
}
temp.Append(selfClosing ? " />" : ">");
return temp.ToString();
}
}
您还可以删除 ElementExtensions
并添加您自己的逻辑以决定何时在 CustomHtmlMarkupFormatter.OpenTag
中自行关闭元素。