jenkins-bot has submitted this change and it was merged. 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(-) Approvals: Jforrester: Looks good to me, approved jenkins-bot: Verified Objections: VolkerE: There's a problem with this change, please improve 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: merged Gerrit-Change-Id: I5c0b554e759f08bbbc490eae3ec8b9fd85f4d299 Gerrit-PatchSet: 1 Gerrit-Project: oojs/ui Gerrit-Branch: master Gerrit-Owner: Bartosz Dziewoński <matma....@gmail.com> Gerrit-Reviewer: Bartosz Dziewoński <matma....@gmail.com> Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org> Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org> Gerrit-Reviewer: VolkerE <volke...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits