Skip to content

Commit

Permalink
pseudo element functions use array lookup instead of loop
Browse files Browse the repository at this point in the history
  • Loading branch information
TRPB committed Feb 2, 2020
1 parent 7e24b6b commit 3923053
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Config.php
Expand Up @@ -72,8 +72,8 @@ public function registerContentPseudo($name, Property\ContentPseudo $pseudo) {
if (isset($this->properties['content'])) $this->properties['content']->addContentPseudo($name, $pseudo);
}

public function registerPseudo(Pseudo $pseudo) {
$this->pseudo[] = $pseudo;
public function registerPseudo($name, Pseudo $pseudo) {
$this->pseudo[$name] = $pseudo;
}

public function loadProperties(Hook\PropertyHook $hook) {
Expand All @@ -82,7 +82,7 @@ public function loadProperties(Hook\PropertyHook $hook) {

public function createPseudoMatcher($pseudo) {
$pseudoMatcher = new Hook\PseudoMatcher($pseudo, $this->valueParser);
foreach ($this->pseudo as $pseudoFunction) $pseudoMatcher->registerFunction(clone $pseudoFunction);
foreach ($this->pseudo as $name => $pseudoFunction) $pseudoMatcher->registerFunction($name, clone $pseudoFunction);
return $pseudoMatcher;
}

Expand Down
12 changes: 7 additions & 5 deletions src/Hook/PseudoMatcher.php
Expand Up @@ -18,17 +18,19 @@ public function __construct($pseudo, \Transphporm\Parser\Value $valueParser) {
$this->valueParser = $valueParser;
}

public function registerFunction(\Transphporm\Pseudo $pseudo) {
$this->functions[] = $pseudo;
public function registerFunction($name, \Transphporm\Pseudo $pseudo) {
$this->functions[$name] = $pseudo;
}

public function matches($element) {
foreach ($this->pseudo as $i => $tokens) {
$parts = $this->getFuncParts($i, $tokens);
foreach ($this->functions as $function) {
$matches = $this->match($parts, $function, $element);
if ($matches === false) return false;
if ($parts['name'] === null) $parts['name'] = 'data';
if (!isset($this->functions[$parts['name']])) return true;
if ($this->match($parts, $this->functions[$parts['name']], $element) === false) {
return false;
}

}
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Module/Pseudo.php
Expand Up @@ -9,8 +9,10 @@
class Pseudo implements \Transphporm\Module {

public function load(\Transphporm\Config $config) {
$config->registerPseudo(new \Transphporm\Pseudo\Attribute());
$config->registerPseudo(new \Transphporm\Pseudo\Nth());
$config->registerPseudo(new \Transphporm\Pseudo\Not($config->getCssToXpath(), $config));
$config->registerPseudo('data', new \Transphporm\Pseudo\Attribute());
$config->registerPseudo('iteration', new \Transphporm\Pseudo\Attribute());
$config->registerPseudo('root', new \Transphporm\Pseudo\Attribute());
$config->registerPseudo('nth-child', new \Transphporm\Pseudo\Nth());
$config->registerPseudo('not', new \Transphporm\Pseudo\Not($config->getCssToXpath(), $config));
}
}
2 changes: 1 addition & 1 deletion src/Pseudo/Attribute.php
Expand Up @@ -7,7 +7,7 @@
namespace Transphporm\Pseudo;
class Attribute implements \Transphporm\Pseudo {
public function match($name, $args, \DomElement $element) {
if (!($name === null || in_array($name, ['data', 'iteration', 'root']))) return true;
if ($name === null) return true;
return $args[0];
}
}

0 comments on commit 3923053

Please sign in to comment.