Skip to content

Commit 8b836f7

Browse files
cliffhallclaude
andcommitted
fix(fetch): keep literal and escaped angle brackets as visible text
Copilot round 3 on #5398: the scanner dropped any <...> run, so a heading 'Comparing x < 5 and y > 3' did not match its own title and a duplicate heading was added. As in CommonMark, '<' opens a tag only before a letter, '/', '!' or '?', and a backslash-escaped character is text. Issue: #2472 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent eabad3f commit 8b836f7

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/fetch/src/mcp_server_fetch/title.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,38 @@ def _normalized(text: str) -> str:
3939
return f" {' '.join(words)} " if words else ""
4040

4141

42+
def _starts_tag(char: str) -> bool:
43+
return char.isascii() and (char.isalpha() or char in "/!?")
44+
45+
4246
def _visible(line: str) -> str:
4347
"""The text a reader sees on one Markdown line.
4448
4549
A link's destination (`](...)`) and an HTML tag (an autolink's URL
4650
included) are dropped; a code span's contents are kept, tag-like text
47-
and all. One pass, so a long line with an unterminated `](`, `<` or
51+
and all. As in CommonMark, a `<` starts a tag only before a letter, `/`,
52+
`!` or `?` (so "x < 5 and y > 3" is text), and a backslash-escaped
53+
character is text. One pass, so a long line with an unterminated `](`, `<` or
4854
backtick costs linear time: each closer is searched for forward from
4955
where it is needed, and once a search for one fails it is never repeated.
5056
"""
5157
out: list[str] = []
5258
missing: set[str] = set()
5359
i, end = 0, len(line)
5460
while i < end:
61+
if line[i] == "\\" and i + 1 < end:
62+
out.append(line[i + 1])
63+
i += 2
64+
continue
5565
for opener, closer, keep in (
5666
("`", "`", True),
5767
("<", ">", False),
5868
("](", ")", False),
5969
):
6070
if closer in missing or not line.startswith(opener, i):
6171
continue
72+
if opener == "<" and not _starts_tag(line[i + 1 : i + 2]):
73+
continue
6274
j = line.find(closer, i + len(opener))
6375
if j == -1:
6476
missing.add(closer)

‎src/fetch/tests/test_page_title.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,20 @@ def test_code_span_contents_count_as_visible() -> None:
183183
)
184184

185185

186-
@pytest.mark.parametrize("opener", ["](", "<", "`"])
186+
@pytest.mark.parametrize(
187+
("title_html", "content"),
188+
[
189+
# A "<" before a space is text, not a tag.
190+
("Comparing x &lt; 5 and y &gt; 3", "# Comparing x < 5 and y > 3\n\nBody."),
191+
# Backslash-escaped brackets are text too.
192+
("Using &lt;T&gt;", "# Using \\<T\\>\n\nBody."),
193+
],
194+
)
195+
def test_literal_angle_brackets_count_as_visible(title_html: str, content: str) -> None:
196+
assert with_page_title(f"<title>{title_html}</title>", content) == content
197+
198+
199+
@pytest.mark.parametrize("opener", ["](", "<a", "`", "\\"])
187200
def test_unterminated_markup_on_a_long_first_line(opener: str) -> None:
188201
# Linear in the line's length: a quadratic scan of 200,000 openers would
189202
# not finish in the test's time. The title is not on the line, so it is added.

0 commit comments

Comments
 (0)