Skip to content

Commit 7b8853d

Browse files
authored
fix: html escape char in non html field (#514)
1 parent ebcd6e6 commit 7b8853d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

apps/commons/tests/test_process_text.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,27 @@ def test_update_project_tab_item(self):
936936
),
937937
content["content"],
938938
)
939+
940+
def test_html_escape_char(self):
941+
self.client.force_authenticate(self.user)
942+
title = "this is a & title with many & char"
943+
description = "<p>this is a & description with many & char</p>"
944+
purpose = "this is a & purpose with many & char"
945+
payload = {
946+
"title": title,
947+
"description": description,
948+
"is_locked": faker.boolean(),
949+
"is_shareable": faker.boolean(),
950+
"purpose": purpose,
951+
"organizations_codes": [self.organization.code],
952+
"images_ids": [],
953+
}
954+
response = self.client.post(reverse("Project-list"), data=payload)
955+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
956+
content = response.json()
957+
958+
self.assertEqual(content["title"], title)
959+
# description is a html field so all & is escaped
960+
description = "<p>this is a &amp; description with many &amp; char</p>"
961+
self.assertEqual(content["description"], description)
962+
self.assertEqual(content["purpose"], purpose)

apps/commons/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import gc
3+
import html
34
import io
45
import itertools
56
import uuid
@@ -69,14 +70,16 @@ def iter_img_b64(soup: BeautifulSoup):
6970
yield img
7071

7172

72-
def remove_images_text(text: str) -> str:
73+
def remove_images_text(text: str, unescape=True) -> str:
7374
"""Process rich text sent by the frontend.
7475
Some texts can contain images
7576
7677
Parameters
7778
----------
7879
text : str
7980
The text to process.
81+
escape : bool
82+
escape html entities in text
8083
8184
Returns
8285
-------
@@ -87,6 +90,9 @@ def remove_images_text(text: str) -> str:
8790

8891
for img in iter_img_b64(soup):
8992
img.decompose()
93+
94+
if unescape:
95+
return html.unescape(str(soup))
9096
return str(soup)
9197

9298

0 commit comments

Comments
 (0)