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 HtmlFormatterRemarks
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
| Name | Description |
|---|---|
| HtmlFormatter() |
HtmlFormatter()
protected HtmlFormatter()No documentation is available.
Properties
| Name | Description |
|---|---|
| Default | The formatter that uses the HTML fragment serialization algorithm: the output is the same as Element.OuterHtml, Element.GetHtml and Document.GetHtml. |
| StyleAttributeFormatter | The formatter of the declarations of the style attributes, e.g. a CssMinifyFormatter; null (the default) to write their value as is. |
| StyleElementFormatter | 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. |
Default
HtmlFormatter.csThe 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
StyleAttributeFormatter
HtmlFormatter.csThe 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
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.csThe 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
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
| Name | Description |
|---|---|
| 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)Format overloads
Format(Node)
HtmlFormatter.csReturns the markup of node (see HtmlMarkupWriter.Write).
Format(Node, TextWriter)
HtmlFormatter.csWrites the markup of node to output (see HtmlMarkupWriter.Write).
public void Format(Node node, TextWriter output)Parameters
nodeNodeoutputTextWriter
GetSerializedAttributeName(Attr)
HtmlFormatter.csGetSerializedTagName(Element)
HtmlFormatter.csHasRawTextContent(Element)
HtmlFormatter.csReturns 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)
HtmlFormatter.csWriteEscaped(TextWriter, ReadOnlySpan<char>, bool)
HtmlFormatter.csWrites 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
outputTextWritervalueReadOnlySpan<char>attributeModebool