-
Notifications
You must be signed in to change notification settings - Fork 3
/
MinifyHtml.php
122 lines (105 loc) · 3.67 KB
/
MinifyHtml.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
<?php
namespace DzId\LaravelHtmlMinifier\Middleware;
class MinifyHtml extends Minifier
{
protected const REGEX_REMOVE_COMMENT = "#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<[^!])#s";
protected function apply()
{
// simpan css dan javascript yang belum di minify dan yang ada attribute ignored--minify nya
$ignoredCss = $this->getByTagOnlyIgnored("style");
$ignoredJs = $this->getByTagOnlyIgnored("script");
if (static::$minifyCssHasBeenUsed && static::$minifyJavascriptHasBeenUsed)
{
$html = $this->replace(static::$dom->saveHtml());
if (empty($ignoredCss) || empty($ignoredCss))
{
return $html;
}
$this->loadDom($html, true);
}
else
{
if (!static::$minifyCssHasBeenUsed)
{
$css = $this->getByTag("style");
}
if (!static::$minifyJavascriptHasBeenUsed)
{
$js = $this->getByTag("script");
}
$html = $this->replace(static::$dom->saveHtml());
$this->loadDom($html, true);
if (isset($css)) {
$this->append("getByTag", "style", $css);
}
if (isset($js)) {
$this->append("getByTag", "script", $js);
}
}
if (!empty($ignoredCss))
{
$this->append("getByTagOnlyIgnored", "style", $ignoredCss);
}
if (!empty($ignoredJs))
{
$this->append("getByTagOnlyIgnored", "script", $ignoredJs);
}
return trim(static::$dom->saveHtml());
}
protected function append(string $function, string $tags, array $backup)
{
$index = 0;
foreach ($this->{$function}($tags) as $el)
{
$el->nodeValue = "";
$el->appendChild(static::$dom->createTextNode($backup[$index]->nodeValue));
$index++;
}
}
protected function removeComment($value)
{
return preg_replace(self::REGEX_REMOVE_COMMENT, "", $value);
}
protected function replace($value)
{
$value = trim(preg_replace([
// t = text
// o = tag open
// c = tag close
// Keep important white-space(s) after self-closing HTML tag(s)
'#<(img|input)(>| .*?>)#s',
// Remove a line break and two or more white-space(s) between tag(s)
'#(<!--.*?-->)|(>)(?:\n*|\s{2,})(<)|^\s*|\s*$#s',
'#(<!--.*?-->)|(?<!\>)\s+(<\/.*?>)|(<[^\/]*?>)\s+(?!\<)#s',
// t+c || o+t
'#(<!--.*?-->)|(<[^\/]*?>)\s+(<[^\/]*?>)|(<\/.*?>)\s+(<\/.*?>)#s',
// o+o || c+c
'#(<!--.*?-->)|(<\/.*?>)\s+(\s)(?!\<)|(?<!\>)\s+(\s)(<[^\/]*?\/?>)|(<[^\/]*?\/?>)\s+(\s)(?!\<)#s',
// c+t || t+o || o+t -- separated by long white-space(s)
'#(<!--.*?-->)|(<[^\/]*?>)\s+(<\/.*?>)#s',
// empty tag
'#<(img|input)(>| .*?>)<\/\1>#s',
// reset previous fix
'#( ) (?![<\s])#',
// clean up ...
'#(?<=\>)( )(?=\<)#',
// --ibid
'/\s+/',
],[
'<$1$2</$1>',
'$1$2$3',
'$1$2$3',
'$1$2$3$4$5',
'$1$2$3$4$5$6$7',
'$1$2$3',
'<$1$2',
'$1 ',
'$1',
" ",
], $value));
$allowRemoveComments = (bool) config("laravel-html-minifier.remove_comments", true);
return $allowRemoveComments == false
? $value
: $this->removeComment($value);
}
}