2021-08-09 05:56:37 -04:00
|
|
|
use jni::objects::{JClass, JObject, JString};
|
|
|
|
use jni::sys::jstring;
|
2020-01-18 01:55:13 -05:00
|
|
|
use jni::JNIEnv;
|
2021-08-09 05:56:37 -04:00
|
|
|
use minify_html::{minify as minify_html_native, Cfg};
|
2021-08-07 05:30:46 -04:00
|
|
|
use std::str::from_utf8;
|
2020-08-25 23:15:32 -04:00
|
|
|
|
2021-08-09 05:56:37 -04:00
|
|
|
fn build_cfg(env: &JNIEnv, obj: &JObject) -> Cfg {
|
2020-07-10 11:15:56 -04:00
|
|
|
Cfg {
|
2021-08-09 05:56:37 -04:00
|
|
|
ensure_spec_compliant_unquoted_attribute_values: env
|
|
|
|
.get_field(*obj, "ensure_spec_compliant_unquoted_attribute_values", "Z")
|
|
|
|
.unwrap()
|
|
|
|
.z()
|
|
|
|
.unwrap(),
|
|
|
|
keep_closing_tags: env
|
|
|
|
.get_field(*obj, "keep_closing_tags", "Z")
|
|
|
|
.unwrap()
|
|
|
|
.z()
|
|
|
|
.unwrap(),
|
|
|
|
keep_comments: env
|
|
|
|
.get_field(*obj, "keep_comments", "Z")
|
|
|
|
.unwrap()
|
|
|
|
.z()
|
|
|
|
.unwrap(),
|
|
|
|
keep_html_and_head_opening_tags: env
|
|
|
|
.get_field(*obj, "keep_html_and_head_opening_tags", "Z")
|
|
|
|
.unwrap()
|
|
|
|
.z()
|
|
|
|
.unwrap(),
|
|
|
|
keep_spaces_between_attributes: env
|
|
|
|
.get_field(*obj, "keep_spaces_between_attributes", "Z")
|
|
|
|
.unwrap()
|
|
|
|
.z()
|
|
|
|
.unwrap(),
|
2021-08-07 05:30:46 -04:00
|
|
|
minify_css: env.get_field(*obj, "minify_css", "Z").unwrap().z().unwrap(),
|
|
|
|
minify_js: env.get_field(*obj, "minify_js", "Z").unwrap().z().unwrap(),
|
2021-08-09 05:56:37 -04:00
|
|
|
remove_bangs: env
|
|
|
|
.get_field(*obj, "remove_bangs", "Z")
|
|
|
|
.unwrap()
|
|
|
|
.z()
|
|
|
|
.unwrap(),
|
|
|
|
remove_processing_instructions: env
|
|
|
|
.get_field(*obj, "remove_processing_instructions", "Z")
|
|
|
|
.unwrap()
|
|
|
|
.z()
|
|
|
|
.unwrap(),
|
2020-07-10 11:15:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 01:55:13 -05:00
|
|
|
#[no_mangle]
|
2020-07-11 11:44:18 -04:00
|
|
|
pub extern "system" fn Java_in_wilsonl_minifyhtml_MinifyHtml_minify(
|
2020-01-18 01:55:13 -05:00
|
|
|
env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
input: JString,
|
2020-07-10 11:15:56 -04:00
|
|
|
cfg: JObject,
|
2021-08-09 05:56:37 -04:00
|
|
|
) -> jstring {
|
2020-01-18 01:55:13 -05:00
|
|
|
let source: String = env.get_string(input).unwrap().into();
|
2021-08-07 05:48:07 -04:00
|
|
|
let code = source.into_bytes();
|
2020-01-18 01:55:13 -05:00
|
|
|
|
2021-08-07 05:48:07 -04:00
|
|
|
let out_code = minify_html_native(&code, &build_cfg(&env, &cfg));
|
2021-08-07 05:30:46 -04:00
|
|
|
let out_code_str = from_utf8(&out_code).unwrap();
|
|
|
|
env.new_string(out_code_str).unwrap().into_inner()
|
2020-01-18 01:55:13 -05:00
|
|
|
}
|