1 | <?php |
||
12 | class EmptyAttributeMinifier implements MinifierInterface |
||
13 | { |
||
14 | protected $repository; |
||
15 | |||
16 | 7 | public function __construct() |
|
20 | |||
21 | /** |
||
22 | * Execute the minification rule. |
||
23 | * |
||
24 | * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
||
25 | * |
||
26 | * @return \ArjanSchouten\HtmlMinifier\MinifyContext |
||
27 | */ |
||
28 | 7 | public function process(MinifyContext $context) |
|
29 | { |
||
30 | 7 | return $context->setContents(preg_replace_callback( |
|
31 | '/ |
||
32 | 7 | (\s*'.Constants::ATTRIBUTE_NAME_REGEX.'\s*) # Match the attribute name |
|
33 | =\s* # Match the equal sign with optional whitespaces |
||
34 | (["\']) # Match quotes and capture for backreferencing |
||
35 | \s* # Strange but possible to have a whitespace in an attribute |
||
36 | \2 # Backreference to the matched quote |
||
37 | \s* |
||
38 | /x', |
||
39 | 7 | function ($match) { |
|
40 | 6 | if ($this->isBooleanAttribute($match[1])) { |
|
41 | 2 | return Html::isLastAttribute($match[0]) ? $match[1] : $match[1].' '; |
|
42 | } |
||
43 | |||
44 | 4 | return Html::hasSurroundingAttributes($match[0]) ? ' ' : ''; |
|
45 | 7 | }, $context->getContents())); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Check if an attribute is a boolean attribute. |
||
50 | * |
||
51 | * @param string $attribute |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | 6 | private function isBooleanAttribute($attribute) |
|
59 | |||
60 | /** |
||
61 | * @param \ArjanSchouten\HtmlMinifier\Minifiers\Html\HtmlBooleanAttributeRepository $repository |
||
62 | */ |
||
63 | 1 | public function setRepository($repository) |
|
67 | |||
68 | /** |
||
69 | * Indicates if minification rules depends on command options. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | 3 | public function provides() |
|
77 | } |
||
78 |