feat: trim whitespace from scanned QR codes

This commit is contained in:
link2xt
2024-11-21 13:50:04 +00:00
committed by l
parent 4aad8fb3de
commit 75e1517dcc

View File

@@ -270,6 +270,7 @@ fn starts_with_ignore_case(string: &str, pattern: &str) -> bool {
/// The function should be called after a QR code is scanned. /// The function should be called after a QR code is scanned.
/// The function takes the raw text scanned and checks what can be done with it. /// The function takes the raw text scanned and checks what can be done with it.
pub async fn check_qr(context: &Context, qr: &str) -> Result<Qr> { pub async fn check_qr(context: &Context, qr: &str) -> Result<Qr> {
let qr = qr.trim();
let qrcode = if starts_with_ignore_case(qr, OPENPGP4FPR_SCHEME) { let qrcode = if starts_with_ignore_case(qr, OPENPGP4FPR_SCHEME) {
decode_openpgp(context, qr) decode_openpgp(context, qr)
.await .await
@@ -994,6 +995,17 @@ mod tests {
} }
); );
// Test that QR code whitespace is stripped.
// Users can copy-paste QR code contents and "scan"
// from the clipboard.
let qr = check_qr(&ctx.ctx, " \thttp://www.hello.com/hello \n\t \r\n ").await?;
assert_eq!(
qr,
Qr::Url {
url: "http://www.hello.com/hello".to_string(),
}
);
Ok(()) Ok(())
} }
@@ -1743,7 +1755,9 @@ mod tests {
); );
// Test URL without port. // Test URL without port.
let res = set_config_from_qr(&t, "https://t.me/socks?server=1.2.3.4").await; //
// Also check that whitespace is trimmed.
let res = set_config_from_qr(&t, " https://t.me/socks?server=1.2.3.4\n").await;
assert!(res.is_ok()); assert!(res.is_ok());
assert_eq!(t.get_config_bool(Config::ProxyEnabled).await?, true); assert_eq!(t.get_config_bool(Config::ProxyEnabled).await?, true);
assert_eq!( assert_eq!(