-
-
Notifications
You must be signed in to change notification settings - Fork 31
initial attempt poc at wasm #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Stranger6667
merged 7 commits into
Stranger6667:master
from
cheapsteak:chang/20/07/attempt-at-wasm
Jul 13, 2020
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a668887
feat: wasm
cheapsteak 6b99172
refactor: accept InlineOptions as JsValue
cheapsteak ed95670
refactor: merge imports from std
cheapsteak e3d7204
refactor: move gitignore into wasm crate
cheapsteak d81df50
refactor: code cleanup
cheapsteak f19f92f
build: add wasm release optimizations
cheapsteak 8ed2cfb
feat: explicitply provide typescript typings
cheapsteak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "css-inline-wasm" | ||
version = "0.3.0" | ||
authors = ["Dmitry Dygalo <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
name = "css_inline" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies.css-inline] | ||
path = ".." | ||
version = "= 0.3.3" | ||
default-features = false | ||
|
||
[dependencies] | ||
url = "2" | ||
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_derive = "1" | ||
serde_json = "1" | ||
Stranger6667 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use css_inline as rust_inline; | ||
use wasm_bindgen::prelude::*; | ||
use std::borrow::Cow; | ||
use std::convert::TryFrom; | ||
use std::convert::TryInto; | ||
Stranger6667 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
struct InlineErrorWrapper(rust_inline::InlineError); | ||
|
||
impl From<InlineErrorWrapper> for JsValue { | ||
fn from(error: InlineErrorWrapper) -> Self { | ||
JsValue::from_str(error.0.to_string().as_str()) | ||
} | ||
} | ||
|
||
struct UrlError(url::ParseError); | ||
|
||
impl From<UrlError> for JsValue { | ||
fn from(error: UrlError) -> Self { | ||
wasm_bindgen::throw_str(&error.0.to_string()) | ||
Stranger6667 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
fn parse_url(url: Option<String>) -> Result<Option<url::Url>, JsValue> { | ||
Ok(if let Some(url) = url { | ||
Some(url::Url::parse(url.as_str()).map_err(UrlError)?) | ||
} else { | ||
None | ||
}) | ||
} | ||
|
||
#[macro_use] | ||
extern crate serde_derive; | ||
|
||
#[derive(Deserialize)] | ||
#[serde(default)] | ||
pub struct Options { | ||
cheapsteak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inline_style_tags: bool, | ||
remove_style_tags: bool, | ||
base_url: Option<String>, | ||
load_remote_stylesheets: bool, | ||
extra_css: Option<String>, | ||
} | ||
|
||
impl Default for Options { | ||
fn default() -> Self { | ||
Options { | ||
inline_style_tags: true, | ||
remove_style_tags: false, | ||
base_url: None, | ||
load_remote_stylesheets: true, | ||
extra_css: None, | ||
} | ||
} | ||
} | ||
|
||
struct SerdeError(serde_json::Error); | ||
|
||
impl From<SerdeError> for JsValue { | ||
fn from(error: SerdeError) -> Self { | ||
JsValue::from_str(error.0.to_string().as_str()) | ||
} | ||
} | ||
|
||
impl TryFrom<Options> for rust_inline::InlineOptions<'_> { | ||
type Error = JsValue; | ||
|
||
fn try_from(value: Options) -> Result<Self, Self::Error> { | ||
Ok(rust_inline::InlineOptions { | ||
inline_style_tags: value.inline_style_tags, | ||
remove_style_tags: value.remove_style_tags, | ||
base_url: parse_url(value.base_url)?, | ||
load_remote_stylesheets: value.load_remote_stylesheets, | ||
extra_css: value.extra_css.map(Cow::Owned), | ||
}) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn inline( | ||
html: &str, | ||
options: JsValue, | ||
Stranger6667 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Result<String, JsValue> { | ||
let options: Options = if !options.is_undefined() { | ||
options.into_serde().map_err(SerdeError)? | ||
} else { | ||
Options::default() | ||
}; | ||
let inliner = rust_inline::CSSInliner::new(options.try_into()?); | ||
Ok(inliner.inline(html).map_err(InlineErrorWrapper)?) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.