diff --git a/crates/markdown/src/inlines.rs b/crates/markdown/src/inlines.rs index 61b3f7047c..f8facbf3f9 100644 --- a/crates/markdown/src/inlines.rs +++ b/crates/markdown/src/inlines.rs @@ -428,7 +428,7 @@ fn angle_inlines(s: &str, start: usize, base: &Fmt) -> Option<(usize, Vec) } else if matches!(lower.trim_end_matches('/').trim(), "br") { push_text(&mut nodes, "\n", base); Some((end, nodes)) - } else if lower.starts_with("a ") && rest.get(inner.len() + 1..).is_some_and(|tail| tail.to_ascii_lowercase().starts_with("")) { + } else if lower.starts_with("a ") && rest.get(inner.len() + 1..).and_then(|tail| tail.get(..4)).is_some_and(|p| p.eq_ignore_ascii_case("")) { if let Some(name) = tag_attr(inner, "id").or_else(|| tag_attr(inner, "name")) { nodes.push(Node::Inl(Inline::Anchor(name))); } diff --git a/crates/markdown/src/tests.rs b/crates/markdown/src/tests.rs index bad3ea57e6..427ef35c1a 100644 --- a/crates/markdown/src/tests.rs +++ b/crates/markdown/src/tests.rs @@ -21,6 +21,29 @@ fn emphasis() { assert!(matches!(&s[0], Inline::Text(t, f) if t == "gone" && f.strike)); } +/// An empty `` or `` is an anchor whatever the case of its `` (wordcraft#3715). +#[test] +fn empty_html_anchor() { + let anchors = |s: &str| inl(s).into_iter().filter_map(|i| if let Inline::Anchor(a) = i { Some(a) } else { None }).collect::>(); + assert_eq!(anchors(r#"x y"#), vec!["n"]); + assert_eq!(anchors(r#"x y"#), vec!["m"]); + assert_eq!(anchors(r#""#), vec!["p"]); + assert!(anchors(r#"text"#).is_empty()); + assert!(anchors(r#"` tag lower-cased a copy of the rest of its paragraph to look for `` +/// (wordcraft#3715): quadratic on a paragraph of tags. +#[test] +fn a_paragraph_of_anchor_tags_parses_in_linear_time() { + let src = "".repeat(1_000_000); + let t = std::time::Instant::now(); + inl(&src); + // Well under a second now; the per-tag copy took 40 s in a release build. + assert!(t.elapsed() < std::time::Duration::from_secs(10), "{:?}", t.elapsed()); +} + #[test] fn links_code_escapes() { let s = inl("see [the *site*](http://x.org \"T\") and `a*b*` \\*lit\\* & ");