From 8f4f9041c60991457ccb1b3f145676d90e28aa8c Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 12 Nov 2026 23:03:23 +0100 Subject: refactor(receiver): simplify HTML mention selectors Store complete CSS selectors alongside their target attributes so HTML mention detection can query supported references directly without rebuilding selectors. --- webmentions_ssg/tasks/mentions.py | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/webmentions_ssg/tasks/mentions.py b/webmentions_ssg/tasks/mentions.py index 42dd0b6..687ba38 100644 --- a/webmentions_ssg/tasks/mentions.py +++ b/webmentions_ssg/tasks/mentions.py @@ -12,22 +12,18 @@ from bs4 import BeautifulSoup IRI_PATTERN = rfc3987.get_compiled_pattern("IRI") TRAILING_IRI_PUNCTUATION = ".,;:!?" -HTML_URL_ATTRIBUTES = { - "href": {"a", "area", "link"}, - "src": { - "audio", - "embed", - "iframe", - "img", - 'input[type="image" i]', - "script", - "audio source", - "video source", - "track", - "video", - }, - "cite": {"blockquote", "del", "ins", "q"}, -} +HTML_URL_ATTRIBUTES = ( + ("a[href], area[href], link[href]", "href"), + ( + ( + "audio[src], embed[src], iframe[src], img[src], " + 'input[type="image" i][src], script[src], ' + "audio source[src], video source[src], track[src], video[src]" + ), + "src", + ), + ("blockquote[cite], del[cite], ins[cite], q[cite]", "cite"), +) def html_mentions_target(body: bytes, source_url: str, target_url: str) -> bool: @@ -49,11 +45,7 @@ def html_mentions_target(body: bytes, source_url: str, target_url: str) -> bool: ): base_url = urljoin(source_url, base_href.strip()) - for attribute, selectors in HTML_URL_ATTRIBUTES.items(): - selector = ", ".join( - [f"{selector_string}[{attribute}]" for selector_string in selectors] - ) - + for selector, attribute in HTML_URL_ATTRIBUTES: for element in document.select(selector): if not isinstance(reference := element.get(attribute), str): continue -- cgit v1.3.1