From f759080fc740822a2e93624ad561899b5bcafa27 Mon Sep 17 00:00:00 2001 From: irfanuddinahmad Date: Wed, 11 Nov 2026 20:18:02 +0500 Subject: [PATCH 1/2] fix: render empty Annotatable blocks instead of raising XMLSyntaxError A bare , which is what Content Libraries v2 creates for a new Annotatable component, is stored with empty `data` by definition_from_xml. get_html() and _render_content() then called etree.fromstring("") and failed with "XMLSyntaxError: Document is empty", so the library preview returned HTTP 500 until content was saved. The built-in block rendered such blocks as empty content. Parse through a helper that treats empty data as an empty element. Co-Authored-By: Claude Opus 5.5 --- src/xblock_annotatable/block.py | 17 +++++++++++-- .../tests/test_annotatable.py | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/xblock_annotatable/block.py b/src/xblock_annotatable/block.py index 5c7331bc..b4736142 100644 --- a/src/xblock_annotatable/block.py +++ b/src/xblock_annotatable/block.py @@ -150,10 +150,23 @@ def _render_annotation(self, index, el): delete_key = value["_delete"] del el.attrib[delete_key] + def _get_xmltree(self): + """ + Parse ``self.data`` into an element tree. + + A bare ```` (e.g. a block just created in a content library) is + stored with empty ``data`` by ``definition_from_xml``. Treat that as an empty + annotatable instead of failing with ``XMLSyntaxError: Document is empty``, + matching the built-in block, which rendered such blocks as empty content. + """ + if not (self.data or "").strip(): + return etree.Element("annotatable") + return etree.fromstring(self.data) + def _render_content(self): """Renders annotatable content with annotation spans and returns HTML.""" - xmltree = etree.fromstring(self.data) + xmltree = self._get_xmltree() self._extract_instructions(xmltree) xmltree.tag = "div" @@ -181,7 +194,7 @@ def get_html(self): return { "element_id": uuid.uuid1(0), "display_name": self.display_name, - "instructions_html": self._extract_instructions(etree.fromstring(self.data)), + "instructions_html": self._extract_instructions(self._get_xmltree()), "content_html": self._render_content(), } diff --git a/src/xblock_annotatable/tests/test_annotatable.py b/src/xblock_annotatable/tests/test_annotatable.py index 64f6a4f4..c1c49192 100644 --- a/src/xblock_annotatable/tests/test_annotatable.py +++ b/src/xblock_annotatable/tests/test_annotatable.py @@ -161,6 +161,31 @@ def test_submit_studio_edits(self): self.assertEqual(self.annotatable.display_name, "New Display Name") self.assertEqual(self.annotatable.data, self.sample_xml) + def _block_with_data(self, data): + runtime = TestRuntime() + scope_ids = ScopeIds("user_id", "block_type", "block_id", "context_key") + return AnnotatableBlock(runtime, DictFieldData({"data": data}), scope_ids) + + def test_get_html_with_empty_data(self): + """ + A block with empty ``data`` (e.g. a bare ```` just created in a + content library) renders as empty content instead of raising XMLSyntaxError. + """ + for data in ("", " \n"): + block = self._block_with_data(data) + context = block.get_html() + assert context["instructions_html"] is None + assert context["content_html"] == "
" + + def test_bare_element_from_xml_renders(self): + """ + ``definition_from_xml`` maps a bare ```` to empty ``data``; that + block must still render (the content-library "create new component" flow). + """ + definition, _children = AnnotatableBlock.definition_from_xml(etree.fromstring(""), None) + block = self._block_with_data(definition["data"]) + assert block.get_html()["content_html"] == "
" + class AnnotatableBlockI18nJsCatalogTest(unittest.TestCase): """Tests for loading the block's namespaced JavaScript translation catalog.""" From a72fdd3cc1cbe106e2b52a691d16a862824340be Mon Sep 17 00:00:00 2001 From: irfanuddinahmad Date: Mon, 23 Nov 2026 12:14:00 +0500 Subject: [PATCH 2/2] fix: store bare as OLX, like the built-in block The pointer-tag support in #118 gave Annotatable the EmptyDataRawMixin version of definition_from_xml, which maps a bare to empty data. The built-in block used RawMixin, which always stores the OLX. With empty data, rendering raised XMLSyntaxError (HTTP 500) and export dropped the block's file while keeping the pointer in its unit. Remove the empty-data branch so a bare element is stored as its OLX. Keep _get_xmltree() so blocks already stored with empty data by earlier releases still render. Add a round-trip test for export. Co-Authored-By: Claude Opus 5.5 --- src/xblock_annotatable/block.py | 12 +++++----- .../tests/test_annotatable.py | 22 +++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/xblock_annotatable/block.py b/src/xblock_annotatable/block.py index b4736142..e7a7e0e2 100644 --- a/src/xblock_annotatable/block.py +++ b/src/xblock_annotatable/block.py @@ -154,10 +154,10 @@ def _get_xmltree(self): """ Parse ``self.data`` into an element tree. - A bare ```` (e.g. a block just created in a content library) is - stored with empty ``data`` by ``definition_from_xml``. Treat that as an empty - annotatable instead of failing with ``XMLSyntaxError: Document is empty``, - matching the built-in block, which rendered such blocks as empty content. + ``definition_from_xml`` no longer produces empty ``data``, but blocks parsed by + earlier releases (which stored a bare ```` as ``""``) may still have + it. Treat that as an empty annotatable instead of failing with + ``XMLSyntaxError: Document is empty``. """ if not (self.data or "").strip(): return etree.Element("annotatable") @@ -299,8 +299,8 @@ def parse_xml_new_runtime(cls, node, runtime, keys): @classmethod def definition_from_xml(cls, xml_object, system): - if len(xml_object) == 0 and len(list(xml_object.items())) == 0: - return {"data": ""}, [] + # Store the OLX as-is, even for a bare ````. The built-in block used + # ``RawMixin`` (not ``EmptyDataRawMixin``), so its ``data`` was never empty. return {"data": etree.tostring(xml_object, pretty_print=True, encoding="unicode")}, [] def definition_to_xml(self, resource_fs): diff --git a/src/xblock_annotatable/tests/test_annotatable.py b/src/xblock_annotatable/tests/test_annotatable.py index c1c49192..b03f7556 100644 --- a/src/xblock_annotatable/tests/test_annotatable.py +++ b/src/xblock_annotatable/tests/test_annotatable.py @@ -168,8 +168,8 @@ def _block_with_data(self, data): def test_get_html_with_empty_data(self): """ - A block with empty ``data`` (e.g. a bare ```` just created in a - content library) renders as empty content instead of raising XMLSyntaxError. + A block with empty ``data`` (stored by earlier releases, which parsed a bare + ```` to ``""``) renders as empty content instead of raising XMLSyntaxError. """ for data in ("", " \n"): block = self._block_with_data(data) @@ -179,13 +179,27 @@ def test_get_html_with_empty_data(self): def test_bare_element_from_xml_renders(self): """ - ``definition_from_xml`` maps a bare ```` to empty ``data``; that - block must still render (the content-library "create new component" flow). + A bare ```` (the content-library "create new component" flow, or an + empty element in imported OLX) must render as empty content. """ definition, _children = AnnotatableBlock.definition_from_xml(etree.fromstring(""), None) block = self._block_with_data(definition["data"]) assert block.get_html()["content_html"] == "
" + def test_bare_element_round_trips_on_export(self): + """ + A bare ```` is kept as its OLX, like the built-in block (``RawMixin``), + so export writes it back instead of dropping the block (``definition_to_xml`` returns + None for empty ``data``). + """ + definition, _children = AnnotatableBlock.definition_from_xml(etree.fromstring(""), None) + assert definition["data"].strip() == "" + block = self._block_with_data(definition["data"]) + exported = block.definition_to_xml(None) + assert exported is not None + assert exported.tag == "annotatable" + assert len(exported) == 0 + class AnnotatableBlockI18nJsCatalogTest(unittest.TestCase): """Tests for loading the block's namespaced JavaScript translation catalog."""