Improve attr min
This commit is contained in:
parent
bdd6458f22
commit
f304baed0f
35
gen/attrs.ts
35
gen/attrs.ts
|
@ -4,19 +4,30 @@ import { join } from "path";
|
|||
import { RUST_OUT_DIR } from "./_common";
|
||||
|
||||
const rsTagAttr = ({
|
||||
redundantIfEmpty,
|
||||
boolean = false,
|
||||
caseInsensitive = false,
|
||||
collapse = false,
|
||||
defaultValue,
|
||||
collapseAndTrim,
|
||||
boolean,
|
||||
redundantIfEmpty = false,
|
||||
trim = false,
|
||||
}: {
|
||||
boolean: boolean;
|
||||
redundantIfEmpty: boolean;
|
||||
collapseAndTrim: boolean;
|
||||
boolean?: boolean;
|
||||
caseInsensitive?: boolean;
|
||||
collapse?: boolean;
|
||||
defaultValue?: string;
|
||||
redundantIfEmpty?: boolean;
|
||||
trim?: boolean;
|
||||
}) =>
|
||||
`AttributeMinification { boolean: ${boolean}, redundant_if_empty: ${redundantIfEmpty}, collapse_and_trim: ${collapseAndTrim}, default_value: ${
|
||||
defaultValue == undefined ? "None" : `Some(b"${defaultValue}")`
|
||||
} }`;
|
||||
`
|
||||
AttributeMinification {
|
||||
boolean: ${boolean},
|
||||
case_insensitive: ${caseInsensitive},
|
||||
collapse: ${collapse},
|
||||
default_value: ${defaultValue == undefined ? "None" : `Some(b"${defaultValue}")`},
|
||||
redundant_if_empty: ${redundantIfEmpty},
|
||||
trim: ${trim},
|
||||
}
|
||||
`;
|
||||
|
||||
let code = `
|
||||
use lazy_static::lazy_static;
|
||||
|
@ -25,9 +36,11 @@ use crate::common::spec::tag::ns::Namespace;
|
|||
|
||||
pub struct AttributeMinification {
|
||||
pub boolean: bool,
|
||||
pub redundant_if_empty: bool,
|
||||
pub collapse_and_trim: bool,
|
||||
pub case_insensitive: bool,
|
||||
pub collapse: bool,
|
||||
pub default_value: Option<&'static [u8]>,
|
||||
pub redundant_if_empty: bool,
|
||||
pub trim: bool,
|
||||
}
|
||||
|
||||
pub enum AttrMapEntry {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@types/node": "^14.0.5",
|
||||
"@wzlin/html-data": "^2021080714.0.0",
|
||||
"ts-node": "^8.10.1",
|
||||
"typescript": "^3.7.4"
|
||||
"@types/node": "^16.4.13",
|
||||
"@wzlin/html-data": "^2021081005.0.0",
|
||||
"ts-node": "^10.2.0",
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -291,7 +291,9 @@ pub fn minify_attr(
|
|||
) -> AttrMinified {
|
||||
let attr_cfg = ATTRS.get(ns, tag, name);
|
||||
|
||||
let should_collapse_and_trim = attr_cfg.filter(|attr| attr.collapse_and_trim).is_some();
|
||||
let should_collapse = attr_cfg.filter(|attr| attr.collapse).is_some();
|
||||
let should_trim = attr_cfg.filter(|attr| attr.trim).is_some();
|
||||
let should_lowercase = attr_cfg.filter(|attr| attr.case_insensitive).is_some();
|
||||
let is_boolean = attr_cfg.filter(|attr| attr.boolean).is_some();
|
||||
// An attribute can have both redundant_if_empty and default_value, which means it has two default values: "" and default_value.
|
||||
let redundant_if_empty = attr_cfg.filter(|attr| attr.redundant_if_empty).is_some();
|
||||
|
@ -299,13 +301,15 @@ pub fn minify_attr(
|
|||
|
||||
if is_meta_viewport {
|
||||
remove_all_whitespace(&mut value_raw);
|
||||
};
|
||||
|
||||
// Trim before checking is_boolean as the entire attribute could be redundant post-minification.
|
||||
if should_collapse_and_trim {
|
||||
right_trim(&mut value_raw);
|
||||
left_trim(&mut value_raw);
|
||||
collapse_whitespace(&mut value_raw);
|
||||
} else {
|
||||
// Trim before checking is_boolean as the entire attribute could be redundant post-minification.
|
||||
if should_trim {
|
||||
right_trim(&mut value_raw);
|
||||
left_trim(&mut value_raw);
|
||||
}
|
||||
if should_collapse {
|
||||
collapse_whitespace(&mut value_raw);
|
||||
};
|
||||
};
|
||||
|
||||
#[cfg(feature = "js-esbuild")]
|
||||
|
@ -331,9 +335,13 @@ pub fn minify_attr(
|
|||
value_raw = value_raw_wrapped_min;
|
||||
}
|
||||
|
||||
// Make lowercase before checking against default value or JAVASCRIPT_MIME_TYPES.
|
||||
if should_lowercase {
|
||||
value_raw.make_ascii_lowercase();
|
||||
};
|
||||
|
||||
if (value_raw.is_empty() && redundant_if_empty)
|
||||
|| default_value.filter(|dv| dv == &value_raw).is_some()
|
||||
// TODO Cfg.
|
||||
|| (tag == b"script" && name == b"type" && JAVASCRIPT_MIME_TYPES.contains(value_raw.as_slice()))
|
||||
{
|
||||
return AttrMinified::Redundant;
|
||||
|
|
Loading…
Reference in New Issue