|
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\Repositories\OptionalElementsRepository; |
|
9
|
|
|
|
|
10
|
|
|
class OptionalElementMinifier implements MinifierInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Execute the minification rule. |
|
14
|
|
|
* |
|
15
|
|
|
* @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
|
16
|
|
|
* |
|
17
|
|
|
* @return \ArjanSchouten\HtmlMinifier\MinifyContext |
|
18
|
|
|
*/ |
|
19
|
8 |
|
public function process(MinifyContext $context) |
|
20
|
|
|
{ |
|
21
|
8 |
|
$elements = (new OptionalElementsRepository())->getElements(); |
|
22
|
8 |
|
$contents = $context->getContents(); |
|
23
|
|
|
|
|
24
|
8 |
|
foreach ($elements as $element) { |
|
25
|
8 |
|
$contents = $this->removeElement($element, $contents); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
8 |
|
return $context->setContents($contents); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Remove an optional element. |
|
33
|
|
|
* |
|
34
|
|
|
* @param object $element |
|
35
|
|
|
* @param string $contents |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
8 |
|
protected function removeElement($element, $contents) |
|
40
|
|
|
{ |
|
41
|
8 |
|
$newContents = preg_replace('@'.$element->regex.'@xi', '', $contents); |
|
42
|
|
|
|
|
43
|
8 |
|
if ($newContents !== $contents && isset($element->elements)) { |
|
44
|
|
|
foreach ($element->elements as $element) { |
|
45
|
|
|
$newContents = $this->removeElement($element, $newContents); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
8 |
|
return $newContents; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Indicates if minification rules depends on command options. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function provides() |
|
58
|
|
|
{ |
|
59
|
3 |
|
return Options::OPTIONAL_ELEMENTS; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|