Make Cfg values all default false

This commit is contained in:
Wilson Lin 2021-08-07 14:56:20 +10:00
parent db7ae23404
commit 2885724931
6 changed files with 35 additions and 62 deletions

View File

@ -15,14 +15,28 @@ pub struct Cfg {
/// enabled; otherwise, this value has no effect. /// enabled; otherwise, this value has no effect.
pub minify_css: bool, pub minify_css: bool,
/// Omit closing tags when possible. /// Do not omit closing tags when possible.
pub omit_closing_tags: bool, pub keep_closing_tags: bool,
/// Remove spaces between attributes when possible (may result in invalid HTML). /// Keep spaces between attributes when possible to conform to HTML standards.
pub remove_spaces_between_attributes: bool, pub keep_spaces_between_attributes: bool,
/// Remove all comments. /// Keep all comments.
pub remove_comments: bool, pub keep_comments: bool,
/// Remove all bangs. /// Remove all bangs.
pub remove_bangs: bool, pub remove_bangs: bool,
/// Remove all processing_instructions. /// Remove all processing_instructions.
pub remove_processing_instructions: bool, pub remove_processing_instructions: bool,
} }
impl Cfg {
pub fn new() -> Cfg {
Cfg {
keep_closing_tags: false,
keep_comments: false,
keep_spaces_between_attributes: false,
minify_css: false,
minify_js: false,
remove_bangs: false,
remove_processing_instructions: false,
}
}
}

View File

@ -30,16 +30,9 @@ mod whitespace;
/// use minify_html::{Cfg, minify}; /// use minify_html::{Cfg, minify};
/// ///
/// let mut code: &[u8] = b"<p> Hello, world! </p>"; /// let mut code: &[u8] = b"<p> Hello, world! </p>";
/// let cfg = &Cfg { /// let mut cfg = Cfg::new();
/// minify_css: false, /// cfg.keep_comments = true;
/// minify_js: false, /// let minified = minify(&code, &cfg);
/// omit_closing_tags: true,
/// remove_bangs: false,
/// remove_comments: true,
/// remove_processing_instructions: false,
/// remove_spaces_between_attributes: true,
/// };
/// let minified = minify(&code, cfg);
/// assert_eq!(minified, b"<p>Hello, world!".to_vec()); /// assert_eq!(minified, b"<p>Hello, world!".to_vec());
/// ``` /// ```
pub fn minify(src: &[u8], cfg: &Cfg) -> Vec<u8> { pub fn minify(src: &[u8], cfg: &Cfg) -> Vec<u8> {

View File

@ -1,7 +1,7 @@
use crate::cfg::Cfg; use crate::cfg::Cfg;
pub fn minify_comment(cfg: &Cfg, out: &mut Vec<u8>, code: &[u8], ended: bool) { pub fn minify_comment(cfg: &Cfg, out: &mut Vec<u8>, code: &[u8], ended: bool) {
if !cfg.remove_comments { if cfg.keep_comments {
out.extend_from_slice(b"<!--"); out.extend_from_slice(b"<!--");
out.extend_from_slice(code); out.extend_from_slice(code);
if ended { if ended {

View File

@ -23,7 +23,7 @@ pub fn minify_element(
closing_tag: ElementClosingTag, closing_tag: ElementClosingTag,
children: Vec<NodeData>, children: Vec<NodeData>,
) { ) {
let can_omit_closing_tag = cfg.omit_closing_tags let can_omit_closing_tag = !cfg.keep_closing_tags
&& (can_omit_as_before(tag_name, next_sibling_as_element_tag_name) && (can_omit_as_before(tag_name, next_sibling_as_element_tag_name)
|| (is_last_child_text_or_element_node && can_omit_as_last_node(parent, tag_name))); || (is_last_child_text_or_element_node && can_omit_as_last_node(parent, tag_name)));
@ -37,7 +37,7 @@ pub fn minify_element(
if min.typ() == AttrType::Redundant { if min.typ() == AttrType::Redundant {
continue; continue;
}; };
if !cfg.remove_spaces_between_attributes || last_attr != AttrType::Quoted { if cfg.keep_spaces_between_attributes || last_attr != AttrType::Quoted {
out.push(b' '); out.push(b' ');
}; };
out.extend_from_slice(&name); out.extend_from_slice(&name);
@ -70,8 +70,7 @@ pub fn minify_element(
children, children,
); );
if closing_tag != ElementClosingTag::Present || (cfg.omit_closing_tags && can_omit_closing_tag) if closing_tag != ElementClosingTag::Present || can_omit_closing_tag {
{
return; return;
}; };
out.extend_from_slice(b"</"); out.extend_from_slice(b"</");

View File

@ -164,8 +164,7 @@ pub fn parse_element(code: &mut Code, ns: Namespace, parent: &[u8]) -> NodeData
closing_tag_omitted, closing_tag_omitted,
children, children,
} = match elem_name.as_slice() { } = match elem_name.as_slice() {
// TODO to_vec call allocates every time? b"script" => match attributes.get(b"type".as_ref()) {
b"script" => match attributes.get(&b"type".to_vec()) {
Some(mime) if !JAVASCRIPT_MIME_TYPES.contains(mime.as_slice()) => { Some(mime) if !JAVASCRIPT_MIME_TYPES.contains(mime.as_slice()) => {
parse_script_content(code, ScriptOrStyleLang::Data) parse_script_content(code, ScriptOrStyleLang::Data)
} }

View File

@ -8,53 +8,21 @@ fn _eval(src: &'static [u8], expected: &'static [u8], cfg: &super::Cfg) {
} }
fn eval(src: &'static [u8], expected: &'static [u8]) { fn eval(src: &'static [u8], expected: &'static [u8]) {
_eval( _eval(src, expected, &super::Cfg::new());
src,
expected,
&super::Cfg {
minify_css: false,
minify_js: false,
omit_closing_tags: true,
remove_bangs: false,
remove_comments: true,
remove_processing_instructions: false,
remove_spaces_between_attributes: true,
},
);
} }
#[cfg(feature = "js-esbuild")] #[cfg(feature = "js-esbuild")]
fn eval_with_js_min(src: &'static [u8], expected: &'static [u8]) -> () { fn eval_with_js_min(src: &'static [u8], expected: &'static [u8]) -> () {
_eval( let mut cfg = super::Cfg::new();
src, cfg.minify_js = true;
expected, _eval(src, expected, &cfg);
&super::Cfg {
minify_js: true,
minify_css: false,
omit_closing_tags: true,
remove_bangs: false,
remove_comments: true,
remove_processing_instructions: false,
remove_spaces_between_attributes: true,
},
);
} }
#[cfg(feature = "js-esbuild")] #[cfg(feature = "js-esbuild")]
fn eval_with_css_min(src: &'static [u8], expected: &'static [u8]) -> () { fn eval_with_css_min(src: &'static [u8], expected: &'static [u8]) -> () {
_eval( let mut cfg = super::Cfg::new();
src, cfg.minify_css = true;
expected, _eval(src, expected, &cfg);
&super::Cfg {
minify_js: false,
minify_css: true,
omit_closing_tags: true,
remove_bangs: false,
remove_comments: true,
remove_processing_instructions: false,
remove_spaces_between_attributes: true,
},
);
} }
#[test] #[test]