Skip to content

Commit bff244d

Browse files
Hipskajf-cbd
andauthored
N°8402 - fix(ActionSlackNotification): Improve TransformHTMLToSlackMarkup with… (#24)
* fix(ActionSlackNotification): Improve TransformHTMLToSlackMarkup with current CKEditor * Add test to avoid future potential regressions on different tags (and also prevent the new line issue) --------- Co-authored-by: jf-cbd <jimmy.fayolle@combodo.com>
1 parent a841614 commit bff244d

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

datamodel.combodo-webhook-integration.xml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,26 +1749,22 @@ protected function ApplyParamsToJson(array $aContextArgs, string $sInputAsJson)
17491749
}
17501750
17511751
$aReplacementsMatrix = array(
1752-
array( array('<br />', '<br/>', '<br>'), "\n" ),
1753-
array( array('<h1>', '</h1>'), array('*', '*') ),
1754-
array( array('<h2>', '</h2>'), array('*', '*') ),
1755-
array( array('<h3>', '</h3>'), array('*', '*') ),
1756-
array( array('<strong>', '</strong>'), array('*', '*') ),
1757-
array( array('<b>', '</b>'), array('*', '*') ),
1758-
array( array('<em>', '</em>'), array('_', '_') ),
1759-
array( array('<i>', '</i>'), array('_', '_') ),
1760-
array( array('<del>', '</del>'), array('~', '~') ),
1761-
array( array('<s>', '</s>'), array('~', '~') ),
1762-
array( array('<li>', '</li>'), array('• ', '') ),
1763-
array( array('<code>', '</code>'), array('`', '`') ),
1764-
array( array('<pre>', '</pre>'), array('```', '```') ),
1752+
[ '#<br(\s*/)?>#i', "\n" ],
1753+
[ ['#<p>#i', '#</p>#i'], ['', "\n"] ],
1754+
[ ['#<h[1,2,3]>#i', '#</h[1,2,3]>#i'], ['*', "*\n"] ],
1755+
[ ['#<(strong|b)>#i', '#</(strong|b)>#i'], '*' ],
1756+
[ ['#<(em|i)>#i', '#</(em|i)>#i'], '_' ],
1757+
[ ['#<(del|s)>#i', '#</(del|s)>#i'], '~' ],
1758+
[ ['#<li>#i', '#</li>#i'], ['• ', "\n"] ],
1759+
[ ['#<pre>(<code[^>]*>)?#i', '#(</code>)?</pre>#i'], ['```', "```\n"] ],
1760+
[ ['#<(mark[^>]*|code)>#i', '#</(mark|code)>#i'], '`' ],
17651761
);
17661762
17671763
// Replace HTML tags (list must contain at least tags from the $aReplacementsMatrix)
1768-
$sContent = strip_tags($sContent, '<h1><h2><h3><br><strong><b><em><i><del><s><li><code><pre><a>');
1764+
$sContent = strip_tags($sContent, '<br><p><h1><h2><h3><strong><b><em><i><del><s><li><pre><code><mark><a>');
17691765
foreach($aReplacementsMatrix as $aReplacements)
17701766
{
1771-
$sContent = str_replace($aReplacements[0], $aReplacements[1], $sContent);
1767+
$sContent = preg_replace($aReplacements[0], $aReplacements[1], $sContent);
17721768
}
17731769
17741770
// Replace hyperlinks

tests/php-unit-tests/ActionSlackNotificationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ public function TransformHTMLToSlackMarkupProvider(): array
5353
HTML,
5454
"expected" => "<!SomeGroup>",
5555
],
56+
"Simple p tags on different lines" => [
57+
"input" => <<<HTML
58+
<p>hello</p><p>hi</p>
59+
HTML,
60+
"expected" => "hello\nhi\n",
61+
],
62+
"Different tags on different lines" => [
63+
"input" => <<<HTML
64+
<h2>hello</h2><p><strong>hi</strong></p>
65+
HTML,
66+
"expected" => "*hello*\n*hi*\n",
67+
],
68+
"Nested tags" => [
69+
"input" => <<<HTML
70+
<p><strong>I'm bold and</strong></p><p><i><strong>I'm bold and italic</strong></i></p>
71+
HTML,
72+
"expected" => "*I'm bold and*\n_*I'm bold and italic*_\n",
73+
],
74+
"Large text with different tags" => [
75+
"input" => <<<HTML
76+
<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3><p><strong>Strong Text</strong><br><b>Bold Text</b><br><em>Emphasized Text</em><br><i>Italic Text</i><br><del>Deleted Text</del><br><s>Strikethrough Text</s><br><mark class="marker-yellow">Marked Text</mark><br><code>Inline Code</code></p><ul><li>List Item 1</li><li>List Item 2</li></ul><pre><code class="language-plaintext">Block of Code\r\nLine 1\r\nLine 2\r\n</code></pre>
77+
HTML,
78+
"expected" => "*Heading 1*\n*Heading 2*\n*Heading 3*\n*Strong Text*\n*Bold Text*\n_Emphasized Text_\n_Italic Text_\n~Deleted Text~\n~Strikethrough Text~\n`Marked Text`\n`Inline Code`\n• List Item 1\n• List Item 2\n```Block of Code\r\nLine 1\r\nLine 2\r\n```\n",
79+
],
5680
];
5781
}
5882
}

0 commit comments

Comments
 (0)