/** * Perform the query on Document * * @param string $expression CSS selector or XPath query * @param Document $document Document to query * @param string $type The type of $expression * @return NodeList */ public static function execute($expression, Document $document, $type = self::TYPE_XPATH) { // Expression check if ($type === static::TYPE_CSS) { $expression = static::cssToXpath($expression); } $xpath = new DOMXPath($document->getDomDocument()); $xpathNamespaces = $document->getXpathNamespaces(); foreach ($xpathNamespaces as $prefix => $namespaceUri) { $xpath->registerNamespace($prefix, $namespaceUri); } if ($xpathPhpfunctions = $document->getXpathPhpFunctions()) { $xpath->registerNamespace('php', 'http://php.net/xpath'); $xpathPhpfunctions === true ? $xpath->registerPHPFunctions() : $xpath->registerPHPFunctions($xpathPhpfunctions); } $nodeList = $xpath->queryWithErrorException($expression); return new NodeList($nodeList); }
public function testDom() { // Gerenciador de Serviços $serviceManager = Application::getApplication()->getServiceManager(); // Resolução de Scripts $resolver = new TemplateMapResolver(['error/404' => './module/Balance/view/error/404.phtml', 'layout/page-header' => './module/Balance/view/layout/page-header.phtml']); // Renderização $renderer = (new PhpRenderer())->setResolver($resolver); // Configurar Serviço $renderer->getHelperPluginManager()->setServiceLocator($serviceManager); // Camada de Visualização $view = (new ViewModel())->setTemplate('error/404'); // Configurar Controladora $view->setVariable('controller', 'FooController'); // Renderização $content = $renderer->render($view); // Apresentar Estrutura $document = new Document($content); // Capturar DOM $document->getDomDocument(); // Validação $this->assertEmpty($document->getErrors(), 'Invalid Document Structure'); }
/** * Execute a DOM/XPath query * * @param string $path * @param bool $useXpath * @return Document\NodeList */ private function query($path, $useXpath = false) { $response = $this->getResponse(); $document = new Document($response->getContent()); if ($useXpath) { $document->registerXpathNamespaces($this->xpathNamespaces); } $result = Document\Query::execute($path, $document, $useXpath ? Document\Query::TYPE_XPATH : Document\Query::TYPE_CSS); return $result; }
/** * @group ZF-3938 */ public function testAllowsSpecifyingEncodingAtConstruction() { $doc = new Document($this->getHtml(), null, 'iso-8859-1'); $this->assertEquals('iso-8859-1', $doc->getEncoding()); }