-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.php
267 lines (239 loc) · 6.08 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
<?php
/**
* HTML CLASS
* VERSION 1.0
*
*/
class Html {
public $debug = false;
public $return = true;
public $echo = false;
private $method = '';
private $tagname = '';
private $content = '';
private $attributes = array();
private $dontClose = array('!DOCTYPE html', 'meta', 'link', 'input');
private $close = array('link', 'base', 'img');
private function __reset() {
$this->method = '';
$this->tagname = '';
$this->content = '';
$this->attributes = array();
}
private function __closeTag() {
return '</'.$this->tagname.'>';
}
public function __call($name, $arguments) {
if(empty($this->method)) {
$this->tagname = strtolower($name);
$this->content = '';
if(isset($arguments[0]) && is_array($arguments[0])) {
$this->attributes = $arguments[0];
}
else {
$this->content = isset($arguments[0]) ? $arguments[0] : null;
if(isset($arguments[1]) && is_array($arguments[1]) && count($arguments[1]) > 0) {
$this->attributes = $arguments[1];
}
}
$this->method = $this->tagname.'Tag';
if(method_exists($this, $this->method)) {
$result = $this->{$this->method}();
}
else {
$result = $this->defaultTag();
}
if($this->debug) {
echo "<table border='1'><tr><td>tagname</td><td>$this->tagname</td></tr><tr><td>content</td><td>$this->content</td></tr><tr><td>arguments</td><td><pre>". print_r($this->attributes, 1)."</pre></td></tr><tr><td><strong>result</strong></td><td><pre>".htmlspecialchars($result)."</pre></td></tr></table><br>";
}
$this->__reset();
if($this->return) {
return $result;
}
if($this->echo) {
echo $result;
}
return $this;
}
}
public function defaultTag() {
$html = '<';
$html.= $this->tagname;
if(count($this->attributes) > 0) {
ksort($this->attributes);
foreach($this->attributes as $key => $value) {
if(is_array($value)) {
$val = implode(' ', $value);
}
else {
$val = $value;
}
if(is_int($key)) {
$html.= ' '.$value;
}
else {
$html.= ' '.$key.'="'.$val.'"';
}
}
}
if(in_array($this->tagname, $this->close)) {
$html.= ' />';
}
else {
$html.= '>';
if(!in_array($this->tagname, $this->dontClose)) {
$html.= $this->content;
$html.= '</'.$this->tagname.'>';
}
}
//$this->__reset();
return $html;
}
public function doctypeTag() {
$this->tagname = '!DOCTYPE html';
return $this->defaultTag();
}
public function mailtoTag() {
$this->tagname = 'a';
$this->attributes['href'] = 'mailto:'.$this->content;
return $this->defaultTag();
}
public function utf8Tag() {
$this->tagname = 'meta';
$this->attributes['charset'] = 'utf-8';
return $this->defaultTag();
}
}
/**
* HTMLCHAIN CLASS
* VERSION 1.0
*
*/
class HtmlChain {
var $Html = '';
var $stack = array();
var $path = array();
var $library = array();
var $i = 0;
var $whitespace = true;
var $dontNest = array('doctype', 'utf8', 'meta', 'title', 'label', 'link', 'script');
function __construct() {
$this->Html = new Html();
$this->Html->debug = false;
$this->Html->echo = false;
$this->Html->return = true;
$this->stack = array();
$this->library = array();
$this->path = array();
$this->i = 0;
}
function getTag($key, $internal = false) {
$tag = '';
$parts = explode('-', $key, 2);
$tag = $parts[0];
if($internal) {
return $tag;
}
if(stristr($tag, '_')) {
$parts = explode('_', $tag, 2);
$tag = $parts[0];
}
return $tag;
}
function addToStack($name) {
$stack = &$this->stack;
$this->i++;
foreach($this->path as $key) {
$stack = &$stack[$key];
}
$elementkey = $name.'-'.$this->i;
$stack[$elementkey] = array();
$this->path[] = $elementkey;
return $elementkey;
}
function searchInPath($name) {
$path = array_reverse($this->path);
foreach($path as $i => $key) {
$tag = $this->getTag($key, true);
if($name == $tag) {
return count($this->path)-1-$i;
}
}
}
function updatePath($name) {
$i = $this->searchInPath($name);
$this->path = array_slice($this->path, 0, $i);
}
function end() {
array_pop($this->path);
return $this;
}
function c() { return $this->end(); }
function close() { return $this->end(); }
function reset() {
$this->__construct();
return $this;
}
function __call($name, $arguments) {
//save internal
$this->updatePath($name);
$key = $this->addToStack($name);
//order the arguments
$args = array('', '');
if(isset($arguments[0]) && is_array($arguments[0])) {
$args[0] = '';
$args[1] = $arguments[0];
}
elseif(isset($arguments[0]) && is_string($arguments[0])) {
$args[0] = $arguments[0];
if(isset($arguments[1]) && is_array($arguments[1])) {
$args[1] = $arguments[1];
}
}
//explore the magic classes
if(stristr($name, '_')) {
$parts = explode('_', $name, 2);
$class = '';
if(isset($args[1]['class'])) {
$class = ' '.$args[1]['class'];
}
$args[1]['class'] = $parts[1].$class;
}
//stop the nesting if the element are not nestable
if(in_array($name, $this->dontNest)) {
$this->end();
}
//store in library
$this->library[$key] = $args;
//and start the chain
return $this;
}
function render($stack, $level = -1) {
$br = $this->whitespace ? "\n" : '';
$tab = $this->whitespace ? "\t" : '';
$html = '';
$level++;
foreach($stack as $key => $value) {
$tag = $this->getTag($key);
$attributes = $this->library[$key];
if(count($value) > 0) {
$content = '';
if(!empty($attributes[0])) {
$content.= $br.str_repeat($tab, $level+1);
$content.= $attributes[0];
}
$content.= $this->render($value, $level);
$content.= $br.str_repeat($tab, $level);
$attributes[0] = $content;
}
$html.= $br.str_repeat($tab, $level);
$html.= call_user_func_array(array($this->Html, $tag), $attributes);
}
return $html;
}
function __toString() {
$html = $this->render($this->stack);
$this->reset();
return $html;
}
}