From c7bedac14f0f2e9f179b3da6bbb58ee592a612ba Mon Sep 17 00:00:00 2001 From: beeraw <3447138+beeraw@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:22:35 +0300 Subject: [PATCH] =?UTF-8?q?craft-markdown:=20an=20=20tag=20?= =?UTF-8?q?checks=20only=20the=20next=20four=20bytes=20for=20its=20=20?= =?UTF-8?q?(wordcraft#3715)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each tag lower-cased a copy of the rest of its paragraph to look for , which is quadratic on a paragraph of tags. Compare the next four bytes with eq_ignore_ascii_case instead; str::get returns None off a character boundary, so a multi-byte character there fails the match without panicking. Port of storytold/wordcraft#4263. --- crates/markdown/src/inlines.rs | 2 +- crates/markdown/src/tests.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/markdown/src/inlines.rs b/crates/markdown/src/inlines.rs index 99c3972463..25d4fe6e43 100644 --- a/crates/markdown/src/inlines.rs +++ b/crates/markdown/src/inlines.rs @@ -415,7 +415,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 96ad1dc3aa..931db94422 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\\* & ");