diff --git a/src/xblock_annotatable/block.py b/src/xblock_annotatable/block.py
index 5c7331bc..e7a7e0e2 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.
+
+ ``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")
+ 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(),
}
@@ -286,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 64f6a4f4..b03f7556 100644
--- a/src/xblock_annotatable/tests/test_annotatable.py
+++ b/src/xblock_annotatable/tests/test_annotatable.py
@@ -161,6 +161,45 @@ 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`` (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)
+ context = block.get_html()
+ assert context["instructions_html"] is None
+ assert context["content_html"] == "
"
+
+ def test_bare_element_from_xml_renders(self):
+ """
+ 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."""