AutoSelectedMarkupFormatter
AutoSelectedMarkupFormatter class to select the proper MarkupFormatter
implementation depending on the used document type.
using AngleSharp.Dom;
using AngleSharp.Html;
using AngleSharp.XHtml;
using AngleSharp.Xml;
namespace AngleSharp
{
public sealed class AutoSelectedMarkupFormatter : IMarkupFormatter
{
private IMarkupFormatter childFormatter;
private IDocumentType _docType;
private IMarkupFormatter ChildFormatter {
get {
if (childFormatter == null && _docType != null) {
if (_docType.PublicIdentifier.Contains("XML"))
childFormatter = XmlMarkupFormatter.Instance;
else if (_docType.PublicIdentifier.Contains("XHTML")) {
childFormatter = XhtmlMarkupFormatter.Instance;
}
}
return childFormatter ?? HtmlMarkupFormatter.Instance;
}
set {
childFormatter = value;
}
}
public AutoSelectedMarkupFormatter(IDocumentType docType = null)
{
_docType = docType;
}
public string Attribute(IAttr attribute)
{
return ChildFormatter.Attribute(attribute);
}
public string OpenTag(IElement element, bool selfClosing)
{
Confirm(element.Owner.Doctype);
return ChildFormatter.OpenTag(element, selfClosing);
}
public string CloseTag(IElement element, bool selfClosing)
{
Confirm(element.Owner.Doctype);
return ChildFormatter.CloseTag(element, selfClosing);
}
public string Comment(IComment comment)
{
Confirm(comment.Owner.Doctype);
return ChildFormatter.Comment(comment);
}
public string Doctype(IDocumentType doctype)
{
Confirm(doctype);
return ChildFormatter.Doctype(doctype);
}
public string Processing(IProcessingInstruction processing)
{
Confirm(processing.Owner.Doctype);
return ChildFormatter.Processing(processing);
}
public string Text(string text)
{
return ChildFormatter.Text(text);
}
private void Confirm(IDocumentType docType)
{
if (_docType == null)
_docType = docType;
}
}
}