Skip to content

Commit

Permalink
fix: don't wrap HTML data with newlines when serializing for LC
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmccormick committed Sep 24, 2024
1 parent 75d111a commit 00b0f41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
11 changes: 5 additions & 6 deletions openedx/core/djangoapps/content_staging/tests/test_clipboard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Tests for the clipboard functionality
"""
from textwrap import dedent
from xml.etree import ElementTree

from rest_framework.test import APIClient
Expand Down Expand Up @@ -155,11 +154,11 @@ def test_copy_html(self):
assert olx_response.get("Content-Type") == "application/vnd.openedx.xblock.v1.html+xml"
# For HTML, we really want to be sure that the OLX is serialized in this exact format (using CDATA), so we check
# the actual string directly rather than using assertXmlEqual():
assert olx_response.content.decode() == dedent("""
<html url_name="toyhtml" display_name="Text"><![CDATA[
<a href='/static/handouts/sample_handout.txt'>Sample</a>
]]></html>
""").lstrip()
assert olx_response.content.decode() == (
"<html url_name=\"toyhtml\" display_name=\"Text\"><![CDATA["
"<a href='/static/handouts/sample_handout.txt'>Sample</a>"
"]]></html>\n"
)

# Now if we GET the clipboard again, the GET response should exactly equal the last POST response:
assert client.get(CLIPBOARD_ENDPOINT).json() == response_data
Expand Down
2 changes: 1 addition & 1 deletion openedx/core/lib/xblock_serializer/block_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _serialize_html_block(self, block) -> etree.Element:

# Escape any CDATA special chars
escaped_block_data = block.data.replace("]]>", "]]&gt;")
olx_node.text = etree.CDATA("\n" + escaped_block_data + "\n")
olx_node.text = etree.CDATA(escaped_block_data)
return olx_node


Expand Down
16 changes: 16 additions & 0 deletions openedx/core/lib/xblock_serializer/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ def test_html_with_fields(self):
"""
)

def test_html_whitespace_exact(self):
"""
Test that the whitespace with an HTML block's content is preserved.
"""
course = CourseFactory.create(display_name='test course', run="Testing_course")
html_block = BlockFactory.create(
parent_location=course.location,
display_name=" Hello ",
category="html",
data=" <span> 🍔 </span>\n",
)
serialized = api.serialize_xblock_to_olx(html_block)
assert serialized.olx_str == (
"<html url_name=\"_Hello_\" display_name=\" Hello \"><![CDATA[ <span> 🍔 </span>\n]]></html>\n"
)

def test_export_sequential(self):
"""
Export a sequential from the toy course, including all of its children.
Expand Down

0 comments on commit 00b0f41

Please sign in to comment.