html-minifier 6.0.0

This library minifies streaming HTML, including embedded JavaScript and CSS, while preserving pre and textarea content.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use std::mem::take;

use educe::Educe;

use crate::{HTMLMinifierError, HTMLWriter, comment::HeldComment, functions::*};

#[derive(Educe, Debug, Copy, Clone, Eq, PartialEq)]
#[educe(Default)]
enum CSSStep {
    #[educe(Default)]
    Normal,
    Slash,
    Comment,
    KeptComment,
    String,
    Escape,
    HexEscape,
    URLStart,
    URLBody,
    CustomStart,
    Custom,
}

/// Check whether a byte can be a part of a CSS name.
#[inline]
const fn is_name_byte(e: u8) -> bool {
    e.is_ascii_alphanumeric() || matches!(e, b'-' | b'_') || e >= 0x80
}

#[inline]
fn hex_digit_value(e: u8) -> u32 {
    char::from(e).to_digit(16).unwrap_or(0)
}

/// Minify the CSS content of a `style` element, which arrives in chunks.
/// Only comments and whitespace are removed, so the CSS keeps its meaning.
#[derive(Educe, Clone)]
#[educe(Debug, Default)]
pub(crate) struct CSSMinifier {
    step:           CSSStep,
    /// The last two written bytes, with the latest one at the end. 0 means nothing.
    last:           [u8; 2],
    /// Whether the latest written byte ends an escape, which then works like a name byte.
    last_escape:    bool,
    /// Whether the latest written byte ends a hex escape that can still take a whitespace byte.
    open_hex:       bool,
    /// Whether whitespace or comments are pending between two tokens.
    gap:            bool,
    /// Whether the pending gap includes whitespace.
    gap_whitespace: bool,
    /// Whether a name is being written.
    in_name:        bool,
    /// Whether the current name follows `#` or `@`, which makes a hash or an at-keyword.
    name_prefixed:  bool,
    /// The first character of the current name, in lowercase.
    name_first:     u8,
    /// The number of characters of the current name, up to 255.
    name_length:    u8,
    /// How many characters of the current name match `url`.
    url_matched:    u8,
    /// Whether the latest name starts with `--`, which is a custom property.
    custom_name:    bool,
    /// How many bytes of `<!--` have been written as tokens.
    cdo:            u8,
    /// The quote of the current string.
    quote:          u8,
    /// The state of an escape in a string, a URL or a custom property value.
    /// 1 is after a backslash, and 2 to 7 are in a hex escape with 1 to 6 hex digits.
    escape:         u8,
    hex_digits:     u8,
    hex_value:      u32,
    /// Whether the previous byte of a comment is `*`.
    comment_star:   bool,
    /// Whether whitespace has been removed at the start of a custom property value.
    custom_space:   bool,
    custom_depth:   u32,
    custom_quote:   u8,
    /// 1 after `/`, 2 in a comment, and 3 in a comment after `*`.
    custom_comment: u8,
    comment:        HeldComment,
}

impl CSSMinifier {
    /// Prepare for a new style sheet.
    #[inline]
    pub(crate) fn reset(&mut self) {
        let comment = take(&mut self.comment);

        *self = Self {
            comment,
            ..Self::default()
        };
    }

    #[inline]
    fn wrote(&mut self, e: u8) {
        self.last = [self.last[1], e];
        self.last_escape = false;
        self.open_hex = false;
    }

    /// Check whether the latest written token and a token starting with `next` become different tokens without anything between them.
    fn merges(&self, next: u8) -> bool {
        let [before, last] = self.last;

        ((self.last_escape || is_name_byte(last))
            && (is_name_byte(next) || matches!(next, b'(' | b'\\' | b'%' | b'+')))
            || (matches!(last, b'.' | b'+') && next.is_ascii_digit())
            || (matches!(last, b'+' | b'-') && next == b'.')
            || (last.is_ascii_digit() && next == b'.')
            || (matches!(last, b'#' | b'@') && (is_name_byte(next) || next == b'\\'))
            || (matches!(last, b'~' | b'|' | b'^' | b'$' | b'*') && next == b'=')
            || (last == b'|' && next == b'|')
            || (last == b'/' && next == b'*')
            || (last == b'<' && matches!(next, b'!' | b'/'))
            || (before == b'<' && last == b'!' && next == b'-')
            || (before == b'-' && last == b'-' && next == b'>')
    }

    /// Write what replaces the pending whitespace and comments before a token starting with `next`.
    fn write_gap(&mut self, next: u8, out: &mut impl HTMLWriter) -> Result<(), HTMLMinifierError> {
        let last = self.last[1];

        // Nothing is needed at the start of the style sheet.
        if last != 0 {
            if self.gap_whitespace {
                // An escaped byte, such as `\:`, is a part of a name.
                let removable = (!self.last_escape
                    && matches!(
                        last,
                        b'{' | b'}' | b';' | b',' | b'(' | b'[' | b':' | b'>' | b'~'
                    ))
                    || matches!(next, b'{' | b'}' | b';' | b',' | b')' | b']' | b'>' | b'~');

                // The space stays if the tokens around it would merge, such as `-- >` and `~ =`.
                if !removable || self.merges(next) {
                    // A hex escape takes the first whitespace byte after it, so one more is needed.
                    if self.open_hex {
                        out.push(b' ')?;
                    }

                    out.push(b' ')?;
                    self.wrote(b' ');
                }
            } else if self.merges(next) {
                // A comment separates tokens without being whitespace, so an empty one is kept.
                out.push_bytes(b"/**/")?;
                self.last = *b"*/";
                self.last_escape = false;
                self.open_hex = false;
            }
        }

        self.gap = false;
        self.gap_whitespace = false;

        Ok(())
    }

    /// Track a byte after a backslash in a string, a URL or a custom property value.
    /// Return `false` if the escape has ended before the byte, so the byte has to be handled as usual.
    #[inline]
    fn continue_escape(&mut self, e: u8) -> bool {
        match self.escape {
            1 => {
                self.escape = if e.is_ascii_hexdigit() { 2 } else { 0 };

                true
            },
            2..=6 if e.is_ascii_hexdigit() => {
                self.escape += 1;

                true
            },
            _ => {
                // A hex escape takes the first whitespace byte after it.
                self.escape = 0;

                is_whitespace(e)
            },
        }
    }

    /// Track a character of the current name, which can come from an escape.
    #[inline]
    fn name_char(&mut self, c: u32) {
        let index = self.name_length;
        let lower = u8::try_from(c).map_or(0, |c| c.to_ascii_lowercase());

        self.url_matched = if !self.name_prefixed
            && index < 3
            && self.url_matched == index
            && lower == b"url"[usize::from(index)]
        {
            index + 1
        } else {
            0
        };

        match index {
            0 => self.name_first = lower,
            1 => self.custom_name = !self.name_prefixed && self.name_first == b'-' && lower == b'-',
            _ => (),
        }

        self.name_length = self.name_length.saturating_add(1);
    }

    /// Track a token byte that is written in the normal step.
    #[inline]
    fn write_token_byte(&mut self, e: u8) {
        let before = self.last[1];

        self.wrote(e);

        self.cdo = match (self.cdo, e) {
            (_, b'<') => 1,
            (1, b'!') => 2,
            (2, b'-') => 3,
            (3, b'-') => 4,
            _ => 0,
        };

        if is_name_byte(e) || e == b'\\' {
            if !self.in_name {
                // A name right after `#` or `@` is a hash or an at-keyword, which is never `url(` or a custom property.
                self.in_name = true;
                self.name_prefixed = matches!(before, b'#' | b'@');
                self.name_length = 0;
                self.url_matched = 0;
                self.custom_name = false;
            }

            if e == b'\\' {
                self.step = CSSStep::Escape;
            } else {
                self.name_char(u32::from(e));
            }

            if self.cdo == 4 {
                // `<!--` is a token of its own, so a name can start right after it.
                self.in_name = false;
                self.url_matched = 0;
                self.custom_name = false;
                self.cdo = 0;
            }
        } else {
            let url = self.in_name && self.url_matched == 3 && self.name_length == 3;

            self.in_name = false;
            self.url_matched = 0;

            match e {
                b'"' | b'\'' => {
                    self.quote = e;
                    self.escape = 0;
                    self.step = CSSStep::String;
                },
                b'(' if url => self.step = CSSStep::URLStart,
                b':' if self.custom_name => {
                    self.custom_space = false;
                    self.step = CSSStep::CustomStart;
                },
                _ => (),
            }

            self.custom_name = false;
        }
    }

    /// Input a chunk of CSS.
    pub(crate) fn feed(
        &mut self,
        bytes: &[u8],
        out: &mut impl HTMLWriter,
    ) -> Result<(), HTMLMinifierError> {
        // The start of the bytes that are written as they are.
        let mut run = 0;
        let mut p = 0;

        while p < bytes.len() {
            let e = bytes[p];

            match self.step {
                CSSStep::Normal => {
                    if is_whitespace(e) || e == b'/' {
                        if run < p {
                            out.push_bytes(&bytes[run..p])?;
                        }

                        run = p + 1;
                        self.in_name = false;
                        self.url_matched = 0;
                        self.cdo = 0;

                        if e == b'/' {
                            self.step = CSSStep::Slash;
                        } else {
                            self.gap = true;
                            self.gap_whitespace = true;
                        }
                    } else {
                        if self.gap {
                            self.write_gap(e, out)?;
                        }

                        self.write_token_byte(e);
                    }
                },
                CSSStep::Slash => {
                    if e == b'*' {
                        run = p + 1;
                        self.gap = true;
                        self.comment.start();
                        self.comment_star = false;
                        self.step = CSSStep::Comment;
                    } else {
                        // The pending `/` is a delimiter.
                        if self.gap {
                            self.write_gap(b'/', out)?;
                        }

                        out.push(b'/')?;
                        self.write_token_byte(b'/');
                        self.step = CSSStep::Normal;

                        continue;
                    }
                },
                CSSStep::Comment => {
                    run = p + 1;

                    if self.comment_star && e == b'/' {
                        // The comment is removed, and it stays a part of the gap.
                        self.step = CSSStep::Normal;
                    } else if self.comment.push(e) {
                        self.write_gap(b'/', out)?;

                        let held = self.comment.as_bytes();
                        out.push_bytes(held)?;
                        self.last = [held[held.len() - 2], held[held.len() - 1]];
                        self.last_escape = false;
                        self.open_hex = false;
                        self.step = CSSStep::KeptComment;
                    }

                    self.comment_star = e == b'*';
                },
                CSSStep::KeptComment => {
                    self.wrote(e);

                    if self.comment_star && e == b'/' {
                        self.step = CSSStep::Normal;
                    }

                    self.comment_star = e == b'*';
                },
                CSSStep::String => {
                    self.wrote(e);

                    if self.escape == 0 || !self.continue_escape(e) {
                        if e == b'\\' {
                            self.escape = 1;
                        } else if e == self.quote || matches!(e, b'\n' | b'\r' | 0x0C) {
                            // A newline ends a bad string, and the newline stays.
                            self.step = CSSStep::Normal;
                        }
                    }
                },
                CSSStep::Escape => {
                    self.wrote(e);

                    if matches!(e, b'\n' | b'\r' | 0x0C) {
                        // A backslash before a newline is not an escape, and the newline stays.
                        self.in_name = false;
                        self.url_matched = 0;
                        self.step = CSSStep::Normal;
                    } else if e.is_ascii_hexdigit() {
                        self.hex_digits = 1;
                        self.hex_value = hex_digit_value(e);
                        self.step = CSSStep::HexEscape;
                    } else {
                        self.name_char(u32::from(e));
                        self.last_escape = true;
                        self.step = CSSStep::Normal;
                    }
                },
                CSSStep::HexEscape => {
                    if self.hex_digits < 6 && e.is_ascii_hexdigit() {
                        self.wrote(e);
                        self.hex_digits += 1;
                        self.hex_value = self.hex_value * 16 + hex_digit_value(e);
                    } else {
                        let terminator = is_whitespace(e);

                        // The whitespace after a hex escape is a part of the escape.
                        if terminator {
                            self.wrote(e);
                        }

                        self.name_char(self.hex_value);
                        self.last_escape = true;
                        self.open_hex = !terminator;
                        self.step = CSSStep::Normal;

                        if !terminator {
                            continue;
                        }
                    }
                },
                CSSStep::URLStart => {
                    if is_whitespace(e) {
                        // The whitespace at the start of `url()` is not a part of the URL.
                        if run < p {
                            out.push_bytes(&bytes[run..p])?;
                        }

                        run = p + 1;
                    } else {
                        // A quoted URL is an ordinary string argument.
                        self.step = if matches!(e, b'"' | b'\'' | b')') {
                            CSSStep::Normal
                        } else {
                            self.escape = 0;
                            CSSStep::URLBody
                        };

                        continue;
                    }
                },
                CSSStep::URLBody => {
                    self.wrote(e);

                    if self.escape == 0 || !self.continue_escape(e) {
                        if e == b'\\' {
                            self.escape = 1;
                        } else if e == b')' {
                            self.step = CSSStep::Normal;
                        }
                    }
                },
                CSSStep::CustomStart => {
                    if is_whitespace(e) {
                        // The whitespace at the start of a custom property value is not a part of the value.
                        if run < p {
                            out.push_bytes(&bytes[run..p])?;
                        }

                        run = p + 1;
                        self.custom_space = true;
                    } else {
                        // Old browsers drop an empty value without whitespace, such as `--x:;`, so one space stays.
                        if self.custom_space && matches!(e, b';' | b'}' | b'!') {
                            out.push(b' ')?;
                            self.wrote(b' ');
                        }

                        self.custom_depth = 0;
                        self.custom_quote = 0;
                        self.custom_comment = 0;
                        self.escape = 0;
                        self.step = CSSStep::Custom;

                        continue;
                    }
                },
                CSSStep::Custom => {
                    // A custom property value is kept as it is until it ends at the top level.
                    if self.custom_comment >= 2 {
                        self.custom_comment = match e {
                            b'*' => 3,
                            b'/' if self.custom_comment == 3 => 0,
                            _ => 2,
                        };
                    } else if self.escape > 0 && self.continue_escape(e) {
                        // The byte belongs to an escape.
                    } else if self.custom_quote != 0 {
                        if e == b'\\' {
                            self.escape = 1;
                        } else if e == self.custom_quote || matches!(e, b'\n' | b'\r' | 0x0C) {
                            self.custom_quote = 0;
                        }
                    } else if self.custom_comment == 1 && e == b'*' {
                        self.custom_comment = 2;
                    } else {
                        self.custom_comment = 0;

                        match e {
                            b'/' => self.custom_comment = 1,
                            b'\\' => self.escape = 1,
                            b'"' | b'\'' => self.custom_quote = e,
                            b'(' | b'[' | b'{' => {
                                self.custom_depth = self.custom_depth.saturating_add(1)
                            },
                            b')' | b']' => self.custom_depth = self.custom_depth.saturating_sub(1),
                            b';' | b'}' if self.custom_depth == 0 => {
                                self.step = CSSStep::Normal;

                                continue;
                            },
                            b'}' => self.custom_depth -= 1,
                            _ => (),
                        }
                    }

                    self.wrote(e);
                },
            }

            p += 1;
        }

        if run < bytes.len() {
            out.push_bytes(&bytes[run..])?;
        }

        Ok(())
    }

    /// Write the rest of the minified CSS.
    pub(crate) fn finish(&mut self, out: &mut impl HTMLWriter) -> Result<(), HTMLMinifierError> {
        if self.step == CSSStep::Slash {
            // A `/` at the end is a delimiter.
            if self.gap {
                self.write_gap(b'/', out)?;
            }

            out.push(b'/')?;
        }

        // Whitespace and an unterminated comment at the end are removed.
        self.reset();

        Ok(())
    }
}