html-minifier 1.2.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -52
- package/cli.js +220 -290
- package/package.json +12 -14
- package/sample-cli-config-file.conf +29 -16
- package/src/.eslintrc.json +5 -0
- package/src/htmllint.js +153 -144
- package/src/htmlminifier.js +1029 -925
- package/src/htmlparser.js +405 -409
- package/src/tokenchain.js +58 -0
- package/src/utils.js +18 -0
- package/CHANGELOG.md +0 -120
- package/dist/htmlminifier.js +0 -1821
- package/dist/htmlminifier.min.js +0 -6
package/README.md
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
# HTMLMinifier
|
|
2
|
+
|
|
1
3
|
[](https://www.npmjs.com/package/html-minifier)
|
|
2
4
|
[](https://travis-ci.org/kangax/html-minifier)
|
|
3
5
|
[](https://david-dm.org/kangax/html-minifier)
|
|
4
6
|
[](https://david-dm.org/kangax/html-minifier#info=devDependencies)
|
|
7
|
+
[](https://gitter.im/kangax/html-minifier)
|
|
5
8
|
|
|
6
|
-
[HTMLMinifier](http://kangax.github.io/html-minifier/) is a highly **configurable**, **well-tested**,
|
|
9
|
+
[HTMLMinifier](http://kangax.github.io/html-minifier/) is a highly **configurable**, **well-tested**, JavaScript-based HTML minifier, with lint-like capabilities.
|
|
7
10
|
|
|
8
11
|
See [corresponding blog post](http://perfectionkills.com/experimenting-with-html-minifier/) for all the gory details of [how it works](http://perfectionkills.com/experimenting-with-html-minifier/#how_it_works), [description of each option](http://perfectionkills.com/experimenting-with-html-minifier/#options), [testing results](http://perfectionkills.com/experimenting-with-html-minifier/#field_testing) and [conclusions](http://perfectionkills.com/experimenting-with-html-minifier/#cost_and_benefits).
|
|
9
12
|
|
|
@@ -13,65 +16,72 @@ Also see corresponding [Ruby wrapper](https://github.com/stereobooster/html_mini
|
|
|
13
16
|
|
|
14
17
|
## Minification comparison
|
|
15
18
|
|
|
16
|
-
How does HTMLMinifier compare to other solutions — [HTML Minifier from Will Peavy](http://www.willpeavy.com/minifier/) (1st result in [
|
|
19
|
+
How does HTMLMinifier compare to other solutions — [HTML Minifier from Will Peavy](http://www.willpeavy.com/minifier/) (1st result in [Google search for "html minifier"](https://www.google.com/#q=html+minifier)) as well as [htmlcompressor.com](http://htmlcompressor.com) and [minimize](https://github.com/Swaagie/minimize)?
|
|
17
20
|
|
|
18
|
-
| Site | Original size
|
|
21
|
+
| Site | Original size *(KB)* | HTMLMinifier | minimize | Will Peavy | htmlcompressor.com |
|
|
19
22
|
| --------------------------------------------------------------------------- |:--------------------:| ------------:| --------:| ----------:| ------------------:|
|
|
20
|
-
| [HTMLMinifier page](https://github.com/kangax/html-minifier) | 49 |
|
|
21
|
-
| [NBC](http://www.nbc.com)
|
|
22
|
-
| [ES6 table](http://kangax.github.io/es5-compat-table/es6/) | 118 |
|
|
23
|
-
| [New York Times](http://www.nytimes.com/) | 131 |
|
|
24
|
-
| [Google](
|
|
25
|
-
| [MSN](http://msn.com)
|
|
26
|
-
| [Stackoverflow](http://stackoverflow.com)
|
|
27
|
-
| [Amazon](http://amazon.com)
|
|
28
|
-
| [
|
|
29
|
-
| [
|
|
30
|
-
| [
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
| [HTMLMinifier page](https://github.com/kangax/html-minifier) | 49 | **37** | 42 | 44 | 43 |
|
|
24
|
+
| [NBC](http://www.nbc.com/) | 91 | **74** | 84 | 85 | 85 |
|
|
25
|
+
| [ES6 table](http://kangax.github.io/es5-compat-table/es6/) | 118 | **80** | 93 | 93 | 94 |
|
|
26
|
+
| [New York Times](http://www.nytimes.com/) | 131 | **100** | 122 | 125 | 120 |
|
|
27
|
+
| [Google](https://www.google.com/) | 133 | **128** | 132 | 135 | 131 |
|
|
28
|
+
| [MSN](http://www.msn.com/) | 157 | **127** | 138 | 145 | 138 |
|
|
29
|
+
| [Stackoverflow](http://stackoverflow.com/) | 200 | **156** | 165 | 173 | 166 |
|
|
30
|
+
| [Amazon](http://www.amazon.com/) | 246 | **203** | 234 | 230 | 219 |
|
|
31
|
+
| [Bootstrap CSS](http://getbootstrap.com/css/) | 277 | **264** | 274 | 232 | 277 |
|
|
32
|
+
| [Wikipedia](https://en.wikipedia.org/wiki/President_of_the_United_States) | 401 | **364** | 388 | 400 | n/a |
|
|
33
|
+
| [Eloquent Javascript](http://eloquentjavascript.net/1st_edition/print.html) | 870 | **815** | 840 | 864 | n/a |
|
|
34
|
+
| [ES6 draft](https://tc39.github.io/ecma262/) | 3678 | **2980** | 3079 | 3183 | n/a |
|
|
33
35
|
|
|
34
36
|
## Options Quick Reference
|
|
35
37
|
|
|
36
38
|
| Option | Description | Default |
|
|
37
39
|
|--------------------------------|-----------------|---------|
|
|
38
|
-
| `
|
|
39
|
-
| `removeCommentsFromCDATA` | [Strip HTML comments from scripts and styles](http://perfectionkills.com/experimenting-with-html-minifier/#remove_comments_from_scripts_and_styles) | `false` |
|
|
40
|
-
| `removeCDATASectionsFromCDATA` | [Remove CDATA sections from script and style elements](http://perfectionkills.com/experimenting-with-html-minifier/#remove_cdata_sections) | `false` |
|
|
41
|
-
| `collapseWhitespace` | [Collapse white space that contributes to text nodes in a document tree.](http://perfectionkills.com/experimenting-with-html-minifier/#collapse_whitespace) | `false` |
|
|
42
|
-
| `conservativeCollapse` | Always collapse to 1 space (never remove it entirely). Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
43
|
-
| `collapseInlineTagWhitespace` | Don't leave any spaces between `display:inline;` elements when collapsing. Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
44
|
-
| `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` | `false` |
|
|
40
|
+
| `caseSensitive` | Treat attributes in case sensitive manner (useful for custom HTML tags) | `false` |
|
|
45
41
|
| `collapseBooleanAttributes` | [Omit attribute values from boolean attributes](http://perfectionkills.com/experimenting-with-html-minifier/#collapse_boolean_attributes) | `false` |
|
|
46
|
-
| `
|
|
47
|
-
| `
|
|
48
|
-
| `
|
|
49
|
-
| `
|
|
50
|
-
| `
|
|
51
|
-
| `
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
54
|
-
| `
|
|
55
|
-
| `
|
|
42
|
+
| `collapseInlineTagWhitespace` | Don't leave any spaces between `display:inline;` elements when collapsing. Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
43
|
+
| `collapseWhitespace` | [Collapse white space that contributes to text nodes in a document tree](http://perfectionkills.com/experimenting-with-html-minifier/#collapse_whitespace) | `false` |
|
|
44
|
+
| `conservativeCollapse` | Always collapse to 1 space (never remove it entirely). Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
45
|
+
| `customAttrAssign` | Arrays of regex'es that allow to support custom attribute assign expressions (e.g. `'<div flex?="{{mode != cover}}"></div>'`) | `[ ]` |
|
|
46
|
+
| `customAttrCollapse` | Regex that specifies custom attribute to strip newlines from (e.g. `/ng-class/`) | |
|
|
47
|
+
| `customAttrSurround` | Arrays of regex'es that allow to support custom attribute surround expressions (e.g. `<input {{#if value}}checked="checked"{{/if}}>`) | `[ ]` |
|
|
48
|
+
| `customEventAttributes` | Arrays of regex'es that allow to support custom event attributes for `minifyJS` (e.g. `ng-click`) | `[ /^on[a-z]{3,}$/ ]` |
|
|
49
|
+
| `decodeEntities` | Use direct Unicode characters whenever possible | `false` |
|
|
50
|
+
| `html5` | Parse input according to HTML5 specifications | `true` |
|
|
51
|
+
| `ignoreCustomComments` | Array of regex'es that allow to ignore certain comments, when matched | `[ ]` |
|
|
52
|
+
| `ignoreCustomFragments` | Array of regex'es that allow to ignore certain fragments, when matched (e.g. `<?php ... ?>`, `{{ ... }}`, etc.) | `[ /<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/ ]` |
|
|
53
|
+
| `includeAutoGeneratedTags` | Insert tags generated by HTML parser | `true` |
|
|
54
|
+
| `keepClosingSlash` | Keep the trailing slash on singleton elements | `false` |
|
|
56
55
|
| `lint` | [Toggle linting](http://perfectionkills.com/experimenting-with-html-minifier/#validate_input_through_html_lint) | `false` |
|
|
57
|
-
| `
|
|
58
|
-
| `
|
|
59
|
-
| `minifyJS` | Minify
|
|
60
|
-
| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/GoalSmashers/clean-css)) | `false` (could be `true`, `false`, `Object` (options)) |
|
|
56
|
+
| `maxLineLength` | Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points |
|
|
57
|
+
| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `false`, `Object` (options)) |
|
|
58
|
+
| `minifyJS` | Minify JavaScript in script elements and event attributes (uses [UglifyJS](https://github.com/mishoo/UglifyJS2)) | `false` (could be `true`, `false`, `Object` (options)) |
|
|
61
59
|
| `minifyURLs` | Minify URLs in various attributes (uses [relateurl](https://github.com/stevenvachon/relateurl)) | `false` (could be `Object` (options)) |
|
|
62
|
-
| `
|
|
63
|
-
| `
|
|
60
|
+
| `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` | `false` |
|
|
61
|
+
| `preventAttributesEscaping` | Prevents the escaping of the values of attributes | `false` |
|
|
62
|
+
| `processConditionalComments` | Process contents of conditional comments through minifier | `false` |
|
|
64
63
|
| `processScripts` | Array of strings corresponding to types of script elements to process through minifier (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.) | `[ ]` |
|
|
65
|
-
| `maxLineLength` | Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points. |
|
|
66
|
-
| `customEventAttributes` | Arrays of regex'es that allow to support custom event attributes for `minifyJS` (e.g. `ng-click`) | `[ /^on[a-z]{3,}$/ ]` |
|
|
67
|
-
| `customAttrAssign` | Arrays of regex'es that allow to support custom attribute assign expressions (e.g. `'<div flex?="{{mode != cover}}"></div>'`) | `[ ]` |
|
|
68
|
-
| `customAttrSurround` | Arrays of regex'es that allow to support custom attribute surround expressions (e.g. `<input {{#if value}}checked="checked"{{/if}}>`) | `[ ]` |
|
|
69
|
-
| `customAttrCollapse` | Regex that specifies custom attribute to strip newlines from (e.g. `/ng\-class/`) | |
|
|
70
64
|
| `quoteCharacter` | Type of quote to use for attribute values (' or ") | |
|
|
65
|
+
| `removeAttributeQuotes` | [Remove quotes around attributes when possible](http://perfectionkills.com/experimenting-with-html-minifier/#remove_attribute_quotes) | `false` |
|
|
66
|
+
| `removeComments` | [Strip HTML comments](http://perfectionkills.com/experimenting-with-html-minifier/#remove_comments) | `false` |
|
|
67
|
+
| `removeEmptyAttributes` | [Remove all attributes with whitespace-only values](http://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_or_blank_attributes) | `false` |
|
|
68
|
+
| `removeEmptyElements` | [Remove all elements with empty contents](http://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_elements) | `false` |
|
|
69
|
+
| `removeOptionalTags` | [Remove optional tags](http://perfectionkills.com/experimenting-with-html-minifier/#remove_optional_tags) | `false` |
|
|
70
|
+
| `removeRedundantAttributes` | [Remove attributes when value matches default.](http://perfectionkills.com/experimenting-with-html-minifier/#remove_redundant_attributes) | `false` |
|
|
71
|
+
| `removeScriptTypeAttributes` | Remove `type="text/javascript"` from `script` tags. Other `type` attribute values are left intact | `false` |
|
|
72
|
+
| `removeStyleLinkTypeAttributes`| Remove `type="text/css"` from `style` and `link` tags. Other `type` attribute values are left intact | `false` |
|
|
73
|
+
| `removeTagWhitespace` | Remove space between attributes whenever possible. **Note that this will result in invalid HTML!** | `false` |
|
|
74
|
+
| `sortAttributes` | [Sort attributes by frequency](#sorting-attributes--style-classes) | `false` |
|
|
75
|
+
| `sortClassName` | [Sort style classes by frequency](#sorting-attributes--style-classes) | `false` |
|
|
76
|
+
| `useShortDoctype` | [Replaces the `doctype` with the short (HTML5) doctype](http://perfectionkills.com/experimenting-with-html-minifier/#use_short_doctype) | `false` |
|
|
77
|
+
|
|
78
|
+
### Sorting attributes / style classes
|
|
79
|
+
|
|
80
|
+
Minifier options like `sortAttributes` and `sortClassName` won't impact the plain-text size of the output. However, they form long repetitive chains of characters that should improve compression ratio of gzip used in HTTP compression.
|
|
71
81
|
|
|
72
82
|
## Special cases
|
|
73
83
|
|
|
74
|
-
### Ignoring chunks of markup
|
|
84
|
+
### Ignoring chunks of markup
|
|
75
85
|
|
|
76
86
|
If you have chunks of markup you would like preserved, you can wrap them `<!-- htmlmin:ignore -->`.
|
|
77
87
|
|
|
@@ -81,17 +91,17 @@ SVG tags are automatically recognized, and when they are minified, both case-sen
|
|
|
81
91
|
|
|
82
92
|
### Working with invalid markup
|
|
83
93
|
|
|
84
|
-
HTMLMinifier **can't work with invalid or partial chunks of markup**. This is because it parses markup into a tree structure, then modifies it (removing anything that was specified for removal, ignoring anything that was specified to be
|
|
94
|
+
HTMLMinifier **can't work with invalid or partial chunks of markup**. This is because it parses markup into a tree structure, then modifies it (removing anything that was specified for removal, ignoring anything that was specified to be ignored, etc.), then it creates a markup out of that tree and returns it.
|
|
85
95
|
|
|
86
96
|
Input markup (e.g. `<p id="">foo`)
|
|
87
97
|
|
|
88
98
|
↓
|
|
89
99
|
|
|
90
|
-
Internal representation of markup in a form of tree (e.g. `{tag: "p", attr: "id", children: ["foo"] }`)
|
|
100
|
+
Internal representation of markup in a form of tree (e.g. `{ tag: "p", attr: "id", children: ["foo"] }`)
|
|
91
101
|
|
|
92
102
|
↓
|
|
93
103
|
|
|
94
|
-
Transformation of internal representation (e.g. removal of
|
|
104
|
+
Transformation of internal representation (e.g. removal of `id` attribute)
|
|
95
105
|
|
|
96
106
|
↓
|
|
97
107
|
|
|
@@ -102,17 +112,20 @@ HTMLMinifier can't know that original markup was only half of the tree; it does
|
|
|
102
112
|
## Installation Instructions
|
|
103
113
|
|
|
104
114
|
From NPM for use as a command line app:
|
|
105
|
-
|
|
115
|
+
|
|
116
|
+
```shell
|
|
106
117
|
npm install html-minifier -g
|
|
107
118
|
```
|
|
108
119
|
|
|
109
120
|
From NPM for programmatic use:
|
|
110
|
-
|
|
121
|
+
|
|
122
|
+
```shell
|
|
111
123
|
npm install html-minifier
|
|
112
124
|
```
|
|
113
125
|
|
|
114
126
|
From Git:
|
|
115
|
-
|
|
127
|
+
|
|
128
|
+
```shell
|
|
116
129
|
git clone git://github.com/kangax/html-minifier.git
|
|
117
130
|
cd html-minifier
|
|
118
131
|
npm link .
|
|
@@ -135,6 +148,7 @@ result; // '<p title=blah id=moo>foo</p>'
|
|
|
135
148
|
## Running benchmarks
|
|
136
149
|
|
|
137
150
|
Benchmarks for minified HTML:
|
|
138
|
-
|
|
151
|
+
|
|
152
|
+
```shell
|
|
139
153
|
node benchmark.js
|
|
140
154
|
```
|