Skip to content

Commit

Permalink
Check for duplicate id attributes in HTML
Browse files Browse the repository at this point in the history
It’s invalid HTML to have multiple elements with the same `id`
attribute. Plus it can cause trouble for Javascripts, and make anchor
linking unpredictable.
  • Loading branch information
quis committed Dec 11, 2024
1 parent 7e64c04 commit e3c4941
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,8 @@ def get_url(
if _test_for_non_smart_quotes:
ClientRequest.test_for_non_smart_quotes(page)

ClientRequest._test_for_duplicate_ids(page)

return page

@staticmethod
Expand Down Expand Up @@ -3149,6 +3151,14 @@ def test_for_non_smart_quotes(page):
"'" in el.text or '"' in el.text
), f"Non-smart quote or apostrophe found in <{el.name}>: {normalize_spaces(el.text)}"

@staticmethod
def _test_for_duplicate_ids(page):
ids = [element["id"] for element in page.select("*[id]")]
for id in ids:
assert ids.count(id) == 1, f"Duplicate id `{id}` found on these elements:\n " + ", ".join(
f"<{element.name}>" for element in page.select(f"*[id='{id}']")
)

return ClientRequest


Expand Down

0 comments on commit e3c4941

Please sign in to comment.