Skip to content

Commit

Permalink
Merge pull request #288 from plone/getVocabulary-incomplete-html
Browse files Browse the repository at this point in the history
getVocabulary: Call scrub_html on individual items
  • Loading branch information
mauritsvanrees authored Aug 8, 2024
2 parents 4806eb6 + 6c1e1f2 commit 0cebad3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions news/288.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
getVocabulary: Fix for terms with incomplete HTML
10 changes: 5 additions & 5 deletions plone/app/content/browser/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,18 @@ def __call__(self):
else:
items = [
{
"id": item.value,
"text": (item.title if item.title else ""),
"id": unescape(transform.scrub_html(item.value)),
"text": (
unescape(transform.scrub_html(item.title)) if item.title else ""
),
}
for item in results
]

if total == 0:
total = len(items)

return unescape(
transform.scrub_html(json_dumps({"results": items, "total": total}))
)
return json_dumps({"results": items, "total": total})

def parsed_query(
self,
Expand Down
23 changes: 23 additions & 0 deletions plone/app/content/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,29 @@ def testGetMimeIcon(self):
[{"getMimeIcon": "/plone/++resource++mimetype.icons/unknown.png"}],
)

def testGeneratesValidJson(self):
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary

view = VocabularyView(self.portal, self.request)
vocab = SimpleVocabulary(
[
SimpleTerm(
token=f"term {idx} <b>",
value=f"term {idx} <b>",
title=f"term {idx} <b>",
)
for idx in range(3)
]
)
with mock.patch.object(view, "get_vocabulary", return_value=vocab):
result = view()
# The above values could result in invalid json if there is an error in
# the code: the following call would give a json.decoder.JSONDecodeError.
# See https://github.com/plone/plone.app.content/pull/288
parsed = json.loads(result)
self.assertEqual(parsed["results"][0]["text"], "term 0 <b></b>")


class FunctionalBrowserTest(unittest.TestCase):
layer = PLONE_APP_CONTENT_DX_FUNCTIONAL_TESTING
Expand Down

0 comments on commit 0cebad3

Please sign in to comment.