|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HTML; |
|
4
|
|
|
|
|
5
|
|
|
use DOMDocument; |
|
6
|
|
|
use DOMElement; |
|
7
|
|
|
use MVC\Exception; |
|
8
|
|
|
use MVC\helper; |
|
9
|
|
|
|
|
10
|
|
|
class element |
|
11
|
|
|
{ |
|
12
|
|
|
public $dom; |
|
13
|
|
|
public $version; |
|
14
|
|
|
public $encoding; |
|
15
|
|
|
public static $static; |
|
16
|
|
|
/** |
|
17
|
|
|
* DOMElement. |
|
18
|
|
|
* |
|
19
|
|
|
* @var DOMElement |
|
20
|
|
|
*/ |
|
21
|
|
|
public $element; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct($version = '1.0', $encoding = 'utf-8') |
|
24
|
|
|
{ |
|
25
|
|
|
$this->version = $version; |
|
26
|
|
|
$this->encoding = $encoding; |
|
27
|
|
|
$this->dom = new DOMDocument($version, $encoding); |
|
28
|
|
|
self::$static = new DOMDocument($version, $encoding); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Array to element |
|
33
|
|
|
* |
|
34
|
|
|
* @return \HTML\array2element |
|
35
|
|
|
*/ |
|
36
|
|
|
public function array2el() |
|
37
|
|
|
{ |
|
38
|
|
|
return new array2element(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function pre($content, $options = ['echo' => true]) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->isArrObj($content)) { |
|
44
|
|
|
$content = json_encode($content, false, true); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
$element = "<pre>$content</pre>"; |
|
|
|
|
|
|
47
|
|
|
if ($options['echo']) { |
|
48
|
|
|
echo $content; |
|
49
|
|
|
} else { |
|
50
|
|
|
return $content; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* var_dump inside pretext. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function htmlvar_dump($content) |
|
58
|
|
|
{ |
|
59
|
|
|
echo '<pre>'; |
|
60
|
|
|
var_dump($content); |
|
|
|
|
|
|
61
|
|
|
echo '</pre>'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function script(array $source = [], $html = false, $print = false) |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($source as $path) { |
|
67
|
|
|
$src_ = $this->get_local_asset($path); |
|
68
|
|
|
if ($html) { |
|
69
|
|
|
if ($src_) { |
|
70
|
|
|
$html = '<script src="' . $src_ . '"></script>'; |
|
71
|
|
|
if ($print) { |
|
72
|
|
|
echo $html; |
|
73
|
|
|
} else { |
|
74
|
|
|
return $html; |
|
75
|
|
|
} |
|
76
|
|
|
} else { |
|
77
|
|
|
echo "<!-- $path not found -->"; |
|
78
|
|
|
} |
|
79
|
|
|
} else { |
|
80
|
|
|
if ($src_) { |
|
81
|
|
|
helper::include_asset($src_); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function direct(string $element, string $content, array $attr = []) |
|
88
|
|
|
{ |
|
89
|
|
|
$create = $this->dom->createElement($element, $content); |
|
90
|
|
|
if (!empty($attributes)) { |
|
|
|
|
|
|
91
|
|
|
$create = $this->fill_attributes($create, $attr); |
|
92
|
|
|
} |
|
93
|
|
|
var_dump($create); |
|
|
|
|
|
|
94
|
|
|
$this->dom->appendChild($create); |
|
95
|
|
|
|
|
96
|
|
|
return $this->dom->saveHTML(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function css(array $source) |
|
100
|
|
|
{ |
|
101
|
|
|
$result = ''; |
|
102
|
|
|
$config = defined('CONFIG') && isset(CONFIG['cache']['key']) ? '?cache=' . CONFIG['cache']['key'] : ''; |
|
103
|
|
|
foreach ($source as $path) { |
|
104
|
|
|
$path .= $config; |
|
105
|
|
|
$result .= '<link rel="stylesheet" href="' . $path . '">'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $result; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function js(array $source) |
|
112
|
|
|
{ |
|
113
|
|
|
$result = ''; |
|
114
|
|
|
$config = defined('CONFIG') && isset(CONFIG['cache']['key']) ? '?cache=' . CONFIG['cache']['key'] : ''; |
|
115
|
|
|
foreach ($source as $path) { |
|
116
|
|
|
if (!empty($path)) { |
|
117
|
|
|
$path .= $config; |
|
118
|
|
|
$result .= '<script src="' . $path . '"></script>'; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $result; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Proxy CSS. |
|
127
|
|
|
* |
|
128
|
|
|
* @param bool $html |
|
129
|
|
|
* @param bool $print |
|
130
|
|
|
* @param string $rel |
|
131
|
|
|
*/ |
|
132
|
|
|
public function link(array $source = [], $html = false, $print = false, $rel = 'stylesheet') |
|
133
|
|
|
{ |
|
134
|
|
|
foreach ($source as $path) { |
|
135
|
|
|
$src_ = $this->get_local_asset($path); |
|
136
|
|
|
if ($html) { |
|
137
|
|
|
if ($src_) { |
|
138
|
|
|
$html = '<link href="' . $src_ . '" rel="' . $rel . '" />'; |
|
139
|
|
|
if ($print) { |
|
140
|
|
|
echo $html; |
|
141
|
|
|
} else { |
|
142
|
|
|
return $html; |
|
143
|
|
|
} |
|
144
|
|
|
} else { |
|
145
|
|
|
echo "<!-- $path not found -->"; |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
if ($src_) { |
|
149
|
|
|
helper::include_asset($src_); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function get_local_asset($path) |
|
156
|
|
|
{ |
|
157
|
|
|
if (is_string($path) && filter_var($path, FILTER_VALIDATE_URL)) { |
|
158
|
|
|
$src_ = $path; |
|
159
|
|
|
} else { |
|
160
|
|
|
if (is_string($path)) { |
|
161
|
|
|
$src_ = helper::asset_find([$path]); |
|
162
|
|
|
} elseif (is_array($path)) { |
|
163
|
|
|
$src_ = helper::asset_find($path); |
|
164
|
|
|
} else { |
|
165
|
|
|
throw new Exception('path required string or array, instead of ' . gettype($path), 1); |
|
166
|
|
|
} |
|
167
|
|
|
if ($src_) { |
|
168
|
|
|
$src_ = helper::webkit_asset($src_); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $src_; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* echo print_r in pretext. |
|
177
|
|
|
* |
|
178
|
|
|
* @param mixed $str |
|
179
|
|
|
*/ |
|
180
|
|
|
public function printr(...$str) |
|
181
|
|
|
{ |
|
182
|
|
|
echo '<pre>'; |
|
183
|
|
|
foreach ($str as $string) { |
|
184
|
|
|
print_r($string); |
|
185
|
|
|
} |
|
186
|
|
|
echo '</pre>'; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function select($attributes = []) |
|
190
|
|
|
{ |
|
191
|
|
|
$select = $this->dom->createElement('select'); |
|
192
|
|
|
foreach ($attributes as $key => $value) { |
|
193
|
|
|
if ('option' == $key) { |
|
194
|
|
|
continue; |
|
195
|
|
|
} |
|
196
|
|
|
$select->setAttribute($key, (string) $value); |
|
197
|
|
|
} |
|
198
|
|
|
if (isset($attributes['option'])) { |
|
199
|
|
|
foreach ($attributes['option'] as $option_attr) { |
|
200
|
|
|
$option = $this->single_create('option', $option_attr['inner'], $option_attr); |
|
201
|
|
|
$select->appendChild($option); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
$this->dom->appendChild($select); |
|
205
|
|
|
|
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function single_create(string $element, $content = '', array $attributes = []) |
|
210
|
|
|
{ |
|
211
|
|
|
if (empty($attributes) && is_array($content)) { |
|
212
|
|
|
$attributes = $content; |
|
213
|
|
|
$content = ''; |
|
214
|
|
|
} |
|
215
|
|
|
$element = $this->dom->createElement($element, $content); |
|
216
|
|
|
if (!empty($attributes)) { |
|
217
|
|
|
$element = $this->fill_attributes($element, $attributes); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return $element; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function create(string $element, $content = '', array $attributes = [], array $options = ['formatOutput' => false]) |
|
|
|
|
|
|
224
|
|
|
{ |
|
225
|
|
|
if ('select' == $element) { |
|
226
|
|
|
return $this->select($attributes); |
|
227
|
|
|
} |
|
228
|
|
|
if (empty($attributes) && is_array($content)) { |
|
229
|
|
|
$attributes = $content; |
|
230
|
|
|
$content = ''; |
|
231
|
|
|
} |
|
232
|
|
|
//exit(var_dump($attributes, $content)); |
|
233
|
|
|
$element = $this->dom->createElement($element, $content); |
|
234
|
|
|
if (!empty($attributes)) { |
|
235
|
|
|
$element = $this->fill_attributes($element, $attributes); |
|
236
|
|
|
} |
|
237
|
|
|
$this->dom->appendChild($element); |
|
238
|
|
|
|
|
239
|
|
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function fill_attributes(DOMElement $element, array $attributes) |
|
243
|
|
|
{ |
|
244
|
|
|
foreach ($attributes as $key => $value) { |
|
245
|
|
|
$element->setAttribute($key, (string) $value); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
return $element; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
public function outerText() |
|
252
|
|
|
{ |
|
253
|
|
|
$save = $this->dom->saveHTML(); |
|
254
|
|
|
$this->dom = new DOMDocument($this->version, $this->encoding); |
|
255
|
|
|
|
|
256
|
|
|
return $save; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function isArrObj($str) |
|
260
|
|
|
{ |
|
261
|
|
|
return is_array($str) || is_object($str); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|