Files
claude-seo/tests/test_parse_html_rel_case.py
Agrici DanielandClaude Fable 5.1 28bf1248f5 fix(parse-html): detect rel="canonical"/"alternate" case-insensitively
PR #269 was a documentation-only change (an added code fence) against a
defect that does not exist as described, so it was not cherry-picked. But
investigating it surfaced a real one: parse_html.py finds the canonical
link and hreflang alternates with soup.find("link", rel="canonical") and
soup.find_all("link", rel="alternate"), which bs4 matches by exact,
lower-case string equality against the parsed rel value.

The underlying HTML parser lower-cases tag and attribute *names*, so
REL="..." is read as the rel attribute either way, but it preserves the
attribute *value* verbatim. A CMS or theme emitting rel="Alternate" or
REL="Canonical" therefore had its canonical link and every hreflang
alternate silently dropped, with no error or warning.

Add _has_rel_token(), a small case-insensitive membership check over bs4's
multi-valued rel list, and use it in place of the two exact-match finds.

Adds tests.test_parse_html_rel_case, covering the existing lower-case
baseline, mixed-case rel values on both canonical and alternate links,
an uppercase REL attribute name, attribute order, and self-closing tags.

Credits the report in #269 in CHANGELOG.md.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015qjDbqj88SjLGY4MdQKGB8
2026-09-10 20:48:25 +03:00

96 lines
3.5 KiB
Python

"""
Tests for case-insensitive `rel` detection in scripts/parse_html.py.
Background: `soup.find("link", rel="canonical")` and
`soup.find_all("link", rel="alternate")` compare bs4's parsed `rel` values
with an exact, lower-case string. The underlying HTML parser lower-cases tag
and attribute *names* (so `REL="..."` is read as the `rel` attribute), but it
preserves the attribute *value* verbatim, so `rel="Alternate"` or
`REL="Canonical"` never matched and both the canonical link and every
hreflang alternate using mixed-case `rel` were silently dropped.
"""
import sys
from pathlib import Path
# Make scripts/ importable without requiring it to be a package
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT / "scripts"))
from parse_html import parse_html # noqa: E402
def test_lowercase_rel_canonical_and_alternate_still_work():
"""Baseline: the existing lower-case forms are unaffected by the fix."""
html = (
"<html><head>"
'<link rel="canonical" href="https://example.com/page">'
'<link rel="alternate" hreflang="en-US" href="https://example.com/en">'
"</head></html>"
)
result = parse_html(html)
assert result["canonical"] == "https://example.com/page"
assert result["hreflang"] == [{"lang": "en-US", "href": "https://example.com/en"}]
def test_titlecase_rel_alternate_is_detected():
html = (
"<html><head>"
'<link href="https://example.com/en" hreflang="en-US" rel="Alternate">'
"</head></html>"
)
result = parse_html(html)
assert result["hreflang"] == [{"lang": "en-US", "href": "https://example.com/en"}]
def test_uppercase_rel_attribute_name_and_titlecase_canonical_is_detected():
html = '<html><head><link REL="Canonical" href="https://example.com/page"></head></html>'
result = parse_html(html)
assert result["canonical"] == "https://example.com/page"
def test_uppercase_rel_value_alternate_is_detected():
html = (
"<html><head>"
'<link rel="ALTERNATE" hreflang="fr" href="https://example.com/fr">'
"</head></html>"
)
result = parse_html(html)
assert result["hreflang"] == [{"lang": "fr", "href": "https://example.com/fr"}]
def test_mixed_case_rel_attribute_order_does_not_matter():
"""`rel` before or after `hreflang`/`href` must both be detected."""
html = (
"<html><head>"
'<link hreflang="de" href="https://example.com/de" rel="Alternate">'
'<link rel="Alternate" hreflang="es" href="https://example.com/es">'
"</head></html>"
)
result = parse_html(html)
assert {"lang": "de", "href": "https://example.com/de"} in result["hreflang"]
assert {"lang": "es", "href": "https://example.com/es"} in result["hreflang"]
def test_self_closing_mixed_case_link_tags_are_detected():
html = (
"<html><head>"
'<link rel="Canonical" href="https://example.com/page" />'
'<link rel="Alternate" hreflang="en" href="https://example.com/en" />'
"</head></html>"
)
result = parse_html(html)
assert result["canonical"] == "https://example.com/page"
assert result["hreflang"] == [{"lang": "en", "href": "https://example.com/en"}]
def test_lowercase_rel_still_ignores_unrelated_link_types():
html = (
"<html><head>"
'<link rel="stylesheet" href="https://example.com/style.css">'
'<link rel="Preload" href="https://example.com/font.woff2">'
"</head></html>"
)
result = parse_html(html)
assert result["canonical"] is None
assert result["hreflang"] == []