|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
namespace Katten\Purge; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Sabberworm\CSS\CSSList\Document; |
|
9
|
|
|
use Sabberworm\CSS\RuleSet\DeclarationBlock; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class Purger { |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var $cssBlocks |
|
18
|
|
|
* A hash table of css DeclarationBlock objects |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cssBlocks; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
public function __construct(BlockHashTable $hashTable) { |
|
25
|
|
|
|
|
26
|
|
|
$this->cssBlocks = $hashTable; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Compares the CSS selectors against those found in the Crawler object |
|
33
|
|
|
* |
|
34
|
|
|
* @param PurgeHtmlCrawler $dom |
|
35
|
|
|
*/ |
|
36
|
|
|
public function purge(PurgeHtmlCrawler $dom) { |
|
37
|
|
|
|
|
38
|
|
|
foreach ($this->cssBlocks->getBlockCollection() as $block) { |
|
39
|
|
|
|
|
40
|
|
|
$decBlock = $block[BlockHashTable::BLOCK_VALUE]; |
|
41
|
|
|
|
|
42
|
|
|
$unusedBlock = $this->filter($decBlock, $dom); |
|
43
|
|
|
|
|
44
|
|
|
if ($unusedBlock == null) { |
|
45
|
|
|
$hash = $this->cssBlocks->hashBlock($decBlock); |
|
46
|
|
|
$this->cssBlocks->setUsedFlag($hash, true); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get all unused css from the cssBlocks hashtable |
|
55
|
|
|
* |
|
56
|
|
|
* @return |
|
57
|
|
|
* An array of DeclarationBlock objects |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getPurgedCss() { |
|
60
|
|
|
|
|
61
|
|
|
$unusedCss = []; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($this->cssBlocks->getBlockCollection() as $block) { |
|
64
|
|
|
|
|
65
|
|
|
if (!$block[BlockHashTable::IS_USED]) { |
|
66
|
|
|
$unusedCss[] = $block[BlockHashTable::BLOCK_VALUE]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $unusedCss; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Filter out any used CSS on a selector by selector basis |
|
77
|
|
|
* |
|
78
|
|
|
* @param DeclarationBlock $block |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
* Return the DeclarationBlock if no usage is found |
|
82
|
|
|
* otherwise, return null |
|
83
|
|
|
*/ |
|
84
|
|
|
public function filter(DeclarationBlock $block, PurgeHtmlCrawler $dom) { |
|
85
|
|
|
|
|
86
|
|
|
foreach ($block->getSelectors() as $selector) { |
|
87
|
|
|
|
|
88
|
|
|
$processedSelector = $this->preprocess($selector->getSelector()); |
|
89
|
|
|
|
|
90
|
|
|
$usage = $dom->findFirstInstance($processedSelector); |
|
91
|
|
|
|
|
92
|
|
|
if (count($usage) > 0) { |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $block; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Strip any pseudo classes out of the selector |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $selector |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
* A processed selector string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function preprocess($selector) { |
|
111
|
|
|
|
|
112
|
|
|
if (strstr($selector, ':')) { |
|
113
|
|
|
$processedSelector = preg_replace('/\([^()]+\)|::?[^ ,:.(]+/i', '', $selector); |
|
114
|
|
|
} |
|
115
|
|
|
else { |
|
116
|
|
|
$processedSelector = $selector; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $processedSelector; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|