|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ArjanSchouten\HtmlMinifier\Minifiers\Html; |
|
4
|
|
|
|
|
5
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface; |
|
6
|
|
|
use ArjanSchouten\HtmlMinifier\MinifyContext; |
|
7
|
|
|
use ArjanSchouten\HtmlMinifier\Options; |
|
8
|
|
|
use ArjanSchouten\HtmlMinifier\Util\Html; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
class RedundantAttributeMinifier implements MinifierInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Attributes which are not needed by the browser. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $redundantAttributes = [ |
|
19
|
|
|
'script' => [ |
|
20
|
|
|
'type' => 'text\/javascript', |
|
21
|
|
|
'language' => 'javascript', |
|
22
|
|
|
], |
|
23
|
|
|
'link' => [ |
|
24
|
|
|
'type' => 'text\/css', |
|
25
|
|
|
], |
|
26
|
|
|
'style' => [ |
|
27
|
|
|
'type' => 'text\/css', |
|
28
|
|
|
], |
|
29
|
|
|
'form' => [ |
|
30
|
|
|
'method' => 'get', |
|
31
|
|
|
], |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Minify redundant attributes which are not needed by the browser. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
|
38
|
|
|
* |
|
39
|
|
|
* @return \ArjanSchouten\HtmlMinifier\MinifyContext |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function process(MinifyContext $context) |
|
42
|
|
|
{ |
|
43
|
|
|
Collection::make($this->redundantAttributes)->each(function ($attributes, $element) use (&$context) { |
|
44
|
|
|
Collection::make($attributes)->each(function ($value, $attribute) use ($element, &$context) { |
|
45
|
2 |
|
$contents = preg_replace_callback( |
|
46
|
|
|
'/ |
|
47
|
2 |
|
'.$element.' # Match the given element |
|
48
|
2 |
|
((?!\s*'.$attribute.'\s*=).)* # Match everything except the given attribute |
|
49
|
|
|
( |
|
50
|
2 |
|
\s*'.$attribute.'\s* # Match the attribute |
|
51
|
|
|
=\s* # Match the equals sign |
|
52
|
|
|
(["\']?) # Match the opening quotes |
|
53
|
2 |
|
\s*'.$value.'\s* # Match the value |
|
54
|
|
|
\3? # Match the captured opening quotes again |
|
55
|
|
|
\s* |
|
56
|
|
|
) |
|
57
|
|
|
/xis', |
|
58
|
2 |
|
function ($match) { |
|
59
|
1 |
|
return $this->removeAttribute($match[0], $match[2]); |
|
60
|
2 |
|
}, $context->getContents()); |
|
61
|
|
|
|
|
62
|
2 |
|
$context->setContents($contents); |
|
63
|
2 |
|
}); |
|
64
|
2 |
|
}); |
|
65
|
|
|
|
|
66
|
2 |
|
return $context; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Remove the attribute from the element. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $element |
|
73
|
|
|
* @param string $attribute |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
protected function removeAttribute($element, $attribute) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$replacement = Html::hasSurroundingAttributes($attribute) ? ' ' : ''; |
|
80
|
|
|
|
|
81
|
1 |
|
return str_replace($attribute, $replacement, $element); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Indicates if minification rules depends on command options. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function provides() |
|
90
|
|
|
{ |
|
91
|
3 |
|
return Options::REMOVE_DEFAULTS; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|