Skip to content

Commit

Permalink
fix(core) Remove data url example
Browse files Browse the repository at this point in the history
  • Loading branch information
huangmingg committed Feb 13, 2024
1 parent b14ebac commit f91ab78
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 155 deletions.
20 changes: 10 additions & 10 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3248,24 +3248,24 @@ fn create_webview<T: UserEvent>(
}

let mut webview_builder =
WebViewBuilder::new(window).map_err(|e| Error::CreateWebview(Box::new(e)))?;
WebViewBuilder::new(window).map_err(|e| Error::CreateWebview(Box::new(e)))?;

// use with_html method if html content can be extracted from url.
// else defaults to with_url method
webview_builder = if let Some(html_string) = tauri_utils::html::extract_html_content(&url) {
webview_builder
.with_html(html_string)
.map_err(|e| Error::CreateWebview(Box::new(e)))?
webview_builder
.with_html(html_string)
.map_err(|e| Error::CreateWebview(Box::new(e)))?
} else {
webview_builder
.with_url(&url)
.map_err(|e| Error::CreateWebview(Box::new(e)))?
webview_builder
.with_url(&url)
.map_err(|e| Error::CreateWebview(Box::new(e)))?
};

webview_builder = webview_builder
.with_focused(focused)
.with_transparent(is_window_transparent)
.with_accept_first_mouse(webview_attributes.accept_first_mouse);
.with_focused(focused)
.with_transparent(is_window_transparent)
.with_accept_first_mouse(webview_attributes.accept_first_mouse);

if webview_attributes.file_drop_handler_enabled {
webview_builder = webview_builder
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub enum WindowUrl {
impl fmt::Display for WindowUrl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::External(url)=> write!(f, "{url}"),
Self::External(url) => write!(f, "{url}"),
#[cfg(feature = "window-data-url")]
Self::DataUrl(url) => write!(f, "{url}"),
Self::App(path) => write!(f, "{}", path.display()),
Expand Down
4 changes: 2 additions & 2 deletions core/tauri-utils/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ pub fn is_html(data_string: &str) -> bool {
/// Temporary naive method to extract data from html data string
pub fn extract_html_content(input: &str) -> Option<&str> {
if input.starts_with("data:text/html,") {
Some(&input[15..])
Some(&input[15..])
} else {
None
None
}
}

Expand Down
5 changes: 0 additions & 5 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,3 @@ path = "../../examples/streaming/main.rs"
name = "isolation"
path = "../../examples/isolation/main.rs"
required-features = [ "isolation" ]

[[example]]
name = "data-url"
path = "../../examples/data-url/main.rs"
required-features = [ "window-data-url" ]
56 changes: 34 additions & 22 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,34 +1007,46 @@ impl<R: Runtime> WindowManager<R> {
));
}

match (url.scheme(), tauri_utils::html::extract_html_content(url.as_str())) {
match (
url.scheme(),
tauri_utils::html::extract_html_content(url.as_str()),
) {
#[cfg(feature = "window-data-url")]
("data", Some(html_string)) => {
// There is an issue with the external DataUrl where HTML containing special characters
// are not correctly processed. A workaround is to first percent encode the html string,
// before it processed by DataUrl.
let encoded_string = urlencoding::encode(html_string);
let url = data_url::DataUrl::process(&format!("data:text/html,{}", encoded_string))
.map_err(|_| crate::Error::InvalidWindowUrl("Failed to process data url"))
.and_then(|data_url| data_url.decode_to_vec().map_err(|_| crate::Error::InvalidWindowUrl("Failed to decode processed data url")))
.and_then(|(body, _)| {
let html = String::from_utf8_lossy(&body).into_owned();
let mut document = tauri_utils::html::parse(html);
if let Some(csp) = self.csp() {
tauri_utils::html::inject_csp(&mut document, &csp.to_string());
}
// decode back to raw html, as the content should be fully decoded
// when passing to wry / tauri-runtime-wry, which will be responsible
// for handling the encoding based on the OS.
let encoded_html = document.to_string();
Ok(percent_encoding::percent_decode_str(encoded_html.as_str()).decode_utf8_lossy().to_string())
}).unwrap_or(html_string.to_string());
pending.url = format!("data:text/html,{}", url);
// There is an issue with the external DataUrl where HTML containing special characters
// are not correctly processed. A workaround is to first percent encode the html string,
// before it processed by DataUrl.
let encoded_string = urlencoding::encode(html_string);
let url = data_url::DataUrl::process(&format!("data:text/html,{}", encoded_string))
.map_err(|_| crate::Error::InvalidWindowUrl("Failed to process data url"))
.and_then(|data_url| {
data_url
.decode_to_vec()
.map_err(|_| crate::Error::InvalidWindowUrl("Failed to decode processed data url"))
})
.and_then(|(body, _)| {
let html = String::from_utf8_lossy(&body).into_owned();
let mut document = tauri_utils::html::parse(html);
if let Some(csp) = self.csp() {
tauri_utils::html::inject_csp(&mut document, &csp.to_string());
}
// decode back to raw html, as the content should be fully decoded
// when passing to wry / tauri-runtime-wry, which will be responsible
// for handling the encoding based on the OS.
let encoded_html = document.to_string();
Ok(
percent_encoding::percent_decode_str(encoded_html.as_str())
.decode_utf8_lossy()
.to_string(),
)
})
.unwrap_or(html_string.to_string());
pending.url = format!("data:text/html,{}", url);
}
_ => {
pending.url = url.to_string();
}
};
};

if !pending.window_builder.has_icon() {
if let Some(default_window_icon) = self.inner.default_window_icon.clone() {
Expand Down
3 changes: 0 additions & 3 deletions examples/data-url/README.md

This file was deleted.

11 changes: 0 additions & 11 deletions examples/data-url/index.html

This file was deleted.

46 changes: 0 additions & 46 deletions examples/data-url/main.rs

This file was deleted.

55 changes: 0 additions & 55 deletions examples/data-url/tauri.conf.json

This file was deleted.

0 comments on commit f91ab78

Please sign in to comment.