diff --git a/agent/scripts/build.ps1 b/agent/scripts/build.ps1 index 97b1102..ff0ff88 100644 --- a/agent/scripts/build.ps1 +++ b/agent/scripts/build.ps1 @@ -1,4 +1,4 @@ -#requires -Version 5.1 +#requires -Version 5.1 <# MD Preview 开发构建(DSH workspace-write / Low 完整性可运行) - cargo test + cargo build --release @@ -72,7 +72,9 @@ $hash = (Get-FileHash $built -Algorithm SHA256).Hash Step 'check removed update strings' $bytes = [IO.File]::ReadAllBytes($built) $ascii = [Text.Encoding]::ASCII.GetString($bytes) -$forbidden = @('api.github', 'btn-update', 'Check for Updates', 'powershell', 'update-check-result', 'open-url') +# Guard the removed updater. 'powershell' used to be on this list, but that word +# can legitimately show up in user-facing text; the updater itself needs these. +$forbidden = @('api.github', 'btn-update', 'Check for Updates', 'Start-Process', 'Invoke-WebRequest', 'update-check-result', 'open-url') $bad = @() foreach ($needle in $forbidden) { $count = ([regex]::Matches($ascii, [regex]::Escape($needle), 'IgnoreCase')).Count diff --git a/src/main.rs b/src/main.rs index 4c84baa..74c3336 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,9 @@ struct SelfWriteRecord { content: String, } +/// Menu-only events exist for macOS; on Windows the page drives the same +/// actions over IPC, so those variants are never constructed here. +#[cfg_attr(not(target_os = "macos"), allow(dead_code))] #[derive(Debug)] enum UserEvent { NewFile(WindowId), @@ -322,7 +325,7 @@ fn confirm_discard_unsaved(error: &str) -> bool { rfd::MessageDialog::new() .set_level(rfd::MessageLevel::Warning) .set_title("Could Not Save") - .set_description(&format!( + .set_description(format!( "{error}\n\nThe file may be read-only or locked by another program. Close the window and discard the unsaved changes?" )) .set_buttons(rfd::MessageButtons::OkCancel) @@ -610,17 +613,20 @@ fn image_mime_type(path: &Path) -> Option<&'static str> { fn base64_encode(bytes: &[u8]) -> String { const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - let mut out = String::with_capacity(((bytes.len() + 2) / 3) * 4); - let mut chunks = bytes.chunks_exact(3); - for chunk in &mut chunks { - let n = ((chunk[0] as u32) << 16) | ((chunk[1] as u32) << 8) | chunk[2] as u32; + let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4); + let mut index = 0; + while index + 3 <= bytes.len() { + let n = ((bytes[index] as u32) << 16) + | ((bytes[index + 1] as u32) << 8) + | bytes[index + 2] as u32; out.push(TABLE[((n >> 18) & 0x3f) as usize] as char); out.push(TABLE[((n >> 12) & 0x3f) as usize] as char); out.push(TABLE[((n >> 6) & 0x3f) as usize] as char); out.push(TABLE[(n & 0x3f) as usize] as char); + index += 3; } - match chunks.remainder() { + match &bytes[index..] { [a] => { let n = (*a as u32) << 16; out.push(TABLE[((n >> 18) & 0x3f) as usize] as char); @@ -2797,6 +2803,15 @@ mod tests { assert!(html.contains("
")); } + #[test] + fn base64_matches_known_vectors() { + assert_eq!(base64_encode(b""), ""); + assert_eq!(base64_encode(b"f"), "Zg=="); + assert_eq!(base64_encode(b"fo"), "Zm8="); + assert_eq!(base64_encode(b"foo"), "Zm9v"); + assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy"); + } + #[test] fn only_markdown_extensions_are_supported_documents() { for name in ["note.md", "note.MARKDOWN", "note.mdown", "note.mkd"] { @@ -4334,6 +4349,7 @@ fn build_startup_page(strings: &Strings) -> String { ) } +#[allow(clippy::too_many_arguments)] // shared window plumbing; split with the webview module fn build_webview( window: &Window, web_context: &mut WebContext, @@ -4365,10 +4381,8 @@ fn build_webview( false, )); false - } else if url.starts_with("file:") { - false } else { - true + !url.starts_with("file:") } }) .with_ipc_handler(move |msg| { @@ -5509,9 +5523,11 @@ fn main() { window_id, .. } => { - if app.windows.contains_key(&window_id) { - close_window(&mut app, window_id, control_flow); + // The window may already be gone (closed by CloseRequested). + if !app.windows.contains_key(&window_id) { + return; } + close_window(&mut app, window_id, control_flow); } _ => {} }