123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- declare(strict_types=1);
- namespace DiDom\Tests;
- use DiDom\Document;
- class SelectorTest extends TestCase
- {
- public function testTag()
- {
- $html = '
- <ul id="first">
- <li><a href="#">Item 1</a></li>
- <li><a href="#">Item 2</a></li>
- <li><a href="#">Item 3</a></li>
- </ul>
- <ol id="second">
- <li><a href="#">Item 1</a></li>
- <li><a href="#">Item 2</a></li>
- <li><a href="#">Item 3</a></li>
- </ol>
- ';
- $document = new Document($html);
- $expected = ['Item 1', 'Item 2', 'Item 3', 'Item 1', 'Item 2', 'Item 3'];
- $result = [];
- foreach ($document->find('li') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- }
- public function testNestedTag()
- {
- $html = '
- <ul id="first">
- <li><a href="#">Item 1</a></li>
- <li><a href="#">Item 2</a></li>
- <li><a href="#">Item 3</a></li>
- </ul>
- <ol id="second">
- <li><a href="#">Item 1</a></li>
- <li><a href="#">Item 2</a></li>
- <li><a href="#">Item 3</a></li>
- </ol>
- ';
- $document = new Document($html);
- $expected = ['Item 1', 'Item 2', 'Item 3'];
- $result = [];
- foreach ($document->find('ul a') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- }
- public function testDirectChild()
- {
- $html = '
- <div>
- <p><span>Lorem ipsum.</span></p>
- <span>Lorem ipsum.</span>
- </div>
- ';
- $document = new Document($html);
- $expected = ['Lorem ipsum.'];
- $result = [];
- foreach ($document->find('div > span') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- }
- public function testId()
- {
- $html = '
- <span>Lorem ipsum dolor.</span>
- <span id="second">Tenetur totam, nostrum.</span>
- <span>Iste, doloremque, praesentium.</span>
- ';
- $document = new Document($html);
- $expected = ['Tenetur totam, nostrum.'];
- $result = [];
- foreach ($document->find('#second') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- }
- public function testClass()
- {
- $html = '
- <span class="odd first">Lorem ipsum dolor.</span>
- <span class="even second">Tenetur totam, nostrum.</span>
- <span class="odd third">Iste, doloremque, praesentium.</span>
- ';
- $document = new Document($html);
- $expected = ['Lorem ipsum dolor.', 'Iste, doloremque, praesentium.'];
- $result = [];
- foreach ($document->find('.odd') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- $expected = ['Iste, doloremque, praesentium.'];
- $result = [];
- foreach ($document->find('.odd.third') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- }
- public function testAttributes()
- {
- $html = '
- <ul class="links">
- <li>
- <a href="https://foo.com" title="Foo" target="_blank">Foo</a>
- <a href="http://bar.com" title="Bar" rel="noreferrer">Bar</a>
- <a href="https://baz.org" title="Baz" rel="nofollow noreferrer">Baz</a>
- <a href="http://qux.org" title="Qux" target="_blank" rel="nofollow">Qux</a>
- </li>
- </ul>
- ';
- $document = new Document($html);
- // has attribute
- $expected = ['Foo', 'Qux'];
- $result = [];
- foreach ($document->find('a[target]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- // has no attribute
- $expected = ['Bar', 'Baz'];
- $result = [];
- foreach ($document->find('a[!target]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- // equals
- $expected = ['Baz'];
- $result = [];
- foreach ($document->find('a[href="https://baz.org"]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- // not equals
- $expected = ['Foo', 'Bar', 'Qux'];
- $result = [];
- foreach ($document->find('a[href!="https://baz.org"]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- // starts with
- $expected = ['Foo', 'Baz'];
- $result = [];
- foreach ($document->find('a[href^="https"]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- // ends with
- $expected = ['Baz', 'Qux'];
- $result = [];
- foreach ($document->find('a[href$="org"]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- // contains word
- $expected = ['Bar', 'Baz'];
- $result = [];
- foreach ($document->find('a[rel~="noreferrer"]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- $this->assertEquals([], $document->find('a[rel~="noref"]'));
- // contains substring
- $expected = ['Bar', 'Baz'];
- $result = [];
- foreach ($document->find('a[href*="ba"]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- // multiple attribute conditions
- $expected = ['Qux'];
- $result = [];
- foreach ($document->find('a[target="_blank"][rel="nofollow"]') as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expected, $result);
- }
- /**
- * @param $selector
- * @param $expectedResult
- *
- * @dataProvider containsPseudoClassTests
- */
- public function testContainsPseudoClass($selector, $expectedResult)
- {
- $html = '
- <ul class="links">
- <li>
- <a href="https://foo.com" title="Foo" target="_blank">Foo</a>
- <a href="http://bar.com" title="Bar" rel="noreferrer">Bar</a>
- <a href="https://baz.org" title="Baz" rel="nofollow noreferrer">Baz</a>
- <a href="http://qux.org" title="Qux" target="_blank" rel="nofollow">Qux</a>
- <a href="https://foobar.com" title="FooBar" target="_blank">FooBar</a>
- </li>
- </ul>
- ';
- $document = new Document($html);
- $result = [];
- foreach ($document->find($selector) as $element) {
- $result[] = $element->text();
- }
- $this->assertEquals($expectedResult, $result);
- }
- public function containsPseudoClassTests()
- {
- return [
- ['a:contains(Baz)', ['Baz']],
- ['a:contains(a)', ['Bar', 'Baz', 'FooBar']],
- ['a:contains(Bar)', ['Bar', 'FooBar']],
- ['a:contains(Bar, true, true)', ['Bar']],
- ['a:contains(bar)', []],
- ['a:contains(bar, false)', ['Bar', 'FooBar']],
- ['a:contains(bar, false, true)', ['Bar']],
- ];
- }
- public function testUnicodeSupport()
- {
- $html = '
- <ul class="links">
- <li>
- <a href="http://foo.com" title="Foo">Foo</a>
- <a href="http://example.com" title="Пример">Example</a>
- <a href="http://bar.com" title="Foo">Bar</a>
- <a href="http://example.ru" title="Example">Пример</a>
- </li>
- </ul>
- ';
- $document = new Document($html);
- $this->assertEquals('Example', $document->first('a[title=Пример]')->text());
- $this->assertEquals('Example', $document->first('a:contains(Пример)')->attr('title'));
- }
- }
|