[BUGFIX] RTE transformation transforms LF/CR between div and hr into space
authorStanislas Rolland <typo3@sjbr.ca>
Fri, 29 Jul 2011 15:49:50 +0000 (11:49 -0400)
committerStanislas Rolland <typo3@sjbr.ca>
Fri, 10 Feb 2012 02:19:17 +0000 (03:19 +0100)
Fix the issue and add test cases.

Change-Id: Iff9ce1adc94fdbcf62f3ab4ee25c6c8a3703fe19
Resolves: #26815
Releases: 4.5, 4.6
Reviewed-on: http://review.typo3.org/3861
Reviewed-by: Oliver Hader
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
t3lib/class.t3lib_parsehtml_proc.php
tests/t3lib/class.t3lib_parsehtml_procTest.php [new file with mode: 0644]

index 5788069..14f50fb 100644 (file)
@@ -863,8 +863,11 @@ class t3lib_parsehtml_proc extends t3lib_parsehtml {
                                }
                        } else { // NON-block:
                                if (strcmp(trim($blockSplit[$k]), '')) {
+                                       $blockSplit[$k] = preg_replace('/<hr\/>/', '<hr />', $blockSplit[$k]);
+                                               // Remove linebreaks preceding hr tags
+                                       $blockSplit[$k] = preg_replace('/[' . preg_quote(LF . CR) . ']+<(hr)(\s[^>\/]*)?[[:space:]]*\/?>/', '<$1$2/>', $blockSplit[$k]);
                                                // Remove linebreaks following hr tags
-                                       $blockSplit[$k] = preg_replace('/<(hr)(\s[^>\/]*)?[[:space:]]*\/?>[' . preg_quote(LF . CR) . ']+/', '<$1$2 />', $blockSplit[$k]);
+                                       $blockSplit[$k] = preg_replace('/<(hr)(\s[^>\/]*)?[[:space:]]*\/?>[' . preg_quote(LF . CR) . ']+/', '<$1$2/>', $blockSplit[$k]);
                                                // Replace other linebreaks with space
                                        $blockSplit[$k] = preg_replace('/[' . preg_quote(LF . CR) . ']+/', ' ', $blockSplit[$k]);
                                        $blockSplit[$k] = $this->divideIntoLines($blockSplit[$k]) . $lastBR;
@@ -1205,7 +1208,7 @@ class t3lib_parsehtml_proc extends t3lib_parsehtml {
                        // Returns plainly the value if there was no div/p sections in it
                if (count($divSplit) <= 1 || $count <= 0) {
                                // Wrap hr tags with LF's
-                       $newValue = preg_replace('/<(hr)(\s[^>\/]*)?[[:space:]]*\/?>/i', LF . '<$1$2 />' . LF, $value);
+                       $newValue = preg_replace('/<(hr)(\s[^>\/]*)?[[:space:]]*\/?>/i', LF . '<$1$2/>' . LF, $value);
                        $newValue = preg_replace('/' . preg_quote(LF . LF) . '/i', LF, $newValue);
                        $newValue = preg_replace('/(^' . preg_quote(LF) . ')|(' . preg_quote(LF) . '$)/i', '', $newValue);
                        return $newValue;
@@ -1298,7 +1301,7 @@ class t3lib_parsehtml_proc extends t3lib_parsehtml {
                                        // Remove positions which are outside div/p tags and without content
                                $divSplit[$k] = trim(strip_tags($divSplit[$k], '<' . implode('><', $allowTagsOutside) . '>'));
                                        // Wrap hr tags with LF's
-                               $divSplit[$k] = preg_replace('/<(hr)(\s[^>\/]*)?[[:space:]]*\/?>/i', LF . '<$1$2 />' . LF, $divSplit[$k]);
+                               $divSplit[$k] = preg_replace('/<(hr)(\s[^>\/]*)?[[:space:]]*\/?>/i', LF . '<$1$2/>' . LF, $divSplit[$k]);
                                $divSplit[$k] = preg_replace('/' . preg_quote(LF . LF) . '/i', LF, $divSplit[$k]);
                                $divSplit[$k] = preg_replace('/(^' . preg_quote(LF) . ')|(' . preg_quote(LF) . '$)/i', '', $divSplit[$k]);
                                if (!strcmp($divSplit[$k], '')) {
@@ -1607,4 +1610,4 @@ if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLA
        include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_parsehtml_proc.php']);
 }
 
-?>
\ No newline at end of file
+?>
diff --git a/tests/t3lib/class.t3lib_parsehtml_procTest.php b/tests/t3lib/class.t3lib_parsehtml_procTest.php
new file mode 100644 (file)
index 0000000..4637549
--- /dev/null
@@ -0,0 +1,125 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2011 Stanislas Rolland <typo3@sjbr.ca>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Testcase for class t3lib_parsehtml_proc
+ *
+ * @package TYPO3
+ * @subpackage t3lib
+ *
+ * @author Stanislas Rolland <typo3@sjbr.ca>
+ */
+class t3lib_parsehtml_procTest extends tx_phpunit_testcase {
+       /**
+        * @var t3lib_parsehtml_proc
+        */
+       private $fixture = NULL;
+
+       public function setUp() {
+               $this->fixture = new t3lib_parsehtml_proc();
+               $this->fixture->procOptions = array(
+                       'dontConvBRtoParagraph' => '1',
+                       'preserveDIVSections' => '1',
+                       'allowTagsOutside' => 'hr, address',
+               );
+       }
+
+       public function tearDown() {
+               unset($this->fixture);
+       }
+       /**
+        * Data provider for TS_transform_db
+        */
+       public static function hrTagCorrectlyTransformedOnWayToDataBaseDataProvider() {
+               return array(
+                       'single hr' => array(
+                               '<hr />',
+                               '<hr />',
+                       ),
+                       'non-xhtml single hr' => array(
+                               '<hr/>',
+                               '<hr />',
+                       ),
+                       'double hr' => array(
+                               '<hr /><hr />',
+                               '<hr />' . LF . '<hr />',
+                       ),
+                       'linefeed followed by hr' => array(
+                               LF . '<hr />',
+                               '<hr />',
+                       ),
+                       'white space followed by hr' => array(
+                               ' <hr />',
+                               ' ' . LF . '<hr />',
+                       ),
+                       'white space followed linefeed and hr' => array(
+                               ' ' . LF . '<hr />',
+                               ' ' . LF . '<hr />',
+                       ),
+                       'br followed by hr' => array(
+                               '<br /><hr />',
+                               '<br />' . LF . '<hr />',
+                       ),
+                       'br followed by linefeed and hr' => array(
+                               '<br />' . LF . '<hr />',
+                               '<br />' . LF . '<hr />',
+                       ),
+                       'preserved div followed by hr' => array(
+                               '<div>Some text</div><hr />',
+                               '<div>Some text</div>' . LF . '<hr />',
+                       ),
+                       'preserved div followed by linefeed and hr' => array(
+                               '<div>Some text</div>' . LF . '<hr />',
+                               '<div>Some text</div>' . LF . '<hr />',
+                       ),
+                       'h1 followed by linefeed and hr' => array(
+                               '<h1>Some text</h1>' . LF . '<hr />',
+                               '<h1>Some text</h1>' . LF . '<hr />',
+                       ),
+                       'paragraph followed by linefeed and hr' => array(
+                               '<p>Some text</p>' . LF . '<hr />',
+                               'Some text' . LF . '<hr />',
+                       ),
+                       'some text followed by hr' => array(
+                               'Some text<hr />',
+                               'Some text' . LF . '<hr />',
+                       ),
+                       'some text followed by linefeed and hr' => array(
+                               'Some text' . LF . '<hr />',
+                               'Some text' . LF . '<hr />',
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider hrTagCorrectlyTransformedOnWayToDataBaseDataProvider
+        */
+       public function hrTagCorrectlyTransformedOnWayToDataBase($content, $expectedResult) {
+               //die(htmlspecialchars($expectedResult) . '-' . htmlspecialchars($this->fixture->TS_transform_db($content)));
+                       // Assume the transformation is ts_css
+               $this->assertEquals($expectedResult, $this->fixture->TS_transform_db($content, TRUE));
+       }
+}
+?>
\ No newline at end of file