public function testConfigLocalOverride() { $dom = new Dom(); $dom->setOptions(['whitespaceTextNode' => false]); $dom->load('<div><p id="hey">Hey you</p> <p id="ya">Ya you!</p></div>', ['whitespaceTextNode' => true]); $this->assertEquals(' ', $dom->getElementById('hey')->nextSibling()->text); }
public function testRemoveScriptsFalse() { $dom = new Dom(); $dom->setOptions(['removeScripts' => false]); $dom->loadFromFile('tests/files/horrible.html'); $this->assertEquals(1, count($dom->find('script'))); $this->assertEquals('text/JavaScript', $dom->find('script')->getAttribute('type')); }
public function testConfigStrictMissingAttribute() { $dom = new Dom(); $dom->setOptions(['strict' => true]); try { // should throw an exception $dom->load('<div><p id="hey" block>Hey you</p> <p id="ya">Ya you!</p></div>'); // we should not get here $this->assertTrue(false); } catch (StrictException $e) { $this->assertEquals("Tag 'p' has an attribute 'block' with out a value! (character #22)", $e->getMessage()); } }
public function run(Page $page) { // Parse the HTML $dom = new Dom(); $dom->setOptions(array('removeScripts' => false, 'removeStyles' => false, 'preserveLineBreaks' => true)); $dom->load($page->body()); // Format foreach ($dom->find($this->selector) as $node) { // Format the node $formattedNode = $this->format($node); // Remove all children foreach ($node->find('*') as $child) { $child->delete(); } // Add the new node $node->addChild($formattedNode); } // Set and return return $page->withBody($dom->root->outerHtml()); }
public function page($path, array $context = array()) { $path = array_filter(explode('/', $path), 'strlen'); $pathname = implode('.', $path); $file = sprintf($this->path, $pathname); if (!file_exists($file)) { throw new PageNotFoundException($path); } // Parse the HTML $dom = new Dom(); $dom->setOptions(array('removeScripts' => false, 'removeStyles' => false, 'preserveLineBreaks' => true)); extract($context); ob_start(); include $file; $dom->load(ob_get_clean()); // Create a new page $page = new Page($path); // Title if (($title = $dom->find('title', 0)) !== null) { $page->withTitle($title->text()); } // Header if (($head = $dom->find('head', 0)) !== null) { foreach ($head->getChildren() as $child) { if ($child->getTag()->name() !== 'title' && !($child->getTag()->name() === 'meta' && $child->getAttribute('charset') !== null)) { $page->withHeader($page->header() . $child->outerHtml()); } } } // Body if (($body = $dom->find('body', 0)) !== null) { $page->withBody($body->innerHtml()); } // Return page return $page; }