Skip to content

Commit

Permalink
Properly escape anchor names (#1908)
Browse files Browse the repository at this point in the history
  • Loading branch information
phillord authored Jun 21, 2022
1 parent afc6a71 commit 4f6a1c6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions components/utils/src/anchors.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use libs::regex::Regex;
use libs::regex::escape;

pub fn has_anchor_id(content: &str, anchor: &str) -> bool {
let checks = anchor_id_checks(anchor);
checks.is_match(content)
}

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)]
Expand Down Expand Up @@ -44,7 +46,11 @@ mod tests {
assert!(m(r#"<a
id="fred">"#));

// Escaped Anchors
assert!(check("fred?george", r#"<a id="fred?george">"#));
assert!(check("fred.george", r#"<a id="fred.george">"#));

// Non matchers
assert!(!m(r#"<a notid="fred">"#))
assert!(!m(r#"<a notid="fred">"#));
}
}

0 comments on commit 4f6a1c6

Please sign in to comment.