in src/Facebook/InstantArticles/Transformer/Rules/ConfigurationSelectorRule.php [73:135]
public function matchesNode($node)
{
// Only matches DOMElements (ignore text and comments)
if (!Type::is($node, 'DOMElement')) {
return false;
}
// Handles selector = tag
if ($node->nodeName === $this->selector) {
return true;
}
// Handles selector = .class
if (preg_match('/^\.[a-zA-Z][a-zA-Z0-9-]*$/', $this->selector) === 1) {
// Tries every class
$classNames = explode(' ', $node->getAttribute('class'));
foreach ($classNames as $className) {
if ('.' . $className === $this->selector) {
return true;
}
}
// No match!
return false;
}
// Handles selector = tag.class
if (preg_match('/^[a-zA-Z][a-zA-Z0-9-]*(\.[a-zA-Z][a-zA-Z0-9-]*)?$/', $this->selector) === 1) {
// Tries every class
$classNames = explode(' ', $node->getAttribute('class'));
foreach ($classNames as $className) {
if ($node->nodeName . '.' . $className === $this->selector) {
return true;
}
}
// No match!
return false;
}
// Proceed with the more expensive XPath query
$document = $node->ownerDocument;
$domXPath = new \DOMXPath($document);
if (substr($this->selector, 0, 1) === '/') {
$xpath = $this->selector;
} else {
$converter = new CssSelectorConverter();
$xpath = $converter->toXPath($this->selector === null ? "" : $this->selector);
}
$results = $domXPath->query($xpath);
if (false !== $results) {
foreach ($results as $result) {
if ($result === $node) {
return true;
}
}
}
return false;
}