[CssSelector] Added cache on top of CssSelectorConverter
This commit is contained in:
parent
07818f2747
commit
ed11d526d9
@ -27,6 +27,10 @@ use Symfony\Component\CssSelector\XPath\Translator;
|
|||||||
class CssSelectorConverter
|
class CssSelectorConverter
|
||||||
{
|
{
|
||||||
private $translator;
|
private $translator;
|
||||||
|
private $cache;
|
||||||
|
|
||||||
|
private static $xmlCache = [];
|
||||||
|
private static $htmlCache = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $html Whether HTML support should be enabled. Disable it for XML documents
|
* @param bool $html Whether HTML support should be enabled. Disable it for XML documents
|
||||||
@ -37,6 +41,9 @@ class CssSelectorConverter
|
|||||||
|
|
||||||
if ($html) {
|
if ($html) {
|
||||||
$this->translator->registerExtension(new HtmlExtension($this->translator));
|
$this->translator->registerExtension(new HtmlExtension($this->translator));
|
||||||
|
$this->cache = &self::$htmlCache;
|
||||||
|
} else {
|
||||||
|
$this->cache = &self::$xmlCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->translator
|
$this->translator
|
||||||
@ -57,6 +64,6 @@ class CssSelectorConverter
|
|||||||
*/
|
*/
|
||||||
public function toXPath(string $cssExpr, string $prefix = 'descendant-or-self::')
|
public function toXPath(string $cssExpr, string $prefix = 'descendant-or-self::')
|
||||||
{
|
{
|
||||||
return $this->translator->cssToXPath($cssExpr, $prefix);
|
return $this->cache[$prefix][$cssExpr] ?? $this->cache[$prefix][$cssExpr] = $this->translator->cssToXPath($cssExpr, $prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,10 @@ class CssSelectorConverterTest extends TestCase
|
|||||||
$this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $converter->toXPath('h1.foo'));
|
$this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $converter->toXPath('h1.foo'));
|
||||||
$this->assertEquals('descendant-or-self::foo:h1', $converter->toXPath('foo|h1'));
|
$this->assertEquals('descendant-or-self::foo:h1', $converter->toXPath('foo|h1'));
|
||||||
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
|
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
|
||||||
|
|
||||||
|
// Test the cache layer
|
||||||
|
$converter = new CssSelectorConverter();
|
||||||
|
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCssToXPathXml()
|
public function testCssToXPathXml()
|
||||||
@ -33,6 +37,10 @@ class CssSelectorConverterTest extends TestCase
|
|||||||
$converter = new CssSelectorConverter(false);
|
$converter = new CssSelectorConverter(false);
|
||||||
|
|
||||||
$this->assertEquals('descendant-or-self::H1', $converter->toXPath('H1'));
|
$this->assertEquals('descendant-or-self::H1', $converter->toXPath('H1'));
|
||||||
|
|
||||||
|
$converter = new CssSelectorConverter(false);
|
||||||
|
// Test the cache layer
|
||||||
|
$this->assertEquals('descendant-or-self::H1', $converter->toXPath('H1'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testParseExceptions()
|
public function testParseExceptions()
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
5.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Added an internal cache layer on top of the CssSelectorConverter
|
||||||
|
|
||||||
5.0.0
|
5.0.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user