Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/xblock_annotatable/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<annotatable/>`` 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"
Expand Down Expand Up @@ -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(),
}

Expand Down Expand Up @@ -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 ``<annotatable/>``. 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):
Expand Down
39 changes: 39 additions & 0 deletions src/xblock_annotatable/tests/test_annotatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
``<annotatable/>`` 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"] == "<div/>"

def test_bare_element_from_xml_renders(self):
"""
A bare ``<annotatable/>`` (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("<annotatable/>"), None)
block = self._block_with_data(definition["data"])
assert block.get_html()["content_html"] == "<div/>"

def test_bare_element_round_trips_on_export(self):
"""
A bare ``<annotatable/>`` 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("<annotatable/>"), None)
assert definition["data"].strip() == "<annotatable/>"
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."""
Expand Down