diff --git a/atlassian/utils.py b/atlassian/utils.py
index f9e310774..10d8fafcd 100644
--- a/atlassian/utils.py
+++ b/atlassian/utils.py
@@ -204,6 +204,18 @@ def html_table_from_nested_dict(data, ordering):
return html + "\n"
+def _escape_cdata(text):
+ """
+ Make text safe to place inside a CDATA section.
+
+ A CDATA section ends at the first ``]]>``, so that sequence is split over
+ two adjacent sections. The text is unchanged once the XML is parsed.
+ :param text:
+ :return:
+ """
+ return str(text).replace("]]>", "]]]]>")
+
+
def block_code_macro_confluence(code, lang=None):
"""
Wrap into code block macro
@@ -220,7 +232,7 @@ def block_code_macro_confluence(code, lang=None):
"""
- ).format(lang=lang, code=code)
+ ).format(lang=lang, code=_escape_cdata(code))
def html_code__macro_confluence(text):
@@ -235,7 +247,7 @@ def html_code__macro_confluence(text):
"""
- ).format(text=text)
+ ).format(text=_escape_cdata(text))
def noformat_code_macro_confluence(text, nopanel=None):
@@ -254,7 +266,7 @@ def noformat_code_macro_confluence(text, nopanel=None):
"""
- ).format(nopanel=nopanel, text=text)
+ ).format(nopanel=nopanel, text=_escape_cdata(text))
def symbol_normalizer(text):
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 03bcd7da2..5c281e129 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -3,9 +3,20 @@
Unit tests for atlassian.utils module
"""
+import xml.etree.ElementTree as ET
+
import pytest
from .mockup import mockup_server
-from atlassian.utils import is_email, html_email, html_list, html_table_header_row, html_row_with_ordered_headers
+from atlassian.utils import (
+ is_email,
+ html_email,
+ html_list,
+ html_table_header_row,
+ html_row_with_ordered_headers,
+ block_code_macro_confluence,
+ html_code__macro_confluence,
+ noformat_code_macro_confluence,
+)
class TestIsEmail:
@@ -341,3 +352,34 @@ def test_mockup_integration(self):
result = is_email("test@example.com")
assert result is not None
assert html_email("test@example.com") == 'test@example.com'
+
+
+class TestConfluenceMacroHelpers:
+ """The code, html and noformat macro helpers must stay well-formed XML for any text."""
+
+ @staticmethod
+ def _plain_text_body(macro):
+ root = ET.fromstring(f'{macro}')
+ return root.find("{urn:ac}structured-macro").find("{urn:ac}plain-text-body").text
+
+ def test_block_code_macro_keeps_text(self):
+ code = "print('hello')"
+ assert self._plain_text_body(block_code_macro_confluence(code, "python")) == code
+
+ def test_html_macro_keeps_text(self):
+ assert self._plain_text_body(html_code__macro_confluence("
hi
")) == "hi
"
+
+ def test_noformat_macro_keeps_text(self):
+ assert self._plain_text_body(noformat_code_macro_confluence("plain text")) == "plain text"
+
+ def test_block_code_macro_with_cdata_terminator(self):
+ code = "x = 'b'"
+ assert self._plain_text_body(block_code_macro_confluence(code, "python")) == code
+
+ def test_html_macro_with_cdata_terminator(self):
+ text = ""
+ assert self._plain_text_body(html_code__macro_confluence(text)) == text
+
+ def test_noformat_macro_with_cdata_terminator(self):
+ text = "a ]]> b ]]]]> c"
+ assert self._plain_text_body(noformat_code_macro_confluence(text)) == text