Minify class attribute values

This commit is contained in:
Wilson Lin 2018-08-07 16:25:15 +12:00
parent 73839141d3
commit 2997fab7cd
3 changed files with 25 additions and 5 deletions

View File

@ -276,6 +276,8 @@ Tags not in one of the categories below are **specific tags**.
### Options
Note that only existing whitespace will be up for removal via minification. Entities that represent whitespace will not be decoded and then removed.
For options that have a list of tags as their values, the tags should be separated by a comma.
An `*` (asterisk, U+002A) can be used to represent the complete set of possible tags. It essentially fully enables or disables the option.
@ -429,7 +431,7 @@ The following removal of attributes and tags as minification strategies are not
If they exist, it is assumed there is a special reason for being so.
- Remove empty attributes
- Remove empty attributes (including ones that would be empty after minification e.g. `class=" "`)
- Remove empty elements
- Remove redundant attributes
- Remove `type` attribute on `<script>` tags

View File

@ -36,12 +36,15 @@ void hbsh_attr(hbs_options_t so, hbu_pipe_t pipe) {
}
}
int collapse_and_trim_whitespace = hbu_buffer_compare_lit(name, "class") == 0 &&
so->trim_class_attr;
hbu_buffer_destroy(name);
if (hbu_pipe_accept_if(pipe, '=')) {
if (hbr_attrvalquote_check(hbu_pipe_peek(pipe))) {
// Quoted attribute value
hbsh_quoteattrval(pipe);
hbsh_quoteattrval(pipe, collapse_and_trim_whitespace);
} else {
if (!hbs_options_supressed_error(so, HBE_PARSE_UNQUOTED_ATTR)) {
hbu_pipe_error(pipe, HBE_PARSE_UNQUOTED_ATTR, "Unquoted attribute value");

View File

@ -6,11 +6,26 @@
#include "../../util/hbchar.h"
#include "../../util/pipe.c"
void hbsh_quoteattrval(hbu_pipe_t pipe) {
void hbsh_quoteattrval(hbu_pipe_t pipe, int collapse_and_trim_whitespace) {
hb_char_t quote_char = hbu_pipe_require_predicate(pipe, &hbr_attrvalquote_check, "attribute value quote");
while (hbu_pipe_peek(pipe) != quote_char) {
hbu_pipe_accept(pipe);
int whitespace = 0;
if (collapse_and_trim_whitespace) {
hbu_pipe_skip_while_predicate(pipe, &hbr_whitespace_check);
}
hb_char_t c;
while ((c = hbu_pipe_peek(pipe)) != quote_char) {
if (collapse_and_trim_whitespace && hbr_whitespace_check(c)) {
whitespace = 1;
hbu_pipe_skip(pipe);
} else {
if (whitespace) {
hbu_pipe_write(pipe, ' ');
}
hbu_pipe_accept(pipe);
}
}
hbu_pipe_require(pipe, quote_char);