minify_html 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9e8c15c45061862bc53a2f8b0c533162a7c01fc431265dcd96304c5b693e3413
4
+ data.tar.gz: 96ae5973cece926b066e04849e3d3a79e72ad9d32abf43cb4564a8daee987cb0
5
+ SHA512:
6
+ metadata.gz: c3bbc7c6adcd0a5f434c210e22f8c61d1c0a112bc66cea15a5987feabb78c7f1486cb25b8da5db4763ae93db302bd6979eb4dae00ed598fab29438c138898dd7
7
+ data.tar.gz: c97c0610c695c66622ab5e03ee56de1aa69e37dd9ef7f9731d030d7ba379bf6232e714a11380311684056c71ee3c5aee61250cca5a19f5927fa2da32129a8bcb
@@ -0,0 +1,520 @@
1
+ # minify-html
2
+
3
+ A fast one-pass in-place HTML minifier written in Rust with context-aware whitespace handling.
4
+
5
+ Also supports JS minification by plugging into [esbuild](https://github.com/evanw/esbuild).
6
+
7
+ Available as:
8
+ - CLI for macOS and Linux.
9
+ - Rust library.
10
+ - Native library for Node.js, Python, Java, and Ruby.
11
+
12
+ ## Features
13
+
14
+ - Minification is done in one pass with no backtracking or DOM/AST building.
15
+ - No extra heap memory is allocated during processing, which increases performance.
16
+ - Context-aware whitespace handling allows maximum minification while retaining desired spaces.
17
+ - Well tested with a large test suite and extensive [fuzzing](./fuzz).
18
+
19
+ ## Performance
20
+
21
+ Speed and effectiveness of Node.js version compared to [html-minfier](https://github.com/kangax/html-minifier) and [minimize](https://github.com/Swaagie/minimize), run on popular already-minified web pages. See [bench](./bench) folder for more details.
22
+
23
+ <img width="435" alt="Chart showing speed of HTML minifiers" src="https://wilsonl.in/minify-html/bench/0.2.5/average-speeds.png"> <img width="435" alt="Chart showing effectiveness of HTML minifiers" src="https://wilsonl.in/minify-html/bench/0.2.5/average-sizes.png">
24
+
25
+ ## Usage
26
+
27
+ ### CLI
28
+
29
+ Precompiled binaries are available for x86-64 macOS and Linux.
30
+
31
+ ##### Get
32
+
33
+ [macOS](https://wilsonl.in/minify-html/bin/0.2.5-macos-x86_64) |
34
+ [Linux](https://wilsonl.in/minify-html/bin/0.2.5-linux-x86_64)
35
+
36
+ ##### Use
37
+
38
+ Use the `--help` argument for more details.
39
+
40
+ ```bash
41
+ minify-html --src /path/to/src.html --out /path/to/output.min.html
42
+ ```
43
+
44
+ ### API
45
+
46
+ <details>
47
+ <summary><strong>Rust</strong></summary>
48
+
49
+ ##### Get
50
+
51
+ ```toml
52
+ [dependencies]
53
+ minify-html = { version = "0.2.5", features = ["js-esbuild"] }
54
+ ```
55
+
56
+ Building with the `js-esbuild` feature requires the Go compiler to be installed as well, to build the [JS minifier](https://github.com/evanw/esbuild).
57
+
58
+ If the `js-esbuild` feature is not enabled, `cfg.minify_js` will have no effect.
59
+
60
+ ##### Use
61
+
62
+ ```rust
63
+ use minify_html::{Cfg, FriendlyError, in_place, copy, with_friendly_error, truncate};
64
+
65
+ fn main() {
66
+ let mut code = b"<p> Hello, world! </p>".to_vec();
67
+ let cfg = &Cfg {
68
+ minify_js: false,
69
+ };
70
+
71
+ // Minifies a slice in-place and returns the new minified length,
72
+ // but leaves any original code after the minified code intact.
73
+ match in_place(&mut code, cfg) {
74
+ Ok(minified_len) => {}
75
+ Err((error_type, error_position)) => {}
76
+ };
77
+
78
+ // Creates a vector copy containing only minified code
79
+ // instead of minifying in-place.
80
+ match copy(&code, cfg) {
81
+ Ok(minified) => {}
82
+ Err((error_type, error_position)) => {}
83
+ };
84
+
85
+ // Minifies a vector in-place, and then truncates the
86
+ // vector to the new minified length.
87
+ match truncate(&mut code, cfg) {
88
+ Ok(()) => {}
89
+ Err((error_type, error_position)) => {}
90
+ };
91
+
92
+ // Identical to `in_place` except with FriendlyError instead.
93
+ // `code_context` is a string of a visual representation of the source,
94
+ // with line numbers and position markers to aid in debugging syntax.
95
+ match with_friendly_error(&mut code, cfg) {
96
+ Ok(minified_len) => {}
97
+ Err(FriendlyError { position, message, code_context }) => {
98
+ eprintln!("Failed at character {}:", position);
99
+ eprintln!("{}", message);
100
+ eprintln!("{}", code_context);
101
+ }
102
+ };
103
+ }
104
+ ```
105
+
106
+ </details>
107
+
108
+ <details>
109
+ <summary><strong>Node.js</strong></summary>
110
+
111
+ Package: [@minify-html/js-esbuild](https://www.npmjs.com/package/@minify-html/js-esbuild),
112
+ Binding: [Neon](https://neon-bindings.com/)
113
+ Platforms: macOS, Linux; Node.js 8 and higher
114
+
115
+ ##### Get
116
+
117
+ Using npm:
118
+
119
+ ```bash
120
+ npm i @minify-html/js-esbuild
121
+ ```
122
+
123
+ Using Yarn:
124
+
125
+ ```bash
126
+ yarn add @minify-html/js-esbuild
127
+ ```
128
+
129
+ ##### Use
130
+
131
+ ```js
132
+ const minifyHtml = require("@minify-html/js-esbuild");
133
+
134
+ const cfg = { minifyJs: false };
135
+ const minified = minifyHtml.minify("<p> Hello, world! </p>", cfg);
136
+
137
+ // Alternatively, minify in place to avoid copying.
138
+ const source = Buffer.from("<p> Hello, world! </p>", cfg);
139
+ minifyHtml.minifyInPlace(source);
140
+ ```
141
+
142
+ minify-html is also available for TypeScript:
143
+
144
+ ```ts
145
+ import * as minifyHtml from "@minify-html/js-esbuild";
146
+ import * as fs from "fs";
147
+
148
+ const cfg = { minifyJs: false };
149
+ const minified = minifyHtml.minify("<p> Hello, world! </p>", cfg);
150
+ minifyHtml.minifyInPlace(fs.readFileSync("source.html"), cfg);
151
+ ```
152
+
153
+ </details>
154
+
155
+ <details>
156
+ <summary><strong>Java</strong></summary>
157
+
158
+ Package: [in.wilsonl.minifyhtml](https://search.maven.org/artifact/in.wilsonl.minifyhtml/minify-html)
159
+ Binding: [JNI](https://github.com/jni-rs/jni-rs)
160
+ Platforms: macOS, Linux; Java 7 and higher
161
+
162
+ ##### Get
163
+
164
+ Add as a Maven dependency:
165
+
166
+ ```xml
167
+ <dependency>
168
+ <groupId>in.wilsonl.minifyhtml</groupId>
169
+ <artifactId>minify-html</artifactId>
170
+ <version>0.2.5</version>
171
+ </dependency>
172
+ ```
173
+
174
+ ##### Use
175
+
176
+ ```java
177
+ import in.wilsonl.minify_html.MinifyHtml;
178
+
179
+ MinifyHtml.Configuration cfg = new MinifyHtml.Configuration.Builder()
180
+ .setMinifyJs(false)
181
+ .build();
182
+ try {
183
+ String minified = MinifyHtml.minify("<p> Hello, world! </p>", cfg);
184
+ } catch (MinifyHtml.SyntaxException e) {
185
+ System.err.println(e.getMessage());
186
+ }
187
+
188
+ // Alternatively, minify in place:
189
+ assert source instanceof ByteBuffer && source.isDirect();
190
+ MinifyHtml.minifyInPlace(source, cfg);
191
+ ```
192
+
193
+ </details>
194
+
195
+ <details>
196
+ <summary><strong>Python</strong></summary>
197
+
198
+ Package: [minify_html](https://pypi.org/project/minify_html)
199
+ Binding: [PyO3](https://github.com/PyO3/pyo3)
200
+ Platforms: macOS, Linux; CPython 3.5 and higher
201
+
202
+ ##### Get
203
+
204
+ Add the PyPI project as a dependency and install it using `pip` or `pipenv`.
205
+
206
+ ##### Use
207
+
208
+ ```python
209
+ import minify_html
210
+
211
+ try:
212
+ minified = minify_html.minify("<p> Hello, world! </p>", minify_js=False)
213
+ except SyntaxError as e:
214
+ print(e)
215
+ ```
216
+
217
+ </details>
218
+
219
+ <details>
220
+ <summary><strong>Ruby</strong></summary>
221
+
222
+ Package: [minify_html](https://rubygems.org/gems/minify_html)
223
+ Binding: [Rutie](https://github.com/danielpclark/rutie)
224
+ Platforms: macOS, Linux; Ruby 2.5 and higher
225
+
226
+ ##### Get
227
+
228
+ Add the library as a dependency to `Gemfile` or `*.gemspec`.
229
+
230
+ ##### Use
231
+
232
+ ```ruby
233
+ require 'minify_html'
234
+
235
+ print MinifyHtml.minify("<p> Hello, world! </p>", { :minify_js => false })
236
+ ```
237
+
238
+ </details>
239
+
240
+ ## Minification
241
+
242
+ ### Whitespace
243
+
244
+ minify-html has advanced context-aware whitespace minification that does things such as:
245
+
246
+ - Leave whitespace untouched in `pre` and `code`, which are whitespace sensitive.
247
+ - Trim and collapse whitespace in content tags, as whitespace is collapsed anyway when rendered.
248
+ - Remove whitespace in layout tags, which allows the use of inline layouts while keeping formatted code.
249
+
250
+ #### Methods
251
+
252
+ There are three whitespace minification methods. When processing text content, minify-html chooses which ones to use depending on the containing element.
253
+
254
+ <details>
255
+ <summary><strong>Collapse whitespace</strong></summary>
256
+
257
+ > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) elements.
258
+
259
+ Reduce a sequence of whitespace characters in text nodes to a single space (U+0020).
260
+
261
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
262
+
263
+ ```html
264
+ <p>↵
265
+ ··The·quick·brown·fox↵
266
+ ··jumps·over·the·lazy↵
267
+ ··dog.↵
268
+ </p>
269
+ ```
270
+
271
+ <td>
272
+
273
+ ```html
274
+ <p>·The·quick·brown·fox·jumps·over·the·lazy·dog.·</p>
275
+ ```
276
+
277
+ </table>
278
+ </details>
279
+
280
+ <details>
281
+ <summary><strong>Destroy whole whitespace</strong></summary>
282
+
283
+ > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs), [content](src/spec/tag/whitespace.rs), [content-first](./src/spec/tag/whitespace.rs), and [formatting](./src/spec/tag/whitespace.rs) elements.
284
+
285
+ Remove any text nodes between tags that only consist of whitespace characters.
286
+
287
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
288
+
289
+ ```html
290
+ <ul>↵
291
+ ··<li>A</li>↵
292
+ ··<li>B</li>↵
293
+ ··<li>C</li>↵
294
+ </ul>
295
+ ```
296
+
297
+ <td>
298
+
299
+ ```html
300
+ <ul>↵
301
+ ··<li>A</li><li>B</li><li>C</li>↵
302
+ </ul>
303
+ ```
304
+
305
+ </table>
306
+ </details>
307
+
308
+ <details>
309
+ <summary><strong>Trim whitespace</strong></summary>
310
+
311
+ > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) and [formatting](./src/spec/tag/whitespace.rs) elements.
312
+
313
+ Remove any leading/trailing whitespace from any leading/trailing text nodes of a tag.
314
+
315
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
316
+
317
+ ```html
318
+ <p>↵
319
+ ··Hey,·I·<em>just</em>·found↵
320
+ ··out·about·this·<strong>cool</strong>·website!↵
321
+ ··<sup>[1]</sup>↵
322
+ </p>
323
+ ```
324
+
325
+ <td>
326
+
327
+ ```html
328
+ <p>Hey,·I·<em>just</em>·found↵
329
+ ··out·about·this·<strong>cool</strong>·website!↵
330
+ ··<sup>[1]</sup></p>
331
+ ```
332
+
333
+ </table>
334
+ </details>
335
+
336
+ #### Element types
337
+
338
+ minify-html recognises elements based on one of a few ways it assumes they are used. By making these assumptions, it can apply optimal whitespace minification strategies.
339
+
340
+ |Group|Elements|Expected children|
341
+ |---|---|---|
342
+ |Formatting|`a`, `strong`, [and others](./src/spec/tag/whitespace.rs)|Formatting elements, text.|
343
+ |Content|`h1`, `p`, [and others](src/spec/tag/whitespace.rs)|Formatting elements, text.|
344
+ |Layout|`div`, `ul`, [and others](./src/spec/tag/whitespace.rs)|Layout elements, content elements.|
345
+ |Content-first|`label`, `li`, [and others](./src/spec/tag/whitespace.rs)|Like content but could be layout with only one child.|
346
+
347
+ <details>
348
+ <summary><strong>Formatting elements</strong></summary>
349
+
350
+ > Whitespace is collapsed.
351
+
352
+ Formatting elements are usually inline elements that wrap around part of some text in a content element, so its whitespace isn't trimmed as they're probably part of the content.
353
+
354
+ </details>
355
+
356
+ <details>
357
+ <summary><strong>Content elements</strong></summary>
358
+
359
+ > Whitespace is trimmed and collapsed.
360
+
361
+ Content elements usually represent a contiguous and complete unit of content such as a paragraph. As such, whitespace is significant but sequences of them are most likely due to formatting.
362
+
363
+ ###### Before
364
+
365
+ ```html
366
+ <p>↵
367
+ ··Hey,·I·<em>just</em>·found↵
368
+ ··out·about·this·<strong>cool</strong>·website!↵
369
+ ··<sup>[1]</sup>↵
370
+ </p>
371
+ ```
372
+
373
+ ###### After
374
+
375
+ ```html
376
+ <p>Hey,·I·<em>just</em>·found·out·about·this·<strong>cool</strong>·website!·<sup>[1]</sup></p>
377
+ ```
378
+
379
+ </details>
380
+
381
+ <details>
382
+ <summary><strong>Layout elements</strong></summary>
383
+
384
+ > Whitespace is trimmed and collapsed. Whole whitespace is removed.
385
+
386
+ These elements should only contain other elements and no text. This makes it possible to remove whole whitespace, which is useful when using `display: inline-block` so that whitespace between elements (e.g. indentation) does not alter layout and styling.
387
+
388
+ ###### Before
389
+
390
+ ```html
391
+ <ul>↵
392
+ ··<li>A</li>↵
393
+ ··<li>B</li>↵
394
+ ··<li>C</li>↵
395
+ </ul>
396
+ ```
397
+
398
+ ###### After
399
+
400
+ ```html
401
+ <ul><li>A</li><li>B</li><li>C</li></ul>
402
+ ```
403
+
404
+ </details>
405
+
406
+ <details>
407
+ <summary><strong>Content-first elements</strong></summary>
408
+
409
+ > Whitespace is trimmed and collapsed.
410
+
411
+ These elements are usually like content elements but are occasionally used like a layout element with one child. Whole whitespace is not removed as it might contain content, but this is OK for using as layout as there is only one child and whitespace is trimmed.
412
+
413
+ ###### Before
414
+
415
+ ```html
416
+ <li>↵
417
+ ··<article>↵
418
+ ····<section></section>↵
419
+ ····<section></section>↵
420
+ ··</article>↵
421
+ </li>
422
+ ```
423
+
424
+ ###### After
425
+
426
+ ```html
427
+ <li><article><section></section><section></section></article></li>
428
+ ```
429
+
430
+ </details>
431
+
432
+ ### Tags
433
+
434
+ [Optional closing tags](https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission) are removed.
435
+
436
+ ### Attributes
437
+
438
+ Any entities in attribute values are decoded, and then the shortest representation of the value is calculated and used:
439
+
440
+ - Double quoted, with any `"` encoded.
441
+ - Single quoted, with any `'` encoded.
442
+ - Unquoted, with `"`/`'` first character (if applicable), `>` last character (if applicable), and any whitespace encoded.
443
+
444
+ `class` and `d` attributes have their whitespace (after any decoding) trimmed and collapsed.
445
+
446
+ [Boolean attribute](./gen/attrs.json) values are removed.
447
+ [Some other attributes](./gen/attrs.json) are completely removed if their value is empty or the default value after any processing.
448
+
449
+ `type` attributes on `script` tags with a value equaling a [JavaScript MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) are removed.
450
+
451
+ If an attribute value is empty after any processing, everything but the name is completely removed (i.e. no `=`), as an empty attribute is implicitly [the same](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2) as an attribute with an empty string value.
452
+
453
+ Spaces are removed between attributes if possible.
454
+
455
+ ### Entities
456
+
457
+ Entities are decoded if valid (see relevant parsing section) and their decoded characters as UTF-8 is shorter or equal in length.
458
+
459
+ Numeric entities that do not refer to a valid [Unicode Scalar Value](https://www.unicode.org/glossary/#unicode_scalar_value) are replaced with the [replacement character](https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character).
460
+
461
+ If an entity is unintentionally formed after decoding, the leading ampersand is encoded, e.g. `&&#97;&#109;&#112;;` becomes `&ampamp;`. This is done as `&amp` is equal to or shorter than all other entity representations of characters part of an entity (`[&#a-zA-Z0-9;]`), and there is no other conflicting entity name that starts with `amp`.
462
+
463
+ It's possible to get an unintentional entity after removing comments, e.g. `&am<!-- -->p`.
464
+
465
+ Left chevrons after any decoding in text are encoded to `&LT` if possible or `&LT;` otherwise.
466
+
467
+ ### Comments
468
+
469
+ Comments are removed.
470
+
471
+ ### Ignored
472
+
473
+ Bangs, [processing instructions](https://en.wikipedia.org/wiki/Processing_Instruction), and empty elements are not removed as it is assumed there is a special reason for their declaration.
474
+
475
+ ## Parsing
476
+
477
+ Only UTF-8/ASCII-encoded HTML code is supported.
478
+
479
+ minify-html does no syntax checking or standards enforcement for performance and code complexity reasons.
480
+
481
+ For example, this means that it's not an error to have self-closing tags, declare multiple `<body>` elements, use incorrect attribute names and values, or write something like `<br>alert('');</br>`
482
+
483
+ However, there are some syntax requirements for speed and sanity.
484
+
485
+ ### Tags
486
+
487
+ Tag names are case sensitive. For example, this means that `P` won't be recognised as a content element, `bR` won't be considered as a void tag, and the contents of `Script` won't be parsed as JavaScript.
488
+
489
+ Tags must not be [omitted](https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission). Void tags must not have a separate closing tag e.g. `</input>`.
490
+
491
+ ### Entities
492
+
493
+ Well-formed entities are decoded, including in attribute values.
494
+
495
+ They are interpreted as characters representing their decoded value. This means that `&#9;` is considered a whitespace character and could be minified.
496
+
497
+ Malformed entities are interpreted literally as a sequence of characters.
498
+
499
+ If a named entity is an invalid reference as per the [specification](https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references), it is considered malformed.
500
+
501
+ Numeric character references that do not reference a valid [Unicode Scalar Value](https://www.unicode.org/glossary/#unicode_scalar_value) are considered malformed.
502
+
503
+ ### Attributes
504
+
505
+ Backticks (`` ` ``) are not valid quote marks and not interpreted as such.
506
+ However, backticks are valid attribute value quotes in Internet Explorer.
507
+
508
+ Special handling of some attributes require case sensitive names and values. For example, `CLASS` won't be recognised as an attribute to minify, and `type="Text/JavaScript"` on a `<script>` will not be removed.
509
+
510
+ ### Script and style
511
+
512
+ `script` and `style` tags must be closed with `</script>` and `</style>` respectively (case sensitive).
513
+
514
+ minify-html does **not** handle [escaped and double-escaped](./notes/Script%20data.md) script content.
515
+
516
+ ## Issues and contributions
517
+
518
+ Pull requests and any contributions welcome!
519
+
520
+ If minify-html did something unexpected, misunderstood some syntax, or incorrectly kept/removed some code, [raise an issue](https://github.com/wilsonzlin/minify-html/issues) with some relevant code that can be used to reproduce and investigate the issue.
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,18 @@
1
+ require 'fiddle'
2
+
3
+ class MinifyHtmlLoader
4
+ def self.operating_system
5
+ case RbConfig::CONFIG['host_os'].downcase
6
+ when /linux|bsd|solaris/ then 'linux'
7
+ when /darwin/ then 'macos'
8
+ when /mingw|mswin/ then 'windows'
9
+ else 'unknown'
10
+ end
11
+ end
12
+
13
+ def self.lib_path
14
+ File.join(__dir__, [operating_system, '-ruby', RUBY_VERSION.split('.')[0...-1].join('.')].join(''))
15
+ end
16
+ end
17
+
18
+ Fiddle::Function.new(Fiddle.dlopen(MinifyHtmlLoader.lib_path)['Init_minify_html'], [], Fiddle::TYPE_VOIDP).call
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minify_html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.5
5
+ platform: ruby
6
+ authors:
7
+ - Wilson Lin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fiddle
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description:
28
+ email:
29
+ - code@wilsonl.in
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - lib/linux-ruby2.5
36
+ - lib/linux-ruby2.6
37
+ - lib/linux-ruby2.7
38
+ - lib/macos-ruby2.5
39
+ - lib/macos-ruby2.6
40
+ - lib/macos-ruby2.7
41
+ - lib/minify_html.rb
42
+ homepage: https://github.com/wilsonzlin/minify_html
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.7.6.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Fast allocation-less HTML minifier with smart whitespace handling
66
+ test_files: []