diff options
author | Mario <mario@mariovavti.com> | 2024-03-20 14:35:43 +0100 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-03-20 14:35:43 +0100 |
commit | 3de8f5e7de424563cfe521848b2e2d11d100fcc5 (patch) | |
tree | c7419a0516af3f79a4b5e405d99dcf6ad5673bcd | |
parent | a18e873d08e733225c70b0ace31c3cbb025ff906 (diff) | |
download | volse-hubzilla-3de8f5e7de424563cfe521848b2e2d11d100fcc5.tar.gz volse-hubzilla-3de8f5e7de424563cfe521848b2e2d11d100fcc5.tar.bz2 volse-hubzilla-3de8f5e7de424563cfe521848b2e2d11d100fcc5.zip |
deal with wihitespace issues in tables and lists and add tests
-rw-r--r-- | include/bbcode.php | 47 | ||||
-rw-r--r-- | tests/unit/includes/BBCodeTest.php | 12 | ||||
-rw-r--r-- | vendor/composer/installed.php | 4 |
3 files changed, 53 insertions, 10 deletions
diff --git a/include/bbcode.php b/include/bbcode.php index db4147c1e..86944ba60 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -1054,6 +1054,15 @@ function bb_fixtable_lf($match) { } +function bb_fix_lf($match) { + // remove extraneous whitespace between element tags since newlines will all + // be converted to '<br />' and turn your neatly crafted tables into a whole lot of + // empty space. + + $new_content = str_replace(["\n\r", "\n", "\r"], '', $match[1]); + return str_replace($match[1], $new_content, $match[0]); +} + function bbtopoll($s) { $pl = []; @@ -1207,8 +1216,6 @@ function bbcode($text, $options = []) { } } - - $text = bb_format_attachdata($text); // If we find any event code, turn it into an event. @@ -1249,7 +1256,7 @@ function bbcode($text, $options = []) { $text = str_replace("<", "<", $text); $text = str_replace(">", ">", $text); - $text = preg_replace_callback("/\[table\](.*?)\[\/table\]/ism",'bb_fixtable_lf',$text); + $text = str_replace(array("\t", " "), array(" ", " "), $text); // Check for [code] text here, before the linefeeds are messed with. @@ -1487,10 +1494,10 @@ function bbcode($text, $options = []) { // Check for list text - $text = preg_replace("/<br \/>\[\*\]/ism",'[*]',$text); - + $text = str_replace(["\r\n[*]", "\r[*]", "\n[*]"], "[*]", $text); $text = str_replace("[*]", "<li>", $text); + // handle nested lists $endlessloop = 0; @@ -1499,16 +1506,35 @@ function bbcode($text, $options = []) { ((strpos($text, "[/ul]") !== false) && (strpos($text, "[ul]") !== false)) || ((strpos($text, "[/dl]") !== false) && (strpos($text, "[dl") !== false)) || ((strpos($text, "[/li]") !== false) && (strpos($text, "[li]") !== false))) && (++$endlessloop < 20)) { + + $text = preg_replace_callback("/\[list\](.*?)\[\/list\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet">$1</ul>', $text); + + $text = preg_replace_callback("/\[list=\](.*?)\[\/list\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $text); + + $text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $text); + + $text = preg_replace_callback("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism",'<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $text); + + $text = preg_replace_callback("/\[list=((?-i)I)\](.*?)\[\/list\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $text); + + $text = preg_replace_callback("/\[list=((?-i)a)\](.*?)\[\/list\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $text); + + $text = preg_replace_callback("/\[list=((?-i)A)\](.*?)\[\/list\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $text); - $text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet">$1</ul>', $text); + + $text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/ism",'bb_fix_lf', $text); $text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $text); - $text = preg_replace("/\[\/li\]<br \/>\[li\]/ism",'[/li][li]',$text); + + $text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/ism",'bb_fix_lf', $text); + $text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet">$1</ul>', $text); + + $text = preg_replace("/\[\/li\]<br \/>\[li\]/ism",'[/li][li]', $text); $text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>', $text); // [dl] tags have an optional [dl terms="bi"] form where bold/italic/underline/mono/large @@ -1521,24 +1547,29 @@ function bbcode($text, $options = []) { } if (strpos($text,'[checklist]') !== false) { + $text = preg_replace_callback("/\[checklist\](.*?)\[\/checklist\]/ism",'bb_fix_lf', $text); $text = preg_replace_callback("/\[checklist\](.*?)\[\/checklist\]/ism", 'bb_checklist', $text); } if (strpos($text,'[th]') !== false) { $text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>', $text); } + if (strpos($text,'[td]') !== false) { $text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '<td>$1</td>', $text); } + if (strpos($text,'[tr]') !== false) { $text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '<tr>$1</tr>', $text); } + if (strpos($text,'[/table]') !== false) { + $text = preg_replace_callback("/\[table\](.*?)\[\/table\]/ism",'bb_fix_lf',$text); $text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '<table>$1</table>', $text); $text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '<table class="table table-responsive table-bordered" >$1</table>', $text); $text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '<table class="table table-responsive" >$1</table>', $text); } - $text = str_replace('</tr><br /><tr>', "</tr>\n<tr>", $text); + $text = str_replace('[hr]', '<hr />', $text); // This is actually executed in prepare_body() diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php index 6f79c45f4..100d28fc4 100644 --- a/tests/unit/includes/BBCodeTest.php +++ b/tests/unit/includes/BBCodeTest.php @@ -107,6 +107,18 @@ class BBCodeTest extends UnitTestCase { "[code]\ntestvar = \"this is a test\"\necho \"the message is \$testvar\"\n[/code]", '<pre><code>testvar = "this is a test"<br />echo "the message is $testvar"</code></pre>', ], + 'list with linebreaks \n' => [ + "some text\n[list]\n[*] item1\n[*] item2\n[/list]\nsome more text", + 'some text<br /><ul class="listbullet"><li> item1<li> item2</ul><br />some more text' + ], + 'list with linebreaks \r' => [ + "some text\r[list]\r[*] item1\r[*] item2\r[/list]\rsome more text", + 'some text<br /><ul class="listbullet"><li> item1<li> item2</ul><br />some more text' + ], + 'list with linebreaks \r\n' => [ + "some text\r\n[list]\r\n[*] item1\r\n[*] item2\r\n[/list]\r\nsome more text", + 'some text<br /><ul class="listbullet"><li> item1<li> item2</ul><br />some more text' + ] ]; } diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 9c1c3eb6a..4386e70b5 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'zotlabs/hubzilla', 'pretty_version' => 'dev-9.0RC', 'version' => 'dev-9.0RC', - 'reference' => '8754f72e63ea0fd9067c20104a2b068e5c518d0a', + 'reference' => 'a18e873d08e733225c70b0ace31c3cbb025ff906', 'type' => 'application', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -340,7 +340,7 @@ 'zotlabs/hubzilla' => array( 'pretty_version' => 'dev-9.0RC', 'version' => 'dev-9.0RC', - 'reference' => '8754f72e63ea0fd9067c20104a2b068e5c518d0a', + 'reference' => 'a18e873d08e733225c70b0ace31c3cbb025ff906', 'type' => 'application', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), |