【问题标题】:How to produce self-closing tags with AngleSharp如何使用 AngleSharp 生成自闭合标签
【发布时间】:2016-07-06 08:39:10
【问题描述】:

对于这个特定的示例,我正在处理 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();

是否可以使用 AngleSharp 来“产生”自闭合标签?

<customTag attr="x" /> 

【问题讨论】:

    标签: c# anglesharp


    【解决方案1】:

    用法:

    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" />
    

    查看源代码,您会发现有两个地方可以做一些事情来实现这样的事情。

    1. readonly NodeFlags Node._flags :请记住,这个字段、它的属性和宿主类都没有暴露。所以你需要一些肮脏的黑客才能得到这份工作。此外,默认格式化程序 HtmlMarkupFormatter 仅使用 &gt;,而不使用 /&gt;
    2. 创建您自己的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 中的元素。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多