HtmlFormatter Class

Converts a node to HTML markup. HtmlFormatter.Default uses the HTML fragment serialization algorithm (https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments); HtmlMinifyFormatter and HtmlPrettyFormatter produce a minified or an indented output.

public abstract class HtmlFormatter

Remarks

A formatter is immutable and can be used concurrently: each call creates a HtmlMarkupWriter that holds the state of the conversion. To customize the output, derive from HtmlMarkupWriter and return it from HtmlFormatter.CreateWriter.

The content of the style elements and the value of the style attributes are written as is, unless HtmlFormatter.StyleElementFormatter or HtmlFormatter.StyleAttributeFormatter is set.

Constructors

NameDescription
HtmlFormatter()

HtmlFormatter()

protected HtmlFormatter()

No documentation is available.

Properties

NameDescription
DefaultThe formatter that uses the HTML fragment serialization algorithm: the output is the same as Element.OuterHtml, Element.GetHtml and Document.GetHtml.
StyleAttributeFormatterThe formatter of the declarations of the style attributes, e.g. a CssMinifyFormatter; null (the default) to write their value as is.
StyleElementFormatterThe formatter of the CSS text of the HTML style elements whose type is CSS (no type attribute, or text/css), e.g. a CssMinifyFormatter; null (the default) to write their text as is.

The formatter that uses the HTML fragment serialization algorithm: the output is the same as Element.OuterHtml, Element.GetHtml and Document.GetHtml.

public static HtmlFormatter Default { get; }

Property value

HtmlFormatter

StyleAttributeFormatter

HtmlFormatter.cs

The formatter of the declarations of the style attributes, e.g. a CssMinifyFormatter; null (the default) to write their value as is.

public CssFormatter? StyleAttributeFormatter { get; init; }

Property value

CssFormatter?

Remarks

The value is parsed as a declaration block and the resulting declarations are formatted: as in the browsers, the invalid declarations and the declarations of unsupported properties are not written (see CssFormatter). The attribute is written even when no declaration is left (style=""), as the selectors and the scripts can test its presence. The style attributes of the elements of all namespaces are formatted, but not the namespaced attributes whose local name is style.

StyleElementFormatter

HtmlFormatter.cs

The formatter of the CSS text of the HTML style elements whose type is CSS (no type attribute, or text/css), e.g. a CssMinifyFormatter; null (the default) to write their text as is.

public CssFormatter? StyleElementFormatter { get; init; }

Property value

CssFormatter?

Remarks

The text is parsed and the resulting style sheet is formatted: as in the browsers, the comments, the invalid rules and the declarations of unsupported properties are not written (see CssFormatter). The text is written as is when the element has children other than text nodes, or when the formatted text contains </style, which would end the element.

Methods

NameDescription
CreateWriter(TextWriter)Creates the writer that converts one node to markup.
Format(Node)Returns the markup of node (see HtmlMarkupWriter.Write).
Format(Node, TextWriter)Writes the markup of node to output (see HtmlMarkupWriter.Write).
GetSerializedAttributeName(Attr)Returns the serialized name of the attribute, with the xml:, xmlns: or xlink: prefix of its namespace.
GetSerializedTagName(Element)Returns the tag name written in the start and end tags of the element: its local name in the HTML, MathML and SVG namespaces, its qualified name otherwise.
HasRawTextContent(Element)Returns whether the text children of the element are serialized without escaping: style, script, xmp, iframe, noembed, noframes, plaintext, and noscript when scripting is enabled (https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments).
IsVoidElement(Element)Returns whether the element is serialized without children and end tag (https://html.spec.whatwg.org/multipage/parsing.html#serializes-as-void).
WriteEscaped(TextWriter, ReadOnlySpan<char>, bool)Writes value escaped as defined by the HTML serialization (https://html.spec.whatwg.org/multipage/parsing.html#escapingString): &, <, > and U+00A0 are replaced by character references, and " too in attribute mode.

CreateWriter(TextWriter)

Creates the writer that converts one node to markup.

protected abstract HtmlMarkupWriter CreateWriter(TextWriter output)

Parameters

output TextWriter

Returns

HtmlMarkupWriter

Format overloads

Format(Node)

HtmlFormatter.cs

Returns the markup of node (see HtmlMarkupWriter.Write).

public string Format(Node node)
Parameters
node Node
Returns

string

Format(Node, TextWriter)

HtmlFormatter.cs

Writes the markup of node to output (see HtmlMarkupWriter.Write).

public void Format(Node node, TextWriter output)
Parameters
node Node
output TextWriter

GetSerializedAttributeName(Attr)

HtmlFormatter.cs

Returns the serialized name of the attribute, with the xml:, xmlns: or xlink: prefix of its namespace.

public static string GetSerializedAttributeName(Attr attribute)

Parameters

attribute Attr

Returns

string

GetSerializedTagName(Element)

HtmlFormatter.cs

Returns the tag name written in the start and end tags of the element: its local name in the HTML, MathML and SVG namespaces, its qualified name otherwise.

public static string GetSerializedTagName(Element element)

Parameters

element Element

Returns

string

HasRawTextContent(Element)

HtmlFormatter.cs

Returns whether the text children of the element are serialized without escaping: style, script, xmp, iframe, noembed, noframes, plaintext, and noscript when scripting is enabled (https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments).

public static bool HasRawTextContent(Element element)

Parameters

element Element

Returns

bool

IsVoidElement(Element)

HtmlFormatter.cs

Returns whether the element is serialized without children and end tag (https://html.spec.whatwg.org/multipage/parsing.html#serializes-as-void).

public static bool IsVoidElement(Element element)

Parameters

element Element

Returns

bool

WriteEscaped(TextWriter, ReadOnlySpan<char>, bool)

HtmlFormatter.cs

Writes value escaped as defined by the HTML serialization (https://html.spec.whatwg.org/multipage/parsing.html#escapingString): &, <, > and U+00A0 are replaced by character references, and " too in attribute mode.

public static void WriteEscaped(TextWriter output, ReadOnlySpan<char> value, bool attributeMode)

Parameters

output TextWriter
value ReadOnlySpan<char>
attributeMode bool