forked from hexydec/htmldoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmldoc.php
More file actions
506 lines (456 loc) · 14.2 KB
/
htmldoc.php
File metadata and controls
506 lines (456 loc) · 14.2 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
<?php
namespace hexydec\html;
class htmldoc {
/**
* @var Array $token Regexp components keyed by their corresponding codename for tokenising HTML
*/
protected static $tokens = Array(
'doctype' => '<!DOCTYPE',
'comment' => '<!--[\d\D]*?-->',
'cdata' => '<!\[CDATA\[[\d\D]*?\]\]>',
'tagopenstart' => '<[^\s>\/]++',
'tagselfclose' => '\s*+\/>',
'tagopenend' => '\s*+>',
'tagclose' => '<\/[^ >]++>',
'textnode' => '(?<=>|^)[^<]++(?=<|$)',
'attributevalue' => '=\s*+(?:"[^"]*+"|\'[^\']*+\'|[^ >]*+)',
'attribute' => '[^<>"\'=\s]++',
'whitespace' => '\s++',
'other' => '.'
);
/**
* @var Array $selectors Regexp components keyed by their corresponding codename for tokenising CSS selectors
*/
protected static $selectors = Array(
'quotes' => '(?<!\\\\)"(?:[^"\\\\]++|\\\\.)*+"',
'join' => '\s*[>+~]\s*',
'comparison' => '[\^*$<>]?=', // comparison operators for media queries or attribute selectors
'squareopen' => '\[',
'squareclose' => '\]',
'bracketopen' => '\(',
'bracketclose' => '\)',
'comma' => ',',
'pseudo' => ':[A-Za-z-]++',
'id' => '#[^ +>\.#{\[]++',
'class' => '\.[^ +>\.#{\[]++',
'string' => '\*|[^\[\]{}\(\):;,>+=~\^$!" #\.*]++',
'whitespace' => '\s++',
);
/**
* @var Array $config Object configuration array
*/
protected $config = Array(
'elements' => Array(
'inline' => Array(
'b', 'big', 'i', 'small', 'ttspan', 'em', 'a', 'strong', 'sub', 'sup', 'abbr', 'acronym', 'cite', 'code', 'dfn', 'em', 'kbd', 'strong', 'samp', 'var', 'span'
),
'singleton' => Array(
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
),
'closeoptional' => Array(
'html', 'head', 'body', 'p', 'dt', 'dd', 'li', 'option', 'thead', 'th', 'tbody', 'tr', 'td', 'tfoot', 'colgroup'
),
'pre' => Array('textarea', 'pre', 'code'), // which elements not to strip whitespace from
'custom' => Array('script', 'style'), // which elements have their own plugins
),
'attributes' => Array(
'boolean' => Array(
'allowfullscreen', 'allowpaymentrequest', 'async', 'autofocus', 'autoplay', 'checked', 'contenteditable', 'controls', 'default', 'defer', 'disabled', 'formnovalidate', 'hidden', 'indeterminate', 'ismap', 'itemscope', 'loop', 'multiple', 'muted', 'nomodule', 'novalidate', 'open', 'readonly', 'required', 'reversed', 'scoped', 'selected', 'typemustmatch'
),
'default' => Array( // default attributes that can be removed
'style' => Array(
'type' => 'text/css',
'media' => 'all'
),
'script' => Array(
'type' => 'text/javascript',
'language' => true
),
'form' => Array(
'method' => 'get'
),
'input' => Array(
'type' => 'text'
)
),
'empty' => Array('id', 'class', 'style', 'title', 'lang', 'dir', 'onfocus', 'onblur', 'onchange', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress', 'onkeydown', 'onkeyup'), // attributes to remove if empty
'urls' => Array('href', 'src', 'action', 'poster'), // attributes to minify URLs in
),
'minify' => Array(
'css' => 'hexydec\\html\\cssmin::minify', // minify CSS
'js' => false, // minify javascript
'lowercase' => true, // lowercase tag and attribute names
'whitespace' => true, // strip whitespace from text nodes
'comments' => Array( // remove comments
'ie' => true
),
'urls' => Array( // update internal URL's to be shorter
'absolute' => true, // process absolute URLs to make them relative to the current document
'host' => true, // remove the host for own domain
'scheme' => true // remove the scheme from URLs that have the same scheme as the current document
),
'attributes' => Array( // minify attributes
'default' => true, // remove default attributes
'empty' => true, // remove these attributes if empty
'option' => true, // remove value attribute from option where the text node has the same value
'style' => true, // minify the style tag
'class' => true, // sort classes
'sort' => true, // sort attributes for better gzip
'boolean' => true, // minify boolean attributes
),
'singleton' => true, // minify singleton element by removing slash
'quotes' => true, // minify attribute quotes
'close' => true // don't write close tags where possible
)
);
protected $output = Array(
'charset' => null, // set the output charset
'quotestyle' => 'double', // double, single, minimal
'singletonclose' => false, // string to close singleton tags, or false to leave as is
'closetags' => false // whether to force tags to have a closing tag (true) or follow tag::close
);
/**
* @var Array $children Stores the regexp components keyed by their corresponding codename for tokenising CSS selectors
*/
protected $children = Array();
//protected $attributes = Array();
/**
* Constructs the object
*
* @param Array $config An array of configuration parameters that is recursively merged with the default config
*/
public function __construct(Array $config = Array()) {
$this->config = array_replace_recursive($this->config, $config);
}
/**
* Retrieves the children of the document as an array
*
* @return Array An array of child nodes
*/
public function toArray() {
return $this->children;
}
public function getConfig() {
$config = $this->config;
foreach (func_get_args() AS $item) {
if (isset($config[$item])) {
$config = $config[$item];
} else {
return null;
}
}
return $config;
}
/**
* Open an HTML file from a URL
*
* @param string $url The address of the HTML file to retrieve
* @param string $context An optional array of context parameters
* @return mixed The loaded HTML, or false on error
*/
public function open(String $url, Resource $context = null, String &$error = null) {
// open a handle to the stream
if (($handle = fopen($url, 'rb', $context)) === false) {
$error = 'Could not open file "'.$url.'"';
// retrieve the stream contents
} elseif (($html = stream_get_contents($handle)) === false) {
$error = 'Could not read file "'.$url.'"';
// success
} else {
// find charset in headers
$charset = null;
$meta = stream_get_meta_data($handle);
if (!empty($meta['wrapper_data'])) {
foreach ($meta['wrapper_data'] AS $item) {
if (stripos($item, 'Content-Type:') === 0 && ($charset = stristr($item, 'charset=')) !== false) {
$charset = substr($charset, 8);
break;
}
}
}
// load html
if ($this->load($html, $charset, $error)) {
return $html;
}
}
return false;
}
/**
* Parse an HTML string into the object
*
* @param string $html A string containing valid HTML
* @param string $charset The charset of the document
* @return bool Whether the input HTML was parsed
*/
public function load(string $html, string $charset = null, &$error = null) : bool {
// detect the charset
if ($charset || ($charset = $this->getCharsetFromHtml($html)) !== false || ($charset = mb_detect_encoding($html)) !== false) {
$html = mb_convert_encoding($html, $charset, mb_internal_encoding());
}
// reset the document
$this->children = Array();
// tokenise the input HTML
if (($tokens = tokenise::tokenise($html, self::$tokens)) === false) {
$error = 'Could not tokenise input';
// parse the document
} elseif (!$this->parse($tokens)) {
$error = 'Input is not invalid';
// success
} else {
// \var_dump($this->debug());
return true;
}
return false;
}
/**
* Reads the charset defined in the Content-Type meta tag, or detects the charset from the HTML content
*
* @param string $html A string containing valid HTML
* @return string The defined or detected charset or false if the charset is not defined
*/
protected function getCharsetFromHtml(string $html) {
if (preg_match('/<meta[^>]+charset[^>]+>/i', $html, $match)) {
$obj = new htmldoc($this->config);
if ($obj->load($match[0], mb_internal_encoding()) && ($value = $obj->eq(0)->attr('content')) !== null && ($charset = stristr($value, 'charset=')) !== false) {
return substr($charset, 8);
}
}
return false;
}
/**
* Parses an array of tokens into an HTML documents
*
* @param array &$tokens An array of tokens generated by tokenise()
* @return bool Whether the parser was able to capture any objects
*/
protected function parse(array &$tokens) {
$tag = new tag($this);
$this->children = $tag->parseChildren($tokens);
return !!$this->children;
}
protected static function parseSelector(string $selector) {
$selector = trim($selector);
if (($tokens = tokenise::tokenise($selector, self::$selectors)) !== false) {
$selectors = $parts = Array();
$join = false;
$token = current($tokens);
do {
switch ($token['type']) {
case 'id':
$parts[] = Array(
'id' => substr($token['value'], 1),
'join' => $join
);
$join = false;
break;
case 'class':
$parts[] = Array(
'class' => substr($token['value'], 1),
'join' => $join
);
$join = false;
break;
case 'string':
$parts[] = Array(
'tag' => $token['value'],
'join' => $join
);
$join = false;
break;
case 'squareopen':
$item = Array('join' => $join);
while (($token = next($tokens)) !== false) {
if ($token['type'] == 'squareclose') {
break;
} elseif ($token['type'] == 'string') {
$item[isset($item['attribute']) ? 'value' : 'attribute'] = $token['value'];
} elseif ($token['type'] == 'comparison') {
$item['comparison'] = $token['value'];
}
}
$parts[] = $item;
$join = false;
break;
case 'pseudo':
$parts[] = Array(
'pseudo' => substr($token['value'], 1),
'join' => $join
);
$join = false;
break;
case 'join':
$join = trim($token['value']);
break;
case 'whitespace':
if ($parts) {
$join = ' ';
}
break;
case 'comma':
$selectors[] = $parts;
$parts = Array();
break;
}
} while (($token = next($tokens)) !== false);
if ($parts) {
$selectors[] = $parts;
}
return $selectors;
}
return false;
}
public function get(int $index = null) {
// build children that are tags
$children = Array();
foreach ($this->children AS $item) {
if (get_class($item) == 'hexydec\\html\\tag') {
$children[] = $item;
}
}
// return all children if no index
if ($index === null) {
return $children;
}
// check if index is minus
if ($index < 0) {
$index = count($children) + $index;
}
// return index if set
if (isset($children[$index])) {
return $children[$index];
}
return null;
}
public function find($selector) {
$found = Array();
// parse selector and find tags
if (is_array($selector) || ($selector = $this->parseSelector($selector)) !== false) {
foreach ($this->children AS $item) {
if (get_class($item) == 'hexydec\\html\\tag') {
foreach ($selector AS $value) {
if (($items = $item->find($value)) !== false) {
$found = array_merge($found, $items);
}
}
}
}
}
// create new document and return
$doc = new htmldoc($this->config);
if ($found) {
$doc->collection($found);
}
return $doc;
}
public function first() : htmldoc {
return $this->eq(0);
}
public function last() : htmldoc {
return $this->eq(-1);
}
public function eq(int $index) : htmldoc {
$doc = new htmldoc($this->config);
if ($index < 0) {
$index = count($this->children) + $index;
}
if (isset($this->children[$index])) {
$doc->collection(Array($this->children[$index]));
}
return $doc;
}
public function children() : htmldoc {
return $this->find('>*');
}
public function attr(string $key) {
foreach ($this->children AS $item) {
if (get_class($item) == 'hexydec\\html\\tag') {
return $item->attr($key);
}
}
}
public function text() : string {
$text = '';
foreach ($this->children AS $item) {
// only get text from these objects
if (in_array(get_class($item), Array('hexydec\\html\\tag', 'hexydec\\html\\text'))) {
$text .= $item->text();
// add a space to make sure words aren't joined
if ($text && mb_substr($text, -1) != ' ') {
$text .= ' ';
}
}
}
return $text;
}
public function collection(array $nodes) {
$this->children = $nodes;
}
public function minify(array $minify = Array()) {
// merge config
$minify = array_replace_recursive($this->config['minify'], $minify);
// set minify output parameters
if ($minify['quotes']) {
$this->output['quotestyle'] = 'minimal';
}
// sort attributes
// if ($config['attributes']['sort']) {
// arsort($this->attributes, SORT_NUMERIC);
// $config['attributes']['sort'] = \array_keys($this->attributes);
// }
foreach ($this->children AS $item) {
$item->minify($minify);
}
}
public function html(array $options = null) : string {
$options = $options ? array_merge($this->output, $options) : $this->output;
$html = '';
foreach ($this->children AS $item) {
$html .= $item->html($options);
}
return $html;
}
public function save(string $file = null, Array $options = Array()) {
// compile html
$html = $this->html($options);
// convert charset
if (!empty($options['charset'])) {
$html = mb_convert_encoding(mb_internal_encoding(), $options['charset'], $html);
}
// save file
if (!$file) {
return $html;
} elseif (file_put_contents($file, $html) === false) {
trigger_error('File could not be written', E_USER_WARNING);
} else {
return true;
}
return false;
}
// public function debug() {
// $output = Array();
// foreach ($this->children AS $item) {
// $node = Array(
// 'type' => get_class($item)
// );
// switch ($node['type']) {
// case 'hexydec\\html\\tag':
// $node['tag'] = $item->tagName;
// $node['attributes'] = $item->attributes;
// $node['singleton'] = $item->singleton;
// $node['close'] = $item->close;
// if ($item->children) {
// $node['children'] = $item->children->debug();
// }
// break;
// case 'hexydec\\html\\doctype':
// $node['doctype'] = $item->content;
// break;
// default:
// $node['content'] = $item->content;
// break;
// }
// $output[] = $node;
// }
// return $output;
// }
}