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.
pub minify_css: bool,
/// Omit closing tags when possible.
pub omit_closing_tags: bool,
/// Remove spaces between attributes when possible (may result in invalid HTML).
pub remove_spaces_between_attributes: bool,
/// Remove all comments.
pub remove_comments: bool,
/// Do not omit closing tags when possible.
pub keep_closing_tags: bool,
/// Keep spaces between attributes when possible to conform to HTML standards.
pub keep_spaces_between_attributes: bool,
/// Keep all comments.
pub keep_comments: bool,
/// Remove all bangs.
pub remove_bangs: bool,
/// Remove all processing_instructions.
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};
///
/// let mut code: &[u8] = b"<p> Hello, world! </p>";
/// let cfg = &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,
/// };
/// let minified = minify(&code, cfg);
/// let mut cfg = Cfg::new();
/// cfg.keep_comments = true;
/// let minified = minify(&code, &cfg);
/// assert_eq!(minified, b"<p>Hello, world!".to_vec());
/// ```
pub fn minify(src: &[u8], cfg: &Cfg) -> Vec<u8> {

View File

@ -1,7 +1,7 @@
use crate::cfg::Cfg;
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(code);
if ended {

View File

@ -23,7 +23,7 @@ pub fn minify_element(
closing_tag: ElementClosingTag,
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)
|| (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 {
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.extend_from_slice(&name);
@ -70,8 +70,7 @@ pub fn minify_element(
children,
);
if closing_tag != ElementClosingTag::Present || (cfg.omit_closing_tags && can_omit_closing_tag)
{
if closing_tag != ElementClosingTag::Present || can_omit_closing_tag {
return;
};
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,
children,
} = match elem_name.as_slice() {
// TODO to_vec call allocates every time?
b"script" => match attributes.get(&b"type".to_vec()) {
b"script" => match attributes.get(b"type".as_ref()) {
Some(mime) if !JAVASCRIPT_MIME_TYPES.contains(mime.as_slice()) => {
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]) {
_eval(
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,
},
);
_eval(src, expected, &super::Cfg::new());
}
#[cfg(feature = "js-esbuild")]
fn eval_with_js_min(src: &'static [u8], expected: &'static [u8]) -> () {
_eval(
src,
expected,
&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,
},
);
let mut cfg = super::Cfg::new();
cfg.minify_js = true;
_eval(src, expected, &cfg);
}
#[cfg(feature = "js-esbuild")]
fn eval_with_css_min(src: &'static [u8], expected: &'static [u8]) -> () {
_eval(
src,
expected,
&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,
},
);
let mut cfg = super::Cfg::new();
cfg.minify_css = true;
_eval(src, expected, &cfg);
}
#[test]