Skip to content

Commit

Permalink
feat: highlight leading space in diff
Browse files Browse the repository at this point in the history
This makes it easier to see what was changed in the string.

Fixes #7516
  • Loading branch information
nijel committed Feb 12, 2025
1 parent b9213bc commit cda5dd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 1 addition & 2 deletions weblate/trans/templatetags/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,12 @@ def format_generator(self) -> Generator[str]:

if pos in tags:
current = tags[pos]
# Special case for single whitespace char in diff
# Special case for leading whitespace char in diff
if (
current
and value[pos] == " "
and "<ins>" in current
and SPACE_START not in current
and "</ins>" in tags[pos + 1]
):
current.append(SPACE_START)
tags[pos + 1].insert(0, SPACE_END)
Expand Down
12 changes: 10 additions & 2 deletions weblate/trans/tests/test_suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ def test_change_diff_for_deleted_suggestion(self, **kwargs) -> None:
response = self.client.get(translate_url)
self.assertNotContains(response, "Suggestion removed")
# 1st diff occurrence is in the "Suggestions" tab, the 2nd in "History" tab
self.assertContains(response, "Nazdar<ins> svete</ins>!", count=2)
self.assertContains(
response,
"""Nazdar<ins><span class="hlspace"><span class="space-space"> </span></span>svete</ins>!""",
count=2,
)

suggestions = self.get_unit().suggestions.values_list("pk", flat=True)
self.assertEqual(len(suggestions), 1)
Expand All @@ -119,7 +123,11 @@ def test_change_diff_for_deleted_suggestion(self, **kwargs) -> None:
self.assertContains(response, "Suggestion added", count=1)
# both diff occurrence are in "History" tab,
# as suggestion is no longer visible in the "Suggestion" tab
self.assertContains(response, "Nazdar<ins> svete</ins>!", count=2)
self.assertContains(
response,
"""Nazdar<ins><span class="hlspace"><span class="space-space"> </span></span>svete</ins>!""",
count=2,
)

def test_delete_spam(self) -> None:
self.test_delete(spam="1")
Expand Down
14 changes: 14 additions & 0 deletions weblate/trans/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,20 @@ def test_diff_changed_whitespace(self) -> None:
""",
)

def test_diff_whitespace_leading_added(self) -> None:
self.assertHTMLEqual(
format_translation(
["新增 :http:get:"],
self.component.source_language,
diff="新增:http:get:",
)["items"][0]["content"],
"""新增
<del>:</del>
<ins><span class="hlspace"><span class="space-space"> </span></span>:</ins>
http:get:
""",
)

def test_glossary(self) -> None:
self.assertHTMLEqual(
format_translation(
Expand Down

0 comments on commit cda5dd4

Please sign in to comment.