From c42b7e961f1db6bad85c338f69c840ea7ad5d5ea Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 24 Feb 2026 15:22:24 +1100 Subject: [PATCH] refactor: use `html` module from `tauri-utils` --- Cargo.lock | 2 -- crates/tauri-cli/Cargo.toml | 2 -- .../tauri-cli/src/dev/builtin_dev_server.rs | 28 ++++--------------- crates/tauri-utils/src/html.rs | 22 +++++++++++++++ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b199ae40008..394d6b0228d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8654,7 +8654,6 @@ dependencies = [ "glob", "handlebars", "heck 0.5.0", - "html5ever", "ignore", "image", "include_dir", @@ -8667,7 +8666,6 @@ dependencies = [ "jsonrpsee-core", "jsonrpsee-ws-client", "jsonschema", - "kuchikiki", "libc", "local-ip-address", "log", diff --git a/crates/tauri-cli/Cargo.toml b/crates/tauri-cli/Cargo.toml index 25cef598ea34..a69c30e3c11b 100644 --- a/crates/tauri-cli/Cargo.toml +++ b/crates/tauri-cli/Cargo.toml @@ -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" diff --git a/crates/tauri-cli/src/dev/builtin_dev_server.rs b/crates/tauri-cli/src/dev/builtin_dev_server.rs index 1b49058ed224..0492bf408d8c 100644 --- a/crates/tauri-cli/src/dev/builtin_dev_server.rs +++ b/crates/tauri-cli/src/dev/builtin_dev_server.rs @@ -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}, @@ -128,28 +126,12 @@ async fn ws_handler(ws: WebSocketUpgrade, state: State) -> Response } fn inject_address(html_bytes: Vec, address: &SocketAddr) -> Vec { - fn with_html_head(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) } diff --git a/crates/tauri-utils/src/html.rs b/crates/tauri-utils/src/html.rs index 958ce597832f..2336d393a0dc 100644 --- a/crates/tauri-utils/src/html.rs +++ b/crates/tauri-utils/src/html.rs @@ -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")), @@ -344,4 +353,17 @@ mod tests { expected.as_bytes() ) } + + #[test] + fn append_script_to_head_test() { + let html = r#""#; + + 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#""#) + ); + } }