Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions crates/tauri-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ env_logger = "0.11"
icns = { package = "tauri-icns", version = "0.1" }
image = { version = "0.25", default-features = false, features = ["ico"] }
axum = { version = "0.8", features = ["ws"] }
html5ever = "0.29"
kuchiki = { package = "kuchikiki", version = "=0.8.8-speedreader" }
tokio = { version = "1", features = ["macros", "sync"] }
common-path = "1"
serde-value = "0.7"
Expand Down
28 changes: 5 additions & 23 deletions crates/tauri-cli/src/dev/builtin_dev_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use axum::{
http::{header, StatusCode, Uri},
response::{IntoResponse, Response},
};
use html5ever::{namespace_url, ns, LocalName, QualName};
use kuchiki::{traits::TendrilSink, NodeRef};
use std::{
net::{IpAddr, SocketAddr},
path::{Path, PathBuf},
Expand Down Expand Up @@ -128,28 +126,12 @@ async fn ws_handler(ws: WebSocketUpgrade, state: State<ServerState>) -> Response
}

fn inject_address(html_bytes: Vec<u8>, address: &SocketAddr) -> Vec<u8> {
fn with_html_head<F: FnOnce(&NodeRef)>(document: &mut NodeRef, f: F) {
if let Ok(ref node) = document.select_first("head") {
f(node.as_node())
} else {
let node = NodeRef::new_element(
QualName::new(None, ns!(html), LocalName::from("head")),
None,
);
f(&node);
document.prepend(node)
}
}
let document = tauri_utils::html::parse(String::from_utf8_lossy(&html_bytes).into_owned());

let mut document = kuchiki::parse_html()
.one(String::from_utf8_lossy(&html_bytes).into_owned())
.document_node;
with_html_head(&mut document, |head| {
let script = RELOAD_SCRIPT.replace("{{reload_url}}", &format!("ws://{address}/__tauri_cli"));
let script_el = NodeRef::new_element(QualName::new(None, ns!(html), "script".into()), None);
script_el.append(NodeRef::new_text(script));
head.prepend(script_el);
});
tauri_utils::html::append_script_to_head(
&document,
&RELOAD_SCRIPT.replace("{{reload_url}}", &format!("ws://{address}/__tauri_cli")),
);

tauri_utils::html::serialize_node(&document)
}
Expand Down
22 changes: 22 additions & 0 deletions crates/tauri-utils/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ pub fn inject_csp(document: &NodeRef, csp: &str) {
});
}

/// Injects a content security policy to the HTML.
pub fn append_script_to_head(document: &NodeRef, script: &str) {
with_head(document, |head| {
let script_el = NodeRef::new_element(QualName::new(None, ns!(html), "script".into()), None);
script_el.append(NodeRef::new_text(script));
head.prepend(script_el);
});
}

fn create_csp_meta_tag(csp: &str) -> NodeRef {
NodeRef::new_element(
QualName::new(None, ns!(html), LocalName::from("meta")),
Expand Down Expand Up @@ -344,4 +353,17 @@ mod tests {
expected.as_bytes()
)
}

#[test]
fn append_script_to_head_test() {
let html = r#"<html><head></head><body></body></html>"#;

let document = super::parse(html.to_string());
super::append_script_to_head(&document, r#"console.log('Test')"#);

assert_eq!(
String::from_utf8(super::serialize_node(&document)).unwrap(),
format!(r#"<html><head><script>console.log('Test')</script></head><body></body></html>"#)
);
}
}
Loading