forked from Airmanbzh/php-html-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkup.php
More file actions
executable file
·404 lines (365 loc) · 9.97 KB
/
Markup.php
File metadata and controls
executable file
·404 lines (365 loc) · 9.97 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
<?php
/*
* @author Airmanbzh
*/
namespace HtmlGenerator;
use ArrayAccess;
if (!defined('ENT_XML1'))
{
define('ENT_XML1', 16);
}
if (!defined('ENT_XHTML'))
{
define('ENT_XHTML', 32);
}
class Markup implements ArrayAccess
{
/** @var boolean Specifies if attribute values and text input sould be protected from XSS injection */
public static $avoidXSS = false;
/** @var int The language convention used for XSS avoiding */
public static $outputLanguage = ENT_XML1;
protected static $_instance = null;
protected $_top = null;
protected $_parent = null;
protected $tag = null;
public $attributeList = null;
protected $classList = null;
protected $content = null;
protected $text = '';
protected $autoclosed = false;
protected $autocloseTagsList = array();
/**
* Constructor
* @param mixed $tag
* @param Markup $top
* @return Markup instance
*/
protected function __construct($tag, $top = null)
{
$this->tag = $tag;
$this->_top =& $top;
$this->attributeList = array();
$this->classList = array();
$this->content = array();
$this->autoclosed = in_array($this->tag, $this->autocloseTagsList);
$this->text = '';
return $this;
}
/**
* Builds markup from static context
* @param string $tag The tag name
* @param array $content The content of the current tag, first argument can be an array containing the attributes
* @return Markup
*/
public static function __callStatic($tag, $content)
{
return self::createElement($tag)
->attr(count($content) && is_array($content[0]) ? array_pop($content) : array())
->text(implode('', $content));
}
/**
* Add a children to the current element
* @param string $tag The name of the tag
* @param array $content The content of the current tag, first argument can be an array containing the attributes
* @return Markup instance
*/
public function __call($tag, $content)
{
return $this
->addElement($tag)
->attr(count($content) && is_array($content[0]) ? array_pop($content) : array())
->text(implode('', $content));
}
/**
* Alias for getParent()
* @return Markup
*/
public function __invoke()
{
return $this->getParent();
}
/**
* Create a new Markup
* @param string $tag
* @return static instance
*/
public static function createElement($tag = '')
{
self::$_instance = new static($tag);
return self::$_instance;
}
/**
*
* Add element at an existing Markup
* @param Markup|string $tag
* @return Markup instance
*/
public function addElement($tag = '')
{
$htmlTag = (is_object($tag) && $tag instanceof self) ? $tag : new static($tag);
$htmlTag->_top = $this->getTop();
$htmlTag->_parent = &$this;
$this->content[] = $htmlTag;
return $htmlTag;
}
/**
* (Re)Define an attribute or many attributes
* @param string|array $attribute
* @param string $value
* @return Markup instance
*/
public function set($attribute, $value = null)
{
if(is_array($attribute)) {
foreach ($attribute as $key => $value) {
$this[$key] = $value;
}
} else {
$this[$attribute] = $value;
}
return $this;
}
/**
* alias to method "set"
* @param string|array $attribute
* @param string $value
* @return Markup instance
*/
public function attr($attribute, $value = null)
{
return call_user_func_array(array($this, 'set'), func_get_args());
}
/**
* Checks if an attribute is set for this tag and not null
*
* @param string $attribute The attribute to test
* @return boolean The result of the test
*/
public function offsetExists($attribute)
{
return isset($this->attributeList[$attribute]);
}
/**
* Returns the value the attribute set for this tag
*
* @param string $attribute The attribute to get
* @return mixed The stored result in this object
*/
public function offsetGet($attribute)
{
return $this->offsetExists($attribute) ? $this->attributeList[$attribute] : null;
}
/**
* Sets the value an attribute for this tag
*
* @param string $attribute The attribute to set
* @param mixed $value The value to set
* @return void
*/
public function offsetSet($attribute, $value)
{
$this->attributeList[$attribute] = $value;
}
/**
* Removes an attribute
*
* @param mixed $attribute The attribute to unset
* @return void
*/
public function offsetUnset($attribute)
{
if ($this->offsetExists($attribute))
unset($this->attributeList[$attribute]);
}
/**
*
* Define text content
* @param string $value
* @return Markup instance
*/
public function text($value)
{
$this->addElement('')->text = static::$avoidXSS ? static::unXSS($value) : $value;
return $this;
}
/**
* Returns the top element
* @return Markup
*/
public function getTop()
{
return $this->_top===null ? $this : $this->_top;
}
/**
*
* Return parent of current element
*/
public function getParent()
{
return $this->_parent;
}
/**
* Return first child of parent of current object
*/
public function getFirst()
{
return is_null($this->_parent) ? null : $this->_parent->content[0];
}
/**
* Return previous element or itself
*
* @return Markup instance
*/
public function getPrevious()
{
$prev = $this;
$find = false;
if (!is_null($this->_parent)) {
foreach ($this->_parent->content as $c) {
if ($c === $this) {
$find=true;
break;
}
if (!$find) {
$prev = $c;
}
}
}
return $prev;
}
/**
* @return Markup last child of parent of current object
*/
public function getNext()
{
$next = null;
$find = false;
if (!is_null($this->_parent)) {
foreach ($this->_parent->content as $c) {
if ($find) {
$next = &$c;
break;
}
if ($c == $this) {
$find = true;
}
}
}
return $next;
}
/**
* @return Markup last child of parent of current object
*/
public function getLast()
{
return is_null($this->_parent) ? null : $this->_parent->content[count($this->_parent->content) - 1];
}
/**
* @return Markup return parent or null
*/
public function remove()
{
$parent = $this->_parent;
if (!is_null($parent)) {
foreach ($parent->content as $key => $value) {
if ($parent->content[$key] == $this) {
unset($parent->content[$key]);
return $parent;
}
}
}
return null;
}
/**
* Generation method
* @return string
*/
public function __toString()
{
return $this->getTop()->toString();
}
/**
* Generation method
* @return string
*/
public function toString()
{
$string = '';
if (!empty($this->tag)) {
$string .= '<' . $this->tag;
$string .= $this->attributesToString();
if ($this->autoclosed) {
$string .= '/>';
} else {
$string .= '>' . $this->contentToString() . '</' . $this->tag . '>';
}
} else {
$string .= $this->text;
$string .= $this->contentToString();
}
return $string;
}
/**
* return current list of attribute as a string $key="$val" $key2="$val2"
* @return string
*/
protected function attributesToString()
{
$string = '';
$XMLConvention = in_array(static::$outputLanguage, array(ENT_XML1, ENT_XHTML));
if (!empty($this->attributeList)) {
foreach ($this->attributeList as $key => $value) {
if ($value!==null && ($value!==false || $XMLConvention)) {
$string.= ' ' . $key;
if($value===true) {
if ($XMLConvention) {
$value = $key;
} else {
continue;
}
}
$string.= '="' . implode(
' ',
array_map(
static::$avoidXSS ? 'static::unXSS' : 'strval',
is_array($value) ? $value : array($value)
)
) . '"';
}
}
}
return $string;
}
/**
* return current list of content as a string
* @return string
*/
protected function contentToString()
{
$string = '';
if (!is_null($this->content)) {
foreach ($this->content as $c) {
$string .= $c->toString();
}
}
return $string;
}
/**
* Protects value from XSS injection by replacing some characters by XML / HTML entities
* @param string $input The unprotected value
* @return string A safe string
*/
public static function unXSS($input)
{
$return = '';
if (version_compare(phpversion(), '5.4', '<'))
{
$return = htmlspecialchars($input);
}
else
{
$return = htmlentities($input, ENT_QUOTES | ENT_DISALLOWED | static::$outputLanguage);
}
return $return;
}
}