RemexCompatMunger: Don't split p-wrapping on style/link tags
authorBrad Jorsch <bjorsch@wikimedia.org>
Sun, 27 Jan 2019 22:13:10 +0000 (14:13 -0800)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 30 Jan 2019 17:10:24 +0000 (09:10 -0800)
<style> and <link> tags are metadata tags, they shouldn't split the <p>
tag when p-wrapping content.

Bug: T208901
Change-Id: I2ef5da68c9ccde4477d8295dfe4abf8497c5d26e

includes/tidy/RemexCompatMunger.php
tests/phpunit/includes/tidy/RemexDriverTest.php

index 78a1104..df9bb96 100644 (file)
@@ -72,6 +72,21 @@ class RemexCompatMunger implements TreeHandler {
                "mark" => true,
        ];
 
+       /**
+        * For the purposes of this class, "metadata" elements are those that
+        * should neither trigger p-wrapping nor stop an outer p-wrapping,
+        * typically those that are themselves invisible in a browser's rendering.
+        * This isn't a complete list, it's just the tags that we're likely to
+        * encounter in practice.
+        * @var array
+        */
+       private static $metadataElements = [
+               'style' => true,
+               'script' => true,
+               'link' => true,
+               'meta' => true,
+       ];
+
        private static $formattingElements = [
                'a' => true,
                'b' => true,
@@ -261,7 +276,11 @@ class RemexCompatMunger implements TreeHandler {
                $under = $preposition === TreeBuilder::UNDER;
                $elementToEnd = null;
 
-               if ( $under && $parentData->isPWrapper && !$inline ) {
+               if ( isset( self::$metadataElements[$elementName] ) ) {
+                       // The element is a metadata element, that we allow to appear in
+                       // both inline and block contexts.
+                       $this->trace( 'insert metadata' );
+               } elseif ( $under && $parentData->isPWrapper && !$inline ) {
                        // [B/b] The element is non-inline and the parent is a p-wrapper,
                        // close the parent and insert into its parent instead
                        $this->trace( 'insert B/b' );
index a5ebaa5..faa9aa1 100644 (file)
@@ -262,6 +262,26 @@ class RemexDriverTest extends MediaWikiTestCase {
                        '<i><blockquote><p></i>',
                        '<i></i><blockquote><p><i></i></p><p><i></i></p></blockquote>',
                ],
+               [
+                       'style tag isn\'t p-wrapped (T186965)',
+                       '<style>/* ... */</style>',
+                       '<style>/* ... */</style>',
+               ],
+               [
+                       'link tag isn\'t p-wrapped (T186965)',
+                       '<link rel="foo" href="bar" />',
+                       '<link rel="foo" href="bar" />',
+               ],
+               [
+                       'style tag doesn\'t split p-wrapping (T208901)',
+                       'foo <style>/* ... */</style> bar',
+                       '<p>foo <style>/* ... */</style> bar</p>',
+               ],
+               [
+                       'link tag doesn\'t split p-wrapping (T208901)',
+                       'foo <link rel="foo" href="bar" /> bar',
+                       '<p>foo <link rel="foo" href="bar" /> bar</p>',
+               ],
        ];
 
        public function provider() {