Skip to content

Commit

Permalink
Merge pull request #23 from AlanFCMV/iframe-detected
Browse files Browse the repository at this point in the history
Add IframeNotHandled rule
  • Loading branch information
cidilabs authored Dec 13, 2021
2 parents 03111f6 + b66170a commit a10a618
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Rule/IframeNotHandled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace CidiLabs\PhpAlly\Rule;

class IframeNotHandled extends BaseRule
{
private $regex = array(
'@youtube\.com/embed/([^"\&\? ]+)@i',
'@youtube\.com/v/([^"\&\? ]+)@i',
'@youtube\.com/watch\?v=([^"\&\? ]+)@i',
'@youtube\.com/\?v=([^"\&\? ]+)@i',
'@youtu\.be/([^"\&\? ]+)@i',
'@youtu\.be/v/([^"\&\? ]+)@i',
'@youtu\.be/watch\?v=([^"\&\? ]+)@i',
'@youtu\.be/\?v=([^"\&\? ]+)@i',
'@youtube-nocookie\.com/embed/([^"\&\? ]+)@i',
'@vimeo\.com/[^0-9]*([0-9]{7,9})@i',
'/(kaltura)/',
'/(dailymotion)/',
);

public function id()
{
return self::class;
}

public function check()
{
foreach ($this->getAllElements('iframe') as $iframe) {
$matches = false;

foreach ($this->regex as $search) {
if (preg_match($search, trim($iframe->getAttribute('src')))) {
$matches = true;
break;
}
}

if (!$matches) {
$this->setIssue($iframe);
}
}

return count($this->issues);
}
}
1 change: 1 addition & 0 deletions src/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"CidiLabs\\PhpAlly\\Rule\\FontIsNotUsed",
"CidiLabs\\PhpAlly\\Rule\\HeadersHaveText",
"CidiLabs\\PhpAlly\\Rule\\HeadingsInOrder",
"CidiLabs\\PhpAlly\\Rule\\IframeNotHandled",
"CidiLabs\\PhpAlly\\Rule\\ImageAltIsDifferent",
"CidiLabs\\PhpAlly\\Rule\\ImageAltIsTooLong",
"CidiLabs\\PhpAlly\\Rule\\ImageAltNotEmptyInAnchor",
Expand Down
40 changes: 40 additions & 0 deletions tests/IframeNotHandledTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use CidiLabs\PhpAlly\Rule\IframeNotHandled;

class IframeNotHandledTest extends PhpAllyTestCase {
public function testCheckEmpty()
{
$html = '<div></div>';
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$rule = new IframeNotHandled($dom);

$this->assertEquals(0, $rule->check(), 'IframeNotHandled should have no issues.');
}

public function testCheckTrueIframe()
{
$html = '<div><iframe src="https://www.thisisatest.com/"></iframe></div>';
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$rule = new IframeNotHandled($dom);

$this->assertEquals(1, $rule->check(), 'IframeNotHandled should have one issue.');
}

public function testCheckFalseIframe()
{
$html = '<div>
<iframe src="https://www.dailymotion.com/us"></iframe>
<iframe src="https://www.youtube.com/watch?v=1xZxxVlu7BM"></iframe>
<iframe src="https://vimeo.com/205755088"></iframe>
<iframe src="https://cdnapisec.kaltura.com/p/4183983"></iframe>
</div>';
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$rule = new IframeNotHandled($dom);

$this->assertEquals(0, $rule->check(), 'IframeNotHandled should have no issues.');
}
}

0 comments on commit a10a618

Please sign in to comment.