From fae0437e498fa47f4377305ab57cd4c2b24761b5 Mon Sep 17 00:00:00 2001 From: abdo Date: Wed, 17 Feb 2021 03:52:23 +0300 Subject: [PATCH] Fix tag replacement matching substrings https://github.com/ankitects/anki/issues/1027 --- rslib/src/tags.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/rslib/src/tags.rs b/rslib/src/tags.rs index 2a91013b9..7513fecac 100644 --- a/rslib/src/tags.rs +++ b/rslib/src/tags.rs @@ -334,7 +334,7 @@ impl Collection { let tags = split_tags(tags) .map(|tag| { let tag = if regex { tag.into() } else { to_re(tag) }; - Regex::new(&format!("(?i)^{}(::.*)?", tag)) + Regex::new(&format!("(?i)^{}(::.*)?$", tag)) .map_err(|_| AnkiError::invalid_input("invalid regex")) }) .collect::>>()?; @@ -566,6 +566,14 @@ mod test { let note = col.storage.get_note(note.id)?.unwrap(); assert_eq!(¬e.tags, &["foo::bar", "foo::bar::bar", "foo::bar::foo",]); + // ensure replacements fully match + let mut note = col.storage.get_note(note.id)?.unwrap(); + note.tags = vec!["foobar".into(), "barfoo".into(), "foo".into()]; + col.update_note(&mut note)?; + col.replace_tags_for_notes(&[note.id], "foo", "", false)?; + let note = col.storage.get_note(note.id)?.unwrap(); + assert_eq!(¬e.tags, &["barfoo", "foobar"]); + // tag children are also cleared when clearing their parent col.storage.clear_tags()?; for name in vec!["a", "a::b", "A::b::c"] {