Beispiel #1
0
    /**
     * Tests base functional
     *
     * @covers CDomAttribute
     * @covers CDomAttributesList
     * @covers CDomDocument
     * @covers CDomList
     * @covers CDomNode
     * @covers CDomNodeCdata
     * @covers CDomNodeCommment
     * @covers CDomNodeDoctype
     * @covers CDomNodesList
     * @covers CDomNodeTag
     * @covers CDomNodeText
     * @covers CDomNodeXmlDeclaration
     * @covers CDomSelector
     * @covers CDom
     * @covers CLexer
     */
    function testBase()
    {
        // ----
        $dom = CDom::fromString();
        $this->checkStructure($dom);
        $this->assertEmpty($dom->nodes);
        $this->assertEquals(CDom::NODE_DOCUMENT, $dom->type);
        $dom->clear();
        // ----
        $dom = CDom::fromString($this->test_html2);
        $this->checkStructure($dom);
        $dom->clear();
        $dom = CDom::fromString($this->test_html);
        $this->checkStructure($dom);
        $dom->clear();
        $dom = CDom::fromString('<div>text</div>');
        $this->checkStructure($dom);
        $dom->clear();
        // ----
        $dom = CDom::fromString('l<a /><bR>l ');
        $this->checkStructure($dom);
        $this->assertEquals(4, count($dom->nodes));
        $this->assertEquals(2, count($dom->children));
        $this->assertEquals(2, $dom->childElementCount);
        $this->assertEquals($dom->nodes, $dom->childNodes);
        $this->assertEquals($dom->nodes, $dom->nodes());
        $a = $dom->firstChild;
        $br = $dom->lastChild;
        $this->assertNotEmpty($a);
        $this->assertNotEmpty($br);
        $this->assertNotEquals($a, $dom->firstNode);
        $this->assertNotEquals($br, $dom->lastNode);
        $this->assertTrue($a->selfClosed);
        $this->assertEquals('a', $a->name);
        $this->assertEquals('a', $a->nameReal);
        $this->assertEquals('br', $a->next->name);
        $this->assertEquals($a->next, $a->nextElementSibling);
        $this->assertEquals($a->next, $a->nextSibling);
        $this->assertTrue($br->selfClosed);
        $this->assertEquals('br', $br->name);
        $this->assertEquals('bR', $br->nameReal);
        $this->assertEquals('a', $br->prev->name);
        $this->assertEquals($br->prev, $br->previousElementSibling);
        $this->assertEquals($br->prev, $br->previousSibling);
        $this->assertEquals('', $a->html());
        $this->assertEquals('', $a->innerHtml());
        $this->assertEquals('', $a->html);
        $this->assertEquals('<a />', $a->outerHtml());
        $this->assertEquals('<a />', $a->outerHtml);
        $this->assertEquals('', $a->text());
        $this->assertEquals('', $a->text);
        $this->assertEquals('', $br->html());
        $this->assertEquals('', $br->innerHtml());
        $this->assertEquals('', $br->html);
        $this->assertEquals('<br />', $br->outerHtml());
        $this->assertEquals('<br />', $br->outerHtml);
        $this->assertEquals('', $br->text());
        $this->assertEquals('', $br->text);
        $this->assertEquals('', $br->textContent);
        $expected = 'l<a /><br />l';
        $this->assertEquals($expected, $dom->html());
        $this->assertEquals($expected, (string) $dom);
        $this->assertEquals($expected, $dom->outerHtml());
        $this->assertEquals("l\nl", $dom->text());
        $catched = false;
        try {
            $dom->unknown();
        } catch (Exception $e) {
            $this->assertTrue($e instanceof BadMethodCallException);
            $catched = true;
        }
        $this->assertTrue($catched);
        $dom->clear();
        // ----
        $dom = CDom::fromString('<?xml version="1.0" encoding="UTF-8"?><XmL:Name />');
        $this->checkStructure($dom);
        $this->assertEquals(2, count($dom->nodes));
        $this->assertEquals(1, count($dom->children));
        $this->assertEquals(1, $dom->childElementCount);
        $this->assertEquals($dom->nodes, $dom->childNodes);
        $xml = $dom->firstNode;
        $child = $dom->firstChild;
        $this->assertNotEquals($xml, $child);
        $this->assertEquals($dom->node(0), $dom->nodes[0]);
        $this->assertEquals($dom->node(-1), $dom->nodes[1]);
        $this->assertEquals(CDom::NODE_XML_DECL, $dom->nodes[0]->type);
        $this->assertEquals(CDom::NODE_XML_DECL, $xml->type);
        $this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>', $xml->outerHtml());
        $this->assertEquals('', $xml->text());
        $this->assertEquals('', $xml->html());
        $this->assertEquals(CDom::NODE_ELEMENT, $child->type);
        $this->assertEquals('xml:name', $child->name);
        $this->assertEquals('XmL:Name', $child->nameReal);
        $this->assertEquals('xml', $child->namespace);
        $this->assertEquals('name', $child->nameLocal);
        $this->assertNotEmpty($dom->child(0));
        $this->assertEmpty($dom->child(1));
        $this->assertEquals($dom->child(0), $dom->children[0]);
        $this->assertEquals($dom->child(-1), $dom->children[0]);
        $this->assertEmpty($dom->child(-2));
        $dom->clear();
        // ----
        $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        $dom = CDom::fromString($html);
        $this->checkStructure($dom);
        $this->assertEquals(1, count($dom->nodes));
        $this->assertEquals(0, count($dom->children));
        $doctype = $dom->firstNode;
        $this->assertEquals(CDom::NODE_DOCTYPE, $doctype->type);
        $this->assertEquals(CDom::NODE_DOCTYPE, $dom->nodes[0]->type);
        $this->assertEquals($html, $doctype->outerHtml());
        $this->assertEquals('', $doctype->html());
        $this->assertEquals('', $doctype->text());
        $dom->clear();
        // ----
        $html = '<b>text текст %$#@!~</b>';
        $dom = CDom::fromString($html);
        $this->checkStructure($dom);
        $this->assertEquals(1, count($dom->nodes));
        $this->assertEquals(1, count($dom->children));
        $b = $dom->firstChild;
        $this->assertNotEmpty($b);
        $this->assertEquals($b, $dom->firstNode);
        $text = $b->firstNode;
        $this->assertEmpty($b->firstChild);
        $this->assertNotEmpty($text);
        $this->assertEquals(CDom::NODE_TEXT, $text->type);
        $this->assertEquals($html, $dom->html());
        $this->assertEquals($html, $b->outerHtml());
        $expected = 'text текст %$#@!~';
        $this->assertEquals($expected, $dom->text());
        $this->assertEquals($expected, $b->text());
        $this->assertEquals($expected, $b->html());
        $this->assertEquals($expected, $b->innerHtml());
        $dom->clear();
        // ----
        $dom = CDom::fromString('  text текст  ');
        $this->checkStructure($dom);
        $this->assertEquals(1, count($dom->nodes));
        $this->assertEquals(0, count($dom->children));
        $text = $dom->firstNode;
        $this->assertEmpty($dom->firstChild);
        $this->assertNotEmpty($text);
        $this->assertEquals($text, $dom->nodes[0]);
        $this->assertEquals(CDom::NODE_TEXT, $dom->nodes[0]->type);
        $this->assertEquals(CDom::NODE_TEXT, $text->type);
        $expected = 'text текст';
        $this->assertEquals($expected, $dom->html());
        $this->assertEquals($expected, $dom->text());
        $this->assertEquals($expected, $dom->outerHtml());
        $this->assertEquals($expected, $text->html());
        $this->assertEquals($expected, $text->text());
        $this->assertEquals($expected, $text->outerHtml());
        $dom->clear();
        // ----
        $html = '<!-- comment -->';
        $dom = CDom::fromString($html);
        $this->checkStructure($dom);
        $this->assertEquals(1, count($dom->nodes));
        $this->assertEquals(0, count($dom->children));
        $comment = $dom->firstNode;
        $this->assertEmpty($dom->firstChild);
        $this->assertNotEmpty($comment);
        $this->assertEquals($comment, $dom->nodes[0]);
        $this->assertEquals(CDom::NODE_COMMENT, $dom->nodes[0]->type);
        $this->assertEquals(CDom::NODE_COMMENT, $comment->type);
        $this->assertEquals('', $dom->text());
        $this->assertEquals($html, $dom->html());
        $this->assertEquals($html, $dom->outerHtml());
        $this->assertEquals('', $comment->text());
        $this->assertEquals('', $comment->html());
        $this->assertEquals($html, $comment->outerHtml());
        $dom->clear();
        // ----
        $html = '<![CDATA[Yeah cdata section <nav>...</nav> here]]>';
        $dom = CDom::fromString($html);
        $this->checkStructure($dom);
        $this->assertEquals(1, count($dom->nodes));
        $this->assertEquals(0, count($dom->children));
        $this->assertEquals(0, $dom->childElementCount);
        $cdata = $dom->firstNode;
        $this->assertEquals($cdata->name, $cdata->nodeName);
        $this->assertEquals($cdata->value, $cdata->nodeValue);
        $this->assertEquals($cdata->type, $cdata->nodeType);
        $this->assertEmpty($dom->firstChild);
        $this->assertNotEmpty($cdata);
        $this->assertEquals($cdata, $dom->nodes[0]);
        $this->assertEquals(CDom::NODE_CDATA, $dom->nodes[0]->type);
        $this->assertEquals(CDom::NODE_CDATA, $cdata->type);
        $text = 'Yeah cdata section <nav>...</nav> here';
        $this->assertEquals($html, $dom->html());
        $this->assertEquals($text, $dom->text());
        $this->assertEquals($html, $dom->outerHtml());
        $this->assertEquals($text, $cdata->html());
        $this->assertEquals($text, $cdata->text());
        $this->assertEquals($html, $cdata->outerHtml());
        $dom->clear();
        // ----
        $html = '<br><br>';
        $dom = CDom::fromString($html);
        $this->checkStructure($dom);
        $res = $dom->find('*');
        $size = 2;
        $this->assertEquals($size, $res->length);
        $this->assertEquals($size, count($res));
        $this->assertEquals($size, sizeOf($res));
        $this->assertTrue(isset($res[0]));
        $this->assertFalse(isset($res[2]));
        $node = $res[1];
        $this->assertTrue($node->selfClosed);
        $this->assertEquals('br', $node->name);
        unset($res[1]);
        $this->assertFalse(isset($res[1]));
        $res[1] = $node;
        $this->assertEquals(1, $res->length);
        $res->add($node);
        $size = 2;
        $this->assertEquals($size, $res->length);
        $this->assertEquals($size, count($res));
        $k = 0;
        foreach ($res as $key => $node) {
            $this->assertEquals($k++, $key);
            $this->assertEquals(CDom::NODE_ELEMENT, $node->type);
            $this->assertEquals('br', $node->name);
        }
        $this->assertEquals('', $res->html());
        $this->assertEquals('', $res->text());
        $this->assertEquals('<br />', $res->outerHtml());
        $this->assertEquals('', $res->htmlAll());
        $this->assertEquals('', $res->textAll());
        $this->assertEquals('<br /><br />', $res->outerHtmlAll());
        $this->assertEquals($res->htmlAll(), (string) $res);
        $res->html('<i>');
        $this->assertEquals('<i></i>', $res->html());
        $this->assertEquals('', $res->text());
        $this->assertFalse($res->get(0)->selfClosed);
        $res->text('<i>');
        $this->assertEquals('<i>', $res->html());
        $this->assertEquals('<i>', $res->text());
        $dom->clear();
        // ----
        $html = 'text <br> text';
        $dom = CDom::fromString($html);
        $this->checkStructure($dom);
        $this->assertEquals("text\ntext", $dom->text());
        $br = $dom->firstChild;
        $this->assertEquals('br', $br->name);
        $this->assertEmpty($br->prev);
        $this->assertEmpty($br->previousElementSibling);
        $this->assertNotEmpty($br->previousSibling);
        $this->assertEmpty($br->next);
        $this->assertEmpty($br->nextElementSibling);
        $this->assertNotEmpty($br->nextSibling);
        $this->assertTrue($br->selfClosed);
        $br->text('test');
        $this->assertFalse($br->selfClosed);
        $this->assertEquals('test', $br->text());
        $this->assertEquals('test', $br->text);
        $this->assertEquals('<br>test</br>', $br->outerHtml());
        $dom->clear();
        // ----
        $dom = CDom::fromString('<tag disabled />');
        $this->checkStructure($dom);
        $this->assertEquals('disabled', (string) $dom->firstChild->disabled);
        $dom->clear();
        $dom = CDom::fromString('<tag on off />');
        $this->checkStructure($dom);
        $this->assertEquals('on', (string) $dom->firstChild->on);
        $this->assertEquals('off', (string) $dom->firstChild->off);
        $dom->clear();
        // ----
        $dom = CDom::fromString('<tag class=value test />');
        $this->checkStructure($dom);
        $this->assertEquals('value', (string) $dom->firstChild->class);
        $this->assertEquals('test', (string) $dom->firstChild->test);
        $dom->clear();
        $dom = CDom::fromString('<tag class="value test" />');
        $this->checkStructure($dom);
        $this->assertEquals('value test', (string) $dom->firstChild->class);
        $this->assertEquals('', (string) $dom->firstChild->test);
        $dom->clear();
        // ----
        $dom = CDom::fromString('<tr><td><tr><td>');
        $this->checkStructure($dom);
        $this->assertEquals(2, count($dom->nodes));
        $this->assertEquals(2, count($dom->children));
        $this->assertEquals('tr', $dom->nodes[0]->name);
        $this->assertEquals(1, count($dom->nodes[0]->nodes));
        $this->assertEquals(1, count($dom->nodes[0]->children));
        $this->assertEquals('td', $dom->nodes[0]->nodes[0]->name);
        $this->assertEquals('tr', $dom->nodes[1]->name);
        $this->assertEquals(1, count($dom->nodes[1]->nodes));
        $this->assertEquals(1, count($dom->nodes[1]->children));
        $this->assertEquals('td', $dom->nodes[1]->nodes[0]->name);
        $dom->clear();
        // ----
        $list = new CDomNodesList();
        $this->assertEquals(0, $list->length);
        $this->assertEquals($list->size(), $list->length);
        $this->assertEmpty($list->first);
        $this->assertEmpty($list->last);
        $this->assertEmpty($list->notDefined);
        $this->assertEmpty($list->text());
        $this->assertEmpty($list->html());
        $this->assertEmpty($list->outerHtml());
        $list->add(new CDomNodeTag('p'));
        $this->assertEquals(1, $list->length);
        $this->assertEquals($list->size(), $list->length);
        $this->assertNotEmpty($list->first);
        $this->assertNotEmpty($list->last);
        $this->assertEquals($list->first->uniqId, $list->last->uniqId);
        $catched = false;
        try {
            $list->notDefined();
        } catch (Exception $e) {
            $catched = true;
            $this->assertContains('is not defined', $e->getMessage());
        }
        $this->assertTrue($catched);
        $this->assertFalse($list->first->selfClosed);
        $this->assertFalse($list->selfClosed);
        $list->selfClosed = true;
        $this->assertTrue($list->first->selfClosed);
        $this->assertTrue($list->selfClosed);
        $this->assertEmpty($list->class);
        $this->assertEmpty($list->first->class);
        $list->class = 'test';
        $this->assertEquals('test', $list->class);
        $this->assertEquals('test', $list->first->class);
        // ----
        $markup = <<<'TXT'
<div class="news">



        <div class="alert push"><div class="body"><div class="title">
  <a href="/mishoo">mishoo</a>
  <span>pushed</span> to master at <a href="/mishoo/UglifyJS">mishoo/UglifyJS</a>
  <time class="js-relative-date" datetime="2011-10-17T06:37:27Z" title="2011-10-17 06:37:27">about 5 hours ago</time>
</div>
<div class="details">
  <div class="gravatar"><img src="https://secure.gravatar.com/avatar/8a966def06bd0f3e02f1af3570ec42a9?s=140&amp;d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png" alt="" width="30" height="30"></div>

  <div class="commits">
    <ul>
      <li>
        <span title="Someone"></span>
        <code><a href="/mishoo/UglifyJS/commit/845992977b5fe46335a1817947a24159a6ac3dd6">8459929</a></code>

        <div class="message">
          <blockquote title="use == instead of === in `member` (fixes #244)">
            use == instead of === in `member` (fixes <a href="https://github.com/mishoo/UglifyJS/issues/244" title="Handle ASTs with embedded tokens in member()" class="issue-link">#244</a>)
          </blockquote>
        </div>
      </li>
    </ul>
  </div>
</div>
</div></div>
        <div class="alert issues_closed"><div class="body"><div class="title">
  <a href="/mishoo">mishoo</a>
  <span>closed</span>
  <a href="/mishoo/UglifyJS/pull/244">pull request 244</a> on <a href="/mishoo/UglifyJS">mishoo/UglifyJS</a>
  <time class="js-relative-date" datetime="2011-10-17T06:37:26Z" title="2011-10-17 06:37:26">about 5 hours ago</time>
</div>
<div class="details">
  <div class="gravatar"><img src="https://secure.gravatar.com/avatar/8a966def06bd0f3e02f1af3570ec42a9?s=140&amp;d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png" alt="" width="30" height="30"></div>
  <div class="message">
    <blockquote>Handle ASTs with embedded tokens in member()</blockquote>
  </div>
</div>
</div></div>
        <div class="alert issues_opened"><div class="body"><div class="title">
  <a href="/jesserosenfield">jesserosenfield</a>
  <span>opened</span>
  <a href="/leafo/lessphp/issues/143">issue 143</a> on <a href="/leafo/lessphp">leafo/lessphp</a>
  <time class="js-relative-date" datetime="2011-10-17T03:03:47Z" title="2011-10-17 03:03:47">about 9 hours ago</time>
</div>

<div class="details">
  <div class="gravatar"><img src="https://secure.gravatar.com/avatar/ea89afd6242491a6f37a3f769a67573d?s=140&amp;d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png" alt="" width="30" height="30"></div>
  <div class="message">
    <blockquote>
      Calculated Percentages Parsed without % sign
    </blockquote>
  </div>
</div>
</div></div>
        <div class="alert issues_comment"><div class="body"><div class="title">
  <a href="/jesserosenfield">jesserosenfield</a>
  <span>commented</span> on
  <a href="/leafo/lessphp/issues/134#issuecomment-2424526">issue 134</a> on <a href="/leafo/lessphp">leafo/lessphp</a>
  <time class="js-relative-date" datetime="2011-10-17T02:56:36Z" title="2011-10-17 02:56:36">about 9 hours ago</time>
</div>

<div class="details">
  <div class="gravatar"><img src="https://secure.gravatar.com/avatar/ea89afd6242491a6f37a3f769a67573d?s=140&amp;d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png" alt="" width="30" height="30"></div>
  <div class="message">
    <blockquote title="I'm running into what I believe is a related @media issue. There is a visual inconsistency between what happens with media-queries parsed by less.js and by lessphp. As far as I can tell this should work fine:

@media all and (max-width: 40em) { ... }

But is producing a ton of visual errors and I can't figure out why. I also get the following css error from the w3c CSS validator (which I haven't been able to find any additional info on): 'max-width Property max-width doesn't exist in all but exists in all'

Currently this error is totally preventing me from using lessphp which is a shame because in theory it will cut both my site's loadtime and my workflow in half!">
      <p>I'm running into what I believe is a related <a href="https://github.com/media" class="user-mention">@media</a> issue. There is a visual inconsistency between what happens with media-queries parsed by less.js a...</p>
    </blockquote>
  </div>
</div>
</div></div>
        <div class="alert issues_comment"><div class="body"><div class="title">
  <a href="/Micoss">Micoss</a>
  <span>commented</span> on
  <a href="/facebook/php-sdk/issues/221#issuecomment-2418553">issue 221</a> on <a href="/facebook/php-sdk">facebook/php-sdk</a>
  <time class="js-relative-date" datetime="2011-10-16T01:31:15Z" title="2011-10-16 01:31:15">1 day ago</time>
</div>

<div class="details">
  <div class="gravatar"><img src="https://secure.gravatar.com/avatar/49061e8503ca321a8045c9065963a8c6?s=140&amp;d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png" alt="" width="30" height="30"></div>
  <div class="message">
    <blockquote>
      Globals in unit test
    </blockquote>
  </div>
</div>
</div></div>
        <div class="alert issues_comment"><div class="body"><div class="title">
  <a href="/ranbena">ranbena</a>
  <span>commented</span> on
  <a href="/necolas/normalize.css/pull/37#issuecomment-2379646">pull request 37</a> on <a href="/necolas/normalize.css">necolas/normalize.css</a>
  <time class="js-relative-date" datetime="2011-10-12T14:16:38Z" title="2011-10-12 14:16:38">4 days ago</time>
</div>

<div class="details">
  <div class="gravatar"><img src="https://secure.gravatar.com/avatar/ff4fc00e6f14d683b3bc5a1714619e92?s=140&amp;d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png" alt="" width="30" height="30"></div>
  <div class="message">
    <blockquote title="Hey Nicolas, I agree. In the next release, we'll remove the differentiation and use your original file.

Thanks for putting in your thoughts and taking the time to reply.

Cheers, Ran">
      <p>Hey Nicolas, I agree. In the next release, we'll remove the differentiation and use your original file. Thanks for putting in your thoughts and taking...</p>
    </blockquote>
  </div>
</div>
</div></div>


        <div class="pagination ajax_paginate"><a href="/dashboard/index/2">More</a></div>
  </div>
TXT;
        $dom = CDom::fromString($markup);
        $this->checkStructure($dom);
        $txt = $dom->text();
        $expected = <<<'TXT'
mishoo pushed to master at mishoo/UglifyJS about 5 hours ago

8459929
use == instead of === in `member` (fixes #244)

mishoo closed pull request 244 on mishoo/UglifyJS about 5 hours ago

Handle ASTs with embedded tokens in member()

jesserosenfield opened issue 143 on leafo/lessphp about 9 hours ago

Calculated Percentages Parsed without % sign

jesserosenfield commented on
issue 134 on leafo/lessphp about 9 hours ago

I'm running into what I believe is a related @media issue. There is a visual inconsistency between what happens with media-queries parsed by less.js a...

Micoss commented on
issue 221 on facebook/php-sdk 1 day ago

Globals in unit test

ranbena commented on
pull request 37 on necolas/normalize.css 4 days ago

Hey Nicolas, I agree. In the next release, we'll remove the differentiation and use your original file. Thanks for putting in your thoughts and taking...

More
TXT;
        $this->assertEquals($expected, $txt);
        $dom->clear();
        // ----
        $markup = <<<'TXT'
<style type="text/css">
	#medialand_adland_inline_div_750,#nadaviSpan,.rbcobmen { display:none !important; }
<crazy><markup>
</style>
TXT;
        $dom = CDom::fromString($markup);
        $this->checkStructure($dom);
        $this->assertEquals(0, count($dom->firstChild->nodes));
        $this->assertContains('medialand_adland_inline_div_750', $dom->firstChild->value);
        $dom->clear();
    }