Skip to content

Commit 0d38b6c

Browse files
Add HTML lang attribute SEO check (#84) (#124)
Detects missing or empty lang attribute on <html> tag and reports it as a warning. Follows the same pattern as existing checks (h1, img alt, og tags). Closes #84
1 parent 61ccadb commit 0d38b6c

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

pyseoanalyzer/page.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ def get_meta_value(key):
273273
self.analyze_a_tags(soup_unmodified)
274274
self.analyze_img_tags(soup_lower)
275275
self.analyze_h1_tags(soup_lower)
276+
self.analyze_html_lang(soup_lower)
276277

277278
if self.analyze_headings:
278279
self.analyze_heading_tags(soup_unmodified)
@@ -449,6 +450,14 @@ def analyze_h1_tags(self, bs):
449450
if len(htags) == 0:
450451
self.warn("Each page should have at least one h1 tag")
451452

453+
def analyze_html_lang(self, bs):
454+
"""
455+
Make sure the HTML tag has a lang attribute
456+
"""
457+
html_tag = bs.find("html")
458+
if html_tag is not None and not html_tag.get("lang"):
459+
self.warn("Missing lang attribute on <html> tag")
460+
452461
def analyze_a_tags(self, bs):
453462
"""
454463
Add any new links (that we didn't find in the sitemap)

tests/test_page.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ def test_analyze():
3030
assert "seth" in p.title.lower()
3131

3232

33+
def test_analyze_html_lang_missing():
34+
from bs4 import BeautifulSoup
35+
36+
p = page.Page(url="https://example.com/", base_domain="https://example.com/")
37+
soup = BeautifulSoup(
38+
"<html><head><title>Test</title></head><body><p>Hello</p></body></html>",
39+
"html.parser",
40+
)
41+
p.analyze_html_lang(soup)
42+
assert len(p.warnings) == 1
43+
assert "lang" in p.warnings[0].lower()
44+
45+
46+
def test_analyze_html_lang_present():
47+
from bs4 import BeautifulSoup
48+
49+
p = page.Page(url="https://example.com/", base_domain="https://example.com/")
50+
soup = BeautifulSoup(
51+
'<html lang="en"><head><title>Test</title></head><body><p>Hello</p></body></html>',
52+
"html.parser",
53+
)
54+
p.analyze_html_lang(soup)
55+
assert len(p.warnings) == 0
56+
57+
3358
def test_analyze_with_llm():
3459
p = page.Page(
3560
url="https://www.sethserver.com/",

0 commit comments

Comments
 (0)