From ad21466d2c2473f2e04af5c35e3cd15b513c6912 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Thu, 16 Feb 2023 19:23:03 +1100 Subject: [PATCH] Change default CSS minifier optimising level and allow configuring it --- CHANGELOG.md | 5 ++ cli/src/main.rs | 15 +++++ .../in/wilsonl/minifyhtml/Configuration.java | 30 +++++++++ nodejs/index.d.ts | 12 ++++ nodejs/src/lib.rs | 65 +++++++------------ python/main/src/lib.rs | 6 ++ ruby/src/lib.rs | 3 + rust/main/src/ast/mod.rs | 4 +- rust/main/src/cfg/mod.rs | 10 ++- rust/main/src/minify/css.rs | 14 +++- rust/main/src/minify/rcdata.rs | 22 +++++-- 11 files changed, 134 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03c5548..ccab3de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # minify-html changelog +## Pending + +- Change the default CSS minifier optimisation level to 1, as higher levels may perform dangerous optimisations. +- Allow configuring the CSS minifier optimisation level. + ## 0.10.8 - [Node.js] Fix assertion failure panic on invalid argument type. diff --git a/cli/src/main.rs b/cli/src/main.rs index 5f38c24..b4e8de2 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -31,6 +31,18 @@ struct Cli { #[structopt(long)] minify_css: bool, + /// Use optimisation level 1 for the CSS minifier. + #[structopt(long)] + minify_css_level_1: bool, + + /// Use optimisation level 2 for the CSS minifier. May perform some dangerous optimisations. + #[structopt(long)] + minify_css_level_2: bool, + + /// Use optimisation level 3 for the CSS minifier. May perform many dangerous optimisations. + #[structopt(long)] + minify_css_level_3: bool, + #[structopt(long)] /// Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. do_not_minify_doctype: bool, @@ -92,6 +104,9 @@ fn main() { keep_html_and_head_opening_tags: args.keep_html_and_head_opening_tags, keep_spaces_between_attributes: args.keep_spaces_between_attributes, minify_css: args.minify_css, + minify_css_level_1: args.minify_css_level_1, + minify_css_level_2: args.minify_css_level_2, + minify_css_level_3: args.minify_css_level_3, minify_js: args.minify_js, remove_bangs: args.remove_bangs, remove_processing_instructions: args.remove_processing_instructions, diff --git a/java/src/main/java/in/wilsonl/minifyhtml/Configuration.java b/java/src/main/java/in/wilsonl/minifyhtml/Configuration.java index c772147..a2ff8ed 100644 --- a/java/src/main/java/in/wilsonl/minifyhtml/Configuration.java +++ b/java/src/main/java/in/wilsonl/minifyhtml/Configuration.java @@ -11,6 +11,9 @@ public class Configuration { public final boolean keep_html_and_head_opening_tags; public final boolean keep_spaces_between_attributes; public final boolean minify_css; + public final boolean minify_css_level_1; + public final boolean minify_css_level_2; + public final boolean minify_css_level_3; public final boolean minify_js; public final boolean remove_bangs; public final boolean remove_processing_instructions; @@ -23,6 +26,9 @@ public class Configuration { boolean keep_html_and_head_opening_tags, boolean keep_spaces_between_attributes, boolean minify_css, + boolean minify_css_level_1, + boolean minify_css_level_2, + boolean minify_css_level_3, boolean minify_js, boolean remove_bangs, boolean remove_processing_instructions @@ -34,6 +40,9 @@ public class Configuration { this.keep_html_and_head_opening_tags = keep_html_and_head_opening_tags; this.keep_spaces_between_attributes = keep_spaces_between_attributes; this.minify_css = minify_css; + this.minify_css_level_1 = minify_css_level_1; + this.minify_css_level_2 = minify_css_level_2; + this.minify_css_level_3 = minify_css_level_3; this.minify_js = minify_js; this.remove_bangs = remove_bangs; this.remove_processing_instructions = remove_processing_instructions; @@ -50,6 +59,9 @@ public class Configuration { private boolean keep_html_and_head_opening_tags = false; private boolean keep_spaces_between_attributes = false; private boolean minify_css = false; + private boolean minify_css_level_1 = false; + private boolean minify_css_level_2 = false; + private boolean minify_css_level_3 = false; private boolean minify_js = false; private boolean remove_bangs = false; private boolean remove_processing_instructions = false; @@ -89,6 +101,21 @@ public class Configuration { return this; } + public Builder setMinifyCssLevel1(boolean val) { + this.minify_css_level_1 = val; + return this; + } + + public Builder setMinifyCssLevel2(boolean val) { + this.minify_css_level_2 = val; + return this; + } + + public Builder setMinifyCssLevel3(boolean val) { + this.minify_css_level_3 = val; + return this; + } + public Builder setMinifyJs(boolean val) { this.minify_js = val; return this; @@ -114,6 +141,9 @@ public class Configuration { this.keep_html_and_head_opening_tags, this.keep_spaces_between_attributes, this.minify_css, + this.minify_css_level_1, + this.minify_css_level_2, + this.minify_css_level_3, this.minify_js, this.remove_bangs, this.remove_processing_instructions diff --git a/nodejs/index.d.ts b/nodejs/index.d.ts index a36446c..64295f6 100644 --- a/nodejs/index.d.ts +++ b/nodejs/index.d.ts @@ -28,6 +28,18 @@ export function minify( * If enabled, CSS in `