[BUGFIX] Empty subpart template if no data given 01/20701/3
authorJigal van Hemert <jigal@xs4all.nl>
Sun, 5 May 2013 10:03:12 +0000 (12:03 +0200)
committerPhilipp Gampe <philipp.gampe@typo3.org>
Sun, 19 May 2013 15:19:48 +0000 (17:19 +0200)
substituteMarkerAndSubpartArrayRecursive() should return an empty string
instead of the raw subpart template if no data for that subpart is given.
To clear a subpart the data can be just an empty array.

Change-Id: Ia6dd92168a86058993648c2fe4768b2cc8436cde
Resolves: #47935
Releases: 6.2, 6.1, 6.0
Reviewed-on: https://review.typo3.org/20701
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
typo3/sysext/core/Classes/Html/HtmlParser.php
typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php [new file with mode: 0644]

index af50942..c047b55 100644 (file)
@@ -237,18 +237,21 @@ class HtmlParser {
         * markers.
         *
         * $markersAndSubparts = array (
-        * '###SINGLEMARKER1###' => 'value 1',
-        * '###SUBPARTMARKER1###' => array(
-        * 0 => array(
-        * '###SINGLEMARKER2###' => 'value 2',
-        * ),
-        * 1 => array(
-        * '###SINGLEMARKER2###' => 'value 3',
-        * )
-        * )
+        *      '###SINGLEMARKER1###' => 'value 1',
+        *      '###SUBPARTMARKER1###' => array(
+        *              0 => array(
+        *                      '###SINGLEMARKER2###' => 'value 2',
+        *              ),
+        *              1 => array(
+        *                      '###SINGLEMARKER2###' => 'value 3',
+        *              )
+        *      ),
+        *      '###SUBPARTMARKER2###' => array(
+        *      ),
         * )
         * Subparts can be nested, so below the 'SINGLEMARKER2' it is possible to have another subpart marker with an array as the
         * value, which in its turn contains the elements of the sub-subparts.
+        * Empty arrays for Subparts will cause the subtemplate to be cleared.
         *
         * @static
         * @param string $content The content stream, typically HTML template content.
@@ -285,16 +288,21 @@ class HtmlParser {
                }
                // Replace the subpart contents recursively
                foreach ($compoundItems as $subpartMarker) {
-                       foreach ($markersAndSubparts[$subpartMarker] as $partialMarkersAndSubparts) {
-                               $completeMarker = $subpartMarker;
-                               if ($uppercase) {
-                                       // use strtr instead of strtoupper to avoid locale problems with Turkish
-                                       $completeMarker = strtr($completeMarker, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
-                               }
-                               if (count($wraps) > 0) {
-                                       $completeMarker = $wraps[0] . $completeMarker . $wraps[1];
+                       $completeMarker = $subpartMarker;
+                       if ($uppercase) {
+                               // use strtr instead of strtoupper to avoid locale problems with Turkish
+                               $completeMarker = strtr($completeMarker, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
+                       }
+                       if (count($wraps) > 0) {
+                               $completeMarker = $wraps[0] . $completeMarker . $wraps[1];
+                       }
+                       if (count($markersAndSubparts[$subpartMarker]) > 0) {
+                               foreach ($markersAndSubparts[$subpartMarker] as $partialMarkersAndSubparts) {
+                                       $subpartSubstitutes[$completeMarker] .= self::substituteMarkerAndSubpartArrayRecursive($subTemplates[$completeMarker],
+                                               $partialMarkersAndSubparts, $wrap, $uppercase, $deleteUnused);
                                }
-                               $subpartSubstitutes[$completeMarker] .= self::substituteMarkerAndSubpartArrayRecursive($subTemplates[$completeMarker], $partialMarkersAndSubparts, $wrap, $uppercase, $deleteUnused);
+                       } else {
+                               $subpartSubstitutes[$completeMarker] = '';
                        }
                }
                // Substitute the single markers and subparts
diff --git a/typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php b/typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php
new file mode 100644 (file)
index 0000000..bc371bd
--- /dev/null
@@ -0,0 +1,249 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Html;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2013 Nicole Cordes <typo3@cordes.co>
+ *  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 \TYPO3\CMS\Core\Html\HtmlParser
+ *
+ * @author Nicole Cordes <typo3@cordes.co>
+ */
+class HtmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Core\Html\HtmlParser
+        */
+       protected $fixture = NULL;
+
+       public function setUp() {
+               $this->fixture = new \TYPO3\CMS\Core\Html\HtmlParser();
+       }
+
+       public function tearDown() {
+               unset($this->fixture);
+       }
+
+       /**
+        * Data provider for substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray
+        */
+       public function substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArrayDataProvider() {
+               $template = '###SINGLEMARKER1###
+<!-- ###FOO### begin -->
+<!-- ###BAR### begin -->
+###SINGLEMARKER2###
+<!-- ###BAR### end -->
+<!-- ###FOOTER### begin -->
+###SINGLEMARKER3###
+<!-- ###FOOTER### end -->
+<!-- ###FOO### end -->';
+
+               $expected ='Value 1
+
+
+Value 2.1
+
+Value 2.2
+
+
+Value 3.1
+
+Value 3.2
+
+';
+
+               return array(
+                       'Single marker' => array(
+                               '###SINGLEMARKER###',
+                               array(
+                                       '###SINGLEMARKER###' => 'Value 1'
+                               ),
+                               '',
+                               FALSE,
+                               FALSE,
+                               'Value 1'
+                       ),
+                       'Subpart marker' => array(
+                               $template,
+                               array(
+                                       '###SINGLEMARKER1###' => 'Value 1',
+                                       '###FOO###' => array(
+                                               array(
+                                                       '###BAR###' => array(
+                                                               array(
+                                                                       '###SINGLEMARKER2###' => 'Value 2.1'
+                                                               ),
+                                                               array(
+                                                                       '###SINGLEMARKER2###' => 'Value 2.2'
+                                                               )
+                                                       ),
+                                                       '###FOOTER###' => array(
+                                                               array(
+                                                                       '###SINGLEMARKER3###' => 'Value 3.1'
+                                                               ),
+                                                               array(
+                                                                       '###SINGLEMARKER3###' => 'Value 3.2'
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               '',
+                               FALSE,
+                               FALSE,
+                               $expected
+                       ),
+                       'Subpart marker with wrap' => array(
+                               $template,
+                               array(
+                                       'SINGLEMARKER1' => 'Value 1',
+                                       'FOO' => array(
+                                               array(
+                                                       'BAR' => array(
+                                                               array(
+                                                                       'SINGLEMARKER2' => 'Value 2.1'
+                                                               ),
+                                                               array(
+                                                                       'SINGLEMARKER2' => 'Value 2.2'
+                                                               )
+                                                       ),
+                                                       'FOOTER' => array(
+                                                               array(
+                                                                       'SINGLEMARKER3' => 'Value 3.1'
+                                                               ),
+                                                               array(
+                                                                       'SINGLEMARKER3' => 'Value 3.2'
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               '###|###',
+                               FALSE,
+                               FALSE,
+                               $expected
+                       ),
+                       'Subpart marker with lower marker array keys' => array(
+                               $template,
+                               array(
+                                       '###singlemarker1###' => 'Value 1',
+                                       '###foo###' => array(
+                                               array(
+                                                       '###bar###' => array(
+                                                               array(
+                                                                       '###singlemarker2###' => 'Value 2.1'
+                                                               ),
+                                                               array(
+                                                                       '###singlemarker2###' => 'Value 2.2'
+                                                               )
+                                                       ),
+                                                       '###footer###' => array(
+                                                               array(
+                                                                       '###singlemarker3###' => 'Value 3.1'
+                                                               ),
+                                                               array(
+                                                                       '###singlemarker3###' => 'Value 3.2'
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               '',
+                               TRUE,
+                               FALSE,
+                               $expected
+                       ),
+                       'Subpart marker with unused markers' => array(
+                               $template,
+                               array(
+                                       '###FOO###' => array(
+                                               array(
+                                                       '###BAR###' => array(
+                                                               array(
+                                                                       '###SINGLEMARKER2###' => 'Value 2.1'
+                                                               )
+                                                       ),
+                                                       '###FOOTER###' => array(
+                                                               array(
+                                                                       '###SINGLEMARKER3###' => 'Value 3.1'
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               '',
+                               FALSE,
+                               TRUE,
+                               '
+
+
+Value 2.1
+
+
+Value 3.1
+
+'
+                       ),
+                       'Subpart marker with empty subpart' => array(
+                               $template,
+                               array(
+                                       '###SINGLEMARKER1###' => 'Value 1',
+                                       '###FOO###' => array(
+                                               array(
+                                                       '###BAR###' => array(
+                                                               array(
+                                                                       '###SINGLEMARKER2###' => 'Value 2.1'
+                                                               ),
+                                                               array(
+                                                                       '###SINGLEMARKER2###' => 'Value 2.2'
+                                                               )
+                                                       ),
+                                                       '###FOOTER###' => array()
+                                               )
+                                       )
+                               ),
+                               '',
+                               FALSE,
+                               FALSE,
+                               'Value 1
+
+
+Value 2.1
+
+Value 2.2
+
+
+'
+                       )
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArrayDataProvider
+        */
+       public function substituteMarkerAndSubpartArrayRecursiveResolvesMarkersAndSubpartsArray($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused, $expected) {
+               $this->assertSame($expected, $this->fixture->substituteMarkerAndSubpartArrayRecursive($template, $markersAndSubparts, $wrap, $uppercase, $deleteUnused));
+       }
+}
+?>
\ No newline at end of file