Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

description_plaintext blocks are improperly concatenated #7600

Merged
merged 5 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 72 additions & 3 deletions saleor/core/tests/test_editorjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)


Expand Down Expand Up @@ -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"
)
14 changes: 7 additions & 7 deletions saleor/core/utils/editorjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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:
Expand All @@ -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):
Expand Down