From 70734ed273f34df401f82bbdf1a5b9298129e77e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Sat, 18 Oct 2025 07:05:15 +0200 Subject: [PATCH] Fix tag cloud highlighting first char when tags are not grouped (#1209) * Fix tag cloud highlighting first char when tags are not grouped * update test --- bookmarks/templates/bookmarks/tag_cloud.html | 6 +++--- bookmarks/tests/test_tag_cloud_template.py | 20 +++++++++++++++++++- bookmarks/views/contexts.py | 7 +++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/bookmarks/templates/bookmarks/tag_cloud.html b/bookmarks/templates/bookmarks/tag_cloud.html index cbf414a..93687d4 100644 --- a/bookmarks/templates/bookmarks/tag_cloud.html +++ b/bookmarks/templates/bookmarks/tag_cloud.html @@ -15,15 +15,15 @@ {% for group in tag_cloud.groups %}

{% for tag in group.tags %} - {# Highlight first char of first tag in group #} - {% if forloop.counter == 1 %} + {# Highlight first char of first tag in group if grouping is enabled #} + {% if group.highlight_first_char and forloop.counter == 1 %} {{ tag.name|first_char }}{{ tag.name|remaining_chars:1 }} {% else %} - {# Render remaining tags normally #} + {# Render tags normally #} {{ tag.name }} diff --git a/bookmarks/tests/test_tag_cloud_template.py b/bookmarks/tests/test_tag_cloud_template.py index 9b401ec..ad734d6 100644 --- a/bookmarks/tests/test_tag_cloud_template.py +++ b/bookmarks/tests/test_tag_cloud_template.py @@ -32,7 +32,12 @@ class TagCloudTemplateTest(TestCase, BookmarkFactoryMixin, HtmlTestMixin): template_to_render = Template("{% include 'bookmarks/tag_cloud.html' %}") return template_to_render.render(context) - def assertTagGroups(self, rendered_template: str, groups: List[List[str]]): + def assertTagGroups( + self, + rendered_template: str, + groups: List[List[str]], + highlight_first_char: bool = True, + ): soup = self.make_soup(rendered_template) group_elements = soup.select("p.group") @@ -48,6 +53,18 @@ class TagCloudTemplateTest(TestCase, BookmarkFactoryMixin, HtmlTestMixin): link_element = link_elements[tag_index] self.assertEqual(link_element.text.strip(), tag) + if tag_index == 0: + if highlight_first_char: + self.assertIn( + f'{tag[0]}', + str(link_element), + ) + else: + self.assertNotIn( + f'{tag[0]}', + str(link_element), + ) + def assertNumSelectedTags(self, rendered_template: str, count: int): soup = self.make_soup(rendered_template) link_elements = soup.select("p.selected-tags a") @@ -178,6 +195,7 @@ class TagCloudTemplateTest(TestCase, BookmarkFactoryMixin, HtmlTestMixin): "Coyote", ], ], + False, ) def test_no_duplicate_tag_names(self): diff --git a/bookmarks/views/contexts.py b/bookmarks/views/contexts.py index df2c30c..84b80b9 100644 --- a/bookmarks/views/contexts.py +++ b/bookmarks/views/contexts.py @@ -383,10 +383,13 @@ class RemoveTagItem: class TagGroup: - def __init__(self, context: RequestContext, char: str): + def __init__( + self, context: RequestContext, char: str, highlight_first_char: bool = True + ): self.context = context self.tags = [] self.char = char + self.highlight_first_char = highlight_first_char def __repr__(self): return f"<{self.char} TagGroup>" @@ -436,7 +439,7 @@ class TagGroup: return [] sorted_tags = sorted(tags, key=lambda x: str.lower(x.name)) - group = TagGroup(context, "Ungrouped") + group = TagGroup(context, "Ungrouped", highlight_first_char=False) for tag in sorted_tags: group.add_tag(tag)