Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311450

Change subject: Tag: Generate valid HTML for self-closing tags
......................................................................

Tag: Generate valid HTML for self-closing tags

Bug: T145974
Change-Id: I5c0b554e759f08bbbc490eae3ec8b9fd85f4d299
---
M php/Tag.php
M tests/phpunit/TagTest.php
2 files changed, 41 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/50/311450/1

diff --git a/php/Tag.php b/php/Tag.php
index b65f5fd..cd7093a 100644
--- a/php/Tag.php
+++ b/php/Tag.php
@@ -368,6 +368,25 @@
         * @throws Exception
         */
        public function toString() {
+               // List of void elements from HTML5, section 8.1.2 as of 
2016-09-19
+               static $voidElements = [
+                       'area',
+                       'base',
+                       'br',
+                       'col',
+                       'embed',
+                       'hr',
+                       'img',
+                       'input',
+                       'keygen',
+                       'link',
+                       'meta',
+                       'param',
+                       'source',
+                       'track',
+                       'wbr',
+               ];
+
                $attributes = '';
                foreach ( $this->getGeneratedAttributes() as $key => $value ) {
                        if ( !preg_match( '/^[0-9a-zA-Z-]+$/', $key ) ) {
@@ -410,7 +429,11 @@
                }
 
                // Tag
-               return '<' . $this->tag . $attributes . '>' . $content . '</' . 
$this->tag . '>';
+               if ( !$content && in_array( $this->tag, $voidElements ) ) {
+                       return '<' . $this->tag . $attributes . ' />';
+               } else {
+                       return '<' . $this->tag . $attributes . '>' . $content 
. '</' . $this->tag . '>';
+               }
        }
 
        /**
diff --git a/tests/phpunit/TagTest.php b/tests/phpunit/TagTest.php
index a1ddfd6..f5bb746 100644
--- a/tests/phpunit/TagTest.php
+++ b/tests/phpunit/TagTest.php
@@ -64,6 +64,23 @@
        }
 
        /**
+        * @covers Tag::toString
+        * @dataProvider provideToString
+        */
+       public function testToString( $tag, $expected ) {
+               $this->assertEquals( $expected, $tag->toString() );
+       }
+
+       public static function provideToString() {
+               return [
+                       [ ( new Tag( 'div' ) ),
+                               '<div></div>' ],
+                       [ ( new Tag( 'input' ) ),
+                               '<input />' ],
+               ];
+       }
+
+       /**
         * @covers Tag::isSafeUrl
         * @dataProvider provideIsSafeUrl
         */

-- 
To view, visit https://gerrit.wikimedia.org/r/311450
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5c0b554e759f08bbbc490eae3ec8b9fd85f4d299
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <matma....@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to