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
This commit is contained in:
Sascha Ißbrücker
2025-10-18 07:05:15 +02:00
committed by GitHub
parent dcb15f1942
commit 70734ed273
3 changed files with 27 additions and 6 deletions

View File

@@ -15,15 +15,15 @@
{% for group in tag_cloud.groups %}
<p class="group">
{% 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 %}
<a href="?{{ tag.query_string }}"
class="mr-2" data-is-tag-item>
<span
class="highlight-char">{{ tag.name|first_char }}</span><span>{{ tag.name|remaining_chars:1 }}</span>
</a>
{% else %}
{# Render remaining tags normally #}
{# Render tags normally #}
<a href="?{{ tag.query_string }}"
class="mr-2" data-is-tag-item>
<span>{{ tag.name }}</span>

View File

@@ -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'<span class="highlight-char">{tag[0]}</span>',
str(link_element),
)
else:
self.assertNotIn(
f'<span class="highlight-char">{tag[0]}</span>',
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):

View File

@@ -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)