|
1
|
|
|
<?php |
|
2
|
|
|
/* @description Transformation Style Sheets - Revolutionising PHP templating * |
|
3
|
|
|
* @author Tom Butler [email protected] * |
|
4
|
|
|
* @copyright 2015 Tom Butler <[email protected]> | https://r.je/ * |
|
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * |
|
6
|
|
|
* @version 1.0 */ |
|
7
|
|
|
namespace Transphporm\Pseudo; |
|
8
|
|
|
class Not implements \Transphporm\Pseudo { |
|
9
|
|
|
private $cssToXpath; |
|
10
|
|
|
private $config; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(\Transphporm\Parser\CssToXpath $cssToXpath, \Transphporm\Config $config) { |
|
13
|
|
|
$this->cssToXpath = $cssToXpath; |
|
14
|
|
|
$this->config = $config; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function match($name, $args, \DomElement $element) { |
|
18
|
|
|
if ($name !== 'not') return true; |
|
19
|
|
|
|
|
20
|
|
|
$xpath = new \DomXpath($element->ownerDocument); |
|
21
|
|
|
return $this->notElement($args, $xpath, $element); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
private function notElement($css, $xpath, $element) { |
|
25
|
|
|
|
|
26
|
|
|
foreach ($css as $selector) { |
|
27
|
|
|
$tokenizer = new \Transphporm\Parser\Tokenizer($selector); |
|
28
|
|
|
$xpathString = $this->cssToXpath->getXpath($tokenizer->getTokens()); |
|
29
|
|
|
$pseudo = $this->cssToXpath->getPseudo($tokenizer->getTokens()); |
|
30
|
|
|
$pseudoMatcher = $this->config->createPseudoMatcher($pseudo); |
|
31
|
|
|
if ($this->matches($xpath->query($xpathString), $element, $pseudoMatcher)) return false; |
|
32
|
|
|
} |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function matches($foundElements, $element, $pseudoMatcher) { |
|
37
|
|
|
//Find all nodes matched by the expressions in the brackets :not(EXPR) |
|
38
|
|
|
foreach ($foundElements as $matchedElement) { |
|
39
|
|
|
//Check to see whether this node was matched by the not query |
|
40
|
|
|
if ($pseudoMatcher->matches($matchedElement) && $element->isSameNode($matchedElement)) return true; |
|
41
|
|
|
} |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|