minify-html/java/src/main/rust/lib.rs

38 lines
1.5 KiB
Rust
Raw Normal View History

2021-08-07 05:30:46 -04:00
use minify_html::{minify as minify_html_native, Cfg};
2020-01-18 01:55:13 -05:00
use jni::JNIEnv;
2021-08-07 05:30:46 -04:00
use jni::objects::{ JClass, JObject, JString};
use jni::sys::{ jstring};
use std::str::from_utf8;
fn build_cfg(
env: &JNIEnv,
obj: &JObject,
) -> Cfg {
Cfg {
2021-08-07 05:30:46 -04:00
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(),
minify_css: env.get_field(*obj, "minify_css", "Z").unwrap().z().unwrap(),
minify_js: env.get_field(*obj, "minify_js", "Z").unwrap().z().unwrap(),
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-01-18 01:55:13 -05:00
#[no_mangle]
pub extern "system" fn Java_in_wilsonl_minifyhtml_MinifyHtml_minify(
2020-01-18 01:55:13 -05:00
env: JNIEnv,
_class: JClass,
input: JString,
cfg: JObject,
2020-01-18 01:55:13 -05:00
)
-> jstring {
let source: String = env.get_string(input).unwrap().into();
let mut code = source.into_bytes();
2020-01-18 01:55:13 -05:00
2021-08-07 05:30:46 -04:00
let out_code = minify_html_native(&mut code, &build_cfg(&env, &cfg));
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
}