|
5 | 5 | use Doctrine\Common\Cache\ArrayCache;
|
6 | 6 | use Doctrine\Common\Cache\Cache;
|
7 | 7 | use DOMNodeList;
|
| 8 | +use DOMXPath; |
8 | 9 | use Exception;
|
9 | 10 | use Symfony\Component\CssSelector\CssSelectorConverter;
|
10 | 11 | use Symfony\Component\CssSelector\Exception\ExceptionInterface;
|
@@ -125,33 +126,12 @@ public function extractCss($html)
|
125 | 126 | {
|
126 | 127 | $document = $this->createDomDocumentFromHtml($html);
|
127 | 128 |
|
128 |
| - $xPath = new \DOMXPath($document); |
129 |
| - |
| 129 | + $xPath = new DOMXPath($document); |
130 | 130 |
|
131 | 131 | $applicable_rules = array_filter(
|
132 | 132 | $this->rules,
|
133 | 133 | function (Rule $rule) use ($xPath) {
|
134 |
| - |
135 |
| - try { |
136 |
| - $expression = $this->cssConverter->toXPath($rule->getSelector()); |
137 |
| - } catch (ExpressionErrorException $expressionErrorException) { |
138 |
| - |
139 |
| - // Allow for pseudo selectors |
140 |
| - // TODO: Find a way to validate this exception without checking strings |
141 |
| - if ($expressionErrorException->getMessage() !== 'Pseudo-elements are not supported.') { |
142 |
| - return false; |
143 |
| - } |
144 |
| - |
145 |
| - try { |
146 |
| - $tokens = explode(':', $rule->getSelector()); |
147 |
| - $expression = $this->cssConverter->toXPath((string)reset($tokens)); |
148 |
| - } catch (Exception $e) { |
149 |
| - return false; |
150 |
| - } |
151 |
| - |
152 |
| - } catch (ExceptionInterface $e) { |
153 |
| - return false; |
154 |
| - } |
| 134 | + $expression = $this->buildExpressionForSelector($rule->getSelector()); |
155 | 135 |
|
156 | 136 | /** @var DOMNodeList $elements */
|
157 | 137 | $elements = $xPath->query($expression);
|
@@ -192,4 +172,39 @@ public function purgeCssStore()
|
192 | 172 |
|
193 | 173 | return $this;
|
194 | 174 | }
|
| 175 | + |
| 176 | + private function buildExpressionForSelector(string $selector) |
| 177 | + { |
| 178 | + |
| 179 | + if ($expression = $this->resultCache->fetch($selector)) { |
| 180 | + return $expression; |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + try { |
| 185 | + $expression = $this->cssConverter->toXPath($selector); |
| 186 | + } catch (ExpressionErrorException $expressionErrorException) { |
| 187 | + |
| 188 | + // Allow for pseudo selectors |
| 189 | + // TODO: Find a way to validate this exception without checking strings |
| 190 | + if ($expressionErrorException->getMessage() !== 'Pseudo-elements are not supported.') { |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | + try { |
| 195 | + $tokens = explode(':', $selector); |
| 196 | + $expression = $this->cssConverter->toXPath((string)reset($tokens)); |
| 197 | + } catch (Exception $e) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + } catch (ExceptionInterface $e) { |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + $this->resultCache->save($selector, $expression); |
| 207 | + |
| 208 | + return $expression; |
| 209 | + } |
195 | 210 | }
|
0 commit comments