-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathTag.php
More file actions
507 lines (411 loc) · 11.8 KB
/
Copy pathTag.php
File metadata and controls
507 lines (411 loc) · 11.8 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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/*
* Textpattern Content Management System
* https://textpattern.com/
*
* Copyright (C) 2026 The Textpattern Development Team
*
* This file is part of Textpattern.
*
* Textpattern 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.
*
* Textpattern 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 Textpattern. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Base HTML component - a tag.
*
* @since 4.9.0
* @package UI
*/
namespace Textpattern\UI;
class Tag implements UIInterface
{
/**
* Global control over tag output.
*
* @var array
*/
static $flags = null;
/**
* The tag name.
*
* @var string
*/
protected $tag = null;
/**
* The key (id) used in the tag.
*
* @var string
*/
protected $key = null;
/**
* The tag's contained contents.
*
* @var string
*/
protected $content = null;
/**
* The tag's attributes as an Attribute object.
*
* @var \Textpattern\UI\Attribute
*/
protected $atts = null;
/**
* The tag properties, keyed via their index.
*
* @var array
*/
protected $properties = array();
/**
* The available schemes that can be used for tag output.
*
* @var array
*/
protected $schemes = array(
'xhtml',
'html5',
);
/**
* A constraint validator.
*
* @var \Textpattern\Validator\Validator
*/
protected $validator = null;
/**
* General constructor for the tag.
*
* @param string $tag The tag name
*/
public function __construct($tag)
{
if (self::$flags === null) {
self::$flags['boolean'] = 'html5';
self::$flags['self-closing'] = 'html5';
self::$flags['break'] = '';
self::$flags['break-on'] = '';
}
$this->setTag($tag);
$this->atts = new \Textpattern\UI\Attribute();
}
/**
* Set the tag to use. Chainable.
*
* @param string $tag The tag name
* @return this
*/
public function setTag($tag)
{
$this->tag = (string)$tag;
return $this;
}
/**
* Set the tag name (key). Chainable.
*
* @param string $key The tag's reference key (name)
* @return this
*/
public function setKey($key)
{
$this->key = (string)$key;
return $this;
}
/**
* Fetch the key (id) in use by this tag.
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Set the tag's contained content. Chainable.
*
* @param array $content The content to put between the opening/closing tags
* @param bool $append Whether to replace (false) or append (true) content
* @return this
*/
public function setContent($content, $append = false)
{
if ($append && $this->content !== null) {
$this->content .= (string)$content;
} else {
$this->content = (string)$content;
}
return $this;
}
/**
* Set the given attribute. Chainable.
*
* @param string $key Attribute key
* @param string $value Attribute value
* @param array $props Name-value attribute options
* @return this
*/
public function setAtt($key, $value = null, $props = array())
{
$this->atts->setAttribute($key, $value, $props);
return $this;
}
/**
* Set the given attributes. Chainable.
*
* @param array $atts Name-value attributes
* @param array $props Name-value attribute options
* @return this
*/
public function setAtts($atts, $props = array())
{
foreach ($atts as $key => $value) {
$this->atts->setAttribute($key, $value, $props);
}
return $this;
}
/**
* Append a value to an existing attribute, if a value already exists. Chainable.
*
* @param string $key Attribute key
* @param string $value Attribute value
* @param string $glue The string to use to join the values together
* @return this
*/
public function appendAtt($key, $value, $glue = ' ')
{
$currentVal = $this->atts->getValue($key);
$parts = array();
if ($currentVal !== null) {
$parts[] = $currentVal;
}
$parts[] = $value;
$this->setAtt($key, implode($glue, $parts));
return $this;
}
/**
* Append a value to an existing attribute, if a value already exists. Chainable.
*
* @param array $atts Name-value attributes
* @param string $glue The string to use to join each value to its existing content
* @return this
*/
public function appendAtts($atts, $glue = ' ')
{
foreach ($atts as $key => $value) {
$this->appendAtt($key, $value, $glue);
}
return $this;
}
/**
* Retrieve the given attribute or assign default if not set.
*
* @param string $key Attribute key
* @param string $default Attribute default value if unset
* @param array $props Name-value attribute options
* @return this
*/
public function getAtt($key, $default = null)
{
$val = $this->atts->getValue($key);
return ($val === null) ? $default : $val;
}
/**
* Set the given attributes. Chainable.
*
* @param string|array $keys One or more keys to set as boolean attributes
* @return this
*/
public function setBool($keys)
{
if (!is_array($keys)) {
$keys = do_list($keys);
}
$props = array('format' => 'bool');
if (self::$flags['boolean'] === 'html5') {
$props['strip'] = TEXTPATTERN_STRIP_TXP;
}
foreach ($keys as $key) {
$this->atts->setAttribute($key, true, $props);
}
return $this;
}
/**
* Permit multiple values to be sent by the tag. Chainable.
*
* @param string $flavour The type of multiple to assign: 'all', 'name', or 'attribute'
*/
public function setMultiple($flavour = 'all')
{
$this->atts->setMultiple($flavour);
return $this;
}
/**
* Define one more more global tag options from this point forward. Chainable.
*
* @param string|array $flag The name of the flag to set or an array of flag=>value pairs
* @param string|null $value The value of the flag
*/
public function setFlag($flag, $value = null)
{
if (!is_array($flag)) {
$flag = array($flag => $value);
}
foreach ($flag as $key => $val) {
self::$flags[$key] = $val;
}
return $this;
}
/**
* Set the tag scheme to xhtml or html5 for all flags at once. Chainable.
*
* Just a shortcut for:
* setFlag('self-closing', $scheme);
* setFlag('boolean', $scheme);
*
* @param string $scheme The scheme to set all the output control flags to. Either 'html5' or 'xhtml'
*/
public function setScheme($scheme)
{
if (in_array($scheme, $this->schemes)) {
$this->setFlag(array(
'self-closing' => $scheme,
'boolean' => $scheme,
));
}
return $this;
}
/**
* Set up any tag constraints. Chainable.
*
* @param \Textpattern\Validator\Constraint|\Textpattern\Validator\Constraint[] $constraints Single or array-of \Textpattern\Validator\Constraint object(s)
*/
public function setConstraints($constraints)
{
if ($this->validator === null) {
$this->validator = new \Textpattern\Validator\Validator();
}
$this->validator->setConstraints($constraints);
foreach ($this->validator->getConstraints() as $c) {
$attMap = $c->getAttsMap();
if ($attMap) {
$this->setAtts($attMap, array('strip' => TEXTPATTERN_STRIP_NONE));
}
}
return $this;
}
/**
* Define one or more local tag properties. Chainable.
*
* @param string|array $prop The name of the property to set or an array of property=>value pairs
* @param string|null $value The value of the property
*/
public function setProperty($prop, $value)
{
if (!is_array($prop)) {
$prop = array($prop => $value);
}
foreach ($prop as $key => $val) {
$this->properties[$key] = $val;
}
return $this;
}
/**
* Retrieve a local tag property.
*
* @param string $key The name of the property to fetch
* @param string|null $default The default value of the property if not defined
*/
public function getProperty($key, $default = null)
{
$out = $default;
if (isset($this->properties[$key])) {
$out = $this->properties[$key];
}
return $out;
}
/**
* Set the break string to use after the tag has been output. Chainable.
*
* @param string $break The break tag to use
*/
public function setBreak($break = br)
{
$this->setProperty('break', $break);
return $this;
}
/**
* Get the break string to use after the tag has been output.
*
* The global break flag is set first, then the break-on list is checked.
* Finally, the local break may override it.
*/
public function getBreak()
{
$breaklist = (empty(self::$flags['break-on'])) ? array() : do_list(self::$flags['break-on']);
$break = (empty($breaklist) || in_array($this->tag, $breaklist)) ? self::$flags['break'] : '';
$break = (array_key_exists('break', $this->properties)) ? $this->properties['break'] : $break;
return $break;
}
/**
* Determine if the tag has content assigned.
*
* @return boolean
*/
public function hasContent()
{
return !empty($this->content);
}
/**
* Render the given content as an XML-style element.
*
* @param string $option To affect the flavour of tag returned - complete, self-closing, open, close, content
* @return string HTML
*/
public function render($option = null)
{
if ($option === null) {
if (empty($this->tag)) {
$option = 'content';
} elseif ($this->content !== null) {
$option = 'complete';
} else {
$option = 'self-closing';
}
}
$break = $this->getBreak();
switch ($option) {
case 'complete':
$out = '<'.$this->tag.$this->atts->render().'>'.$this->content.'</'.$this->tag.'>'.$break;
break;
case 'self-closing':
$out = '<'.$this->tag.$this->atts->render().(self::$flags['self-closing'] === 'html5' ? '>' : ' />').$break;
break;
case 'open':
$out = '<'.$this->tag.$this->atts->render().'>';
break;
case 'close':
$out = '</'.$this->tag.'>'.$break;
break;
case 'content':
default:
$out = $this->content;
break;
}
return $out;
}
/**
* Magic method that prints the tag with default options.
*
* @return string HTML
*/
public function __toString()
{
return $this->render();
}
}