From 4f6a1c6bcc4a609b6d27b8d2a4f309dd25b40079 Mon Sep 17 00:00:00 2001 From: Phil Lord Date: Tue, 21 Jun 2022 18:08:16 +0100 Subject: [PATCH] Properly escape anchor names (#1908) --- components/utils/src/anchors.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/components/utils/src/anchors.rs b/components/utils/src/anchors.rs index 06af56b4..fe86e960 100644 --- a/components/utils/src/anchors.rs +++ b/components/utils/src/anchors.rs @@ -1,4 +1,5 @@ use libs::regex::Regex; +use libs::regex::escape; pub fn has_anchor_id(content: &str, anchor: &str) -> bool { let checks = anchor_id_checks(anchor); @@ -6,7 +7,8 @@ pub fn has_anchor_id(content: &str, anchor: &str) -> bool { } fn anchor_id_checks(anchor: &str) -> Regex { - Regex::new(&format!(r#"\s(?i)(id|name) *= *("|')*{}("|'| |>)+"#, anchor)).unwrap() + Regex::new(&format!(r#"\s(?i)(id|name) *= *("|')*{}("|'| |>)+"#, + escape(anchor))).unwrap() } #[cfg(test)] @@ -44,7 +46,11 @@ mod tests { assert!(m(r#""#)); + // Escaped Anchors + assert!(check("fred?george", r#""#)); + assert!(check("fred.george", r#""#)); + // Non matchers - assert!(!m(r#""#)) + assert!(!m(r#""#)); } }