diff --git a/controllers/Parse.php b/controllers/Parse.php index c6ce4af..586181d 100644 --- a/controllers/Parse.php +++ b/controllers/Parse.php @@ -159,15 +159,27 @@ class Parse { $xpath = new DOMXPath($doc); $found = []; - foreach($xpath->query('//a[@href]') as $href) { - $u = $href->getAttribute('href'); - - if($target) { - # target parameter was provided + if($target) { + self::xPathFindNodeWithAttribute($xpath, 'a', 'href', function($u) use($target, &$found){ if($u == $target) { $found[$u] = null; } - } + }); + self::xPathFindNodeWithAttribute($xpath, 'img', 'src', function($u) use($target, &$found){ + if($u == $target) { + $found[$u] = null; + } + }); + self::xPathFindNodeWithAttribute($xpath, 'video', 'src', function($u) use($target, &$found){ + if($u == $target) { + $found[$u] = null; + } + }); + self::xPathFindNodeWithAttribute($xpath, 'audio', 'src', function($u) use($target, &$found){ + if($u == $target) { + $found[$u] = null; + } + }); } if(!$found) { @@ -198,4 +210,11 @@ class Parse { ]); } + private static function xPathFindNodeWithAttribute($xpath, $node, $attr, $callback) { + foreach($xpath->query('//'.$node.'[@'.$attr.']') as $el) { + $v = $el->getAttribute($attr); + $callback($v); + } + } + } diff --git a/tests/ParseTest.php b/tests/ParseTest.php index 1867485..c6b2e8f 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -73,6 +73,39 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->html); } + public function testFindTargetLinkIsImage() { + $url = 'http://source.example.com/link-is-img'; + $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body); + $this->assertObjectNotHasAttribute('name', $data->data); + $this->assertEquals('This page has an img tag with the target URL.', $data->data->content->text); + } + + public function testFindTargetLinkIsVideo() { + $url = 'http://source.example.com/link-is-video'; + $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/movie.mp4']); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body); + $this->assertObjectNotHasAttribute('name', $data->data); + $this->assertEquals('This page has a video tag with the target URL.', $data->data->content->text); + } + + public function testFindTargetLinkIsAudio() { + $url = 'http://source.example.com/link-is-audio'; + $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/media.mp3']); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body); + $this->assertObjectNotHasAttribute('name', $data->data); + $this->assertEquals('This page has an audio tag with the target URL.', $data->data->content->text); + } + public function testTextContent() { $url = 'http://source.example.com/text-content'; $response = $this->parse(['url' => $url]); diff --git a/tests/data/source.example.com/link-is-audio b/tests/data/source.example.com/link-is-audio new file mode 100644 index 0000000..f5fbb6b --- /dev/null +++ b/tests/data/source.example.com/link-is-audio @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + +

This page has an audio tag with the target URL.

+ + + diff --git a/tests/data/source.example.com/link-is-img b/tests/data/source.example.com/link-is-img new file mode 100644 index 0000000..5ad51a0 --- /dev/null +++ b/tests/data/source.example.com/link-is-img @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + +

This page has an img tag with the target URL.

+ + + diff --git a/tests/data/source.example.com/link-is-video b/tests/data/source.example.com/link-is-video new file mode 100644 index 0000000..be3858c --- /dev/null +++ b/tests/data/source.example.com/link-is-video @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + +

This page has a video tag with the target URL.

+ + +