[BUGFIX] Make getFirstTag() return empty string for input without tag 98/42898/3
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 28 Aug 2015 06:27:36 +0000 (08:27 +0200)
committerAlexander Opitz <opitz.alexander@googlemail.com>
Tue, 8 Sep 2015 07:12:20 +0000 (09:12 +0200)
getFirstTag() didn't take into account that strpos() returns FALSE when
the needle isn't found.

Resolves: #69318
Releases: master
Change-Id: Ie530db9cbed92fa11199852cc0aec77024354814
Reviewed-on: http://review.typo3.org/42898
Reviewed-by: Michael Oehlhof <typo3@oehlhof.de>
Tested-by: Michael Oehlhof <typo3@oehlhof.de>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
typo3/sysext/core/Classes/Html/HtmlParser.php
typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php

index 94b588a..8a61c1d 100644 (file)
@@ -362,8 +362,8 @@ class HtmlParser {
         */
        public function getFirstTag($str) {
                // First:
         */
        public function getFirstTag($str) {
                // First:
-               $endLen = strpos($str, '>') + 1;
-               return substr($str, 0, $endLen);
+               $endLen = strpos($str, '>');
+               return $endLen !== FALSE ? substr($str, 0, $endLen + 1) : '';
        }
 
        /**
        }
 
        /**
index 36bb3ce..842d1f9 100644 (file)
@@ -343,8 +343,10 @@ class HtmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function getFirstTagDataProvider() {
                return array(
         */
        public function getFirstTagDataProvider() {
                return array(
-                       array('<body><span></span></body>',
-                       '<body>'),
+                       array('<body><span></span></body>', '<body>'),
+                       array('<span>Wrapper<div>Some content</div></span>', '<span>'),
+                       array('Something before<span>Wrapper<div>Some content</div></span>Something after', 'Something before<span>'),
+                       array('Something without tag', '')
                );
        }
 
                );
        }
 
@@ -429,4 +431,5 @@ class HtmlParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function removeFirstAndLastTag($str, $expectedResult) {
                $this->assertEquals($expectedResult, $this->subject->removeFirstAndLastTag($str));
        }
        public function removeFirstAndLastTag($str, $expectedResult) {
                $this->assertEquals($expectedResult, $this->subject->removeFirstAndLastTag($str));
        }
+
 }
 }