Skip to content

Commit 9a6f2e3

Browse files
committed
Expanded rules cache to included negatives / provide html cache identifier to prevent collisions across pages
1 parent f3455aa commit 9a6f2e3

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/CssFromHTMLExtractor.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,13 @@ public function extractCss($html)
138138
$xPath = new DOMXPath($document);
139139

140140
$cssIdentifier = join('-', $this->loadedfiles);
141+
$htmlIdentifier = md5($html);
141142

142143
$applicable_rules = array_filter(
143144
$this->rules,
144-
function (Rule $rule) use ($xPath, $cssIdentifier) {
145-
if (isset($this->cachedRules[$cssIdentifier][$rule->getSelector()])) {
146-
return true;
145+
function (Rule $rule) use ($xPath, $cssIdentifier, $htmlIdentifier) {
146+
if (isset($this->cachedRules[$cssIdentifier][$htmlIdentifier][$rule->getSelector()])) {
147+
return $this->cachedRules[$cssIdentifier][$htmlIdentifier][$rule->getSelector()];
147148
}
148149

149150
$expression = $this->buildExpressionForSelector($rule->getSelector());
@@ -152,10 +153,11 @@ function (Rule $rule) use ($xPath, $cssIdentifier) {
152153
$elements = $xPath->query($expression);
153154

154155
if ($elements->length === 0) {
156+
$this->cachedRules[$cssIdentifier][$htmlIdentifier][$rule->getSelector()] = false;
155157
return false;
156158
}
157159

158-
$this->cachedRules[$cssIdentifier][$rule->getSelector()] = true;
160+
$this->cachedRules[$cssIdentifier][$htmlIdentifier][$rule->getSelector()] = true;
159161

160162
return true;
161163
}
@@ -204,7 +206,6 @@ private function buildExpressionForSelector(string $selector)
204206
return $expression;
205207
}
206208

207-
208209
try {
209210
$expression = $this->cssConverter->toXPath($selector);
210211
} catch (ExpressionErrorException $expressionErrorException) {

0 commit comments

Comments
 (0)