Skip to content

Commit

Permalink
bugfix: escape special characters in URL prefixes used in a regex (#301)
Browse files Browse the repository at this point in the history
* escape any special characters in a URL prefix before turning into a regex

* fix issue w/ app menu not showing up correctly in macos
  • Loading branch information
a5huynh committed Feb 6, 2023
1 parent dd33f1b commit bad9ac2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
13 changes: 10 additions & 3 deletions crates/shared/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ pub fn regex_for_domain(domain: &str) -> String {

pub fn regex_for_prefix(prefix: &str) -> String {
if prefix.ends_with('$') {
return format!("^{prefix}");
let escaped = regex::escape(prefix.strip_suffix('$').unwrap());
format!("^{escaped}$")
} else {
let escaped = regex::escape(prefix);
format!("^{escaped}.*")
}

format!("^{prefix}.*")
}

/// Convert a robots.txt rule into a proper regex string
Expand Down Expand Up @@ -133,5 +135,10 @@ mod test {
] {
assert!(!regex.is_match(test));
}

let prefix = "https://en.wikipedia.org/wiki/\'($";
let regex = Regex::new(&regex_for_prefix(prefix)).unwrap();
dbg!(&regex);
assert!(regex.is_match("https://en.wikipedia.org/wiki/\'("));
}
}
2 changes: 2 additions & 0 deletions crates/spyglass/src/api/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,8 @@ mod test {
.await
.expect("Unable to find indexed docs");
assert_eq!(indexed.len(), 0);
// Add a small delay so that the documents can be properly committed
std::thread::sleep(std::time::Duration::from_millis(500));
assert_eq!(state.index.reader.searcher().num_docs(), 0);
}
}
17 changes: 10 additions & 7 deletions crates/tauri/src/menu.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

use shared::config::Config;
use strum_macros::{Display, EnumString};
use tauri::{
utils::assets::EmbeddedAssets, Context, CustomMenuItem, Menu, MenuItem, Submenu,
utils::assets::EmbeddedAssets, Context, CustomMenuItem, Menu,
SystemTrayMenu, SystemTrayMenuItem, SystemTraySubmenu,
};
#[cfg(not(target_os = "linux"))]
use tauri::{MenuItem, Submenu};

#[derive(Display, Debug, EnumString)]
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
Expand Down Expand Up @@ -95,16 +98,16 @@ pub fn get_tray_menu(ctx: &Context<EmbeddedAssets>, config: &Config) -> SystemTr
.add_item(quit)
}

pub fn get_app_menu(ctx: &Context<EmbeddedAssets>) -> Menu {
if cfg!(target_os = "linux") {
return Menu::new();
}
pub fn get_app_menu(_ctx: &Context<EmbeddedAssets>) -> Menu {
#[cfg(target_os = "linux")]
return Menu::new();

#[cfg(not(target_os = "linux"))]
Menu::new().add_submenu(Submenu::new(
&ctx.package_info().name,
&_ctx.package_info().name,
Menu::new()
.add_native_item(MenuItem::About(
ctx.package_info().name.to_string(),
_ctx.package_info().name.to_string(),
Default::default(),
))
// Currently we need to include these so that the shortcuts for these
Expand Down

0 comments on commit bad9ac2

Please sign in to comment.