/** * @group GlobalFunctions */ public function testFunctionWithContent() { $dom = new DOMDocument(); $node = $dom->appendChild($dom->createElement('html')); $fd = FluentDOM($node); $this->assertTrue($fd instanceof FluentDOM); $this->assertTag(array('tag' => 'html'), $fd->document); }
public function testCollectionPath() { $feedPath = $this->getFoodieMultilevelPath(); // id nodes should score 1 $scorer = new IsUnique(); $this->assertEquals(1, $scorer->__invoke(FluentDOM($feedPath)->find('/items/item[position()=1]/id')[0])); // city should score 0 (all city fields are the same) $scorer = new IsUnique(); $this->assertEquals(0, $scorer->__invoke(FluentDOM($feedPath)->find('/items/item[position()=1]/city')[0])); // score with an exponent $scorer = new IsUnique(2); $this->assertEquals(1, $scorer->__invoke(FluentDOM($feedPath)->find('/items/item[position()=1]/id')[0])); $scorer = new IsUnique(2); $this->assertEquals(0, $scorer->__invoke(FluentDOM($feedPath)->find('/items/item[position()=1]/city')[0])); }
/** * Check if DOMNode is unique (compared to it's siblings) */ public function __invoke(DOMNode $node) { $nodePath = (new Utils())->toXPath($node); // as all nodes with the same path will have the same score, // we cache pr. path name if (!isset(self::$pathCache[$nodePath])) { $collection = FluentDOM($node->ownerDocument)->find($nodePath); $grouped = (new Utils())->groupByContentHash($collection); $totalCount = count($collection); $uniqueCount = count($grouped); // special case for uniqueCount 1: // not unique at all, return 0 if ($uniqueCount === 1) { $score = 0; } else { $score = $uniqueCount / $totalCount; } self::$pathCache[$nodePath] = $score; } return self::$pathCache[$nodePath]; }
public function execute($request) { if (!$this->context->user->hasCredential('administrator') || !sfConfig::get('app_check_for_updates')) { return sfView::NONE; } $this->currentVersion = qubitConfiguration::VERSION; $this->updateCheckUrl = 'http://updatecheck.qubit-toolkit.org/check/'; $this->cookiePath = sfContext::getInstance()->request->getRelativeUrlRoot(); if (1 > strlen($this->cookiePath)) { $this->cookiePath = '/'; } $this->updateCheckData = array(); $this->updateCheckData['address'] = $request->getUriPrefix() . $request->getScriptName() . $request->getPathInfo(); $this->updateCheckData['version'] = qubitConfiguration::VERSION . ' - ' . sfConfig::get('app_version'); if (null === ($this->updateCheckData['distribution'] = $this->context->user->getAttribute('distribution'))) { $packageXmlPath = sfConfig::get('sf_config_dir') . '/package.xml'; if (file_exists($packageXmlPath)) { require_once sfConfig::get('sf_root_dir') . '/vendor/FluentDOM/FluentDOM.php'; $fd = FluentDOM($packageXmlPath)->namespaces(array('p' => 'http://pear.php.net/dtd/package-2.0')); $this->context->user->setAttribute('distribution', $this->updateCheckData['distribution'] = $fd->find('/*/p:name')->item(0)->textContent); } } $this->updateCheckData['site_description'] = sfConfig::get('app_siteDescription'); $this->updateCheckData['site_title'] = sfConfig::get('app_siteTitle'); if (!$request->getCookie('has_js')) { if (null === ($this->lastVersion = $this->context->user->getAttribute('last_version'))) { try { $browser = new sfWebBrowser(); $this->lastVersion = $browser->post($this->updateCheckUrl, $this->updateCheckData)->getResponseText(); } catch (Exception $e) { $this->lastVersion = 0; } $this->context->user->setAttribute('last_version', $this->lastVersion); } if (0 == $this->lastVersion || 1 > version_compare($this->lastVersion, qubitConfiguration::VERSION)) { return sfView::NONE; } } }
public static function checkDependencies() { require_once sfConfig::get('sf_root_dir') . '/vendor/FluentDOM/FluentDOM.php'; $dependencies = array(); // Check if any dependencies are defined $packageXmlPath = sfConfig::get('sf_config_dir') . '/package.xml'; if (!file_exists($packageXmlPath)) { return $dependencies; } $fd = FluentDOM($packageXmlPath)->namespaces(array('p' => 'http://pear.php.net/dtd/package-2.0')); // Check if a minimum PHP version is defined, and if it is less than our // current version if (0 < strlen($min = $fd->find('p:dependencies/p:required/p:php/p:min')) && 0 > version_compare(PHP_VERSION, $min)) { $dependencies['php']['min'] = $min; } foreach ($fd->find('p:dependencies/*/p:extension/p:name') as $node) { if (!extension_loaded($node->textContent)) { $dependencies['extensions'][] = $node->textContent; } } return $dependencies; }
<?php /** * * @version $Id: insertAfter.php 322 2009-09-14 20:19:48Z subjective $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p> is what I said... </p><div id="foo">FOO!</div> </body> </html> XML; require_once '../FluentDOM.php'; echo FluentDOM($xml)->find('//p')->insertAfter('//div[@id = "foo"]');
<?php /** * * @version $Id: find.php 322 2009-09-14 20:19:48Z subjective $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p><span>Hello</span>, how are you?</p> <p>Me? I'm <span>good</span>.</p> </body> </html> XML; require_once '../FluentDOM.php'; echo FluentDOM($xml)->find('//p')->find('span')->addClass('red');
/** * * @version $Id: add.php 321 2009-09-14 20:17:23Z lapis $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p>I would like to say: <b>HELLO</b></p> <b>HELLO</b> <div>Another list of childNodes</div> </body> </html> XML; require_once '../FluentDOM.php'; $dom = FluentDOM($xml); echo $dom->find('//p')->add('//p/b')->toggleClass('inB'); echo "\n\n"; $dom = FluentDOM($xml); echo $dom->find('//p')->add($dom->find('//div'))->toggleClass('inB'); echo "\n\n"; $dom = FluentDOM($xml); echo $dom->add($dom->find('//div'))->toggleClass('inB'); echo "\n\n"; $dom = FluentDOM($xml); echo $dom->add('//div')->toggleClass('inB');
<?php /** * * @version $Id: wrap.php 322 2009-09-14 20:19:48Z subjective $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p>Hello</p> <p>cruel</p> <p>World</p> </body> </html> XML; require_once '../FluentDOM.php'; echo FluentDOM($xml)->find('//p')->wrap('<div class="outer"><div class="inner"></div></div>');
<?php /** * * @version $Id$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p>Hello</p> <p>cruel</p> <p>World</p> </body> </html> XML; require_once '../src/FluentDOM.php'; echo FluentDOM($xml)->find('//p')->replaceWith('<b>Paragraph. </b>');
<?php /** * * @version $Id$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <span>I have nothing more to say... </span> <div id="foo">FOO! </div> </body> </html> XML; require_once '../src/FluentDOM.php'; echo FluentDOM($xml)->find('//span')->appendTo('//div[@id = "foo"]');
<?php require '../../src/FluentDOM.php'; $fd = FluentDOM('find.xml'); // find the document element <root> var_dump($fd->find('/root')->item(0)->nodeName); //find the first <child> in <root> var_dump($fd->find('/root/child')->item(0)->textContent); //find the all <child>s anywhere in the document foreach ($fd->find('//child') as $child) { var_dump($child->textContent); } //find the <root> first then the second element in it var_dump($fd->find('/root')->find('*[2]')->item(0)->textContent);
<?php header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p>Hello</p> <p>cruel</p> <p>World</p> </body> </html> XML; require_once '../../FluentDOM.php'; $dom = FluentDOM($xml)->find('//p'); echo $dom[0]->textContent, ' ', $dom[2]->textContent;
<?php /** * * @version $Id$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <div id="foo">FOO!</div><p>I would like to say: </p> </body> </html> XML; require_once '../src/FluentDOM.php'; echo FluentDOM($xml)->find('//p')->insertBefore('//div[@id = "foo"]');
<?php /** * Example file for property 'attr' * * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2010 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $html = <<<HTML <html> <head> <title>FluentDOM project page</title> </head> <body> <p style="color: red;"> Always nice to visit <a href='http://fluentdom.org' target="_blank">here</a> or <a href='http://github.org/FluentDOM' target="_blank">here.</a> </p> </body> </html> HTML; echo "Example for property 'attr' - remove attributes:\n\n"; require_once '../../src/FluentDOM.php'; $fd = FluentDOM($html, 'text/html')->find('/html/body//*'); // like $fd->removeAttr(array('style', 'target')); but array syntax unset($fd->attr[array('style', 'target')]); echo (string) $fd;
/** * Check text node content for strings, split them by the strings, and replace strings with * a node structure. By using the DOM functions and not a somple replace, we can avoid * any problems with encodings. * * @param DOMText $node */ public function replace(DOMText $node) { if (preg_match($this->_pattern, $node->nodeValue)) { // split using the pattern, but capture the delimiter strings $parts = preg_split($this->_pattern, $node->nodeValue, -1, PREG_SPLIT_DELIM_CAPTURE); $items = array(); foreach ($parts as $part) { $string = strtolower($part); if (isset($this->_highlights[$string])) { $span = $node->ownerDocument->createElement('span'); $items[] = FluentDOM($span)->addClass($this->_highlights[$string])->text($part)->item(0); } else { $items[] = $node->ownerDocument->createTextNode($part); } } // replace the text node FluentDOM($node)->replaceWith($items); } }
<?php header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p>Hello</p> <p>cruel</p> <p>World</p> </body> </html> XML; require_once '../../FluentDOM.php'; foreach (FluentDOM($xml)->find('//p') as $key => $value) { echo $key, ': ', $value->nodeName, "\n"; }
public static function parseDateRange($node) { $fd = FluentDOM($node)->namespaces(array('eac' => 'urn:isbn:1-931666-33-4')); $range = array($fd->find('eac:fromDate')->attr('standardDate'), $fd->find('eac:toDate')->attr('standardDate')); return $range; }
function callback($node) { $fluentNode = FluentDOM($node); $fluentNode->prepend($fluentNode->document->createTextNode($fluentNode->parent()->item(0)->tagName . ' > ')); }
* @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <ul> <li>One</li> <li>Two</li> <li class="hilite">Three</li> <li>Four</li> </ul> <ul> <li>Five</li> <li>Six</li> <li>Seven</li> </ul> <ul> <li>Eight</li> <li class="hilite">Nine</li> <li>Ten</li> <li class="hilite">Eleven</li> </ul> <p>Unique siblings: <b></b></p> </body> </html> XML; require_once '../FluentDOM.php'; echo FluentDOM($xml)->find('//li[@class = "hilite"]')->siblings()->addClass('before');
<?php /** * * @version $Id$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p class="first">Hello One</p> <p>Hello Two</p> </body> </html> XML; require_once '../src/FluentDOM.php'; echo FluentDOM($xml)->find('//p[@class = "first"]')->empty();
<?php // include FluentDOM require '../../FluentDOM.php'; // define url $url = 'http://www.papaya-cms.com/'; // load data from an url $html = file_get_contents($url); $fd = FluentDOM($html, 'html')->find('//a[@href]')->each(function ($node) use($url) { //convert node to FluentDOM $item = FluentDOM($node); // check for relative url if (!preg_match('(^[a-zA-Z]+://)', $item->attr('href'))) { // add base url $item->attr('href', $url . $item->attr('href')); } }); // change content type $fd->contentType = 'xml'; // send content type header header('Content-type: text/xml'); // output new document echo $fd;
<?php /** * * @version $Id$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p>Hello</p> <p>cruel</p> <p>World</p> </body> </html> XML; require_once '../src/FluentDOM.php'; echo FluentDOM($xml)->find('//p[position() = last()]')->xml('<b>New</b>World');
<?php /** * * @version $Id$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p>Hello</p> <p>cruel</p> <p>World</p> </body> </html> XML; require_once '../src/FluentDOM.php'; echo FluentDOM($xml)->find('//p')->wrapInner('<b></b>');
<?php /** * * @version $Id$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <div>first</div> <div>sibling<div>child</div></div> <div>sibling</div> <div>sibling</div> </body> </html> XML; require_once '../src/FluentDOM.php'; echo FluentDOM($xml)->find('//div[position() = 1]')->nextAll()->addClass('after');
$categories = array_unique($dom->find('//_:category')->map('callbackCategoryTerm')); if (count($categories) > 0) { echo '<ul class="categories">' . "\n"; foreach ($categories as $category) { printf('<li><a href="?label=%s">%s</a></li>' . "\n", urlencode($category), htmlspecialchars($category)); } echo '</ul>' . "\n"; } if (empty($_GET['label'])) { $expr = '//_:entry'; } else { $expr = '//_:entry[_:category[@term = "' . htmlspecialchars($_GET['label']) . '"]]'; } foreach ($dom->find($expr) as $entryNode) { echo '<div class="entry">' . "\n"; $entry = FluentDOM($entryNode); printf('<h2><a href="%s">%s</a></h2>' . "\n", htmlspecialchars($entry->find('.//_:link[@rel = "alternate" and @type = "text/html"]')->attr('href')), htmlspecialchars($entry->find('.//_:title')->text())); $summary = $entry->find('.//_:summary|.//_:content'); switch ($summary->attr('type')) { case 'html': case 'text/html': //in real world you whould need to use a purifier printf('<div class="summary">%s</div>' . "\n", $summary->text()); break; case 'text': case 'text/plain': case '': printf('<div class="summary">%s</div>' . "\n", htmlspecialchars($summary->text())); break; default: break;
/** * * * @param $node * @param $index * @return string | array */ function getNodeAttribValue($node, $index) { return FluentDOM($node)->attr('value'); }
<?php /** * * @version $Id: prev.php 322 2009-09-14 20:19:48Z subjective $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <div><span>has child</span></div> <div id="start"></div> <div></div> <div><span>has child</span></div> <div class="here"><span>has child</span></div> <div><span>has child</span></div> <div class="here"></div> <div></div> <p><button>Go to Prev</button></p> </body> </html> XML; require_once '../FluentDOM.php'; echo FluentDOM($xml)->find('//div[@id = "start"]')->prev()->addClass('before'); echo "\n\n"; echo FluentDOM($xml)->find('//div[@class= "here"]')->prev('.//span')->addClass('nextTest');
<?php /** * * @version $Id: before.php 322 2009-09-14 20:19:48Z subjective $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert */ header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <p> is what I said...</p> </body> </html> XML; require_once '../FluentDOM.php'; echo FluentDOM($xml)->find('//p')->before(' World')->before('<b>Hello</b>');
header('Content-type: text/plain'); $xml = <<<XML <html> <head></head> <body> <div> <p>Hello</p> <p>cruel</p> <p>World!</p> <p>I am</p> <p>leaving</p> <p>you today!</p> </div> </body> </html> XML; require_once '../FluentDOM.php'; /* * get first 3 paragraphs of the document and replace every <div> element with them */ echo FluentDOM($xml)->find('//p')->slice(0, 3)->replaceAll('//div'); echo "\n\n"; echo FluentDOM($xml)->find('//p')->slice(5, 2)->replaceAll('//div'); echo "\n\n"; echo FluentDOM($xml)->find('//p')->slice(1, -2)->replaceAll('//div'); echo "\n\n"; /* * get all paragraphs after the first 3 of the document and replace every <div> element with them */ echo FluentDOM($xml)->find('//p')->slice(3)->replaceAll('//div');