Skip to content

Commit

Permalink
fix(core): Clippy fixes. (#7869)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
martinfrances107 and lucasfernog authored Oct 18, 2023
1 parent c1ec0f1 commit 8db26bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
13 changes: 6 additions & 7 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ fn map_core_assets(
if path.extension() == Some(OsStr::new("html")) {
#[allow(clippy::collapsible_if)]
if csp {
let mut document = parse_html(String::from_utf8_lossy(input).into_owned());
let document = parse_html(String::from_utf8_lossy(input).into_owned());

if target == Target::Linux {
::tauri_utils::html::inject_csp_token(&mut document);
::tauri_utils::html::inject_csp_token(&document);
}

inject_nonce_token(&mut document, &dangerous_disable_asset_csp_modification);
inject_nonce_token(&document, &dangerous_disable_asset_csp_modification);

if dangerous_disable_asset_csp_modification.can_modify("script-src") {
if let Ok(inline_script_elements) = document.select("script:not(empty)") {
Expand Down Expand Up @@ -97,14 +97,13 @@ fn map_isolation(
) -> impl Fn(&AssetKey, &Path, &mut Vec<u8>, &mut CspHashes) -> Result<(), EmbeddedAssetsError> {
move |_key, path, input, _csp_hashes| {
if path.extension() == Some(OsStr::new("html")) {
let mut isolation_html =
tauri_utils::html::parse(String::from_utf8_lossy(input).into_owned());
let isolation_html = tauri_utils::html::parse(String::from_utf8_lossy(input).into_owned());

// this is appended, so no need to reverse order it
tauri_utils::html::inject_codegen_isolation_script(&mut isolation_html);
tauri_utils::html::inject_codegen_isolation_script(&isolation_html);

// temporary workaround for windows not loading assets
tauri_utils::html::inline_isolation(&mut isolation_html, &dir);
tauri_utils::html::inline_isolation(&isolation_html, &dir);

*input = isolation_html.to_string().as_bytes().to_vec()
}
Expand Down
18 changes: 9 additions & 9 deletions core/tauri-utils/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn parse(html: String) -> NodeRef {
kuchiki::parse_html().one(html)
}

fn with_head<F: FnOnce(&NodeRef)>(document: &mut NodeRef, f: F) {
fn with_head<F: FnOnce(&NodeRef)>(document: &NodeRef, f: F) {
if let Ok(ref node) = document.select_first("head") {
f(node.as_node())
} else {
Expand All @@ -132,7 +132,7 @@ fn with_head<F: FnOnce(&NodeRef)>(document: &mut NodeRef, f: F) {
}
}

fn inject_nonce(document: &mut NodeRef, selector: &str, token: &str) {
fn inject_nonce(document: &NodeRef, selector: &str, token: &str) {
if let Ok(scripts) = document.select(selector) {
for target in scripts {
let node = target.as_node();
Expand All @@ -150,7 +150,7 @@ fn inject_nonce(document: &mut NodeRef, selector: &str, token: &str) {

/// Inject nonce tokens to all scripts and styles.
pub fn inject_nonce_token(
document: &mut NodeRef,
document: &NodeRef,
dangerous_disable_asset_csp_modification: &DisabledCspModificationKind,
) {
if dangerous_disable_asset_csp_modification.can_modify("script-src") {
Expand All @@ -162,14 +162,14 @@ pub fn inject_nonce_token(
}

/// Injects a content security policy to the HTML.
pub fn inject_csp(document: &mut NodeRef, csp: &str) {
pub fn inject_csp(document: &NodeRef, csp: &str) {
with_head(document, |head| {
head.append(create_csp_meta_tag(csp));
});
}

/// Injects a content security policy token to the HTML.
pub fn inject_csp_token(document: &mut NodeRef) {
pub fn inject_csp_token(document: &NodeRef) {
inject_csp(document, CSP_TOKEN)
}

Expand Down Expand Up @@ -239,7 +239,7 @@ impl Default for IsolationSide {
///
/// Note: This function is not considered part of the stable API.
#[cfg(feature = "isolation")]
pub fn inject_codegen_isolation_script(document: &mut NodeRef) {
pub fn inject_codegen_isolation_script(document: &NodeRef) {
with_head(document, |head| {
let script = NodeRef::new_element(QualName::new(None, ns!(html), "script".into()), None);
script.append(NodeRef::new_text(
Expand All @@ -257,7 +257,7 @@ pub fn inject_codegen_isolation_script(document: &mut NodeRef) {
///
/// Note: this does not prevent path traversal due to the isolation application expectation that it
/// is secure.
pub fn inline_isolation(document: &mut NodeRef, dir: &Path) {
pub fn inline_isolation(document: &NodeRef, dir: &Path) {
for script in document
.select("script[src]")
.expect("unable to parse document for scripts")
Expand Down Expand Up @@ -297,8 +297,8 @@ mod tests {
"<html></html>".to_string(),
];
for html in htmls {
let mut document = kuchiki::parse_html().one(html);
super::inject_csp_token(&mut document);
let document = kuchiki::parse_html().one(html);
super::inject_csp_token(&document);
assert_eq!(
document.to_string(),
format!(
Expand Down

0 comments on commit 8db26bd

Please sign in to comment.