PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.7
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / vendor / symfony / css-selector / CssSelectorConverter.php

CssSelectorConverter.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.7, at vendor/symfony/css-selector/CssSelectorConverter.php

85 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\CssSelector;
13
14 use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser;
15 use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser;
16 use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
17 use Symfony\Component\CssSelector\Parser\Shortcut\HashParser;
18 use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension;
19 use Symfony\Component\CssSelector\XPath\Translator;
20
21 /**
22 * CssSelectorConverter is the main entry point of the component and can convert CSS
23 * selectors to XPath expressions.
24 *
25 * @author Christophe Coevoet <stof@notk.org>
26 */
27 class CssSelectorConverter
28 {
29 public static int $maxCachedItems = 1024;
30
31 private Translator $translator;
32 private array $cache;
33
34 private static array $xmlCache = [];
35 private static array $htmlCache = [];
36
37 /**
38 * @param bool $html Whether HTML support should be enabled. Disable it for XML documents
39 */
40 public function __construct(bool $html = true)
41 {
42 $this->translator = new Translator();
43
44 if ($html) {
45 $this->translator->registerExtension(new HtmlExtension($this->translator));
46 $this->cache = &self::$htmlCache;
47 } else {
48 $this->cache = &self::$xmlCache;
49 }
50
51 $this->translator
52 ->registerParserShortcut(new EmptyStringParser())
53 ->registerParserShortcut(new ElementParser())
54 ->registerParserShortcut(new ClassParser())
55 ->registerParserShortcut(new HashParser())
56 ;
57 }
58
59 /**
60 * Translates a CSS expression to its XPath equivalent.
61 *
62 * Optionally, a prefix can be added to the resulting XPath
63 * expression with the $prefix parameter.
64 */
65 public function toXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string
66 {
67 $cacheKey = $prefix."\0".$cssExpr;
68
69 if (isset($this->cache[$cacheKey])) {
70 // Move the item last in cache (LRU)
71 $value = $this->cache[$cacheKey];
72 unset($this->cache[$cacheKey]);
73
74 return $this->cache[$cacheKey] = $value;
75 }
76
77 if (\count($this->cache) >= self::$maxCachedItems) {
78 // Evict the oldest entry
79 unset($this->cache[array_key_first($this->cache)]);
80 }
81
82 return $this->cache[$cacheKey] = $this->translator->cssToXPath($cssExpr, $prefix);
83 }
84 }
85