Browse Source

added HtmlHelper::nonVoidTag()

Fabian Peter Hammerle 7 years ago
parent
commit
3ad92a6d9b
2 changed files with 53 additions and 2 deletions
  1. 10 0
      HtmlHelper.php
  2. 43 2
      tests/HtmlHelperTest.php

+ 10 - 0
HtmlHelper.php

@@ -71,4 +71,14 @@ class HtmlHelper
             return '</' . $name . '>';
         }
     }
+
+    public static function nonVoidTag($tag_name, $content, array $attributes = [])
+    {
+        // @see https://www.w3.org/TR/html-markup/syntax.html#syntax-elements
+        return StringHelper::embed(
+            self::startTag($tag_name, $attributes),
+            $content,
+            self::endTag($tag_name)
+            );
+    }
 }

+ 43 - 2
tests/HtmlHelperTest.php

@@ -50,6 +50,7 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
         return [
             ['tag', [], '<tag />'],
             ['void', ['a' => '1'], '<void a="1" />'],
+            ['void', ['a' => ''], '<void a="" />'],
             ['void', ['a' => 1], '<void a="1" />'],
             ['void', ['a' => '1', 'b' => '2'], '<void a="1" b="2" />'],
             ['void', ['b' => '1', 'a' => '2'], '<void b="1" a="2" />'],
@@ -59,8 +60,8 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
             ['void', ['a' => false], '<void />'],
             ['void', ['a' => false, 'b' => '2'], '<void b="2" />'],
             ['script', ['type' => 'text/javascript'], '<script type="text/javascript" />'],
-            ['span', ['onclick' => 'alert(":-)")'], '<span onclick="alert(&quot;:-)&quot;)" />'],
-            ['span', ['onclick' => "alert(':-)')"], '<span onclick="alert(&#039;:-)&#039;)" />'],
+            ['img', ['ondrag' => 'alert(":-)")'], '<img ondrag="alert(&quot;:-)&quot;)" />'],
+            ['img', ['ondrag' => "alert(':-)')"], '<img ondrag="alert(&#039;:-)&#039;)" />'],
             [null, [], null],
             [null, ['attr' => 'v'], null],
             ];
@@ -74,6 +75,11 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($expected_tag, HtmlHelper::voidTag($name, $attributes));
     }
 
+    public function testVoidTagNoAttributes()
+    {
+        $this->assertSame('<tag />', HtmlHelper::voidTag('tag'));
+    }
+
     public function startTagTypeErrorProvider()
     {
         return [
@@ -98,6 +104,7 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
         return [
             ['tag', [], '<tag>'],
             ['start', ['a' => '1'], '<start a="1">'],
+            ['start', ['a' => ''], '<start a="">'],
             ['start', ['a' => 1], '<start a="1">'],
             ['start', ['a' => '1', 'b' => '2'], '<start a="1" b="2">'],
             ['start', ['b' => '1', 'a' => '2'], '<start b="1" a="2">'],
@@ -122,6 +129,11 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($expected_tag, HtmlHelper::startTag($name, $attributes));
     }
 
+    public function testStartTagNoAttributes()
+    {
+        $this->assertSame('<tag>', HtmlHelper::startTag('tag'));
+    }
+
     public function endTagTypeErrorProvider()
     {
         return [
@@ -155,4 +167,33 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
     {
         $this->assertSame($expected_tag, HtmlHelper::endTag($name));
     }
+
+    public function nonVoidTagProvider()
+    {
+        return [
+            [null, null, [], null],
+            [null, null, ['a' => '1'], null],
+            [null, 'content', [], 'content'],
+            [null, 'content', ['a' => '1'], 'content'],
+            [null, 'qu"ote', [], 'qu"ote'],
+            [null, '', [], ''],
+            ['tag', null, [], null],
+            ['tag', null, ['a' => true, 'b' => 2], null],
+            ['tag', '', [], '<tag></tag>'],
+            ['tag', '', ['a' => true, 'b' => 2], '<tag a="a" b="2"></tag>'],
+            ['tag', 'content', [], '<tag>content</tag>'],
+            ['tag', 'content', ['a' => true, 'b' => 2], '<tag a="a" b="2">content</tag>'],
+            ['tag', 'content', ['a' => null], '<tag>content</tag>'],
+            ['tag', 'content', ['a' => 'qu"ote'], '<tag a="qu&quot;ote">content</tag>'],
+            ['tag', 'cont"ent', ['a' => ''], '<tag a="">cont"ent</tag>'],
+            ];
+    }
+
+    /**
+     * @dataProvider nonVoidTagProvider
+     */
+    public function testNonVoidTag($name, $content, $attributes, $expected_tag)
+    {
+        $this->assertSame($expected_tag, HtmlHelper::nonVoidTag($name, $content, $attributes));
+    }
 }