minify-html/nodejs/native/src/lib.rs

49 lines
1.2 KiB
Rust
Raw Normal View History

2021-08-07 05:48:07 -04:00
use minify_html::{minify, Cfg};
use std::slice;
2019-12-30 02:16:33 -05:00
#[no_mangle]
2021-08-07 05:48:07 -04:00
pub extern "C" fn ffi_create_cfg(
keep_closing_tags: bool,
keep_comments: bool,
keep_html_and_head_opening_tags: bool,
keep_spaces_between_attributes: bool,
minify_css: bool,
minify_js: bool,
remove_bangs: bool,
remove_processing_instructions: bool,
) -> *const Cfg {
Box::into_raw(Box::new(Cfg {
2021-08-07 05:48:07 -04:00
keep_closing_tags,
keep_comments,
keep_html_and_head_opening_tags,
keep_spaces_between_attributes,
2021-01-07 08:26:02 -05:00
minify_css,
2021-08-07 05:48:07 -04:00
minify_js,
remove_bangs,
remove_processing_instructions,
}))
}
#[no_mangle]
2020-07-24 02:03:47 -04:00
pub extern "C" fn ffi_drop_cfg(cfg: *const Cfg) -> () {
unsafe {
2020-07-24 02:03:47 -04:00
Box::from_raw(cfg as *mut Cfg);
};
2019-12-30 02:16:33 -05:00
}
#[no_mangle]
2021-08-07 05:48:07 -04:00
pub extern "C" fn ffi_in_place(
code: *mut u8,
code_len: usize,
cfg: *const Cfg,
out_min_len: *mut usize,
) {
let code_slice = unsafe { slice::from_raw_parts_mut(code, code_len) };
2021-08-07 05:48:07 -04:00
let min_code = minify(code_slice, unsafe { &*cfg });
let min_len = min_code.len();
code_slice[..min_len].copy_from_slice(&min_code);
unsafe {
*out_min_len = min_len;
}
}