Skip to content

XHTML Minifier

Taritsyn edited this page Aug 6, 2024 · 17 revisions

XHTML Minifier produces minification of HTML and XHTML code. As a result of minification on the output we get code, that follows the rules of XHTML.

Work with the XHTML Minifier is very similar to working with the HTML Minifier, only difference is that instead of the HtmlMinifier and HtmlMinificationSettings classes need to use the XhtmlMinifier and XhtmlMinificationSettings.

Let's consider in detail properties of the XhtmlMinificationSettings class:

Property name Data type Default value Description
WhitespaceMinificationMode WhitespaceMinificationMode enumeration Medium Whitespace minification mode. Can take the following values:
  • None. Keep whitespace.
  • Safe. Safe whitespace minification: removes whitespace characters from top and bottom of HTML document; multiple whitespace characters are replaced by a single space; removes all leading and trailing whitespace characters from DOCTYPE declaration; removes all leading and trailing whitespace characters from outer and inner contents of invisible tags (html, head, body, meta, link, script, etc.); removes unnecessary leading and trailing whitespace characters from outer contents of non-independent tags (li, dt, dd, rb, rtc, rt, rp, option, tr, td, th, etc.).
  • Medium. Medium whitespace minification: executes all operations of the safe whitespace minification + removes all leading and trailing whitespace characters from outer and internal contents of block-level tags.
  • Aggressive. Aggressive whitespace minification: executes all operations of the medium whitespace minification + removes all leading and trailing whitespace characters from internal contents of inline and inline-block tags.
PreserveNewLines Boolean false Flag for whether to collapse whitespace to one newline string when whitespace contains a newline.
NewLineStyle NewLineStyle enumeration Auto Style of the newline. Can take the following values:
  • Auto. Auto-detect style for newline based on the source input.
  • Native. CRLF in Windows, LF on other platforms.
  • Windows. Force the Windows style for newline (CRLF).
  • Mac. Force the Macintosh style for newline (CR).
  • Unix. Force the Unix style for newline (LF).
RemoveHtmlComments Boolean true Flag for whether to remove all HTML comments, except conditional, noindex, KnockoutJS containerless comments, AngularJS 1.X comment directives, React DOM component comments and Blazor markers.
PreservableHtmlCommentList String Empty string Comma-separated list of string representations of simple regular expressions, that define what HTML comments can not be removed (e.g. "/^\s*saved from url=\(\d+\)/i, /^\/?\$$/, /^[\[\]]$/"). Simple regular expressions somewhat similar to the ECMAScript regular expression literals. There are two varieties of the simple regular expressions:
  • /pattern/
  • /pattern/i
RemoveHtmlComments­FromScriptsAndStyles Boolean true Flag for whether to remove HTML comments from script and style tags.
UseShortDoctype Boolean false Flag for whether to replace existing document type declaration by short declaration - <!DOCTYPE html>.
UseMetaCharsetTag Boolean false Flag for whether to replace <meta http-equiv="content-type" content="text/html; charset=…" /> tag by <meta charset="…" /> tag
RenderEmptyTagsWithSpace Boolean true Flag for whether to allow the inserting space before slash in empty tags (for example, true - <br />; false - <br/>).
RemoveTagsWithoutContent Boolean false Flag for whether to remove tags without content, except for textarea, tr, th and td tags, and tags with class, id, name, role, src and custom attributes.
AttributeQuotesStyle HtmlAttributeQuotesStyle enumeration Auto Style of the HTML attribute quotes. Can take the following values:
  • Auto. Auto-detect style for attribute quotes based on the source input.
  • Optimal. Optimal style for attribute quotes based on the attribute value.
  • Single. Single quotes.
  • Double. Double quotes.
RemoveEmptyAttributes Boolean true Flag for whether to remove attributes, which have empty value (valid attributes are: class, id, name, style, title, lang, event attributes, action attribute of form tag and value attribute of input tag).
RemoveRedundantAttributes Boolean false Flag for whether to remove redundant attributes:
  • <a id="…" name="…" …>
  • <area shape="rect" …>
  • <button type="submit" …>
  • <form autocomplete="on" …>
  • <form enctype="application/x-www-form-urlencoded" …>
  • <form method="get" …>
  • <img decoding="auto" …>
  • <input type="text" …>
  • <script src="…" charset="…" …>
  • <script language="javascript" …>
  • <textarea wrap="soft" …>
  • <track kind="subtitles" …>
PreservableAttributeList String Empty string Comma-separated list of string representations of attribute expressions, that define what attributes can not be removed (e.g. "form[method=get i], input[type], [xmlns]"). Attribute expressions somewhat similar to the CSS Attribute Selectors. There are six varieties of the attribute expressions:
  • [attrName]
  • tagName[attrName]
  • [attrName=attrValue]
  • tagName[attrName=attrValue]
  • [attrName=attrValue i]
  • tagName[attrName=attrValue i]
RemoveHttpProtocol­FromAttributes Boolean false Flag for whether to remove the HTTP protocol portion (http:) from URI-based attributes (tags marked with rel="external" are skipped).
RemoveHttpsProtocol­FromAttributes Boolean false Flag for whether to remove the HTTPS protocol portion (https:) from URI-based attributes (tags marked with rel="external" are skipped).
RemoveJsProtocol­FromAttributes Boolean true Flag for whether to remove the javascript: pseudo-protocol portion from event attributes.
MinifyEmbeddedCssCode Boolean true Flag for whether to minify CSS code in style tags.
MinifyInlineCssCode Boolean true Flag for whether to minify CSS code in style attributes.
MinifyEmbeddedJsCode Boolean true Flag for whether to minify JS code in script tags.
MinifyInlineJsCode Boolean true Flag for whether to minify JS code in event attributes and hyperlinks with javascript: pseudo-protocol.
MinifyEmbeddedJsonData Boolean true Flag for whether to minify JSON data in script tags with application/json, application/ld+json, importmap and speculationrules types.
ProcessableScriptTypeList String "text/html" Comma-separated list of types of script tags, that are processed by minifier (e.g. "text/html, text/ng-template"). Currently only supported the KnockoutJS, Kendo UI MVVM and AngularJS 1.X views.
MinifyKnockout­BindingExpressions Boolean false Flag for whether to minify the KnockoutJS binding expressions in data-bind attributes and containerless comments.
MinifyAngular­BindingExpressions Boolean false Flag for whether to minify the AngularJS 1.X binding expressions in Mustache-style tags ({{}}) and directives.
CustomAngularDirectiveList String Empty string Comma-separated list of names of custom AngularJS 1.X directives (e.g. "myDir, btfCarousel"), that contain expressions. If value of the MinifyAngularBindingExpressions property equal to true, then the expressions in custom directives will be minified.