コード例 #1
0
 protected function compare()
 {
     $bench = new Ubench();
     $url = 'tests/templated-retrospect/index.html';
     $file = 'test.html';
     if (!file_exists($file)) {
         $htmlstr = file_get_contents($url);
         file_put_contents($file, $htmlstr);
     }
     $htmlstr = file_get_contents($file);
     $this->log('', true);
     $this->log('Measuring Simple HTML DOM Parser...');
     $resultsSimpleHtmlDomParser = $bench->run(function ($htmlstr) {
         $results = [];
         $html = HtmlDomParser::str_get_html($htmlstr);
         $html->find('title', 0)->innertext('New Title');
         $results[1] = $html->__toString();
         $tpl = HtmlDomParser::str_get_html(file_get_contents('tests/templated-retrospect/index.html'));
         foreach ($tpl->find('link') as $elem) {
             $elem->href = '//localhost/xparser/tests/templated-retrospect/' . $elem->href;
         }
         foreach ($tpl->find('img, script') as $elem) {
             $elem->src = '//localhost/xparser/tests/templated-retrospect/' . $elem->src;
         }
         $results[2] = $tpl->__toString();
         return $results;
     }, $htmlstr);
     //$this->log('distance: ' . similar_text($htmlstr, $result));
     $this->logBench($bench);
     $this->log('', true);
     $this->log('Measuring XParser...');
     $resultsXParser = $bench->run(function ($htmlstr) {
         $results = [];
         $html = new XNode($htmlstr);
         $html->find('title')->inner('New Title');
         $results[1] = $html->__toString();
         $tpl = new XNode(file_get_contents('tests/templated-retrospect/index.html'));
         foreach ($tpl('link') as $elem) {
             $elem->href = '//localhost/xparser/tests/templated-retrospect/' . $elem->href;
         }
         foreach ($tpl('img, script') as $elem) {
             $elem->src = '//localhost/xparser/tests/templated-retrospect/' . $elem->src;
         }
         $results[2] = $tpl->__toString();
         return $results;
     }, $htmlstr);
     //$this->log('distance: ' . similar_text($htmlstr, $result));
     $this->logBench($bench);
     $this->log('', true);
     $this->log('Measuring Ganon...');
     $resultsGanon = $bench->run(function ($htmlstr) {
         $html = str_get_dom($htmlstr);
         foreach ($html('title') as $title) {
             $title->setInnerText('New Title');
         }
         $results[1] = $html->__toString();
         $tpl = new XNode(file_get_contents('tests/templated-retrospect/index.html'));
         foreach ($tpl('link') as $elem) {
             $elem->href = '//localhost/xparser/tests/templated-retrospect/' . $elem->href;
         }
         foreach ($tpl('img, script') as $elem) {
             $elem->src = '//localhost/xparser/tests/templated-retrospect/' . $elem->src;
         }
         $results[2] = $tpl->__toString();
         return $results;
     }, $htmlstr);
     //$this->log('distance: ' . similar_text($htmlstr, $result));
     $this->logBench($bench);
     $this->log('', true);
     $this->log('Symfony CSS Selector combined with DOMDocument and DOMXPath...');
     $resultsXParser = $bench->run(function ($htmlstr) {
         $results = [];
         $html = new DOMDocument();
         libxml_use_internal_errors(true);
         $html->loadHTML($htmlstr);
         $converter = new CssSelectorConverter();
         $xpath = new DOMXPath($html);
         $elements = $xpath->query($converter->toXPath('title'));
         foreach ($elements as $element) {
             $element->innserHTML = 'New Title';
         }
         $results[1] = $html->saveHTML();
         $tpl = new DOMDocument();
         $tpl->load('tests/templated-retrospect/index.html');
         foreach ($xpath->query($converter->toXPath('link')) as $elem) {
             $elem->setAttribute('href', '//localhost/xparser/tests/templated-retrospect/' . $elem->getAttribute('href'));
         }
         foreach ($xpath->query($converter->toXPath('img, script')) as $elem) {
             $elem->setAttribute('src', '//localhost/xparser/tests/templated-retrospect/' . $elem->getAttribute('src'));
         }
         $results[2] = $tpl->saveHTML();
         return $results;
     }, $htmlstr);
     //$this->log('distance: ' . similar_text($htmlstr, $result));
     $this->logBench($bench);
     $this->log('', true);
     $this->log('Simple HTML DOM Parser vs Ganon distance: ' . similar_text($resultsSimpleHtmlDomParser[2], $resultsGanon[2]));
     $this->log('Simple HTML DOM Parser vs XParser distance: ' . similar_text($resultsSimpleHtmlDomParser[2], $resultsXParser[2]));
     $this->log('Ganon vs XParser distance: ' . similar_text($resultsGanon[2], $resultsXParser[2]));
     $this->log('', true);
     $this->log('', true);
 }
コード例 #2
0
ファイル: XParserTests.php プロジェクト: gymadarasz/xparser
    public function test6()
    {
        $html = <<<HTML

<!DOCTYPE HTML>
<!--
\tRetrospect by TEMPLATED
\ttemplated.co @templatedco
\tReleased for free under the Creative Commons Attribution 3.0 license (templated.co/license)
-->
<html>
\t<head>
\t\t<title>Retrospect by TEMPLATED</title>
\t\t<meta charset="utf-8" />
\t\t<meta name="viewport" content="width=device-width, initial-scale=1" />
\t\t<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
\t\t<link rel="stylesheet" href="assets/css/main.css" />
\t\t<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
\t\t<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]-->
\t</head>
\t<body class="landing">

\t\t<!-- Header -->
\t\t\t\t
\t\t\t<header id="header" class="alt">
\t\t\t\t<h1><a href="index.html">Retrospect</a></h1>
\t\t\t\t<a href="#nav">Menu</a>
\t\t\t</header>
\t\t\t\t

\t\t<!-- Nav -->
\t\t\t\t
\t\t\t<nav id="nav">
\t\t\t\t<ul class="links">
\t\t\t\t\t<li><a href="index.html">Home</a></li>
\t\t\t\t\t<li><a href="generic.html">Generic</a></li>
\t\t\t\t\t<li><a href="elements.html">Elements</a></li>
\t\t\t\t</ul>
\t\t\t</nav>
\t\t\t\t
\t\t<!-- Banner -->
\t\t\t\t
\t\t\t<section id="banner">
\t\t\t\t<i class="icon fa-diamond"></i>
\t\t\t\t<h2>Etiam adipiscing</h2>
\t\t\t\t<p>Magna feugiat lorem dolor egestas</p>
\t\t\t\t<ul class="actions">
\t\t\t\t\t<li><a href="#" class="button big special">Learn More</a></li>
\t\t\t\t</ul>
\t\t\t</section>
\t\t\t\t
\t\t<!-- One -->
\t\t\t\t
\t\t\t<section id="one" class="wrapper style1">
\t\t\t\t<div class="inner">
\t\t\t\t\t<article class="feature left">
\t\t\t\t\t\t<span class="image"><img src="images/pic01.jpg" alt="" /></span>
\t\t\t\t\t\t<div class="content">
\t\t\t\t\t\t\t<h2>Integer vitae libero acrisus egestas placerat  sollicitudin</h2>
\t\t\t\t\t\t\t<p>Sed egestas, ante et vulputate volutpat, eros pede semper est, vitae luctus metus libero eu augue. Morbi purus libero, faucibus adipiscing, commodo quis, gravida id, est.</p>
\t\t\t\t\t\t\t<ul class="actions">
\t\t\t\t\t\t\t\t<li>
\t\t\t\t\t\t\t\t\t<a href="#" class="button alt">More</a>
\t\t\t\t\t\t\t\t</li>
\t\t\t\t\t\t\t</ul>
\t\t\t\t\t\t</div>
\t\t\t\t\t</article>
\t\t\t\t\t<article class="feature right">
\t\t\t\t\t\t<span class="image"><img src="images/pic02.jpg" alt="" /></span>
\t\t\t\t\t\t<div class="content">
\t\t\t\t\t\t\t<h2>Integer vitae libero acrisus egestas placerat  sollicitudin</h2>
\t\t\t\t\t\t\t<p>Sed egestas, ante et vulputate volutpat, eros pede semper est, vitae luctus metus libero eu augue. Morbi purus libero, faucibus adipiscing, commodo quis, gravida id, est.</p>
\t\t\t\t\t\t\t<ul class="actions">
\t\t\t\t\t\t\t\t<li>
\t\t\t\t\t\t\t\t\t<a href="#" class="button alt">More</a>
\t\t\t\t\t\t\t\t</li>
\t\t\t\t\t\t\t</ul>
\t\t\t\t\t\t</div>
\t\t\t\t\t</article>
\t\t\t\t</div>
\t\t\t</section>
\t\t\t\t
\t\t<!-- Two -->
\t\t\t\t
\t\t\t<section id="two" class="wrapper special">
\t\t\t\t<div class="inner">
\t\t\t\t\t<header class="major narrow">
\t\t\t\t\t\t<h2>Aliquam Blandit Mauris</h2>
\t\t\t\t\t\t<p>Ipsum dolor tempus commodo turpis adipiscing Tempor placerat sed amet accumsan</p>
\t\t\t\t\t</header>
\t\t\t\t\t<div class="image-grid">
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic03.jpg" alt="" /></a>
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic04.jpg" alt="" /></a>
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic05.jpg" alt="" /></a>
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic06.jpg" alt="" /></a>
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic07.jpg" alt="" /></a>
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic08.jpg" alt="" /></a>
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic09.jpg" alt="" /></a>
\t\t\t\t\t\t<a href="#" class="image"><img src="images/pic10.jpg" alt="" /></a>
\t\t\t\t\t</div>
\t\t\t\t\t<ul class="actions">
\t\t\t\t\t\t<li><a href="#" class="button big alt">Tempus Aliquam</a></li>
\t\t\t\t\t</ul>
\t\t\t\t</div>
\t\t\t</section>
\t\t\t\t
\t\t<!-- Three -->
\t\t\t\t
\t\t\t<section id="three" class="wrapper style3 special">
\t\t\t\t<div class="inner">
\t\t\t\t\t<header class="major narrow\t">
\t\t\t\t\t\t<h2>Magna sed consequat tempus</h2>
\t\t\t\t\t\t<p>Ipsum dolor tempus commodo turpis adipiscing Tempor placerat sed amet accumsan</p>
\t\t\t\t\t</header>
\t\t\t\t\t<ul class="actions">
\t\t\t\t\t\t<li><a href="#" class="button big alt">Magna feugiat</a></li>
\t\t\t\t\t</ul>
\t\t\t\t</div>
\t\t\t</section>
\t\t\t\t
\t\t<!-- Scripts -->
\t\t\t<script src="assets/js/jquery.min.js"></script>
\t\t\t<script src="assets/js/skel.min.js"></script>
\t\t\t<script src="assets/js/util.js"></script>
\t\t\t<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
\t\t\t<script src="assets/js/main.js"></script>
\t</body>
</html>
HTML;
        $x = new XNode($html);
        $excepted = false;
        try {
            $x->find('div');
        } catch (XParserException $e) {
            $excepted = true;
        }
        $this->equ($excepted, true);
        $divs = $x->find('div.inner')->getElements();
        $good = 3;
        $found = count($divs);
        $divsCnt = $x->getCount('div.inner');
        $this->equ($divsCnt, $found);
        $this->equ($good, $found);
        // more test for bench..
        $results = [];
        $html = new XNode($html);
        $html->find('title')->inner('New Title');
        $results[1] = $html->__toString();
        $this->equ(strpos($results[1], 'New Title') !== false, true);
        $tpl = new XNode(file_get_contents('tests/templated-retrospect/index.html'));
        foreach ($tpl('link') as $elem) {
            $elem->href = '//localhost/xparser/tests/templated-retrospect/' . $elem->href;
        }
        foreach ($tpl('img, script') as $elem) {
            $elem->src = '//localhost/xparser/tests/templated-retrospect/' . $elem->src;
        }
        $results[2] = $tpl->__toString();
        $links = count($tpl('link')->getElements());
        $imgs = count($tpl('img')->getElements());
        $scripts = count($tpl('script')->getElements());
        $sum = $links + $imgs + $scripts;
        $linksCnt = $tpl->getCount('link');
        $imgsCnt = $tpl->getCount('img');
        $scriptsCnt = $tpl->getCount('script');
        $this->equ($linksCnt, $links);
        $this->equ($imgsCnt, $imgs);
        $this->equ($scriptsCnt, $scripts);
        $pieces = count(explode('//localhost/xparser/tests/templated-retrospect/', $results[2]));
        $this->equ($sum, $pieces - 1);
    }