-
Notifications
You must be signed in to change notification settings - Fork 0
/
Html.php
490 lines (428 loc) · 13.1 KB
/
Html.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/**
* Like SimpleXMLElement, but work with HTML.
*
* TODO :
* 1) Complete all element parsers with useElementNameAsTagName;
* 2) Make revers-operation - HTML-code into this object;
* 3) Make possible parallel creating of unlimited HTML-fragments;
* @author Se#
*/
class Evil_Html
{
/**
* Known tags
*/
const TEXT = 'text';
const DIV = 'div';
const LI = 'li';
const SELECT = 'select';
const OPTION = 'option';
const TABLE = 'table';
const TR = 'tr';
const TD = 'td';
const INPUT = 'input';
/**
* Default tag
* @var string
*/
public static $defaultTag = 'text';
/**
* Use default tag if option 'tag' in element is missed.
* @var boolean
*/
public static $useDefaultTagIfItMissed = true;
/**
* Use element name (first parameter in the __construct) as tag attribute.
* @var boolean
*/
public static $useElementNameAsTagName = true;
/**
* Contain all information for current HTML-creation
* @var array of arrays
*/
protected static $_code = null;
/**
* Head element's name
* @var string
*/
protected static $_head = '';
/**
* Parent element's name
* @var string
*/
protected $_parent = null;
/**
* Current element's name
* @var string
*/
protected $_current = null;
/**
* Current element's options (attributes)
* @var mixed
*/
protected $_currentOptions = null;
/**
* Result HTML
* @var string
*/
protected $_result = '';
/**
* If tag's type is missed, use default tag.
* Attr $adding will be used for parallel creating of different HTML-code.
*
* @param string $name
* @param string,array $options
* @param string $tagType default null
* @param string $parent default null
*/
public function __construct($name = '', $options = null, $tagType = null, $parent = null, $adding = false)
{
if(is_null($tagType) && empty($options)){
$tagType = $name;
$name = $this->_generateName();
$options = array();
}
if(is_null($tagType))
$tagType = self::$defaultTag;
if(empty($name))
$name = $this->_generateName();
if(is_string($tagType)){// we don't know how to work with not-a-string
if(empty(self::$_head)){// check, if we are crerating head-element
self::$_head = $name;
self::$_code = array(); // new HTML-code is started
}
if(is_null($parent))// does current element is a child?
self::$_code = array($name => array('tag' => $tagType, 'options' => $options));
else
self::$_code[$parent]['childs'][] = array($name => array('tag' => $tagType, 'options' => $options));
$this->_setCurrent($name);// save current element's name
$this->_setCurrentOptions($options); // save current element's options for comfort extracting
}
}
protected function _generateName()
{
return sha1(mt_rand(1,9999) . time() . mt_rand(0,99999)) . md5(mt_rand(0,99999));
}
public function __toString()
{
echo ' === START === <br/>' . self::$_head . ':';
print_r(self::$_code);
return ' === END === ';
}
/**
* Add child for current element.
* @param string $name
* @param string, array $options
* @param string $htmlTagType default null
* @return self
*/
public function addChild($name, $options = null, $htmlTagType = null)
{
if(is_null($options) && is_null($htmlTagType)){
$htmlTagType = $name;
$options = array();
$name = '';
}
$child = new self($name, $options, $htmlTagType, $this->_current, true);
$child->_setParent($this->_current);
return $child;
}
/**
* Append child to the current parent.
*
* @param string $name
* @param mixed $options
* @param string $htmlTagType default null
* @return self
*/
public function append($name, $options = null, $htmlTagType = null)
{
return new self($name, $options, $htmlTagType, $this->_parent, true);
}
/**
* Generate HTML-code by current state of code
* @return string
*/
public function asHtml()
{
$this->_result = '';
// Begin from the head
$this->_forEach(self::$_head, self::$_code[self::$_head]);
self::$_head = null;
return $this->_result;
}
/**
* Parses elements to create an HTML-code.
*
* @param string $name
* @param array $options
*/
protected function _forEach($name, $options)
{
if(isset(self::$_code[$name])){// it means current element if a child
if($name != self::$_head){// head has already childs in it's options
$options = array_merge($options, self::$_code[$name]);
unset(self::$_code[$name]);// to not operate element twice
}
}
if(!is_array($options))// we don't know how to operate not-an-array
throw new Exception($name);
if(!isset($options['tag'])){// tag is missed
if(self::$useDefaultTagIfItMissed)// use default
$options['tag'] = self::$defaultTag;
else
throw new Exception(' Incorrect element "' . $name . '" ');
}
$method = '_tag' . ucfirst($options['tag']);// create a method name for current tag
if(method_exists($this, $method))
$this->_result .= $this->$method(true, $options['options']);
else
throw new Exception(' Unknown tag "' . $options['tag'] . '" ');
/**
* TODO : make it more correct - reduce cicles
*/
if(isset($options['childs'])){// current element has a children
foreach($options['childs'] as $index=>$childConfig){
foreach($options['childs'][$index] as $childName => $childOptions){
$this->_forEach($childName, $childOptions);
break;
}
}
}
$this->_result .= $this->$method(false, $options['options']);// pass options, as we can realise post-options in the future
}
/**
* Realise formating and creating simple open-close tag (may with a text).
*
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _simpleTag($name, $start, $options, $hasText = false)
{
if(false == $start)
return '</' . $name . '>';
$text = '';
if($hasText){
if(is_string($options))
$text = $options;
elseif(isset($options['text'])){
$text = $options['text'];
unset($options['text']);
}
}
$result = '<' . $name . ' ';
if(is_array($options)){
if(!isset($options['name']) && self::$useElementNameAsTagName)
$options['name'] = $name;
foreach($options as $attr => $value){
$attr = (string) $attr;
$value = (string) $value;
$result .= $attr . '="' . $value . '" ';
}
}
return $result .= '>' . $text;
}
/**
* Realise formating and creating TABLE-tag.
*
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagTable($start, $options = null)
{
return $this->_simpleTag(self::TABLE, $start, $options);
}
/**
* Realise formating and creating TR-tag.
*
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagTr($start, $options = null)
{
return $this->_simpleTag(self::TR, $start, $options);
}
/**
* Realise formating and creating TD-tag.
*
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagTd($start, $options)
{
return $this->_simpleTag(self::TD, $start, $options, true);
}
protected function _tagInput($start, $options)
{
if(false == $start)
return '';
$result = '<input ';
foreach($options as $attr => $value){
$attr = (string) $attr;
$value = (string) $value;
if( ('readonly' == $attr) && empty($value) )
continue;
$result .= $attr . '="' . $value . '" ';
}
return $result . '/>';
}
/**
* Realise formating and creating SELECT-tag.
*
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagSelect($start, $options = null)
{
if(false == $start)
return '</select>';
$result = '<select ';
$hasOptions = false;
if(!is_array($options))
return '';
foreach($options as $attr => $value){
if(self::OPTION == $attr){
$hasOptions = true;
continue;
}
$attr = (string) $attr;
$value = (string) $value;
$result .= $attr . '="' . $value . '" ';
}
$result .= '>';
if($hasOptions){
$list = $options['options'];
foreach($list as $option){
$result .= $this->_tagOption(true, $option);
$result .= $this->_tagOption(false);
}
}
return $result;
}
/**
* Realise formatting and creating OPTION-tag.
*
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagOption($start, $options = null)
{
if(false == $start)
return '</option>';
$result = '';
if(!is_array($options))
return '';
$result .= '<option ';
if(isset($options['text'])){
$text = $options['text'];
unset($options['text']);
}
else
$text = '';
foreach($options as $attr => $value)
$result .= $attr . '="' . $value . '" ';
$result .= '>' . $text;
return $result;
}
/**
* Realise formating and creating LI-tag.
*
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagLi($start, $options = null)
{
if(false == $start)
return '</li>';
$text = '';
$attrs = '';
if(is_string($options))
$text = $options;
elseif(is_array($options)){
$attrs .= ' ';
if(isset($options['text'])){
$text = $options['text'];
unset($options['text']);
}
foreach($options as $attr => $value)
$attrs .= $attr . '="' . $value . '" ';
}
return '<li'.$attrs.'>' . $text;
}
/**
* Realise formating and creating simple text.
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagText($start, $options = null)
{
if(false == $start)
return '';
$result = '';
if(is_string($options))
return $options;
foreach($options as $value)
$result .= $value;
return $result;
}
/**
* Realise formating and creating DIV-tag
* @param boolean $start
* @param mixed $options
* @return string
*/
protected function _tagDiv($start, $options = null)
{
if(false == $start)
return '</div>';
$result = '<div ';
foreach($options as $attr => $value){
if('childs' == $attr)
continue;
$result .= $attr . '="' . $value . '" ';
}
$result .= '>';
return $result;
}
/**
* Set parent for current element
* @param string $name
*/
protected function _setParent($name)
{
$this->_parent = $name;
}
/**
* Set name of a current element
* @param string $name
*/
protected function _setCurrent($name)
{
$this->_current = $name;
}
/**
* Set options (attributes) for current element
* @param mixed $options
*/
protected function _setCurrentOptions($options)
{
$this->_currentOptions = $options;
}
/**
* Return options (attributes) of a current element
* @return mixed
*/
public function getAttrs()
{
return $this->_currentOptions;
}
}