minifyHTML: HTML Minifier

Description Usage Arguments See Also Examples

Description

This function takes a text string as input and minifies it according to large number of options. The blog post provides a fuller descriptions of the options.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
minifyHTML(input, caseSensitive = FALSE,
  collapseBooleanAttributes = TRUE,
  collapseInlineTagWhitespace = FALSE, collapseWhitespace = TRUE,
  conservativeCollapse = FALSE, continueOnParseError = FALSE,
  customAttrAssign = "[]", customAttrCollapse = NULL,
  customAttrSurround = "[]",
  customEventAttributes = "[ /^on[a-z]{3,}$/ ]", decodeEntities = TRUE,
  html5 = TRUE, ignoreCustomComments = "[ /^!/ ]",
  ignoreCustomFragments = "[ /<%[\\s\\S]*?%>/, /<\\?[,\\s\\S]*?\\?>/ ]",
  includeAutoGeneratedTags = TRUE, keepClosingSlash = FALSE,
  maxLineLength = NULL, minifyCSS = TRUE, minifyJS = TRUE,
  minifyURLs = FALSE, preserveLineBreaks = FALSE,
  preventAttributesEscaping = FALSE, processConditionalComments = TRUE,
  processScripts = "[]", quoteCharacter = NULL,
  removeAttributeQuotes = FALSE, removeComments = TRUE,
  removeEmptyAttributes = TRUE, removeEmptyElements = FALSE,
  removeOptionalTags = FALSE, removeRedundantAttributes = FALSE,
  removeScriptTypeAttributes = FALSE,
  removeStyleLinkTypeAttributes = FALSE, removeTagWhitespace = FALSE,
  sortAttributes = TRUE, sortClassName = TRUE,
  trimCustomFragments = TRUE, useShortDoctype = TRUE)

Arguments

input

String

caseSensitive

Treat attributes in case sensitive manner (useful for custom HTML tags)

collapseBooleanAttributes

Omit attribute values from boolean attributes

collapseInlineTagWhitespace

Don't leave any spaces between display:inline; elements when collapsing. Must be used in conjunction with collapseWhitespace=TRUE

collapseWhitespace

Collapse white space that contributes to text nodes in a document tree

conservativeCollapse

Always collapse to 1 space (never remove it entirely). Must be used in conjunction with collapseWhitespace=true

continueOnParseError

Handle parse errors instead of aborting.

customAttrAssign

Arrays of regex'es that allow to support custom attribute assign expressions (e.g. '<div flex?="{{mode != cover}}"></div>')

customAttrCollapse

Regex that specifies custom attribute to strip newlines from (e.g. /ng-class/)

customAttrSurround

Arrays of regex'es that allow to support custom attribute surround expressions (e.g. <input {{#if value}}checked="checked"{{/if}}>)

customEventAttributes

Arrays of regex'es that allow to support custom event attributes for minifyJS (e.g. ng-click)

decodeEntities

Use direct Unicode characters whenever possible

html5

Parse input according to HTML5 specifications

ignoreCustomComments

Array of regex'es that allow to ignore certain comments, when matched

ignoreCustomFragments

Array of regex'es that allow to ignore certain fragments, when matched (e.g. <?php ... ?>, {{ ... }}, etc.)

includeAutoGeneratedTags

Insert tags generated by HTML parser

keepClosingSlash

Keep the trailing slash on singleton elements

maxLineLength

Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points

minifyCSS

Minify CSS in style elements and style attributes (uses clean-css)

minifyJS

Minify JavaScript in script elements and event attributes (uses UglifyJS)

minifyURLs

Minify URLs in various attributes (uses relateurl)

preserveLineBreaks

Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break. Must be used in conjunction with collapseWhitespace=true

preventAttributesEscaping

Prevents the escaping of the values of attributes

processConditionalComments

Process contents of conditional comments through minifier

processScripts

Array of strings corresponding to types of script elements to process through minifier (e.g. text/ng-template, text/x-handlebars-template, etc.)

quoteCharacter

Type of quote to use for attribute values (' or ")

removeAttributeQuotes

Remove quotes around attributes when possible

removeComments

Strip HTML comments

removeEmptyAttributes

Remove all attributes with whitespace-only values

removeEmptyElements

Remove all elements with empty contents

removeOptionalTags

Remove optional tags

removeRedundantAttributes

Remove attributes when value matches default.

removeScriptTypeAttributes

Remove type="text/javascript" from script tags. Other type attribute values are left intact

removeStyleLinkTypeAttributes

Remove type="text/css" from style and link tags. Other type attribute values are left intact

removeTagWhitespace

Remove space between attributes whenever possible. Note that this will result in invalid HTML!

sortAttributes

Sort attributes by frequency

sortClassName

Sort style classes by frequency

trimCustomFragments

Trim white space around ignoreCustomFragments.

useShortDoctype

Replaces the doctype with the short (HTML5) doctype

See Also

html-minifer github page

Examples

1
2
input = "<!-- foo --><div>baz</div><!-- bar\n\n moo -->"
minifyHTML(input)

csgillespie/minifyHTML documentation built on June 10, 2019, 10 a.m.