-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHTML5Tags.body.php
170 lines (139 loc) · 5.04 KB
/
HTML5Tags.body.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Helper class for implementing core functionality
*
* @author Leo Wallentin
*/
class HTML5Tags {
/* Sets all hooks on ParserFirstCallInit
TODO: Should probably check for each and everyone that they are not already set */
public static function setHooks ( $parser ) {
$parser->setHook( 'article', array( 'HTML5Tags', 'tagArticle' ) );
$parser->setHook( 'aside', array( 'HTML5Tags', 'tagAside' ) );
$parser->setHook( 'details', array( 'HTML5Tags', 'tagDetails' ) );
$parser->setHook( 'figure', array( 'HTML5Tags', 'tagFigure' ) );
$parser->setHook( 'figcaption', array( 'HTML5Tags', 'tagFigcaption' ) );
$parser->setHook( 'hgroup', array( 'HTML5Tags', 'tagHgroup' ) );
$parser->setHook( 'mark', array( 'HTML5Tags', 'tagMark' ) );
$parser->setHook( 'nav', array( 'HTML5Tags', 'tagNav' ) );
/* Do not register "section" if LabeledSectionTransclusion is installed */
if ( !class_exists( 'LabeledSectionTransclusion' ) ) {
$parser->setHook( 'section', array( 'HTML5Tags', 'tagSection' ) );
}
$parser->setHook( 'summary', array( 'HTML5Tags', 'tagSummary' ) );
$parser->setHook( 'time', array( 'HTML5Tags', 'tagTime' ) );
$parser->setHook( 'footer', array( 'HTML5Tags', 'tagFooter' ) );
$parser->setHook( 'header', array( 'HTML5Tags', 'tagHeader' ) );
return true;
}
/* This function takes all attributes that are allowed fot all HTML5 tags,
removes them from args and returns them */
private static function extractGlobalAttributes ( &$args ) {
$allowedGlobalAttributes = array (
'accesskey',
'hidden',
'itemtype',
'class',
'id',
'lang',
'contenteditable',
'inert',
'spellcheck',
'contextmenu',
'itemid',
'style',
'dir',
'itemprop',
'tabindex',
'draggable',
'itemref',
'title',
'dropzone',
'itemscope',
'translate',
'aria-hidden',
);
$returnGlobalAttributes = array ();
foreach ( $args as $k => $v ) {
if ( in_array( $k, $allowedGlobalAttributes ) || ( strpos( $k, 'data-' ) !== false ) ) {
$returnGlobalAttributes[$k] = $v;
unset ( $args[$k] );
}
}
return $returnGlobalAttributes;
}
/* Create an opening tag with attributes */
private static function createOpeningTag ( $name, $args, $selfClosing = false ) {
$html = "<$name";
if ( !empty( $args ) ) {
foreach( $args as $k => $v ) {
$html .= " $k='$v'";
}
}
if ( $selfClosing )
$html .= '/';
$html .= '>';
return $html;
}
/* Shorthand function for the simplest possible tag function */
private static function html ( $tag, $input, $args, $parser, $frame ) {
$args = self::extractGlobalAttributes( $args );
if ( !empty( $args ) ) {
foreach( $args as $k => &$v ) {
$k = htmlspecialchars( $k );
$v = htmlspecialchars( $parser->recursiveTagParse( $v, $frame ) );
}
}
$start = self::createOpeningTag( $tag, $args );
$input = $parser->recursiveTagParse ( $input, $frame ); //This does not run Parser::preSaveTransform
return "$start$input</$tag>";
}
public static function tagArticle ( $input, $args, $parser, $frame ) {
return self::html( 'article', $input, $args, $parser, $frame);
}
public static function tagAside ( $input, $args, $parser, $frame ) {
return self::html( 'aside', $input, $args, $parser, $frame);
}
public static function tagDetails ( $input, $args, $parser, $frame ) {
return self::html( 'details', $input, $args, $parser, $frame);
}
public static function tagFigcaption ( $input, $args, $parser, $frame ) {
return self::html( 'figcaption', $input, $args, $parser, $frame);
}
public static function tagFigure ( $input, $args, $parser, $frame ) {
return self::html( 'figure', $input, $args, $parser, $frame);
}
public static function tagFooter ( $input, $args, $parser, $frame ) {
return self::html( 'footer', $input, $args, $parser, $frame);
}
public static function tagHeader ( $input, $args, $parser, $frame ) {
return self::html( 'header', $input, $args, $parser, $frame);
}
public static function tagHgroup ( $input, $args, $parser, $frame ) {
return self::html( 'hgroup', $input, $args, $parser, $frame);
}
public static function tagMark ( $input, $args, $parser, $frame ) {
return self::html( 'mark', $input, $args, $parser, $frame);
}
public static function tagNav ( $input, $args, $parser, $frame ) {
return self::html( 'nav', $input, $args, $parser, $frame);
}
public static function tagSection ( $input, $args, $parser, $frame ) {
return self::html( 'section', $input, $args, $parser, $frame);
}
public static function tagSummary ( $input, $args, $parser, $frame ) {
return self::html( 'summary', $input, $args, $parser, $frame);
}
public static function tagTime ( $input, $args, $parser, $frame ) {
$finalArgs = self::extractGlobalAttributes( $args );
foreach ( $args as $k => $v ) {
if ( 'datetime' === $k ) {
$finalArgs['datetime'] = htmlspecialchars( $v );
}
}
$start = self::createOpeningTag( 'time', $finalArgs );
$input = $parser->recursivePreprocess ( $input, $frame );
$input = $parser->recursiveTagParse ( $input, $frame );
return "$start$input</time>";
}
}