diff --git a/news/288.bugfix b/news/288.bugfix new file mode 100644 index 00000000..2a88b713 --- /dev/null +++ b/news/288.bugfix @@ -0,0 +1 @@ +getVocabulary: Fix for terms with incomplete HTML diff --git a/plone/app/content/browser/vocabulary.py b/plone/app/content/browser/vocabulary.py index 7419e80c..4b707cfb 100644 --- a/plone/app/content/browser/vocabulary.py +++ b/plone/app/content/browser/vocabulary.py @@ -261,8 +261,10 @@ 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 ] @@ -270,9 +272,7 @@ def __call__(self): 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, diff --git a/plone/app/content/tests/test_widgets.py b/plone/app/content/tests/test_widgets.py index 95e662dd..1a37c7aa 100644 --- a/plone/app/content/tests/test_widgets.py +++ b/plone/app/content/tests/test_widgets.py @@ -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} ", + value=f"term {idx} ", + title=f"term {idx} ", + ) + 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 ") + class FunctionalBrowserTest(unittest.TestCase): layer = PLONE_APP_CONTENT_DX_FUNCTIONAL_TESTING