-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml5.php
More file actions
373 lines (320 loc) · 8.4 KB
/
html5.php
File metadata and controls
373 lines (320 loc) · 8.4 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
function __autoload( $class )
{
require_once( strtolower( $class ) . ".php" );
}
/**
* HTML5 Helper Class.
*
* Simplifies the construction of HTML5 markup in PHP.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright Copyright 2011, David Frey
* @package HTML5_Helper
* @since v 0.1
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class HTML5
{
/**
* Attribute Order.
*
* A sequential list of html tag attributes for code vanity.
*
* @var array
* @access public
*/
public $attribute_order = array(
'type',
'name',
'href',
'src',
'for',
'rel',
'id',
'class'
);
/**
* Create an HTML5 tag.
*
* Wraps optional content between start and end tags. If content is ommited
* a self-closing tag is created.
*
* @access public
* @param string $name
* @param array $attributes
* @param string $content
*
* @uses start_tag()
* @uses end_tag()
* @return string complete tag
*/
public function tag( $name, $attributes = array(), $content = null, $options = array() )
{
$format = ( isset( $options['format'] ) ) ? $options['format'] : 'block';
$markup = '';
if ( 'inline' == $format ) $markup .= "\t"; //vanity hack
$markup .= $this->auto_format( $this->start_tag( $name, $attributes ), $format );
if ( isset( $content) )
{
$markup .= $this->auto_format( $content, $format );
$markup .= $this->auto_format( $this->end_tag( $name ), $format )."\n";
}
return $markup;
}
/**
* Creates the opening part of a tag.
*
* @access private
* @param string $name the tag name (e.g. a, img, input, textarea, etc.).
* @param array $attributes (optional) names and values of tag attributes
* @return string tag
*
* @todo Make sure all attributes are unique and not repeated
*/
private function start_tag( $name, $attributes = array() )
{
return "<$name".$this->stringify_attributes( $this->attribute_sorter( $attributes ) ).">";
}
/**
* Creates the closing part of a tag.
*
* @access private
* @param string $name the tag name (e.g. a, img, input, textarea, etc.).
* @return string closing tag
*/
private function end_tag( $name )
{
return "</$name>";
}
/**
* Titleizes a string
*
* @param string $string
* @return string reformated with words uppercased
*/
function to_label( $string )
{
/** todo: add a lot more patterns */
$string = str_replace( '-', ' ', $string );
$string = str_replace( '_', ' ', $string );
return ucwords( $string );
}
/**
* Form tag
*
* @param string $action
* @param array $fields
* @param array $attributes
* @return string valid label tag
*/
function form( $action, $fields = array(), $attributes = array() )
{
$attributes['action'] = ( isset( $action ) ) ? $action : '/';
$attributes['id'] = ( isset( $attributes['id'] ) ) ? $attributes['id'] : "form_".time();
$attributes['method'] = ( isset( $attributes['method'] ) ) ? $attributes['method'] : 'post';
return $this->tag( 'form', $attributes, $fields );
}
/**
* Label tag
*
* @param string $label
* @param string $id
* @param array $attributes
* @return string valid label tag
*/
function label( $label, $id, $attributes = array() )
{
$attributes['for'] = $id;
return $this->tag( 'label', $attributes, $label, array( 'format' => 'inline' ) );
}
/**
* Input tag
*
* @param string $name
* @param string $value
* @param string $type
* @param array $attributes
* @return string valid input tag
*/
function input( $name, $value = null, $type = 'text', $attributes = array() )
{
$attributes['type'] = $type;
$attributes['name'] = $name;
if ( isset( $value ) )
{
$attributes['value'] = $value;
}
return $this->tag( 'input', $attributes );
}
/**
* Select tag
*
* @param string $name
* @param string $value
* @param array $collection
* @param array $attributes
* @return string valid select tag with options
*/
function select( $name, $value = null, $collection = array(), $attributes = array() )
{
$attributes['name'] = $name;
$collection = $this->normalize_collection( $collection );
$options = array();
foreach ( $collection as $item['value'] => $item['name'] )
{
$option_attributes = array();
$option_attributes['value'] = $item['value'];
if ( isset( $value ) && $item['value'] == $value )
{
$option_attributes['selected'] = 'selected';
}
$options[] = $this->tag( 'option', $option_attributes, $item['name'], array( 'format' => 'inline' ) );
}
return $this->tag( 'select', $attributes, join( "\n", $options ) );
}
/**
* Textarea tag
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string valid textarea tag
*/
function textarea( $name, $value = null, $attributes = array() )
{
$attributes['name'] = $name;
return $this->tag( 'textarea', $attributes, $value, array( 'format' => 'inline' ) );
}
/**
* Wrap your content with an inline or block element
*
* @param string $content
* @param string $attributes
* @param array $options
* @return string content wrapped with <span> or <div>
*/
function wrap( $content, $attributes = array(), $options = array() )
{
if ( ! isset( $options['format'] ) ) $options['format'] = 'block';
switch( $options['format'] )
{
case "block":
$markup = $this->tag( 'div', $attributes, $content, $options );
break;
case "inline":
$markup = $this->tag( 'span', $attributes, $content, $options );
break;
default:
$markup = $content;
}
return $markup;
}
/**
* Sort tag attributes.
*
* Allows you to sort tag attributes to produce consistently formatted HTML
* for vanity purposes.
*
* @access private
* @param array $attributes names and values of tag attributes
* @uses $attribute_order compares $attribute_order with $attributes
* @since v 0.1
* @return array resorted array
*/
private function attribute_sorter( $attributes )
{
if ( ! is_array( $attributes ) ) return false;
$common = array();
foreach ( $this->attribute_order as $attribute )
{
if ( isset( $attributes[$attribute] ) )
{
$common[$attribute] = $attributes[$attribute];
}
}
$custom = array_diff_key( $attributes, $common );
return array_merge( $common, $custom );
}
/**
* Converts associative array into tag attributes.
*
* Takes an associative array and produces a $key="$value" pattern to be
* used when generating an HTML tag.
*
* @access private
* @param array $attributes names and values of tag attributes
* @since v 0.1
* @return string attributes in a $key="$value" pattern
*/
public function stringify_attributes( $attributes = array() )
{
$string = '';
if ( is_array( $attributes ) && count( $attributes ) > 0 )
{
foreach( $attributes as $key => $value )
{
$string .= ' '.$key.'="'.$value.'"';
}
}
return $string;
}
public function auto_format( $code, $format)
{
switch( $format )
{
case "inline":
$indent = '';
$line_break = '';
break;
default:
$indent = "\t";
$line_break = "\n";
break;
}
$markup = '';
foreach( preg_split( "/\r?\n/", $code ) as $line )
{
if ( strlen( $line ) > 0 ) $markup .= $indent . $line . $line_break;
}
return $markup;
}
/**
* Ensures collection is made up of key/value pairs.
*
* Converts indexed arrays into associative arrays.
*
* @access private
* @param array $attributes names and values of tag attributes
* @since v 0.1
* @return string attributes in a $key="$value" pattern
*/
private function normalize_collection( $array )
{
$collection = array();
if ( array_merge( $array ) === $array && is_numeric( implode( array_keys( $array ) ) ) )
{
foreach( $array as $item )
{
$collection[$item] = $item;
}
}
else
{
$collection = $array;
}
return $collection;
}
}
?>