Skip to content
Merged
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
18 changes: 15 additions & 3 deletions atlassian/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ def html_table_from_nested_dict(data, ordering):
return html + "\n</tbody></table>"


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("]]>", "]]]]><![CDATA[>")


def block_code_macro_confluence(code, lang=None):
"""
Wrap into code block macro
Expand All @@ -220,7 +232,7 @@ def block_code_macro_confluence(code, lang=None):
<ac:plain-text-body><![CDATA[{code}]]></ac:plain-text-body>
</ac:structured-macro>
"""
).format(lang=lang, code=code)
).format(lang=lang, code=_escape_cdata(code))


def html_code__macro_confluence(text):
Expand All @@ -235,7 +247,7 @@ def html_code__macro_confluence(text):
<ac:plain-text-body><![CDATA[{text}]]></ac:plain-text-body>
</ac:structured-macro>
"""
).format(text=text)
).format(text=_escape_cdata(text))


def noformat_code_macro_confluence(text, nopanel=None):
Expand All @@ -254,7 +266,7 @@ def noformat_code_macro_confluence(text, nopanel=None):
<ac:plain-text-body><![CDATA[{text}]]></ac:plain-text-body>
</ac:structured-macro>
"""
).format(nopanel=nopanel, text=text)
).format(nopanel=nopanel, text=_escape_cdata(text))


def symbol_normalizer(text):
Expand Down
44 changes: 43 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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") == '<a href="mailto:test@example.com">test@example.com</a>'


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'<root xmlns:ac="urn:ac">{macro}</root>')
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("<p>hi</p>")) == "<p>hi</p>"

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 = '<![CDATA[a]]>b'"
assert self._plain_text_body(block_code_macro_confluence(code, "python")) == code

def test_html_macro_with_cdata_terminator(self):
text = "<script>//<![CDATA[\nvar a = 1;\n//]]></script>"
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