Browse Source

added HtmlHelper::voidTag()

Fabian Peter Hammerle 7 years ago
parent
commit
ceafb01f91
2 changed files with 86 additions and 13 deletions
  1. 38 13
      HtmlHelper.php
  2. 48 0
      tests/HtmlHelperTest.php

+ 38 - 13
HtmlHelper.php

@@ -9,6 +9,39 @@ class HtmlHelper
         return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE);
     }
 
+    private static function _renderTagAttributes(array $attributes = [])
+    {
+        return StringHelper::implode(' ', ArrayHelper::mapIfSet(
+            $attributes,
+            function($k, $v) {
+                if($v === true) {
+                    return sprintf('%s="%s"', $k, $k);
+                } elseif($v === false) {
+                    return null;
+                } else {
+                    return sprintf('%s="%s"', $k, self::encode($v));
+                }
+                }
+            ));
+    }
+
+    public static function voidTag($tag_name, array $attributes = [])
+    {
+        if($tag_name === null) {
+            return null;
+        } elseif(!is_string($tag_name)) {
+            throw new \TypeError(
+                sprintf('expected string or null as tag name, %s given', gettype($tag_name))
+                );
+        } else {
+            return sprintf(
+                '<%s%s />',
+                $tag_name,
+                StringHelper::prepend(' ', self::_renderTagAttributes($attributes))
+                );
+        }
+    }
+
     public static function startTag($tag_name, array $attributes = [])
     {
         if($tag_name === null) {
@@ -18,19 +51,11 @@ class HtmlHelper
                 sprintf('expected string or null as tag name, %s given', gettype($tag_name))
                 );
         } else {
-            $rendered_attributes = StringHelper::implode(' ', ArrayHelper::mapIfSet(
-                $attributes,
-                function($k, $v) {
-                    if($v === true) {
-                        return sprintf('%s="%s"', $k, $k); 
-                    } elseif($v === false) {
-                        return null; 
-                    } else {
-                        return sprintf('%s="%s"', $k, self::encode($v));
-                    }
-                    }
-                ));
-            return '<' . $tag_name . StringHelper::prepend(' ', $rendered_attributes) . '>';
+            return sprintf(
+                '<%s%s>',
+                $tag_name,
+                StringHelper::prepend(' ', self::_renderTagAttributes($attributes))
+                );
         }
     }
 

+ 48 - 0
tests/HtmlHelperTest.php

@@ -26,6 +26,54 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($expected, HtmlHelper::encode($string));
     }
 
+    public function voidTagTypeErrorProvider()
+    {
+        return [
+            [1, []],
+            [false, []],
+            ['tag', true],
+            ['tag', 'attr'],
+            ];
+    }
+
+    /**
+     * @dataProvider voidTagTypeErrorProvider
+     * @expectedException \TypeError
+     */
+    public function testVoidTagTypeError($name, $attributes)
+    {
+        HtmlHelper::voidTag($name, $attributes);
+    }
+
+    public function voidTagProvider()
+    {
+        return [
+            ['tag', [], '<tag />'],
+            ['void', ['a' => '1'], '<void a="1" />'],
+            ['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" />'],
+            ['void', ['a' => null, 'b' => '2'], '<void b="2" />'],
+            ['void', ['a' => true], '<void a="a" />'],
+            ['void', ['a' => true, 'b' => '2'], '<void a="a" b="2" />'],
+            ['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;)" />'],
+            [null, [], null],
+            [null, ['attr' => 'v'], null],
+            ];
+    }
+
+    /**
+     * @dataProvider voidTagProvider
+     */
+    public function testVoidTag($name, $attributes, $expected_tag)
+    {
+        $this->assertSame($expected_tag, HtmlHelper::voidTag($name, $attributes));
+    }
+
     public function startTagTypeErrorProvider()
     {
         return [