[BUGFIX] Properly match html tags with data-* attributes 06/34406/2
authorLudwig Rafelsberger <ludwig.rafelsberger@gmx.at>
Wed, 19 Nov 2014 21:38:21 +0000 (22:38 +0100)
committerMarkus Klein <klein.t3@reelworx.at>
Thu, 20 Nov 2014 19:35:59 +0000 (20:35 +0100)
The cropHTML method needs to account for html tags - therefore it must
match them. The regex used for matching tags misses html tags with
attributes that contain dashes. (\w matches "any 'word' character",
being "[…] any letter or digit or the underscore character […]") [1].

The current W3C html draft sets even less constraints on what makes an
attribute name [2], but including dashes looks like the most urgent
need.

[1] http://php.net/manual/en/regexp.reference.escape.php
[2] http://www.w3.org/TR/html51/syntax.html#attributes-0

Resolves: #63086
Releases: master, 6.2
Change-Id: I52832ca3e8d9660faa939e673af6cdc32f310b18
Reviewed-on: http://review.typo3.org/34406
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 49dc492..dc20c83 100644 (file)
@@ -3899,7 +3899,7 @@ class ContentObjectRenderer {
                                (?:
                                        (?:
                                                (?:
-                                                       \\s+\\w+                                # EITHER spaces, followed by word characters (attribute names)
+                                                       \\s+\\w[\\w-]*          # EITHER spaces, followed by attribute names
                                                        (?:
                                                                \\s*=?\\s*              # equals
                                                                (?>
index 429a917..ee8f998 100644 (file)
@@ -692,7 +692,13 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                                        'Some text with a link to <link email.address@example.org - mail "Open email window">my email.address@example.org</link> and text after it',
                                        'Some text with a link to <link email.address@example.org - mail "Open email window">my...</link>',
                                        $charset
-                               )
+                               ),
+                               $charset . ' html elements with dashes in attributes' => array(
+                                       '9',
+                                       '<em data-foo="x">foobar</em>foobaz',
+                                       '<em data-foo="x">foobar</em>foo',
+                                       $charset
+                               ),
                        ));
                }
                return $data;
@@ -721,6 +727,7 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function cropHtmlWorksWithComplexContent() {
                $GLOBALS['TSFE']->renderCharset = 'iso-8859-1';
+
                $subject = '
 <h1>Blog Example</h1>
 <hr>
@@ -735,7 +742,7 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                Below are the most recent posts:
        </p>
        <ul>
-               <li>
+               <li data-element="someId">
                        <h3>
                                <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog]=&amp;tx_blogexample_pi1[action]=show&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=003b0131ed">The Post #1</a>
                        </h3>
@@ -776,12 +783,12 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                Below are the most recent posts:
        </p>
        <ul>
-               <li>
+               <li data-element="someId">
                        <h3>
                                <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog]=&amp;tx_blogexample_pi1[action]=show&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=003b0131ed">The Pos</a></h3></li></ul></div>';
                $this->assertEquals($expected, $result);
                $result = $this->cObj->cropHTML($subject, '-100');
-               $expected = '<div class="tx-blogexample-list-container"><ul><li><p>Design]&nbsp;<br>
+               $expected = '<div class="tx-blogexample-list-container"><ul><li data-element="someId"><p>Design]&nbsp;<br>
                                <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[action]=show&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=f982643bc3">read more &gt;&gt;</a><br>
                                <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=edit&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=5b481bc8f0">Edit</a>&nbsp;<a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=delete&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=4e52879656">Delete</a>
                        </p>