Skip to content

Commit

Permalink
#349 add workaround for CDATA
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Dec 14, 2022
1 parent f0367c5 commit 419515e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libs/Format/Confluence/DetailsToExpand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public function convert(string $content): string
{
// convert <namespace:tag to <namespace__tag for DOMDocument to be happy
$content = preg_replace('/<(\/?)(\w+):(\w+)/', '<\1\2___\3', $content);
$content = str_replace('<![CDATA[', '__CDATA_START__', $content);
$content = str_replace(']]>', '__CDATA_END__', $content);

$dom = new \DOMDocument();

Expand All @@ -29,6 +31,9 @@ public function convert(string $content): string
}

// restore namespace tags
$finalContent = str_replace('__CDATA_START__', '<![CDATA[', $finalContent);
$finalContent = str_replace('__CDATA_END__', ']]>', $finalContent);

return preg_replace('/<(\/?)(\w+)___(\w+)/', '<\1\2:\3', $finalContent);
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Format/Confluence/DetailsToExpandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,44 @@ public function provideExpandData()
</ac:rich-text-body></ac:structured-macro>
EOD
],
[
// Works with confluence code blocks
<<<'EOD'
<ac:structured-macro ac:name="html">
<ac:plain-text-body> <![CDATA[
<script>
console.log("hi friends");
</script>
]]></ac:plain-text-body>
</ac:structured-macro>
<details>
<summary>Title !</summary>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</details>
EOD,
<<<'EOD'
<ac:structured-macro ac:name="html">
<ac:plain-text-body> <![CDATA[
<script>
console.log("hi friends");
</script>
]]></ac:plain-text-body>
</ac:structured-macro>
<ac:structured-macro ac:name="expand"><ac:parameter ac:name="">Title !</ac:parameter><ac:rich-text-body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</ac:rich-text-body></ac:structured-macro>
EOD
],
[
// Don't convert without title
<<<'EOD'
Expand Down

0 comments on commit 419515e

Please sign in to comment.