Skip to content
Permalink
Browse files

feature #35425 [CssSelector] Added cache on top of CssSelectorConvert…

…er (lyrixx)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[CssSelector] Added cache on top of CssSelectorConverter

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Commits
-------

ed11d52 [CssSelector] Added cache on top of CssSelectorConverter
  • Loading branch information
fabpot committed Jan 27, 2020
2 parents b22a584 + ed11d52 commit 1cfadb41dc7c8cb9e8f7222f095b344f8bc52542
@@ -27,6 +27,10 @@
class CssSelectorConverter
{
private $translator;
private $cache;

private static $xmlCache = [];
private static $htmlCache = [];

/**
* @param bool $html Whether HTML support should be enabled. Disable it for XML documents
@@ -37,6 +41,9 @@ public function __construct(bool $html = true)

if ($html) {
$this->translator->registerExtension(new HtmlExtension($this->translator));
$this->cache = &self::$htmlCache;
} else {
$this->cache = &self::$xmlCache;
}

$this->translator
@@ -57,6 +64,6 @@ public function __construct(bool $html = true)
*/
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,13 +26,21 @@ public function testCssToXPath()
$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::h1', $converter->toXPath('H1'));

// Test the cache layer
$converter = new CssSelectorConverter();
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
}

public function testCssToXPathXml()
{
$converter = new CssSelectorConverter(false);

$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()
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* Added an internal cache layer on top of the CssSelectorConverter

5.0.0
-----

0 comments on commit 1cfadb4

Please sign in to comment.
You can’t perform that action at this time.