Skip to content

Saleor Core: the high performance, composable, headless commerce API.

description_plaintext blocks are improperly concatenated (#7600)

Dalton804ebc5b2bd5
diff --git a/saleor/core/tests/test_editorjs.py b/saleor/core/tests/test_editorjs.py
index b2450486a5..0d9ec8e7fa 100644
--- a/saleor/core/tests/test_editorjs.py
+++ b/saleor/core/tests/test_editorjs.py
@@ -141,10 +141,10 @@ def test_clean_editor_js_for_list():
assert result == strip_tags(
"The Saleor Winter Sale is snowed "
'<a href="https://docs.saleor.io/docs/">. Test.'
- "It is a block-styled editor "
+ " It is a block-styled editor "
'<a href="https://docs.saleor.io/docs/">.'
- "It returns clean data output in JSON"
- "Designed to be extendable and pluggable with a simple API"
+ " It returns clean data output in JSON"
+ " Designed to be extendable and pluggable with a simple API"
)
@@ -197,3 +197,72 @@ def test_clean_editor_js_for_list_invalid_url(parse_url_mock):
assert (
result["blocks"][1]["data"]["items"][2] == data["blocks"][1]["data"]["items"][2]
)
+
+
+def test_clean_editor_js_for_complex_description():
+ # given
+ data = {
+ "blocks": [
+ {
+ "data": {
+ "text": "The Saleor Winter Sale is snowed"
+ '<a href="https://docs.saleor.io/docs/">. Test.'
+ },
+ "type": "paragraph",
+ },
+ {
+ "data": {
+ "text": "The one thing you be sure of is: Polish winters are quite"
+ " unpredictable. The coldest months are January and February"
+ " with temperatures around -3.0 °C (on average), but the"
+ " weather might change from mild days with over 5 °C and"
+ " days where temperatures may drop to −20 °C (−4 °F)."
+ },
+ "type": "paragraph",
+ },
+ {
+ "type": "list",
+ "data": {
+ "style": "ordered",
+ "items": [
+ "Bring your coat",
+ "warm clothes",
+ ],
+ },
+ },
+ {
+ "type": "list",
+ "data": {
+ "style": "unordered",
+ "items": [
+ "test item",
+ "item test",
+ ],
+ },
+ },
+ ]
+ }
+
+ # when
+ result = clean_editor_js(data)
+
+ # then
+ assert result == data
+
+ # when
+ result = clean_editor_js(data, to_string=True)
+
+ # then
+ assert result == strip_tags(
+ "The Saleor Winter Sale is snowed"
+ '<a href="https://docs.saleor.io/docs/">. Test.'
+ " The one thing you be sure of is: Polish winters are quite"
+ " unpredictable. The coldest months are January and February"
+ " with temperatures around -3.0 °C (on average), but the"
+ " weather might change from mild days with over 5 °C and"
+ " days where temperatures may drop to −20 °C (−4 °F)."
+ " Bring your coat"
+ " warm clothes"
+ " test item"
+ " item test"
+ )
diff --git a/saleor/core/utils/editorjs.py b/saleor/core/utils/editorjs.py
index d10dedef20..bcfce0dbf7 100644
--- a/saleor/core/utils/editorjs.py
+++ b/saleor/core/utils/editorjs.py
@@ -17,15 +17,15 @@ def clean_editor_js(definitions: Optional[Dict], *, to_string: bool = False):
`to_string` flag is used for returning concatenated string from all blocks
instead of returning json object.
"""
- string = ""
-
if definitions is None:
- return string if to_string else definitions
+ return "" if to_string else definitions
blocks = definitions.get("blocks")
if not blocks or not isinstance(blocks, list):
- return string if to_string else definitions
+ return "" if to_string else definitions
+
+ plain_text_list = []
for index, block in enumerate(blocks):
block_type = block["type"]
@@ -39,7 +39,7 @@ def clean_editor_js(definitions: Optional[Dict], *, to_string: bool = False):
continue
new_text = clean_text_data(item)
if to_string:
- string += strip_tags(new_text)
+ plain_text_list.append(strip_tags(new_text))
else:
blocks[index]["data"]["items"][item_index] = new_text
else:
@@ -48,11 +48,11 @@ def clean_editor_js(definitions: Optional[Dict], *, to_string: bool = False):
continue
new_text = clean_text_data(text)
if to_string:
- string += strip_tags(new_text)
+ plain_text_list.append(strip_tags(new_text))
else:
blocks[index]["data"]["text"] = new_text
- return string if to_string else definitions
+ return " ".join(plain_text_list) if to_string else definitions
def clean_text_data(text: str):