DiDom\Query::compile PHP Method

compile() public static method

Converts a CSS selector into an XPath expression.
public static compile ( string $expression, string $type = self::TYPE_CSS ) : string
$expression string XPath expression or CSS selector
$type string The type of the expression
return string XPath expression
    public static function compile($expression, $type = self::TYPE_CSS)
    {
        if (strcasecmp($type, self::TYPE_XPATH) === 0) {
            return $expression;
        }
        $selectors = explode(',', $expression);
        $paths = [];
        foreach ($selectors as $selector) {
            $selector = trim($selector);
            if (array_key_exists($selector, static::$compiled)) {
                $paths[] = static::$compiled[$selector];
                continue;
            }
            static::$compiled[$selector] = static::cssToXpath($selector);
            $paths[] = static::$compiled[$selector];
        }
        return implode('|', $paths);
    }

Usage Example

Example #1
0
 public function testGetCompiled()
 {
     Query::setCompiled([]);
     $selector = '.post h2';
     $xpath = '//*[contains(concat(" ", normalize-space(@class), " "), " post ")]//h2';
     $compiled = [$selector => $xpath];
     Query::compile($selector);
     $this->assertEquals($compiled, Query::getCompiled());
 }
All Usage Examples Of DiDom\Query::compile