Skip to content

Commit ece36c3

Browse files
committed
feat: wasm
Signed-off-by: Chang Wang <[email protected]>
1 parent 85c17f6 commit ece36c3

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# will have compiled files and executables
33
/target/
44

5+
wasm/target
6+
57
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
68
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
79
Cargo.lock

wasm/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "css-inline-wasm"
3+
version = "0.3.0"
4+
authors = ["Dmitry Dygalo <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[lib]
10+
name = "css_inline"
11+
crate-type = ["cdylib"]
12+
13+
[dependencies.css-inline]
14+
path = ".."
15+
version = "= 0.3.3"
16+
default-features = false
17+
18+
[dependencies]
19+
wasm-bindgen = "0.2"

wasm/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use css_inline as rust_inline;
2+
use wasm_bindgen::prelude::*;
3+
4+
struct InlineErrorWrapper(rust_inline::InlineError);
5+
6+
impl From<InlineErrorWrapper> for JsValue {
7+
fn from(error: InlineErrorWrapper) -> Self {
8+
JsValue::from_str(error.0.to_string().as_str())
9+
}
10+
}
11+
12+
#[wasm_bindgen]
13+
pub fn inline(
14+
html: &str,
15+
options: rust_inline::InlineOptions,
16+
) -> Result<String, JsValue> {
17+
let default_options = rust_inline::CSSInliner::default();
18+
let options = rust_inline::InlineOptions {
19+
remove_style_tags: options.remove_style_tags.unwrap_or(default_options.remove_style_tags),
20+
extra_css: options.extra_css.unwrap_or(default_options.extra_css),
21+
inline_style_tags: options.inline_style_tags.unwrap_or(default_options.inline_style_tags),
22+
base_url: options.base_url.unwrap_or(default_options.base_url),
23+
load_remote_stylesheets: options.load_remote_stylesheets.unwrap_or(default_options.load_remote_stylesheets),
24+
};
25+
let inliner = rust_inline::CSSInliner::new(options);
26+
Ok(inliner.inline(html).map_err(InlineErrorWrapper)?)
27+
}

0 commit comments

Comments
 (0)