-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHtmlHelper.php
70 lines (65 loc) · 1.95 KB
/
HtmlHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
// no direct access
defined( '_JEXEC' ) or die;
/**
* Helper class to build HTML strings.
*/
class HtmlHelper
{
/**
* valid HTML 5 tags
*/
private static $HTML5_TAGS = [
// semantic and structural elements
'article', 'aside', 'details', 'dialog', 'figcaption', 'figure', 'footer', 'header', 'main', 'mark',
'menuitem', 'meter', 'nav', 'progress', 'rp', 'rt', 'ruby', 'section', 'summary', 'time',
// text-level elements
'bdi', 'wbr',
// form elements, graphics and media elements
'datalist', 'keygen', 'output', 'canvas', 'svg', 'audio', 'embed', 'picture', 'source', 'track', 'video',
];
/**
* Checks whether a tag is a HTML 5 tag.
*
* @param string $tagName tag name
* @return bool true if the tag is a valid HTML 5 tag, false otherwise
*/
public static function isHtml5Tag( $tagName )
{
return in_array( $tagName, self::$HTML5_TAGS );
}
/**
* Builds the HTML string of a tag with attributes but no children.
*
* @param string $tagName tag name
* @param array $attributes attributes as associative array
* @return string HTML code representing the specified tag
*/
public static function buildSimpleTag( $tagName, $attributes )
{
$html = "<$tagName";
foreach ($attributes as $key => $value)
{
$html .= " $key" . ($value !== null ? "=\"$value\"" : '');
}
return $html . '/>';
}
/**
* Returns the attributes of a node.
*
* @param DOMNode $node node
* @return array attributes of the specified node
*/
public static function getNodeAttributes( &$node )
{
$attributes = [];
if ($node->hasAttributes())
{
foreach ($node->attributes as $attr)
{
$attributes[$attr->nodeName] = $attr->nodeValue;
}
}
return $attributes;
}
}