-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTag.php
More file actions
445 lines (418 loc) · 14.1 KB
/
Copy pathTag.php
File metadata and controls
445 lines (418 loc) · 14.1 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
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
<?php
/**
* Html Tag Constructor
*
* @author Konstantin Kutsevalov
* @email <konstantin@act-prog.ru>
*/
namespace adamasantares\html;
if (function_exists('\adamasantares\html\tg') || class_exists('\adamasantares\html\Tag')) return;
class Tag {
/**
* @var integer
*/
private $level = 0;
/**
* Single tags
* @var array
*/
private static $single = ['area', 'base', 'basefont', 'bgsound', 'br', 'col', 'command', 'embed', 'hr', 'img',
'input', 'isindex', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
/**
* @var string
*/
private $tagName = 'div';
/**
* @var array
*/
private $contents = [];
/**
* @var array
*/
private $classesNames = [];
/**
* @var array
*/
private $attributes = [];
/**
* Creates new tag object
* @param string|array $properties <p>
* This is complex argument. It may describes tag name, classes names, "id", "rel" attribute, "type" attribute
* and name of attribute for $content (if a tag is single).
* Example 1: "script #jquery !text/javascript %src" or "script#jquery!text/javascript%src"
* there are:
* "script" - is tag name
* "#jquery" - is value for "id" attribute
* "!text/javascript" - is value for "type" attribute
* "%src" - is name of attribute for $content
* Example 2: "link @icon !image/x-icon %href" or "link@icon!image/x-icon%href"
* there are:
* "link" - is tag name
* "@icon" - is value for "rel" attribute
* "!image/x-icon" - is value for "type" attribute
* "%href" - is name of attribute for $content
* Example 3: tg(['a.button', 'href' => '/url/path'], tg(['img.my-img%src', 'alt' => 'just-a-link'], '/image.png'));
*
* ```<a class="button" href="/url/path"><img src="/image.png" alt="just-a-link" class="my-img"></a>```
*
* Example 4: tg("input!text^option '123'"); // last element of string argument wrapped by ' is value or content
*
* ```<input type="text" name="option" value="123">```
*
* Example 5: tg(".super.puper.element 'My content'"); // by default a tag name is "DIV"
*
* ```<div class="super puper element">My content</div>```
*
* Tokens: "#id", ".class", "^nameAttributeValue", "@relAttributeValue", "!typeAttributeValue", "%contentAttributeName"
* </p>
* @param mixed $children <p>
* For single tags (like input, meta, img, etc..) this may be value for special
* attribute that defined in $properties argument via "%" char
* tg('script@text/javascript%src', '/js/script.js');
* in this example the "%src" is the name of attribute that will get $content value
* </p>
*/
public function __construct($properties, $children = null)
{
// 'tagName .class.class.class #id ^nameAttr @rel !type %contentAttribute'
$idString = '';
if (is_array($properties)) {
// parse attributes
foreach ($properties as $property => $value) {
if ($property == '0' || (!is_string($value) && !is_numeric($value))) continue;
if (gettype($property) == 'integer') {
$this->attributes[$value] = '';
} else {
$this->attributes[$property] = $value;
}
}
if (isset($properties[0])) {
$idString = $properties[0];
}
} else {
$idString = $properties;
}
// let's check last part for content/text
if (preg_match_all('/\'(.*)\'/', $idString, $matches)) {
$content = $matches[1][0];
$idString = str_replace("'{$content}'", '', $idString);
if (!empty($children)) {
if (is_array($children)) {
$children = array_merge([$content], $children);
} else if (is_string($children)) {
$children = [
$content,
$children
];
}
} else {
$children = $content;
}
}
// name, class, id, etc.
// Tokens: "#id", ".class", "^nameAttributeValue", "@relAttributeValue", "!typeAttributeValue", "%contentAttributeName"
$idString = str_replace(['#', '.', '^', '@', '!', '%', ':', ' '], [' #', ' .', ' ^', ' @', ' !', ' %', ' :', ' '], $idString);
$idString = explode(' ', trim($idString));
$idString = array_filter($idString, function($v){
return !empty($v);
});
foreach ($idString as $token) {
$char = substr($token, 0, 1);
switch ($char) {
case '#':
$this->id(substr($token, 1)); break;
case '.':
$this->addCls(substr($token, 1)); break;
case '^':
$this->attributes['name'] = substr($token, 1); break;
case '@':
$this->attributes['rel'] = substr($token, 1); break;
case '!':
$this->attributes['type'] = substr($token, 1); break;
case ':':
$this->attributes[ substr($token, 1) ] = '';
break;
case '%':
if (is_string($children) || is_numeric($children)) {
$this->attributes[substr($token, 1)] = htmlspecialchars($children);
$children = ''; // reset content
}
break;
default:
$this->tagName = $token;
}
}
// contents
if (!in_array($this->tagName, self::$single)) {
// tag pair
if ($this->tagName == 'select' && is_array($children)) {
$this->contents = $this->generateSelectOptions($children);
} else if (($this->tagName == 'ul' || $this->tagName == 'ol') && is_array($children)) {
foreach ($children as $item) {
if (is_string($item) || is_numeric($item)) {
$this->contents[] = tg('li', $item);
} else {
$this->contents[] = $item;
}
}
} else {
if (!empty($children)) {
if (!is_array($children)) {
$children = [$children];
}
$this->contents = $children;
}
if ($this->tagName == 'a' && !isset($this->attributes['title'])) { // fix "title" of LINK
$this->attributes['title'] = '';
} else if ($this->tagName == 'img' && !isset($this->attributes['alt'])) { // fix "alt" of IMG
$this->attributes['alt'] = '';
}
}
} else {
// single tag
if (is_string($children) || is_numeric($children)) {
if ($this->tagName == 'input') {
$this->attributes['value'] = $children;
} else if ($this->tagName == 'img') {
$this->attributes['src'] = $children;
if (!isset($this->attributes['alt'])) { $this->attributes['alt'] = ''; } // fix "alt" of "img"
} else if ($this->tagName == 'meta') {
$this->attributes['content'] = $children;
} else if ($this->tagName == 'link') {
$this->attributes['href'] = $children;
}
}
}
}
/**
* Getter and Setter for tag ID
* @param string $id
* @return $this|string If used as setter then will be returned current Tag object
*/
public function id($id = null)
{
if ($id === null) {
return $this->attr('id');
}
$this->attr('id', $id);
return $this;
}
/**
* Getter and Setter for tag attributes
* @param $name
* @param string $value
* @return $this|string If used as setter then will be returned current Tag object
*/
public function attr($name, $value = null)
{
// getter
if ($value === null) {
return isset($this->attributes[$name]) ? $this->attributes[$name] : '';
}
// setter
if (is_object($value)) {
$value = @get_class($value);
} else if (is_array($value)) {
$value = array_filter($value, function($v) {
return (is_string($v) || is_numeric($v)) ? true : false;
});
if ($name != 'class') {
$value = implode(' ', $value);
}
}
if ($name == 'class') {
$this->addCls($value);
} else {
$this->attributes[$name] = $value;
}
return $this;
}
/**
* Getter and Setter for tag name
* @param string $name
* @return $this|string If used as setter then will be returned current Tag object
*/
public function name($name = null)
{
// getter
if ($name === null) {
return $this->tagName;
}
// setter
if (is_string($name)) {
$this->tagName = $name;
}
return $this;
}
/**
* Add class name to tag
* @param string|array $name
* @return $this
*/
public function addCls($name)
{
if (is_string($name)) {
$name = explode(' ', trim($name));
}
if (is_array($name)) {
foreach ($name as $value) {
if (!in_array($value, $this->classesNames)) {
$this->classesNames[] = $value;
}
}
}
return $this;
}
/**
* Deletes class name of tag
* @param string|array $name
* @return $this
*/
public function delCls($name)
{
if (is_string($name)) {
$name = explode(' ', trim($name));
}
if (is_array($name)) {
foreach ($name as $value) {
$key = array_search($value, $this->classesNames);
if ($key !== false) {
unset($this->classesNames[$key]);
}
}
}
return $this;
}
/**
* Checks class name of tag
* @param string $name
* @return boolean
*/
public function isCls($name)
{
return array_search($name, $this->classesNames) === false ? false : true;
}
/**
* Appends content
* @param $content
* @return $this
*/
public function append($content)
{
if ((is_object($content) && get_class($content) == get_class($this)) || is_string($content) || is_numeric($content)) {
$this->contents[] = $content;
}
return $this;
}
/**
* Clears all content
* @return $this
*/
public function clear()
{
$this->contents = [];
return $this;
}
/**
* @param $options <p>
* [
* '1' => 'Cat',
* '20' => ['Dog', 'selected'],
* '58' => ['Hamster', 'disabled']
* ]
* </p>
* @return array
*/
private function generateSelectOptions($options = [])
{
$tags = [];
if (is_array($options)) {
foreach ($options as $value => $properties) {
if (!is_array($properties)) {
$properties = [$properties];
}
$title = empty($properties[0]) ? '---' : $properties[0];
unset($properties[0]);
$properties = array_merge(['option'], $properties, ['value' => $value]);
$tags[] = new self($properties, $title);
}
}
return $tags;
}
/**
* Set element's level
* @param $level
* @return $this
*/
public function setLevel($level)
{
$this->level = $level;
return $this;
}
/**
* Search elements
* @param $query
*/
public function query($query)
{
// TODO query
// delegation to helper TagQuery's methods
}
public function __toString()
{
$offset = str_repeat("\t", $this->level);
$tag = '';
$this->tagName = strtolower($this->tagName);
if ($this->tagName == 'html5') {
$tag .= "<!DOCTYPE html>\n";
$this->tagName = 'html';
}
$tag .= $offset . '<' . $this->tagName;
// attributes
foreach ($this->attributes as $attr => $value) {
if (trim($value) == '') {
$tag .= ' ' . $attr; // empty attribute
} else {
$tag .= ' ' . $attr . '="' . htmlspecialchars($value) . '"';
}
}
// classes
if (!empty($this->classesNames)) {
$tag .= ' class="';
foreach ($this->classesNames as $i => $cls) {
$tag .= ($i == 0 ? '' : ' ') . $cls;
}
$tag .= '"';
}
$tag .= '>';
if (!in_array($this->tagName, self::$single)) {
// if tag pair
foreach ($this->contents as $content) {
if (!empty($content)) {
if (is_string($content) || is_numeric($content)) {
if ($content == 'br/' || $content == 'hr/') { // special marks for short tags
$content = '<' . substr($content, 0, 2) . '>';
}
$tag .= $content;
} else if (is_object($content) && get_class($content) == get_class($this)) {
$tag .= "\n";
$content->setLevel($this->level + 1);
$tag .= $content->__toString();
}
}
}
$tag .= "\n" . $offset . '</' . $this->tagName . '>';
}
return $tag;
}
}
/**
* Alias for create tag object
* @param string|array $properties
* @param mixed $children It can be a String, Tag or Tag[]
* @return Tag
*/
function tg($properties, $children = null)
{
return new Tag($properties, $children);
}