|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\LaravelTracy\Panels; |
|
4
|
|
|
|
|
5
|
|
|
use DOMDocument; |
|
6
|
|
|
use LibXMLError; |
|
7
|
|
|
use Recca0120\LaravelTracy\Contracts\IAjaxPanel; |
|
8
|
|
|
use Recca0120\LaravelTracy\Events\BeforeBarRender; |
|
9
|
|
|
|
|
10
|
|
|
class HtmlValidatorPanel extends AbstractSubscriablePanel implements IAjaxPanel |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* $html. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $html; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* $ignoreErrors. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $ignoreErrors = [ |
|
25
|
|
|
// XML_ERR_ENTITYREF_SEMICOL_MISSING |
|
26
|
|
|
23, |
|
27
|
|
|
// XML_HTML_UNKNOWN_TAG |
|
28
|
|
|
801, |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* $severenity. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected static $severenity = [ |
|
37
|
|
|
LIBXML_ERR_WARNING => 'Warning', |
|
38
|
|
|
LIBXML_ERR_ERROR => 'Error', |
|
39
|
|
|
LIBXML_ERR_FATAL => 'Fatal error', |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* setHTML. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $html |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function setHtml($html) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->html = $html; |
|
51
|
|
|
|
|
52
|
1 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Removes special controls characters and normalizes line endings and spaces. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $str |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
1 |
|
protected static function normalize($str) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$str = static::normalizeNewLines($str); |
|
64
|
|
|
// remove control characters; leave \t + \n |
|
65
|
1 |
|
$str = preg_replace('#[\x00-\x08\x0B-\x1F\x7F-\x9F]+#u', '', $str); |
|
66
|
|
|
// right trim |
|
67
|
1 |
|
$str = preg_replace('#[\t ]+$#m', '', $str); |
|
68
|
|
|
// leading and trailing blank lines |
|
69
|
1 |
|
$str = trim($str, "\n"); |
|
70
|
|
|
|
|
71
|
1 |
|
return $str; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Standardize line endings to unix-like. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $s |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
1 |
|
protected static function normalizeNewLines($s) |
|
81
|
|
|
{ |
|
82
|
1 |
|
return str_replace(["\r\n", "\r"], "\n", $s); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* getAttributes. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
1 |
|
protected function getAttributes() |
|
91
|
|
|
{ |
|
92
|
1 |
|
libxml_use_internal_errors(true); |
|
93
|
1 |
|
$dom = new DOMDocument('1.0', 'UTF-8'); |
|
94
|
1 |
|
$dom->resolveExternals = false; |
|
95
|
1 |
|
$dom->validateOnParse = true; |
|
96
|
1 |
|
$dom->preserveWhiteSpace = false; |
|
97
|
1 |
|
$dom->strictErrorChecking = true; |
|
98
|
1 |
|
$dom->recover = true; |
|
99
|
|
|
|
|
100
|
1 |
|
@$dom->loadHTML($this->normalize($this->html)); |
|
101
|
|
|
|
|
102
|
1 |
|
$errors = array_filter(libxml_get_errors(), function (LibXMLError $error) { |
|
103
|
|
|
return in_array((int) $error->code, static::$ignoreErrors, true) === false; |
|
104
|
1 |
|
}); |
|
105
|
|
|
|
|
106
|
1 |
|
libxml_clear_errors(); |
|
107
|
|
|
|
|
108
|
|
|
return [ |
|
109
|
1 |
|
'severenity' => static::$severenity, |
|
110
|
1 |
|
'counter' => count($errors), |
|
111
|
1 |
|
'errors' => $errors, |
|
112
|
1 |
|
'html' => $this->html, |
|
113
|
|
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* subscribe. |
|
118
|
|
|
**/ |
|
119
|
1 |
|
protected function subscribe() |
|
120
|
|
|
{ |
|
121
|
1 |
|
$events = $this->laravel['events']; |
|
122
|
1 |
|
$events->listen(BeforeBarRender::class, function ($barRender) { |
|
123
|
1 |
|
$this->setHtml($barRender->response->getContent()); |
|
124
|
1 |
|
}); |
|
125
|
1 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|