Skip to content

Commit

Permalink
formatter: Fix display of diff inside a placeable
Browse files Browse the repository at this point in the history
Fixes #9821
  • Loading branch information
nijel committed Sep 10, 2023
1 parent 141b953 commit 45a3bdb
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions weblate/trans/templatetags/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def parse_diff(self): # noqa: C901
formatter.parse()
self.tags[offset].append(f"<del>{formatter.format()}</del>")
elif op == self.differ.DIFF_INSERT:
end = offset + len(data)
# Rearrange space highlighting
move_space = False
start_space = -1
Expand Down Expand Up @@ -172,10 +173,46 @@ def parse_diff(self): # noqa: C901
self.tags[offset].append("<ins>")
if move_space:
self.tags[offset].append(SPACE_START)
offset += len(data)
self.tags[offset].append("</ins>")
self.tags[end].append("</ins>")
if start_space != -1:
self.tags[offset].append(SPACE_START)
self.tags[end].append(SPACE_START)

# Rearange other tags
open_tags = 0
process = False
for i in range(offset, end + 1):
remove = []
for pos, tag in enumerate(self.tags[i]):
if not process:
if tag.startswith("<ins"):
process = True
continue
if tag.startswith("</ins>"):
break
if tag.startswith("<span"):
open_tags += 1
elif tag.startswith("</span"):
if open_tags == 0:
# Remove tags spanning over <ins>
remove.append(pos)
found = None
for back in range(offset - 1, 0, -1):
for child_pos, child in reversed(
list(enumerate(self.tags[back]))
):
if child.startswith("<span"):
found = child_pos
break
if found is not None:
del self.tags[back][found]
break
else:
open_tags -= 1
# Remove closing tags (do this outside the loop)
for pos in reversed(remove):
del self.tags[i][pos]

offset = end
elif op == self.differ.DIFF_EQUAL:
offset += len(data)

Expand Down

0 comments on commit 45a3bdb

Please sign in to comment.