GuillaumeGomez / minifier-rs

Minifier tool/lib for JS/CSS/JSON files
MIT License
86 stars 16 forks source link

Panicking when minifying some JS text #57

Closed magiclen closed 4 years ago

magiclen commented 4 years ago

I've found this crate will cause panic when minifying some JS text (not sure what exactly causes the panic, and maybe it only happens after version 0.0.32). Here I use https://code.jquery.com/jquery-3.4.1.slim.min.js for example.

To reproduce,

Cargo.toml

...

[dependencies]
minifier = "0.0.35"
easy-http-request = "0.2.5"

main.rs

extern crate easy_http_request;

use easy_http_request::StaticHttpRequest;
use minifier::js;

fn main() {
    println!("Downloading jquery-3.4.1.slim.js");

    let response = StaticHttpRequest::get_from_url_str("https://code.jquery.com/jquery-3.4.1.slim.js").unwrap().send().unwrap();

    let minified_js = js::minify(&String::from_utf8(response.body).unwrap()); // OK

    println!("{}", minified_js);

    println!("Downloading jquery-3.4.1.slim.min.js");

    let response = StaticHttpRequest::get_from_url_str("https://code.jquery.com/jquery-3.4.1.slim.min.js").unwrap().send().unwrap();

    let minified_js = js::minify(&String::from_utf8(response.body).unwrap()); // Panic

    println!("{}", minified_js);
}
magiclen commented 4 years ago

I found the following JS texts which could cause panic,

0.0.35

console.log(/MSIE|Trident|Edge/.test(window.navigator.userAgent));

>= 0.0.32 <=0.0.34

function (i, n, a) { i[n].type.replace(/ *;(.|\s)*/,"")===t&&a.push(i[n].MathJax.elementJax);return a}

However, the code below can be minified without panicking, (only add a space character after ===)

function (i, n, a) { i[n].type.replace(/ *;(.|\s)*/,"")=== t&&a.push(i[n].MathJax.elementJax);return a}
GuillaumeGomez commented 4 years ago

Thanks for the detailed report! I'll try to fix it in the next days.