Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions minhtml/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ struct Cli {
#[structopt(short, long, parse(from_os_str))]
output: Option<std::path::PathBuf>,

/// Always quote attribute values.
#[structopt(long)]
always_quote_attribute_values: bool,

/// Allow unquoted attribute values in the output to contain characters prohibited by the [WHATWG specification](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2). These will still be parsed correctly by almost all browsers.
#[structopt(long)]
allow_noncompliant_unquoted_attribute_values: bool,
Expand Down Expand Up @@ -113,6 +117,7 @@ fn main() {

#[rustfmt::skip]
let cfg = Arc::new(Cfg {
always_quote_attribute_values: args.always_quote_attribute_values,
allow_noncompliant_unquoted_attribute_values: args.allow_noncompliant_unquoted_attribute_values,
allow_optimal_entities: args.allow_optimal_entities,
allow_removing_spaces_between_attributes: args.allow_removing_spaces_between_attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Class representing minification configuration.
*/
public class Configuration {
public final boolean always_quote_attribute_values;
public final boolean allow_noncompliant_unquoted_attribute_values;
public final boolean allow_optimal_entities;
public final boolean allow_removing_spaces_between_attributes;
Expand All @@ -23,6 +24,7 @@ public class Configuration {
public final boolean remove_processing_instructions;

private Configuration(
boolean always_quote_attribute_values,
boolean allow_noncompliant_unquoted_attribute_values,
boolean allow_optimal_entities,
boolean allow_removing_spaces_between_attributes,
Expand All @@ -39,6 +41,7 @@ private Configuration(
boolean remove_bangs,
boolean remove_processing_instructions
) {
this.always_quote_attribute_values = always_quote_attribute_values;
this.allow_noncompliant_unquoted_attribute_values = allow_noncompliant_unquoted_attribute_values;
this.allow_optimal_entities = allow_optimal_entities;
this.allow_removing_spaces_between_attributes = allow_removing_spaces_between_attributes;
Expand All @@ -60,6 +63,7 @@ private Configuration(
* Builder to help create configuration.
*/
public static class Builder {
private boolean always_quote_attribute_values = false;
private boolean allow_noncompliant_unquoted_attribute_values = false;
private boolean allow_optimal_entities = false;
private boolean allow_removing_spaces_between_attributes = false;
Expand All @@ -76,6 +80,10 @@ public static class Builder {
private boolean remove_bangs = false;
private boolean remove_processing_instructions = false;

public Builder setAlwaysQuoteAttributeValues(boolean v) {
this.always_quote_attribute_values = v;
return this;
}
public Builder setAllowNoncompliantUnquotedAttributeValues(boolean v) {
this.allow_noncompliant_unquoted_attribute_values = v;
return this;
Expand Down Expand Up @@ -139,6 +147,7 @@ public Builder setRemoveProcessingInstructions(boolean v) {

public Configuration build() {
return new Configuration(
this.always_quote_attribute_values,
this.allow_noncompliant_unquoted_attribute_values,
this.allow_optimal_entities,
this.allow_removing_spaces_between_attributes,
Expand Down
1 change: 1 addition & 0 deletions minify-html-java/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn build_cfg(env: &JNIEnv, obj: &JObject) -> Cfg {
#[rustfmt::skip]
// This is a statement because "attributes on expressions are experimental".
let cfg = Cfg {
always_quote_attribute_values: env.get_field(*obj, "always_quote_attribute_values", "Z").unwrap().z().unwrap(),
allow_noncompliant_unquoted_attribute_values: env.get_field(*obj, "allow_noncompliant_unquoted_attribute_values", "Z").unwrap().z().unwrap(),
allow_optimal_entities: env.get_field(*obj, "allow_optimal_entities", "Z").unwrap().z().unwrap(),
allow_removing_spaces_between_attributes: env.get_field(*obj, "allow_removing_spaces_between_attributes", "Z").unwrap().z().unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions minify-html-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
export function minify(
src: Buffer,
cfg: {
/** Always quote attribute values in the output. */
always_quote_attribute_values?: boolean;
/** Allow unquoted attribute values in the output to contain characters prohibited by the [WHATWG specification](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2). These will still be parsed correctly by almost all browsers. */
allow_noncompliant_unquoted_attribute_values?: boolean;
/** Allow some minifications around entities that may not pass validation, but will still be parsed correctly by almost all browsers. */
Expand Down
1 change: 1 addition & 0 deletions minify-html-nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn minify(mut cx: FunctionContext) -> JsResult<JsBuffer> {
};
#[rustfmt::skip]
let cfg = minify_html::Cfg {
always_quote_attribute_values: get_bool!(cx, opt, "always_quote_attribute_values"),
allow_noncompliant_unquoted_attribute_values: get_bool!(cx, opt, "allow_noncompliant_unquoted_attribute_values"),
allow_optimal_entities: get_bool!(cx, opt, "allow_optimal_entities"),
allow_removing_spaces_between_attributes: get_bool!(cx, opt, "allow_removing_spaces_between_attributes"),
Expand Down
1 change: 1 addition & 0 deletions minify-html-python/minify_html.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def minify(
code: str,
always_quote_attribute_values: bool = False,
allow_noncompliant_unquoted_attribute_values: bool = False,
allow_optimal_entities: bool = False,
allow_removing_spaces_between_attributes: bool = False,
Expand Down
3 changes: 3 additions & 0 deletions minify-html-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::string::String;
signature = (
code,
*,
always_quote_attribute_values = true,
allow_noncompliant_unquoted_attribute_values = false,
allow_optimal_entities = false,
allow_removing_spaces_between_attributes = false,
Expand All @@ -29,6 +30,7 @@ use std::string::String;
)]
fn minify(
code: String,
always_quote_attribute_values: bool,
allow_noncompliant_unquoted_attribute_values: bool,
allow_optimal_entities: bool,
allow_removing_spaces_between_attributes: bool,
Expand All @@ -47,6 +49,7 @@ fn minify(
) -> String {
let code = code.into_bytes();
let out_code = minify_html_native(&code, &Cfg {
always_quote_attribute_values,
allow_noncompliant_unquoted_attribute_values,
allow_optimal_entities,
allow_removing_spaces_between_attributes,
Expand Down
1 change: 1 addition & 0 deletions minify-html-ruby/ext/minify_html/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use minify_html::Cfg as CfgNative;
fn minify_html(source: String, cfg: RHash) -> String {
#[rustfmt::skip]
let out_code = minify_html_native(source.as_bytes(), &CfgNative {
always_quote_attribute_values: cfg.aref(StaticSymbol::new("always_quote_attribute_values")).unwrap_or_default(),
allow_noncompliant_unquoted_attribute_values: cfg.aref(StaticSymbol::new("allow_noncompliant_unquoted_attribute_values")).unwrap_or_default(),
allow_optimal_entities: cfg.aref(StaticSymbol::new("allow_optimal_entities")).unwrap_or_default(),
allow_removing_spaces_between_attributes: cfg.aref(StaticSymbol::new("allow_removing_spaces_between_attributes")).unwrap_or_default(),
Expand Down
1 change: 1 addition & 0 deletions minify-html-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ macro_rules! get_prop {
pub fn minify(code: &[u8], cfg: &JsValue) -> Vec<u8> {
#[rustfmt::skip]
let cfg = minify_html::Cfg {
always_quote_attribute_values: get_prop!(cfg, "always_quote_attribute_values"),
allow_noncompliant_unquoted_attribute_values: get_prop!(cfg, "allow_noncompliant_unquoted_attribute_values"),
allow_optimal_entities: get_prop!(cfg, "allow_optimal_entities"),
allow_removing_spaces_between_attributes: get_prop!(cfg, "allow_removing_spaces_between_attributes"),
Expand Down
2 changes: 2 additions & 0 deletions minify-html/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/// minification approach.
#[derive(Clone, Default)]
pub struct Cfg {
/// Always quote attribute values in the output.
pub always_quote_attribute_values: bool,
/// Allow unquoted attribute values in the output to contain characters prohibited by the [WHATWG specification](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2). These will still be parsed correctly by almost all browsers.
pub allow_noncompliant_unquoted_attribute_values: bool,
/// Allow some minifications around entities that may not pass validation, but will still be parsed correctly by almost all browsers.
Expand Down
18 changes: 10 additions & 8 deletions minify-html/src/minify/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,15 @@ pub fn minify_attr(
if sq.len() < min.len() {
min = sq;
};
let uq = encode_unquoted(
&encoded,
must_end_with_semicolon,
!cfg.allow_noncompliant_unquoted_attribute_values,
);
if uq.len() < min.len() {
min = uq;
};
if !cfg.always_quote_attribute_values {
let uq = encode_unquoted(
&encoded,
must_end_with_semicolon,
!cfg.allow_noncompliant_unquoted_attribute_values,
);
if uq.len() < min.len() {
min = uq;
};
}
AttrMinified::Value(min)
}