1 | <?php |
||||
2 | |||||
3 | namespace SilverStripe\View; |
||||
4 | |||||
5 | use InvalidArgumentException; |
||||
6 | use SilverStripe\Core\Config\Configurable; |
||||
7 | use SilverStripe\Core\Convert; |
||||
8 | |||||
9 | /** |
||||
10 | * HTML Helper class |
||||
11 | */ |
||||
12 | class HTML |
||||
13 | { |
||||
14 | use Configurable; |
||||
15 | |||||
16 | /** |
||||
17 | * List of HTML5 void elements |
||||
18 | * |
||||
19 | * @see https://www.w3.org/TR/html51/syntax.html#void-elements |
||||
20 | * @config |
||||
21 | * @var array |
||||
22 | */ |
||||
23 | private static $void_elements = [ |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
24 | 'area', |
||||
25 | 'base', |
||||
26 | 'br', |
||||
27 | 'col', |
||||
28 | 'embed', |
||||
29 | 'hr', |
||||
30 | 'img', |
||||
31 | 'input', |
||||
32 | 'keygen', |
||||
33 | 'link', |
||||
34 | 'menuitem', |
||||
35 | 'meta', |
||||
36 | 'param', |
||||
37 | 'source', |
||||
38 | 'track', |
||||
39 | 'wbr' |
||||
40 | ]; |
||||
41 | |||||
42 | /** |
||||
43 | * List of attributes that should be rendered even if they contain no value |
||||
44 | * |
||||
45 | * @config |
||||
46 | * @var array |
||||
47 | */ |
||||
48 | private static $legal_empty_attributes = [ |
||||
0 ignored issues
–
show
|
|||||
49 | 'alt', |
||||
50 | ]; |
||||
51 | |||||
52 | /** |
||||
53 | * Construct and return HTML tag. |
||||
54 | * |
||||
55 | * @param string $tag |
||||
56 | * @param array $attributes |
||||
57 | * @param string $content Content to use between two tags. Not valid for void elements (e.g. link) |
||||
58 | * @return string |
||||
59 | */ |
||||
60 | public static function createTag($tag, $attributes, $content = null) |
||||
61 | { |
||||
62 | $tag = strtolower($tag); |
||||
63 | |||||
64 | // Build list of arguments |
||||
65 | $legalEmptyAttributes = static::config()->get('legal_empty_attributes'); |
||||
66 | $preparedAttributes = ''; |
||||
67 | foreach ($attributes as $attributeKey => $attributeValue) { |
||||
68 | $whitelisted = in_array($attributeKey, $legalEmptyAttributes); |
||||
69 | |||||
70 | // Only set non-empty strings (ensures strlen(0) > 0) |
||||
71 | if (strlen($attributeValue) > 0 || $whitelisted) { |
||||
72 | $preparedAttributes .= sprintf( |
||||
73 | ' %s="%s"', |
||||
74 | $attributeKey, |
||||
75 | Convert::raw2att($attributeValue) |
||||
0 ignored issues
–
show
It seems like
SilverStripe\Core\Conver...aw2att($attributeValue) can also be of type array and array ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
76 | ); |
||||
77 | } |
||||
78 | } |
||||
79 | |||||
80 | // Check void element type |
||||
81 | if (in_array($tag, static::config()->get('void_elements'))) { |
||||
82 | if ($content) { |
||||
83 | throw new InvalidArgumentException("Void element \"{$tag}\" cannot have content"); |
||||
84 | } |
||||
85 | return "<{$tag}{$preparedAttributes} />"; |
||||
86 | } |
||||
87 | |||||
88 | // Closed tag type |
||||
89 | return "<{$tag}{$preparedAttributes}>{$content}</{$tag}>"; |
||||
90 | } |
||||
91 | } |
||||
92 |