Html content containing special characters are not properly encoded when creating a new webview window using data url.
Reproduction
let html = r#"
<html>
<body>
special character incoming ###
thai characters incoming สระ
</body>
</html>
"#;
let data = format!("data:text/html,{}", html);
let _docs_window = tauri::WindowBuilder::new(
&handle,
"myWindow",
tauri::WindowUrl::External(data.parse().unwrap())
)
.build()
.unwrap();
does not work, while reading from an external file
let html = r#"
<html>
<body>
special character incoming ###
thai characters incoming สระ
</body>
</html>
"#;
std::fs::write(path.clone(), html).expect("Unable to write file");
let data = format!("data:text/html,{}", html);
let _docs_window = tauri::WindowBuilder::new(
&handle,
"myWindow",
tauri::WindowUrl::App("external.html".parse().unwrap())
)
.build()
.unwrap();
let html = r#"
<html>
<body>
special character incoming ###
thai characters incoming สระ
</body>
</html>
"#;
let data = format!("data:text/html,{}", html);
let result = data_url::DataUrl::process(&data).unwrap();
let (body, _) = result.decode_to_vec().unwrap();
let res = String::from_utf8(body).unwrap();
Will produce
<html> <body> special character incoming
As a temporary workaround, we can percent encode the HTML on tauri level, before running the process function - that seems to work
let html = r#"
<html>
<body>
special character incoming ###
thai characters incoming สระ
</body>
</html>
"#;
let html = urlencoding::encode(&html);
let data = format!("data:text/html,{}", html);
let result = data_url::DataUrl::process(&data).unwrap();
let (body, _) = result.decode_to_vec().unwrap();
let res = String::from_utf8(body).unwrap();
Will produce back the same html
`
special character incoming ###
thai characters incoming สระ
</html>`
url.to_string() / to_path() converts the html into percent encoded ASCII string, which in turn is passed to the unified webview of WRY, that does encoding again on webview layer depending on the OS. As a result, the html is encoded twice on OS like windows,
https://github.com/tauri-apps/wry/blob/dev/src/webview2/mod.rs#L842
On the tauri-runtime-wry layer, its using .with_url(&url) even for data url with html content. There is another method .with_html(&html) in wry we can make use of, for data url containing html content.
Describe the bug
Html content containing special characters are not properly encoded when creating a new webview window using data url.
Reproduction
does not work, while reading from an external file
works
Expected behavior
No response
Full
tauri info
outputStack trace
No response
Additional context
After some initial investigation, and help from @i-c-b on Discord, I have identified some problems
Will produce
<html> <body> special character incoming
As a temporary workaround, we can percent encode the HTML on tauri level, before running the process function - that seems to work
Will produce back the same html `
special character incoming ### thai characters incoming สระurl.to_string() / to_path() converts the html into percent encoded ASCII string, which in turn is passed to the unified webview of WRY, that does encoding again on webview layer depending on the OS. As a result, the html is encoded twice on OS like windows, https://github.com/tauri-apps/wry/blob/dev/src/webview2/mod.rs#L842
On the tauri-runtime-wry layer, its using
.with_url(&url)
even for data url with html content. There is another method.with_html(&html)
in wry we can make use of, for data url containing html content.